flexbiz-server
Version:
Flexible Server
25 lines (23 loc) • 8.46 kB
JavaScript
const kbmpttct=global.getModel("kbmPttct"),rptform=global.getModel("rptform"),vatvao=global.getModel("vatvao"),vatra=global.getModel("vatra"),sokho=global.getModel("sokho"),async=require("async"),_=require("lodash"),{calcGrid,evalute}=require("../../libs/utils"),utils=require("../../libs/utils"),moment=require("moment"),controller=require("../../controllers/controllerRPT"),{getCodeChildren,groupByCondition}=require("../../libs/utils"),getRegString=function($arrayTk$$){return $arrayTk$$?$arrayTk$$.reg():
null};
module.exports=function($router$$){new controller($router$$,"ptcttct",async function($req$$,$callback$$){let $query$$=$req$$.query;if(!$query$$.den_ngay||!$query$$.tu_ngay||!$query$$.id_rptform&&!$query$$.form_code)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), id m\u1eabu b\u00e1o c\u00e1o");if($query$$.id_rptform&&!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 $_rptform_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:$_rptform_bps_dts_phis_query_form$$}}$query$$.ma_phi&&($_rptform_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:$_rptform_bps_dts_phis_query_form$$});$query$$.ma_dt&&($_rptform_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:$_rptform_bps_dts_phis_query_form$$});$_rptform_bps_dts_phis_query_form$$={id_app:$query$$.id_app};$query$$.id_rptform?$_rptform_bps_dts_phis_query_form$$._id=$query$$.id_rptform:$_rptform_bps_dts_phis_query_form$$.form_code=$query$$.form_code;$_rptform_bps_dts_phis_query_form$$=await rptform.findOne($_rptform_bps_dts_phis_query_form$$).lean();if(!$_rptform_bps_dts_phis_query_form$$)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();let $report$$=$_rptform_bps_dts_phis_query_form$$.report;$report$$&&$_rptform_bps_dts_phis_query_form$$.report.length!==0||($report$$=await kbmpttct.find({id_rptform:$_rptform_bps_dts_phis_query_form$$._id.toString()}).sort({stt:1}).lean());async.map($report$$,function($r$$,$c1$$){setImmediate(()=>{$r$$.stt_child=0;if($r$$.cach_tinh==="9")$r$$.cong_thuc="",utils.getInfoFromQuery($query$$,
$r$$.query,($e$$,$rs$$)=>{if($e$$)return $c1$$();_.isNumber($rs$$)?($r$$.so_kn=$rs$$,$r$$.so_kn_nt=$rs$$):$r$$.value_string=$rs$$;$c1$$()});else if($r$$.cach_tinh==="2")$r$$.cong_thuc="",async.parallel({ky_nay:function($callback$$){setImmediate(async()=>{let $condition$$={ngay_ct:{$gte:$query$$.tu_ngay,$lte:$query$$.den_ngay},id_app:$query$$.id_app};$query$$.ma_dvcs&&($condition$$.ma_dvcs=$query$$.ma_dvcs);$query$$.ma_bp&&($condition$$.ma_bp=$query$$.ma_bp);$query$$.ma_hd&&($condition$$.ma_hd=$query$$.ma_hd);
$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);$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");let $rs_socai$$=await groupByCondition($socai_str_func$$,$condition$$,["id_ct"],{sum:["tien","tien_nt"],max:["so_ct","ngay_ct","dien_giai","ma_ct"]});$r$$.so_kn=$rs_socai$$.csum("tien");$r$$.so_kn_nt=$rs_socai$$.csum("tien_nt");let $id_cts$$=$rs_socai$$.map($r$$=>$r$$.id_ct);async.parallel({vatra:$callback$$=>{groupByCondition(vatra,{id_app:$query$$.id_app,id_ct:{$in:$id_cts$$}},
["id_ct"],{sum:["t_thue","t_thue_nt"]}).then($rs$$=>{$callback$$(null,$rs$$)}).catch($e$$=>{$callback$$($e$$)})},vatvao:$callback$$=>{groupByCondition(vatvao,{id_app:$query$$.id_app,id_ct:{$in:$id_cts$$}},["id_ct"],{sum:["t_thue","t_thue_nt"]}).then($rs$$=>{$callback$$(null,$rs$$)}).catch($e$$=>{$callback$$($e$$)})},sokho:$callback$$=>{groupByCondition(sokho,{id_app:$query$$.id_app,id_ct:{$in:$id_cts$$}},["id_ct","ma_vt"],{sum:"sl_nhap sl_xuat sl_nhap_qd sl_xuat_qd tien_nhap tien_xuat".split(" "),
max:["so_ct","ngay_ct","dien_giai","ma_ct"]}).then($rs$$=>{$callback$$(null,$rs$$)}).catch($e$$=>{$callback$$($e$$)})},socai:$callback$$=>{$callback$$(null,$rs_socai$$)}},($e$$,$rs$$)=>{$r$$.vatra=$rs$$.vatra.csum("t_thue");$r$$.vatvao=$rs$$.vatvao.csum("t_thue");$r$$.vatra_nt=$rs$$.vatra.csum("t_thue_nt");$r$$.vatvao_nt=$rs$$.vatvao.csum("t_thue_nt");$r$$.thue=$r$$.vatra+$r$$.vatvao;$r$$.thue_nt=$r$$.vatra_nt+$r$$.vatvao_nt;$r$$.sl_nhap=$rs$$.sokho.csum("sl_nhap");$r$$.sl_xuat=$rs$$.sokho.csum("sl_xuat");
$r$$.so_luong=$r$$.sl_nhap+$r$$.sl_xuat;$r$$.t_tt=$r$$.thue+$r$$.so_kn;$r$$.t_tt_nt=$r$$.thue_nt+$r$$.so_kn_nt;$r$$.sokho=$rs$$.sokho;$r$$.ct_socai&&($r$$.socai=$rs$$.socai,$r$$.socai.sort(($a$$,$b$$)=>(new Date($a$$.ngay_ct)).getTime()>(new Date($b$$.ngay_ct)).getTime()?1:(new Date($a$$.ngay_ct)).getTime()<(new Date($b$$.ngay_ct)).getTime()?-1:$a$$.so_ct>$b$$.so_ct?1:$a$$.so_ct<$b$$.so_ct?-1:0).forEach(($s$$,$index$$)=>{$s$$.thue=$rs$$.vatra.csum("t_thue",{id_ct:$s$$.id_ct})+$rs$$.vatvao.csum("t_thue",
{id_ct:$s$$.id_ct});$s$$.thue_nt=$rs$$.vatra.csum("t_thue_nt",{id_ct:$s$$.id_ct})+$rs$$.vatvao.csum("t_thue_nt",{id_ct:$s$$.id_ct});$s$$.so_luong=$rs$$.sokho.csum("sl_nhap",{id_ct:$s$$.id_ct})+$rs$$.sokho.csum("sl_xuat",{id_ct:$s$$.id_ct});$s$$.gia=$s$$.so_luong!==0?Math.roundBy($s$$.tien/$s$$.so_luong,0):0;$s$$.gia_nt=$s$$.so_luong!==0?Math.roundBy($s$$.tien_nt/$s$$.so_luong,2):0;$s$$.t_tt=$s$$.thue+$s$$.tien;$s$$.t_tt_nt=$s$$.thue_nt+$s$$.tien_nt;$s$$.stt=$r$$.stt;$r$$.stt_child=$index$$+1;$report$$.push($s$$)}));
$callback$$($e$$,$r$$)})})}},function($error$$,$results$$){$c1$$($error$$,$results$$)});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($cbRow_error$$){if($cbRow_error$$)return $callback$$($cbRow_error$$);$cbRow_error$$=function($e$$,$r$$){$r$$.chi_tinh_cac_cot&&$r$$.chi_tinh_cac_cot.length>
0&&("so_kn so_kn_nt vatra vatvao vatra_nt vatvao_nt thue thue_nt sl_nhap sl_xuat so_luong t_tt t_tt_nt".split(" ").forEach($c$$=>{$r$$.chi_tinh_cac_cot.indexOf($c$$)<0&&($r$$[$c$$]=0)}),$r$$.chi_tinh_cac_cot.indexOf("t_tt")<0&&($r$$.t_tt=$r$$.thue+$r$$.so_kn))};calcGrid($report$$,{so_kn:$cbRow_error$$,so_kn_nt:$cbRow_error$$,vatra:$cbRow_error$$,vatvao:$cbRow_error$$,vatra_nt:$cbRow_error$$,vatvao_nt:$cbRow_error$$,thue:$cbRow_error$$,thue_nt:$cbRow_error$$,sl_nhap:$cbRow_error$$,sl_xuat:$cbRow_error$$,
so_luong:$cbRow_error$$,t_tt:$cbRow_error$$,t_tt_nt:$cbRow_error$$},function($rs$$){$rs$$=$rs$$.sort(($a$$,$b$$)=>$a$$.stt>$b$$.stt?1:$a$$.stt<$b$$.stt?-1:$a$$.stt_child-$b$$.stt_child);$callback$$(null,$rs$$)})})})};