UNPKG

flexbiz-server

Version:

Flexible Server

8 lines (7 loc) 3.06 kB
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$$)=>{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$$).inTxn().lean().then($ctgt$$=>{$ctgt$$.forEach($c$$=>{$c$$.cong_tru=="2"&&($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$$)}).catch($e$$=>{$reject$$($e$$)})})).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().then($rs$$=>{$rs$$.groupBy(["id_ct"],["sl_nhap"],($e$$,$vs$$)=>{if($e$$)return $fn$$($e$$);async.mapSeries($vs$$,($v$$,$callback$$)=>{pnk.findOne({_id:$v$$.id_ct}, ($e$$,$p$$)=>{if($e$$)return $callback$$($e$$);if(!$p$$)return $callback$$();$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$$,$n_p$$)=>{if($e$$)return $callback$$($e$$);controller.postData($n_p$$.toObject(),$ctrl$$,function($e$$){$callback$$($e$$)})})})},($e$$,$rs$$)=>{$fn$$($e$$,$rs$$)})})}).catch($e$$=>{$fn$$($e$$)})}).catch($e$$=>{$fn$$($e$$)})};