UNPKG

flexbiz-server

Version:

Flexible Server

8 lines (7 loc) 3.24 kB
'use strict';const pnk=global.getModel("pnk"),sokho=global.getModel("sokho"),gt_ctgt=global.getModel("gt_ctgt"),async=require("async"),underscore=require("underscore"),moment=require("moment"),controller=require("../controllers/controller"); module.exports=async function($condition$$,$fn$$){if(!($condition$$&&$condition$$.thang&&$condition$$.nam&&$condition$$.id_app))return $fn$$("L\u1ed7i: T\u00ednh n\u0103ng n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: thang,nam,id_app,ma_yt");let $thang$$=Number($condition$$.thang),$nam$$=Number($condition$$.nam);const $app$$=await global.getModel("app").findOne({_id:$condition$$.id_app},{options:1}).lean();if(!$app$$)return $fn$$("C\u00f4ng ty n\u00e0y kh\u00f4ng t\u1ed3n t\u1ea1i");const $f_tien$$= ($app$$.options||{}).f_tien||0;(new Promise(($resole$$,$reject$$)=>{setImmediate(()=>{let $cond$$={id_app:$condition$$.id_app,nam:$nam$$,thang:$thang$$,yt_trung_gian:!1};$condition$$.ma_bp&&($cond$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($cond$$.id_lenhsx=$condition$$.id_lenhsx);gt_ctgt.find($cond$$).lean().exec(($e$$,$ctgt$$)=>{if($e$$)return $reject$$($e$$);$ctgt$$.forEach($c$$=>{"2"==$c$$.cong_tru&&($c$$.gt=-$c$$.gt, $c$$.gt_nt=-$c$$.gt_nt,$c$$.t_gt=-$c$$.t_gt,$c$$.t_gt_nt=-$c$$.t_gt_nt)});$resole$$($ctgt$$)})})})).then($ctgt$$=>new Promise(($resolve$$,$reject$$)=>{$ctgt$$.groupBy(["ma_sp","ma_bp","id_lenhsx"],["gt","gt_nt","t_gt","t_gt_nt"],($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resolve$$($rs$$)})})).then($gts$$=>{let $ctrl$$=global.controllers.PNK;var $cond$$=underscore.pluck($gts$$,"ma_sp");$cond$$={id_app:$condition$$.id_app,ma_gd:"1",ma_ct:"PNK",ma_vt:{$in:$cond$$}};$condition$$.ma_bp&&($cond$$.ma_bp= $condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($cond$$.id_lenhsx=$condition$$.id_lenhsx);let $tu_ngay$$=moment(new Date($nam$$,$thang$$-1,1)).startOf("date").toDate(),$den_ngay$$=moment(new Date($nam$$,$thang$$,0)).endOf("date").toDate();$cond$$.ngay_ct={$gte:$tu_ngay$$,$lte:$den_ngay$$};sokho.find($cond$$).lean().exec(($e$jscomp$3$$,$rs$jscomp$0$$)=>{if($e$jscomp$3$$)return $fn$$($e$jscomp$3$$);$rs$jscomp$0$$.groupBy(["id_ct"], ["sl_nhap"],($e$jscomp$2$$,$vs$$)=>{if($e$jscomp$2$$)return $fn$$($e$jscomp$2$$);async.map($vs$$,($v$$,$callback$$)=>{pnk.findOne({_id:$v$$.id_ct},($e$jscomp$1$$,$p$$)=>{if($e$jscomp$1$$)return $callback$$($e$jscomp$1$$);if(!$p$$)return $callback$$();setImmediate(()=>{$p$$.details.forEach($detail$$=>{$detail$$.ma_bp||($detail$$.ma_bp="");$detail$$.id_lenhsx||($detail$$.id_lenhsx="");let $gt$$=underscore.find($gts$$,$gt$$=>$gt$$.ma_sp==$detail$$.ma_vt&&$gt$$.ma_bp==$detail$$.ma_bp&&$gt$$.id_lenhsx== $detail$$.id_lenhsx);$gt$$&&($detail$$.gia_von=$gt$$.gt,$detail$$.gia_von_nt=$detail$$.gia_von,$detail$$.tien_nhap=Math.roundBy($detail$$.gia_von*$detail$$.sl_nhap,$f_tien$$),$detail$$.tien_nhap_nt=$detail$$.tien_nhap)});$p$$.save(($e$jscomp$0$$,$n_p$$)=>{if($e$jscomp$0$$)return $callback$$($e$jscomp$0$$);controller.postData($n_p$$.toObject(),$ctrl$$,function($e$$){$callback$$($e$$)})})})})},($e$$,$rs$$)=>{$fn$$($e$$,$rs$$)})})})}).catch($e$$=>{$fn$$($e$$)})};