flexbiz-server
Version:
Flexible Server
10 lines (9 loc) • 4.22 kB
JavaScript
const dkvt=require("./dkvt"),sokho=global.getModel("sokho"),dmvt=global.getModel("dmvt"),async=require("async"),_=require("lodash"),utils=require("./utils"),crypto=require("crypto");
module.exports=async function($condition$$,$fn$$,$log$$=()=>{}){$log$$("stocksummary: prepare condition");if($condition$$&&$condition$$.tu_ngay&&$condition$$.den_ngay&&$condition$$.id_app){var $uniqueId$$=crypto.randomUUID(),$id_app$$=$condition$$.id_app,$app$$=await global.getModel("app").findOne({_id:$id_app$$},{options:1}).lean();if(!$app$$)return $fn$$("C\u00f4ng ty n\u00e0y kh\u00f4ng t\u1ed3n t\u1ea1i");var $f_sl$$=($app$$.options||{}).f_sl==void 0?4:($app$$.options||{}).f_sl,$f_tien$$=($app$$.options||
{}).f_tien||0,$groupBy$$=$condition$$.groupBy||$condition$$.groupby;$groupBy$$||=["ma_vt"];if(_.isString($groupBy$$))if($groupBy$$.indexOf("[")>=0)try{$groupBy$$=JSON.parse($groupBy$$)}catch($e$$){Logger.error($e$$)}else $groupBy$$=$groupBy$$.split(",");if(_.isString($condition$$.ma_vt)&&$condition$$.ma_vt.indexOf("[")>=0&&$condition$$.ma_vt.indexOf("]")>0)try{$condition$$.ma_vt=JSON.parse($condition$$.ma_vt),$condition$$.ma_vt.length==0&&($condition$$.ma_vt=void 0)}catch($e$$){Logger.error($e$$)}async.parallel({dn:function($callback$$){setImmediate(()=>
{$log$$("stocksummary: calculate balance begin");console.time("[thnxt] t\u00ednh \u0111\u1ea7u k\u1ef3 "+$uniqueId$$.toString());dkvt($condition$$,function($error$$,$result$$){console.timeEnd("[thnxt] t\u00ednh \u0111\u1ea7u k\u1ef3 "+$uniqueId$$.toString());if($error$$)return $callback$$($error$$);$callback$$(null,$result$$)})})},ps:function($callback$$){console.time("[thnxt] t\u00ednh ph\u00e1t sinh "+$uniqueId$$.toString());setImmediate(()=>{let $ma_vt$$=$condition$$.ma_vt;_.isArray($ma_vt$$)&&
($ma_vt$$={$in:$ma_vt$$});const $query$$={id_app:$id_app$$,ngay_ct:{$gte:$condition$$.tu_ngay,$lte:$condition$$.den_ngay}};$condition$$.ma_kho&&($query$$.ma_kho=$condition$$.ma_kho);$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);$ma_vt$$&&($query$$.ma_vt=$ma_vt$$);$condition$$.ma_lo&&($query$$.ma_lo=$condition$$.ma_lo);$condition$$.ma_tt1&&($query$$.ma_tt1=$condition$$.ma_tt1);$condition$$.ma_tt2&&($query$$.ma_tt2=$condition$$.ma_tt2);$condition$$.ma_tt3&&($query$$.ma_tt3=$condition$$.ma_tt3);
!$condition$$.exclude_ct_dc||$condition$$.ma_kho||$groupBy$$.find($g$$=>$g$$==="ma_kho")||($query$$.ma_ct={$nin:["PNC","PXC"]});$log$$("stocksummary: calculate araising");sokho.find($query$$).lean().then($pss$$=>{console.timeEnd("[thnxt] t\u00ednh ph\u00e1t sinh "+$uniqueId$$.toString());$pss$$=$pss$$.map($ps$$=>{$ps$$.sl_nhap=utils.round($ps$$.sl_nhap_qd,4);$ps$$.sl_xuat=utils.round($ps$$.sl_xuat_qd,4);return $ps$$});$pss$$.joinModel2($id_app$$,dmvt,{where:"ma_vt",fields:["ma_nvt","ma_nvt2","ma_nvt3",
"ma_nvt4"]},()=>{$callback$$(null,$pss$$)})}).catch($error$$=>{$callback$$($error$$)})})}},function($error$$,$results$$){$error$$?$fn$$($error$$):setImmediate(()=>{let $data$$=$results$$.dn.concat($results$$.ps);$log$$("stocksummary: group by");console.time("[thnxt] nh\u00f3m d\u1eef li\u1ec7u "+$uniqueId$$.toString());$data$$.groupBy($groupBy$$,[{name:"ton_dau",value:"ton00"},{name:"du_dau",value:"du00"},"sl_nhap","sl_xuat","tien_nhap","tien_xuat"],async($e$jscomp$2_result$$,$groups$$)=>{console.timeEnd("[thnxt] nh\u00f3m d\u1eef li\u1ec7u "+
$uniqueId$$.toString());if($e$jscomp$2_result$$)return $fn$$($e$jscomp$2_result$$);for(let $r$$ of $groups$$)$r$$.ton_dau=utils.round($r$$.ton_dau||0,$f_sl$$),$r$$.sl_nhap=utils.round($r$$.sl_nhap||0,$f_sl$$),$r$$.sl_xuat=utils.round($r$$.sl_xuat||0,$f_sl$$),$r$$.ton_cuoi=utils.round($r$$.ton_dau+$r$$.sl_nhap-$r$$.sl_xuat,$f_sl$$),$r$$.du_cuoi=Math.roundBy($r$$.du_dau,$f_tien$$)+Math.roundBy($r$$.tien_nhap,$f_tien$$)-Math.roundBy($r$$.tien_xuat,$f_tien$$);$e$jscomp$2_result$$=_.filter($groups$$,function($r$$){return Math.abs($r$$.ton_dau)+
Math.abs($r$$.du_dau)+Math.abs($r$$.sl_nhap)+Math.abs($r$$.tien_nhap)+Math.abs($r$$.sl_xuat)+Math.abs($r$$.tien_xuat)+Math.abs($r$$.ton_cuoi)+Math.abs($r$$.du_cuoi)!==0});$fn$$(null,$e$jscomp$2_result$$,$groupBy$$)})})})}else $fn$$("L\u1ed7i: B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: tu_ngay,den_ngay,id_app")};