UNPKG

flexbiz-server

Version:

Flexible Server

33 lines (30 loc) 10.1 kB
const rptobject=global.getModel("rptobject"),dmkh=global.getModel("customer"),utils=require("../../libs/utils"),kbmpttct=global.getModel("kbmPttct"),async=require("async"),_=require("lodash"),{calcGrid,evalute,sumByCondition}=require("../../libs/utils"),controller=require("../../controllers/controllerRPT"),rptform=global.getModel("rptform"),moment=require("moment"),numeral=require("numeral"),{getCodeChildren}=require("../../libs/utils"),getRegString=function($arrayTk$$){if($arrayTk$$)return $arrayTk$$.reg()}; module.exports=function($router$$){new controller($router$$,"pttctkho",async($req$$,$callback$$)=>{const $query$$=$req$$.query;if(!$query$$.den_ngay||!$query$$.tu_ngay||!$query$$.id_rptform&&!$query$$.form_code||!$query$$.bao_cao_theo)return $callback$$("B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u tham s\u1ed1 t\u1eeb ng\u00e0y(tu_ngay), \u0111\u1ebfn ng\u00e0y(den_ngay), m\u1eabu (id_rptform), \u0110\u1ed1i t\u01b0\u1ee3ng b\u00e1o c\u00e1o(bao_cao_theo)");if(!global.mongoose.Types.ObjectId.isValid($query$$.id_rptform))return $callback$$("M\u1eabu b\u00e1o c\u00e1o n\u00e0y kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb"); if($query$$.ma_bp){var $bps_dts_phis_query_form$$=await getCodeChildren(global.getModel("dmbp"),$query$$.id_app,$query$$.ma_bp,"ma_bp","ma_bp_me");$query$$.ma_bp={$in:$bps_dts_phis_query_form$$}}$query$$.ma_phi&&($bps_dts_phis_query_form$$=await getCodeChildren(global.getModel("dmphi"),$query$$.id_app,$query$$.ma_phi,"ma_phi","ma_phi_me"),$query$$.ma_phi={$in:$bps_dts_phis_query_form$$});$query$$.ma_dt&&($bps_dts_phis_query_form$$=await getCodeChildren(global.getModel("dmdt"),$query$$.id_app,$query$$.ma_dt, "ma_dt","ma_dt_me"),$query$$.ma_dt={$in:$bps_dts_phis_query_form$$});$bps_dts_phis_query_form$$={id_app:$query$$.id_app};$query$$.id_rptform?$bps_dts_phis_query_form$$._id=$query$$.id_rptform:$bps_dts_phis_query_form$$.form_code=$query$$.form_code;const $_rptform$$=await rptform.findOne($bps_dts_phis_query_form$$).lean();if(!$_rptform$$)return $callback$$("M\u1eabu b\u00e1o c\u00e1o n\u00e0y kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb");$query$$.tu_ngay=moment(new Date($query$$.tu_ngay)).startOf("date").toDate(); $query$$.den_ngay=moment(new Date($query$$.den_ngay)).endOf("date").toDate();const $bao_cao_theo$$=$query$$.bao_cao_theo;let $report$$=$_rptform$$.report;$report$$&&$_rptform$$.report.length!==0||($report$$=await kbmpttct.find({id_rptform:$_rptform$$._id.toString(),id_app:$query$$.id_app}).sort({stt:1}).lean());async.parallel({dsobj:function($callback$$){if($bao_cao_theo$$=="thang_nam"){for(var $ds_quy_nam_ds_thang_nam$$=[],$currentDate_currentDate$$=new Date($query$$.tu_ngay);$currentDate_currentDate$$<= $query$$.den_ngay;){var $current_quarter_tu_ngay$$=Math.max(moment($currentDate_currentDate$$).startOf("month").toDate(),$query$$.tu_ngay),$den_ngay_tu_ngay$$=Math.min(moment($currentDate_currentDate$$).endOf("month").toDate(),$query$$.den_ngay);$ds_quy_nam_ds_thang_nam$$.push({field:"ngay_ct",ten_obj:`Th\u00e1ng ${$currentDate_currentDate$$.getMonth()+1}/${$currentDate_currentDate$$.getFullYear()}`,ma_obj:{$gte:$current_quarter_tu_ngay$$,$lte:$den_ngay_tu_ngay$$}});$currentDate_currentDate$$.setMonth($currentDate_currentDate$$.getMonth()+ 1)}Logger.info($bao_cao_theo$$,$ds_quy_nam_ds_thang_nam$$);return $callback$$(null,$ds_quy_nam_ds_thang_nam$$)}if($bao_cao_theo$$=="quy_nam"){$ds_quy_nam_ds_thang_nam$$=[];for($currentDate_currentDate$$=new Date($query$$.tu_ngay);$currentDate_currentDate$$<=$query$$.den_ngay;){$current_quarter_tu_ngay$$=moment($currentDate_currentDate$$).quarter();$den_ngay_tu_ngay$$=Math.max(moment($currentDate_currentDate$$).startOf("quarter").toDate(),$query$$.tu_ngay);let $den_ngay$$=Math.min(moment($currentDate_currentDate$$).endOf("quarter").toDate(), $query$$.den_ngay);$ds_quy_nam_ds_thang_nam$$.push({field:"ngay_ct",ten_obj:`Qu\u00fd ${$current_quarter_tu_ngay$$}/${$currentDate_currentDate$$.getFullYear()}`,ma_obj:{$gte:$den_ngay_tu_ngay$$,$lte:$den_ngay$$}});$currentDate_currentDate$$=moment($currentDate_currentDate$$).quarter($current_quarter_tu_ngay$$+1).toDate()}Logger.info($bao_cao_theo$$,$ds_quy_nam_ds_thang_nam$$);return $callback$$(null,$ds_quy_nam_ds_thang_nam$$)}rptobject.findOne({code_rpt:"pttctkho",ma_obj:$bao_cao_theo$$}).lean().then(async function($rptobj$$){if(!$rptobj$$)return $callback$$("Kh\u00f4ng th\u1ec3 b\u00e1o c\u00e1o theo \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y"); let $modelBaocaotheo$$=global.getModel(""+$rptobj$$.model_obj),$query_ds_bc_theo$$={id_app:$query$$.id_app,status:!0};$query$$[$bao_cao_theo$$]&&_.has($modelBaocaotheo$$.schema.paths,$bao_cao_theo$$)&&($query_ds_bc_theo$$[$bao_cao_theo$$]=$query$$[$bao_cao_theo$$]);$query$$.phu_trach&&_.has($modelBaocaotheo$$.schema.paths,"phu_trach")&&($query_ds_bc_theo$$.phu_trach=$query$$.phu_trach);if($query$$.nh_kh&&!$query$$.ma_kh&&$rptobj$$.model_obj=="customer"){let $dskh$$=(await dmkh.find({id_app:$query$$.id_app, status:!0,nh_kh:$query$$.nh_kh},{ma_kh:1})).map($r$$=>$r$$.ma_kh);$query_ds_bc_theo$$.ma_kh={$in:$dskh$$}}$modelBaocaotheo$$.find($query_ds_bc_theo$$).lean().then($rs$$=>{$rs$$.forEach(function($r$$){$r$$.ma_obj=$r$$[$rptobj$$.value_field];$r$$.ten_obj=$r$$[$rptobj$$.header_field]});$callback$$(null,$rs$$)}).catch($e$$=>{$callback$$($e$$)})}).catch($e$$=>{$callback$$($e$$)})}},function($e$$,$rs$$){if($e$$)return $callback$$($e$$);let $ds_obj$$=$rs$$.dsobj;async.map($report$$,function($r$$,$c1$$){setImmediate(()=> {if($r$$.cach_tinh==="9")$r$$.cong_thuc="",async.map($ds_obj$$,function($kho$$,$callback$$){utils.getInfoFromQuery($kho$$,$r$$.query,($e$$,$rs$$)=>{if($e$$)return $callback$$($e$$);_.isNumber($rs$$)?($r$$[$kho$$.ma_obj]=$rs$$,$r$$[$kho$$.ma_obj+"_nt"]=$rs$$):$r$$[$kho$$.ma_obj+"_string"]=$rs$$;$callback$$()})},$e$$=>{$c1$$($e$$,$r$$)});else if($r$$.cach_tinh==="2")$r$$.cong_thuc="",async.map($ds_obj$$,function($kho$$,$callback$$){setImmediate(async()=>{let $condition$$={ngay_ct:{$gte:$query$$.tu_ngay, $lte:$query$$.den_ngay},id_app:$query$$.id_app};$bao_cao_theo$$.toLowerCase()==="ma_kh"?$condition$$.$or=[{ma_kh_co:$kho$$.ma_obj},{ma_kh_no:$kho$$.ma_obj}]:$condition$$[$kho$$.field||$bao_cao_theo$$]=$kho$$.ma_obj;$query$$.ma_dvcs&&($condition$$.ma_dvcs=$query$$.ma_dvcs);$bao_cao_theo$$!="ma_bp"&&$query$$.ma_bp&&($condition$$.ma_bp=$query$$.ma_bp);$query$$.ma_hd&&($condition$$.ma_hd=$query$$.ma_hd);$bao_cao_theo$$!="ma_dt"&&$query$$.ma_dt&&($condition$$.ma_dt=$query$$.ma_dt);$query$$.ma_sp&&($condition$$.ma_sp= $query$$.ma_sp);$query$$.ma_nv&&($condition$$.ma_nv=$query$$.ma_nv);$bao_cao_theo$$!="ma_phi"&&$r$$.ma_phi&&($condition$$.ma_phi={$in:await getCodeChildren(global.getModel("dmphi"),$query$$.id_app,$r$$.ma_phi,"ma_phi","ma_phi_me")});var $tk_co_tk_no$$=getRegString($r$$.tk_no);$tk_co_tk_no$$&&($condition$$.tk_no=$r$$.giam_tru_no?{$not:new RegExp($tk_co_tk_no$$)}:{$regex:$tk_co_tk_no$$,$options:"i"});if($tk_co_tk_no$$=getRegString($r$$.tk_co))$condition$$.tk_co=$r$$.giam_tru_co&&$tk_co_tk_no$$?{$not:new RegExp($tk_co_tk_no$$)}: {$regex:$tk_co_tk_no$$,$options:"i"};if($r$$.dieu_kien_khac)try{$r$$.dieu_kien_khac.indexOf("{")<0&&($r$$.dieu_kien_khac="{"+$r$$.dieu_kien_khac+"}");var $socai_str_func$$=$r$$.dieu_kien_khac;$socai_str_func$$.indexOf("return ")<0&&($socai_str_func$$=`return ${$socai_str_func$$}`);let $dieu_kien_khac$$=utils.evalute($socai_str_func$$,{moment,condition:$condition$$,row:$r$$});$condition$$={...$condition$$,...$dieu_kien_khac$$}}catch($e$$){$callback$$($e$$.toString())}$r$$.ma_kho?($condition$$.ma_kho= $r$$.ma_kho,$socai_str_func$$=global.getModel("socaitc")):$socai_str_func$$=global.getModel("socaitt");sumByCondition($socai_str_func$$,["tien","tien_nt"],$condition$$).then($rs$$=>{let $t_tien$$=$rs$$.tien;$rs$$=$rs$$.tien_nt;if($t_tien$$||$rs$$)$r$$[$kho$$.ma_obj]=$t_tien$$,$r$$[$kho$$.ma_obj+"_nt"]=$rs$$,$kho$$.has_data=!0;$callback$$(null,$r$$)}).catch($e$$=>{$callback$$($e$$)})})},function($e$$){$c1$$($e$$)});else if($r$$.cach_tinh==="99"&&$r$$.script){$r$$.cong_thuc="";if($r$$.script.indexOf("async ")>= 0)return $c1$$("async function is not allow");let $func_string$$=`return (async ()=>{ try{ ${$r$$.script} }catch(e){ return {error:e} } })`;evalute($func_string$$,{row:$r$$,moment,condition:$query$$,user:$req$$.user,_,utils})().then($rs$$=>{if($rs$$&&$rs$$.error)return Logger.error($rs$$.error,$func_string$$),$c1$$($rs$$.error.message||$rs$$.error);$c1$$(null,$r$$)}).catch($e$$=>{Logger.error($e$$,$func_string$$);return $c1$$($e$$.message||$e$$)})}else $c1$$(null,$r$$)})},function($error$$){if($error$$)return $callback$$($error$$);let $kho_cols$$="";$ds_obj$$=$ds_obj$$.filter($o$$=>$o$$.has_data);$ds_obj$$.forEach(function($kho$$){$kho_cols$$= $kho_cols$$?$kho_cols$$+","+$kho$$.ma_obj+","+$kho$$.ma_obj+"_nt":$kho$$.ma_obj+","+$kho$$.ma_obj+"_nt";$kho$$.field=$kho$$.ma_obj;$kho$$.header=$kho$$.ten_obj;$kho$$.datatype="Number"});calcGrid($report$$,$kho_cols$$,async function($rs$$){let $_ds_obj$$=[];$ds_obj$$.forEach(function($kho$$){let $v$$=0;$rs$$&&$rs$$.forEach(function($r$$){$v$$+=Math.abs($r$$[$kho$$.ma_obj]||0)});$v$$&&$_ds_obj$$.push($kho$$)});$rs$$.forEach(function($r$$){$r$$.khong_tinh_tong||($r$$.total=0,$r$$.total_nt=0,$ds_obj$$.forEach(function($kho$$){$r$$.total+= $r$$[$kho$$.ma_obj]?$r$$[$kho$$.ma_obj]:0;$r$$.total_nt+=$r$$[$kho$$.ma_obj+"_nt"]?$r$$[$kho$$.ma_obj+"_nt"]:0}))});if($_rptform$$.handle_data_expression_server){if($_rptform$$.handle_data_expression_server.indexOf("async ")>=0)return $callback$$("async function is not allow");let $func_string$$=`return (async ()=>{ try{ ${$_rptform$$.handle_data_expression_server} }catch(e){ return {error:e} } })`;try{if(($rs$$=await utils.evalute($func_string$$,{data:$rs$$,moment,numeral,condition:$req$$.query})())&&$rs$$.error)return Logger.error($rs$$.error,$func_string$$),$callback$$($rs$$.error.message||$rs$$.error)}catch($e$$){return Logger.error($e$$,$func_string$$),$callback$$($e$$.message||$e$$)}}$callback$$(null,{rows:$rs$$.filter($r$$=>$r$$.print!==!1),columns:$_ds_obj$$})})})})})};