UNPKG

flexbiz-server

Version:

Flexible Server

20 lines (19 loc) 9.12 kB
const model=global.getModel("pgh"),PostBook=require("../../libs/post-book"),PostSokho=require("../../libs/post-sokho"),controller=require("../../controllers/controller"),async=require("async"),{onAfterCommit}=require("../../libs/sessionContext"),ma_ct="PGH"; module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1},onPopulate:function($mongooseDoc$$){return $mongooseDoc$$.populate({path:"details.don_hang"})}});$contr_router$$.route();$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details.forEach($d$$=>{$d$$.don_hang=$d$$.don_hang?._id});$next$$(null,$obj$$)};$contr_router$$.created=async function($user$$,$obj$$,$next$$){onAfterCommit(async()=> {for(let $hd$$ of $obj$$.details||[])$hd$$.don_hang?.trang_thai&&await global.getModel("hd2").updateOne({_id:$hd$$.don_hang._id},{trang_thai:$obj$$.trang_thai,user_nv_giao_hang:$obj$$.user_nv_giao_hang})});$next$$(null,$obj$$)};$contr_router$$.updating=async function($user$$,$data$$,$obj$$,$next$$){($data$$.details||[]).forEach($d$$=>{$d$$.don_hang=typeof $d$$.don_hang=="string"?$d$$.don_hang:$d$$.don_hang?._id});$next$$(null,$data$$,$obj$$)};$contr_router$$.updated=async function($user$$,$obj$$, $next$$){onAfterCommit(async()=>{for(let $hd$$ of $obj$$.details||[])$hd$$.don_hang?.trang_thai&&await global.getModel("hd2").updateOne({_id:$hd$$.don_hang._id},{trang_thai:$obj$$.trang_thai,user_nv_giao_hang:$obj$$.user_nv_giao_hang})});$next$$(null,$obj$$)};$contr_router$$.deleting=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.post=async function($obj$$,$fn$$){$fn$$||=function(){};$obj$$=await model.findById($obj$$._id).populate({path:"details.don_hang"});if(!$obj$$)return $fn$$("Ch\u1ee9ng t\u1eeb kh\u00f4ng t\u1ed3n t\u1ea1i"); $obj$$=$obj$$.toObject();$obj$$.details=$obj$$.details.map($d$$=>{$d$$.toObject&&($d$$=$d$$.toObject());return{...$d$$}});let $details$$=$obj$$.details.map($d$$=>$d$$.don_hang?.details||[]).concat(($a$$,$b$$)=>$a$$.concat($b$$),[]),$product_details$$=$details$$.map($detail$$=>{$detail$$.toObject&&($detail$$=$detail$$.toObject());return $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 $details$$.filter($d$$=>$d$$.ma_pk_xuat&&$d$$.sl_pk_xuat))$d$$={...$d$$},$d$$.ma_vt=$d$$.ma_pk_xuat,$d$$.ma_dvt=$d$$.ma_dvt_pk_xuat,$d$$.nxt=2,$d$$.tk_no=$obj$$.tk_co,$d$$.tk_co="",$d$$.sl_xuat=$d$$.sl_pk_xuat,$d$$.gia_von_nt=$d$$.gia_von_pk_xuat_nt||0,$d$$.gia_von=$d$$.gia_von_nt*($obj$$.ty_gia||1),$d$$.tien_xuat_nt=$d$$.tien_xuat_pk_nt||0,$d$$.tien_xuat=$d$$.tien_xuat_nt*($obj$$.ty_gia||1),$d$$.sl_nhap=0,$d$$.tien_nhap_nt=$d$$.tien_nhap= 0,$d$$.tien_hang_nt=$d$$.tien_hang=0,$d$$.tien_ck=$d$$.tien_ck_nt=$d$$.ty_le_ck=0,$d$$.tien_phi=$d$$.tien_phi_nt=0,$product_details$$.push($d$$);async.series({post_sokho:function($callback$$){const $details_kho$$=[];$product_details$$.forEach(function($d$jscomp$6_detail$$){$d$jscomp$6_detail$$={...$d$jscomp$6_detail$$,nxt:2};$d$jscomp$6_detail$$.ma_kho=$obj$$.ma_kho;$details_kho$$.push($d$jscomp$6_detail$$)});$product_details$$.forEach(function($d$jscomp$7_detail$$){$d$jscomp$7_detail$$={...$d$jscomp$7_detail$$, nxt:1,pn_gia_tb:!0};$d$jscomp$7_detail$$.ma_kho=$obj$$.ma_kho_nv;$d$jscomp$7_detail$$.sl_nhap=$d$jscomp$7_detail$$.sl_xuat;$d$jscomp$7_detail$$.tien_nhap=$d$jscomp$7_detail$$.tien_xuat;$d$jscomp$7_detail$$.tien_nhap_nt=$d$jscomp$7_detail$$.tien_xuat_nt;$d$jscomp$7_detail$$.sl_xuat=0;$d$jscomp$7_detail$$.sl_xuat_qd=0;$d$jscomp$7_detail$$.tien_xuat=0;$d$jscomp$7_detail$$.tien_xuat_nt=0;$details_kho$$.push($d$jscomp$7_detail$$)});async.series({khott:$callback$$=>{(new PostBook($obj$$,[...$details_kho$$], global.getModel("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$$){$callback$$($e$$)})}},($e$$,$rs$$)=>{$callback$$($e$$,$rs$$)})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};$contr_router$$.view=function($user$$,$pghs$$,$cb$$){const $id_app$$=$user$$.current_id_app;async.map($pghs$$,($pgh$$, $next$$)=>{const $items$$=($pgh$$.details||[]).map($p$$=>$p$$.don_hang||{});async.parallel({kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel2($id_app$$,"customer",[{where:"ma_kh",fields:["ten_kh","email","nh_kh"]}],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$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.details&& ($r$$.t_sl=$r$$.details.csum("sl_xuat"),$r$$.t_thue=$r$$.details.csum("tien_thue"),$r$$.t_thue_nt=$r$$.details.csum("tien_thue_nt"),$r$$.t_tien=$r$$.details.csum("tien"),$r$$.t_tien_nt=$r$$.details.csum("tien_nt"),$r$$.t_tien_ct=$r$$.details.csum("tien_ct"),$r$$.t_tien_ct_nt=$r$$.details.csum("tien_ct_nt"),$r$$.t_ck=$r$$.details.csum("tien_ck"),$r$$.t_ck_nt=$r$$.details.csum("tien_ck_nt"),$r$$.tc_ck_nt=$r$$.t_ck_nt+($r$$.tien_ck_hd||0),$r$$.tc_ck=$r$$.t_ck+($r$$.tien_ck_hd||0)*$r$$.ty_gia,$r$$.t_sl_vt_du= $r$$.details.csum("sl_vt_du"),$r$$.t_gt_vt_du=$r$$.details.csum("gt_vt_du"),$r$$.t_tt_nt=$r$$.t_tien_ct_nt-$r$$.tc_ck_nt+$r$$.t_thue_nt+($r$$.tien_phi_vc_nt||0)-$r$$.t_gt_vt_du,$r$$.t_tt=Math.round($r$$.t_tt_nt*$r$$.ty_gia||1,0),$r$$.t_tien_xuat=$r$$.details.csum("tien_xuat"),$r$$.t_tien_xuat_nt=$r$$.details.csum("tien_xuat_nt"),$r$$.tien_hoa_hong=$r$$.details.csum("tien_hoa_hong"),$r$$.tien_hoa_hong_gt=$r$$.details.csum("tien_hoa_hong_gt"),$r$$.tien_hoa_hong_ql=$r$$.details.csum("tien_hoa_hong_ql"), $r$$.tien_hoa_hong_kho=$r$$.details.csum("tien_hoa_hong_kho"),$r$$.thu_nhap_ctv=$r$$.details.csum("tien_hoa_hong_ctv"),$r$$.thu_nhap_ctv2=$r$$.details.csum("tien_hoa_hong_ctv2"))});$callback$$()},details_kho:function($callback$$){async.map($items$$,function($r$$,$callback$$){($r$$.details||[]).filter($item$$=>$item$$.ma_kho).joinModel2($id_app$$,global.getModel("dmkho"),[{where:"ma_kho",fields:["ten_kho"]}],function(){$callback$$()})},function(){$callback$$()})},details_vt:function($callback$$){async.map($items$$, function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,global.getModel("dmvt"),[{where:"ma_vt",setFields:($item$$,$ref$$)=>{$item$$.ten_vt=$ref$$.ten_vt;$item$$.tk_tl=$ref$$.tk_tl;$item$$.trong_luong=$item$$.trong_luong||$ref$$.trong_luong;$item$$.chieu_sau=$item$$.chieu_sau||$ref$$.chieu_sau;$item$$.chieu_rong=$item$$.chieu_rong||$ref$$.chieu_rong;$item$$.chieu_cao=$item$$.chieu_cao||$ref$$.chieu_cao;$item$$.ma_tt1_yn=$ref$$.ma_tt1_yn;$item$$.ma_tt2_yn=$ref$$.ma_tt2_yn;$item$$.ma_tt3_yn= $ref$$.ma_tt3_yn;$item$$.ma_lo_yn=$ref$$.ma_lo_yn;$item$$.ma_nvt||($item$$.ma_nvt=$ref$$.ma_nvt)}}],function(){$callback1$$()})},function(){$callback$$()})},details_vt_du:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.ma_vt_du).joinModel2($id_app$$,global.getModel("dmvt"),[{where:{ma_vt_du:"ma_vt"},fields:{ten_vt_du:"ten_vt"}}],function(){$callback1$$()})},function(){$callback$$()})},details_pk_nhap:function($callback$$){async.map($items$$, function($r$$,$callback1$$){($r$$.details||[]).filter($r$$=>$r$$.ma_pk_nhap).joinModel2($id_app$$,global.getModel("dmvt"),[{where:{ma_pk_nhap:"ma_vt"},fields:{ten_pk_nhap:"ten_vt"}}],function(){$callback1$$()})},function(){$callback$$()})},details_pk_xuat:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($r$$=>$r$$.ma_pk_xuat).joinModel2($id_app$$,global.getModel("dmvt"),[{where:{ma_pk_xuat:"ma_vt"},fields:{ten_pk_xuat:"ten_vt"}}],function(){$callback1$$()})}, function(){$callback$$()})},details_csck:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.id_cs_ck).joinModel2($id_app$$,"dmchietkhau",[{where:{id_cs_ck:"_id"},setFields:($item$$,$ref$$)=>{$item$$.ten_chietkhau=$ref$$.ten_chietkhau}}],function(){$callback1$$()})},function(){$callback$$()})},details_tt1:function($callback$$){async.map($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.ma_tt1).joinModel2($id_app$$,"dmtt", [{where:{ma_vt:"ma_vt",ma_tt1:"ma_tt"},setFields:($item$$,$ref$$)=>{$item$$.thuoc_tinh=$ref$$;$item$$.nh_tt||($item$$.nh_tt=$ref$$.nh_tt)}}],function(){$callback1$$()})},function(){$callback$$()})}},function(){$next$$(null,$items$$)})},async()=>{await $pghs$$.filter($item$$=>$item$$.trang_thai).asyncJoinModel2(null,"trangthai",[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["ten_trang_thai","color"]}]);await $pghs$$.filter($item$$=>$item$$.user_nv_giao_hang).joinModel2(null,"participant", [{where:{user_nv_giao_hang:"email"},fields:{ten_nv_giao_hang:"name"}}]);$cb$$(null,$pghs$$)})}};