UNPKG

flexbiz-server

Version:

Flexible Server

11 lines (10 loc) 4.94 kB
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"}){if($condition$$&&$condition$$.ngay&&$condition$$.id_app){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")}var $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"};var $id_app$$=$condition$$.id_app,$ngay$$=new Date($condition$$.ngay);$ngay$$=moment($ngay$$).startOf("date").toDate();if(!await global.getModel("app").findOne({_id:$id_app$$},{ngay_ks:1}).lean())return $fn$$("C\u00f4ng ty kh\u00f4ng t\u1ed3n t\u1ea1i");var $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();var $ngay_dn$$=moment($ngay$$).startOf("year").toDate();if(!$ngay_ks$$||$ngay_ks$$<$ngay_dn$$){var $Cdkh$$=global.getModel("cdkh");var $nam$$=$ngay$$.getFullYear()}else $Cdkh$$=global.getModel("cdkhtheongay"),$ngay_dn$$=moment($ngay_ks$$).startOf("date").add(1,"days").toDate();async.parallel({dn:function($callback$$){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$$({message:$error$$.message})})},ps:function($callback$$){async.waterfall([function($callback1$$){dstkcn($id_app$$,$tk$$,function($error$$,$accs$$){$error$$?$callback1$$($error$$):$callback1$$(null,$accs$$)})},function($accs$$,$callback1$$){$accs$$={id_app:$id_app$$,ngay_ct:{$gte:$ngay_dn$$,$lt:$ngay$$}, tk:{$in:$accs$$}};$condition$$.ma_kh&&(underscore.isArray($condition$$.ma_kh)&&$condition$$.ma_kh.length>0?$accs$$.ma_kh={$in:$condition$$.ma_kh}:$accs$$.ma_kh=$condition$$.ma_kh);$condition$$.ma_dvcs&&($accs$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_kho&&($accs$$.ma_kho=$condition$$.ma_kho);$condition$$.ma_bp&&($accs$$.ma_bp=$condition$$.ma_bp);$condition$$.ma_phi&&($accs$$.ma_phi=$condition$$.ma_phi);$condition$$.ma_nv&&($accs$$.ma_nv=$condition$$.ma_nv);$condition$$.ma_hd&&($accs$$.ma_hd= $condition$$.ma_hd);$condition$$.ma_dt&&($accs$$.ma_dt=$condition$$.ma_dt);$condition$$.ex_id_ct&&($accs$$.id_ct={$ne:$condition$$.ex_id_ct});Vsocai.find($accs$$,function($error$$,$pss$$){$error$$?$callback1$$($error$$):$callback1$$(null,$pss$$)},$options$$)}],function($error$$,$result$$){$error$$?$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($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$$)=>{$error$$?$fn$$($error$$):($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$$))})})}else $fn$$("Ch\u1ee9c n\u0103ng n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: ngay")};