flexbiz-server
Version:
Flexible Server
30 lines (27 loc) • 8.65 kB
JavaScript
const rptobject=global.getModel("rptobject"),dmkh=global.getModel("customer"),utils=require("../../libs/utils"),kbmpttct=global.getModel("kbmPttct"),async=require("async"),_=require("lodash"),{calcGrid,getCodeChildren,sumByCondition}=require("../../libs/utils"),controller=require("../../controllers/controllerRPT"),moment=require("moment"),numeral=require("numeral"),rptform=global.getModel("rptform"),getRegString=function($arrayTk$$){if($arrayTk$$)return $arrayTk$$.reg()};
module.exports=function($router$$){new controller($router$$,"pttctkho2",async function($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 kh\u00f4ng t\u1ed3n t\u1ea1i");$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$$&&$_rptform$$.report.length!==0||($kbm$$=await kbmpttct.find({id_rptform:$_rptform$$._id.toString(),id_app:$query$$.id_app}).lean().sort({stt:1}));async.parallel({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&&$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$$}}$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$$,$rs$$){if($e$$)return $callback$$($e$$);let $ds_obj$$=$rs$$.dsobj;async.map($ds_obj$$,($kho$$,$callback$$)=>{setImmediate(()=>{async.map($kho$$.kbm,function($r$$,$callback$$){$r$$.field=$r$$[`${$r$$.ma_so}_tien`];$r$$.header=$r$$.chi_tieu;$r$$.datatype="Number";setImmediate(()=>{async.parallel({tinh_theo_query:$callback$$=>{$r$$.cach_tinh==="9"&&$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($r$$.cach_tinh==="2"){$r$$.cong_thuc="";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$$[$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);$query$$.ma_sp&&($condition$$.ma_sp=$query$$.ma_sp);$query$$.ma_nv&&($condition$$.ma_nv=$query$$.ma_nv);$bao_cao_theo$$!="ma_dt"&&$query$$.ma_dt&&($condition$$.ma_dt=$query$$.ma_dt);$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$$=>
{$r$$.tien=$rs$$.tien;$r$$.tien_nt=$rs$$.tien_nt;$callback$$(null,$r$$)}).catch($e$$=>{$callback$$($e$$)})}else $callback$$()})},calc_expression:$c1$$=>{if($r$$.cach_tinh==="99"&&$r$$.script){if($r$$.script.indexOf("async ")>=0)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 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$$)}},($e$$,$rs$$)=>{$callback$$($e$$,$rs$$)})})},$error$$=>{if($error$$)return $callback$$($error$$);calcGrid($kho$$.kbm,"tien,tien_nt",function($rs$$){$kho$$.kbm=
$rs$$;$callback$$(null,$rs$$)})})})},async $e$$=>{$ds_obj$$=$ds_obj$$.filter($obj$$=>($obj$$.kbm||[]).map($k$$=>Math.abs($k$$.tien||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0)!=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$$=>$k$$.khong_tinh_tong==0).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($_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{let $rs$$=await utils.evalute($func_string$$,{data:$ds_obj$$,moment,numeral,condition:$req$$.query})();if($rs$$&&$rs$$.error)return Logger.error($rs$$.error,$func_string$$),$callback$$($rs$$.error.message||$rs$$.error);$ds_obj$$=$rs$$}catch($e$$){return Logger.error($e$$,$func_string$$),$callback$$($e$$.message||$e$$)}}$callback$$($e$$,{rows:$ds_obj$$,columns:$kbm$$.filter($r$$=>$r$$.print!==!1)})})})})};