UNPKG

flexbiz-server

Version:

Flexible Server

11 lines (10 loc) 4.88 kB
const tdttno=global.getModel("tdttno"),tdttno_tt=global.getModel("tdttno_tt"),async=require("async"),moment=require("moment"),func=async $condition$$=>{let $objs$$=await tdttno.find({id_app:$condition$$.id_app,ngay_tt:{$exists:!1}});for(let $obj$$ of $objs$$)Logger.info("update ngay_tt",$obj$$.ngay_hd),$obj$$.ngay_tt=moment($obj$$.ngay_hd).add($obj$$.han_tt||0,"days").toDate(),await $obj$$.save();await tdttno.updateMany({id_app:$condition$$.id_app,ngay_tt:{$exists:!1}},{date_updated:new Date});return new Promise(($resolve$$, $reject$$)=>{if(!$condition$$.id_app)return $reject$$("Ch\u1ee9c n\u0103ng n\u00e0y y\u00eau c\u1ea7u tham s\u1ed1: id_app");$condition$$.den_ngay||($condition$$.den_ngay=moment().endOf("date").toDate());$condition$$.tuoi_no&&($condition$$.tuoi_no=Number($condition$$.tuoi_no),$condition$$.tuoi_no>0&&($condition$$.tuoi_no_min=$condition$$.tuoi_no));let $ngay_tt_min$$,$ngay_tt_max$$;$condition$$.tuoi_no_min&&($ngay_tt_min$$=moment().add($condition$$.tuoi_no_min,"days").toDate());$condition$$.tuoi_no_max&& ($ngay_tt_max$$=moment().add($condition$$.tuoi_no_max,"days").toDate());async.series({invoices:function($callback$$){setImmediate(async()=>{const $query$$={id_app:$condition$$.id_app};$query$$.ngay_ct={$lte:$condition$$.den_ngay};$condition$$.tu_ngay&&($query$$.ngay_ct.$gte=$condition$$.tu_ngay);$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);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")}$condition$$.ma_kh&&($query$$.ma_kh=$condition$$.ma_kh);$condition$$.so_ct&&($query$$.so_ct=$condition$$.so_ct);$condition$$.so_hd&&($query$$.so_hd=$condition$$.so_hd);$condition$$.tat_toan!==void 0&&($query$$.tat_toan=$condition$$.tat_toan);$ngay_tt_min$$&&($query$$.ngay_tt={$gte:$ngay_tt_min$$});$ngay_tt_max$$&&($query$$.ngay_tt?$query$$.ngay_tt.$lte=$ngay_tt_max$$: $query$$.ngay_tt={$lte:$ngay_tt_max$$});tdttno.find($query$$).lean().then(function($results$$){const $invoices$$=[];for(let $i$$=0;$i$$<$results$$.length;$i$$++){const $invoice$$=$results$$[$i$$];$invoice$$.line=$i$$;$invoices$$.push($invoice$$)}$callback$$(null,$invoices$$)}).catch($e$$=>{$callback$$($e$$)})})}},($error$$,$results$$)=>{if($error$$)return $reject$$($error$$);let $report$$=[];async.map($results$$.invoices,function($invoice$$,$callback$$){setImmediate(()=>{const $t_invoice$$={sysorder:1, bold:!1,tat_toan:$invoice$$.tat_toan,id_ct:$invoice$$.id_ct,ma_ct:$invoice$$.ma_ct,id_hd:$invoice$$.id_ct,so_ct:$invoice$$.so_ct,ngay_ct:$invoice$$.ngay_ct,so_hd:$invoice$$.so_hd,ngay_hd:$invoice$$.ngay_hd,tien_hd_nt:$invoice$$.tien_nt};$t_invoice$$.ma_nt_hd=$invoice$$.ma_nt;$t_invoice$$.ty_gia_hd=$invoice$$.ty_gia;$t_invoice$$.ma_kh=$invoice$$.ma_kh;$t_invoice$$.ma_dt=$invoice$$.ma_dt;$t_invoice$$.ma_nv=$invoice$$.ma_nv;$t_invoice$$.dien_giai=$invoice$$.dien_giai;$t_invoice$$.tien_nt=0;$t_invoice$$.tien= 0;$t_invoice$$.da_thanh_toan_nt=0;$t_invoice$$.con_lai_nt=0;$t_invoice$$.thanh_toan_qd=0;$t_invoice$$.tk_co=$invoice$$.tk_no;$t_invoice$$.han_tt=$invoice$$.han_tt?$invoice$$.han_tt:0;$report$$.push($t_invoice$$);tdttno_tt.find({id_tdtt:$invoice$$._id,id_app:$invoice$$.id_app}).sort({ngay_ct:1}).lean().then(function($results$$){let $da_thanh_toan_nt$$=0;$results$$.forEach(function($invoice_tt$$){$da_thanh_toan_nt$$+=$invoice_tt$$.thanh_toan_qd});$t_invoice$$.da_thanh_toan_nt=$da_thanh_toan_nt$$;$t_invoice$$.con_lai_nt= $t_invoice$$.tien_hd_nt-$t_invoice$$.da_thanh_toan_nt;$t_invoice$$.tat_toan&&($t_invoice$$.con_lai_nt=0);$t_invoice$$.con_lai_nt!==0?($t_invoice$$.tuoi_no=moment().diff(moment($t_invoice$$.ngay_hd),"days"),$t_invoice$$.so_ngay_trong_han=Math.max(0,$t_invoice$$.han_tt-$t_invoice$$.tuoi_no),$t_invoice$$.so_ngay_qua_han=Math.max(0,$t_invoice$$.tuoi_no-$t_invoice$$.han_tt),$t_invoice$$.so_ngay_qua_han>0&&$t_invoice$$.so_ngay_qua_han<=30&&($t_invoice$$.qua_han1=$t_invoice$$.con_lai_nt),$t_invoice$$.so_ngay_qua_han> 30&&$t_invoice$$.so_ngay_qua_han<=60&&($t_invoice$$.qua_han2=$t_invoice$$.con_lai_nt),$t_invoice$$.so_ngay_qua_han>60&&$t_invoice$$.so_ngay_qua_han<=90&&($t_invoice$$.qua_han3=$t_invoice$$.con_lai_nt),$t_invoice$$.so_ngay_qua_han>90&&($t_invoice$$.qua_han4=$t_invoice$$.con_lai_nt)):($t_invoice$$.tuoi_no=0,$t_invoice$$.so_ngay_trong_han=0,$t_invoice$$.so_ngay_qua_han=0);$callback$$(null,$t_invoice$$)}).catch($e$$=>{$callback$$($e$$)})})},$error$$=>{if($error$$)return $reject$$($error$$);$report$$= $report$$.filter($r$$=>$r$$.tien_hd_nt>0);$resolve$$($report$$)})})})};module.exports=($condition$$,$callback$$)=>{if(!$callback$$)return func($condition$$);func($condition$$).then($rs$$=>{$callback$$(null,$rs$$)}).catch($e$$=>{$callback$$($e$$)})};