345 lines
17 KiB
PHP
345 lines
17 KiB
PHP
<?php
|
||
/**
|
||
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
||
* =========================================================
|
||
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
|
||
* ----------------------------------------------
|
||
* 官方网址: https://www.gobuysaas.com
|
||
* =========================================================
|
||
*/
|
||
namespace addon\commission\event;
|
||
use addon\commission\job\ComputeIntegralJob;
|
||
use addon\commission\model\Legumes;
|
||
use app\model\NewBaseModel;
|
||
use think\Exception;
|
||
use think\facade\Db;
|
||
use think\facade\Queue;
|
||
|
||
class OrderRefundFinish{
|
||
// 订单支付成功
|
||
public function handle($data){
|
||
Db::startTrans();
|
||
try{
|
||
// trace($data, '平台抽成 - 订单退款');
|
||
|
||
if($data['refund_status_action']){
|
||
// 判断:当前商品是否存在豆豆积分抵扣情况
|
||
if((float)$data['legumes_integral_use'] <= 0) throw new Exception('商品不存在豆豆积分抵扣');
|
||
//计算退款比例 单位:%
|
||
$refundRate = (float)sprintf("%.2f",(float)$data['refund_real_money'] / (float)$data['real_goods_money'] * 100);
|
||
// 退款成功 计算平台抽成相关退款内容
|
||
$this->handleCommissionRecord($data['order_goods_id'], $refundRate);
|
||
// 退款成功 权重值相关处理
|
||
$this->handleWeightValue($data['order_goods_id'], $refundRate);
|
||
// 招商员佣金 & 推广员佣金处理
|
||
$this->HandleCommissionMoney($data['order_goods_id'], $refundRate);
|
||
// 合伙人佣金处理
|
||
$this->HandlePartnerMoney($data['order_goods_id'], $refundRate);
|
||
// 积分和豆豆处理
|
||
$this->HandleLegumes($data['order_goods_id'], $refundRate);
|
||
// 抵扣积分退回
|
||
$this->HandleIntegral($data['order_goods_id'], $refundRate);
|
||
}
|
||
|
||
Db::commit();
|
||
}
|
||
catch(Exception $e){
|
||
Db::rollback();
|
||
$error = [
|
||
'order_goods_id' => $data['order_goods_id'],
|
||
'msg' => $e->getMessage()
|
||
];
|
||
trace($error, '平台抽成 - 订单退款 - 错误');
|
||
}
|
||
return success(0,'平台抽成退款操作完成');
|
||
}
|
||
|
||
// 退款处理 - 平台抽成记录处理
|
||
private function handleCommissionRecord($orderGoodsId, $refundRate){
|
||
$hasRefundRatio = (float)model('commission_record')->getValue(['order_goods_id'=>$orderGoodsId],'refund_ratio');
|
||
$updateData = [
|
||
'refund_ratio' => (float)sprintf("%.2f", $hasRefundRatio + $refundRate)
|
||
];
|
||
if($updateData['refund_ratio'] >= 100){
|
||
$updateData['refund_ratio'] = 100;
|
||
$updateData['partner_status'] = 3;
|
||
$updateData['merchants_status'] = 3;
|
||
$updateData['promoter_status'] = 3;
|
||
}
|
||
model('commission_record')->update($updateData,[
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
]);
|
||
return true;
|
||
}
|
||
// 退款处理 - 权重值相关处理
|
||
private function handleWeightValue($orderGoodsId, $refundRate){
|
||
$weightValueLogList = model('commission_weight_value_log')->getList([
|
||
['order_goods_id', '=', $orderGoodsId],
|
||
['change_type', '=', 1],
|
||
['source', '=', 0],
|
||
],'id,member_id,level_id,order_goods_id,change_quantity,goods_id,order_id');
|
||
$insertLogData = [];
|
||
$updateData = [];
|
||
foreach($weightValueLogList as $weightValueLogInfo){
|
||
// 获取用户持有信息
|
||
$holdInfo = model('commission_weight_value')->getInfo([
|
||
['member_id', '=', $weightValueLogInfo['member_id']],
|
||
['level_id', '=', $weightValueLogInfo['level_id']],
|
||
]);
|
||
// 获取已经减少数量
|
||
$reduced = model('commission_weight_value_log')->getSum([
|
||
['order_goods_id', '=', $weightValueLogInfo['order_goods_id']],
|
||
['change_type', '=', 0],
|
||
['source', '=', 1],
|
||
['member_id', '=', $weightValueLogInfo['member_id']],
|
||
], 'change_quantity');
|
||
// 计算减少信息,总减少数量不能超过获取数量
|
||
$reduce = (float)sprintf("%.2f",$weightValueLogInfo['change_quantity'] * $refundRate / 100);
|
||
$totalReduce = (float)sprintf("%.2f",$reduced * $reduce);
|
||
if($totalReduce > $weightValueLogInfo['change_quantity']) $reduce = (float)sprintf("%.2f",$weightValueLogInfo['change_quantity'] - $reduced);
|
||
$changeFront = $holdInfo->quantity;
|
||
$surplusQuantity = (float)sprintf("%.2f",$holdInfo['quantity'] - $reduce);
|
||
$updateData[] = [
|
||
'id' => $holdInfo['id'],
|
||
'quantity' => $surplusQuantity
|
||
];
|
||
// 记录变更记录
|
||
$insertLogData[] = [
|
||
'site_id' => $holdInfo['site_id'],
|
||
'member_id' => $weightValueLogInfo['member_id'],
|
||
'level_id' => $weightValueLogInfo['level_id'],
|
||
'goods_id' => $weightValueLogInfo['goods_id'],
|
||
'order_id' => $weightValueLogInfo['order_id'],
|
||
'order_goods_id' => $weightValueLogInfo['order_goods_id'],
|
||
'change_type' => 0,
|
||
'change_quantity' => $reduce,
|
||
'change_front' => $changeFront,
|
||
'change_after' => (float)$surplusQuantity,
|
||
'remark' => '商品退款,减少权重值',
|
||
'source' => 1,
|
||
];
|
||
}
|
||
if(count($insertLogData) > 0){
|
||
model('commission_weight_value_log')->addList($insertLogData);
|
||
}
|
||
if(count($updateData) > 0){
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_weight_value', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
}
|
||
return true;
|
||
}
|
||
// 退款处理 - 招商员佣金 & 推广员佣金处理
|
||
private function HandleCommissionMoney($orderGoodsId, $refundRate){
|
||
$where = [
|
||
['order_goods_id', '=', $orderGoodsId],
|
||
['merchants_status', '=', 1],
|
||
];
|
||
$field = [
|
||
'a.id',
|
||
'a.site_id',
|
||
'a.store_id',
|
||
'a.order_id',
|
||
'a.order_goods_id',
|
||
'a.merchants_money',
|
||
's.merchants_member_id',
|
||
's.store_name',
|
||
'a.member_id',
|
||
'a.promoter_money',
|
||
'm.is_fenxiao',
|
||
'm.nickname',
|
||
'fx.member_id as fenxiao_member_id',
|
||
'fx.parent'
|
||
];
|
||
$join = [
|
||
['store s', 's.store_id = a.store_id', 'LEFT'], // 关联客服
|
||
['member m', 'a.member_id = m.member_id', 'LEFT'],
|
||
['fenxiao fx', 'm.fenxiao_id = fx.fenxiao_id', 'LEFT'],
|
||
];
|
||
$info = model('commission_record')->getInfo($where,$field,'a',$join);
|
||
if(!$info) return false;
|
||
// 处理招商员佣金
|
||
if((int)$info['merchants_member_id'] > 0){
|
||
// 获取冻结中佣金信息
|
||
$reduce = (float)sprintf("%.3f",$info['merchants_money'] * $refundRate / 100);// 减少数量
|
||
model('commission_account')->add([
|
||
'site_id' => $info['site_id'],
|
||
'member_id' => $info['merchants_member_id'],
|
||
'join_id' => $info['id'],
|
||
'account_type' => 'merchants_refund',
|
||
'account_type_name' => '招商员佣金退款',
|
||
'account_data' => '-' . $reduce,
|
||
'remark' => '订单退款,减少收入',
|
||
'status' => 2,
|
||
]);
|
||
$memberCommissionMoney = model('member')->getValue([['member_id', '=', $info['merchants_member_id']]], 'commission_money');
|
||
model('member')->update(['commission_money' => sprintf("%.2f", $memberCommissionMoney - $reduce)],[
|
||
['member_id', '=', $info['merchants_member_id']],
|
||
]);
|
||
}
|
||
// 处理推广员佣金
|
||
if($info['is_fenxiao'] == 1) $parentMemberId = model('fenxiao')->getValue(['fenxiao_id'=>$info['parent']], 'member_id');// 当前用户是分销商 使用parent查询上级用户id
|
||
else $parentMemberId = $info['fenxiao_member_id'];// 当前用户不是分销商 直接为fenxiao_member_id
|
||
if($parentMemberId > 0){
|
||
$reduce = (float)sprintf("%.3f",$info['promoter_money'] * $refundRate / 100);// 减少数量
|
||
model('commission_account')->add([
|
||
'site_id' => $info['site_id'],
|
||
'member_id' => $parentMemberId,
|
||
'join_id' => $info['id'],
|
||
'account_type' => 'promoter_refund',
|
||
'account_type_name' => '推广员佣金退款',
|
||
'account_data' => '-' . $reduce,
|
||
'remark' => '订单退款,减少收入',
|
||
'status' => 2,
|
||
]);
|
||
$memberCommissionMoney = model('member')->getValue([['member_id', '=', $parentMemberId]], 'commission_money');
|
||
model('member')->update(['commission_money' => sprintf("%.2f", $memberCommissionMoney - $reduce)],[
|
||
['member_id', '=', $parentMemberId],
|
||
]);
|
||
}
|
||
return true;
|
||
}
|
||
// 退款处理 - 合伙人佣金处理
|
||
private function HandlePartnerMoney($orderGoodsId, $refundRate){
|
||
$commissionRecord = model('commission_record')->getInfo([
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
],'partner_status,create_time,partner_money');
|
||
// 待结算-结算时处理,已结算-减少用户已获得佣金,2、3-无处理
|
||
if($commissionRecord['partner_status'] == 1){
|
||
// 获取:获取当前抽成结算的归属周期
|
||
$createTime = strtotime($commissionRecord['create_time']);
|
||
$cycleInfo = model('commission_partner_cycle')->getInfo([
|
||
['start_time', '<', $createTime],
|
||
['end_time', '>', $createTime],
|
||
]);
|
||
if($cycleInfo){
|
||
// 计算总退款的合伙人佣金
|
||
$reduceCommissionMoney = (float)sprintf("%.3f",$commissionRecord['partner_money'] * $refundRate / 100);// 减少数量
|
||
// 获取退款的用户列表
|
||
$userList = model('commission_partner')->getList([
|
||
['cycle_id', '=', $cycleInfo['cycle_id']],
|
||
],'id,site_id,member_id,money,proportion,cycle_id');
|
||
$insertData = [];
|
||
foreach($userList as $partnerInfo){
|
||
// 计算当前用户应扣除合伙人佣金
|
||
$reduce = (float)sprintf("%.2f",$reduceCommissionMoney * $partnerInfo['proportion'] / 100);
|
||
// 记录账单
|
||
$insertData = [
|
||
'site_id' => $partnerInfo['site_id'],
|
||
'member_id' => $partnerInfo['member_id'],
|
||
'join_id' => $partnerInfo['id'],
|
||
'account_type' => 'partner_refund',
|
||
'account_type_name' => '合伙人佣金退款',
|
||
'account_data' => '-' . $reduce,
|
||
'remark' => "订单退款,减少收入",
|
||
'status' => 2,
|
||
];
|
||
$memberCommissionMoney = model('member')->getValue([['member_id', '=', $partnerInfo['member_id']]], 'commission_money');
|
||
model('member')->update(['commission_money' => sprintf("%.2f", $memberCommissionMoney - $reduce)],[
|
||
['member_id', '=', $partnerInfo['member_id']],
|
||
]);
|
||
}
|
||
// 记录账单变化信息
|
||
if (count($insertData) > 0) model('commission_account')->addList($insertData);
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
// 退款处理 - 积分和豆豆处理
|
||
private function HandleLegumes($orderGoodsId, $refundRate){
|
||
// 获取当前抽成信息
|
||
$commissionRecord = model('commission_record')->getInfo([
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
],'id,create_time');
|
||
// 判断:当前抽成豆豆信息是否已经结算,未结算-无操作处理(结算时处理退款内容);已结算-处理退款相关内容
|
||
$maxEndTime = model('commission_legumes')->getMax([],'end_time');
|
||
$createTime = strtotime($commissionRecord['create_time']);
|
||
if($createTime < $maxEndTime){
|
||
// 已结算 - 处理退款相关内容
|
||
$legumesId = model('commission_legumes')->getValue([
|
||
['start_time', '<', $createTime],
|
||
['end_time', '>', $createTime]
|
||
], 'id');
|
||
if($legumesId > 0){
|
||
// 获取需要修改的信息列表
|
||
$correlationList = model('commission_legumes_log')->getList([
|
||
['legumes_id', '=', $legumesId]
|
||
], 'id,member_id,order_money,refund_order_money,get_legumes,refund_get_legumes,get_integral');
|
||
// 循环处理
|
||
$updateData = [];
|
||
foreach($correlationList as $correlationInfo){
|
||
// 计算 退款金额
|
||
$reduceOrderMoney = (float)sprintf("%.2f",$correlationInfo['order_money'] * $refundRate / 100);
|
||
$refundOrderMoney = (float)sprintf("%.3f", $correlationInfo['refund_order_money'] + $reduceOrderMoney);
|
||
if($refundOrderMoney > $correlationInfo['order_money']) $refundOrderMoney = $correlationInfo['order_money'];
|
||
// 计算 退款豆豆
|
||
$reduceGetLegumes = (float)sprintf("%.2f",$correlationInfo['get_legumes'] * $refundRate / 100);
|
||
$refundGetLegumes = (float)sprintf("%.3f", $correlationInfo['refund_get_legumes'] + $reduceGetLegumes);
|
||
if($refundGetLegumes > $correlationInfo['get_legumes']) $refundGetLegumes = $correlationInfo['get_legumes'];
|
||
// 记录修改信息
|
||
$updateData[] = [
|
||
'id' => $correlationInfo['id'],
|
||
'refund_order_money' => $refundOrderMoney,
|
||
'refund_get_legumes' => $refundGetLegumes
|
||
];
|
||
}
|
||
if(count($updateData) > 0) {
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
Queue::push(ComputeIntegralJob::class,[
|
||
'ids' => array_column($correlationList,'id')
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
// 退款处理 - 抵扣积分退回
|
||
private function HandleIntegral($orderGoodsId, $refundRate){
|
||
// 获取订单商品信息
|
||
$orderProductInfo = model('order_goods')->getInfo([
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
],'site_id,member_id,order_id,legumes_integral_use');
|
||
$useLegumesIntegral = $orderProductInfo['legumes_integral_use'] ?? 0;
|
||
$memberId = $orderProductInfo['member_id'] ?? 0;
|
||
if($useLegumesIntegral > 0 && $memberId > 0){
|
||
// 计算退回积分
|
||
$returnIntegral = (float)sprintf("%.2f",$useLegumesIntegral * $refundRate / 100);
|
||
// 获取退回的记录列表
|
||
$useLegumesLogList = (new Legumes())->getUsedList((int)$memberId,(float)$returnIntegral);
|
||
// 循环处理
|
||
$returnComputeIntegral = $returnIntegral;// 计算用 退回积分
|
||
$updateData = [];
|
||
foreach($useLegumesLogList as $logInfo){
|
||
// 当前分配信息可退回积分 剩余可退回积分
|
||
$reduce = $returnComputeIntegral < $logInfo['use_integral'] ? $returnComputeIntegral : $logInfo['use_integral'];
|
||
$returnComputeIntegral = (float)sprintf("%.2f",$returnComputeIntegral - $reduce);
|
||
$updateData[] = [
|
||
'id' => $logInfo['id'],
|
||
'use_integral' => (float)sprintf("%.2f",$logInfo['use_integral'] - $reduce)
|
||
];
|
||
if($returnComputeIntegral <= 0) break;
|
||
}
|
||
if($updateData){
|
||
// 修改
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
// 添加用户账单信息变更记录
|
||
$mark = '订单退款 - 退回抵扣积分:'.$returnIntegral;
|
||
model('commission_account')->add([
|
||
'site_id' => $orderProductInfo['site_id'],
|
||
'member_id' => $memberId,
|
||
'join_id' => $orderProductInfo['order_id'],
|
||
'account_type' => 'refund_legumes_integral',
|
||
'account_type_name' => '积分退回',
|
||
'account_data' => $returnIntegral,
|
||
'status' => 2,
|
||
'remark' => $mark,
|
||
]);
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
}
|