UNPKG

flexbiz-server

Version:

Flexible Server

97 lines 64.8 kB
const moment=require("moment"),numeral=require("numeral"),_=require("lodash"),PostSokho=require("../../libs/post-sokho"),options=global.getModel("options"),PostSocai=require("../../libs/post-socai"),User=global.getModel("user"),Socai=global.getModel("socai"),transaction=global.getModel("transaction"),Evoucher=global.getModel("evoucher"),dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),dmtt=global.getModel("dmtt"),dmcpmh=global.getModel("dmcpmh"),dmkho=global.getModel("dmkho"),customer=global.getModel("customer"), dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),trangthai=global.getModel("trangthai"),currency=global.getModel("currency"),ptthanhtoan=global.getModel("ptthanhtoan"),dmdiemthuong=global.getModel("dmdiemthuong"),model=global.getModel("so1"),pt1=global.getModel("pt1"),controller=require("../../controllers/controller"),async=require("async"),ma_ct="SO1",ckdiem=require("../../libs/ckdiem"),tinhhoahong=require("../../libs/tinhhoahong"),blockchainconnect= require("../../libs/blockchainconnect"),App=global.getModel("app"),Introducer=global.getModel("introducer"),getGiaBan=require("../../libs/getGiabanAsync"),dmchietkhauhd=global.getModel("dmchietkhauhd"),dmkhuyenmaihd=global.getModel("dmkhuyenmaihd"),utils=require("../../libs/utils"),spend=async($appInfo$$,$receiver_user$$,$receiver_address$$,$amount$$,$denom$$,$memo$$,$introduce_code$$,$sub_action$$="")=>{try{await blockchainconnect.spend($appInfo$$._id,$appInfo$$.wallet_token,$appInfo$$.wallet_address, $receiver_address$$,{amount:$amount$$,denom:$denom$$,memo:$memo$$,action:"bonus",introduce_code:$introduce_code$$,sub_action:$sub_action$$})}catch($e$$){Logger.error("Error send coin",$e$$,$receiver_user$$,$receiver_address$$,$amount$$,$denom$$,$memo$$)}},validOffline=$detail$$=>{$detail$$.ma_thue?($detail$$.gia_ban_ct_nt=$detail$$.gia_ban_ct=Math.roundBy($detail$$.gia_ban_nt/(1+$detail$$.thue_suat/100),5),$detail$$.tien_hang_ct=$detail$$.tien_hang_ct_nt=Math.roundBy($detail$$.sl_xuat*$detail$$.gia_ban_ct_nt, 0),$detail$$.ty_le_ck&&($detail$$.tien_ck=$detail$$.tien_ck_nt=utils.round($detail$$.tien_hang_ct*$detail$$.ty_le_ck/100)),$detail$$.tien_thue=$detail$$.tien_thue_nt=Math.roundBy(($detail$$.tien_hang_ct_nt-$detail$$.tien_ck_nt)*$detail$$.thue_suat/100)):($detail$$.gia_ban_ct=$detail$$.gia_ban,$detail$$.gia_ban_ct_nt=$detail$$.gia_ban_nt,$detail$$.tien_hang_ct=$detail$$.tien_hang_ct_nt=$detail$$.tien_hang_nt,$detail$$.ty_le_ck&&($detail$$.tien_ck=$detail$$.tien_ck_nt=utils.round($detail$$.tien_hang_ct* $detail$$.ty_le_ck/100)),$detail$$.tien_thue=0,$detail$$.tien_thue_nt=0)},khuyenmaihd=async $obj$$=>{if($obj$$.details&&$obj$$.online){var $ckhds_query_t_tien_hang_ct_nt$$=$obj$$.details.filter($d$$=>!$d$$.km_yn_for).csum("tien_hang_ct_nt");if($ckhds_query_t_tien_hang_ct_nt$$!==0){var $hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$=$obj$$.details.filter($d$$=>!$d$$.km_yn_for).csum("tien_ck_nt");$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$=$ckhds_query_t_tien_hang_ct_nt$$-$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$; $obj$$.details=$obj$$.details.filter($d$$=>$d$$.km_yn_for!="khuyenmaihd");$ckhds_query_t_tien_hang_ct_nt$$={id_app:$obj$$.id_app,gt_tu:{$lte:$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$},ma_kho:{$in:[void 0,null,"",$obj$$.ma_kho]}};$ckhds_query_t_tien_hang_ct_nt$$.$and=[{$or:[{gt_den:{$in:[0,void 0,null]}},{gt_den:{$gte:$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$}}]}];var $ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$=moment($obj$$.ngay_ct).startOf("date").toDate();$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$={$lte:moment($obj$$.ngay_ct).endOf("date").toDate()}; $ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$={$gte:$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$};$ckhds_query_t_tien_hang_ct_nt$$.hieu_luc_tu=$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$;$ckhds_query_t_tien_hang_ct_nt$$.hieu_luc_den=$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$;$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$={$in:[void 0,null,"",$obj$$.ma_kh]};$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$={$in:[void 0,null,""]};if($obj$$.ma_kh){var $kh_lan_mua_thu$$=await customer.findOne({id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh},{nh_kh:1}); $kh_lan_mua_thu$$&&$kh_lan_mua_thu$$.nh_kh&&($ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$={$in:[void 0,null,"",$kh_lan_mua_thu$$.nh_kh]});($kh_lan_mua_thu$$=await model.countDocuments({id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh,trang_thai:{$in:["5","6"]}}))||($kh_lan_mua_thu$$=0);$kh_lan_mua_thu$$+=1}$ckhds_query_t_tien_hang_ct_nt$$.$or=[{$and:[{ma_kh:$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$},{nh_kh:{$in:["",null,void 0]}}]},{$and:[{nh_kh:$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$},{ma_kh:{$in:["",null,void 0]}}]}]; $ckhds_query_t_tien_hang_ct_nt$$.ap_dung_lan_mua_thu={$in:[null,void 0,0,void 0]};$ckhds_query_t_tien_hang_ct_nt$$.online=!0;$ckhds_query_t_tien_hang_ct_nt$$=await dmkhuyenmaihd.find($ckhds_query_t_tien_hang_ct_nt$$).sort({stt_uu_tien:1,gt_tu:-1,ma_kh:-1,nh_kh:-1,hieu_luc_tu:-1}).lean();for($hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$=0;$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$<$ckhds_query_t_tien_hang_ct_nt$$.length;$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$++)if(($ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$=$ckhds_query_t_tien_hang_ct_nt$$[$hieu_luc_tu_i_q_ma_kh_t_ck_nt_t_tt_nt$$])&& $ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.details_km&&$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.details_km.length>0){$kh_lan_mua_thu$$=$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.sl_toi_thieu||0;let $sl_dap_ung$$=0;$kh_lan_mua_thu$$&&$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.vts&&$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.vts.length>0&&$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.vts.forEach($vt$$=>{var $ds_tien$$=$obj$$.details.filter($d$$=>$d$$.ma_vt===$vt$$.ma_vt&&(!$vt$$.ma_tt1||$vt$$.ma_tt1===$d$$.ma_tt1)&&(!$vt$$.ma_tt2||$vt$$.ma_tt2=== $d$$.ma_tt2)&&(!$vt$$.ma_tt3||$vt$$.ma_tt3===$d$$.ma_tt3));if($ds_tien$$.length!=0){var $so_luong$$=$ds_tien$$.map($d$$=>$d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$ds_tien$$=$ds_tien$$.map($d$$=>($d$$.tien_hang_ct_nt||0)-($d$$.tien_ck_nt||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$vt$$.sl_tu&&$so_luong$$<$vt$$.sl_tu||$vt$$.sl_den&&$so_luong$$>$vt$$.sl_den||$vt$$.gt_tu&&$ds_tien$$<$vt$$.gt_tu||$vt$$.gt_den&&$ds_tien$$>$vt$$.gt_den||($sl_dap_ung$$+=1)}});if($kh_lan_mua_thu$$<=$sl_dap_ung$$){$ckhd_hieu_luc_den_q_nh_kh_tu_ngay$$.details_km.forEach($d$jscomp$6_detail$$=> {$d$jscomp$6_detail$$={...$d$jscomp$6_detail$$,km_yn_for:"khuyenmaihd"};$d$jscomp$6_detail$$.sl_xuat=$d$jscomp$6_detail$$.sl_km;$d$jscomp$6_detail$$.tien_hang_ct_nt=$d$jscomp$6_detail$$.tien_hang_ct=$d$jscomp$6_detail$$.tien_hang_nt=$d$jscomp$6_detail$$.tien_hang=0;$d$jscomp$6_detail$$.tien_ck_nt=$d$jscomp$6_detail$$.tien_ck=0;$d$jscomp$6_detail$$.ty_le_ck=0;$d$jscomp$6_detail$$.tien_nt=$d$jscomp$6_detail$$.tien=0;$obj$$.details.push($d$jscomp$6_detail$$)});break}}}}},chietkhauhd=async $obj$$=>{if(!$obj$$.signature&& $obj$$.online){$obj$$.details=$obj$$.details||[];var $t_ck_nt$$=$obj$$.details.csum("tien_ck_nt"),$t_tien_hang_ct_nt$$=$obj$$.details.csum("tien_hang_ct_nt");dmchietkhauhd.tinhChietKhau($obj$$.user_created,$obj$$.id_app,$obj$$,$t_tien_hang_ct_nt$$-$t_ck_nt$$)}},getPointBalance=async($id_app$$,$sender_email$$)=>new Promise(($resolve$$,$reject$$)=>{ckdiem({id_app:$id_app$$,email:$sender_email$$},($e$$,$rs$$)=>{if($e$$)return $reject$$(Error($e$$));$resolve$$($rs$$.length===0?0:$rs$$[0].ton00)})}),createTransactionBonus= async function($obj$$){if($obj$$.trang_thai!="5"&&$obj$$.trang_thai!="6")return await transaction.deleteTrans({id_ct:$obj$$._id.toString()+":bonus"},"Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m th\u01b0\u1edfng \u0111\u1ec3 t\u00ednh l\u1ea1i"),await transaction.deleteTrans({id_ct:$obj$$._id.toString()+":introduce"},"Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m th\u01b0\u1edfng gi\u1edbi thi\u1ec7u th\u00e0nh vi\u00ean m\u1edbi \u0111\u1ec3 t\u00ednh l\u1ea1i"),null;let $kh$$=await customer.findOne({id_app:$obj$$.id_app, ma_kh:$obj$$.ma_kh})||{},$lan_mua$$=(await model.find({id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh,ngay_ct:{$lte:$obj$$.ngay_ct}})).length+1;var $_app_diem_thuong_gioi_thieu_old_trans$$=await App.findOne({_id:$obj$$.id_app}),$_intro_amount$$=await Introducer.findOne({id_app:$obj$$.id_app,be_introduced:$obj$$.of_user||$obj$$.user_created});$_app_diem_thuong_gioi_thieu_old_trans$$=$_app_diem_thuong_gioi_thieu_old_trans$$.exfields.diem_thuong_gioi_thieu;$lan_mua$$===1&&$_app_diem_thuong_gioi_thieu_old_trans$$&& $_intro_amount$$&&await transaction.createTrans({id_app:$obj$$.id_app,sender_email:$obj$$.id_app,receiver_email:$_intro_amount$$.be_introduced_by,amount:$_app_diem_thuong_gioi_thieu_old_trans$$,description:"\u0110i\u1ec3m th\u01b0\u1edfng gi\u1edbi thi\u1ec7u kh\u00e1ch h\u00e0ng",otherData:{ma_ct:$obj$$.ma_ct,so_ct:$obj$$.so_ct,ngay_ct:$obj$$.ngay_ct,id_ct:$obj$$._id.toString()+":introduce"}});if(!$obj$$.exfields.survey)return null;let $now$$=moment(new Date).startOf("date").toDate();$_intro_amount$$= $obj$$.details.filter($d$$=>$d$$.tien_hang).map(async $d$$=>{var $amount$$=await dmvt.findOne({ma_vt:$d$$.ma_vt,id_app:$obj$$.id_app}),$ckdiemthuong_ty_le_diem_thuong$$=await dmdiemthuong.find({id_app:$obj$$.id_app,status:!0,ma_kh:{$in:[null,"",$kh$$.ma_kh]},ma_kho:{$in:[null,"",$obj$$.ma_kho]},nh_kh:{$in:[null,"",$kh$$.nh_kh]},hieu_luc_tu:{$lte:$now$$},hieu_luc_den:{$gte:$now$$},ap_dung_lan_mua_thu:{$in:[0,$lan_mua$$]},ma_vt:{$in:[null,"",void 0,$d$$.ma_vt]},ma_nvt:{$in:[null,"",void 0,$amount$$? $amount$$.ma_nvt:""]}}).sort({ma_vt:-1,ma_nvt:-1,hieu_luc_tu:-1}).limit(1);return $ckdiemthuong_ty_le_diem_thuong$$.length>0?($amount$$=0,$ckdiemthuong_ty_le_diem_thuong$$[0].tien?($ckdiemthuong_ty_le_diem_thuong$$=$ckdiemthuong_ty_le_diem_thuong$$[0].diem/$ckdiemthuong_ty_le_diem_thuong$$[0].tien)||($amount$$=Math.roundBy(($d$$.tien_hang-($d$$.tien_ck||0))*$ckdiemthuong_ty_le_diem_thuong$$,0)):$amount$$=$ckdiemthuong_ty_le_diem_thuong$$[0].diem*($d$$.sl_xuat||1),$amount$$):0});if($_intro_amount$$= (await Promise.all($_intro_amount$$)).reduce(($a$$,$b$$)=>$a$$+$b$$,0)){if($_app_diem_thuong_gioi_thieu_old_trans$$=await transaction.findOne({id_ct:$obj$$._id.toString()+":bonus"}),!$_app_diem_thuong_gioi_thieu_old_trans$$||$_app_diem_thuong_gioi_thieu_old_trans$$.amount!==$_intro_amount$$)return await transaction.deleteTrans({id_ct:$obj$$._id.toString()+":bonus"},"Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m th\u01b0\u1edfng \u0111\u1ec3 t\u00ednh l\u1ea1i"),await transaction.createTrans({id_app:$obj$$.id_app, sender_email:$obj$$.id_app,receiver_email:$obj$$.of_user||$obj$$.user_created,amount:$_intro_amount$$,description:"\u0110i\u1ec3m th\u01b0\u1edfng mua h\u00e0ng",otherData:{ma_ct:$obj$$.ma_ct,so_ct:$obj$$.so_ct,ngay_ct:$obj$$.ngay_ct,id_ct:$obj$$._id.toString()+":bonus"}})}else await transaction.deleteTrans({id_ct:$obj$$._id.toString()+":bonus"},"Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m th\u01b0\u1edfng \u0111\u1ec3 t\u00ednh l\u1ea1i")},createTransactionPayment=async function($obj$$){await transaction.deleteTrans({id_ct:$obj$$._id.toString()}); $obj$$.paymentByPoints&&await transaction.createTrans({id_app:$obj$$.id_app,sender_email:$obj$$.of_user||$obj$$.user_created,receiver_email:$obj$$.id_app,amount:$obj$$.paymentByPoints,description:"Thanh to\u00e1n cho \u0111\u01a1n h\u00e0ng s\u1ed1 "+$obj$$.so_ct,otherData:{ma_ct:$obj$$.ma_ct,so_ct:$obj$$.so_ct,ngay_ct:$obj$$.ngay_ct,id_ct:$obj$$._id.toString()}})},checkEvoucher=($user$$,$obj$$)=>{let $gt_don_hang$$=$obj$$.details.map($d$$=>$d$$.tien_hang_nt-($d$$.tien_ck_nt||0)).reduce(($a$$,$b$$)=> $a$$+$b$$,0);return($obj$$.evouchers||[]).map($ev$$=>new Promise(($resolve$$,$reject$$)=>{(async()=>{const $_e$$=await Evoucher.findOne({ma:$ev$$.ma,id_app:$user$$.current_id_app});if(!$_e$$)return Logger.error("Th\u1ebb mua h\u00e0ng kh\u00f4ng t\u1ed3n t\u1ea1i",$ev$$),$reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} kh\u00f4ng t\u1ed3n t\u1ea1i`));if(!$_e$$.online)return $reject$$(Error(`Kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng th\u1ebb mua h\u00e0ng ${$ev$$.ma} cho \u0111\u01a1n h\u00e0ng online`)); if($_e$$.id_ct_used&&$_e$$.id_ct_used!==($obj$$._id||"").toString())return $reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng`));if($_e$$.so_lan_sd>=1&&$_e$$.id_ct_useds&&$_e$$.id_ct_useds.length>0&&($_e$$.id_ct_useds=[...(new Set([...$_e$$.id_ct_useds]))],$_e$$.id_ct_useds.length>=$_e$$.so_lan_sd&&(!$obj$$._id||$_e$$.id_ct_useds.indexOf($obj$$._id)<0)))return $reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng h\u1ebft`)); if($_e$$.user&&$_e$$.user!==($obj$$.of_user||$obj$$.user_created||$user$$.email))return $reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} kh\u00f4ng h\u1ee3p l\u1ec7`));var $date_created_so_tien_giam$$=new Date($obj$$.date_created);if(moment($_e$$.hieu_luc_den).endOf("date").toDate().getTime()<$date_created_so_tien_giam$$.getTime())return $reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} \u0111\u00e3 h\u1ebft h\u1ea1n s\u1eed d\u1ee5ng`));if($_e$$.gt_don_hang_toi_thieu&&$gt_don_hang$$<$_e$$.gt_don_hang_toi_thieu)return $reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} ch\u1ec9 \u00e1p d\u1ee5ng cho c\u00e1c \u0111\u01a1n h\u00e0ng c\u00f3 gi\u00e1 tr\u1ecb l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng ${numeral($_e$$.gt_don_hang_toi_thieu).format()}`)); if($_e$$.vts&&$_e$$.vts.length>0&&$_e$$.sl_toi_thieu){let $sl_dap_ung$$=0;$_e$$.vts.forEach($vt$$=>{var $ds$jscomp$1_tien$$=$obj$$.details.filter($d$$=>$d$$.ma_vt===$vt$$.ma_vt&&(!$vt$$.ma_tt1||$vt$$.ma_tt1===$d$$.ma_tt1)&&(!$vt$$.ma_tt2||$vt$$.ma_tt2===$d$$.ma_tt2)&&(!$vt$$.ma_tt3||$vt$$.ma_tt3===$d$$.ma_tt3));if($ds$jscomp$1_tien$$.length!=0){var $so_luong$$=$ds$jscomp$1_tien$$.map($d$$=>$d$$.sl_xuat).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$ds$jscomp$1_tien$$=$ds$jscomp$1_tien$$.map($d$$=>($d$$.tien_hang_nt|| 0)-($d$$.tien_ck_nt||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$vt$$.sl_tu&&$so_luong$$<$vt$$.sl_tu||$vt$$.sl_den&&$so_luong$$>$vt$$.sl_den||$vt$$.gt_tu&&$ds$jscomp$1_tien$$<$vt$$.gt_tu||$vt$$.gt_den&&$ds$jscomp$1_tien$$>$vt$$.gt_den||($sl_dap_ung$$+=1)}});if($_e$$.sl_toi_thieu>$sl_dap_ung$$)return $reject$$(Error(`Th\u1ebb mua h\u00e0ng ${$ev$$.ma} kh\u00f4ng h\u1ee3p l\u1ec7 do kh\u00f4ng \u0111\u00e1p \u1ee9ng \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u s\u1ea3n ph\u1ea9m`))}$date_created_so_tien_giam$$= $_e$$.so_tien;$_e$$.ty_le&&($date_created_so_tien_giam$$=Math.roundBy($gt_don_hang$$*$_e$$.ty_le/100));$_e$$.so_tien_max&&$date_created_so_tien_giam$$>$_e$$.so_tien_max&&($date_created_so_tien_giam$$=$_e$$.so_tien_max);if($ev$$.so_tien_giam!==$date_created_so_tien_giam$$)return $reject$$(Error(`S\u1ed1 ti\u1ec1n gi\u1ea3m cho th\u1ebb ${$ev$$.ma} kh\u00f4ng h\u1ee3p l\u1ec7`));$ev$$.so_tien_giam=$date_created_so_tien_giam$$;$resolve$$(!0)})()}))},updateEvoucher=async($id_app$$,$evoucher_code$$,$id_ct_rs$$)=> {if($evoucher_code$$){$id_ct_rs$$=$id_ct_rs$$.toString();var $e$$=await Evoucher.findOne({ma:$evoucher_code$$,id_app:$id_app$$});$e$$?($id_app$$=$e$$.id_ct_useds||[],$id_app$$.push($id_ct_rs$$),$id_app$$=[...(new Set($id_app$$))],$id_ct_rs$$=await Evoucher.findOneAndUpdate({_id:$e$$._id},{id_ct_useds:$id_app$$}),Logger.info("update id_ct_useds",$evoucher_code$$,$id_app$$,$id_ct_rs$$)):Logger.error("can't find evoucher",$evoucher_code$$)}},deleteEvoucher=async($e$jscomp$3_id_app$$,$evoucher_code$jscomp$1_id_ct_useds$$, $id_ct$$)=>{$evoucher_code$jscomp$1_id_ct_useds$$&&($id_ct$$=$id_ct$$.toString(),$e$jscomp$3_id_app$$=await Evoucher.findOne({id_app:$e$jscomp$3_id_app$$,ma:$evoucher_code$jscomp$1_id_ct_useds$$}))&&($evoucher_code$jscomp$1_id_ct_useds$$=($e$jscomp$3_id_app$$.id_ct_useds||[]).filter($ct$$=>$ct$$!==$id_ct$$),await Evoucher.findOneAndUpdate({_id:$e$jscomp$3_id_app$$._id},{id_ct_useds:$evoucher_code$jscomp$1_id_ct_useds$$}))},validOnline=async function($user$$,$obj$$,$_opt_pre_obj_t_tien_evoucher$$){$_opt_pre_obj_t_tien_evoucher$$&& ($obj$$.tinh_thanh=$obj$$.tinh_thanh||$_opt_pre_obj_t_tien_evoucher$$.tinh_thanh,$obj$$.quan_huyen=$obj$$.quan_huyen||$_opt_pre_obj_t_tien_evoucher$$.quan_huyen,$obj$$.xa_phuong=$obj$$.xa_phuong||$_opt_pre_obj_t_tien_evoucher$$.xa_phuong,$obj$$.ma_freeship=$obj$$.ma_freeship!==void 0?$obj$$.ma_freeship:$_opt_pre_obj_t_tien_evoucher$$.ma_freeship,$obj$$.tien_freeship=$obj$$.tien_freeship!==void 0?$obj$$.tien_freeship:$_opt_pre_obj_t_tien_evoucher$$.ma_freeship);await ($obj$$.details||[]).filter($d$$=> !$d$$.ma_dvt&&$d$$.ma_tt1).asyncJoinModel2($user$$.current_id_app,dmtt,{where:{ma_vt:"ma_vt",ma_tt1:"ma_tt"},fields:"ma_dvt"});await ($obj$$.details||[]).filter($d$$=>!$d$$.ma_dvt&&$d$$.ma_tt2).asyncJoinModel2($user$$.current_id_app,dmtt,{where:{ma_vt:"ma_vt",ma_tt2:"ma_tt"},fields:"ma_dvt"});await ($obj$$.details||[]).filter($d$$=>!$d$$.ma_dvt&&$d$$.ma_tt3).asyncJoinModel2($user$$.current_id_app,dmtt,{where:{ma_vt:"ma_vt",ma_tt3:"ma_tt"},fields:"ma_dvt"});await ($obj$$.details||[]).filter($d$$=> !$d$$.ma_dvt).asyncJoinModel2($user$$.current_id_app,dmvt,{where:"ma_vt",fields:"ma_dvt"});await ($obj$$.details||[]).asyncJoinModel2($user$$.current_id_app,dmvt,{where:$item$$=>({ma_vt:$item$$.ma_vt}),fields:["ma_danh_muc","khu_vuc_kd","ten_vt","ten_vt_eng"]});var $details_dvgh_t_tien_nt$$=$obj$$.details.filter($d$$=>$d$$.khu_vuc_kd&&$d$$.khu_vuc_kd.length>0);for(var $freeship_i$$=0;$freeship_i$$<$details_dvgh_t_tien_nt$$.length;$freeship_i$$++){var $detail$$=$details_dvgh_t_tien_nt$$[$freeship_i$$], $i$$=$detail$$.khu_vuc_kd.find($k$$=>$k$$.tinh_thanh===$obj$$.tinh_thanh);if(!$i$$)throw Error(`Kh\u00f4ng th\u1ec3 giao m\u1eb7t h\u00e0ng ${$detail$$.ten_vt} t\u1edbi ${$obj$$.tinh_thanh}`);if($i$$.quan_huyen&&$i$$.quan_huyen.length>0){$i$$=$i$$.quan_huyen.find($q$$=>$q$$.quan_huyen===$obj$$.quan_huyen);if(!$i$$)throw Error(`Kh\u00f4ng th\u1ec3 giao m\u1eb7t h\u00e0ng ${$detail$$.ten_vt} t\u1edbi ${$obj$$.tinh_thanh}-${$obj$$.quan_huyen}`);if($i$$.xa_phuong&&$i$$.xa_phuong.length>0&&!$i$$.xa_phuong.find($q$$=> $q$$.xa_phuong===$obj$$.xa_phuong))throw Error(`Kh\u00f4ng th\u1ec3 giao m\u1eb7t h\u00e0ng ${$detail$$.ten_vt} t\u1edbi ${$obj$$.tinh_thanh}-${$obj$$.quan_huyen} - ${$obj$$.xa_phuong}`);}}const $t_gt_hd$$=$obj$$.details.filter($d$$=>!$d$$.km_yn_for).map($a$$=>$a$$.tien_hang_nt||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0);try{await Promise.all($obj$$.details.map($item$$=>(async()=>{if($item$$.km_yn_for&&$item$$.km_yn_for.toUpperCase()!="SO1")$item$$.gia_ban=$item$$.gia_ban_nt=0,$item$$.tien_ck=$item$$.tien_ck_nt= 0,$item$$.tien_hang=$item$$.tien_hang_nt=0,$item$$.tien=$item$$.tien_nt=0;else if(!$item$$.km_yn_for||$item$$.km_yn_for.toUpperCase()=="SO1"){if($item$$.km_yn_for&&$item$$.km_yn_for.toUpperCase()=="SO1"&&!$item$$.id_cs)throw Error("S\u1ea3n ph\u1ea9m mua th\u00eam theo ch\u00ednh s\u00e1ch chi\u1ebft kh\u1ea5u thi\u1ebfu id_cs");var $condition$$={ma_vt:$item$$.ma_vt,id_app:$user$$.current_id_app,user:{email:$obj$$.user_created},ma_kh:$obj$$.ma_kh,ngay_ct:$obj$$.ngay_ct};$item$$.ma_tt1&&($condition$$.ma_tt1= $item$$.ma_tt1);$item$$.ma_tt2&&($condition$$.ma_tt2=$item$$.ma_tt2);$item$$.ma_tt3&&($condition$$.ma_tt3=$item$$.ma_tt3);$condition$$.sl_xuat=$item$$.sl_xuat;$condition$$.online=!0;$condition$$.t_gt_hd=$t_gt_hd$$;if($condition$$=await getGiaBan($condition$$)){if($item$$.gia_ban_nt!=$condition$$.gia_ban_le)throw Error("Gi\u00e1 b\u00e1n kh\u00f4ng h\u1ee3p l\u1ec7");if($item$$.km_yn_for&&$item$$.km_yn_for.toUpperCase()=="SO1"){let $dc$$=($condition$$.discount||[]).find($r$$=>$r$$._id===$item$$.id_cs&& $r$$.mua_them&&$r$$.gt_hd_tu>0&&$r$$.gt_hd_tu<=$t_gt_hd$$&&(!$r$$.gt_hd_den||$r$$.gt_hd_den>=$t_gt_hd$$));if(!$dc$$)throw Error("S\u1ea3n ph\u1ea9m mua th\u00eam theo ch\u00ednh s\u00e1ch chi\u1ebft kh\u1ea5u c\u00f3 id_cs kh\u00f4ng h\u1ee3p l\u1ec7");$condition$$.ty_le_ck=$dc$$.ty_le_ck;$condition$$.tien_ck=$dc$$.tien_ck;$condition$$.sl_tu=$dc$$.sl_tu;$condition$$.gt_hd_tu=$dc$$.gt_hd_tu;$condition$$.gt_hd_den=$dc$$.gt_hd_den;$condition$$.sl_ap_dung=$dc$$.sl_ap_dung;$condition$$.so_tien_max=$dc$$.so_tien_max}$item$$.tien_ck= 0;$item$$.tien_ck_nt=0;$item$$.ty_le_ck=0;delete $condition$$._id;delete $condition$$.ma_vt;delete $condition$$.ten_vt;_.extend($item$$,$condition$$);$item$$.gia_ban=$item$$.gia_ban_nt=$item$$.gia_ban_le;$item$$.gia_ban_ct=$item$$.gia_ban_ct_nt=$item$$.gia_ban_le_ct;$condition$$.sl_ap_dung?$item$$.tien_ck?($item$$.tien_ck=$item$$.tien_ck_nt=Math.roundBy(($item$$.tien_ck||0)*Math.min($item$$.sl_xuat,$condition$$.sl_ap_dung)),$item$$.ty_le_ck=0):$item$$.tien_ck=$item$$.tien_ck_nt=Math.roundBy(($item$$.ty_le_ck|| 0)*$item$$.gia_ban_ct_nt/100*Math.min($item$$.sl_xuat,$condition$$.sl_ap_dung)):$item$$.tien_ck?($item$$.tien_ck=$item$$.tien_ck_nt=Math.roundBy(($item$$.tien_ck||0)*$item$$.sl_xuat),$item$$.ty_le_ck=0):$item$$.tien_ck=$item$$.tien_ck_nt=Math.roundBy(($item$$.ty_le_ck||0)*$item$$.gia_ban_ct_nt/100*$item$$.sl_xuat)}else $item$$.gia_ban=$item$$.gia_ban_nt=$item$$.gia_ban_le0;$item$$.tien_hang=$item$$.tien_hang_nt=Math.roundBy($item$$.gia_ban*$item$$.sl_xuat);$item$$.tien_hang_ct=$item$$.tien_hang_ct_nt= Math.roundBy($item$$.gia_ban_ct*$item$$.sl_xuat);$item$$.so_tien_max&&($item$$.tien_ck=$item$$.tien_ck_nt=Math.min($item$$.so_tien_max,$item$$.tien_ck_nt));$item$$.tien=$item$$.tien_nt=$item$$.tien_hang_ct-($item$$.tien_ck||0)+($item$$.tien_thue||0);$item$$.ma_thue?$item$$.tien_thue=$item$$.tien_thue_nt=Math.roundBy(($item$$.tien_hang_ct_nt-$item$$.tien_ck_nt)*$item$$.thue_suat/100):($item$$.tien_thue=0,$item$$.tien_thue_nt=0)}})()))}catch($e$$){throw Error($e$$.message);}$details_dvgh_t_tien_nt$$= $obj$$.details.map($a$$=>($a$$.tien_hang_ct_nt||0)-($a$$.tien_ck_nt||0)+($a$$.tien_thue_nt||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0)-($obj$$.tien_ck_hd||0);$obj$$.ngay_ct=new Date($obj$$.ngay_ct);if($obj$$.tien_freeship){if(!$obj$$.ma_freeship)throw Error("Ti\u1ec1n mi\u1ec5n ph\u00ed v\u1eadn chuy\u1ec3n kh\u00f4ng h\u1ee3p l\u1ec7 do kh\u00f4ng c\u00f3 m\u00e3 freeship");$freeship_i$$=await global.getModel("ecompromotion_freeships").findOne({id_app:$obj$$.id_app,ma_chietkhau:$obj$$.ma_freeship,status:!0, trang_thai:"1",hieu_luc_tu:{$lte:$obj$$.ngay_ct},hieu_luc_den:{$gte:$obj$$.ngay_ct}});if(!$freeship_i$$)throw Error(`M\u00e3 freeship ${$obj$$.ma_freeship} kh\u00f4ng t\u1ed3n t\u1ea1i ho\u1eb7c kh\u00f4ng c\u00f3 hi\u1ec7u l\u1ef1c`);if($freeship_i$$.phi_van_chuyen<$obj$$.tien_freeship)throw Error("Ti\u1ec1n mi\u1ec5n ph\u00ed v\u1eadn chuy\u1ec3n ph\u1ea3i nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng ti\u1ec1n mi\u1ec5n ph\u00ed v\u1eadn chuy\u1ec3n trong ch\u00ednh s\u00e1ch freeship");if($freeship_i$$.gia_tri_dh&& $freeship_i$$.gia_tri_dh>$details_dvgh_t_tien_nt$$)throw Error("Ti\u1ec1n mi\u1ec5n ph\u00ed v\u1eadn chuy\u1ec3n kh\u00f4ng h\u1ee3p l\u1ec7. Gi\u00e1 tr\u1ecb \u0111\u01a1n h\u00e0ng ph\u1ea3i >="+$freeship_i$$.gia_tri_dh);if($freeship_i$$.details&&$freeship_i$$.details.length>0){$detail$$=!1;for($i$$=0;$i$$<$freeship_i$$.details.length;$i$$++){let $free$$=$freeship_i$$.details[$i$$];if($free$$.ma_vt){if($obj$$.details.find($d$$=>$d$$.ma_vt===$free$$.ma_vt)){$detail$$=!0;break}}else if($free$$.ma_danh_muc&& $obj$$.details.find($d$$=>$d$$.ma_danh_muc===$free$$.ma_danh_muc)){$detail$$=!0;break}}if(!$detail$$)throw Error("Ti\u1ec1n mi\u1ec5n ph\u00ed v\u1eadn chuy\u1ec3n kh\u00f4ng h\u1ee3p l\u1ec7");}if($obj$$.tien_phi_vc<$obj$$.tien_freeship)throw Error("Ti\u1ec1n mi\u1ec5n ph\u00ed v\u1eadn chuy\u1ec3n ph\u1ea3i nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng ti\u1ec1n ph\u00ed v\u1eadn chuy\u1ec3n");}try{var $_checkEvoucher_tien_con_lai$$=checkEvoucher($user$$,$obj$$,$_opt_pre_obj_t_tien_evoucher$$);await Promise.all($_checkEvoucher_tien_con_lai$$)}catch($e$$){throw Logger.error($e$$), Error($e$$.message||$e$$);}$obj$$.evouchers=$obj$$.evouchers||($_opt_pre_obj_t_tien_evoucher$$||{}).evouchers||[];$_opt_pre_obj_t_tien_evoucher$$=$obj$$.evouchers.map($e$$=>$e$$.so_tien_giam||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$_checkEvoucher_tien_con_lai$$=$details_dvgh_t_tien_nt$$-($obj$$.tt_tien_thuong||0);$_opt_pre_obj_t_tien_evoucher$$>$_checkEvoucher_tien_con_lai$$&&($_opt_pre_obj_t_tien_evoucher$$=$_checkEvoucher_tien_con_lai$$);$obj$$.tien_evoucher=$_opt_pre_obj_t_tien_evoucher$$;if($obj$$.tien_evoucher&& ($_opt_pre_obj_t_tien_evoucher$$=await options.findOne({id_app:$obj$$.id_app,id_func:$obj$$.ma_ct.toLowerCase()}).lean())&&$_opt_pre_obj_t_tien_evoucher$$.option&&$_opt_pre_obj_t_tien_evoucher$$.option.phan_bo_evoucher){let $tien_evoucher_da_phan_bo$$=0,$tien_evoucher$$=$obj$$.tien_evoucher,$tien_evoucher_con_lai$$=$tien_evoucher$$-$tien_evoucher_da_phan_bo$$;($obj$$.details||[]).forEach($detail$$=>{if($tien_evoucher_con_lai$$>0){var $tien_evoucher_duoc_pb_tien_hang_con_lai$$=$detail$$.tien_hang_nt- ($detail$$.tien_ck_nt||0);$tien_evoucher_duoc_pb_tien_hang_con_lai$$=$tien_evoucher_con_lai$$>$tien_evoucher_duoc_pb_tien_hang_con_lai$$?$tien_evoucher_duoc_pb_tien_hang_con_lai$$:$tien_evoucher_con_lai$$;$detail$$.tien_evoucher_duoc_pb=$tien_evoucher_duoc_pb_tien_hang_con_lai$$;$tien_evoucher_da_phan_bo$$+=$tien_evoucher_duoc_pb_tien_hang_con_lai$$;$tien_evoucher_con_lai$$=$tien_evoucher$$-$tien_evoucher_da_phan_bo$$}else $tien_evoucher_con_lai$$=0})}},valid=async function($user$$,$obj$$,$next$$, $detail$jscomp$5_i$jscomp$3_pre_obj$$){$detail$jscomp$5_i$jscomp$3_pre_obj$$&&($obj$$.online=$detail$jscomp$5_i$jscomp$3_pre_obj$$.online,$obj$$._id=$detail$jscomp$5_i$jscomp$3_pre_obj$$._id,$obj$$.details=$obj$$.details||$detail$jscomp$5_i$jscomp$3_pre_obj$$.details||[],$obj$$.evouchers=$obj$$.evouchers||$detail$jscomp$5_i$jscomp$3_pre_obj$$.evouchers);await $obj$$.details.asyncJoinModel2($obj$$.id_app,"vat",{where:"ma_thue",fields:["thue_suat","tk_thue_co","tk_thue_co_treo"]});if($obj$$.online)try{await validOnline($user$$, $obj$$,$detail$jscomp$5_i$jscomp$3_pre_obj$$)}catch($e$$){return $next$$($e$$.message)}else for($detail$jscomp$5_i$jscomp$3_pre_obj$$=0;$detail$jscomp$5_i$jscomp$3_pre_obj$$<($obj$$.details||[]).length;$detail$jscomp$5_i$jscomp$3_pre_obj$$++)validOffline(($obj$$.details||[])[$detail$jscomp$5_i$jscomp$3_pre_obj$$]);if($obj$$.paymentByPoints)try{var $he_so_qd_i$$=await currency.getRate($obj$$.id_app,$obj$$.ma_nt);$obj$$.tien_paymentByPoints=$obj$$.paymentByPoints*($he_so_qd_i$$||1)}catch($e$$){return $next$$($e$$.message)}for($he_so_qd_i$$= 0;$he_so_qd_i$$<($obj$$.details||[]).length;$he_so_qd_i$$++)$detail$jscomp$5_i$jscomp$3_pre_obj$$=($obj$$.details||[])[$he_so_qd_i$$],$detail$jscomp$5_i$jscomp$3_pre_obj$$.line=$he_so_qd_i$$,$obj$$.ma_nt==="VND"&&($detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_hang=$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_hang_nt,$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_hang_ct=$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_hang_ct_nt,$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_ck=$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_ck_nt|| 0,$detail$jscomp$5_i$jscomp$3_pre_obj$$.gia_ban=$detail$jscomp$5_i$jscomp$3_pre_obj$$.gia_ban_nt,$detail$jscomp$5_i$jscomp$3_pre_obj$$.gia_ban_ct=$detail$jscomp$5_i$jscomp$3_pre_obj$$.gia_ban_ct_nt,$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_thue=$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_thue_nt||0),$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_nt=Math.max(0,$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_hang_ct_nt-$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_ck_nt+$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_thue_nt), $detail$jscomp$5_i$jscomp$3_pre_obj$$.tien=Math.max(0,$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_hang-$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_ck+$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_thue),$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_xuat=$detail$jscomp$5_i$jscomp$3_pre_obj$$.tien_xuat_nt;$obj$$.ma_dvcs?$next$$(null,$obj$$):dvcs.findOne({id_app:$user$$.current_id_app},{_id:1},function($error$$,$dv$$){return $error$$?$next$$($error$$):$dv$$?($obj$$.ma_dvcs=$dv$$._id.toString(),$next$$(null,$obj$$)): $next$$("C\u00f4ng ty/c\u1eeda h\u00e0ng n\u00e0y ch\u01b0a c\u00f3 m\u1ed9t \u0111\u01a1n v\u1ecb c\u01a1 s\u1edf n\u00e0o")})}; module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,requireRecaptchaToken:!1,kiem_tra_han_muc_cong_no:!0,unique:["so_ct"],sort:{ngay_ct:-1,so_ct:-1},onEmitEvent:async($obj$$,$users_will_receive$$,$next$$)=>{$obj$$.user_nv_giao_hang&&$users_will_receive$$.push($obj$$.user_nv_giao_hang);$next$$(null,$users_will_receive$$)}});$contr_router$$.route();$contr_router$$.saving=async($user$$,$obj$$)=>{$obj$$.user_created||($obj$$.user_created= $user$$.email);$obj$$.details.forEach($d$$=>{$d$$.tien_xuat_tmp=$d$$.sl_xuat*($d$$.gia_mua||0)});await chietkhauhd($obj$$);await khuyenmaihd($obj$$);$obj$$.details.forEach($d$$=>{$d$$.sl_xuat=utils.round($d$$.sl_xuat,4);$d$$.tt_nt=($d$$.tien_han_ct_nt||0)-($d$$.tien_ck_nt||0);$d$$.tt=($d$$.tien_hang_ct||0)-($d$$.tien_ck||0)});await tinhhoahong($obj$$,$user$$)};$contr_router$$.handleAfterPayment=async $obj$$=>{Logger.info("[so1][handleAfterPayment]",$obj$$);$obj$$.trang_thai_tt="5";$obj$$.trang_thai= "1";await model.updateOne({_id:$obj$$._id},{details:$obj$$.details,trang_thai_tt:"5",trang_thai:"1"});let $ctrlSO1$$=global.controllers.SO1;$ctrlSO1$$&&(await controller.asyncPostData($obj$$,$ctrlSO1$$),controller.pushNotification($ctrlSO1$$,$obj$$));return null};$contr_router$$.post=async function($obj$$,$fn$$,$_options$$={}){$fn$$||=function(){};let $he_so_qd$$=await currency.getRate($obj$$.id_app,$obj$$.ma_nt)||1;const $appInfo$$=await global.getModel("app").findOne({_id:$obj$$.id_app}).lean(); await $obj$$.details.filter($d$$=>!$d$$.ma_thue).asyncJoinModel2($obj$$.id_app,dmvt,{where:"ma_vt",fields:["ma_thue","gia_mua"]});await $obj$$.details.filter($d$$=>$d$$.ma_thue).asyncJoinModel2($obj$$.id_app,global.getModel("vat"),{where:"ma_thue",fields:["thue_suat","tk_thue_co","tk_thue_co_treo"]});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; $item$$.ma_kho=$detail$$.ma_kho;return $item$$}):[$detail$$]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]),$ket_qua_post_sokho$$;async.series({post_sokho:function($callback$$){const $details$$=[];$obj$$.details.forEach($d$$=>{$details$$.push({...$d$$})});(new PostSokho($obj$$,$product_details$$,async function($detail$$,$callback$$){$detail$$.nxt=2;$detail$$.ngay_ct=$obj$$.ngay_giao_hang||$obj$$.ngay_ct;$detail$$.ma_kho=$detail$$.ma_kho||$obj$$.ma_kho;$callback$$($detail$$)})).run(function($e$$,$rs$$){if($e$$)return $callback$$($e$$); $ket_qua_post_sokho$$=$rs$$.rs_sokho;$callback$$(null,$rs$$)})},post_socai:function($callback$$){options.findOne({id_app:$obj$$.id_app,id_func:ma_ct.toLowerCase()}).lean().exec(async($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$,$rs$jscomp$3_tien_paymentByPoints_tt$$)=>{let $option$$;$option$$=$rs$jscomp$3_tien_paymentByPoints_tt$$?$rs$jscomp$3_tien_paymentByPoints_tt$$.option||{}:{};let $kho$$=await dmkho.findOne({id_app:$obj$$.id_app,ma_kho:$obj$$.ma_kho}).lean();$kho$$||={};let $pt$$; $obj$$.pt_thanh_toan&&global.mongoose.Types.ObjectId.isValid($obj$$.pt_thanh_toan)&&($pt$$=await ptthanhtoan.findOne({id_app:$obj$$.id_app,_id:$obj$$.pt_thanh_toan}).lean());$pt$$=$pt$$||{};$pt$$.tk_tien=$pt$$.tk_cn;$pt$$.tk_cn=$pt$$.tk_cn||$kho$$.tk_cn;$pt$$.tk_dt=$kho$$.tk_dt||$pt$$.tk_dt;$pt$$.tk_gv=$kho$$.tk_gv||$pt$$.tk_gv;$pt$$.tk_ck=$kho$$.tk_ck||$pt$$.tk_ck;$pt$$.tk_cp_gh=$kho$$.tk_cp_gh||$pt$$.tk_cp_gh;$pt$$.tk_cp_bh=$kho$$.tk_cp_bh||$pt$$.tk_cp_bh;$pt$$.tk_cp_ctv=$kho$$.tk_cp_ctv||$pt$$.tk_cp_ctv; $pt$$.tk_cn_ctv=$kho$$.tk_cn_ctv||$pt$$.tk_cn_ctv;$option$$.tk_tien=$pt$$.tk_tien||$kho$$.tk_tien||$option$$.tk_tien||"1111";$option$$.tk_cn=$pt$$.tk_cn||$option$$.tk_cn||"131111";$option$$.tk_dt=$pt$$.tk_dt||$option$$.tk_dt||"51111";$option$$.tk_tl=$pt$$.tk_tl||$option$$.tk_tl||"52131";$option$$.tk_ck=$pt$$.tk_ck||$option$$.tk_ck||"52111";$option$$.tk_gv=$pt$$.tk_gv||$option$$.tk_gv||"6321";$option$$.tk_cp_gh=$pt$$.tk_cp_gh||$option$$.tk_cp_gh||"6418";$option$$.tk_cp_bh=$pt$$.tk_cp_bh||$option$$.tk_cp_bh|| $option$$.tk_ck;$option$$.tk_cp_ctv=$pt$$.tk_cp_ctv||$option$$.tk_cp_ctv;$option$$.tk_cn_ctv=$pt$$.tk_cn_ctv||$option$$.tk_cn_ctv;$pt$$.ten&&$pt$$.ten.indexOf("COD")>=0?$option$$.ma_kh=$obj$$.nhan_vien_giao_hang||$obj$$.don_vi_vc||$pt$$.ma_kh||$obj$$.ma_kh:$option$$.ma_kh=$pt$$.ma_kh||$obj$$.ma_kh;const $details$$=[];$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$=$obj$$.details.map($a$$=>($a$$.tien_hang_nt||0)-($a$$.tien_ck_nt||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0)-(($obj$$.tt_tien_thuong|| 0)+($obj$$.tien_ck_hd||0)+($obj$$.tien_evoucher||0));$rs$jscomp$3_tien_paymentByPoints_tt$$=$obj$$.tien_paymentByPoints||0;const $hoachtoandtvathue$$=($tk_tien$$,$ty_le$$)=>{$obj$$.details.forEach(function($d$jscomp$26_detail$$){$d$jscomp$26_detail$$={...$d$jscomp$26_detail$$};let $tien_nt$$;$tien_nt$$=$d$jscomp$26_detail$$.tien_hang_ct_nt;$d$jscomp$26_detail$$.tk_co=$option$$.tk_dt;$d$jscomp$26_detail$$.tk_no=$tk_tien$$;$d$jscomp$26_detail$$.ma_kh_co=$obj$$.ma_kh;$d$jscomp$26_detail$$.ma_kh_no=$option$$.ma_kh; $d$jscomp$26_detail$$.ngay_ct=$obj$$.ngay_giao_hang||$obj$$.ngay_ct;$d$jscomp$26_detail$$.tien=$d$jscomp$26_detail$$.tien_nt=Math.roundBy($tien_nt$$*$ty_le$$);$d$jscomp$26_detail$$.flag="DT_"+($d$jscomp$26_detail$$.ma_vt||"");$details$$.push($d$jscomp$26_detail$$)});$obj$$.details.filter($d$$=>$d$$.tien_thue).forEach($detail$$=>{$detail$$={...$detail$$};$detail$$.tk_no=$tk_tien$$;$detail$$.tk_co=$detail$$.tk_thue_co_treo||$detail$$.tk_thue_co;$detail$$.ma_kh||($detail$$.ma_kh=$pt$$.ma_kh||$obj$$.ma_kh); $detail$$.tien=$detail$$.tien_nt=Math.roundBy($detail$$.tien_thue_nt*$ty_le$$);$detail$$.flag="THUE_"+($detail$$.ma_vt||"");$details$$.push($detail$$)})};$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$&&($hoachtoandtvathue$$($option$$.tk_cn,$rs$jscomp$3_tien_paymentByPoints_tt$$/$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$),$hoachtoandtvathue$$($option$$.tk_tien,($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$-$rs$jscomp$3_tien_paymentByPoints_tt$$)/ $error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$));$obj$$.details.filter($d$$=>$d$$.diem_thuong&&$d$$.tk_km_no&&$d$$.tk_km_co).forEach(function($d$$){let $detail$$={...$d$$};$detail$$.tk_co=$option$$.tk_km_co||$d$$.tk_km_co;$detail$$.tk_no=$option$$.tk_km_no||$d$$.tk_km_no;$detail$$.ma_kh=$obj$$.ma_kh;$detail$$.tien=$detail$$.tien_nt=$detail$$.diem_thuong*$he_so_qd$$;$details$$.push($detail$$)});$obj$$.ctcpbhs&&$obj$$.ctcpbhs.forEach(function($d$jscomp$30_detail$$){$d$jscomp$30_detail$$= {...$d$jscomp$30_detail$$};$d$jscomp$30_detail$$.tk_co=$option$$.tk_dt;$d$jscomp$30_detail$$.tk_no=$option$$.tk_cn;$d$jscomp$30_detail$$.ma_kh_co=$obj$$.ma_kh;$d$jscomp$30_detail$$.ma_kh_no=$option$$.ma_kh;$d$jscomp$30_detail$$.ngay_ct=$obj$$.ngay_giao_hang||$obj$$.ngay_ct;$d$jscomp$30_detail$$.tien_nt=$d$jscomp$30_detail$$.tien_cp_nt;$d$jscomp$30_detail$$.tien=$d$jscomp$30_detail$$.tien_cp;$details$$.push($d$jscomp$30_detail$$)});$obj$$.tien_phi_vc&&$option$$.tk_phi_vc&&($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$= {},$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_co=$option$$.tk_phi_vc,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_no=$option$$.tk_cn,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt=$obj$$.tien_phi_vc||0,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien=$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_co=$obj$$.ma_kh,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_no= $obj$$.nhan_vien_giao_hang||$obj$$.don_vi_vc,$details$$.push($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$));$obj$$.tien_freeship&&($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$={},$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_no=$option$$.tk_cp_gh,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_co=$option$$.tk_cn,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt=$obj$$.tien_freeship||0,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien= $error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_no=$obj$$.ma_kh,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_co=$option$$.ma_kh,$details$$.push($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$));$obj$$.tt_tien_thuong&&($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$={},$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_no=$option$$.tk_cp_bh, $error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_co=$option$$.tk_cn,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt=$obj$$.tt_tien_thuong||0,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien=$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_no=$obj$$.ma_kh,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_co=$option$$.ma_kh,$details$$.push($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$)); $obj$$.tien_evoucher&&($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$={},$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_no=$obj$$.tk_no_evoucher||$option$$.tk_no_evoucher||$option$$.tk_ck,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tk_co=$obj$$.tk_co_evoucher||$option$$.tk_co_evoucher||$option$$.tk_cn,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt=$obj$$.tien_evoucher||0,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien= $error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.tien_nt,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_no=$obj$$.ma_kh,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.ma_kh_co=$option$$.ma_kh,$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$.check_balance=!1,$details$$.push($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$));if($error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$=($obj$$.tien_ck_hd||0)+$obj$$.details.map($d$$=> $d$$.tien_ck||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0))$rs$jscomp$3_tien_paymentByPoints_tt$$={},$rs$jscomp$3_tien_paymentByPoints_tt$$.tk_no=$option$$.tk_ck,$rs$jscomp$3_tien_paymentByPoints_tt$$.tk_co=$option$$.tk_cn,$rs$jscomp$3_tien_paymentByPoints_tt$$.tien_nt=$error$jscomp$3_t_tt_tien_ck_hd_tt_tt$jscomp$1_tt$jscomp$2_tt$$,$rs$jscomp$3_tien_paymentByPoints_tt$$.tien=$rs$jscomp$3_tien_paymentByPoints_tt$$.tien_nt,$rs$jscomp$3_tien_paymentByPoints_tt$$.ma_kh_no=$obj$$.ma_kh,$rs$jscomp$3_tien_paymentByPoints_tt$$.ma_kh_co= $option$$.ma_kh,$rs$jscomp$3_tien_paymentByPoints_tt$$.check_balance=!1,$details$$.push($rs$jscomp$3_tien_paymentByPoints_tt$$);$obj$$.thu_nhap_ctv&&($obj$$.details.filter($r$$=>$r$$.tien_hoa_hong_ctv).forEach($r$$=>{$r$$.tk_cp_ctv=$r$$.tk_cp_ctv||$obj$$.tk_cp_ctv||$kho$$.tk_cp_ctv||options.tk_cp_ctv||$obj$$.tk_no_hoa_hong||$kho$$.tk_no_hoa_hong||options.tk_no_hoa_hong;$r$$.tk_cn_ctv=$r$$.tk_cn_ctv||$obj$$.tk_cn_ctv||$kho$$.tk_cn_ctv||options.tk_cn_ctv||$obj$$.tk_co_hoa_hong||$kho$$.tk_co_hoa_hong|| options.tk_co_hoa_hong}),(await $obj$$.details.filter($r$$=>$r$$.tien_hoa_hong_ctv).asyncGroupBy(["tk_cp_ctv","tk_cn_ctv"],["tien_hoa_hong_ctv"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_no=$r$$.tk_cp_ctv;$tt$$.tk_co=$r$$.tk_cn_ctv;$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong_ctv;$tt$$.ma_kh=$obj$$.ma_ctv;$tt$$.ma_nt="VND";$tt$$.ty_gia=1;$tt$$.tk_no&&$tt$$.tk_co&&$details$$.push($tt$$)}));$obj$$.tien_hoa_hong&&$obj$$.ma_kh_hoa_hong&&($obj$$.details.filter($r$$=>$r$$.tien_hoa_hong).forEach($r$$=>{$r$$.tk_no_hoa_hong= $r$$.tk_no_hoa_hong||$obj$$.tk_no_hoa_hong||$kho$$.tk_no_hoa_hong||options.tk_no_hoa_hong;$r$$.tk_co_hoa_hong=$r$$.tk_co_hoa_hong||$obj$$.tk_co_hoa_hong||$kho$$.tk_co_hoa_hong||options.tk_co_hoa_hong}),(await $obj$$.details.filter($r$$=>$r$$.tien_hoa_hong).asyncGroupBy(["tk_no_hoa_hong","tk_co_hoa_hong"],["tien_hoa_hong"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_no=$r$$.tk_no_hoa_hong;$tt$$.tk_co=$r$$.tk_co_hoa_hong;$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong;$tt$$.tien=$tt$$.tien_nt*($obj$$.ty_gia|| 1);$tt$$.ma_kh=$obj$$.ma_kh_hoa_hong;$tt$$.ma_nv=$obj$$.ma_nv;$tt$$.check_balance=!1;$tt$$.ma_nt="VND";$tt$$.ty_gia=1;$tt$$.tk_no&&$tt$$.tk_co&&$details$$.push($tt$$)}));$obj$$.tien_hoa_hong_gt&&$obj$$.ma_kh_hoa_hong_gt&&($obj$$.details.filter($r$$=>$r$$.tien_hoa_hong_gt).forEach($r$$=>{$r$$.tk_no_hoa_hong_gt=$r$$.tk_no_hoa_hong_gt||$obj$$.tk_no_hoa_hong_gt||$kho$$.tk_no_hoa_hong_gt||options.tk_no_hoa_hong_gt||$obj$$.tk_no_hoa_hong||$kho$$.tk_no_hoa_hong||options.tk_no_hoa_hong;$r$$.tk_co_hoa_hong_gt= $r$$.tk_co_hoa_hong_gt||$obj$$.tk_co_hoa_hong_gt||$kho$$.tk_co_hoa_hong_gt||options.tk_co_hoa_hong_gt||$obj$$.tk_co_hoa_hong||$kho$$.tk_co_hoa_hong||options.tk_co_hoa_hong}),(await $obj$$.details.filter($r$$=>$r$$.tien_hoa_hong_gt).asyncGroupBy(["tk_no_hoa_hong_gt","tk_co_hoa_hong_gt"],["tien_hoa_hong_gt"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_no=$r$$.tk_no_hoa_hong_gt;$tt$$.tk_co=$r$$.tk_co_hoa_hong_gt;$tt$$.tien_nt=$r$$.tien_hoa_hong_gt;$tt$$.tien=$tt$$.tien_nt*($obj$$.ty_gia||1);$tt$$.ma_kh=$obj$$.ma_kh_hoa_hong_gt; $tt$$.ma_nv=$obj$$.ma_nv;$tt$$.check_balance=!1;$tt$$.ma_nt="VND";$tt$$.ty_gia=1;$tt$$.tk_no&&$tt$$.tk_co&&$details$$.push($tt$$)}));let $details_tienvon$$=[...($ket_qua_post_sokho$$||[])];$details_tienvon$$.joinModel2($obj$$.id_app,dmvt,{where:"ma_vt",fields:["tk_vt","gia_mua","tk_gv","tg_tk"]},()=>{$details_tienvon$$.forEach(function($d$jscomp$32_detail$$){$d$jscomp$32_detail$$={...$d$jscomp$32_detail$$};$d$jscomp$32_detail$$.tk_no=$option$$.tk_gv;$d$jscomp$32_detail$$.ma_kh=$obj$$.ma_kh;$d$jscomp$32_detail$$.ngay_ct= $obj$$.ngay_giao_hang||$obj$$.ngay_ct;$d$jscomp$32_detail$$.tk_co=$d$jscomp$32_detail$$.tk_vt||"1561";$d$jscomp$32_detail$$.tien_nt=$d$jscomp$32_detail$$.tien_xuat_nt;$d$jscomp$32_detail$$.tien=$d$jscomp$32_detail$$.tien_xuat;$d$jscomp$32_detail$$.tg_tk||($d$jscomp$32_detail$$.tien=$d$jscomp$32_detail$$.tien_nt=0);$d$jscomp$32_detail$$.ma_nt="VND";$d$jscomp$32_detail$$.ty_gia=1;$details$$.push($d$jscomp$32_detail$$)});(new PostSocai($obj$$,$details$$,null,Socai,$_options$$)).run(function($e$$,$rs$$){$callback$$($e$$, $rs$$)})})})},thanhtoanhoahongnv:$callback$$=>{if($obj$$.da_chuyen_hoa_hong||!$obj$$.tien_hoa_hong||!$obj$$.user_nv||!$obj$$.chuyen_ngay)return $callback$$();setImmediate(async()=>{try{if($appInfo$$.wallet_address&&$appInfo$$.wallet_token&&$appInfo$$.tang_gioi_thieu_denom){let $wallet_nhan_vien$$=await global.getModel("asskey").findOne({id_app:$appInfo$$._id.toString(),username:$obj$$.user_nv,wallet_type:8});$wallet_nhan_vien$$||=await global.getModel("asskey").createWallet($appInfo$$._id.toString(), $obj$$.user_nv,"T\u00e0i kho\u1ea3n nh\u1eadn th\u01b0\u1edfng",8);let $amount$$=$obj$$.tien_hoa_hong,$denom$$=$appInfo$$.tang_gioi_thieu_denom,$memo$$=`Hoa h\u1ed3ng cho \u0111\u01a1n h\u00e0ng s\u1ed1 ${$obj$$.so_ct}: ${numeral($amount$$).format()} ${$denom$$.toUpperCase()}`;await spend($appInfo$$,$obj$$.be_introduced,$wallet_nhan_vien$$.address,$amount$$,$denom$$,$memo$$,"","2");$obj$$.da_chuyen_hoa_hong=!0;await model.updateOne({_id:$obj$$._id},{da_chuyen_hoa_hong:!0})}}catch($e$$){Logger.error("Kh\u00f4ng th\u1ec3 thanh to\u00e1n ti\u1ec1n hoa h\u1ed3ng", $e$$)}$callback$$()})},thanhtoanhoahonggt:$callback$$=>{if($obj$$.da_chuyen_hoa_hong_gt||!$obj$$.tien_hoa_hong_gt||!$obj$$.user_gt||!$obj$$.chuyen_ngay_gt)return $callback$$();setImmediate(async()=>{try{if($appInfo$$.wallet_address&&$appInfo$$.wallet_token&&$appInfo$$.tang_gioi_thieu_denom){let $wallet_gt$$=await global.getModel("asskey").findOne({id_app:$appInfo$$._id.toString(),username:$obj$$.user_gt,wallet_type:8});$wallet_gt$$||=await global.getModel("asskey").createWallet($appInfo$$._id.toString(), $obj$$.user_gt,"T\u00e0i kho\u1ea3n nh\u1eadn th\u01b0\u1edfng",8);let $amount$$=$obj$$.tien_hoa_hong_gt,$denom$$=$appInfo$$.tang_gioi_thieu_denom,$memo$$=`Hoa h\u1ed3ng cho \u0111\u01a1n h\u00e0ng s\u1ed1 ${$obj$$.so_ct}: ${numeral($amount$$).format()} ${$denom$$.toUpperCase()}`;await spend($appInfo$$,$obj$$.be_introduced,$wallet_gt$$.address,$amount$$,$denom$$,$memo$$,$obj$$.introduce_code,"2");$obj$$.da_chuyen_hoa_hong_gt=!0;await model.updateOne({_id:$obj$$._id},{da_chuyen_hoa_hong_gt:!0})}}catch($e$$){Logger.error("Kh\u00f4ng th\u1ec3 thanh to\u00e1n ti\u1ec1n hoa h\u1ed3ng", $e$$)}$callback$$()})},thanhtoanhoahongctv:$callback$$=>{if($obj$$.da_chuyen_hoa_hong_ctv||!$obj$$.thu_nhap_ctv||!$obj$$.user_ctv||!$obj$$.chuyen_ngay_ctv)return $callback$$();setImmediate(async()=>{try{if($appInfo$$.wallet_address&&$appInfo$$.wallet_token&&$appInfo$$.tang_gioi_thieu_denom){let $wallet_gt$$=await global.getModel("asskey").findOne({id_app:$appInfo$$._id.toString(),username:$obj$$.user_ctv,wallet_type:8});$wallet_gt$$||=await global.getModel("asskey").createWallet($appInfo$$._id.toString(), $obj$$.user_ctv,"T\u00e0i kho\u1ea3n nh\u1eadn th\u01b0\u1edfng",8);let $amount$$=$obj$$.thu_nhap_ctv,$denom$$=$appInfo$$.tang_gioi_thieu_denom,$memo$$=`Hoa h\u1ed3ng cho \u0111\u01a1n h\u00e0ng s\u1ed1 ${$obj$$.so_ct}: ${numeral($amount$$).format()} ${$denom$$.toUpperCase()}`;await spend($appInfo$$,$obj$$.be_introduced,$wallet_gt$$.address,$amount$$,$denom$$,$memo$$,$obj$$.introduce_code,"2");$obj$$.da_chuyen_hoa_hong_ctv=!0;await model.updateOne({_id:$obj$$._id},{da_chuyen_hoa_hong_ctv:!0})}}catch($e$$){Logger.error("Kh\u00f4ng th\u1ec3 thanh to\u00e1n ti\u1ec1n hoa h\u1ed3ng", $e$$)}$callback$$()})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};$contr_router$$.creating=async function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);if(!$obj$$.ma_kh){var $afflink_kh$$=await customer.findOne({id_app:$obj$$.id_app||$user$$.current_id_app,of_user:$user$$.email},{ma_kh:-1});$afflink_kh$$&&($obj$$.ma_kh=$afflink_kh$$.ma_kh)}$obj$$.id_afflink&&global.mongoose.Types.ObjectId.isValid($obj$$.id_afflink)&&($afflink_kh$$=await global.getModel("afflink").findOne({_id:$obj$$.id_afflink}))&& ($obj$$.user_afflink=$afflink_kh$$.of_user||$afflink_kh$$.user_created);delete $obj$$.virtual_payment_client_url;$obj$$.da_chuyen_hoa_hong=!1;$obj$$.da_chuyen_hoa_hong_gt=!1;await $obj$$.details.asyncJoinModel2($user$$.current_id_app,dmvt,{where:"ma_vt",setFields:($item$$,$ref$$)=>{$item$$.combo=$ref$$.combo||[];$item$$.ma_thue=$ref$$.ma_thue;$item$$.gia_mua=$ref$$.gia_mua_tmp}});let $ngay_nay_thang_truoc$$=moment($obj$$.ngay_ct).subtract(1,"months").toDate(),$details$$=[];$obj$$.details.forEach($d$$=> {$d$$.combo&&$d$$.combo.length>0?$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,gia:{$gt:0}}),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,gia:{$gt:0}}),fields:[{gia_mua:"gia"}]});await $details$$.filter($r$$=>!$r$$.ma_kho).asyncJoinModel2($obj$$.id_app,"dmvt",{where:"ma_vt",fields:{ma_kho:"ma_kho_df"}});$obj$$.details.forEach($detail$$=>{$detail$$.combo&&$detail$$.combo.length>0&&($detail$$.combo=$detail$$.combo.map($item$$=>{$item$$=$item$$.toObject?$item$$.toObject():{...$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=$item$$.sl_xuat*($item$$.gia_ban_nt||0);$item$$.tien_ck=$item$$.tien_ck_nt=$item$$.sl_xuat*($item$$.tien_ck_nt||0);$item$$.tien=$item$$.tien_nt=$item$$.tien_hang_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 $error$$=>{if($error$$)return $next$$($error$$);if(!$obj$$.paymentByPoints)return $next$$(null,$obj$$);try{if(await getPointBalance($user$$.current_id_app,$obj$$.of_user||$user$$.email)<$obj$$.paymentByPoints)return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 \u0111\u1ee7 \u0111i\u1ec3m \u0111\u1ec3 th\u1ef1c hi\u1ec7n giao d\u1ecbch n\u00e0y.");$next$$(null,$obj$$)}catch($e$$){$next$$($e$$.message)}})};$contr_router$$.created=async function($user$$,$obj$$, $next$$){if($obj$$.pt_thanh_toan&&global.mongoose.Types.ObjectId.isValid($obj$$.pt_thanh_toan))try{const $pt_thanh_toan$$=await global.getModel("ptthanhtoan").findOne({_id:$obj$$.pt_thanh_toan});if($pt_thanh_toan$$.payment_service){let $paymentServiceModel$$=global.getModel($pt_thanh_toan$$.payment_service);$obj$$.amount=$obj$$.t_tt_nt-($obj$$.tien_paymentByPoints||0);if($obj$$.amount&&$paymentServiceModel$$.createVirtualPaymentClientURL){let $virtual_payment_client_url$$=await $paymentServiceModel$$.createVirtualPaymentClientURL($obj$$, $user$$.clientIp,$pt_thanh_toan$$);$obj$$.virtual_payment_client_url=$virtual_payment_client_url$$;$obj$$.payment_service=$pt_thanh_toan$$.payment_service;$obj$$.payment_id=((await $paymentServiceModel$$.findOne({$or:[{id_ct:$obj$$._id.toString()},{request_id:$obj$$._id.toString()},{order_id:$obj$$._id.toString()}]})||{})._id||"").toString()}else $obj$$.virtual_payment_client_url="",$obj$$.payment_service="",$obj$$.payment_id=""}else $obj$$.virtual_payment_client_url=""}catch($e$$){return Logger.error($e$$), $next$$("Kh\u00f4ng t\u1ea1o li\u00ean k\u1ebft thanh to\u00e1n:"+($e$$.message||$e$$.error||$e$$))}else Logger.info("Chua nhap phuong thuc thanh toan"