flexbiz-server
Version:
Flexible Server
9 lines (8 loc) • 3.51 kB
JavaScript
const so9=global.getModel("so9"),hd8=global.getModel("hd8"),async=require("async"),trangthai=global.getModel("trangthai"),transaction=global.getModel("transaction");
module.exports=function($condition$$,$callback$$){if(!$condition$$.id_app)return $callback$$("H\u00e0m n\u00e0y y\u00eau c\u1ea7u tham s\u1ed1 id_app");async.parallel({so:$callback$$=>{setImmediate(()=>{if(!$condition$$.ma_ct||$condition$$.ma_ct.indexOf("so9")>=0){let $_cd$$={...$condition$$};delete $_cd$$.show_payment_method;delete $_cd$$.show_bonus_points;so9.find($_cd$$).lean().then($rs$$=>{$rs$$.forEach($r$$=>{$r$$.ngay_ct=$r$$.ngay_giao_hang||$r$$.ngay_ct;$r$$.tien_nt=($r$$.tien_hang_ct_nt||
0)-($r$$.tien_ck_nt||0)+($r$$.tien_thue_nt||0);$r$$.tien=($r$$.tien_hang_ct||0)-($r$$.tien_ck||0)+($r$$.tien_thue||0)});$callback$$(null,$rs$$)}).catch($error$$=>{$callback$$({message:$error$$.message,voucher:"so9",condition:$_cd$$})})}else $callback$$(null,[])})},hd8:$callback$$=>{setImmediate(()=>{if(!$condition$$.ma_ct||$condition$$.ma_ct.indexOf("SO9")>=0){let $_cd$$={...$condition$$};delete $_cd$$.show_payment_method;delete $_cd$$.show_bonus_points;delete $_cd$$.ma_ct;hd8.find($_cd$$).lean().then($rs$$=>
{$callback$$(null,$rs$$)}).catch($error$$=>{$callback$$({message:$error$$.message,voucher:"hd8",condition:$_cd$$})})}else $callback$$(null,[])})}},async($error$$,$rs$$)=>{if($error$$)return $callback$$($error$$);$rs$$.pbl=[];async.series({so:$callback$$=>{setImmediate(()=>{($rs$$.so||[]).forEach($r$$=>{$r$$.ctcpbhs&&$r$$.ctcpbhs.forEach($c$$=>{$c$$.tien_hang_ct=0;$c$$.tien_hang_ct_nt=0;$c$$.sl_xuat=0;$c$$.tien=$c$$.tien_hang_ct+$c$$.tien_cp;$c$$.tien_nt=$c$$.tien_hang_ct_nt+$c$$.tien_cp_nt;$r$$.details.push($c$$)});
$rs$$.pbl.push($r$$)});$callback$$()})},so_tl:$callback$$=>{setImmediate(()=>{$rs$$.hd8.forEach($r$$=>{$r$$.details_tl=$r$$.details.map($r$$=>$r$$);$r$$.details=[];$rs$$.pbl.push($r$$)});$callback$$()})}},async()=>{let $reportData$$=$rs$$.pbl;await $reportData$$.asyncJoinModel2(null,trangthai,[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["post_sokho","post","post_socai","ten_trang_thai"]}]);$reportData$$=$reportData$$.filter($p$$=>$p$$.post);const $ptthanhtoans$$=$condition$$.show_payment_method!==
!1?(await global.getModel("ptthanhtoan").find({status:!0,id_app:$condition$$.id_app}).lean()).map($pt$$=>{$pt$$.ma=`pttt__${$pt$$._id}`;return $pt$$}):[];$condition$$.show_bonus_points||$ptthanhtoans$$&&$ptthanhtoans$$.length>0?async.mapLimit($reportData$$,100,($r$$,$callback$$)=>{setImmediate(async()=>{try{if($condition$$.show_bonus_points){let $trans$$=await transaction.find({id_app:$condition$$.id_app,id_ct:{$regex:$r$$._id.toString(),$options:"i"}}).lean();$r$$.bonus_points=$trans$$.filter($tran$$=>
$tran$$.receiver.email.toLowerCase()===$r$$.ma_kh.toLowerCase()).map($tran$$=>$tran$$.amount).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$r$$.used_points=$trans$$.filter($tran$$=>$tran$$.sender.email.toLowerCase()===$r$$.ma_kh.toLowerCase()).map($tran$$=>$tran$$.amount).reduce(($a$$,$b$$)=>$a$$+$b$$,0)}$ptthanhtoans$$&&$ptthanhtoans$$.length>0&&($r$$.thanh_toan=$ptthanhtoans$$,$r$$.thanh_toan.forEach($pt$$=>{$r$$[$pt$$.ma]=0;$r$$.pt_thanh_toan===$pt$$._id.toString()&&($r$$[$pt$$.ma]+=$r$$.tien_thu||$r$$.tien_con_lai||
0);$r$$.pt_thanh_toan2===$pt$$._id.toString()&&($r$$[$pt$$.ma]+=$r$$.tien_thu2||0);$pt$$.tien_thu=$r$$[$pt$$.ma]}));$callback$$(null,$r$$)}catch($e$$){$callback$$($e$$)}})},()=>{$callback$$(null,$reportData$$,$ptthanhtoans$$)}):$callback$$(null,$reportData$$)})})};