UNPKG

flexbiz-server

Version:

Flexible Server

36 lines (35 loc) 17.4 kB
'use strict';const async=require("async"),moment=require("moment"),_=require("lodash"),dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),customer=global.getModel("customer"),dmnv=global.getModel("dmnv"),dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),salecontract=global.getModel("sale_contract"),model=global.getModel("dh2"),hd2=global.getModel("hd2"),pxh=global.getModel("pxh"),controller=require("../../controllers/controller"),utils=require("../../libs/utils"), tinhhoahong=require("../../libs/tinhhoahong"),trangthai=global.getModel("trangthai"),ma_ct="DH2"; 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});$contr_router$$.route();$contr_router$$.validating=async($user$$,$obj$$,$next$$)=>{$obj$$.sdt_nguoi_nhan&&($obj$$.sdt_nguoi_nhan=$obj$$.sdt_nguoi_nhan.split(" ").join(""));if($obj$$.sdt_nguoi_nhan&&!utils.isMobilePhone($obj$$.sdt_nguoi_nhan,"vi-VN"))return $next$$(`S\u1ed1 \u0111i\u1ec7n tho\u1ea1i ng\u01b0\u1eddi nh\u1eadn ${$obj$$.sdt_nguoi_nhan} l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7`); $next$$(null,$obj$$)};$contr_router$$.saving=async($t_tt_nt_user$$,$obj$$)=>{$obj$$.user_created||($obj$$.user_created=$t_tt_nt_user$$.email);$obj$$.ma_kho_npp="";$obj$$.of_user="";var $cust_kho_npp$$=$obj$$.ma_kh?await customer.findOne({id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh}).lean():void 0;$cust_kho_npp$$&&$cust_kho_npp$$.of_user&&($cust_kho_npp$$=await global.getModel("dmkho").findOne({id_app:$obj$$.id_app,user_npp:$cust_kho_npp$$.of_user,la_kho_npp:!0}).lean())&&($obj$$.ma_kho_npp=$cust_kho_npp$$.ma_kho, $obj$$.of_user=$cust_kho_npp$$.user_npp);let $kho_thue$$;!$obj$$.ma_kho_npp&&$obj$$.ma_kho&&($kho_thue$$=await global.getModel("dmkho").findOne({id_app:$obj$$.id_app,ma_kho:$obj$$.ma_kho,la_kho_thue:!0}).lean());$obj$$.ma_kho_npp||$kho_thue$$||$obj$$.ma_kho||!($kho_thue$$=await global.getModel("dmkho").findOne({id_app:$obj$$.id_app,ds_ctv_kho_thue:$t_tt_nt_user$$.email,la_kho_thue:!0}).lean())||($obj$$.ma_kho=$kho_thue$$.ma_kho);$kho_thue$$?$obj$$.details.forEach($d$$=>{$d$$.ma_kho=""}):await $obj$$.details.asyncJoinModel2($obj$$.id_app, "dmvt",[{where:"ma_vt",fields:[{ma_kho:"ma_kho_df"}]}]);$obj$$.details.forEach($d$$=>{$d$$.sl_xuat=utils.round($d$$.sl_xuat,4);$d$$.tt_nt=$d$$.tien_ct_nt-$d$$.tien_ck_nt;$d$$.tt=$d$$.tien_ct-$d$$.tien_ck;$d$$.tien_xuat_tmp=$d$$.sl_xuat*($d$$.gia_mua||0)});await tinhhoahong($obj$$,$t_tt_nt_user$$);$obj$$.details=$obj$$.details||[];$t_tt_nt_user$$=$obj$$.details.csum("tt_nt");await global.getModel("dmchietkhauhd").tinhChietKhau($obj$$.user_created,$obj$$.id_app,$obj$$,$t_tt_nt_user$$)};const $tinhGiaChuaThue$$= ($obj$$,$detail$$)=>{let $f_tien_nt$$="VND"==$obj$$.ma_nt?0:3;$obj$$=($detail$$.ma_thue?$detail$$.thue_suat:$obj$$.thue_suat)||0;$detail$$.gia_ban_ct_nt||($detail$$.tien_thue_nt?($detail$$.gia_ban_ct_nt=$detail$$.gia_ban_nt,$detail$$.tien_ct_nt=$detail$$.tien_nt,$detail$$.tien_ck_nt=$detail$$.tien_ck_nt||0,$detail$$.tien_thue_nt=$detail$$.tien_thue_nt||0,$detail$$.gia_ban_nt=$detail$$.gia_ban_ct_nt*(1+$detail$$.thue_suat/100)):($detail$$.gia_ban_ct_nt=utils.round($detail$$.gia_ban_nt/(1+$obj$$/100), 5),$detail$$.tien_ct_nt=utils.round($detail$$.gia_ban_ct_nt*$detail$$.sl_xuat,$f_tien_nt$$)));$detail$$.tien_ck_nt=$detail$$.tien_ck_nt||0;$detail$$.ty_le_ck&&($detail$$.tien_ck_nt=utils.round($detail$$.tien_ct_nt*$detail$$.ty_le_ck/100,$f_tien_nt$$));$detail$$.tien_thue_nt=utils.round(($detail$$.tien_ct_nt-($detail$$.tien_ck_nt||0))*$obj$$/100,$f_tien_nt$$);$detail$$.tien_nt=$detail$$.tien_ct_nt-$detail$$.tien_ck_nt+$detail$$.tien_thue_nt},$valid$$=function($f_tien_nt$jscomp$1_t_thue_nt_user$$,$obj$$, $next$$){$obj$$.thue_suat=$obj$$.thue_suat||0;$f_tien_nt$jscomp$1_t_thue_nt_user$$="VND"==$obj$$.ma_nt?0:3;$obj$$.tien_phi_vc_nt=$obj$$.tien_phi_vc_nt||0;$obj$$.tien_phi_vc=utils.round($obj$$.tien_phi_vc_nt*$obj$$.ty_gia,0);$obj$$.details.forEach($detail$$=>{$tinhGiaChuaThue$$($obj$$,$detail$$)});for(var $i_t_thue$$=0;$i_t_thue$$<$obj$$.details.length;$i_t_thue$$++){var $detail$jscomp$0$$=$obj$$.details[$i_t_thue$$];$detail$jscomp$0$$.line=$i_t_thue$$;$detail$jscomp$0$$.tien_ck_nt=$detail$jscomp$0$$.tien_ck_nt|| 0;$detail$jscomp$0$$.tien_thue_nt=$detail$jscomp$0$$.tien_thue_nt||0;$detail$jscomp$0$$.tien=utils.round($detail$jscomp$0$$.tien_nt*$obj$$.ty_gia,0);$detail$jscomp$0$$.tien_ct=utils.round($detail$jscomp$0$$.tien_ct_nt*$obj$$.ty_gia,0);$detail$jscomp$0$$.gia_ban=utils.round($detail$jscomp$0$$.gia_ban_nt*$obj$$.ty_gia,0);$detail$jscomp$0$$.gia_ban_ct=utils.round($detail$jscomp$0$$.gia_ban_ct_nt*$obj$$.ty_gia,0);$detail$jscomp$0$$.tien_ck=utils.round($detail$jscomp$0$$.tien_ck_nt*$obj$$.ty_gia,0);$detail$jscomp$0$$.tien_thue= utils.round($detail$jscomp$0$$.tien_thue_nt*$obj$$.ty_gia,0);$detail$jscomp$0$$.tien_xuat=$detail$jscomp$0$$.tien_xuat_nt}!$obj$$.details.find($r$$=>$r$$.ma_thue)&&$obj$$.thue_suat&&($obj$$.t_thue_nt=utils.round(($obj$$.details.csum("tien_ct_nt")-$obj$$.details.csum("tien_ck_nt"))*$obj$$.thue_suat/100,$f_tien_nt$jscomp$1_t_thue_nt_user$$),$obj$$.t_thue=utils.round($obj$$.t_thue_nt*($obj$$.ty_gia||1),0),$f_tien_nt$jscomp$1_t_thue_nt_user$$=$obj$$.details.csum("tien_thue_nt"),$i_t_thue$$=$obj$$.details.csum("tien_thue"), ($f_tien_nt$jscomp$1_t_thue_nt_user$$!==$obj$$.t_thue_nt||$i_t_thue$$!==$obj$$.t_thue)&&0<$obj$$.details.length&&($detail$jscomp$0$$=$obj$$.details[$obj$$.details.length-1],$detail$jscomp$0$$.tien_thue_nt+=$obj$$.t_thue_nt-$f_tien_nt$jscomp$1_t_thue_nt_user$$,$detail$jscomp$0$$.tien_thue+=$obj$$.t_thue-$i_t_thue$$));$obj$$.id_contract?salecontract.findOne({_id:$obj$$.id_contract}).lean().exec(($e$$,$c$$)=>{$c$$&&($obj$$.ma_hd=$c$$.ma_hd);$next$$(null,$obj$$)}):$next$$(null,$obj$$)};$contr_router$$.creating= async function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);$obj$$.id_hd2=void 0;await $obj$$.details.asyncJoinModel2($user$$.current_id_app,dmvt,{where:"ma_vt",setFields:($item$$,$ref$$)=>{$item$$.ten_vt=$ref$$.ten_vt;$item$$.combo=$ref$$.combo||[];$item$$.ma_thue=$ref$$.ma_thue;$item$$.status_vt=$ref$$.status;$item$$.gia_mua=$ref$$.gia_mua_tmp}});let $sp_het_su_dung$$=$obj$$.details.filter($d$$=>!$d$$.status_vt);if(0<$sp_het_su_dung$$.length)return $next$$(`C\u00e1c m\u1eb7t h\u00e0ng sau \u0111\u00e3 ng\u1eebng kinh doanh : ${$sp_het_su_dung$$.map($d$$=> $d$$.ten_vt).join(",")}`);let $ngay_nay_thang_truoc$$=moment($obj$$.ngay_ct).subtract(1,"months").toDate(),$details$$=[];$obj$$.details.forEach($d$$=>{$d$$.combo&&0<$d$$.combo.length?$details$$=[...$details$$,...$d$$.combo]:$details$$.push($d$$)});await $details$$.filter($r$$=>!$r$$.gia_mua).asyncJoinModel2($obj$$.id_app,"giatb",{where:$item$$=>({ma_vt:$item$$.ma_vt,id_app:$obj$$.id_app,nam:$ngay_nay_thang_truoc$$.getFullYear(),thang:$ngay_nay_thang_truoc$$.getMonth()+1}),fields:[{gia_mua:"gia"}]}); $ngay_nay_thang_truoc$$=moment($ngay_nay_thang_truoc$$).subtract(1,"months").toDate();await $details$$.filter($r$$=>!$r$$.gia_mua).asyncJoinModel2($obj$$.id_app,"giatb",{where:$item$$=>({ma_vt:$item$$.ma_vt,id_app:$obj$$.id_app,nam:$ngay_nay_thang_truoc$$.getFullYear(),thang:$ngay_nay_thang_truoc$$.getMonth()+1}),fields:[{gia_mua:"gia"}]});$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=$item$$.sl_xuat*($item$$.gia_ban_nt||0);$item$$.tien_ck=$item$$.tien_ck_nt=$item$$.sl_xuat*($item$$.tien_ck_nt||0);$item$$.tt=$item$$.tt_nt=$item$$.tien_nt-($item$$.tien_ck_nt||0);return{...$item$$}});$detail$$.gia_mua=$detail$$.combo.map($c$$=>($c$$.gia_mua||0)*$c$$.sl_xuat_1_combo).reduce(($a$$, $b$$)=>$a$$+$b$$,0)});$valid$$($user$$,$obj$$,async function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$obj$$)})};$contr_router$$.updating=async($user$$,$data$$,$obj$$,$next$$)=>{$data$$.details||($data$$.details=$obj$$.details);$data$$.ngay_ct=$data$$.ngay_ct||$obj$$.ngay_ct;await $data$$.details.asyncJoinModel2($user$$.current_id_app,dmvt,{where:"ma_vt",setFields:($item$$,$ref$$)=>{let $old_combo$$=($item$$.combo||[]).map($i$$=>({ma_vt:$i$$.ma_vt,sl_xuat:$i$$.sl_xuat})),$new_combo$$= ($ref$$.combo||[]).map($i$$=>({ma_vt:$i$$.ma_vt,sl_xuat:$i$$.sl_xuat}));_.isEqual($old_combo$$,$new_combo$$)||($item$$.combo=$ref$$.combo||[]);$item$$.gia_mua=$ref$$.gia_mua_tmp}});let $ngay_nay_thang_truoc$$=moment($data$$.ngay_ct).subtract(1,"months").toDate(),$details$$=[];$data$$.details.forEach($d$$=>{$d$$.combo&&0<$d$$.combo.length?$details$$=[...$details$$,...$d$$.combo]:$details$$.push($d$$)});await $details$$.filter($r$$=>!$r$$.gia_mua).asyncJoinModel2($obj$$.id_app,"giatb",{where:$item$$=> ({ma_vt:$item$$.ma_vt,id_app:$obj$$.id_app,nam:$ngay_nay_thang_truoc$$.getFullYear(),thang:$ngay_nay_thang_truoc$$.getMonth()+1}),fields:[{gia_mua:"gia"}]});$ngay_nay_thang_truoc$$=moment($ngay_nay_thang_truoc$$).subtract(1,"months").toDate();await $details$$.filter($r$$=>!$r$$.gia_mua).asyncJoinModel2($obj$$.id_app,"giatb",{where:$item$$=>({ma_vt:$item$$.ma_vt,id_app:$obj$$.id_app,nam:$ngay_nay_thang_truoc$$.getFullYear(),thang:$ngay_nay_thang_truoc$$.getMonth()+1}),fields:[{gia_mua:"gia"}]});$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=$item$$.sl_xuat*($item$$.gia_ban_nt||0);$item$$.tien_ck=$item$$.tien_ck_nt=$item$$.sl_xuat*($item$$.tien_ck_nt||0);$item$$.tt= $item$$.tt_nt=$item$$.tien_nt-($item$$.tien_ck_nt||0);return{...$item$$}});$detail$$.gia_mua=$detail$$.combo.map($c$$=>($c$$.gia_mua||0)*$c$$.sl_xuat_1_combo).reduce(($a$$,$b$$)=>$a$$+$b$$,0)});$valid$$($user$$,$data$$,async function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})};$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).joinModel2($id_app$$, dvcs,[{where:{ma_dvcs:"_id"},fields:[{name:"ten_dvcs",value:"ten_dvcs"}]}],function(){$callback$$()})},dt:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dt).joinModel2($id_app$$,dmdt,[{where:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback$$()})},kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel2($id_app$$,customer,[{where:"ma_kh",fields:["ten_kh","dia_chi","dien_thoai","email","nh_kh"]}],function(){$callback$$()})},nv:function($callback$$){$items$$.filter($item$$=> $item$$.ma_nv).joinModel2($id_app$$,dmnv,[{where:"ma_nv",fields:[{name:"ten_nv",value:"ten_nv"}]}],function(){$callback$$()})},contract:function($callback$$){$items$$.filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:["ten_hd"]}],function(){$callback$$()})},hd2:function($callback$$){$items$$.joinModel2($id_app$$,hd2,[{where:{_id:"id_dh2"},fields:[{name:"id_hd2",value:"_id"}]}],function(){$callback$$()})},lenhsx:function($callback$$){$items$$.joinModel2($id_app$$, global.getModel("lenhsx"),[{where:{_id:"id_dh2"},fields:[{name:"id_lenhsx",value:"_id"}]}],function(){$callback$$()})},pxh:function($callback$$){$items$$.joinModel2($id_app$$,pxh,[{where:{_id:"id_dh2"},fields:[{name:"id_pxh",value:"_id"}]}],function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.details&&($r$$.details.forEach($d$$=>{void 0==$d$$.gia_ban_ct_nt&&$r$$.ma_nt&&$tinhGiaChuaThue$$($r$$,$d$$);$d$$.tien_nt=$d$$.tien_ct_nt-$d$$.tien_ck_nt+$d$$.tien_thue_nt}), $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_tt= $r$$.t_tien_ct-$r$$.tc_ck+$r$$.t_thue+($r$$.tien_phi_vc||0),$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_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_vt:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){const $details$$=$r$$.details||[];setImmediate(async()=>{await $details$$.asyncJoinModel2($id_app$$,dmvt,[{where:"ma_vt",fields:"ten_vt ma_lvt tk_dt tk_gv tk_vt tk_ck".split(" "),setFields:($item$$,$ref$$)=>{$item$$.ten_vt=$ref$$.ten_vt;$item$$.tk_tl=$ref$$.tk_tl;$item$$.tk_dt=$ref$$.tk_dt;$item$$.tk_gv= $ref$$.tk_gv;$item$$.tk_vt=$ref$$.tk_vt;$item$$.tk_ck=$ref$$.tk_ck;$item$$.ma_nvt||($item$$.ma_nvt=$ref$$.ma_nvt)}}]);await $details$$.filter($d$$=>$d$$.ma_dvt&&!$d$$.he_so_qd).asyncJoinModel2($id_app$$,"dmqddvt",[{where:{ma_vt:"ma_vt",ma_dvt:"ma_dvt"},setFields:($item$$,$qddct$$)=>{let $he_so_qd$$=1;$qddct$$&&($he_so_qd$$=$qddct$$.ty_le_qd||1,$qddct$$.mau&&$qddct$$.tu&&($he_so_qd$$=$qddct$$.tu/$qddct$$.mau));$item$$.he_so_qd=$he_so_qd$$}}]);$details$$.filter($i$$=>void 0!=$i$$.sl_xuat).forEach($item$$=> {$item$$.sl_xuat_qd=($item$$.sl_xuat||0)*$item$$.he_so_qd});$callback1$$()})},function(){$callback$$()})},details_tt1:function($callback$$){async.mapSeries($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$$()})},details_csck:function($callback$$){async.mapSeries($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_dt:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_dt).joinModel2($id_app$$,dmdt,[{where:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback1$$()})}, function(){$callback$$()})},details_contract:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:[{name:"ten_hd",value:"ten_hd"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_phi:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_phi).joinModel2($id_app$$,dmphi,[{where:"ma_phi", fields:[{name:"ten_phi",value:"ten_phi"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_chietkhau:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.id_cs_ck).joinModel2($id_app$$,"dmchietkhau",[{where:{id_cs_ck:"_id"},fields:["ma_chietkhau","ten_chietkhau"]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){$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(){$fn$$(null,$items$$)})})};$contr_router$$.on("saved",async function($obj$$){try{if($obj$$.ma_kh&&$obj$$.dia_chi_nguoi_nhan){var $kh$$=await customer.findOne({ma_kh:$obj$$.ma_kh,id_app:$obj$$.id_app}).lean();if($kh$$){var $ecomuser_address$$=global.getModel("ecomuser_address"),$ten_nguoi_nhan$$=$obj$$.ten_nguoi_nhan||$kh$$.ten_kh,$sdt_nguoi_nhan$$=$obj$$.sdt_nguoi_nhan||$kh$$.dien_thoai,$tinh_thanh$$=$obj$$.tinh_thanh||$kh$$.tinh_thanh, $quan_huyen$$=$obj$$.quan_huyen||$kh$$.quan_huyen,$xa_phuong$$=$obj$$.xa_phuong||$kh$$.xa_phuong,$dia_chi$$=$obj$$.dia_chi_nguoi_nhan;await $ecomuser_address$$.findOne({ma_kh:$obj$$.ma_kh,id_app:$obj$$.id_app,tinh_thanh:$tinh_thanh$$,quan_huyen:$quan_huyen$$,xa_phuong:$xa_phuong$$,dia_chi:$dia_chi$$}).lean()||await $ecomuser_address$$.create({loai_dia_chi:"2",id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh,ten_nguoi_nhan:$ten_nguoi_nhan$$,sdt_nguoi_nhan:$sdt_nguoi_nhan$$,email_nguoi_nhan:$obj$$.email_nguoi_nhan|| $kh$$.email,dia_chi:$dia_chi$$,tinh_thanh:$tinh_thanh$$,quan_huyen:$quan_huyen$$,xa_phuong:$xa_phuong$$,ghi_chu:$obj$$.note_ship,ngam_dinh:!0,status:!0,user_created:$obj$$.user_created})}}}catch($e$$){console.error("Can't auto create address for customer",$e$$)}})};