flexbiz-server
Version:
Flexible Server
21 lines (20 loc) • 9.9 kB
JavaScript
'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"),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$$||($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&&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:1};$d$jscomp$1_detail$$.pn_gia_tb=!$d$jscomp$1_detail$$.px_gia_dd;$d$jscomp$1_detail$$.ma_kho=$d$jscomp$1_detail$$.ma_kho_n||$obj$$.ma_kho_n;$d$jscomp$1_detail$$.tk_vt=$d$jscomp$1_detail$$.tk_du;$d$jscomp$1_detail$$.sl_nhap=$d$jscomp$1_detail$$.sl_xuat;$d$jscomp$1_detail$$.tien_nhap=$d$jscomp$1_detail$$.tien_xuat;
$d$jscomp$1_detail$$.tien_nhap_nt=$d$jscomp$1_detail$$.tien_xuat_nt;$d$jscomp$1_detail$$.sl_xuat=0;$d$jscomp$1_detail$$.sl_xuat_qd=0;$d$jscomp$1_detail$$.tien_xuat=0;$d$jscomp$1_detail$$.tien_xuat_nt=0;$details_kho$$.push($d$jscomp$1_detail$$)});async.series({khokt:$callback$jscomp$0$$=>{(new PostBook($obj$$,$details_kho$$,Sokho,async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})},khott:$callback$jscomp$0$$=>{(new PostBook($obj$$,
$details_kho$$,Sokhott,async($_detail$$,$callback$$)=>{$callback$$({...$_detail$$})})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})}},($e$$,$rs$$)=>{$callback$jscomp$1$$($e$$,$rs$$)})},post_socai:function($callback$jscomp$0$$){(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$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)}$obj$$.id_px?pxc.find({_id:$obj$$.id_px}).lean().exec(($e$jscomp$0$$,$pxcs$$)=>{if($e$jscomp$0$$)return $next$$($e$jscomp$0$$);
async.mapSeries($obj$$.details,($d$jscomp$0$$,$callback$$)=>{let $detail$$=$d$jscomp$0$$;$d$jscomp$0$$.toObject&&($detail$$=$d$jscomp$0$$.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&&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_px&&pxc.updateMany({_id:$obj$$.id_px},{id_pnc:$obj$$._id.toString(),trang_thai:"7"},($e$jscomp$1$$,$rs$$)=>{console.log("update pxc",$rs$$);pxc.findOne({_id:$obj$$.id_px},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PXC,$e$$=>{$e$$&&console.error($e$$)})})})});$contr_router$$.on("deleted",function($obj$$){$obj$$.id_px&&pxc.updateMany({_id:$obj$$.id_px},{id_pnc:""},()=>{pxc.updateMany({_id:$obj$$.id_px,
trang_thai:"7"},{trang_thai:"5"},($e$jscomp$1$$,$rs$$)=>{console.log("update pxc",$rs$$);pxc.findOne({_id:$obj$$.id_px},($e$jscomp$0$$,$_obj$$)=>{$_obj$$&&controller.postData($_obj$$,global.controllers.PXC,$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$$.filter($item$$=>$item$$.ma_dvcs).joinModel($id_app$$,dvcs,[{akey:"ma_dvcs",bkey:"_id",fields:[{name:"ten_dvcs",
value:"ten_dvcs"}]}],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$$()})},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$jscomp$0$$,$b$jscomp$0$$)=>{$a$jscomp$0$$&&$b$jscomp$0$$&&($a$jscomp$0$$.details.forEach($a_detail$$=>
{$a_detail$$.sl_xuat_pxc=$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_xuat=$b$jscomp$0$$.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_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||[]).filter($item$$=>$item$$.ma_kho_n).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||[]).filter($item$$=>$item$$.ma_kho_x).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(){$fn$$(null,$items$$)})}};