UNPKG

flexbiz-server

Version:

Flexible Server

23 lines (22 loc) 10.9 kB
'use strict';const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Sokho=global.getModel("sokho"),Sokhott=global.getModel("sokhott"),dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),dmkho=global.getModel("dmkho"),account=global.getModel("account"),pnc=global.getModel("pnc"),pdn=global.getModel("pdn"),model=global.getModel("pxc"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"),async=require("async"),ma_ct="PXC"; 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();$contr_router$$.post=async function($obj$$,$fn$$){$fn$$||($fn$$=function(){});$obj$$=$obj$$.toObject?$obj$$.toObject():{...$obj$$};$obj$$.details=$obj$$.details.map($d$$=>{$d$$.toObject&&($d$$=$d$$.toObject());return{...$d$$}});const $khonhap$$=await dmkho.findOne({id_app:$obj$$.id_app,ma_kho:$obj$$.ma_kho_n}).lean()|| {};let $product_details$$=$obj$$.details.map($detail$$=>$detail$$.combo&&0<$detail$$.combo.length?$detail$$.combo.map($item$$=>{$item$$=$item$$.toObject?$item$$.toObject():{...$item$$};delete $item$$.px_gia_dd;return $item$$}):[$detail$$]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);async.series({post_sokho:function($callback$jscomp$1$$){const $details_kho$$=[];$product_details$$.forEach(function($d$jscomp$1_detail$$){$d$jscomp$1_detail$$={...$d$jscomp$1_detail$$,nxt:2};$d$jscomp$1_detail$$.ma_kho= $d$jscomp$1_detail$$.ma_kho_x||$obj$$.ma_kho_x;$details_kho$$.push($d$jscomp$1_detail$$)});$khonhap$$.kho_dc&&$product_details$$.forEach(function($d$jscomp$2_detail$$){$d$jscomp$2_detail$$={...$d$jscomp$2_detail$$,nxt:1,pn_gia_tb:!0};$d$jscomp$2_detail$$.ma_kho=$d$jscomp$2_detail$$.ma_kho_n||$obj$$.ma_kho_n;$d$jscomp$2_detail$$.tk_vt=$d$jscomp$2_detail$$.tk_du;$d$jscomp$2_detail$$.sl_nhap=$d$jscomp$2_detail$$.sl_xuat;$d$jscomp$2_detail$$.tien_nhap=$d$jscomp$2_detail$$.tien_xuat;$d$jscomp$2_detail$$.tien_nhap_nt= $d$jscomp$2_detail$$.tien_xuat_nt;$d$jscomp$2_detail$$.sl_xuat=0;$d$jscomp$2_detail$$.sl_xuat_qd=0;$d$jscomp$2_detail$$.tien_xuat=0;$d$jscomp$2_detail$$.tien_xuat_nt=0;$details_kho$$.push($d$jscomp$2_detail$$)});async.series({khott:$callback$jscomp$0$$=>{(new PostBook($obj$$,[...$details_kho$$],Sokhott,async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(($e$$,$rs$$)=>{$callback$jscomp$0$$($e$$,$rs$$)})},khokt:$callback$jscomp$0$$=>{(new PostBook($obj$$,[...$details_kho$$],Sokho,async($_detail$$, $callback$$)=>{$callback$$({...$_detail$$})})).run(function($e$jscomp$0$$){!$e$jscomp$0$$&&$khonhap$$.kho_dc?model.findOneAndUpdate({_id:$obj$$._id},{$set:{id_pn:$obj$$._id.toString()}},{new:!1},($e$$,$rs$$)=>{$obj$$.id_pn=$obj$$._id.toString();$callback$jscomp$0$$($e$$,$rs$$)}):model.findOneAndUpdate({_id:$obj$$._id,id_pn:$obj$$._id.toString()},{$set:{id_pn:null}},{new:!1},($e$$,$rs$$)=>{$obj$$.id_pn=null;$callback$jscomp$0$$($e$$,$rs$$)})})}},($e$$,$rs$$)=>{$callback$jscomp$1$$($e$$,$rs$$)})},post_socai:function($callback$jscomp$0$$){let $details_sc$$= $obj$$.details.map($_detail$jscomp$2_detail$$=>{$_detail$jscomp$2_detail$$={...$_detail$jscomp$2_detail$$};$_detail$jscomp$2_detail$$.tk_no=$_detail$jscomp$2_detail$$.tk_du;$_detail$jscomp$2_detail$$.tk_co=$_detail$jscomp$2_detail$$.tk_vt;$_detail$jscomp$2_detail$$.ma_kho=$_detail$jscomp$2_detail$$.ma_kho_x||$obj$$.ma_kho_x;$_detail$jscomp$2_detail$$.tien_nt=$_detail$jscomp$2_detail$$.tien_xuat_nt;$_detail$jscomp$2_detail$$.tien=$_detail$jscomp$2_detail$$.tien_xuat;return $_detail$jscomp$2_detail$$}); $khonhap$$.kho_dc&&($details_sc$$=[...$details_sc$$,...$obj$$.details.map($_detail$jscomp$3_detail$$=>{$_detail$jscomp$3_detail$$={...$_detail$jscomp$3_detail$$};$_detail$jscomp$3_detail$$.tk_no=$_detail$jscomp$3_detail$$.tk_vt;$_detail$jscomp$3_detail$$.tk_co=$_detail$jscomp$3_detail$$.tk_du;$_detail$jscomp$3_detail$$.ma_kho=$_detail$jscomp$3_detail$$.ma_kho_n||$obj$$.ma_kho_n;$_detail$jscomp$3_detail$$.tien_nt=$_detail$jscomp$3_detail$$.tien_xuat_nt;$_detail$jscomp$3_detail$$.tien=$_detail$jscomp$3_detail$$.tien_xuat; return $_detail$jscomp$3_detail$$})]);(new PostSocai($obj$$,$details_sc$$,function($_detail$$,$callback$$){$callback$$($_detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};const $valid$$=function($i_user$$,$obj$$,$next$$){for($i_user$$=0;$i_user$$<$obj$$.details.length;$i_user$$++){const $detail$$=$obj$$.details[$i_user$$];$detail$$.line=$i_user$$;"VND"==$obj$$.ma_nt&&($detail$$.tien_xuat=$detail$$.tien_xuat_nt)}$next$$(null,$obj$$)}; $contr_router$$.creating=async function($user$$,$obj$$,$next$$){if($user$$.current_app_info.options.phieu_pxc_phai_co_phieu_de_nghi&&!$obj$$.id_dn)return $next$$("Ch\u01b0a c\u00f3 phi\u1ebfu \u0111\u1ec1 ngh\u1ecb \u0111i\u1ec1u chuy\u1ec3n");$obj$$.id_pnc="";$obj$$.details||($obj$$.details=[]);$valid$$($user$$,$obj$$,async function($error$$){if($error$$)return $next$$($error$$);await $obj$$.details.asyncJoinModel2($obj$$.id_app,"dmvt",{where:"ma_vt",fields:["combo"]});$obj$$.details.filter($d$$=> $d$$.combo&&0<$d$$.combo.length).forEach($detail$$=>{$detail$$.combo=$detail$$.combo.map($item$$=>{$item$$.sl_xuat_1_combo=$item$$.sl_xuat;$item$$.sl_xuat=$item$$.sl_xuat_1_combo*$detail$$.sl_xuat;$item$$.tien_xuat=$item$$.tien_xuat_nt=$item$$.sl_xuat*($item$$.gia_von_nt||0);$item$$.tien_hang=$item$$.tien_hang_nt=0;$item$$.tien=$item$$.tien_nt=0;$item$$.tien_ck=$item$$.tien_ck_nt=0;$item$$.tt=$item$$.tt_nt=0;return{...$item$$}})});$next$$(null,$obj$$)})};$contr_router$$.updating=function($user$$, $data$$,$obj$$,$next$$){$data$$.details=$data$$.details||$obj$$.details;$valid$$($user$$,$data$$,async function($error$$){if($error$$)return $next$$($error$$);await $data$$.details.asyncJoinModel2($obj$$.id_app,"dmvt",{where:"ma_vt",setFields:($item$$,$ref$$)=>{$item$$.combo&&0!=$item$$.combo.length?$ref$$.combo&&$ref$$.combo.forEach($c$$=>{let $cb$$=$item$$.combo.find($ci$$=>$ci$$.ma_vt===$c$$.ma_vt);$cb$$?$cb$$.sl_xuat_1_combo=$c$$.sl_xuat||0:$item$$.combo.push($c$$)}):$item$$.combo=$ref$$.combo}}); $data$$.details.filter($d$$=>$d$$.combo&&0<$d$$.combo.length).forEach($detail$$=>{$detail$$.combo=$detail$$.combo.map($item$$=>{void 0==$item$$.sl_xuat_1_combo&&($item$$.sl_xuat_1_combo=$item$$.sl_xuat||0);$item$$.sl_xuat=$item$$.sl_xuat_1_combo*$detail$$.sl_xuat;$item$$.tien_xuat=$item$$.tien_xuat_nt=$item$$.sl_xuat*($item$$.gia_von_nt||0);$item$$.tien_hang=$item$$.tien_hang_nt=0;$item$$.tien=$item$$.tien_nt=0;$item$$.tien_ck=$item$$.tien_ck_nt=0;$item$$.tt=$item$$.tt_nt=0;return{...$item$$}})}); $next$$(null,$data$$,$obj$$)})};$contr_router$$.on("saved",function($obj$$){$obj$$.id_dn&&pdn.updateMany({_id:$obj$$.id_dn},{id_pxc:$obj$$._id.toString(),trang_thai:"7"},($e$jscomp$1$$,$rs$$)=>{console.log("update pdn",$rs$$);pdn.findOne({_id:$obj$$.id_dn},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PDN,$e$$=>{$e$$&&console.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_dn&&pdn.updateMany({_id:$obj$$.id_dn},{id_pxc:""},()=>{pdn.updateMany({_id:$obj$$.id_dn, trang_thai:"7"},{trang_thai:"5"},($e$jscomp$1$$,$rs$$)=>{console.log("update pdn",$rs$$);pdn.findOne({_id:$obj$$.id_dn},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PDN,$e$$=>{$e$$&&console.error($e$$)})})})})});$contr_router$$.view=function($user$$,$items$$,$fn$$){const $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$$()})}, trangthai:function($callback$$){$items$$.joinModel2(null,trangthai,[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["ten_trang_thai","color"]}],function(){$callback$$()})},denghi:function($callback$$){$items$$.joinModel2($id_app$$,pdn,[{where:{id_dn:"_id"},fields:[{so_ct_dn:"so_ct"}],setFields:($a$jscomp$0$$,$b$jscomp$0$$)=>{$a$jscomp$0$$&&$b$jscomp$0$$&&($a$jscomp$0$$.details.forEach($a_detail$$=>{$a_detail$$.sl_dn=$b$jscomp$0$$.details.filter($d$$=>$d$$.ma_vt===$a_detail$$.ma_vt).map($d$$=> $d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0)}),$a$jscomp$0$$.t_sl_dn=$a$jscomp$0$$.details.map($d$$=>$d$$.sl_dn).reduce(($a$$,$b$$)=>$a$$+$b$$,0))}}],function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.details&&($r$$.t_sl=$r$$.details.csum("sl_xuat"),$r$$.t_tien_xuat=$r$$.details.csum("tien_xuat"),$r$$.t_tien_xuat_nt=$r$$.details.csum("tien_xuat_nt"))});$callback$$()},ma_kho_x:$callback$$=>{$items$$.joinModel2($id_app$$,dmkho,[{where:{ma_kho_x:"ma_kho"}, fields:[{name:"ten_kho_x",value:"ten_kho"},{name:"kho_dc_x",value:"kho_dc"}]}],function(){$callback$$()})},ma_kho_n:$callback$$=>{$items$$.joinModel2($id_app$$,dmkho,[{where:{ma_kho_n:"ma_kho"},fields:[{name:"ten_kho_n",value:"ten_kho"},{name:"kho_dc_n",value:"kho_dc"}]}],function(){$callback$$()})},details_tk:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,account,[{akey:"tk_vt",bkey:"tk",fields:[{name:"ten_tk_vt",value:"ten_tk"}]}, {akey:"tk_du",bkey:"tk",fields:[{name:"ten_tk_du",value:"ten_tk"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_vt:function($callback$$){async.mapSeries($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_kho_n:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$, dmkho,[{akey:"ma_kho_n",bkey:"ma_kho",fields:[{name:"ten_kho_n",value:"ten_kho"},{name:"kho_dc_n",value:"kho_dc"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_kho_x:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,dmkho,[{akey:"ma_kho_x",bkey:"ma_kho",fields:[{name:"ten_kho_x",value:"ten_kho"},{name:"kho_dc_x",value:"kho_dc"}]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){async.mapSeries($items$$, ($pxc$$,$callback$$)=>{$pxc$$.kho_dc_n?($pxc$$.t_sl_da_nhap=$pxc$$.t_sl,$callback$$()):pnc.find({id_px:$pxc$$._id}).lean().exec(($e$$,$pncs$$)=>{if($e$$)return $callback$$($e$$);$pxc$$.ds_pnc=$pncs$$.map($p$$=>({_id:$p$$._id,so_ct:$p$$.so_ct,ngay_ct:$p$$.ngay_ct}));let $pnc_details$$=$pncs$$.map($p$$=>$p$$.details).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);$pxc$$.details.forEach($pxc_detail$$=>{$pxc_detail$$.sl_da_nhap=$pnc_details$$.filter($d$$=>$d$$.ma_vt===$pxc_detail$$.ma_vt).map($d$$=>$d$$.sl_xuat).reduce(($a$$, $b$$)=>$a$$+$b$$,0)});$pxc$$.t_sl_da_nhap=$pnc_details$$.map($d$$=>$d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$callback$$()})},()=>{$fn$$(null,$items$$)})})}};