UNPKG

flexbiz-server

Version:

Flexible Server

21 lines (20 loc) 9.83 kB
const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Tdttno_tt=global.getModel("tdttno_tt"),Tdttno=global.getModel("tdttno"),dmphi=global.getModel("dmphi"),customer=global.getModel("customer"),dmnv=global.getModel("dmnv"),dmdt=global.getModel("dmdt"),contract=global.getModel("contract"),model=global.getModel("pt1"),pn6=global.getModel("pn6"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"),async=require("async"),ma_ct="PT1", {round}=require("../../libs/utils"),tien_cl_can_xu_ly=5E3,valid_detail=function($i_i$$,$obj$$,$fn$$){for($i_i$$=0;$i_i$$<$obj$$.details.length;$i_i$$++){var $detail_tdttno$$=$obj$$.details[$i_i$$];$detail_tdttno$$.line=$i_i$$;$obj$$.ma_nt=="VND"&&($detail_tdttno$$.tien=Math.roundBy($detail_tdttno$$.tien_nt,0))}if($obj$$.tdttnos)for($i_i$$=0;$i_i$$<$obj$$.tdttnos.length;$i_i$$++)$detail_tdttno$$=$obj$$.tdttnos[$i_i$$],$detail_tdttno$$.line=$i_i$$,$obj$$.ma_nt==="VND"&&($detail_tdttno$$.tien=$detail_tdttno$$.tien_nt), $detail_tdttno$$.thanh_toan_qd=$detail_tdttno$$.ma_nt_hd==$obj$$.ma_nt?$detail_tdttno$$.tien_nt:round($detail_tdttno$$.tien/$detail_tdttno$$.ty_gia_hd,2);$fn$$(null,$obj$$)}; module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1},keyValues:{ma_ct},onExport:function($user$$,$obj$$,$next$$){$obj$$.tdttnos&&$obj$$.tdttnos.length>0&&($obj$$.details=$obj$$.tdttnos);$next$$()}});$contr_router$$.route();$contr_router$$.post=async function($obj$$,$fn$$){$fn$$||=function(){};const $appInfo$$=await global.getModel("app").findOne({_id:$obj$$.id_app}).lean();$obj$$.tdttnos=$obj$$.tdttnos|| [];async.series({post_tdttno:function($callback$$){(new PostBook($obj$$,$obj$$.tdttnos,Tdttno_tt,function($tdttno$$,$callback$$){$tdttno$$.tk_no=$obj$$.tk_no;$callback$$($tdttno$$)})).run(function($e$$,$details$$){if($e$$)return $callback$$($e$$);$callback$$(null,$details$$)})},post_socai:function($callback$$){setImmediate(async()=>{const $details_sc$$=[];$obj$$.details.forEach(function($d_detail$$){$d_detail$$={...$d_detail$$};$d_detail$$.tk_no=$obj$$.tk_no;$d_detail$$.ma_kh_no=$obj$$.ma_kh_no|| $obj$$.ma_kh;$d_detail$$.ma_kh||($d_detail$$.ma_kh=$obj$$.ma_kh);$details_sc$$.push($d_detail$$)});$obj$$.tdttnos.forEach($t_tdttno$$=>{$t_tdttno$$={...$t_tdttno$$};$t_tdttno$$.tk_no=$obj$$.tk_no;$t_tdttno$$.ma_kh||($t_tdttno$$.ma_kh=$obj$$.ma_kh);$details_sc$$.push($t_tdttno$$)});await $obj$$.tdttnos.filter($t$$=>$t$$.ty_gia_hd!=$obj$$.ty_gia&&$t$$.ma_nt_hd==$obj$$.ma_nt).asyncJoinModel2($obj$$.id_app,"currency",{where:{ma_nt_hd:"ma_nt"},fields:["tk_cl_no","tk_cl_co"]});for(let $t$$ of $obj$$.tdttnos){var $con_lai_nt_ht_so_du_hd$$= await Tdttno.getBalance({id_app:$obj$$.id_app,id_ct:$obj$$._id.toString(),_id:$t$$.id_tdtt});if($con_lai_nt_ht_so_du_hd$$.length==1){let $con_lai$$=$con_lai_nt_ht_so_du_hd$$[0].con_lai-$t$$.tien;$con_lai_nt_ht_so_du_hd$$=$con_lai_nt_ht_so_du_hd$$[0].con_lai_nt-$t$$.thanh_toan_qd;if(Math.abs($con_lai$$)<=tien_cl_can_xu_ly||$con_lai_nt_ht_so_du_hd$$<=0)$con_lai$$&&($con_lai_nt_ht_so_du_hd$$={...$t$$,ma_nt:"VND",ty_gia:1},$con_lai_nt_ht_so_du_hd$$.tien_nt=$con_lai_nt_ht_so_du_hd$$.tien=Math.abs($con_lai$$), $con_lai_nt_ht_so_du_hd$$.ma_kh||($con_lai_nt_ht_so_du_hd$$.ma_kh=$obj$$.ma_kh),$t$$.ty_gia_hd!=$obj$$.ty_gia&&$t$$.ma_nt_hd==$obj$$.ma_nt?($t$$.chenh_lech_ty_gia=$con_lai$$,$con_lai$$<0?($con_lai_nt_ht_so_du_hd$$.tk_no=$t$$.tk_co,$con_lai_nt_ht_so_du_hd$$.tk_co=$t$$.tk_cl_co):($con_lai_nt_ht_so_du_hd$$.tk_no=$t$$.tk_cl_no,$con_lai_nt_ht_so_du_hd$$.tk_co=$t$$.tk_co),$details_sc$$.push($con_lai_nt_ht_so_du_hd$$)):$appInfo$$?.options.tk_tn_khac&&$appInfo$$?.options.tk_cp_khac&&($con_lai$$<0?($con_lai_nt_ht_so_du_hd$$.tk_no= $t$$.tk_co,$con_lai_nt_ht_so_du_hd$$.tk_co=$appInfo$$?.options.tk_tn_khac):($con_lai_nt_ht_so_du_hd$$.tk_no=$appInfo$$?.options.tk_cp_khac,$con_lai_nt_ht_so_du_hd$$.tk_co=$t$$.tk_co),$details_sc$$.push($con_lai_nt_ht_so_du_hd$$))),await Tdttno.updateOne({_id:$t$$.id_tdtt.toString()},{tat_toan:!0}).catch(Logger.error)}}(new PostSocai($obj$$,$details_sc$$)).run(($e$$,$rs$$)=>{$callback$$($e$$,$rs$$)})})}},($e$$,$rs$$)=>{$fn$$($e$$,$rs$$)})};$contr_router$$.creating=async function($user$$,$obj$$,$next$$){$obj$$.details|| ($obj$$.details=[]);if($obj$$.tdttnos&&$obj$$.tdttnos.length>0&&(await $obj$$.tdttnos.asyncJoinModel2($user$$.current_id_app,Tdttno,{where:{id_tdtt:"_id"},fields:["tat_toan",{tien_hd_nt:"tien_nt"}]}),(await Promise.all($obj$$.tdttnos.map($hd$$=>(async()=>{if(!global.mongoose.Types.ObjectId.isValid($hd$$.id_tdtt))return{error:"Ho\u00e1 \u0111\u01a1n kh\u00f4ng h\u1ee3p l\u1ec7",invoice:$hd$$};let $ds_da_thanh_toan$$=await Tdttno_tt.find({id_app:$obj$$.id_app,id_tdtt:$hd$$.id_tdtt}).lean(),$da_thanh_toan$$= $ds_da_thanh_toan$$.map($t$$=>$t$$.thanh_toan_qd||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$con_lai$$=$hd$$.tien_hd_nt-$da_thanh_toan$$;return $con_lai$$<=0?{error:"Ho\u00e1 \u0111\u01a1n \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n",invoice:$hd$$,da_thanh_toan:$da_thanh_toan$$,con_lai:$con_lai$$,ds_da_thanh_toan:$ds_da_thanh_toan$$}:{invoice:$hd$$,da_thanh_toan:$da_thanh_toan$$,con_lai:$con_lai$$,ds_da_thanh_toan:$ds_da_thanh_toan$$}})()))).filter($hd$$=>$hd$$.error).length>0))return $next$$("Ho\u00e1 \u0111\u01a1n \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n"); valid_detail($user$$,$obj$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$obj$$)})};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$next$$){valid_detail($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})};$contr_router$$.deleted=($details$jscomp$1_user$$,$obj$$,$next$$)=>{$details$jscomp$1_user$$=($obj$$.details||[]).filter($d$$=>$d$$.id_tdtt);async.map($details$jscomp$1_user$$,($detail$$,$callback$$)=>{setImmediate(()=> {Tdttno.findOneAndUpdate({_id:$detail$$.id_tdtt.toString()},{$set:{tat_toan:!1}},($error$$,$result$$)=>{$callback$$($error$$,$result$$)})})},$e$$=>{$next$$($e$$,$obj$$)})};$contr_router$$.on("saved",function($obj$$){$obj$$.id_pn6&&pn6.updateMany({_id:$obj$$.id_pn6,id_app:$obj$$.id_app},{id_pt1:$obj$$._id.toString(),trang_thai:"7"},($e$$,$rs$$)=>{Logger.info("update pn6",$obj$$.id_pn6,$rs$$);pn6.findOne({_id:$obj$$.id_pn6,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$, global.controllers.PN6,$e$$=>{$e$$&&Logger.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_pn6&&pn6.updateMany({_id:$obj$$.id_pn6,id_app:$obj$$.id_app},{id_pt1:""},()=>{pn6.updateMany({_id:$obj$$.id_pn6,id_app:$obj$$.id_app,trang_thai:"7"},{trang_thai:"5"},($e$$,$rs$$)=>{Logger.info("update pn6",$obj$$.id_pn6,$rs$$);pn6.findOne({_id:$obj$$.id_pn6,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PN6,$e$$=>{$e$$&&Logger.error($e$$)})})})})}); $contr_router$$.view=function($user$$,$items$$,$fn$$){let $id_app$$=$user$$.current_id_app;async.parallel({kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel($id_app$$,customer,[{akey:"ma_kh",bkey:"ma_kh",fields:[{name:"ten_kh",value:"ten_kh"}]}],function(){$callback$$()})},nv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_nv).joinModel($id_app$$,dmnv,[{akey:"ma_nv",bkey:"ma_nv",fields:[{name:"ten_nv",value:"ten_nv"}]}],function(){$callback$$()})},trangthai:function($callback$$){$items$$.filter($item$$=> $item$$.trang_thai).joinModel2(null,trangthai,[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["ten_trang_thai","color"]}],function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.t_tien=0;$r$$.t_tien_nt=0;$r$$.details&&($r$$.t_tien=$r$$.details.csum("tien"),$r$$.t_tien_nt=$r$$.details.csum("tien_nt"));$r$$.tdttnos&&($r$$.t_tien+=$r$$.tdttnos.csum("tien"),$r$$.t_tien_nt+=$r$$.tdttnos.csum("tien_nt"));$r$$.t_tt=$r$$.t_tien;$r$$.t_tt_nt=$r$$.t_tien_nt}); $callback$$()},details_contract_tdttno:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.tdttnos||[]).filter($item$$=>$item$$.ma_hd).joinModel($id_app$$,contract,[{akey:"ma_hd",bkey:"so_hd",fields:[{name:"ten_hd",value:"ten_hd"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_dt_tdttno:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.tdttnos||[]).filter($item$$=>$item$$.ma_dt).joinModel($id_app$$,dmdt,[{akey:"ma_dt", bkey:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_customer:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_kh).joinModel($id_app$$,customer,[{akey:"ma_kh",bkey:"ma_kh",fields:[{name:"ten_kh",value:"ten_kh"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_dt:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details|| []).filter($item$$=>$item$$.ma_dt).joinModel($id_app$$,dmdt,[{akey:"ma_dt",bkey:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_contract:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_hd).joinModel($id_app$$,contract,[{akey:"ma_hd",bkey:"so_hd",fields:[{name:"ten_hd",value:"ten_hd"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_phi:function($callback$$){async.mapLimit($items$$, 20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_phi).joinModel($id_app$$,dmphi,[{akey:"ma_phi",bkey:"ma_phi",fields:[{name:"ten_phi",value:"ten_phi"}]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){$fn$$(null,$items$$)})}};