优化分销预收益
parent
9e0bfa86bd
commit
e7adab6c49
|
|
@ -54,6 +54,7 @@ class UserInfo extends Model
|
|||
'quota_v1',
|
||||
'quota_v2',
|
||||
'growth_value',
|
||||
'pre_growth_value',
|
||||
'group_sales_value',
|
||||
];
|
||||
|
||||
|
|
@ -160,6 +161,34 @@ class UserInfo extends Model
|
|||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 变更预收益成长值
|
||||
*
|
||||
* @param float $growthValue
|
||||
* @return void
|
||||
*/
|
||||
public function incrPreGrowthValue($growthValue)
|
||||
{
|
||||
if (bccomp($growthValue, '0', 2) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 增加预收益的成长值
|
||||
$this->increment('pre_growth_value', $growthValue);
|
||||
|
||||
$compared = bccomp(bcadd($this->growth_value, $this->pre_growth_value, 2), '650', 2);
|
||||
|
||||
if ($this->agent_level === 1 && $compared < 0) {
|
||||
$this->update([
|
||||
'agent_level' => 0,
|
||||
]);
|
||||
} elseif ($this->agent_level === 0 && $compared >= 0) {
|
||||
$this->update([
|
||||
'agent_level' => 1,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理等级排名
|
||||
*
|
||||
|
|
|
|||
|
|
@ -359,7 +359,36 @@ class AfterSaleService
|
|||
{
|
||||
if ($this->isWaitFinance($afterSale)) {
|
||||
$order = $afterSale->order;
|
||||
$afterSaleProduct = $afterSale->orderProduct;
|
||||
|
||||
// 售后变更的销售值
|
||||
$salesValue = '0';
|
||||
|
||||
if (in_array($afterSale->type, [AfterSale::TYPE_REFUND_AND_RETURN, AfterSale::TYPE_REFUND])) {
|
||||
if ($afterSaleProduct->total_amount > 0) {
|
||||
// 总销售值
|
||||
$totalSalesValue = bcmul($afterSaleProduct->sales_value, $afterSaleProduct->quantity);
|
||||
// 售后变更金额
|
||||
$amount = $afterSale->amount;
|
||||
|
||||
if ($amount >= $afterSaleProduct->total_amount) {
|
||||
$amount = $afterSaleProduct->total_amount;
|
||||
}
|
||||
|
||||
$changeSalesValue = bcmul($totalSalesValue, $amount, 2);
|
||||
$changeSalesValue = bcdiv($changeSalesValue, $afterSaleProduct->total_amount, 3);
|
||||
// 四舍五入(保留两位小数)
|
||||
$salesValue = round($changeSalesValue, 2);
|
||||
} else {
|
||||
$qty = $afterSale->num;
|
||||
|
||||
if ($qty > $afterSaleProduct->quantity) {
|
||||
$qty = $afterSaleProduct->quantity;
|
||||
}
|
||||
|
||||
$salesValue = bcmul($afterSaleProduct->sales_value, $qty, 2);
|
||||
}
|
||||
|
||||
//执行实际退款操作;
|
||||
if ($afterSale->amount > 0) {//退款金额大于0才做实际退款
|
||||
$order->refundLogs()->create([
|
||||
|
|
@ -369,17 +398,25 @@ class AfterSaleService
|
|||
]);
|
||||
}
|
||||
} elseif (in_array($afterSale->type, [AfterSale::TYPE_CHANGE])) {//换货流程
|
||||
$qty = $afterSale->num;
|
||||
|
||||
if ($qty > $afterSaleProduct->quantity) {
|
||||
$qty = $afterSaleProduct->quantity;
|
||||
}
|
||||
|
||||
$salesValue = bcmul($afterSaleProduct->sales_value, $qty, 2);
|
||||
|
||||
//复制一个订单(存商品价格,支付价格为0;)
|
||||
$changeOrder = new Order();
|
||||
$changeOrder->user_id = $order->user_id;
|
||||
$changeOrder->sn = serial_number();
|
||||
$changeOrder->products_total_amount = bcmul($afterSale->orderProduct->sell_price, $afterSale->num);
|
||||
$changeOrder->products_total_amount = bcmul($afterSaleProduct->sell_price, $afterSale->num);
|
||||
$changeOrder->coupon_discount_amount = 0;
|
||||
$changeOrder->vip_discount_amount = bcmul(($afterSale->orderProduct->sell_price-$afterSale->orderProduct->vip_price), $afterSale->num);
|
||||
$changeOrder->vip_discount_amount = bcmul(($afterSaleProduct->sell_price-$afterSaleProduct->vip_price), $afterSale->num);
|
||||
$changeOrder->reduced_amount = 0;
|
||||
$changeOrder->shipping_fee = 0;
|
||||
$changeOrder->total_amount = 0;
|
||||
$changeOrder->sales_value = $salesValue;
|
||||
|
||||
// 收货地址
|
||||
$changeOrder->consignee_name = $order->consignee_name;
|
||||
|
|
@ -394,20 +431,20 @@ 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,
|
||||
'name' => $afterSale->orderProduct->name,
|
||||
'specs' => $afterSale->orderProduct->specs,
|
||||
'cover' => $afterSale->orderProduct->cover,
|
||||
'weight' => $afterSale->orderProduct->weight,
|
||||
'sell_price' => $afterSale->orderProduct->sell_price,
|
||||
'vip_price' => $afterSale->orderProduct->vip_price,
|
||||
'sales_value' => $afterSale->orderProduct->sales_value,
|
||||
'gift_for_sku_id' => $afterSaleProduct->gift_for_sku_id,
|
||||
'spu_id' => $afterSaleProduct->spu_id,
|
||||
'sku_id' => $afterSaleProduct->sku_id,
|
||||
'category_id' => $afterSaleProduct->category_id,
|
||||
'name' => $afterSaleProduct->name,
|
||||
'specs' => $afterSaleProduct->specs,
|
||||
'cover' => $afterSaleProduct->cover,
|
||||
'weight' => $afterSaleProduct->weight,
|
||||
'sell_price' => $afterSaleProduct->sell_price,
|
||||
'vip_price' => $afterSaleProduct->vip_price,
|
||||
'sales_value' => $afterSaleProduct->sales_value,
|
||||
'quantity' => $afterSale->num,
|
||||
'coupon_discount_amount'=> 0,
|
||||
'vip_discount_amount' => $afterSale->orderProduct->sell_price - $afterSale->orderProduct->vip_price,
|
||||
'vip_discount_amount' => $afterSaleProduct->sell_price - $afterSaleProduct->vip_price,
|
||||
'reduced_amount' => 0,
|
||||
'total_amount' => $afterSale->amount,
|
||||
]);
|
||||
|
|
@ -419,7 +456,18 @@ class AfterSaleService
|
|||
]);
|
||||
}
|
||||
|
||||
if (in_array($afterSale->type, [
|
||||
$afterSale->update([
|
||||
'sales_value' => $salesValue,
|
||||
'state' => AfterSale::STATE_FINISH,
|
||||
'remarks' => $remarks,
|
||||
]);
|
||||
|
||||
$afterSale->logs()->create([
|
||||
'name' => '财务审核',
|
||||
'desc' => $remarks,
|
||||
]);
|
||||
|
||||
if (! $afterSaleProduct->isGift() && in_array($afterSale->type, [
|
||||
AfterSale::TYPE_REFUND_AND_RETURN,
|
||||
AfterSale::TYPE_REFUND,
|
||||
AfterSale::TYPE_CHANGE,
|
||||
|
|
@ -431,15 +479,9 @@ class AfterSaleService
|
|||
]);
|
||||
}
|
||||
|
||||
AfterSaleLog::create([
|
||||
'after_sale_id' => $afterSale->id,
|
||||
'name' => '财务审核',
|
||||
'desc' => $remarks,
|
||||
]);
|
||||
$afterSale->update([
|
||||
'state' => $afterSale::STATE_FINISH,
|
||||
'remarks' => $remarks,
|
||||
]);
|
||||
if (bccomp($afterSale->sales_value, '0', 2) === 1) {
|
||||
$order->user->userInfo->incrPreGrowthValue(bcmul($afterSale->sales_value, '-1', 2));
|
||||
}
|
||||
} else {
|
||||
throw new BizException('该售后订单状态异常');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,41 +76,6 @@ class DistributionPreIncomeJobService
|
|||
return;
|
||||
}
|
||||
|
||||
// 售后商品的总差价(元)
|
||||
$totalDiffPrice = 0;
|
||||
|
||||
if (! is_null($afterSaleProduct->vip_price) && $afterSaleProduct->vip_discount_amount === 0) {
|
||||
$diffPrice = $afterSaleProduct->sell_price - $afterSaleProduct->vip_price;
|
||||
|
||||
if ($diffPrice > 0) {
|
||||
$totalDiffPrice = bcdiv($diffPrice * $afterSaleProduct->quantity, '100');
|
||||
}
|
||||
}
|
||||
|
||||
// 售后商品的总销售值
|
||||
$totalSalesValue = bcmul($afterSaleProduct->sales_value, $afterSaleProduct->quantity);
|
||||
|
||||
// 变更比例
|
||||
$changeRate = 1;
|
||||
|
||||
if ($afterSaleProduct->total_amount > 0) {
|
||||
$amount = $afterSale->amount;
|
||||
|
||||
if ($amount >= $afterSaleProduct->total_amount) {
|
||||
$amount = $afterSaleProduct->total_amount;
|
||||
}
|
||||
|
||||
$changeRate = bcdiv($amount, $afterSaleProduct->total_amount);
|
||||
} else {
|
||||
$qty = $afterSale->num;
|
||||
|
||||
if ($qty > $afterSaleProduct->quantity) {
|
||||
$qty = $afterSaleProduct->quantity;
|
||||
}
|
||||
|
||||
$changeRate = bcdiv($qty, $afterSaleProduct->quantity);
|
||||
}
|
||||
|
||||
$preIncomes = DistributionPreIncome::where('order_id', $afterSale->order_id)->get();
|
||||
|
||||
$time = now();
|
||||
|
|
@ -119,49 +84,91 @@ class DistributionPreIncomeJobService
|
|||
foreach ($preIncomes as $preIncome) {
|
||||
$rule = $preIncome->rule;
|
||||
|
||||
$changeAmount = 0;
|
||||
$changeSalesValue = 0;
|
||||
$changeRevenue = 0;
|
||||
if ($preIncome->type === DistributionPreIncome::TYPE_PRICE_DIFF) {
|
||||
// 售后商品的总差价(元)
|
||||
$totalDiffPrice = 0;
|
||||
|
||||
switch ($preIncome->type) {
|
||||
case DistributionPreIncome::TYPE_PRICE_DIFF:
|
||||
$changeAmount = bcmul($totalDiffPrice, $changeRate);
|
||||
$changeSalesValue = bcmul($totalSalesValue, $changeRate);
|
||||
// 差价奖励按变更的差价计算
|
||||
$changeRevenue = bcmul($changeAmount, $rule['bonus_rate']);
|
||||
$changeRevenue = bcmul($changeRevenue, bcsub('1', $rule['fee_rate']));
|
||||
break;
|
||||
if (! is_null($afterSaleProduct->vip_price) && $afterSaleProduct->vip_discount_amount === 0) {
|
||||
$diffPrice = $afterSaleProduct->sell_price - $afterSaleProduct->vip_price;
|
||||
|
||||
case DistributionPreIncome::TYPE_LEVEL_DIFF:
|
||||
case DistributionPreIncome::TYPE_LEVEL_SAME:
|
||||
$changeAmount = bcmul(bcdiv($afterSaleProduct->total_amount, '100'), $changeRate);
|
||||
$changeSalesValue = bcmul($totalSalesValue, $changeRate);
|
||||
// 级差奖励和平级奖励按变更的销售值算收益
|
||||
$changeRevenue = bcmul($changeSalesValue, $rule['bonus_rate']);
|
||||
$changeRevenue = bcmul($changeRevenue, bcsub('1', $rule['fee_rate']));
|
||||
break;
|
||||
if ($diffPrice > 0) {
|
||||
$totalDiffPrice = bcdiv($diffPrice * $afterSaleProduct->quantity, '100');
|
||||
}
|
||||
}
|
||||
|
||||
if (bccomp($totalDiffPrice, '0', 2) <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$changeAmount = 0;
|
||||
if ($afterSaleProduct->total_amount > 0) {
|
||||
$amount = $afterSale->amount;
|
||||
|
||||
if ($amount >= $afterSaleProduct->total_amount) {
|
||||
$amount = $afterSaleProduct->total_amount;
|
||||
}
|
||||
|
||||
$changeAmount = bcdiv(bcmul($totalDiffPrice, $amount), $afterSaleProduct->total_amount);
|
||||
} else {
|
||||
$qty = $afterSale->num;
|
||||
|
||||
if ($qty > $afterSaleProduct->quantity) {
|
||||
$qty = $afterSaleProduct->quantity;
|
||||
}
|
||||
|
||||
$changeAmount = bcdiv(bcmul($totalDiffPrice, $qty), $afterSaleProduct->quantity);
|
||||
}
|
||||
|
||||
// 差价奖励按变更的差价计算
|
||||
$changeRevenue = bcmul($changeAmount, $rule['bonus_rate']);
|
||||
$changeRevenue = bcmul($changeRevenue, bcsub('1', $rule['fee_rate']));
|
||||
$changeRevenue = round($changeRevenue, 2);
|
||||
// 变更金额
|
||||
$changeAmount = round($changeAmount, 2);
|
||||
// 变更成长值
|
||||
$changeSalesValue = $afterSale->sales_value;
|
||||
|
||||
$preIncome->update([
|
||||
'total_amount' => DB::raw("total_amount-{$changeAmount}"),
|
||||
'total_revenue' => DB::raw("total_revenue-{$changeRevenue}"),
|
||||
'total_sales_value' => DB::raw("total_sales_value-{$changeSalesValue}"),
|
||||
]);
|
||||
|
||||
$preIncomeLogs[] = [
|
||||
'pre_income_id' => $preIncome->id,
|
||||
'pre_income_job_id' => $job->id,
|
||||
'change_amount' => bcmul($changeAmount, '-1'),
|
||||
'change_revenue' => bcmul($changeRevenue, '-1'),
|
||||
'change_sales_value' => bcmul($changeSalesValue, '-1'),
|
||||
'remarks' => $afterSale->isChange() ? '订单换货' : '订单退款',
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time,
|
||||
];
|
||||
} elseif (in_array($preIncome->type, [DistributionPreIncome::TYPE_LEVEL_DIFF, DistributionPreIncome::TYPE_LEVEL_SAME])) {
|
||||
$changeAmount = bcdiv($afterSale->amount, '100');
|
||||
$changeSalesValue = $afterSale->sales_value;
|
||||
// 级差奖励和平级奖励按变更的销售值算收益
|
||||
$changeRevenue = bcmul($changeSalesValue, $rule['bonus_rate']);
|
||||
$changeRevenue = bcmul($changeRevenue, bcsub('1', $rule['fee_rate']));
|
||||
$changeRevenue = round($changeRevenue, 2);
|
||||
|
||||
$preIncome->update([
|
||||
'total_amount' => DB::raw("total_amount-{$changeAmount}"),
|
||||
'total_revenue' => DB::raw("total_revenue-{$changeRevenue}"),
|
||||
'total_sales_value' => DB::raw("total_sales_value-{$changeSalesValue}"),
|
||||
]);
|
||||
|
||||
$preIncomeLogs[] = [
|
||||
'pre_income_id' => $preIncome->id,
|
||||
'pre_income_job_id' => $job->id,
|
||||
'change_amount' => bcmul($changeAmount, '-1'),
|
||||
'change_revenue' => bcmul($changeRevenue, '-1'),
|
||||
'change_sales_value' => bcmul($changeSalesValue, '-1'),
|
||||
'remarks' => $afterSale->isChange() ? '订单换货' : '订单退款',
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time,
|
||||
];
|
||||
}
|
||||
|
||||
$changeAmount = round($changeAmount, 2);
|
||||
$changeSalesValue = round($changeSalesValue, 2);
|
||||
$changeRevenue = round($changeRevenue, 2);
|
||||
|
||||
$preIncome->update([
|
||||
'total_amount' => DB::raw("total_amount-{$changeAmount}"),
|
||||
'total_sales_value' => DB::raw("total_sales_value-{$changeSalesValue}"),
|
||||
'total_revenue' => DB::raw("total_revenue-{$changeRevenue}"),
|
||||
]);
|
||||
|
||||
$preIncomeLogs[] = [
|
||||
'pre_income_id' => $preIncome->id,
|
||||
'pre_income_job_id' => $job->id,
|
||||
'change_amount' => bcmul($changeAmount, '-1'),
|
||||
'change_sales_value' => bcmul($changeSalesValue, '-1'),
|
||||
'change_revenue' => bcmul($changeRevenue, '-1'),
|
||||
'remarks' => $afterSale->isChange() ? '订单换货' : '订单退款',
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time,
|
||||
];
|
||||
}
|
||||
|
||||
DistributionPreIncomeLog::insert($preIncomeLogs);
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ class OrderService
|
|||
$productsTotalAmount,
|
||||
$vipDiscountAmount,
|
||||
$couponDiscountAmount,
|
||||
$salesValue
|
||||
) = $this->calculateFees($mapProducts);
|
||||
|
||||
$order = $this->storeOrder(
|
||||
|
|
@ -133,6 +134,7 @@ class OrderService
|
|||
$couponDiscountAmount,
|
||||
$vipDiscountAmount,
|
||||
$shippingFee,
|
||||
$salesValue,
|
||||
$note,
|
||||
$coupon
|
||||
);
|
||||
|
|
@ -160,6 +162,7 @@ class OrderService
|
|||
* @param int $couponDiscountAmount
|
||||
* @param int $vipDiscountAmount
|
||||
* @param int $shippingFee
|
||||
* @param float $salesValue
|
||||
* @param string|null $note
|
||||
* @param \App\Models\UserCoupon|null $coupon
|
||||
* @return \App\Models\Order
|
||||
|
|
@ -171,6 +174,7 @@ class OrderService
|
|||
int $couponDiscountAmount,
|
||||
int $vipDiscountAmount,
|
||||
int $shippingFee,
|
||||
$salesValue,
|
||||
?string $note = null,
|
||||
?UserCoupon $coupon = null,
|
||||
): Order {
|
||||
|
|
@ -194,6 +198,7 @@ class OrderService
|
|||
'shipping_fee' => $shippingFee,
|
||||
'products_total_amount' => $productsTotalAmount,
|
||||
'total_amount' => $totalAmount, // 商品总额-券折扣金额-会员折扣金额+邮费
|
||||
'sales_value' => $salesValue, // 订单总销售值
|
||||
'note' => $note,
|
||||
// 收货地址
|
||||
'consignee_name' => $shippingAddress->consignee,
|
||||
|
|
@ -445,6 +450,7 @@ class OrderService
|
|||
$productsTotalAmount,
|
||||
$vipDiscountAmount,
|
||||
$couponDiscountAmount,
|
||||
$salesValue,
|
||||
) = $this->calculateFees($mapProducts);
|
||||
|
||||
$totalAmount = $productsTotalAmount - $couponDiscountAmount - $vipDiscountAmount;
|
||||
|
|
@ -484,17 +490,20 @@ class OrderService
|
|||
$productsTotalAmount = 0;
|
||||
$vipDiscountAmount = 0;
|
||||
$couponDiscountAmount = 0;
|
||||
$salesValue = 0;
|
||||
|
||||
foreach ($products as $product) {
|
||||
$productsTotalAmount += $product['total_amount'];
|
||||
$vipDiscountAmount += $product['vip_discount_amount'];
|
||||
$couponDiscountAmount += $product['coupon_discount_amount'];
|
||||
$salesValue = bcadd($salesValue, $product['total_sales_value']);
|
||||
}
|
||||
|
||||
return [
|
||||
$productsTotalAmount,
|
||||
$vipDiscountAmount,
|
||||
$couponDiscountAmount,
|
||||
$salesValue,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -567,6 +576,8 @@ class OrderService
|
|||
),
|
||||
// 总金额
|
||||
'total_amount' => $sku->sell_price * $item['quantity'],
|
||||
// 总销售值
|
||||
'total_sales_value' => bcdiv($sku->sales_value, $item['quantity']),
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -832,6 +843,9 @@ class OrderService
|
|||
'reason' => '取消订单',
|
||||
]);
|
||||
|
||||
// 扣除用户的预成长值
|
||||
$order->user->userInfo->incrPreGrowthValue(bcmul($order->sales_value, '-1', 2));
|
||||
|
||||
DistributionPreIncomeJob::create([
|
||||
'jobable_id' => $refundLog->id,
|
||||
'jobable_type' => $refundLog->getMorphClass(),
|
||||
|
|
|
|||
|
|
@ -87,6 +87,9 @@ class PayService
|
|||
'auto_complete_at' => now()->addDays(app_settings('app.order_auto_complete_days', 7)),
|
||||
]);
|
||||
|
||||
// 增加用户的预成长值
|
||||
$payable->user->userInfo->incrPreGrowthValue($payable->sales_value);
|
||||
|
||||
DistributionPreIncomeJob::create([
|
||||
'jobable_id' => $payable->id,
|
||||
'jobable_type' => $payable->getMorphClass(),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddPreGrowthsValueToUserInfosTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('user_infos', function (Blueprint $table) {
|
||||
$table->unsignedDecimal('pre_growth_value', 18, 2)->default(0)->comment('预收益成长值');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('user_infos', function (Blueprint $table) {
|
||||
$table->dropColumn(['pre_growth_value']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddSalesValueToAfterSalesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('after_sales', function (Blueprint $table) {
|
||||
$table->unsignedDecimal('sales_value', 18, 2)->default(0.00)->comment('变更销售值');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('after_sales', function (Blueprint $table) {
|
||||
$table->dropColumn(['sales_value']);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue