UNPKG

flexbiz-server

Version:

Flexible Server

21 lines (20 loc) 9.91 kB
const PostBook=require("../../libs/post-book"),PostSokho=require("../../libs/post-sokho"),PostSocai=require("../../libs/post-socai"),Sokhott=global.getModel("sokhott"),dmvt=global.getModel("dmvt"),dmkho=global.getModel("dmkho"),model=global.getModel("pnc"),pxc=global.getModel("pxc"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"),async=require("async"),ma_ct="PNC"; module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1},check_stock:!1,onCreated:($user$$,$obj$$,$next$$)=>{$obj$$.id_px?pxc.findOneAndUpdate({_id:$obj$$.id_px},{$set:{id_pn:$obj$$._id}},{new:!1},()=>{$next$$(null,$obj$$)}):$next$$(null,$obj$$)},onDeleted:($user$$,$obj$$,$next$$)=>{$obj$$.id_px?pxc.findOneAndUpdate({_id:$obj$$.id_px},{$set:{id_pn:null}},{new:!1},()=>{$next$$(null,$obj$$)}):$next$$(null, $obj$$)}});$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$$.toObject():$d$$);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=1;$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:1};$d$jscomp$3_detail$$.pn_gia_tb=!$d$jscomp$3_detail$$.px_gia_dd;$d$jscomp$3_detail$$.ma_kho=$d$jscomp$3_detail$$.ma_kho_n||$obj$$.ma_kho_n;$d$jscomp$3_detail$$.tk_vt= $d$jscomp$3_detail$$.tk_du;$d$jscomp$3_detail$$.sl_nhap=$d$jscomp$3_detail$$.sl_xuat;$d$jscomp$3_detail$$.tien_nhap=$d$jscomp$3_detail$$.tien_xuat;$d$jscomp$3_detail$$.tien_nhap_nt=$d$jscomp$3_detail$$.tien_xuat_nt;$d$jscomp$3_detail$$.sl_xuat=0;$d$jscomp$3_detail$$.sl_xuat_qd=0;$d$jscomp$3_detail$$.tien_xuat=0;$d$jscomp$3_detail$$.tien_xuat_nt=0;$details_kho$$.push($d$jscomp$3_detail$$)});async.parallel({khokt:$callback$$=>{(new PostSokho($obj$$,$details_kho$$,async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(function($e$$, $rs$$){$callback$$($e$$,$rs$$)})},khott:$callback$$=>{(new PostBook($obj$$,$details_kho$$,Sokhott,async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})}},($e$$,$rs$$)=>{$callback$$($e$$,$rs$$)})},post_socai:function($callback$$){(new PostSocai($obj$$,$obj$$.details,function($detail$$,$callback$$){$detail$$.tk_no=$detail$$.tk_vt;$detail$$.tk_co=$detail$$.tk_du;$detail$$.ma_kho=$detail$$.ma_kho_n||$obj$$.ma_kho_n;$detail$$.tien_nt=$detail$$.tien_xuat_nt; $detail$$.tien=$detail$$.tien_xuat;$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)}$obj$$.id_px?pxc.find({_id:$obj$$.id_px,id_app:$obj$$.id_app}).lean().exec(($e$$,$pxcs$$)=>{if($e$$)return $next$$($e$$); async.map($obj$$.details,($d$$,$callback$$)=>{let $detail$$=$d$$;$d$$.toObject&&($detail$$=$d$$.toObject());$detail$$.sl_xuat_pxc=$pxcs$$.map($p$$=>($p$$.details||[]).filter($d_pxc$$=>$d_pxc$$.ma_vt===$detail$$.ma_vt).map($d$$=>$d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$detail$$.sl_xuat_pxc&&$detail$$.sl_xuat_pxc<$detail$$.sl_xuat?$callback$$(`S\u1ed1 l\u01b0\u1ee3ng phi\u1ebfu nh\u1eadp \u0111i\u1ec1u chuy\u1ec3n v\u1eadt t\u01b0 ${$detail$$.ten_vt||$detail$$.ma_vt} l\u1edbn h\u01a1n s\u1ed1 l\u01b0\u1ee3ng phi\u1ebfu xu\u1ea5t \u0111i\u1ec1u chuy\u1ec3n`): $callback$$()},$e$$=>{if($e$$)return $next$$($e$$);$next$$(null,$obj$$)})}):$next$$(null,$obj$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$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_px&&pxc.updateMany({_id:$obj$$.id_px,id_app:$obj$$.id_app},{id_pnc:$obj$$._id.toString(), trang_thai:"7"},($e$$,$rs$$)=>{Logger.info("update pxc",$rs$$);pxc.findOne({_id:$obj$$.id_px,id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PXC,$e$$=>{$e$$&&Logger.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_px&&pxc.updateMany({_id:$obj$$.id_px,id_app:$obj$$.id_app},{id_pnc:""},()=>{pxc.updateMany({_id:$obj$$.id_px,id_app:$obj$$.id_app,trang_thai:"7"},{trang_thai:"5"},($e$$,$rs$$)=>{Logger.info("update pxc",$rs$$);pxc.findOne({_id:$obj$$.id_px, id_app:$obj$$.id_app},($e$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PXC,$e$$=>{$e$$&&Logger.error($e$$)})})})})});$contr_router$$.view=function($user$$,$items$$,$fn$$){const $id_app$$=$user$$.current_id_app;async.parallel({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$$()})},pxc:function($callback$$){$items$$.joinModel2($id_app$$, pxc,[{where:{id_px:"_id"},fields:[{so_ct_px:"so_ct"},{ngay_ct_px:"ngay_ct"}],setFields:($a$$,$b$$)=>{$a$$&&$b$$&&($a$$.details.forEach($a_detail$$=>{$a_detail$$.sl_xuat_pxc=($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_xuat=$b$$.details.map($d$$=>$d$$.sl_xuat).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$$||[]).filter($item$$=>$item$$.ma_kho_x).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$$||[]).filter($item$$=>$item$$.ma_kho_n).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.mapLimit($items$$,20,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.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_kho_n).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.mapLimit($items$$,20,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_kho_x).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(){$fn$$(null,$items$$)})}};