UNPKG

flexbiz-server

Version:

Flexible Server

10 lines (9 loc) 4.21 kB
var sokho=global.getModel("sokho"),gt_sltk=global.getModel("gt_sltk"),spdd_dk=global.getModel("spdd_dk"),spdd_ck=global.getModel("spdd_ck");const async=require("async"),underscore=require("underscore"),moment=require("moment"); module.exports=function($condition$$,$fn$$){if(!($condition$$&&$condition$$.thang&&$condition$$.nam&&$condition$$.id_app))return $fn$$("L\u1ed7i: 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),$tu_ngay$$=moment(new Date($nam$$,$thang$$-1,1)).startOf("date").toDate(),$den_ngay$$=moment(new Date($nam$$,$thang$$,0)).endOf("date").toDate(),$data$$=[];async.series({nhapkhotp:$callback$$=>{let $condition_nhapkho$$= {id_app:$condition$$.id_app,ngay_ct:{$gte:$tu_ngay$$,$lte:$den_ngay$$},ma_gd:"1",ma_ct:"PNK"};$condition$$.ma_bp&&($condition_nhapkho$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&!mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&delete $condition$$.id_lenhsx;$condition$$.ma_sp&&($condition_nhapkho$$.ma_sp=$condition$$.ma_sp.toUpperCase());sokho.find($condition_nhapkho$$).lean().then($sk$$=>{for(let $s$$ of $sk$$)$data$$.push({ma_sp:$s$$.ma_vt,ma_bp:$s$$.ma_bp,id_lenhsx:$s$$.id_lenhsx, sl_nk_tk:$s$$.sl_nhap_qd,ty_le_ht_dk:0,ty_le_ht_ck:0});$callback$$(null)}).catch($e$$=>{$callback$$($e$$)})},ddck:$callback$$=>{let $condition_ck$$={id_app:$condition$$.id_app,nam:$nam$$,thang:$thang$$};$condition$$.ma_sp&&($condition_ck$$.ma_sp=$condition$$.ma_sp.toUpperCase());$condition$$.ma_bp&&($condition_ck$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($condition_ck$$.id_lenhsx=$condition$$.id_lenhsx);spdd_ck.find($condition_ck$$).lean().then($ck$$=> {for(let $s$$ of $ck$$)$s$$.sl_dd_ck=$s$$.sl_dd,$s$$.ty_le_ht_ck=$s$$.ty_le_ht,$s$$.ty_le_ht_dk=0,$data$$.push($s$$);$callback$$(null)}).catch($e$$=>{$callback$$($e$$)})},dddk:$callback$$=>{async.parallel({dauky:$callback$$=>{let $condition_dk$$={id_app:$condition$$.id_app,nam:$nam$$,thang:$thang$$};$condition$$.ma_sp&&($condition_dk$$.ma_sp=$condition$$.ma_sp.toUpperCase());$condition$$.ma_bp&&($condition_dk$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&& ($condition_dk$$.id_lenhsx=$condition$$.id_lenhsx);spdd_dk.find($condition_dk$$).lean().then($dk$$=>{$callback$$(null,$dk$$)}).catch($e$$=>{$callback$$($e$$)})},cuoikytruoc:$callback$$=>{let $condition_ck$$={id_app:$condition$$.id_app,nam:$nam$$,thang:$thang$$-1};$condition_ck$$.thang===0&&($condition_ck$$.thang=12,--$condition_ck$$.nam);$condition$$.ma_sp&&($condition_ck$$.ma_sp=$condition$$.ma_sp.toUpperCase());$condition$$.ma_bp&&($condition_ck$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&& mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($condition_ck$$.id_lenhsx=$condition$$.id_lenhsx);spdd_ck.find($condition_ck$$).lean().then($ck$$=>{$callback$$(null,$ck$$)}).catch($e$$=>{$callback$$($e$$)})}},function($dauky_e$$,$rs$$){if($dauky_e$$)return $callback$$($dauky_e$$);$dauky_e$$=$rs$$.dauky;for(let $dk$$ of $dauky_e$$)$dk$$.sl_dd_dk=$dk$$.sl_dd,$dk$$.ty_le_ht_dk=$dk$$.ty_le_ht,$dk$$.ty_le_ht_ck=0,$data$$.push($dk$$);for(let $ckt$$ of $rs$$.cuoikytruoc)$ckt$$.sl_dd_dk=$ckt$$.sl_dd, $ckt$$.ty_le_ht_dk=$ckt$$.ty_le_ht,$ckt$$.ty_le_ht_ck=0,underscore.find($dauky_e$$,$d$$=>$d$$.ma_sp==$ckt$$.ma_sp&&$d$$.ma_bp==$ckt$$.ma_bp&&$d$$.id_lenhsx==$ckt$$.id_lenhsx)||$data$$.push($ckt$$);$callback$$(null)})}},function($e$$){if($e$$)return $fn$$($e$$);$data$$.groupBy(["ma_sp","ma_bp","id_lenhsx"],["sl_dd_dk","sl_nk_tk","sl_dd_ck"],function($e$$,$rs$$){if($e$$)return $fn$$($e$$);for(let $r$$ of $rs$$)$r$$.id_app=$condition$$.id_app,$r$$.nam=$condition$$.nam,$r$$.thang=$condition$$.thang,$r$$.ty_le_ht_dk|| ($r$$.ty_le_ht_dk=0),$r$$.ty_le_ht_ck||($r$$.ty_le_ht_ck=0),$r$$.sl_nk_tk||($r$$.sl_nk_tk=0),$r$$.sl_dd_ck||($r$$.sl_dd_ck=0),$r$$.sl_dd_dk||($r$$.sl_dd_dk=0),$r$$.sl_sx_tk=$r$$.sl_nk_tk+$r$$.sl_dd_ck-$r$$.sl_dd_dk;gt_sltk.create($rs$$,($e$$,$dt$$)=>{$fn$$($e$$,$dt$$)})},{fieldmaxs:["ty_le_ht_dk","ty_le_ht_ck"]})})};