flexbiz-server
Version:
Flexible Server
9 lines (8 loc) • 3.53 kB
JavaScript
const dmtk=global.getModel("account"),dmkh=global.getModel("customer"),socaitmp=global.getModel("socaitmp"),socai=global.getModel("socai"),ptthanhtoan=global.getModel("ptthanhtoan"),moment=require("moment"),async=require("async"),controller=require("../../controllers/controllerRPT");
module.exports=function($router$$){new controller($router$$,"getdk4pht",async function($query_req_tu_ngay$$,$callback$$){const $condition$$=$query_req_tu_ngay$$.query;$condition$$.thang&&$condition$$.nam&&($condition$$.tu_ngay=moment([$condition$$.nam,Number($condition$$.thang),1]).startOf("month").toDate(),$condition$$.den_ngay=moment($condition$$.tu_ngay).toDate());if(!$condition$$.tu_ngay||!$condition$$.den_ngay||!$condition$$.id_app)return $callback$$("Ch\u1ee9c n\u0103ng n\u00e0y y\u00eau c\u1ea7u tham s\u1ed1: tu_ngay,den_ngay,id_app");
$query_req_tu_ngay$$=moment($condition$$.tu_ngay).startOf("date").toDate();var $da_lay_den_ngay_query_da_post$$=moment($condition$$.den_ngay).endOf("date").toDate();$query_req_tu_ngay$$={id_app:$condition$$.id_app,ngay_ct:{$gte:$query_req_tu_ngay$$,$lte:$da_lay_den_ngay_query_da_post$$}};$condition$$.ma_ct&&($query_req_tu_ngay$$.ma_ct=$condition$$.ma_ct);$condition$$.ma_dvcs&&($query_req_tu_ngay$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_kho&&($query_req_tu_ngay$$.ma_kho=$condition$$.ma_kho);
$condition$$.ma_bp&&($query_req_tu_ngay$$.ma_bp=$condition$$.ma_bp);$condition$$.ma_dt&&($query_req_tu_ngay$$.ma_dt=$condition$$.ma_dt);$condition$$.ma_kh&&($query_req_tu_ngay$$.$or=[{ma_kh_no:$condition$$.ma_kh},{ma_kh_co:$condition$$.ma_kh}]);$condition$$.pt_thanh_toan&&($query_req_tu_ngay$$.pt_thanh_toan=$condition$$.pt_thanh_toan);$da_lay_den_ngay_query_da_post$$={...$query_req_tu_ngay$$,ma_ct:"PHT"};delete $da_lay_den_ngay_query_da_post$$.ngay_ct;$da_lay_den_ngay_query_da_post$$=(await socai.find($da_lay_den_ngay_query_da_post$$,
{id_ct_goc:1}).lean()).map($d$$=>$d$$.id_ct_goc);$query_req_tu_ngay$$.id_ct={$nin:$da_lay_den_ngay_query_da_post$$};socaitmp.find($query_req_tu_ngay$$).sort({stt:1}).lean().exec(function($error$$,$report$$){if($error$$)return $callback$$($error$$);$report$$.forEach(function($r$$,$i$$){$r$$.line=$i$$+1;$r$$.id_ct_goc=$r$$.id_ct;$r$$.ma_ct_goc=$r$$.ma_ct;$r$$.so_ct_goc=$r$$.so_ct;$r$$.ngay_ct_goc=$r$$.ngay_ct;$r$$.pt_thanh_toan_goc=$r$$.pt_thanh_toan;delete $r$$.id_ct;delete $r$$.ma_ct});async.parallel({details_tk:function($callback$$){$report$$.joinModel2($condition$$.id_app,
dmtk,[{where:{tk_no:"tk"},fields:{ten_tk_no:"ten_tk"}},{where:{tk_co:"tk"},fields:{ten_tk_co:"ten_tk"}}],function(){$callback$$()})},details_customer:function($callback$$){$report$$.filter($r$$=>$r$$.ma_kh_co||$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$$()})},details_ptthanhtoan:function($callback$$){$report$$.filter($r$$=>$r$$.pt_thanh_toan_goc).joinModel2($condition$$.id_app,
ptthanhtoan,[{where:{pt_thanh_toan_goc:"_id"},fields:[{ten_pt_thanh_toan_goc:"ten"}]}],function(){$callback$$()})},details_ctgoc:function($callback$$){async.mapLimit($report$$.filter($r$$=>$r$$.id_ct_goc&&$r$$.ma_ct_goc),20,async $item$$=>{try{let $ctgoc$$=await global.getModel($item$$.ma_ct_goc.toLowerCase()).findOne({_id:$item$$.id_ct_goc}).lean();$item$$.ctgoc=$ctgoc$$}catch($e$$){Logger.error($e$$)}return $item$$},()=>{$callback$$()})}},function(){$report$$.forEach($r$$=>{delete $r$$._id});$callback$$(null,
$report$$)})})},{cache:!1})};