UNPKG

flexbiz-server

Version:

Flexible Server

18 lines (17 loc) 8.09 kB
const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Tdttco=global.getModel("tdttco"),Tdttco_tt=global.getModel("tdttco_tt"),Vatvao=global.getModel("vatvao"),account=global.getModel("account"),customer=global.getModel("customer"),dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),model=global.getModel("pn2"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"),async=require("async"), ma_ct="PN2",moment=require("moment"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,kiem_tra_han_muc_cong_no:!0,sort:{ngay_ct:-1,so_ct:-1}});$contr_router$$.route();$contr_router$$.post=async function($obj$$,$fn$$,$_options$$={}){$fn$$||=function(){};$obj$$.toObject&&($obj$$=$obj$$.toObject());let $options$$;try{$options$$=await global.getModel("options").getInfo($obj$$.id_app,{id_func:($obj$$.listinfo_code||$obj$$.ma_ct).toLowerCase()},"option")}catch($e$$){Logger.info($e$$.message, {id_func:$obj$$.ma_ct})}$options$$||={};let $data_vatvaos$$=$obj$$.vatvaos;$data_vatvaos$$&&$data_vatvaos$$.length!=0||($data_vatvaos$$=($obj$$.details||[]).filter($d$$=>$d$$.ma_thue),$data_vatvaos$$=await $data_vatvaos$$.asyncGroupBy("ma_thue thue_suat ma_hoa_don ky_hieu_hoa_don ma_tc so_hd so_seri ngay_hd".split(" "),["tien_nt","tien","tien_thue","tien_thue_nt"]),$data_vatvaos$$.forEach($v$$=>{$v$$.t_tien_nt=$v$$.tien_nt;$v$$.t_tien=$v$$.t_tien_nt*$obj$$.ty_gia;$v$$.t_thue_nt=$v$$.tien_thue_nt; $v$$.t_thue=$v$$.t_thue_nt*$obj$$.ty_gia;$v$$.t_hd_nt=$v$$.t_tien_nt+$v$$.t_thue_nt;$v$$.t_hd=$v$$.t_tien+$v$$.t_thue;$v$$.tk_du_thue=$obj$$.tk_co}),await $data_vatvaos$$.asyncJoinModel2($obj$$.id_app,"vat",{where:"ma_thue",setFields:($item$$,$ref$$)=>{$item$$.tk_thue_no||($item$$.tk_thue_no=$ref$$.tk_thue_no)}}));async.series({post_vatvao:function($callback$$){(new PostBook($obj$$,$data_vatvaos$$,Vatvao,($detail$$,$callback$$)=>{$detail$$={...$detail$$};$detail$$.ten_vt=$detail$$.ten_vt||$obj$$.dien_giai|| ($obj$$.details||[]).filter($d$$=>$d$$.dien_giai).map($d$$=>$d$$.dien_giai).join(", ");$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})},post_socai:function($callback$$){const $details_sc$$=[];$data_vatvaos$$.forEach(function($v$$){$v$$.toObject&&($v$$=$v$$.toObject());$v$$={...$v$$};$v$$.tk_no=$v$$.tk_thue_no;$v$$.tk_co=$v$$.tk_du_thue;$v$$.tien_nt=$v$$.t_thue_nt;$v$$.tien=$v$$.t_thue;$v$$.ma_kh=$v$$.ma_kh_cn||$v$$.ma_kh;$details_sc$$.push($v$$)});$obj$$.details.forEach(function($d$jscomp$3_detail$$){$d$jscomp$3_detail$$.toObject&& ($d$jscomp$3_detail$$=$d$jscomp$3_detail$$.toObject());$d$jscomp$3_detail$$={...$d$jscomp$3_detail$$};$d$jscomp$3_detail$$.ma_kh||($d$jscomp$3_detail$$.ma_kh=$obj$$.ma_kh);$d$jscomp$3_detail$$.tk_co=$obj$$.tk_co;$details_sc$$.push($d$jscomp$3_detail$$)});(new PostSocai($obj$$,$details_sc$$,void 0,void 0,$_options$$)).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})},post_tdttco:function($callback$$){account.findOne({tk:$obj$$.tk_co,id_app:$obj$$.id_app},{tk_cn:1},function($error$$,$tk$$){if($error$$)return $callback$$($error$$); $tk$$?.tk_cn===!0||$options$$.post_tdtt_tm?(new PostBook($obj$$,[{...$obj$$}],Tdttco,function($obj$$,$callback$$){$obj$$.so_hd=$obj$$.so_ct;$obj$$.ngay_hd=$obj$$.ngay_ct;$obj$$.tien_nt=$obj$$.details.csum("tien_nt")-$obj$$.details.csum("tien_ck_nt")+($obj$$.vatvaos||[]).csum("t_thue_nt");$obj$$.tien=$obj$$.details.csum("tien")-$obj$$.details.csum("tien_ck")+($obj$$.vatvaos||[]).csum("t_thue");$callback$$($obj$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)}):Tdttco.deleteMany({id_ct:$obj$$._id}, function(){$callback$$($error$$)})})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};const $valid$$=function($i_i$$,$obj$$,$next$$){for($i_i$$=0;$i_i$$<$obj$$.details.length;$i_i$$++){var $detail$jscomp$2_vatvao$$=$obj$$.details[$i_i$$];$detail$jscomp$2_vatvao$$.line=$i_i$$;$obj$$.ma_nt=="VND"&&($detail$jscomp$2_vatvao$$.tien=Math.roundBy($detail$jscomp$2_vatvao$$.tien_nt,0),$detail$jscomp$2_vatvao$$.tien_thue=Math.roundBy($detail$jscomp$2_vatvao$$.tien_thue_nt,0))}if($obj$$.vatvaos)for($i_i$$=0;$i_i$$< $obj$$.vatvaos.length;$i_i$$++)$detail$jscomp$2_vatvao$$=$obj$$.vatvaos[$i_i$$],$detail$jscomp$2_vatvao$$.line=$i_i$$,$obj$$.ma_nt=="VND"&&($detail$jscomp$2_vatvao$$.t_thue=Math.roundBy($detail$jscomp$2_vatvao$$.t_thue_nt,0));$next$$(null,$obj$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);$valid$$($user$$,$obj$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$obj$$)})};$contr_router$$.updating=function($user$$,$data$$,$obj$$, $next$$){Tdttco_tt.findOne({id_hd:$obj$$._id},function($error$$,$ct_tt$$){if($error$$)return $next$$($error$$);$ct_tt$$?$next$$(`Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt phi\u1ebfu n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n b\u1edfi ${$ct_tt$$.ma_ct||"ch\u1ee9ng t\u1eeb"}: ${$ct_tt$$.so_ct}, ng\u00e0y ${moment($ct_tt$$.ngay_ct).format("DD/MM/YYYY")}`):$valid$$($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})})};$contr_router$$.deleting= function($user$$,$obj$$,$next$$){Tdttco_tt.findOne({id_hd:$obj$$._id},function($error$$,$ct_tt$$){if($error$$)return $next$$($error$$);$ct_tt$$?$next$$(`Kh\u00f4ng th\u1ec3 xo\u00e1 phi\u1ebfu n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n b\u1edfi ${$ct_tt$$.ma_ct||"ch\u1ee9ng t\u1eeb"}: ${$ct_tt$$.so_ct}, ng\u00e0y ${moment($ct_tt$$.ngay_ct).format("DD/MM/YYYY")}`):$next$$(null,$obj$$)})};$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$$()})},dt:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dt).joinModel2($id_app$$,dmdt,{where:"ma_dt",fields:"ten_dt"},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$$.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$$.vatvaos&&$r$$.vatvaos.length>0?($r$$.t_thue_vao=$r$$.vatvaos.csum("t_thue"),$r$$.t_thue_vao_nt=$r$$.vatvaos.csum("t_thue_nt")):($r$$.t_thue_vao=($r$$.details||[]).csum("tien_thue"),$r$$.t_thue_vao_nt=($r$$.details||[]).csum("tien_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});$callback$$()},details_ncc:function($callback$$){async.each($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.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$$)})}};