flexbiz-server
Version:
Flexible Server
25 lines (24 loc) • 11.8 kB
JavaScript
const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Socai=global.getModel("socai"),Vatvao=global.getModel("vatvao"),Tdttco_tt=global.getModel("tdttco_tt"),Tdttco=global.getModel("tdttco"),dvcs=global.getModel("dvcs"),account=global.getModel("account"),dmdt=global.getModel("dmdt"),customer=global.getModel("customer"),dmnv=global.getModel("dmnv"),contract=global.getModel("contract"),pn6=global.getModel("pn6"),dmphi=global.getModel("dmphi"),model=global.getModel("pc2"),
trangthai=global.getModel("trangthai"),pc3=global.getModel("pc3"),controller=require("../../controllers/controller"),async=require("async"),ma_ct="PC2",valid_detail=function($i_i$jscomp$1_i$$,$obj$$,$fn$$){for($i_i$jscomp$1_i$$=0;$i_i$jscomp$1_i$$<$obj$$.details.length;$i_i$jscomp$1_i$$++){var $detail_tdttco_vatvao$$=$obj$$.details[$i_i$jscomp$1_i$$];$detail_tdttco_vatvao$$.line=$i_i$jscomp$1_i$$;$obj$$.ma_nt=="VND"&&($detail_tdttco_vatvao$$.tien=$detail_tdttco_vatvao$$.tien_nt)}if($obj$$.vatvaos)for($i_i$jscomp$1_i$$=
0;$i_i$jscomp$1_i$$<$obj$$.vatvaos.length;$i_i$jscomp$1_i$$++)$detail_tdttco_vatvao$$=$obj$$.vatvaos[$i_i$jscomp$1_i$$],$detail_tdttco_vatvao$$.line=$i_i$jscomp$1_i$$,$obj$$.ma_nt=="VND"&&($detail_tdttco_vatvao$$.t_thue=$detail_tdttco_vatvao$$.t_thue_nt);if($obj$$.tdttcos)for($i_i$jscomp$1_i$$=0;$i_i$jscomp$1_i$$<$obj$$.tdttcos.length;$i_i$jscomp$1_i$$++)$detail_tdttco_vatvao$$=$obj$$.tdttcos[$i_i$jscomp$1_i$$],$detail_tdttco_vatvao$$.line=$i_i$jscomp$1_i$$,$obj$$.ma_nt=="VND"&&($detail_tdttco_vatvao$$.tien=
$detail_tdttco_vatvao$$.tien_nt);$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},onExport:function($user$$,$obj$$,$next$$){$obj$$.tdttcos&&$obj$$.tdttcos.length>0&&($obj$$.details=$obj$$.tdttcos);$next$$()}});$contr_router$$.route();$contr_router$$.post=function($obj$$,$fn$$){$fn$$||=function(){};async.series({post_vatvao:function($callback$$){(new PostBook($obj$$,$obj$$.vatvaos,Vatvao)).run(function($e$$,$rs$$){$callback$$($e$$,
$rs$$)})},post_tdttco:function($callback$$){(new PostBook($obj$$,$obj$$.tdttcos,Tdttco_tt,function($tdttco$$,$callback$$){$tdttco$$.tk_co=$obj$$.tk_co;$callback$$($tdttco$$)})).run(function($e$$,$details$$){if($e$$)return $callback$$($e$$);async.map($details$$,function($detail$$,$callback$$){$detail$$.con_lai_nt<=$detail$$.thanh_toan_qd?Tdttco.findOneAndUpdate({_id:$detail$$.id_hd},{$set:{tat_toan:!0}},function($error$$,$result$$){$callback$$($error$$,$result$$)}):$callback$$(null,$detail$$)},function($e$$,
$rs$$){$callback$$($e$$,$rs$$)})})},post_socai:function($callback$$){const $details_sc$$=[];$obj$$.vatvaos.forEach(function($v_vatvao$$){$v_vatvao$$={...$v_vatvao$$};$v_vatvao$$.tk_no=$v_vatvao$$.tk_thue_no;$v_vatvao$$.tk_co=$v_vatvao$$.tk_du_thue;$v_vatvao$$.tien_nt=$v_vatvao$$.t_thue_nt;$v_vatvao$$.tien=$v_vatvao$$.t_thue;$details_sc$$.push($v_vatvao$$)});$obj$$.details.forEach(function($d_detail$$){$d_detail$$={...$d_detail$$};$d_detail$$.tk_co=$obj$$.tk_co;$d_detail$$.ma_kh||($d_detail$$.ma_kh=
$obj$$.ma_kh);$details_sc$$.push($d_detail$$)});$obj$$.tdttcos.forEach(function($t_tdttco$$){$t_tdttco$$={...$t_tdttco$$};$t_tdttco$$.tk_co=$obj$$.tk_co;$t_tdttco$$.ma_kh||($t_tdttco$$.ma_kh=$obj$$.ma_kh);$details_sc$$.push($t_tdttco$$)});(new PostSocai($obj$$,$details_sc$$)).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};$contr_router$$.creating=async function($user$$,$obj$$,$next$$){if($user$$.current_app_info.options.phieu_chi_phai_co_phieu_de_nghi&&
!$obj$$.id_pc3)return $next$$("Ch\u01b0a c\u00f3 phi\u1ebfu \u0111\u1ec1 ngh\u1ecb t\u1ea1m \u1ee9ng");$obj$$.id_pn6="";$obj$$.details||($obj$$.details=[]);if($obj$$.id_pc3){var $dntt_t_tien_dn$$=await pc3.findOne({_id:$obj$$.id_pc3,id_app:$user$$.current_id_app});if(!$dntt_t_tien_dn$$)return $next$$("\u0110\u1ec1 ngh\u1ecb t\u1ea1m \u1ee9ng kh\u00f4ng t\u1ed3n t\u1ea1i");$dntt_t_tien_dn$$=($dntt_t_tien_dn$$.details||[]).map($d$$=>$d$$.tien_nt||0)+($dntt_t_tien_dn$$.vatvaos||[]).map($d$$=>$d$$.t_thue_nt||
0);let $t_tien_tt$$=($obj$$.details||[]).map($d$$=>$d$$.tien_nt||0)+($obj$$.vatvaos||[]).map($d$$=>$d$$.t_thue_nt||0);if($dntt_t_tien_dn$$!==$t_tien_tt$$)return $next$$("T\u1ed5ng s\u1ed1 ti\u1ec1n t\u1ea1m \u1ee9ng ph\u1ea3i b\u1eb1ng v\u1edbi s\u1ed1 ti\u1ec1n trong \u0111\u1ec1 ngh\u1ecb t\u1ea1m \u1ee9ng \u0111\u00e3 \u0111\u01b0\u1ee3c duy\u1ec7t")}valid_detail($user$$,$obj$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$obj$$)})};$contr_router$$.created=function($user$$,
$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.updating=async function($user$$,$data$$,$obj$$,$next$$){var $dntt$jscomp$1_id_pc3_t_tien_dn$$=$data$$.id_pc3||$obj$$.id_pc3;if($dntt$jscomp$1_id_pc3_t_tien_dn$$){$dntt$jscomp$1_id_pc3_t_tien_dn$$=await pc3.findOne({_id:$dntt$jscomp$1_id_pc3_t_tien_dn$$,id_app:$user$$.current_id_app});if(!$dntt$jscomp$1_id_pc3_t_tien_dn$$)return $next$$("\u0110\u1ec1 ngh\u1ecb t\u1ea1m \u1ee9ng kh\u00f4ng t\u1ed3n t\u1ea1i");$dntt$jscomp$1_id_pc3_t_tien_dn$$=($dntt$jscomp$1_id_pc3_t_tien_dn$$.details||
[]).map($d$$=>$d$$.tien_nt||0)+($dntt$jscomp$1_id_pc3_t_tien_dn$$.vatvaos||[]).map($d$$=>$d$$.t_thue_nt||0);let $t_tien_tt$$=($data$$.details||$obj$$.details||[]).map($d$$=>$d$$.tien_nt||0)+($data$$.vatvaos||$obj$$.vatvaos||[]).map($d$$=>$d$$.t_thue_nt||0);if($dntt$jscomp$1_id_pc3_t_tien_dn$$!==$t_tien_tt$$)return $next$$("T\u1ed5ng s\u1ed1 ti\u1ec1n t\u1ea1m \u1ee9ng ph\u1ea3i b\u1eb1ng v\u1edbi s\u1ed1 ti\u1ec1n trong \u0111\u1ec1 ngh\u1ecb t\u1ea1m \u1ee9ng \u0111\u00e3 \u0111\u01b0\u1ee3c duy\u1ec7t")}valid_detail($user$$,
$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})};$contr_router$$.updated=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.on("saved",function($obj$$){$obj$$.id_pc3&&pc3.updateMany({_id:$obj$$.id_pc3,id_app:$obj$$.id_app},{id_pc2:$obj$$._id.toString(),trang_thai:"6"},($e$$,$rs$$)=>{Logger.info("update pc3",$rs$$);pc3.findOne({_id:$obj$$.id_pc3,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PC3,
$e$$=>{$e$$&&Logger.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_pc3&&pc3.updateMany({_id:$obj$$.id_pc3,id_app:$obj$$.id_app},{id_pc2:""},()=>{pc3.updateMany({_id:$obj$$.id_pc3,id_app:$obj$$.id_app,trang_thai:"6"},{trang_thai:"4"},($e$$,$rs$$)=>{Logger.info("update pc3",$rs$$);pc3.findOne({_id:$obj$$.id_pc3,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PC3,$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).joinModel2($id_app$$,customer,[{where:"ma_kh",fields:["ten_kh"]}],function(){$callback$$()})},nv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_nv).joinModel2($id_app$$,dmnv,[{where:"ma_nv",fields:["ten_nv"]}],function(){$callback$$()})},tk:function($callback$$){$items$$.filter($item$$=>$item$$.tk_co).joinModel2($id_app$$,account,[{where:{tk_co:"tk"},
fields:[{ten_tk_co:"ten_tk"}]}],function(){$callback$$()})},dv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dvcs).joinModel2($id_app$$,dvcs,[{where:{ma_dvcs:"_id"},fields:["ten_dvcs"]}],function(){$callback$$()})},pn6:function($callback$$){$items$$.joinModel2($id_app$$,pn6,[{where:{_id:"id_pc2"},fields:[{name:"id_pn6",value:"_id"}]}],function(){$callback$$()})},trangthai:function($callback$$){$items$$.joinModel2(null,trangthai,[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["ten_trang_thai",
"color"]}],function(){$callback$$()})},pc3:function($callback$$){$items$$.joinModel2($id_app$$,pc3,[{where:{id_pc3:"_id"},fields:[{id_pc3:"_id"},{so_ct_pc3:"so_ct"},{ngay_ct_pc3:"ngay_ct"}]}],function(){$callback$$()})},details_tk:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.tk_no).joinModel2($id_app$$,account,[{where:{tk_no:"tk"},fields:[{ten_tk_no:"ten_tk"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_tk_tdttco:function($callback$$){async.mapLimit($items$$,
20,function($r$$,$callback1$$){($r$$.tdttcos||[]).filter($item$$=>$item$$.tk_no).joinModel2($id_app$$,account,[{where:{tk_no:"tk"},fields:[{ten_tk_no:"ten_tk"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_dt_tdttco:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.tdttcos||[]).filter($item$$=>$item$$.ma_dt).joinModel2($id_app$$,dmdt,[{where:"ma_dt",fields:["ten_dt"]}],function(){$callback1$$()})},function(){$callback$$()})},details_hd_tdttco:function($callback$$){async.mapLimit($items$$,
20,function($r$$,$callback1$$){($r$$.tdttcos||[]).filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:["ten_hd"]}],function(){$callback1$$()})},function(){$callback$$()})},details_phi_tdttco:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.tdttcos||[]).filter($item$$=>$item$$.ma_phi).joinModel2($id_app$$,dmphi,[{where:"ma_phi",fields:["ten_phi"]}],function(){$callback1$$()})},function(){$callback$$()})},details_customer:function($callback$$){async.mapLimit($items$$,
20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_kh).joinModel2($id_app$$,customer,[{where:"ma_kh",fields:["ten_kh"]}],function(){$callback1$$()})},function(){$callback$$()})},details_dt:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_dt).joinModel2($id_app$$,dmdt,[{where:"ma_dt",fields:["ten_dt"]}],function(){$callback1$$()})},function(){$callback$$()})},details_hopdong:function($callback$$){async.mapLimit($items$$,
20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:["ten_hd"]}],function(){$callback1$$()})},function(){$callback$$()})},details_phi:function($callback$$){async.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_phi).joinModel2($id_app$$,dmphi,[{where:"ma_phi",fields:["ten_phi"]}],function(){$callback1$$()})},function(){$callback$$()})}},async()=>{try{await Promise.all($items$$.map($r$$=>
(async()=>{$r$$.t_tien=0;$r$$.t_tien_nt=0;$r$$.t_thue_vao=0;$r$$.t_thue_vao_nt=0;$r$$.details&&($r$$.t_tien=$r$$.details.csum("tien"),$r$$.t_tien_nt=$r$$.details.csum("tien_nt"));$r$$.tdttcos&&($r$$.t_tien+=$r$$.tdttcos.csum("tien"),$r$$.t_tien_nt+=$r$$.tdttcos.csum("tien_nt"));$r$$.vatvaos&&($r$$.t_thue_vao=$r$$.vatvaos.csum("t_thue"),$r$$.t_thue_vao_nt=$r$$.vatvaos.csum("t_thue_nt"));$r$$.t_tt=$r$$.t_tien+$r$$.t_thue_vao;$r$$.t_tt_nt=$r$$.t_tien_nt+$r$$.t_thue_vao_nt;var $id_pn6s_sc_pn6$$=(await pn6.find({id_app:$r$$.id_app,
id_pc2:$r$$._id.toString()}).lean()).map($d$$=>$d$$._id.toString());Logger.info("pn6",$id_pn6s_sc_pn6$$);var $sc_khac_tk_cos$$=($r$$.details||[]).map($d$$=>$d$$.tk_no);$id_pn6s_sc_pn6$$=await Socai.find({id_app:$r$$.id_app,tk_co:{$in:$sc_khac_tk_cos$$},id_ct:{$in:$id_pn6s_sc_pn6$$}}).lean();$r$$.t_hoan_ung_nt=$id_pn6s_sc_pn6$$.map($r$$=>$r$$.tien_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$sc_khac_tk_cos$$=await Socai.find({id_app:$r$$.id_app,tk_co:{$in:$sc_khac_tk_cos$$},id_link:$r$$._id.toString()}).lean();
$r$$.t_hoan_ung_nt=$sc_khac_tk_cos$$.filter($r$$=>($r$$.ma_ct_link||"").toLowerCase()!=="pn6").map($r$$=>$r$$.tien_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$r$$.t_con_lai_nt=$r$$.t_tt_nt-$r$$.t_hoan_ung_nt;return $r$$})())),$fn$$(null,$items$$)}catch($e$$){$fn$$($e$$.message||$e$$)}})}};