UNPKG

flexbiz-server

Version:

Flexible Server

6 lines (5 loc) 1.86 kB
const model=global.getModel("templaterpt"),options=global.getModel("options"),controller=require("../../controllers/controller"),fs=require("fs"),path=require("path"); module.exports=function($router$$){(new controller($router$$,model,"templaterpt",{sort:{ma_cn:1},notNeedRight:($user$$,$options$$={})=>($options$$.action||"").toLowerCase()=="view"?!0:!1,onFound:($user$$,$c$$,$obj4view$$,$callback$$)=>{$c$$.ma_cn&&$obj4view$$.length===0?(new Promise($resole$$=>{options.findOne({id_app:$user$$.current_id_app,id_func:{$regex:$c$$.ma_cn,$options:"i"}}).lean().exec(($e$$,$op$$)=>{let $templateDefault$$;$op$$&&$op$$.option&&$op$$.option.system&&$op$$.option.system.templateRpt&& ($templateDefault$$=$op$$.option.system.templateRpt);$resole$$($templateDefault$$)})})).then($templateDefault$$=>new Promise($resole$$=>{if($templateDefault$$)return Logger.info("get default rpt from options"),$resole$$($templateDefault$$);let $df_file_tmp$$=`${path.dirname(path.dirname(__dirname))}/templates/rpts/${$c$$.ma_cn.toLowerCase()}.html`;if(!fs.existsSync($df_file_tmp$$)&&($df_file_tmp$$=`${path.dirname(path.dirname(__dirname))}/templates/rpts/reports/${$c$$.ma_cn.toLowerCase()}/templates/print.html`, !fs.existsSync($df_file_tmp$$)&&($df_file_tmp$$=`${path.dirname(path.dirname(__dirname))}/templates/rpts/vouchers/${$c$$.ma_cn.toLowerCase()}/templates/print.html`,!fs.existsSync($df_file_tmp$$))))return $resole$$(null);fs.readFile($df_file_tmp$$,"utf8",($err$$,$str$$)=>{Logger.info("get default rpt from file");$resole$$($str$$)})})).then($str$$=>{$str$$?(new model({ma_cn:$c$$.ma_cn,ten_mau_in:"M\u1eabu m\u1eb7c \u0111\u1ecbnh",id_app:$user$$.current_id_app,html_code:$str$$,options:{}})).save(($e$$, $rs$$)=>{$rs$$&&$obj4view$$.push($rs$$.toObject());$callback$$(null,$obj4view$$)}):$callback$$(null,$obj4view$$)}):$callback$$(null,$obj4view$$)}})).route()};