diff --git a/app/Services/AfterSaleService.php b/app/Services/AfterSaleService.php index db0c2682..6eadc97a 100644 --- a/app/Services/AfterSaleService.php +++ b/app/Services/AfterSaleService.php @@ -7,6 +7,7 @@ use App\Exceptions\BizException; use App\Helpers\Order as OrderHelper; use App\Models\AfterSale; use App\Models\AfterSaleLog; +use App\Models\DistributionPreIncomeJob; use App\Models\Order; use App\Models\OrderProduct; use App\Models\User; @@ -363,6 +364,7 @@ class AfterSaleService OrderProduct::create([ 'user_id' => $changeOrder->user_id, 'order_id' => $changeOrder->id, + 'gift_for_sku_id' => $afterSale->orderProduct->gift_for_sku_id, 'spu_id' => $afterSale->orderProduct->spu_id, 'sku_id' => $afterSale->orderProduct->sku_id, 'category_id' => $afterSale->orderProduct->category_id, @@ -372,6 +374,7 @@ class AfterSaleService 'weight' => $afterSale->orderProduct->weight, 'sell_price' => $afterSale->orderProduct->sell_price, 'vip_price' => $afterSale->orderProduct->vip_price, + 'sales_value' => $afterSale->orderProduct->sales_value, 'quantity' => $afterSale->num, 'coupon_discount_amount'=> 0, 'vip_discount_amount' => $afterSale->orderProduct->sell_price - $afterSale->orderProduct->vip_price, @@ -386,6 +389,18 @@ class AfterSaleService ]); } + if (in_array($afterSale->type, [ + AfterSale::TYPE_REFUND_AND_RETURN, + AfterSale::TYPE_REFUND, + AfterSale::TYPE_CHANGE, + ])) { + DistributionPreIncomeJob::create([ + 'jobable_id' => $afterSale->id, + 'jobable_type' => $afterSale->getMorphClass(), + 'remarks' => $afterSale->isChange() ? '订单换货' : '支付退货', + ]); + } + AfterSaleLog::create([ 'after_sale_id' => $afterSale->id, 'name' => '财务审核', diff --git a/app/Services/DistributionPreIncomeJobService.php b/app/Services/DistributionPreIncomeJobService.php index 3861b7a4..ae61d58e 100644 --- a/app/Services/DistributionPreIncomeJobService.php +++ b/app/Services/DistributionPreIncomeJobService.php @@ -245,12 +245,7 @@ class DistributionPreIncomeJobService $preIncomeLogs = []; - /* - |----------------------------------------------- - | 推粉丝赚差价 - |----------------------------------------------- - */ - + // 推粉丝赚差价 // 1. 总差价必须大于0 // 2. 下单用户必须有邀请人 // 3. 下单用户的邀请人必须可以享受分红 @@ -261,7 +256,7 @@ class DistributionPreIncomeJobService ) { // 手续费率 $feeRate = $config['price_diff_fee_rate'] ?? '0'; - // 奖金比例 + // 奖励比例 $bonusRate = '1'; // 总收益 $totalRevenue = bcmul($totalDiffPrice, $bonusRate); @@ -294,48 +289,39 @@ class DistributionPreIncomeJobService ]; } - /* - |----------------------------------------------- - | 代理奖励 - |----------------------------------------------- - */ + // 如果总销售值大于0,需计算各级代理的奖励 + if ($totalSalesValue > 0) { + // 可获取奖励 + $agents = $this->getAgentsByUser($user); - // 如果总成长值小于或等于0,则直接不计算代理奖励 - if ($totalSalesValue <= 0) { - return; - } + $lastAgent = null; + // 已分配的级差奖励比例 + $assignedLvlDiffBonusRate = 0; - // 可获取奖励 - $agents = $this->getAgentsByUser($user); + foreach ($agents as $agent) { + // 如果当前代理可以享受奖励 + if ($agent->bonusable) { + $rule = Arr::get($config, "rules.{$agent->agent_level_key}"); - $lastAgent = null; - // 已分配的级差奖金比例 - $assignedLvlDiffBonusRate = 0; + if ($lastAgent && $agent->agent_level === $lastAgent->agent_level) { + /* + |----------------------------------------------- + | 平级奖励 + |----------------------------------------------- + */ - foreach ($agents as $agent) { - // 如果当前代理可以享受奖励 - if ($agent->bonusable) { - $rule = Arr::get($config, "rules.{$agent->agent_level_key}"); + $bonusRate = $rule['lvl_same_bonus_rate'] ?? '0'; - if ($lastAgent && $agent->agent_level === $lastAgent->agent_level) { - /* - |----------------------------------------------- - | 平级奖励 - |----------------------------------------------- - */ + if (bccomp($bonusRate, '0') === 1) { + // 手续费率 + $feeRate = $config['lvl_same_bonus_fee_rate'] ?? '0'; - $bonusRate = $rule['lvl_same_bonus_rate'] ?? '0'; + // 总收益 + $totalRevenue = bcmul($totalSalesValue, $bonusRate); + // 扣除手续费 + $totalRevenue = bcmul($totalRevenue, bcsub('1', $feeRate)); - if (bccomp($bonusRate, '0') === 1) { - // 手续费率 - $feeRate = $config['lvl_same_bonus_fee_rate'] ?? '0'; - - // 总收益 - $totalRevenue = bcmul($totalSalesValue, $bonusRate); - // 扣除手续费 - $totalRevenue = bcmul($totalRevenue, bcsub('1', $feeRate)); - - $preIncome = $agent->distributionPreIncomes()->create([ + $preIncome = $agent->distributionPreIncomes()->create([ 'order_id' => $order->id, 'type' => DistributionPreIncome::TYPE_LEVEL_SAME, 'agent_level' => $agent->agent_level, @@ -351,7 +337,7 @@ class DistributionPreIncomeJobService 'remarks' => "平级奖励-订单号: {$order->sn}", ]); - $preIncomeLogs[] = [ + $preIncomeLogs[] = [ 'pre_income_id' => $preIncome->id, 'pre_income_job_id' => $job->id, 'change_amount' => $preIncome->total_amount, @@ -361,34 +347,34 @@ class DistributionPreIncomeJobService 'created_at' => $preIncome->created_at, 'updated_at' => $preIncome->created_at, ]; - } - } else { - /* - |----------------------------------------------- - | 级差奖励 - |----------------------------------------------- - */ - - $lvlDiffBonusRate = $rule['lvl_diff_bonus_rate'] ?? '0'; - - if (bccomp($lvlDiffBonusRate, '0') === 1) { - // 可得级差奖励比例 = 当前等级的级差奖励 - 已分配的级差奖励比例 - $bonusRate = bcsub($lvlDiffBonusRate, $assignedLvlDiffBonusRate); - - // 如果可得级差奖励比例小于或等于0,则停止分润 - if (bccomp($bonusRate, '0') <= 0) { - break; } + } else { + /* + |----------------------------------------------- + | 级差奖励 + |----------------------------------------------- + */ - // 手续费率 - $feeRate = $config['lvl_diff_bonus_fee_rate'] ?? '0'; + $lvlDiffBonusRate = $rule['lvl_diff_bonus_rate'] ?? '0'; - // 总收益 - $totalRevenue = bcmul($totalSalesValue, $bonusRate); - // 扣除手续费 - $totalRevenue = bcmul($totalRevenue, bcsub('1', $feeRate)); + if (bccomp($lvlDiffBonusRate, '0') === 1) { + // 可得级差奖励比例 = 当前等级的级差奖励 - 已分配的级差奖励比例 + $bonusRate = bcsub($lvlDiffBonusRate, $assignedLvlDiffBonusRate); - $preIncome = $agent->distributionPreIncomes()->create([ + // 如果可得级差奖励比例小于或等于0,则停止分润 + if (bccomp($bonusRate, '0') <= 0) { + break; + } + + // 手续费率 + $feeRate = $config['lvl_diff_bonus_fee_rate'] ?? '0'; + + // 总收益 + $totalRevenue = bcmul($totalSalesValue, $bonusRate); + // 扣除手续费 + $totalRevenue = bcmul($totalRevenue, bcsub('1', $feeRate)); + + $preIncome = $agent->distributionPreIncomes()->create([ 'order_id' => $order->id, 'type' => DistributionPreIncome::TYPE_LEVEL_DIFF, 'agent_level' => $agent->agent_level, @@ -400,13 +386,13 @@ class DistributionPreIncomeJobService 'fee_rate' => bcmul($feeRate, '1', 2), // 实际奖励比例 'bonus_rate' => bcmul($bonusRate, '1', 2), - // 级差奖金比例 + // 级差奖励比例 'lvl_diff_bonus_rate' => bcmul($lvlDiffBonusRate, '1', 2), ], 'remarks' => "级差奖励-订单号: {$order->sn}", ]); - $preIncomeLogs[] = [ + $preIncomeLogs[] = [ 'pre_income_id' => $preIncome->id, 'pre_income_job_id' => $job->id, 'change_amount' => $preIncome->total_amount, @@ -418,12 +404,13 @@ class DistributionPreIncomeJobService ]; - $assignedLvlDiffBonusRate = $lvlDiffBonusRate; + $assignedLvlDiffBonusRate = $lvlDiffBonusRate; + } } } - } - $lastAgent = $agent; + $lastAgent = $agent; + } } DistributionPreIncomeLog::insert($preIncomeLogs); diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 7b5ba929..4f9d2a0a 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -10,6 +10,7 @@ use App\Exceptions\BizException; use App\Exceptions\ShippingNotSupportedException; use App\Helpers\Numeric; use App\Helpers\Order as OrderHelper; +use App\Models\DistributionPreIncomeJob; use App\Models\Order; use App\Models\OrderProduct; use App\Models\ProductGift; @@ -191,6 +192,7 @@ class OrderService 'weight' => $sku->weight, 'sell_price' => $sku->sell_price, 'vip_price' => $sku->vip_price, + 'sales_value' => $sku->sales_value, 'quantity' => $qty, 'remain_quantity' => $qty, // 剩余发货数量 'coupon_discount_amount' => $product['coupon_discount_amount'], @@ -219,6 +221,7 @@ class OrderService 'weight' => $giftSku->weight, 'sell_price' => $giftSku->sell_price, 'vip_price' => $giftSku->vip_price, + 'sales_value' => 0, // 赠品不算销售值 'quantity' => $gift['num'], 'remain_quantity' => $gift['num'], // 剩余发货数量 'coupon_discount_amount' => 0, @@ -753,8 +756,11 @@ class OrderService report($th); } - - // todo 处理预收益 + DistributionPreIncomeJob::create([ + 'jobable_id' => $order->id, + 'jobable_type' => $order->getMorphClass(), + 'remarks' => '支付订单', + ]); return $order; } @@ -796,11 +802,17 @@ class OrderService } if ($order->isWaitShipping()) { - $order->refundLogs()->create([ + $refundLog = $order->refundLogs()->create([ 'sn' => OrderHelper::serialNumber(), 'amount' => $order->total_amount, 'reason' => '取消订单', ]); + + DistributionPreIncomeJob::create([ + 'jobable_id' => $refundLog->id, + 'jobable_type' => $refundLog->getMorphClass(), + 'remarks' => '取消订单', + ]); } $products = $order->products()->get();