UNPKG

flexbiz-server

Version:

Flexible Server

13 lines (12 loc) 5.67 kB
'use strict';const dmyt=global.getModel("dmyt"),gt_thcppstk=global.getModel("gt_thcppstk"),gt_ctgt=global.getModel("gt_ctgt"),gt_sltk=global.getModel("gt_sltk"),gt_hspb=global.getModel("gt_hspb"),async=require("async"),utils=require("../libs/utils"),_=require("lodash"); module.exports=function($condition$$,$fn$$){if(!($condition$$&&$condition$$.thang&&$condition$$.nam&&$condition$$.id_app))return $fn$$("T\u00ednh n\u0103ng n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: thang,nam,id_app");let $thang$$=Number($condition$$.thang),$nam$$=Number($condition$$.nam),$query_yt$$={id_app:$condition$$.id_app,pt_phan_bo:"5",th_sp:!1,status:!0};$condition$$.ma_bp&&($query_yt$$.ma_bp={$in:[$condition$$.ma_bp.toUpperCase(),null,void 0]});let $sltk$$=[];dmyt.find($query_yt$$).lean().exec(async($cond_e$$, $yts$$)=>{if($cond_e$$)return $fn$$($cond_e$$);0<$yts$$.length&&($cond_e$$={id_app:$condition$$.id_app,nam:$condition$$.nam,thang:$condition$$.thang},$condition$$.ma_bp&&($cond_e$$.ma_bp=$condition$$.ma_bp.toUpperCase()),$condition$$.id_lenhsx&&mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($cond_e$$.id_lenhsx=$condition$$.id_lenhsx),$sltk$$=await gt_sltk.find($cond_e$$).lean(),$sltk$$.forEach($p$$=>{$p$$.ma_bp=$p$$.ma_bp||"";$p$$.id_lenhsx=$p$$.id_lenhsx||""}));async.map($yts$$,($yt$$, $callback$jscomp$0$$)=>{async.series({gt:$callback$$=>{setImmediate(()=>{let $cond$$={id_app:$condition$$.id_app,ma_yt:$yt$$.ma_yt,nam:$condition$$.nam,thang:$condition$$.thang};$condition$$.ma_bp&&($cond$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($cond$$.id_lenhsx=$condition$$.id_lenhsx);gt_thcppstk.find($cond$$).lean().exec(($e$$,$pstk$$)=>{if($e$$)return $callback$$($e$$);$pstk$$.groupBy(["ma_bp","ma_yt","id_lenhsx"], ["dd_dk","dd_dk_nt","ps_tk","ps_tk_nt"],($_sltk_e$$,$rs$$)=>{if($_sltk_e$$)return $callback$$($_sltk_e$$);$rs$$.forEach($p$$=>{$p$$.co_do_dang=$yt$$.co_do_dang;$p$$.nam=$nam$$;$p$$.thang=$thang$$;$p$$.id_app=$condition$$.id_app;$p$$.yt_trung_gian=$yt$$.yt_trung_gian;$p$$.cong_tru=$yt$$.cong_tru;$p$$.ma_bp=$p$$.ma_bp||"";$p$$.ma_yt=$p$$.ma_yt||"";$p$$.id_lenhsx=$p$$.id_lenhsx||"";$p$$.th_bp=$yt$$.th_bp;$p$$.th_lsx=$yt$$.th_lsx});$_sltk_e$$=_.cloneDeep($sltk$$);let $fiels_join$$={};$yt$$.th_bp&&($fiels_join$$.ma_bp= "ma_bp");$yt$$.th_lsx&&($fiels_join$$.id_lenhsx="id_lenhsx");$rs$$.crossJoin($_sltk_e$$,$fiels_join$$,async $dt$$=>{$callback$$(null,$dt$$)})})})})}},function($e$$,$rs$$){if($e$$)return $callback$jscomp$0$$($e$$);$callback$jscomp$0$$(null,$rs$$.gt)})},async($data_pstk_e$$,$condition_s_cppstk_t_sl_tinh_he_so$$)=>{if($data_pstk_e$$)return $fn$$($data_pstk_e$$);$data_pstk_e$$=$condition_s_cppstk_t_sl_tinh_he_so$$.reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);await $data_pstk_e$$.asyncJoinModel2($condition$$.id_app, gt_hspb,{where:$item$$=>({nam:$item$$.nam,thang:$item$$.thang,ma_sp:$item$$.ma_sp,ma_yt:{$in:[void 0,null,"",$item$$.ma_yt]},ma_bp:{$in:[void 0,null,"",$item$$.ma_bp]},id_lenhsx:{$in:[void 0,null,"",$item$$.id_lenhsx]}}),fields:{he_so_dc:"he_so",dieu_chinh_slsx:"dieu_chinh_slsx"}});await $data_pstk_e$$.filter($r$$=>void 0==$r$$.he_so_dc).asyncJoinModel2($condition$$.id_app,gt_hspb,{where:$item$$=>({nam:$item$$.nam,thang:0,ma_sp:$item$$.ma_sp,ma_yt:{$in:[void 0,null,"",$item$$.ma_yt]},ma_bp:{$in:[void 0, null,"",$item$$.ma_bp]},id_lenhsx:{$in:[void 0,null,"",$item$$.id_lenhsx]}}),fields:{he_so_dc:"he_so",dieu_chinh_slsx:"dieu_chinh_slsx"}});$data_pstk_e$$.forEach($r$$=>{$r$$.he_so_dc=$r$$.he_so_dc||0;$r$$.sl_tinh_he_so=$r$$.dieu_chinh_slsx?($r$$.sl_nk_tk||0)*$r$$.he_so_dc+($r$$.sl_dd_ck||0)*$r$$.he_so_dc:$r$$.he_so_dc});for(let $s$$ of $data_pstk_e$$){$condition_s_cppstk_t_sl_tinh_he_so$$={ma_yt:$s$$.ma_yt};$s$$.th_bp&&($condition_s_cppstk_t_sl_tinh_he_so$$.ma_bp=$s$$.ma_bp);$s$$.th_lsx&&($condition_s_cppstk_t_sl_tinh_he_so$$.id_lenhsx= $s$$.id_lenhsx);$condition_s_cppstk_t_sl_tinh_he_so$$=$data_pstk_e$$.csum("sl_tinh_he_so",$condition_s_cppstk_t_sl_tinh_he_so$$);$s$$.he_so=0!==$condition_s_cppstk_t_sl_tinh_he_so$$?$s$$.sl_tinh_he_so/$condition_s_cppstk_t_sl_tinh_he_so$$:0;delete $s$$._id;$s$$.co_do_dang||($s$$.ty_le_ht_dk=0,$s$$.ty_le_ht_ck=0);$s$$.he_so||($s$$.he_so=0);$s$$.sl_dd_dk||($s$$.sl_dd_dk=0);$s$$.sl_nk_tk||($s$$.sl_nk_tk=0);$s$$.sl_sx_tk||($s$$.sl_sx_tk=0);$s$$.sl_dd_ck||($s$$.sl_dd_ck=0);$s$$.ty_le_ht_ck||($s$$.ty_le_ht_ck= 0);$s$$.ty_le_ht_dk||($s$$.ty_le_ht_dk=0);if(!$s$$.dd_dk||isNaN($s$$.dd_dk))$s$$.dd_dk=0;if(!$s$$.dd_dk_nt||isNaN($s$$.dd_dk_nt))$s$$.dd_dk_nt=0;if(!$s$$.ps_tk||isNaN($s$$.ps_tk))$s$$.ps_tk=0;if(!$s$$.ps_tk_nt||isNaN($s$$.ps_tk_nt))$s$$.ps_tk_nt=0;$s$$.dd_dk=utils.round($s$$.he_so*$s$$.dd_dk,5);$s$$.dd_dk_nt=utils.round($s$$.he_so*$s$$.dd_dk_nt,5);$s$$.ps_tk=utils.round($s$$.he_so*$s$$.ps_tk,5);$s$$.ps_tk_nt=utils.round($s$$.he_so*$s$$.ps_tk_nt,5);$s$$.sl_dd_dk_qd=utils.round($s$$.sl_dd_dk*$s$$.ty_le_ht_dk/ 100,5);$s$$.sl_dd_ck_qd=utils.round($s$$.sl_dd_ck*$s$$.ty_le_ht_ck/100,5);$s$$.sl_sx_tk=$s$$.sl_dd_ck+$s$$.sl_nk_tk-$s$$.sl_dd_dk;$s$$.sl_sp_ht_qd=$s$$.sl_dd_ck_qd+$s$$.sl_nk_tk;$s$$.t_cp_sx=$s$$.dd_dk+$s$$.ps_tk;$s$$.t_cp_sx_nt=$s$$.dd_dk_nt+$s$$.ps_tk_nt;$s$$.gt=utils.round(0===$s$$.sl_nk_tk?0:$s$$.t_cp_sx/$s$$.sl_sp_ht_qd,5);$s$$.gt_nt=utils.round(0===$s$$.sl_nk_tk?0:$s$$.t_cp_sx_nt/$s$$.sl_sp_ht_qd,5);$s$$.t_gt=utils.round($s$$.gt*$s$$.sl_nk_tk,5);$s$$.t_gt_nt=utils.round($s$$.gt_nt*$s$$.sl_nk_tk, 5);$s$$.dd_ck=utils.round($s$$.t_cp_sx-$s$$.t_gt,5);$s$$.dd_ck_nt=utils.round($s$$.t_cp_sx_nt-$s$$.t_gt_nt,5)}gt_ctgt.create($data_pstk_e$$.filter($d$$=>$d$$.ma_sp),($e$$,$rs$$)=>{$e$$&&console.error($e$$.message);$fn$$($e$$,$rs$$)})})})};