flexbiz-server
Version:
Flexible Server
29 lines (26 loc) • 8.27 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,evalute}=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$jscomp$0$$)=>{const $query$$=$req$$.query;if(!($query$$.den_ngay&&$query$$.tu_ngay&&$query$$.id_rptform&&$query$$.bao_cao_theo))return $callback$jscomp$0$$("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$0$$("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});if(!$_rptform$$)return $callback$jscomp$0$$("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$$&&0!==$_rptform$$.report.length||($report$$=await kbmpttct.find({id_rptform:$_rptform$$._id.toString(),
id_app:$query$$.id_app}).sort({stt:1}).lean());async.series({dsobj:function($callback$$){rptobject.findOne({code_rpt:"pttctkho",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$$).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]});$callback$$(null,$rs$$)})})}},function($e$jscomp$0$$,$rs$jscomp$0$$){if($e$jscomp$0$$)return $callback$jscomp$0$$($e$jscomp$0$$);let $ds_obj$$=$rs$jscomp$0$$.dsobj;async.mapSeries($report$$,function($r$$,$c1$$){setImmediate(()=>{if("9"===$r$$.cach_tinh)$r$$.cong_thuc="",async.mapSeries($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("2"===$r$$.cach_tinh)$r$$.cong_thuc="",async.mapSeries($ds_obj$$,function($kho$$,$callback$$){setImmediate(async()=>{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);"ma_dt"!=$bao_cao_theo$$&&$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);"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$$);$error$$=
$rs$$.csum("tien");$rs$$=$rs$$.csum("tien_nt");if($error$$||$rs$$)$r$$[$kho$$.ma_obj]=$error$$,$r$$[$kho$$.ma_obj+"_nt"]=$rs$$,$kho$$.has_data=!0;$callback$$(null,$r$$)})})},function($e$$){$c1$$($e$$)});else if("99"===$r$$.cach_tinh&&$r$$.script){$r$$.cong_thuc="";if(0<=$r$$.script.indexOf("async "))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 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$$)})},function($error$$){if($error$$)return $callback$jscomp$0$$($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"});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(0<=$_rptform$$.handle_data_expression_server.indexOf("async "))return $callback$jscomp$0$$("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 console.error($rs$$.error,$func_string$$),$callback$jscomp$0$$($rs$$.error.message||$rs$$.error)}catch($e$$){return console.error($e$$,$func_string$$),$callback$jscomp$0$$($e$$.message||$e$$)}}$callback$jscomp$0$$(null,{rows:$rs$$.filter($r$$=>!1!==$r$$.print),columns:$_ds_obj$$})})})})})};