UNPKG

flexbiz-server

Version:

Flexible Server

16 lines (15 loc) 7.34 kB
const PostSocai=require("../../libs/post-socai"),User=global.getModel("user"),transaction=global.getModel("transaction"),dvcs=global.getModel("dvcs"),account=global.getModel("account"),dmphi=global.getModel("dmphi"),customer=global.getModel("customer"),dmnv=global.getModel("dmnv"),dmdt=global.getModel("dmdt"),contract=global.getModel("contract"),model=global.getModel("pt5"),currency=global.getModel("currency"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"), async=require("async"),numeral=require("numeral"),ma_ct="BC5",valid_detail=function($i_user$$,$obj$$,$fn$$){if(!$obj$$.details)return $fn$$(null,$obj$$);for($i_user$$=0;$i_user$$<$obj$$.details.length;$i_user$$++){const $detail$$=$obj$$.details[$i_user$$];$detail$$.line=$i_user$$;$obj$$.ma_nt=="VND"&&($detail$$.tien=Math.roundBy($detail$$.tien_nt,0))}$fn$$(null,$obj$$)},createTransaction=async function($obj$$){if($obj$$.trang_thai!="5")return await transaction.deleteTrans({id_ct:$obj$$._id.toString()}, "Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m \u0111\u00e3 mua"),null;var $amount_ty_le_qd$$=await currency.getRate($obj$$.id_app,$obj$$.ma_nt);let $t_tien$$=$obj$$.details.map($d$$=>$d$$.tien).reduce(($a$$,$b$$)=>$a$$+$b$$,0);var $diem_khuyen_mai_old_trans$$=$obj$$.details.map($d$$=>$d$$.diem_khuyen_mai||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0);if(!$amount_ty_le_qd$$)return null;$amount_ty_le_qd$$=$t_tien$$/$amount_ty_le_qd$$+$diem_khuyen_mai_old_trans$$;if(($diem_khuyen_mai_old_trans$$=await transaction.findOne({id_ct:$obj$$._id.toString()}))&& $diem_khuyen_mai_old_trans$$.amount===$amount_ty_le_qd$$&&$diem_khuyen_mai_old_trans$$.receiver&&$obj$$.email===$diem_khuyen_mai_old_trans$$.receiver.email)return null;await transaction.deleteTrans({id_ct:$obj$$._id.toString()},"Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m \u0111\u00e3 mua");return await transaction.createTrans({id_app:$obj$$.id_app,sender_email:$obj$$.id_app,receiver_email:$obj$$.email,amount:$amount_ty_le_qd$$,description:$obj$$.dien_giai||`N\u1ed9p ${numeral($t_tien$$).format()} ${$obj$$.ma_nt} qua ng\u00e2n h\u00e0ng v\u00e0o v\u00ed`, otherData:{ma_ct:$obj$$.ma_ct,so_ct:$obj$$.so_ct,ngay_ct:$obj$$.ngay_ct,id_ct:$obj$$._id.toString()}})}; module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,requireRecaptchaToken:!1,sort:{ngay_ct:-1,so_ct:-1},keyValues:{ma_ct},onCreating:($user$$,$obj$$,$next$$)=>{$obj$$.details||($obj$$.details=[]);valid_detail($user$$,$obj$$,async $error$$=>{if($error$$)return $next$$($error$$);try{if($obj$$.ma_kh){let $_customer$$=await customer.getInfo($user$$.current_id_app,{ma_kh:$obj$$.ma_kh});if($_customer$$.of_user)$obj$$.email=$_customer$$.of_user; else return $next$$("Kh\u00e1ch h\u00e0ng n\u00e0y kh\u00f4ng c\u00f3 t\u00e0i kho\u1ea3n truy c\u1eadp h\u1ec7 th\u1ed1ng")}$next$$(null,$obj$$)}catch($e$$){$next$$(`${$e$$.message}, user: ${$obj$$.email}`)}})},onCreated:async($user$$,$obj$$,$next$$)=>{try{await createTransaction($obj$$),$next$$(null,$obj$$)}catch($e$$){$next$$($e$$.message)}},onUpdating:($user$$,$data$$,$obj$$,$next$$)=>{valid_detail($user$$,$data$$,async $error$$=>{if($error$$)return $next$$($error$$);if(!$data$$.email&&!$data$$.ma_kh)return $next$$(null, $data$$,$obj$$);if(!$data$$.email&&$data$$.ma_kh)return $next$$("Can't update customer code for this voucher");try{let $_customer$$=await customer.getInfo($user$$.current_id_app,{of_user:$data$$.email});$data$$.ma_kh=$_customer$$.ma_kh;$next$$(null,$data$$,$obj$$)}catch($e$$){$next$$($e$$.message)}})},onUpdated:async($user$$,$obj$$,$next$$)=>{try{await createTransaction($obj$$),$next$$(null,$obj$$)}catch($e$$){$next$$($e$$.message)}},onDeleted:async($user$$,$obj$$,$next$$)=>{if($obj$$.id_trans)try{await transaction.deleteTrans({id_ct:$obj$$._id.toString()}, "Tr\u1ea3 l\u1ea1i \u0111i\u1ec3m \u0111\u00e3 mua")}catch($e$$){return $next$$($e$$.message)}$next$$(null,$obj$$)}});$contr_router$$.route();$contr_router$$.post=async function($obj$$,$fn$$){$fn$$||=function(){};$obj$$=$obj$$.toObject?$obj$$.toObject():{...$obj$$};let $he_so_qd$$=await currency.getRate($obj$$.id_app,$obj$$.ma_nt)||1;async.series({post_socai:$callback$$=>{const $details_sc$$=[];$obj$$.details.forEach($d$$=>{var $detail$$={...$d$$};$detail$$.tk_no=$obj$$.tk_no;$detail$$.ma_kh||($detail$$.ma_kh= $obj$$.ma_kh);$details_sc$$.push($detail$$);$d$$.diem_khuyen_mai&&$d$$.tk_km_co&&$d$$.tk_km_no&&($detail$$={...$d$$},$detail$$.tk_no=$d$$.tk_km_no,$detail$$.tk_co=$d$$.tk_km_co,$detail$$.tien=$detail$$.tien_nt=$d$$.diem_khuyen_mai*$he_so_qd$$,$detail$$.ma_kh||($detail$$.ma_kh=$obj$$.ma_kh),$details_sc$$.push($detail$$))});(new PostSocai($obj$$,$details_sc$$)).run(($e$$,$rs$$)=>{$callback$$($e$$,$rs$$)})}},($e$$,$rs$$)=>{$fn$$($e$$,$rs$$)})};$contr_router$$.view=function($user$$,$items$$,$fn$$){const $id_app$$= $user$$.current_id_app;async.series({e:function($callback$$){$items$$.filter($item$$=>$item$$.email).joinModel2(null,User,[{where:"email",fields:"name"}],function(){$callback$$()})},kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel2($id_app$$,customer,[{where:"ma_kh",fields:"ten_kh"}],function(){$callback$$()})},nv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_nv).joinModel2($id_app$$,dmnv,[{where:"ma_nv",fields:"ten_nv"}],function(){$callback$$()})},tk:function($callback$$){$items$$.filter($item$$=> $item$$.tk_no).joinModel2($id_app$$,account,[{where:{tk_no:"tk"},fields:{ten_tk_no:"ten_tk"}}],function(){$callback$$()})},dv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dvcs).joinModel2($id_app$$,dvcs,[{where:{ma_dvcs:"_id"},fields:"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$$()})}, t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.t_tien=0;$r$$.t_tien_nt=0;$r$$.details&&($r$$.t_tien=$r$$.details.csum("tien"),$r$$.t_tien_nt=$r$$.details.csum("tien_nt"));$r$$.t_tt=$r$$.t_tien;$r$$.t_tt_nt=$r$$.t_tien_nt});$callback$$()},details_tk:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,account,[{akey:"tk_co",bkey:"tk",fields:[{name:"ten_tk_co",value:"ten_tk"}]}],function(){$callback1$$()})},function(){$callback$$()})}, details_dt:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_dt).joinModel($id_app$$,dmdt,[{akey:"ma_dt",bkey:"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).joinModel($id_app$$,contract,[{akey:"ma_hd",bkey:"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).joinModel($id_app$$,dmphi,[{akey:"ma_phi",bkey:"ma_phi",fields:[{name:"ten_phi",value:"ten_phi"}]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){$fn$$(null,$items$$)})}};