UNPKG

flexbiz-server

Version:

Flexible Server

14 lines (13 loc) 6.12 kB
const Account=global.getModel("account"),Customer=global.getModel("customer"),contract=global.getModel("contract"),dmphi=global.getModel("dmphi"),dmdt=global.getModel("dmdt"),dmnv=global.getModel("dmnv"),dmvt=global.getModel("dmvt"),dmbp=global.getModel("dmbp"),group=global.getModel("group"),Socai=global.getModel("socai"),Lenhsx=global.getModel("lenhsx"),underscore=require("underscore"),async=require("async"),controller=require("../../controllers/controllerRPT"),{getCodeChildren}=require("../../libs/utils"), moment=require("moment"); module.exports=function($router$$){new controller($router$$,"bkct",async function($req$$,$callback$$){const $query$$={};$query$$.id_app=$req$$.query.id_app;if($req$$.query.q){var $no_phis_phis$jscomp$1_phis$$=JSON.parse($req$$.query.q);underscore.extend($query$$,$no_phis_phis$jscomp$1_phis$$)}else{$query$$.$and=[];$req$$.query.tu_ngay&&$query$$.$and.push({ngay_ct:{$gte:$req$$.query.tu_ngay}});$req$$.query.den_ngay&&$query$$.$and.push({ngay_ct:{$lte:$req$$.query.den_ngay}});$req$$.query.dien_giai&& ($query$$.dien_giai={$regex:$req$$.query.dien_giai,$options:"i"});$req$$.query.so_ct&&($query$$.so_ct={$regex:$req$$.query.so_ct,$options:"i"});$req$$.query.ma_kh&&($query$$.$and.push({$or:[{ma_kh_no:$req$$.query.ma_kh}]}),$query$$.$and.push({$or:[{ma_kh_co:$req$$.query.ma_kh}]}));$req$$.query.ma_hd&&($query$$.ma_hd=$req$$.query.ma_hd);$req$$.query.ma_dt&&($query$$.ma_dt=$req$$.query.ma_dt);$req$$.query.ma_phi&&($no_phis_phis$jscomp$1_phis$$=await getCodeChildren(dmphi,$req$$.query.id_app,$req$$.query.ma_phi, "ma_phi","ma_phi_me"),$query$$.ma_phi={$in:$no_phis_phis$jscomp$1_phis$$});$req$$.query.ma_dt&&($no_phis_phis$jscomp$1_phis$$=await getCodeChildren(dmdt,$req$$.query.id_app,$req$$.query.ma_dt,"ma_dt","ma_dt_me"),$query$$.ma_dt={$in:$no_phis_phis$jscomp$1_phis$$});$req$$.query.ma_bp&&($no_phis_phis$jscomp$1_phis$$=await getCodeChildren(dmbp,$req$$.query.id_app,$req$$.query.ma_bp,"ma_bp","ma_bp_me"),$query$$.ma_bp={$in:$no_phis_phis$jscomp$1_phis$$});$req$$.query.ma_nv&&($query$$.ma_nv=$req$$.query.ma_nv); $req$$.query.ma_dvcs&&mongoose.Types.ObjectId.isValid($req$$.query.ma_dvcs)&&($query$$.ma_dvcs=$req$$.query.ma_dvcs);$req$$.query.id_lenhsx&&mongoose.Types.ObjectId.isValid($req$$.query.id_lenhsx)&&($query$$.id_lenhsx=$req$$.query.id_lenhsx);$req$$.query.ma_kho&&($query$$.ma_kho=$req$$.query.ma_kho);if($req$$.query.tk){$no_phis_phis$jscomp$1_phis$$={tk_no:{$regex:"^"+$req$$.query.tk,$options:"i"}};let $co$$={tk_co:{$regex:"^"+$req$$.query.tk,$options:"i"}};$req$$.query.tk_du&&($no_phis_phis$jscomp$1_phis$$.tk_co= {$regex:"^"+$req$$.query.tk_du,$options:"i"},$co$$.tk_no={$regex:"^"+$req$$.query.tk_du,$options:"i"});$query$$.$and||($query$$.$and=[]);$query$$.$and.push({$or:[$no_phis_phis$jscomp$1_phis$$,$co$$]})}$query$$.$and.length===0&&delete $query$$.$and}Socai.find($query$$).lean().then(function($rp$$){$rp$$.groupBy("id_app ma_dvcs id_ct ma_ct ma_gd ngay_ct so_ct ngay_hd so_hd tk_no tk_co ma_kh_no ma_kh_co ma_nt ty_gia dien_giai ma_bp id_lenhsx ma_phi ma_hd ma_dt ma_nv ma_sp ma_vt ma_cp".split(" "),["tien", "tien_nt"],($e$$,$rp$$)=>{$rp$$.forEach(function($r$$){$r$$.systotal=0});async.parallel([function($callback$$){$rp$$.joinModel2($req$$.query.id_app,Account,[{where:{tk_no:"tk"},fields:[{name:"ten_tk_no",value:"ten_tk"}]},{where:{tk_co:"tk"},fields:[{name:"ten_tk_co",value:"ten_tk"}]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_kh_no).joinModel2($req$$.query.id_app,Customer,[{where:{ma_kh_no:"ma_kh"},fields:[{name:"ten_kh_no",value:"ten_kh"},{name:"nh_kh_no",value:"nh_kh"}]}], function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_kh_co).joinModel2($req$$.query.id_app,Customer,[{where:{ma_kh_co:"ma_kh"},fields:[{name:"ten_kh_co",value:"ten_kh"},{name:"nh_kh_co",value:"nh_kh"}]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_hd).joinModel2($req$$.query.id_app,contract,[{where:{ma_hd:"so_hd"},fields:[{name:"ten_hd",value:"ten_hd"}]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_dt).joinModel2($req$$.query.id_app, dmdt,[{where:"ma_dt",fields:["ten_dt"]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_phi).joinModel2($req$$.query.id_app,dmphi,[{where:"ma_phi",fields:["ten_phi"]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_nv).joinModel2($req$$.query.id_app,dmnv,[{where:"ma_nv",fields:["ten_nv"]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_vt).joinModel2($req$$.query.id_app,dmvt,[{where:"ma_vt",fields:["ten_vt"]}], function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.ma_sp).joinModel2($req$$.query.id_app,dmvt,[{where:{ma_sp:"ma_vt"},fields:[{ten_sp:"ten_vt"}]}],function(){$callback$$()})},function($callback$$){$rp$$.filter($r$$=>$r$$.id_lenhsx).joinModel2($req$$.query.id_app,Lenhsx,[{where:{id_lenhsx:"_id"},fields:[{so_lenhsx:"so_ct"}]}],function(){$callback$$()})},function($callback$$){async.map($rp$$.filter($r$$=>$r$$.ma_ct),($item$$,$cb$$)=>{global.getModel($item$$.ma_ct.toLowerCase()).findOne({_id:$item$$.id_ct}, {_id:1}).then($rs$$=>{$item$$.id_ct_goc=$rs$$?$rs$$._id:void 0;$cb$$()}).catch(()=>{$cb$$()})},()=>{$callback$$()})}],function($error$$){if($req$$.query.co_ct_goc==0||$req$$.query.co_ct_goc=="false")$rp$$=$rp$$.filter($r$$=>!$r$$.id_ct_goc);if($req$$.query.co_ct_goc==1||$req$$.query.co_ct_goc=="true")$rp$$=$rp$$.filter($r$$=>$r$$.id_ct_goc);$rp$$.filter($r$$=>$r$$.nh_kh_no||$r$$.nh_kh_co).joinModel2($req$$.query.id_app,group,[{where:{nh_kh_no:"_id"},fields:[{name:"ten_nh_kh_no",value:"group_name"}]}, {where:{nh_kh_co:"_id"},fields:[{name:"ten_nh_kh_co",value:"group_name"}]}],function(){$rp$$=underscore.sortBy($rp$$,function($r$$){return($r$$.ngay_ct?moment($r$$.ngay_ct).format("YYYYMMDD"):"0")+$r$$.ma_ct+$r$$.so_ct});const $rt$$={dien_giai:"T\u1ed5ng c\u1ed9ng",tien:$rp$$.csum("tien"),tien_nt:$rp$$.csum("tien_nt"),systotal:1,bold:!0};$rp$$.push($rt$$);$callback$$($error$$,$rp$$)})})})}).catch($error$$=>{$callback$$($error$$)})})};