UNPKG

flexbiz-server

Version:

Flexible Server

10 lines (9 loc) 4.27 kB
const sokho=global.getModel("sokho"),sokhokhongton=global.getModel("sokhokhongton"),dinhmucsx=global.getModel("dinhmucsx"),dmvt=global.getModel("dmvt"),dmdvt=global.getModel("dmdvt"),dmdt=global.getModel("dmdt"),controller=require("../../controllers/controllerRPT"),moment=require("moment"); module.exports=function($router$$){new controller($router$$,"getdinhmuc4pxk",async($req_sokho_phat_sinh_vts$$,$callback$$)=>{const $condition$$=$req_sokho_phat_sinh_vts$$.query;if(!$condition$$.ma_ct||!$condition$$.tu_ngay||!$condition$$.den_ngay)return $callback$$("B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1:ma_ct,tu_ngay,den_ngay");var $query_sokho_phat_sinh_khong_ton$$={id_app:$condition$$.id_app,ngay_ct:{$gte:moment($condition$$.tu_ngay).startOf("date").toDate(),$lte:moment($condition$$.den_ngay).endOf("date").toDate()}, ma_ct:{$in:$condition$$.ma_ct.toUpperCase().split(",")}};$condition$$.ma_vt&&($query_sokho_phat_sinh_khong_ton$$.ma_vt=$condition$$.ma_vt);$condition$$.ma_dt&&($query_sokho_phat_sinh_khong_ton$$.ma_dt=$condition$$.ma_dt);$condition$$.ma_hd&&($query_sokho_phat_sinh_khong_ton$$.ma_hd=$condition$$.ma_hd);$condition$$.ma_kh&&($query_sokho_phat_sinh_khong_ton$$.ma_kh=$condition$$.ma_kh);$condition$$.ma_kho&&($query_sokho_phat_sinh_khong_ton$$.ma_kho=$condition$$.ma_kho);$req_sokho_phat_sinh_vts$$=await sokho.find($query_sokho_phat_sinh_khong_ton$$).lean(); $query_sokho_phat_sinh_khong_ton$$=await sokhokhongton.find($query_sokho_phat_sinh_khong_ton$$).lean();$req_sokho_phat_sinh_vts$$=$req_sokho_phat_sinh_vts$$.concat($query_sokho_phat_sinh_khong_ton$$);$req_sokho_phat_sinh_vts$$=await $req_sokho_phat_sinh_vts$$.asyncGroupBy(["ma_vt","ngay_ct"],["sl_xuat","sl_nhap"]);$req_sokho_phat_sinh_vts$$=await Promise.all($req_sokho_phat_sinh_vts$$.map($sp$$=>(async()=>{var $detail_xk_query_dinhmuc$$={id_app:$condition$$.id_app,ma_sp:$sp$$.ma_vt};let $ngay_ct$$= $sp$$.ngay_ct;$detail_xk_query_dinhmuc$$.hieu_luc_tu_ngay={$lte:moment($ngay_ct$$).startOf("date").toDate()};$detail_xk_query_dinhmuc$$.hieu_luc_den_ngay={$gte:moment($ngay_ct$$).endOf("date").toDate()};let $dinhmuc$$=await dinhmucsx.findOne($detail_xk_query_dinhmuc$$).sort({hieu_luc_tu_ngay:-1}).lean();if(!$dinhmuc$$)return[];$detail_xk_query_dinhmuc$$=$dinhmuc$$.details.map($detail$$=>{delete $detail$$._id;$detail$$.sl_dinh_muc=$detail$$.sl_dinh_muc*(1+($detail$$.ty_le_hao_hut||0)/100)/($dinhmuc$$.so_luong|| 1);$detail$$.sl_xuat=$detail$$.sl_dinh_muc*($sp$$.sl_xuat||$sp$$.sl_nhap);$detail$$.gia_von=$detail$$.gia_von_nt=0;$detail$$.tien_xuat=$detail$$.tien_xuat_nt=0;$detail$$.ma_sp=$dinhmuc$$.ma_sp;$detail$$.id_dinhmucsx=$dinhmuc$$._id.toString();return $detail$$});const $da_xk$$=await sokho.find({id_app:$condition$$.id_app,ma_ct:"PXK",ngay_ct:{$gte:moment($condition$$.tu_ngay).startOf("date").toDate(),$lte:moment($condition$$.den_ngay).endOf("date").toDate()},ma_sp:$dinhmuc$$.ma_sp,id_dinhmucsx:$dinhmuc$$._id.toString()}).lean(); $detail_xk_query_dinhmuc$$.forEach($detail$$=>{const $sl_da_xuat_nvt$$=$da_xk$$.filter($d$$=>$d$$.ma_vt===$detail$$.ma_vt).map($d$$=>$d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$detail$$.sl_xuat=Math.max(0,$detail$$.sl_xuat-$sl_da_xuat_nvt$$)});return $detail_xk_query_dinhmuc$$})()));$req_sokho_phat_sinh_vts$$=$req_sokho_phat_sinh_vts$$.reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);$req_sokho_phat_sinh_vts$$=await $req_sokho_phat_sinh_vts$$.asyncGroupBy("ma_vt ma_dvt ma_dt ma_kh ma_sp id_dinhmucsx ma_bp".split(" "), ["sl_xuat","tien_xuat","tien_xuat_nt"]);await $req_sokho_phat_sinh_vts$$.asyncJoinModel2($condition$$.id_app,dmvt,{where:"ma_vt",fields:["ten_vt","tk_vt",{tk_du:"tk_gv"}]});await $req_sokho_phat_sinh_vts$$.filter($r$$=>$r$$.ma_sp).asyncJoinModel2($condition$$.id_app,dmvt,{where:{ma_sp:"ma_vt"},fields:{ten_sp:"ten_vt"}});await $req_sokho_phat_sinh_vts$$.filter($r$$=>$r$$.ma_dvt).asyncJoinModel2($condition$$.id_app,dmdvt,{where:"ma_dvt",fields:"ten_dvt"});await $req_sokho_phat_sinh_vts$$.filter($r$$=> $r$$.ma_dt).asyncJoinModel2($condition$$.id_app,dmdt,{where:"ma_dt",fields:"ten_dt"});await $req_sokho_phat_sinh_vts$$.filter($r$$=>$r$$.ma_bp).asyncJoinModel2($condition$$.id_app,global.getModel("dmbp"),{where:"ma_bp",fields:"ten_bp"});$callback$$(null,$req_sokho_phat_sinh_vts$$)})};