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