flexbiz-server
Version:
Flexible Server
12 lines (11 loc) • 5.32 kB
JavaScript
const Vsocai=require("./vsocai"),dstkcn=require("./dstkcn"),async=require("async"),underscore=require("underscore"),moment=require("moment");
module.exports=async function($condition$$,$fn$$,$options$$={model:"socaitc"}){try{if(!$condition$$||!$condition$$.ngay||!$condition$$.id_app)return $fn$$("Ch\u1ee9c n\u0103ng n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: ngay");if(!$condition$$.ma_kh&&($condition$$.nh_kh||$condition$$.phu_trach)){let $_q$$={};$condition$$.nh_kh&&($_q$$.nh_kh=$condition$$.nh_kh);$condition$$.phu_trach&&($_q$$.phu_trach=$condition$$.phu_trach);$condition$$.ma_kh=await global.getModel("customer").getList($condition$$.id_app,
$_q$$,"ma_kh")}let $tk$$=$condition$$.tk;$tk$$||="";if(underscore.isArray($tk$$)){let $tks$$;$tk$$.forEach(function($t$$){$tks$$=$tks$$?$tks$$+"|^"+$t$$:"^"+$t$$});$tks$$||="^";$tk$$={$regex:$tks$$,$options:"i"}}else $tk$$={$regex:"^"+$tk$$,$options:"i"};let $id_app$$=$condition$$.id_app;if(!await global.getModel("app").findOne({_id:$condition$$.id_app},{ngay_ks:1}).lean())return $fn$$("C\u00f4ng ty kh\u00f4ng t\u1ed3n t\u1ea1i");let $ngay$$=moment($condition$$.ngay).endOf("date").toDate(),$ngay_ks$$=
(await global.getModel("cdkhtheongay").findOne({id_app:$id_app$$,den_ngay:{$lte:moment($ngay$$).startOf("date").toDate()}},{den_ngay:1}).sort({den_ngay:-1}).lean()||{}).den_ngay;$ngay_ks$$&&=moment($ngay_ks$$).endOf("date").toDate();let $ngay_tinh_dn$$=$condition$$.tu_ngay&&new Date($condition$$.tu_ngay)<$ngay$$?moment($condition$$.tu_ngay).endOf("date").toDate():$ngay$$,$ngay_dn$$=moment($ngay_tinh_dn$$).startOf("year").toDate(),$nam$$,$Cdkh$$;!$ngay_ks$$||$ngay_ks$$<$ngay_dn$$?($Cdkh$$=global.getModel("cdkh"),
$nam$$=$ngay_tinh_dn$$.getFullYear()):($Cdkh$$=global.getModel("cdkhtheongay"),$ngay_dn$$=moment($ngay_ks$$).startOf("date").add(1,"days").toDate());async.parallel({dn:function($callback$$){setImmediate(()=>{let $c_dk$$={id_app:$id_app$$,tk:$tk$$};$nam$$?$c_dk$$.nam=$nam$$:(Logger.info("lay cdcn theo ngay khoa so",$ngay_ks$$),$c_dk$$.den_ngay=$ngay_ks$$);$condition$$.ma_kh&&(underscore.isArray($condition$$.ma_kh)&&$condition$$.ma_kh.length>0?$c_dk$$.ma_kh={$in:$condition$$.ma_kh}:$c_dk$$.ma_kh=$condition$$.ma_kh);
$condition$$.ma_dvcs&&($c_dk$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_kho&&($c_dk$$.ma_kho=$condition$$.ma_kho);$condition$$.ma_bp&&($c_dk$$.ma_bp=$condition$$.ma_bp);$condition$$.ma_phi&&($c_dk$$.ma_phi=$condition$$.ma_phi);$condition$$.ma_nv&&($c_dk$$.ma_nv=$condition$$.ma_nv);$condition$$.ma_hd&&($c_dk$$.ma_hd=$condition$$.ma_hd);$condition$$.ma_dt&&($c_dk$$.ma_dt=$condition$$.ma_dt);$Cdkh$$.find($c_dk$$).lean().then(function($results$$){$callback$$(null,$results$$)}).catch($error$$=>{$callback$$($error$$)})})},
ps:function($callback$$){async.waterfall([function($callback1$$){setImmediate(()=>{dstkcn($id_app$$,$tk$$,function($error$$,$accs$$){if($error$$)return $callback1$$($error$$);$callback1$$(null,$accs$$)})})},function($accs$$,$callback1$$){setImmediate(()=>{let $query$$={id_app:$id_app$$,ngay_ct:{$gte:$ngay_dn$$,$lte:$ngay$$},tk:{$in:$accs$$}};$condition$$.ma_kh&&(underscore.isArray($condition$$.ma_kh)&&$condition$$.ma_kh.length>0?$query$$.ma_kh={$in:$condition$$.ma_kh}:$query$$.ma_kh=$condition$$.ma_kh);
$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_kho&&($query$$.ma_kho=$condition$$.ma_kho);$condition$$.ma_bp&&($query$$.ma_bp=$condition$$.ma_bp);$condition$$.ma_phi&&($query$$.ma_phi=$condition$$.ma_phi);$condition$$.ma_nv&&($query$$.ma_nv=$condition$$.ma_nv);$condition$$.ma_hd&&($query$$.ma_hd=$condition$$.ma_hd);$condition$$.ma_dt&&($query$$.ma_dt=$condition$$.ma_dt);$condition$$.ex_id_ct&&($query$$.id_ct={$ne:$condition$$.ex_id_ct});Vsocai.find($query$$,function($error$$,
$pss$$){if($error$$)return $callback1$$($error$$);$callback1$$(null,$pss$$)},$options$$)})}],function($error$$,$result$$){if($error$$)return $callback$$($error$$);$callback$$(null,$result$$)})}},function($data_error$$,$groupBy_results$$){if($data_error$$)return $fn$$($data_error$$);$data_error$$=$groupBy_results$$.dn.concat($groupBy_results$$.ps);$groupBy_results$$=$condition$$.groupby||$condition$$.groupBy||"tk ma_kh ma_kho ma_bp ma_phi ma_dt ma_hd ma_nv".split(" ");if(!underscore.isArray($groupBy_results$$))if($groupBy_results$$.indexOf("[")>=
0)try{$groupBy_results$$=JSON.parse($groupBy_results$$)}catch($e$$){Logger.error("err parse json groupby",$groupBy_results$$),$groupBy_results$$=[$groupBy_results$$]}else $groupBy_results$$=$groupBy_results$$.indexOf(",")>=0?$groupBy_results$$.split(","):[$groupBy_results$$];$data_error$$.groupBy($groupBy_results$$,"du_no00 du_co00 du_no_nt00 du_co_nt00 ps_no ps_co ps_no_nt ps_co_nt".split(" "),($error$$,$rs$$)=>{if($error$$)return $fn$$($error$$);setImmediate(()=>{$rs$$.forEach($r$$=>{$r$$.du_no00=
$r$$.du_no00+$r$$.ps_no-$r$$.du_co00-$r$$.ps_co;$r$$.du_no_nt00=$r$$.du_no_nt00+$r$$.ps_no_nt-$r$$.du_co_nt00-$r$$.ps_co_nt;$r$$.du_co00=0-$r$$.du_no00;$r$$.du_co_nt00=0-$r$$.du_no_nt00;$r$$.du_no00<0&&($r$$.du_no00=0);$r$$.du_no_nt00<0&&($r$$.du_no_nt00=0);$r$$.du_co00<0&&($r$$.du_co00=0);$r$$.du_co_nt00<0&&($r$$.du_co_nt00=0);$r$$.ps_no_nt=0;$r$$.ps_co_nt=0;$r$$.ps_no=0;$r$$.ps_co=0});$fn$$(null,$rs$$)})})})}catch($e$$){Logger.error("[ckcn]",$e$$,$condition$$),$fn$$($e$$.message||$e$$.error||$e$$)}};