flexbiz-server
Version:
Flexible Server
10 lines (9 loc) • 4.26 kB
JavaScript
'use strict';const vsocai=require("../../libs/vsocai"),dmkh=global.getModel("customer"),dmkc=global.getModel("dmkc"),moment=require("moment"),async=require("async"),underscore=require("underscore"),controller=require("../../controllers/controllerRPT");
module.exports=function($router$$){new controller($router$$,"getdk4pkc",function($req$$,$callback$jscomp$0$$){const $condition$$=$req$$.query;if(!$condition$$.thang||!$condition$$.nam||!$condition$$.id_app)return $callback$jscomp$0$$("Ch\u1ee9c n\u0103ng n\u00e0y y\u00eau c\u1ea7u tham s\u1ed1: thang,nam,id_app");$condition$$.thang=Number($condition$$.thang);const $ngay_dau_thang$$=moment([$condition$$.nam,$condition$$.thang,1]).startOf("month").toDate(),$ngay_cuoi_thang$$=moment($ngay_dau_thang$$).endOf("month").toDate();
let $report$$=[];dmkc.find({status:!0,id_app:$condition$$.id_app}).sort({stt:1}).lean().exec(function($error$jscomp$0$$,$kcs$$){if($error$jscomp$0$$)return $callback$jscomp$0$$($error$jscomp$0$$);async.mapSeries($kcs$$,function($kc$$,$callback$$){const $tk$$=$kc$$.tk_chuyen,$query$$={tk:{$regex:"^"+$tk$$,$options:"i"},id_app:$condition$$.id_app};$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.id_ct&&($query$$.id_ct={$ne:$condition$$.id_ct});$query$$.ngay_ct={$gte:$ngay_dau_thang$$,
$lte:$ngay_cuoi_thang$$};vsocai.find($query$$,function($error$$,$data_socai$$){if($error$$)return $callback$$($error$$);$report$$.forEach(function($r$$){0==$r$$.tk_no.indexOf($tk$$)&&$data_socai$$.push({...$r$$,tk:$r$$.tk_no,ps_no_nt:$r$$.tien_nt,ps_no:$r$$.tien,ps_co_nt:0,ps_co:0});0==$r$$.tk_co.indexOf($tk$$)&&$data_socai$$.push({...$r$$,tk:$r$$.tk_co,ps_co_nt:$r$$.tien_nt,ps_co:$r$$.tien,ps_no_nt:0,ps_no:0})});$error$$=["tk"];!0!==$kc$$.not_nt_yn?$error$$.push("ma_nt"):$data_socai$$.forEach($r$$=>
{$r$$.ps_no_nt=$r$$.ps_no;$r$$.ps_co_nt=$r$$.ps_co});$kc$$.kho_yn&&$error$$.push("ma_kho");$kc$$.kh_yn&&$error$$.push("ma_kh");$kc$$.phi_yn&&$error$$.push("ma_phi");$kc$$.dt_yn&&$error$$.push("ma_dt");$kc$$.bp_yn&&$error$$.push("ma_bp");$kc$$.sp_yn&&$error$$.push("ma_sp");$kc$$.hd_yn&&$error$$.push("ma_hd");$data_socai$$.groupBy($error$$,["ps_no_nt","ps_co_nt","ps_no","ps_co"],($e$$,$groups$$)=>{const $kqs$$=[];$groups$$.forEach($kq$$=>{$kq$$.ma_kh_no=$kq$$.ma_kh;$kq$$.ma_kh_co=$kq$$.ma_kh;if("1"==
$kc$$.loai_kc){var $tien_tien$$=$kq$$.ps_no-$kq$$.ps_co;$kq$$.tien_nt=$kq$$.ps_no_nt-$kq$$.ps_co_nt;$kq$$.tien=$tien_tien$$;$kq$$.tk_no=$kc$$.tk_nhan;$kq$$.tk_co=$kq$$.tk}else $tien_tien$$=$kq$$.ps_co-$kq$$.ps_no,$kq$$.tien_nt=$kq$$.ps_co_nt-$kq$$.ps_no_nt,$kq$$.tien=$tien_tien$$,$kq$$.tk_no=$kq$$.tk,$kq$$.tk_co=$kc$$.tk_nhan;if($kc$$.chi_kc_gt_duong||0==$kq$$.tk_no.indexOf("421")||0==$kq$$.tk_co.indexOf("421"))$kq$$.tien_nt=0<$kq$$.tien_nt?$kq$$.tien_nt:0,$kq$$.tien=0<$kq$$.tien?$kq$$.tien:0;"VND"==
$kq$$.ma_nt?($kq$$.tien_nt=$kq$$.tien,$kq$$.ty_gia=1):$kq$$.ty_gia=$kq$$.tien_nt?Math.roundBy($kq$$.tien/$kq$$.tien_nt,0):1;0===$kq$$.tien_nt&&0===$kq$$.tien||$kqs$$.push($kq$$)});$kqs$$.forEach($kq$$=>{delete $kq$$.tk;$report$$.push($kq$$)})});$callback$$(null)})},function($error$$){if($error$$)return $callback$jscomp$0$$($error$$);$report$$=underscore.filter($report$$,function($r$$){return 0!==$r$$.tien_nt||0!==$r$$.tien});$report$$.forEach(function($r$$,$i$$){$r$$.line=$i$$+1});async.series({details_bp:function($callback$$){$report$$.filter($r$$=>
$r$$.ma_bp).joinModel2($condition$$.id_app,"dmbp",[{where:{ma_bp:"ma_bp"},fields:{ten_bp:"ten_bp"}}],function(){$callback$$()})},details_kho:function($callback$$){$report$$.filter($r$$=>$r$$.ma_kho).joinModel2($condition$$.id_app,"dmkho",[{where:{ma_kho:"ma_kho"},fields:{ten_kho:"ten_kho"}}],function(){$callback$$()})},details_dt:function($callback$$){$report$$.filter($r$$=>$r$$.ma_dt).joinModel2($condition$$.id_app,"dmdt",[{where:{ma_dt:"ma_dt"},fields:{ten_dt:"ten_dt"}}],function(){$callback$$()})},
details_customer:function($callback$$){$report$$.filter($r$$=>$r$$.ma_kh_no||$r$$.ma_kh_co).joinModel2($condition$$.id_app,dmkh,[{where:{ma_kh_no:"ma_kh"},fields:[{ten_kh_no:"ten_kh"}]},{where:{ma_kh_co:"ma_kh"},fields:{ten_kh_co:"ten_kh"}}],function(){$callback$$()})}},function(){$report$$.forEach($r$$=>{delete $r$$._id});$callback$jscomp$0$$(null,$report$$)})})})},{cache:!1})};