UNPKG

flexbiz-server

Version:

Flexible Server

10 lines (9 loc) 3.79 kB
const model=global.getModel("bds_nhadat_moigioi_ban"),controller=require("../../controllers/controller"); module.exports=function($router$$){const $tinhhoahong$$=async($nd_user$$,$obj$$,$action_ty_le_hh$$="create")=>{$nd_user$$=await global.getModel("nhadat").findOne({id_app:$obj$$.id_app,ma_nd:$obj$$.ma_nd}).lean();if(!$nd_user$$)throw"BDS kh\u00f4ng t\u1ed3n t\u1ea1i";if($action_ty_le_hh$$=="create"&&$nd_user$$.trang_thai!="5")throw"BDS n\u00e0y kh\u00f4ng s\u1eb5n s\u00e0ng \u0111\u1ec3 b\u00e1n";let $duan$$;$nd_user$$.ma_dt&&($duan$$=await global.getModel("dmdt").findOne({id_app:$nd_user$$.id_app, ma_dt:$nd_user$$.ma_dt}).lean());$obj$$.ty_le_hh=0;$obj$$.hoa_hong=0;$obj$$.ty_le_hh2=0;$obj$$.hoa_hong2=0;$obj$$.tong_chiet_khau_khuyen_mai=($duan$$?.chiet_khau_khuyen_mai||[]).filter($c$$=>!$c$$.pt_thanh_toan||$c$$.pt_thanh_toan==$obj$$.pt_thanh_toan).map($c$$=>Number($c$$.gia_tri||0)).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$obj$$.gia_ban_thuc=Math.max(0,($obj$$.gia_ban_de_nghi||0)-$obj$$.tong_chiet_khau_khuyen_mai);$action_ty_le_hh$$=$nd_user$$.ty_le_hh||await global.getModel("nhadat").tinhtylehh($obj$$.id_app, $obj$$.user_created,$duan$$?.chinh_sach_hoa_hong,"1")||0;let $ty_le_hh2$$=0;var $intro_kh_user_gt_kh$$=await global.getModel("introducer").findOne({id_app:$obj$$.id_app,be_introduced:$obj$$.user_created});$intro_kh_user_gt_kh$$&&($intro_kh_user_gt_kh$$=await global.getModel("participant").findOne({id_app:$obj$$.id_app,email:$intro_kh_user_gt_kh$$.be_introduced_by}).lean())&&($obj$$.user_ref=$intro_kh_user_gt_kh$$.email,$ty_le_hh2$$=$nd_user$$.ty_le_hh2||await global.getModel("nhadat").tinhtylehh($obj$$.id_app, $intro_kh_user_gt_kh$$.email,$duan$$?.chinh_sach_hoa_hong,"2")||0);$obj$$.ty_le_hh=$action_ty_le_hh$$;$obj$$.hoa_hong=Math.round($obj$$.gia_ban_thuc*$obj$$.ty_le_hh/100);$obj$$.ty_le_hh2=$ty_le_hh2$$;$obj$$.hoa_hong2=Math.round($obj$$.gia_ban_thuc*$obj$$.ty_le_hh2/100)};(new controller($router$$,model,"nhadat_moigioi_ban",{sort:{stt:1},onView:async function($user$$,$items$$,$next$$){await $items$$.asyncJoinModel2($user$$.current_id_app,"nhadat",{where:"ma_nd",fields:"ten_nd so_to so_thua dien_tich chieu_dai chieu_ngang".split(" ")}); $next$$(null,$items$$)},onCreating:async($user$$,$obj$$,$next$$)=>{try{$obj$$.user_created=$user$$.email;await $tinhhoahong$$($user$$,$obj$$,"create");await global.getModel("nhadat").updateOne({id_app:$obj$$.id_app,ma_nd:$obj$$.ma_nd},{trang_thai:"5.1"});if(!$obj$$.ma_kh&&$obj$$.dien_thoai&&$obj$$.ten_nguoi_mua)try{let $cust$$=await global.getModel("customer").asyncCreateCustomer({id_app:$obj$$.id_app,ma_kh:`${$obj$$.dien_thoai}_${$user$$.email}`,ten_kh:$obj$$.ten_nguoi_mua,dia_chi:$obj$$.dia_chi, dien_thoai:$obj$$.dien_thoai,cccd:$obj$$.so_cccd,trang_thai:"1",phu_trach:$user$$.email,isbuyer:!0,user_created:$user$$.email});$obj$$.ma_kh=$cust$$?.ma_kh}catch($e$$){Logger.error("[bds_nhadat_moigioi_ban][onCreated] l\u1ed7i t\u1ea1o kh\u00e1ch h\u00e0ng m\u1edbi")}$next$$(null,$obj$$)}catch($e$$){$next$$($e$$)}},onCreated:async($user$$,$obj$$,$next$$)=>{$next$$(null,$obj$$)},onUpdating:async($user$$,$data$$,$obj$$,$next$$)=>{try{$data$$.ma_nd=$data$$.ma_nd||$obj$$.ma_nd,$data$$.user_created=$obj$$.user_created, $data$$.pt_thanh_toan=$obj$$.pt_thanh_toan,$data$$.gia_ban_de_nghi=$data$$.gia_ban_de_nghi||$obj$$.gia_ban_de_nghi||0,await $tinhhoahong$$($user$$,$data$$,"update"),$data$$.trang_thai=="9"&&$obj$$.trang_thai!="9"&&await global.getModel("nhadat").updateOne({id_app:$obj$$.id_app,ma_nd:$data$$.ma_nd},{trang_thai:"5"}),$next$$(null,$data$$,$obj$$)}catch($e$$){$next$$($e$$)}},onDeleting:async($user$$,$obj$$,$next$$)=>{try{await global.getModel("nhadat").updateOne({id_app:$obj$$.id_app,ma_nd:$obj$$.ma_nd}, {trang_thai:"5"}),$next$$(null,$obj$$)}catch($e$$){$next$$($e$$)}}})).route()};