UNPKG

flexbiz-server

Version:

Flexible Server

16 lines (15 loc) 7.06 kB
'use strict';const dmkho=global.getModel("dmkho"),dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),customer=global.getModel("customer"),dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),salecontract=global.getModel("sale_contract"),so1=global.getModel("so1"),model=global.getModel("pxh"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"),async=require("async"),ma_ct="PXH",Sokhott=global.getModel("sokhott"), Sokho=global.getModel("sokho"),Options=global.getModel("options"),PostBook=require("../../libs/post-book"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1}});$contr_router$$.route();const $valid$$=async($_so1_i_user$$,$obj$$,$next$$)=>{for($_so1_i_user$$=0;$_so1_i_user$$<$obj$$.details.length;$_so1_i_user$$++){const $detail$$=$obj$$.details[$_so1_i_user$$];$detail$$.line=$_so1_i_user$$;$detail$$.tien_hang_nt=$detail$$.tien_hang_nt?$detail$$.tien_hang_nt:$detail$$.sl_xuat*$detail$$.gia_von_nt;$detail$$.tien_hang= $detail$$.tien_hang?$detail$$.tien_hang:$detail$$.sl_xuat*$detail$$.gia_von;$detail$$.tien_xuat_nt=($detail$$.tien_hang_nt||0)+($detail$$.tien_phi_nt||0)-($detail$$.tien_ck_nt||0);$detail$$.tien_xuat=($detail$$.tien_hang||0)+($detail$$.tien_phi||0)-($detail$$.tien_ck||0);"VND"===$obj$$.ma_nt&&($detail$$.tien_hang=$detail$$.tien_hang_nt,$detail$$.tien_ck=$detail$$.tien_ck_nt,$detail$$.tien_phi=$detail$$.tien_phi_nt,$detail$$.tien_xuat=$detail$$.tien_xuat_nt)}$obj$$.id_so1&&!$obj$$.id_contract&&($_so1_i_user$$= await so1.findOne({_id:$obj$$.id_so1}))&&($obj$$.id_contract=$_so1_i_user$$.id_contract);$obj$$.id_contract?salecontract.findOne({_id:$obj$$.id_contract}).lean().exec(($e$$,$c$$)=>{$c$$&&($obj$$.ma_hd=$c$$.ma_hd);$next$$(null,$obj$$)}):$next$$(null,$obj$$)};$contr_router$$.post=async function($obj$$,$fn$$){$fn$$||($fn$$=function(){});let $options$$;try{$options$$=await Options.getInfo($obj$$.id_app,{id_func:$obj$$.ma_ct.toLowerCase()},"option")}catch($e$$){console.log($e$$.message,{id_func:$obj$$.ma_ct})}$options$$|| ($options$$={});async.series({post_sokho:function($callback$jscomp$0$$){$options$$.post_sokho?(new PostBook($obj$$,$obj$$.details,Sokho,async($detail$$,$callback$$)=>{$detail$$={...$detail$$,nxt:2};$detail$$.tk_no=$obj$$.tk_no;$detail$$.ma_kho=$detail$$.ma_kho||$obj$$.ma_kho;$callback$$($detail$$)})).run(function(){$callback$jscomp$0$$()}):$callback$jscomp$0$$()},post_sokhott:function($callback$jscomp$0$$){$options$$.not_post_sokhott?$callback$jscomp$0$$():(new PostBook($obj$$,$obj$$.details,Sokhott, async($detail$$,$callback$$)=>{$detail$$={...$detail$$,nxt:2};$detail$$.tk_no=$obj$$.tk_no;$detail$$.ma_kho=$detail$$.ma_kho||$obj$$.ma_kho;$callback$$($detail$$)})).run(function(){$callback$jscomp$0$$()})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};$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$$){$valid$$($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})};$contr_router$$.on("saved",function($obj$$){$obj$$.id_dh2&&global.getModel("dh2").updateMany({_id:$obj$$.id_dh2,trang_thai:"3"},{id_pxh:$obj$$._id.toString(),trang_thai:"6"},()=>{global.getModel("dh2").findOne({_id:$obj$$.id_dh2},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.DH2,$e$$=>{$e$$&&console.error($e$$)})})})});$contr_router$$.on("deleted", function($obj$$){$obj$$.id_dh2&&global.getModel("dh2").updateMany({_id:$obj$$.id_dh2,trang_thai:"6"},{id_pxh:"",trang_thai:"3"},()=>{global.getModel("dh2").findOne({_id:$obj$$.id_dh2},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.DH2,$e$$=>{$e$$&&console.error($e$$)})})})});$contr_router$$.view=function($user$$,$items$$,$fn$$){let $id_app$$=$user$$.current_id_app;async.series({dv:function($callback$$){$items$$.joinModel($id_app$$,dvcs,[{akey:"ma_dvcs",bkey:"_id", fields:[{name:"ten_dvcs",value:"ten_dvcs"}]}],function(){$callback$$()})},dt:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dt).joinModel($id_app$$,dmdt,[{akey:"ma_dt",bkey:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback$$()})},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$$()})},kho:function($callback$$){$items$$.filter($item$$=> $item$$.ma_kho).joinModel($id_app$$,dmkho,[{akey:"ma_kho",bkey:"ma_kho",fields:[{name:"ten_kho",value:"ten_kho"}]}],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$$()})},contract:function($callback$$){$items$$.filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:["ten_hd","ten_hd"]}],function(){$callback$$()})}, t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.t_thue_ra=0;$r$$.t_thue_ra_nt=0;$r$$.t_tien_xuat=0;$r$$.t_tien_xuat_nt=0;$r$$.details&&($r$$.t_sl=$r$$.details.csum("sl_xuat"),$r$$.t_tien_hang=$r$$.details.csum("tien_hang"),$r$$.t_tien_hang_nt=$r$$.details.csum("tien_hang_nt"),$r$$.t_ck=$r$$.details.csum("tien_ck"),$r$$.t_ck_nt=$r$$.details.csum("tien_ck_nt"),$r$$.t_cp=$r$$.details.csum("tien_phi"),$r$$.t_cp_nt=$r$$.details.csum("tien_phi_nt"),$r$$.t_tien_xuat=$r$$.details.csum("tien_xuat"), $r$$.t_tien_xuat_nt=$r$$.details.csum("tien_xuat_nt"));$r$$.t_tt=$r$$.t_tien_xuat+$r$$.t_thue_ra;$r$$.t_tt_nt=$r$$.t_tien_xuat_nt+$r$$.t_thue_ra_nt});$callback$$()},details_vt:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,dmvt,[{akey:"ma_vt",bkey:"ma_vt",fields:[{name:"ten_vt",value:"ten_vt"}]}],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$$)})}};