UNPKG

flexbiz-server

Version:

Flexible Server

14 lines (13 loc) 6.3 kB
const dkvt=require("./dkvt"),sokho=global.getModel("sokho"),dmvt=global.getModel("dmvt"),_=require("lodash"),utils=require("./utils"),crypto=require("crypto"),moment=require("moment"); module.exports=async function($condition$$,$fn$$,$log$$=()=>{}){$log$$("stocksummary: prepare condition");const $uniqueId$$=crypto.randomUUID();try{if(!($condition$$&&$condition$$.tu_ngay&&$condition$$.den_ngay&&$condition$$.id_app))throw Error("L\u1ed7i: B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: tu_ngay, den_ngay, id_app");const $id_app$$=$condition$$.id_app;var $app_processData$$=await global.getModel("app").findOne({_id:$id_app$$},{options:1}).lean();if(!$app_processData$$)throw Error("C\u00f4ng ty n\u00e0y kh\u00f4ng t\u1ed3n t\u1ea1i"); const $f_sl$$=$app_processData$$.options?.f_sl??4,$f_tien$$=$app_processData$$.options?.f_tien??0;let $groupBy$$=$condition$$.groupBy||$condition$$.groupby||["ma_vt"];if(_.isString($groupBy$$))try{$groupBy$$=$groupBy$$.includes("[")?JSON.parse($groupBy$$):$groupBy$$.split(",")}catch($e$$){$groupBy$$=["ma_vt"]}if(_.isString($condition$$.ma_vt)&&$condition$$.ma_vt.includes("["))try{$condition$$.ma_vt=JSON.parse($condition$$.ma_vt),$condition$$.ma_vt.length===0&&($condition$$.ma_vt=void 0)}catch($e$$){}console.time("[thnxt] Total "+ $uniqueId$$);const $pDauKy$$=new Promise(($resolve$$,$reject$$)=>{$log$$("stocksummary: calculate balance begin");const $result$$=dkvt({...$condition$$,ngay:$condition$$.tu_ngay},($err$$,$res$$)=>{if($err$$)return $reject$$($err$$);$resolve$$($res$$)});$result$$&&$result$$.then&&$result$$.then($resolve$$).catch($reject$$)}),$pPhatSinh$$=(async()=>{$log$$("stocksummary: calculate araising");var $isGroupByKho_ma_vt_tu_ngay$$=moment($condition$$.tu_ngay).startOf("day").toDate();const $den_ngay$$=moment($condition$$.den_ngay).endOf("day").toDate(), $query$$={id_app:$id_app$$,ngay_ct:{$gte:$isGroupByKho_ma_vt_tu_ngay$$,$lte:$den_ngay$$}};$condition$$.ma_kho&&($query$$.ma_kho=$condition$$.ma_kho);$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);if($isGroupByKho_ma_vt_tu_ngay$$=$condition$$.ma_vt)_.isArray($isGroupByKho_ma_vt_tu_ngay$$)?$query$$.ma_vt={$in:$isGroupByKho_ma_vt_tu_ngay$$}:$query$$.ma_vt=$isGroupByKho_ma_vt_tu_ngay$$;["ma_lo","ma_tt1","ma_tt2","ma_tt3"].forEach($f$$=>{$condition$$[$f$$]&&($query$$[$f$$]=$condition$$[$f$$])}); $isGroupByKho_ma_vt_tu_ngay$$=$groupBy$$.includes("ma_kho");!$condition$$.exclude_ct_dc||$condition$$.ma_kho||$isGroupByKho_ma_vt_tu_ngay$$||($query$$.ma_ct={$nin:["PNC","PXC"]});return sokho.aggregate([{$match:$query$$},{$group:{_id:{ma_vt:"$ma_vt",ma_kho:"$ma_kho",ma_dvcs:"$ma_dvcs",ma_lo:"$ma_lo",ma_tt1:"$ma_tt1",ma_tt2:"$ma_tt2",ma_tt3:"$ma_tt3"},sl_nhap:{$sum:"$sl_nhap_qd"},sl_xuat:{$sum:"$sl_xuat_qd"},tien_nhap:{$sum:"$tien_nhap"},tien_xuat:{$sum:"$tien_xuat"}}},{$project:{_id:0,ma_vt:"$_id.ma_vt", ma_kho:"$_id.ma_kho",ma_dvcs:"$_id.ma_dvcs",ma_lo:"$_id.ma_lo",ma_tt1:"$_id.ma_tt1",ma_tt2:"$_id.ma_tt2",ma_tt3:"$_id.ma_tt3",sl_nhap:1,sl_xuat:1,tien_nhap:1,tien_xuat:1}}])})(),[$dnData$$,$psData$$]=await Promise.all([$pDauKy$$,$pPhatSinh$$]),$allMaVt$$=new Set;$dnData$$.forEach($d$$=>$d$$.ma_vt&&$allMaVt$$.add($d$$.ma_vt));$psData$$.forEach($d$$=>$d$$.ma_vt&&$allMaVt$$.add($d$$.ma_vt));let $dmvtMap$$=new Map;$allMaVt$$.size>0&&(await dmvt.find({id_app:$id_app$$,ma_vt:{$in:[...$allMaVt$$]}},{ma_vt:1, ten_vt:1,dvt:1,ma_nvt:1,ma_nvt2:1,ma_nvt3:1,ma_nvt4:1}).lean()).forEach($d$$=>$dmvtMap$$.set($d$$.ma_vt,$d$$));$log$$("stocksummary: group by");const $dataMap$$=new Map,$getGroupKey$$=$item$$=>$groupBy$$.map($k$$=>$item$$[$k$$]||"").join("||");$app_processData$$=($JSCompiler_temp_const$jscomp$0$$,$type$$)=>{for(const $row$$ of $JSCompiler_temp_const$jscomp$0$$){$row$$.ma_vt&&$dmvtMap$$.has($row$$.ma_vt)&&($JSCompiler_temp_const$jscomp$0$$=$dmvtMap$$.get($row$$.ma_vt),$row$$.ten_vt=$JSCompiler_temp_const$jscomp$0$$.ten_vt, $row$$.dvt=$JSCompiler_temp_const$jscomp$0$$.dvt,$row$$.ma_nvt=$JSCompiler_temp_const$jscomp$0$$.ma_nvt,$row$$.ma_nvt2=$JSCompiler_temp_const$jscomp$0$$.ma_nvt2,$row$$.ma_nvt3=$JSCompiler_temp_const$jscomp$0$$.ma_nvt3,$row$$.ma_nvt4=$JSCompiler_temp_const$jscomp$0$$.ma_nvt4);const $key$$=$getGroupKey$$($row$$);if(!$dataMap$$.has($key$$)){$JSCompiler_temp_const$jscomp$0$$=$dataMap$$;var $JSCompiler_temp_const$$=$JSCompiler_temp_const$jscomp$0$$.set;var $JSCompiler_inline_result$$={..._.pick($row$$, $groupBy$$),ton_dau:0,du_dau:0,sl_nhap:0,sl_xuat:0,tien_nhap:0,tien_xuat:0,ten_vt:$row$$.ten_vt||"",dvt:$row$$.dvt||""};$JSCompiler_temp_const$$.call($JSCompiler_temp_const$jscomp$0$$,$key$$,$JSCompiler_inline_result$$)}$JSCompiler_temp_const$jscomp$0$$=$dataMap$$.get($key$$);$type$$==="dn"?($JSCompiler_temp_const$jscomp$0$$.ton_dau+=$row$$.ton00||0,$JSCompiler_temp_const$jscomp$0$$.du_dau+=$row$$.du00||0):$type$$==="ps"&&($JSCompiler_temp_const$jscomp$0$$.sl_nhap+=$row$$.sl_nhap||0,$JSCompiler_temp_const$jscomp$0$$.sl_xuat+= $row$$.sl_xuat||0,$JSCompiler_temp_const$jscomp$0$$.tien_nhap+=$row$$.tien_nhap||0,$JSCompiler_temp_const$jscomp$0$$.tien_xuat+=$row$$.tien_xuat||0)}};$app_processData$$($dnData$$,"dn");$app_processData$$($psData$$,"ps");let $result$$=Array.from($dataMap$$.values()).map($r$$=>{$r$$.ton_dau=utils.round($r$$.ton_dau,$f_sl$$);$r$$.du_dau=Math.roundBy($r$$.du_dau,$f_tien$$);$r$$.sl_nhap=utils.round($r$$.sl_nhap,$f_sl$$);$r$$.sl_xuat=utils.round($r$$.sl_xuat,$f_sl$$);$r$$.tien_nhap=Math.roundBy($r$$.tien_nhap, $f_tien$$);$r$$.tien_xuat=Math.roundBy($r$$.tien_xuat,$f_tien$$);$r$$.ton_cuoi=utils.round($r$$.ton_dau+$r$$.sl_nhap-$r$$.sl_xuat,$f_sl$$);$r$$.du_cuoi=Math.roundBy($r$$.du_dau+$r$$.tien_nhap-$r$$.tien_xuat,$f_tien$$);return $r$$});$result$$=$result$$.filter($r$$=>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);$result$$.sort(($a$$,$b$$)=>{const $cmpVt$$= ($a$$.ma_vt||"").localeCompare($b$$.ma_vt||"");return $cmpVt$$!==0?$cmpVt$$:($a$$.ma_kho||"").localeCompare($b$$.ma_kho||"")});console.timeEnd("[thnxt] Total "+$uniqueId$$);$fn$$&&$fn$$(null,$result$$,$groupBy$$);return $result$$}catch($error$$){throw console.error("StockSummary Error:",$error$$),$fn$$&&$fn$$($error$$.message||$error$$),$error$$;}};