flexbiz-server
Version:
Flexible Server
12 lines (11 loc) • 5.19 kB
JavaScript
'use strict';const Vsocai=require("./vsocai"),dstkcn=require("./dstkcn"),async=require("async"),underscore=require("underscore"),moment=require("moment");
module.exports=async function($condition$$,$fn$$){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)){var $_q_app_ngay_chua_ks$$={};$condition$$.nh_kh&&($_q_app_ngay_chua_ks$$.nh_kh=$condition$$.nh_kh);$condition$$.phu_trach&&($_q_app_ngay_chua_ks$$.phu_trach=$condition$$.phu_trach);$condition$$.ma_kh=await global.getModel("customer").getList($condition$$.id_app,
$_q_app_ngay_chua_ks$$,"ma_kh")}let $tk$$=$condition$$.tk;$tk$$||($tk$$="");if(underscore.isArray($tk$$)){let $tks$$;$tk$$.forEach(function($t$$){$tks$$=$tks$$?$tks$$+"|^"+$t$$:"^"+$t$$});$tks$$||($tks$$="^");$tk$$={$regex:$tks$$,$options:"i"}}else $tk$$={$regex:"^"+$tk$$,$options:"i"};let $id_app$$=$condition$$.id_app;$_q_app_ngay_chua_ks$$=await global.getModel("app").findOne({_id:$condition$$.id_app},{ngay_ks:1}).lean();if(!$_q_app_ngay_chua_ks$$)return $fn$$("C\u00f4ng ty kh\u00f4ng t\u1ed3n t\u1ea1i");
let $ngay_ks$$=moment($_q_app_ngay_chua_ks$$.ngay_ks).endOf("date").toDate();$_q_app_ngay_chua_ks$$=moment($_q_app_ngay_chua_ks$$.ngay_ks).startOf("date").add(1,"days").toDate();let $ngay$$=moment($condition$$.ngay).endOf("date").toDate(),$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_tinh_dn$$<$_q_app_ngay_chua_ks$$||$ngay_dn$$>=
$_q_app_ngay_chua_ks$$?($Cdkh$$=global.getModel("cdkh"),$nam$$=$ngay_tinh_dn$$.getFullYear()):($Cdkh$$=global.getModel("cdkhtheongay"),$ngay_dn$$=$_q_app_ngay_chua_ks$$);async.parallel({dn:function($callback$$){setImmediate(()=>{let $c_dk$$={id_app:$id_app$$,tk:$tk$$};$nam$$?$c_dk$$.nam=$nam$$:(console.log("lay cdcn theo ngay khoa so",$ngay_ks$$),$c_dk$$.den_ngay=$ngay_ks$$);$condition$$.ma_kh&&(underscore.isArray($condition$$.ma_kh)?$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().exec(function($error$$,$results$$){if($error$$)return $callback$$($error$$);
$callback$$(null,$results$$)})})},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)?$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$$)})})}],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||"tk ma_kh ma_kho ma_bp ma_phi ma_dt ma_hd ma_nv".split(" ");if(!underscore.isArray($groupBy_results$$))if(0<=$groupBy_results$$.indexOf("["))try{$groupBy_results$$=
JSON.parse($groupBy_results$$)}catch($e$$){console.error("err parse json groupby",$groupBy_results$$),$groupBy_results$$=[$groupBy_results$$]}else $groupBy_results$$=0<=$groupBy_results$$.indexOf(",")?$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;0>$r$$.du_no00&&($r$$.du_no00=0);0>$r$$.du_no_nt00&&($r$$.du_no_nt00=0);0>$r$$.du_co00&&($r$$.du_co00=0);0>$r$$.du_co_nt00&&($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$$)})})})};