flexbiz-server
Version:
Flexible Server
29 lines (26 loc) • 8.5 kB
JavaScript
'use strict';const socai=global.getModel("socai"),rptobject=global.getModel("rptobject"),dmkh=global.getModel("customer"),utils=require("../../libs/utils"),kbmpttct=global.getModel("kbmPttct"),async=require("async"),_=require("lodash"),{calcGrid}=require("../../libs/utils"),controller=require("../../controllers/controllerRPT"),moment=require("moment"),numeral=require("numeral"),{getCodeChildren}=require("../../libs/utils"),rptform=global.getModel("rptform"),getRegString=function($arrayTk$$){if($arrayTk$$)return $arrayTk$$.reg()};
module.exports=function($router$$){new controller($router$$,"pttctkho2",async function($req$$,$callback$jscomp$2$$){const $query$$=$req$$.query;if(!($query$$.den_ngay&&$query$$.tu_ngay&&$query$$.id_rptform&&$query$$.bao_cao_theo))return $callback$jscomp$2$$("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$jscomp$2$$("M\u1eabu b\u00e1o c\u00e1o n\u00e0y kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb");
if($query$$.ma_bp){var $bps_dts_phis$$=await getCodeChildren(global.getModel("dmbp"),$query$$.id_app,$query$$.ma_bp,"ma_bp","ma_bp_me");$query$$.ma_bp={$in:$bps_dts_phis$$}}$query$$.ma_phi&&($bps_dts_phis$$=await getCodeChildren(global.getModel("dmphi"),$query$$.id_app,$query$$.ma_phi,"ma_phi","ma_phi_me"),$query$$.ma_phi={$in:$bps_dts_phis$$});$query$$.ma_dt&&($bps_dts_phis$$=await getCodeChildren(global.getModel("dmdt"),$query$$.id_app,$query$$.ma_dt,"ma_dt","ma_dt_me"),$query$$.ma_dt={$in:$bps_dts_phis$$});
const $_rptform$$=await rptform.findOne({_id:$query$$.id_rptform,id_app:$query$$.id_app}).lean();if(!$_rptform$$)return $callback$jscomp$2$$("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 $kbm$$=$_rptform$$.report;$kbm$$&&0!==$_rptform$$.report.length||($kbm$$=await kbmpttct.find({id_rptform:$_rptform$$._id.toString(),
id_app:$query$$.id_app}).lean().sort({stt:1}));async.series({dsobj:function($callback$$){rptobject.findOne({code_rpt:"pttctkho2",ma_obj:$bao_cao_theo$$}).lean().exec(async function($e_modelBaocaotheo$$,$rptobj$$){if($e_modelBaocaotheo$$)return $callback$$($e_modelBaocaotheo$$);if(!$rptobj$$)return $callback$$("Kh\u00f4ng th\u1ec3 b\u00e1o c\u00e1o theo \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y");$e_modelBaocaotheo$$=global.getModel(""+$rptobj$$.model_obj);let $query_ds_bc_theo$$={id_app:$query$$.id_app,
status:!0};$query$$[$bao_cao_theo$$]&&_.has($e_modelBaocaotheo$$.schema.paths,$bao_cao_theo$$)&&($query_ds_bc_theo$$[$bao_cao_theo$$]=$query$$[$bao_cao_theo$$]);$query$$.phu_trach&&_.has($e_modelBaocaotheo$$.schema.paths,"phu_trach")&&($query_ds_bc_theo$$.phu_trach=$query$$.phu_trach);if($query$$.nh_kh&&!$query$$.ma_kh&&"customer"==$rptobj$$.model_obj){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$$}}$e_modelBaocaotheo$$.find($query_ds_bc_theo$$,
{[$rptobj$$.value_field]:1,[$rptobj$$.header_field]:1}).lean().exec(function($e$$,$rs$$){if($e$$)return $callback$$($e$$);$rs$$.forEach(function($r$$){$r$$.ma_obj=$r$$[$rptobj$$.value_field];$r$$.ten_obj=$r$$[$rptobj$$.header_field];let $_kbm$$=_.cloneDeep($kbm$$);$r$$.kbm=$_kbm$$});$callback$$(null,$rs$$)})})}},function($e$jscomp$1$$,$rs$jscomp$0$$){if($e$jscomp$1$$)return $callback$jscomp$2$$($e$jscomp$1$$);let $ds_obj$$=$rs$jscomp$0$$.dsobj;async.mapSeries($ds_obj$$,($kho$$,$callback$jscomp$1$$)=>
{setImmediate(()=>{async.mapSeries($kho$$.kbm,function($r$$,$callback$jscomp$0$$){setImmediate(()=>{async.series({tinh_theo_query:$callback$$=>{"9"===$r$$.cach_tinh&&$r$$.query?($r$$.cong_thuc="",utils.getInfoFromQuery($kho$$,$r$$.query,($e$$,$rs$$)=>{if($e$$)return $callback$$($e$$);_.isNumber($rs$$)?($r$$.tien=$rs$$,$r$$.tien_nt=$rs$$):$r$$.value_string=$rs$$;$callback$$()})):$callback$$()},tinh_theo_socai:$callback$$=>{setImmediate(async()=>{if("2"===$r$$.cach_tinh){$r$$.cong_thuc="";let $condition$$=
{ngay_ct:{$gte:$query$$.tu_ngay,$lte:$query$$.den_ngay},id_app:$query$$.id_app};"ma_kh"===$bao_cao_theo$$.toLowerCase()?$condition$$.$or=[{ma_kh_co:$kho$$.ma_obj},{ma_kh_no:$kho$$.ma_obj}]:$condition$$[$bao_cao_theo$$]=$kho$$.ma_obj;$query$$.ma_dvcs&&($condition$$.ma_dvcs=$query$$.ma_dvcs);"ma_bp"!=$bao_cao_theo$$&&$query$$.ma_bp&&($condition$$.ma_bp=$query$$.ma_bp);$query$$.ma_hd&&($condition$$.ma_hd=$query$$.ma_hd);$query$$.ma_sp&&($condition$$.ma_sp=$query$$.ma_sp);$query$$.ma_nv&&($condition$$.ma_nv=
$query$$.ma_nv);"ma_dt"!=$bao_cao_theo$$&&$query$$.ma_dt&&($condition$$.ma_dt=$query$$.ma_dt);"ma_phi"!=$bao_cao_theo$$&&$r$$.ma_phi&&($condition$$.ma_phi={$in:await getCodeChildren(global.getModel("dmphi"),$query$$.id_app,$r$$.ma_phi,"ma_phi","ma_phi_me")});$r$$.ma_kho&&($condition$$.ma_kho=$r$$.ma_kho);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{0>$r$$.dieu_kien_khac.indexOf("{")&&($r$$.dieu_kien_khac="{"+$r$$.dieu_kien_khac+"}");let $str_func$$=$r$$.dieu_kien_khac;0>$str_func$$.indexOf("return ")&&($str_func$$=`return ${$str_func$$}`);let $dieu_kien_khac$$=utils.evalute($str_func$$,{moment,condition:$condition$$,row:$r$$});$condition$$={...$condition$$,...$dieu_kien_khac$$}}catch($e$$){$callback$$($e$$.toString())}socai.find($condition$$,
{tien:1,tien_nt:1}).lean().exec(function($error$$,$rs$$){if($error$$)return $callback$$($error$$);$r$$.tien=$rs$$.csum("tien");$r$$.tien_nt=$rs$$.csum("tien_nt");$callback$$(null,$r$$)})}else $callback$$()})},calc_expression:$c1$$=>{if("99"===$r$$.cach_tinh&&$r$$.script){if(0<=$r$$.script.indexOf("async "))return $c1$$("async function is not allow");$r$$.cong_thuc="";let $func_string$$=`return (async ()=>{
try{
${$r$$.script}
}catch(e){
return {error:e}
}
})`;utils.evalute($func_string$$,{row:$r$$,moment,condition:$query$$,user:$req$$.user,_,utils})().then($rs$$=>{if($rs$$&&$rs$$.error)return console.error($rs$$.error,$func_string$$),$c1$$($rs$$.error.message||$rs$$.error);$c1$$(null,$r$$)}).catch($e$$=>{console.error($e$$,$func_string$$);return $c1$$($e$$.message||$e$$)})}else $c1$$(null,$r$$)}},($e$$,$rs$$)=>{$callback$jscomp$0$$($e$$,$rs$$)})})},$error$$=>{if($error$$)return $callback$jscomp$1$$($error$$);calcGrid($kho$$.kbm,"tien,tien_nt",
function($rs$$){$kho$$.kbm=$rs$$;$callback$jscomp$1$$(null,$rs$$)})})})},async $e$jscomp$0$$=>{$ds_obj$$=$ds_obj$$.filter($obj$$=>0!=($obj$$.kbm||[]).map($k$$=>Math.abs($k$$.tien||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0));$ds_obj$$.forEach($obj$$=>{$obj$$.kbm.forEach($k$$=>{$obj$$[`${$k$$.ma_so}_tien`]=$k$$.tien||0;$obj$$[`${$k$$.ma_so}_tien_nt`]=$k$$.tien_nt||0})});let $tong_cong$$={ten_obj:"T\u1ed5ng c\u1ed9ng",bold:!0,kbm:$kbm$$};$kbm$$.filter($k$$=>0==$k$$.khong_tinh_tong).forEach($k$$=>{let $tien$$=
0,$tien_nt$$=0;$ds_obj$$.forEach($obj$$=>{$tien$$+=$obj$$[`${$k$$.ma_so}_tien`];$tien_nt$$+=$obj$$[`${$k$$.ma_so}_tien_nt`]});$tong_cong$$[`${$k$$.ma_so}_tien`]=$tien$$;$tong_cong$$[`${$k$$.ma_so}_tien_nt`]=$tien_nt$$});$ds_obj$$.push($tong_cong$$);if($_rptform$$.handle_data_expression_server){if(0<=$_rptform$$.handle_data_expression_server.indexOf("async "))return $callback$jscomp$2$$("async function is not allow");let $func_string$$=`return (async ()=>{
try{
${$_rptform$$.handle_data_expression_server}
}catch(e){
return {error:e}
}
})`;try{let $rs$$=await utils.evalute($func_string$$,{data:$ds_obj$$,moment,numeral,condition:$req$$.query})();if($rs$$&&$rs$$.error)return console.error($rs$$.error,$func_string$$),$callback$jscomp$2$$($rs$$.error.message||$rs$$.error);$ds_obj$$=$rs$$}catch($e$$){return console.error($e$$,$func_string$$),$callback$jscomp$2$$($e$$.message||$e$$)}}$callback$jscomp$2$$($e$jscomp$0$$,{rows:$ds_obj$$,columns:$kbm$$.filter($r$$=>!1!==$r$$.print)})})})})};