修复:经销商分佣 佣金和平级奖计算错误

This commit is contained in:
wuhui_zzw 2024-04-24 16:47:21 +08:00
parent a013f3c2e0
commit d74b6aee93
2 changed files with 75 additions and 17 deletions

View File

@ -120,7 +120,7 @@ class TeamDividendController extends BaseController
public function test(){
// date_default_timezone_set("PRC");
// //订单model
// $model = Order::find(81);
// $model = Order::find(80);
// (new OrderCreatedListener())->handle($model);
//
//
@ -129,7 +129,7 @@ class TeamDividendController extends BaseController
// (new \Yunshop\TeamDividend\services\TimedTaskService)->handle();
debug("结束");
debug("结束。。。");

View File

@ -63,6 +63,10 @@ class TeamReturnService
private $team_levels;
private $parentId;
private $parentUpId;
private $monthLvDiffMaxMoney = 0;// 月月返 经销商奖励 级差处理,已分配最大金额
private $upAgent; // 上一个处理的经销商信息
// public function __construct($order, $set, $buyMember, $order_goods, $agents)
// {
// $this->order = $order;
@ -113,7 +117,7 @@ class TeamReturnService
private function running()
{
foreach ($this->agents as $agent) {
\Log::debug(' ***************'.$agent['uid']." 开始处理 ***************");
$this->amount_service = new GetAmountService($agent, $this->order, $this->order_goods, $this->set, $this->team_goods, $this->finish_ratio, $this->finish_price);
//商品分红限制 不计入平级奖层级 不算感恩奖
@ -136,6 +140,8 @@ class TeamReturnService
$this->hierarchyAward($agent);
// 额外分红
$this->extraDividend($agent);
$this->upAgent = $agent;
}
}
@ -208,13 +214,16 @@ class TeamReturnService
// 经销商奖励
private function dividendAward($agent)
{
\Log::debug(' 经销商奖励 - 开始处理');
//平级跳出
if ($this->dividend_level_ids[$agent['level']]) {
\Log::debug("平级跳出 经销商奖励处理");
return;
}
// 平级奖处理初始化
$this->hierarchyHandle($agent);
// $this->dividend_level_ids[$agent['level']]['hierarchy'] = 0;
// $this->hierarchyHandle($agent);
$this->dividend_level_ids[$agent['level']]['hierarchy'] = 0;
$this->hierarchy_uid = $agent['uid'];
// 分红比例
@ -222,7 +231,7 @@ class TeamReturnService
// 分红金额 判断:是否开启分期
if($this->team_goods->is_stages == 1){
// 判断:仅直推上级获取
if($agent['uid'] != $this->parentId ) return;
// if($agent['uid'] != $this->parentId ) return;
// 佣金处理
$levelName = 'level_'.$agent['level'];
$stages_num = unserialize($this->team_goods->stages_num);//分期数量
@ -237,6 +246,8 @@ class TeamReturnService
$expectThawTime = time();// 每个商品的预计解冻时间 = 当前时间
$expectThawTimeMonth = 0;
$lvDiffReduceMoney = $this->monthLvDiffMaxMoney;// 级差减少金额
foreach(array_values($moneyList) as $monthIndex => $monthInfo){
// 预计解冻时间 增加
$currentExpectThawTime = getNextMonthDays($expectThawTime, $expectThawTimeMonth);
@ -245,6 +256,20 @@ class TeamReturnService
if($currentStagesType == 1) $dividend_amount = (float)sprintf("%.2f",$monthInfo * $this->order_goods->total);// 固定金额
else $dividend_amount = $this->amount_service->getDividendAmount($monthInfo);// 百分比
if($dividend_amount <= 0) continue;
// 月月返 级差处理
if($monthIndex > 0) {
$this->monthLvDiffMaxMoney = $dividend_amount;
$dividend_amount = sprintf("%.2f", $dividend_amount - $lvDiffReduceMoney);
}
// todo 调试中...
if($monthIndex == 0 || $monthIndex == 1) {
\Log::debug("经销商奖励 {$agent['uid']} 金额:{$dividend_amount}");
}
// 计算基数
$dividendData = [
'dividend_amount' => $dividend_amount,
@ -314,11 +339,13 @@ class TeamReturnService
// 是否开启平级奖
if (!$this->set['is_flat_prize']) {
\Log::debug("未开启平级奖");
return;
}
// 商品是否开启平级奖
if (!$this->amount_service->isHierarchy()) {
\Log::debug("商品未开启平级奖");
return;
}
@ -331,30 +358,46 @@ class TeamReturnService
// 经销商奖励层级 经销商等级奖励层级 >= 当前奖励层级
$dividend_level = $this->dividend_level_ids[$agent['level']];
if ($dividend_level == null) {
\Log::debug(" 经销商奖励层级 不存在");
return;
}
// 平级奖奖励层级
$award_hierarchy = $this->amount_service->getAwardHierarchy();
// 平级奖信息存在 并且 (平级奖奖励层级 小于等于 当前层级 或者 平级奖用户和代理用户为同一个人 不参加平级奖
\Log::debug("平级奖励层级:{$award_hierarchy};当前层级:{$dividend_level['hierarchy']}");
if ($dividend_level && ($award_hierarchy <= $dividend_level['hierarchy'] || $this->hierarchy_uid == $agent['uid'])) {
\Log::debug("平级奖奖励层级小于等于当前层级或者平级奖用户和代理用户为同一个人-不参加平级奖");
return;
}
// 平级奖奖励比例
$award_ratio = $this->amount_service->getAwardHierarchyRatio();
if (!isset($award_ratio) || $award_ratio <= 0) return;
if (!isset($award_ratio) || $award_ratio <= 0) {
\Log::debug("平级奖奖励比例 不存在");
return;
}
// 分红金额 判断:是否开启分期
if($this->team_goods->is_stages == 1){
// 仅购买者上级的上级可得
if($agent['uid'] != $this->parentUpId) return;
// if($agent['uid'] != $this->parentUpId) {
// \Log::debug("仅购买者上级的上级可得 不符合条件");
// return;
// }
// 佣金处理
\Log::debug("上一个经销商用户ID {$this->upAgent['uid']}");
$amountList = TeamDividendModel::where('order_sn',$this->order->order_sn)
->select(['amount','status','expect_thaw_time','reality_thaw_time','month'])
->whereIn('type',[7,8])
->get()->toArray();
->where('member_id',$this->upAgent['uid'])
->get()
->toArray();
foreach($amountList as $amountInfo){
\Log::debug("列表内容: ",$amountList);
foreach($amountList as $amountIndex => $amountInfo){
// 计算基数
$dividendData = [
'dividend_amount' => (float)sprintf("%.2f",$amountInfo['amount'] * $award_ratio / 100),
@ -369,6 +412,10 @@ class TeamReturnService
'month' => $amountInfo['month']
];
if($amountIndex == 0 || $amountIndex == 1) {
\Log::debug("经销平级奖奖励 {$agent['uid']} 金额:{$dividendData['dividend_amount']}");
}
// 分红
$dividend_model = $this->addDividend($agent, $dividendData);
}
@ -454,21 +501,25 @@ class TeamReturnService
}
// 平级奖层数增加
$this->hierarchyHandle($agent);
// $this->dividend_level_ids[$agent['level']]['hierarchy'] += 1;
// $this->hierarchyHandle($agent);
$this->dividend_level_ids[$agent['level']]['hierarchy'] += 1;
}
private function hierarchyHandle($agent){
// 平级奖层数增加 修改:等级权重大于等于当前经销商用户 都拿平级奖处理 2024-3-15
foreach($this->team_levels as $levelInfo){
if($agent['has_one_level']['level_weight'] <= $levelInfo['level_weight']){
if($this->dividend_level_ids[$levelInfo['id']]['hierarchy'] != ''){
$levelInfoHierarchy = $this->dividend_level_ids[$levelInfo['id']];
if(is_array($levelInfoHierarchy)){
$this->dividend_level_ids[$levelInfo['id']]['hierarchy'] += 1;
}else{
$this->dividend_level_ids[$levelInfo['id']]['hierarchy'] = 0;
}
}
}
}
@ -600,10 +651,17 @@ class TeamReturnService
$dividendData['is_pay'] = 1;
}
// 插入
// if($dividendData['order_sn'] != 'SN2404231906E1F45C') {
// if($agent['uid'] != 216) {
if ($model = TeamDividendModel::create($dividendData)) {
// 统计
OrderCreatedService::setAareaDividend($dividendData, $agent);
}
// }
return $model;
}