flexbiz-server
Version:
Flexible Server
22 lines (21 loc) • 10.2 kB
JavaScript
'use strict';const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Tdttno_tt=global.getModel("tdttno_tt"),Tdttno=global.getModel("tdttno"),dvcs=global.getModel("dvcs"),account=global.getModel("account"),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=50,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$$;"VND"==$obj$$.ma_nt&&($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$$,"VND"===$obj$$.ma_nt&&($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&&0<$obj$$.tdttnos.length&&($obj$$.details=$obj$$.tdttnos);$next$$()}});$contr_router$$.route();$contr_router$$.post=function($obj$$,$fn$$){$fn$$||($fn$$=function(){});$obj$$.tdttnos=$obj$$.tdttnos||[];$obj$$.tdttnos.forEach($tdttno$$=>{$tdttno$$.con_lai_nt*$tdttno$$.ty_gia_hd-
$tdttno$$.tien_nt*$obj$$.ty_gia<=tien_cl_can_xu_ly&&($tdttno$$.tien=$tdttno$$.con_lai_nt*$tdttno$$.ty_gia_hd,$tdttno$$.tien_nt=$tdttno$$.tien/$obj$$.ty_gia,$tdttno$$.thanh_toan_qd=$tdttno$$.con_lai_nt)});async.series({post_tdttno:function($callback$jscomp$0$$){(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$jscomp$0$$($e$$);async.mapSeries($details$$,($detail$$,
$callback$$)=>{$detail$$.con_lai_nt<=$detail$$.thanh_toan_qd?Tdttno.findOneAndUpdate({_id:$detail$$.id_tdtt.toString()},{$set:{tat_toan:!0}},($error$$,$result$$)=>{$callback$$($error$$,$result$$)}):$callback$$(null,$detail$$)},($e$$,$rs$$)=>{$callback$jscomp$0$$($e$$,$rs$$)})})},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||($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$$)});const $tdttnos_cl$$=$obj$$.tdttnos.filter($t$$=>$t$$.ty_gia_hd!=$obj$$.ty_gia);await $tdttnos_cl$$.asyncJoinModel2($obj$$.id_app,"currency",{where:{ma_nt_hd:"ma_nt"},fields:["tk_cl_no","tk_cl_co"]});$tdttnos_cl$$.forEach($t$$=>{let $chenh_lech$$=Math.roundBy($obj$$.ty_gia*
$t$$.tien_nt-$t$$.ty_gia_hd*$t$$.thanh_toan_qd,0);if(0!=$chenh_lech$$){const $ht$$={...$t$$,ma_nt:"VND",ty_gia:1};$ht$$.tien_nt=$ht$$.tien=Math.abs($chenh_lech$$);0>$chenh_lech$$?$ht$$.tk_no=$t$$.tk_cl_no:($ht$$.tk_no=$t$$.tk_co,$ht$$.tk_co=$t$$.tk_cl_co);$ht$$.ma_kh||($ht$$.ma_kh=$obj$$.ma_kh);$details_sc$$.push($ht$$)}});(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&&0<$obj$$.tdttnos.length&&(await $obj$$.tdttnos.asyncJoinModel2($user$$.current_id_app,Tdttno,{where:{id_tdtt:"_id"},fields:["tat_toan",{tien_hd_nt:"tien_nt"}]}),0<(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 0>=$con_lai$$?{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))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.mapSeries($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_pt1:$obj$$._id.toString(),trang_thai:"7"},($e$jscomp$1$$,$rs$$)=>{console.log("update pn6",$obj$$.id_pn6,$rs$$);pn6.findOne({_id:$obj$$.id_pn6},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PN6,$e$$=>
{$e$$&&console.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_pn6&&pn6.updateMany({_id:$obj$$.id_pn6},{id_pt1:""},()=>{pn6.updateMany({_id:$obj$$.id_pn6,trang_thai:"7"},{trang_thai:"5"},($e$jscomp$1$$,$rs$$)=>{console.log("update pn6",$obj$$.id_pn6,$rs$$);pn6.findOne({_id:$obj$$.id_pn6},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PN6,$e$$=>{$e$$&&console.error($e$$)})})})})});$contr_router$$.view=function($user$$,$items$$,$fn$$){let $id_app$$=
$user$$.current_id_app;async.series({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$$()})},tk:function($callback$$){$items$$.filter($item$$=>$item$$.tk_no).joinModel($id_app$$,
account,[{akey:"tk_no",bkey:"tk",fields:[{name:"ten_tk_no",value:"ten_tk"}]}],function(){$callback$$()})},dv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dvcs).joinModel($id_app$$,dvcs,[{akey:"ma_dvcs",bkey:"_id",fields:[{name:"ten_dvcs",value:"ten_dvcs"}]}],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_tk:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.tk_co).joinModel($id_app$$,
account,[{akey:"tk_co",bkey:"tk",fields:[{name:"ten_tk_co",value:"ten_tk"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_tk_tdttno:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.tdttnos||[]).filter($item$$=>$item$$.tk_co).joinModel($id_app$$,account,[{akey:"tk_co",bkey:"tk",fields:[{name:"ten_tk_co",value:"ten_tk"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_contract_tdttno:function($callback$$){async.mapSeries($items$$,
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.mapSeries($items$$,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.mapSeries($items$$,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.mapSeries($items$$,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.mapSeries($items$$,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.mapSeries($items$$,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$$)})}};