UNPKG

flexbiz-server

Version:

Flexible Server

23 lines (22 loc) 11.2 kB
const PostBook=require("../../libs/post-book"),PostSokho=require("../../libs/post-sokho"),PostSocai=require("../../libs/post-socai"),Sokhott=global.getModel("sokhott"),dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),dmkho=global.getModel("dmkho"),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$$||=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&&$detail$$.combo.length>0?$detail$$.combo.map($item$$=>{$item$$=$item$$.toObject?$item$$.toObject():{...$item$$};delete $item$$.px_gia_dd;return $item$$}):[$detail$$]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);for(let $d$$ of($obj$$.ds_pk_xuat||[]).filter($d$$=>$d$$.ma_pk_xuat&&$d$$.sl_pk_xuat)){const $pk$$={...$d$$};$pk$$.ma_vt=$d$$.ma_pk_xuat;$pk$$.ma_dvt=$d$$.ma_dvt_pk_xuat;$pk$$.ma_kho_x=$d$$.ma_kho_pk_xuat;$pk$$.ma_kho_n=$d$$.ma_kho_pk_nhap;$pk$$.nxt= 2;$pk$$.tk_no=$obj$$.tk_co;$pk$$.tk_co="";$pk$$.sl_xuat=$d$$.sl_pk_xuat;$pk$$.gia_von_nt=$d$$.gia_von_pk_xuat_nt||0;$pk$$.gia_von=$pk$$.gia_von_nt*($obj$$.ty_gia||1);$pk$$.tien_xuat_nt=$d$$.tien_xuat_pk_nt||0;$pk$$.tien_xuat=$pk$$.tien_xuat_nt*($obj$$.ty_gia||1);$pk$$.sl_nhap=0;$pk$$.tien_nhap_nt=$pk$$.tien_nhap=0;$pk$$.tien_hang_nt=$pk$$.tien_hang=0;$pk$$.tien_ck=$pk$$.tien_ck_nt=$pk$$.ty_le_ck=0;$pk$$.tien_phi=$pk$$.tien_phi_nt=0;$product_details$$.push($pk$$)}async.series({post_sokho:function($callback$$){const $details_kho$$= [];$product_details$$.forEach(function($d$jscomp$3_detail$$){$d$jscomp$3_detail$$={...$d$jscomp$3_detail$$,nxt:2};$d$jscomp$3_detail$$.ma_kho=$d$jscomp$3_detail$$.ma_kho_x||$obj$$.ma_kho_x;$details_kho$$.push($d$jscomp$3_detail$$)});$khonhap$$.kho_dc&&$product_details$$.forEach(function($d$jscomp$4_detail$$){$d$jscomp$4_detail$$={...$d$jscomp$4_detail$$,nxt:1,pn_gia_tb:!0};$d$jscomp$4_detail$$.ma_kho=$d$jscomp$4_detail$$.ma_kho_n||$obj$$.ma_kho_n;$d$jscomp$4_detail$$.tk_vt=$d$jscomp$4_detail$$.tk_du; $d$jscomp$4_detail$$.sl_nhap=$d$jscomp$4_detail$$.sl_xuat;$d$jscomp$4_detail$$.tien_nhap=$d$jscomp$4_detail$$.tien_xuat;$d$jscomp$4_detail$$.tien_nhap_nt=$d$jscomp$4_detail$$.tien_xuat_nt;$d$jscomp$4_detail$$.sl_xuat=0;$d$jscomp$4_detail$$.sl_xuat_qd=0;$d$jscomp$4_detail$$.tien_xuat=0;$d$jscomp$4_detail$$.tien_xuat_nt=0;$details_kho$$.push($d$jscomp$4_detail$$)});async.series({khott:$callback$$=>{(new PostBook($obj$$,[...$details_kho$$],Sokhott,async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(($e$$, $rs$$)=>{$callback$$($e$$,$rs$$)})},khokt:$callback$$=>{(new PostSokho($obj$$,[...$details_kho$$],async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(function($e$$){!$e$$&&$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$$($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$$($e$$, $rs$$)})})}},($e$$,$rs$$)=>{$callback$$($e$$,$rs$$)})},post_socai:function($callback$$){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$$($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$$;$obj$$.ma_nt=="VND"&&($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&&$d$$.combo.length>0).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&&$item$$.combo.length!=0?$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&&$d$$.combo.length>0).forEach($detail$$=>{$detail$$.combo=$detail$$.combo.map($item$$=>{$item$$.sl_xuat_1_combo==void 0&&($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_app:$obj$$.id_app},{id_pxc:$obj$$._id.toString(),trang_thai:"7"},($e$$,$rs$$)=>{Logger.info("update pdn",$rs$$);pdn.findOne({_id:$obj$$.id_dn,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PDN,$e$$=>{$e$$&&Logger.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_dn&& pdn.updateMany({_id:$obj$$.id_dn,id_app:$obj$$.id_app},{id_pxc:""},()=>{pdn.updateMany({_id:$obj$$.id_dn,id_app:$obj$$.id_app,trang_thai:"7"},{trang_thai:"5"},($e$$,$rs$$)=>{Logger.info("update pdn",$rs$$);pdn.findOne({_id:$obj$$.id_dn,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PDN,$e$$=>{$e$$&&Logger.error($e$$)})})})})});$contr_router$$.view=function($user$$,$items$$,$fn$$){const $id_app$$=$user$$.current_id_app;async.parallel({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$$,$b$$)=>{$a$$&&$b$$&&($a$$.details.forEach($a_detail$$=>{$a_detail$$.sl_dn=($b$$.details|| []).filter($d$$=>$d$$.ma_vt===$a_detail$$.ma_vt).map($d$$=>$d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0)}),$a$$.t_sl_dn=$a$$.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_vt:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,dmvt,[{where:"ma_vt",fields:["ten_vt","ma_tt1_yn", "ma_tt2_yn","ma_tt3_yn","ma_lo_yn"]}],function(){$callback1$$()})},function(){$callback$$()})},details_kho_n:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).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(){$callback1$$()})},function(){$callback$$()})},details_kho_x:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).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(){$callback1$$()})},function(){$callback$$()})}},function(){async.map($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$$)})})}};