调整抵扣红包使用比例

This commit is contained in:
luofei 2024-02-04 16:21:41 +08:00
parent 423fc1d013
commit 6c90de3771
4 changed files with 126 additions and 33 deletions

View File

@ -44,6 +44,10 @@ class StoreConsumptionUserDao extends BaseDao
public $consumptionTotalAmount; public $consumptionTotalAmount;
/** @var int $orderType 订单类型1 groupOrder2 order */ /** @var int $orderType 订单类型1 groupOrder2 order */
public $orderType = 2; public $orderType = 2;
/** @var float $consumptionUsed 已使用的红包金额 */
public $consumptionUsed;
/** @var float $profitRate 商品毛利率 */
public $profitRate;
protected function getModel(): string protected function getModel(): string
{ {
@ -353,6 +357,7 @@ class StoreConsumptionUserDao extends BaseDao
} }
/** /**
* @deprecated 已废弃,用下面的 typeTwoByProduct typeOneByProduct
* 根据订单计算实际金额和红包金额 * 根据订单计算实际金额和红包金额
* @return array * @return array
*/ */
@ -452,4 +457,83 @@ class StoreConsumptionUserDao extends BaseDao
]); ]);
} }
/**
* 红包类型为 现金抵扣红包
* 根据商品毛利率计算红包金额
* @return array
*/
public function typeTwoByProduct()
{
// 把所有金额转换成分,避免红包金额产生误差
$totalPrice = $this->orderProductPrice * 100;
$consumptionUsed = $this->consumptionUsed * 100;
$consumptionTotal = $this->consumptionTotalAmount * 100;
$consumptionBalance = $consumptionTotal - $consumptionUsed;
if ($consumptionBalance <= 0) {
return [$this->orderProductPrice, '0.00', $this->consumptionUsed];
}
$rate = $this->getRate($this->profitRate);
$useAmount = bcmul($totalPrice, $rate, 2);
if ($useAmount >= $consumptionBalance) {
$useAmount = $consumptionBalance;
}
$payPrice = $totalPrice - $useAmount;
$consumptionUsed += $useAmount;
$payPrice = bcdiv($payPrice, 100, 2);
$useAmount = bcdiv($useAmount, 100, 2);
$consumptionUsed = bcdiv($consumptionUsed, 100, 2);
/** $payPrice实际支付的金额$useAmount当前商品使用的红包金额$consumptionUsed使用的红包金额总计 */
return [$payPrice, $useAmount, $consumptionUsed];
}
/**
* 红包类型为 无门槛红包
* 根据商品毛利率计算红包金额
* @return array
*/
public function typeOneByProduct()
{
// 把所有金额转换成分,避免红包金额产生误差
$totalPrice = $this->orderProductPrice * 100;
$consumptionUsed = $this->consumptionUsed * 100;
$consumptionTotal = $this->consumptionTotalAmount * 100;
$groupOrderTotalPrice = $this->groupOrderTotalPrice * 100;
$rate = bcdiv($totalPrice, $groupOrderTotalPrice, 5);
if ($consumptionTotal >= $groupOrderTotalPrice) {
$useAmount = $totalPrice;
} else {
$useAmount = $this->isLast ? $consumptionTotal - $consumptionUsed : round(bcmul($consumptionTotal, $rate, 1));
}
$payPrice = $totalPrice - $useAmount;
$consumptionUsed += $useAmount;
$payPrice = bcdiv($payPrice, 100, 2);
$useAmount = bcdiv($useAmount, 100, 2);
$consumptionUsed = bcdiv($consumptionUsed, 100, 2);
/** $payPrice实际支付的金额$useAmount当前商品使用的红包金额$consumptionUsed使用的红包金额总计 */
return [$payPrice, $useAmount, $consumptionUsed];
}
/**
* 根据商品毛利率计算红包抵扣比例
* @param float $profitRate 毛利率
* @return float|int
*/
public function getRate($profitRate)
{
$rateArray = [
'0.25' => 0.4,
'0.2' => 0.3,
'0.15' => 0.2,
'0.1' => 0.1,
];
$rate = 0;
foreach ($rateArray as $k => $item) {
if ($profitRate >= $k) {
$rate = $item;
break;
}
}
return $rate;
}
} }

View File

@ -91,7 +91,7 @@ class UserBillDao extends BaseDao
$lockMoney = 0; $lockMoney = 0;
if (count($lst)) { if (count($lst)) {
$lockMoney = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id')) $lockMoney = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id'))
->where('category', 'mer_refund_money')->sum('number'); ->where('category', 'mer_refund_money')->where('mer_id', $merId)->sum('number');
} }
foreach ($lst as $bill) { foreach ($lst as $bill) {
$lockMoney = bcadd($lockMoney, $bill['number'], 2); $lockMoney = bcadd($lockMoney, $bill['number'], 2);

View File

@ -457,6 +457,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
$cart['true_price'] = $cartTotalPrice; $cart['true_price'] = $cartTotalPrice;
$procure = $this->cartByPrice($cart, 1); $procure = $this->cartByPrice($cart, 1);
$procure_price = bcmul($cart['cart_num'], $procure, 2); $procure_price = bcmul($cart['cart_num'], $procure, 2);
$cart['profit_rate'] = bcdiv(($cartTotalPrice - $procure_price), $cartTotalPrice, 2);
$orderProcurePrice = bcadd($orderProcurePrice, $procure_price, 2); $orderProcurePrice = bcadd($orderProcurePrice, $procure_price, 2);
} }
unset($cart, $_k); unset($cart, $_k);
@ -621,42 +622,49 @@ class StoreOrderCreateRepository extends StoreOrderRepository
$allow_no_address = false; $allow_no_address = false;
} }
// 计算红包总金额 if ($this->store_consumption_user['type'] == 2 && $order_total_price >= 6 && $this->payType == 'balance') {
$consumptionTotal = 0; throw new ValidateException('余额支付时,不能使用抵扣红包');
if ($consumption_id > 0 && $this->store_consumption_user && $source == 103) {
if ($this->store_consumption_user['type'] == 2 && $order_total_price >= 6) {
if ($this->payType == 'balance') {
throw new ValidateException('余额支付时,不能使用抵扣红包');
}
$consumptionTotal = bcdiv($order_total_price, 6, 2);
}
if ($this->store_consumption_user['type'] == 1) {
$consumptionTotal = min($order_total_price, $this->balance);
}
$consumptionTotal = min($consumptionTotal, $this->balance);
} }
$groupOrderPayPrice = $order_total_price;
foreach ($merchantCartList as $k => &$merchantCart) {
$isLast = count($merchantCartList) == $k + 1;
foreach ($merchantCart['list'] as &$cart) {
$cart['total_price'] = bcadd($cart['total_price'], $cart['svip_discount'], 2);
}
$orderPayPrice = $merchantCart['order']['total_price']; $consumptionTotal = 0;
if ($consumptionTotal) { $consumptionUsed = 0;
$storeConsumptionUserDao = new StoreConsumptionUserDao(); $groupOrderPayPrice = 0;
$storeConsumptionUserDao->groupOrderTotalPrice = $groupOrderPayPrice; if ($consumption_id > 0 && $this->store_consumption_user && $source == 103) {
$storeConsumptionUserDao->orderTotalPrice = $orderPayPrice; $consumptionTotal = min($order_total_price, $this->balance);
$storeConsumptionUserDao->consumptionTotalAmount = $consumptionTotal; }
$storeConsumptionUserDao->isLast = $isLast; foreach ($merchantCartList as $k => &$merchantCart) {
[$orderPayPrice, $groupOrderPayPrice, $useAmount, $consumptionTotal] = $storeConsumptionUserDao->calculateByOrder(); $lastOrder = count($merchantCartList) == $k + 1;
$this->consumption_money = bcadd($this->consumption_money, $useAmount, 2); $orderPayPrice = 0;
$this->balance = bcsub($this->balance, $useAmount, 2); $orderConsumptionAmount = 0;
foreach ($merchantCart['list'] as $key => &$cart) {
$cart['total_price'] = bcadd($cart['total_price'], $cart['svip_discount'], 2);
if ($consumptionTotal) {
$lastProduct = count($merchantCart['list']) == $key + 1;
$storeConsumptionUserDao = new StoreConsumptionUserDao();
$storeConsumptionUserDao->groupOrderTotalPrice = $order_total_price;
$storeConsumptionUserDao->orderProductPrice = $cart['total_price'];
$storeConsumptionUserDao->profitRate = $cart['profit_rate'];
$storeConsumptionUserDao->consumptionTotalAmount = $consumptionTotal;
$storeConsumptionUserDao->consumptionUsed = $consumptionUsed;
$storeConsumptionUserDao->isLast = $lastOrder && $lastProduct;
if ($this->store_consumption_user['type'] == 2) {
[$payPrice, $useAmount, $consumptionUsed] = $storeConsumptionUserDao->typeTwoByProduct();
} else {
[$payPrice, $useAmount, $consumptionUsed] = $storeConsumptionUserDao->typeOneByProduct();
}
$this->consumption_money = bcadd($this->consumption_money, $useAmount, 2);
$this->balance = bcsub($this->balance, $useAmount, 2);
}
$cart['pay_price'] = $payPrice ?? $cart['total_price'];
$cart['consumption_price'] = $useAmount ?? 0;
$orderPayPrice = bcadd($orderPayPrice, $cart['pay_price'], 2);
$orderConsumptionAmount = bcadd($orderConsumptionAmount, $cart['consumption_price'], 2);
$groupOrderPayPrice = bcadd($groupOrderPayPrice, $cart['pay_price'], 2);
} }
if ($source == 105) { if ($source == 105) {
$this->consumption_money = bcsub($orderPayPrice, 2, 2); $this->consumption_money = bcsub($orderPayPrice, 2, 2);
$useAmount = $this->consumption_money; $orderConsumptionAmount = $this->consumption_money;
$orderPayPrice = '2.00'; $orderPayPrice = '2.00';
$groupOrderPayPrice = $orderPayPrice; $groupOrderPayPrice = $orderPayPrice;
$order_total_price = $orderPayPrice; $order_total_price = $orderPayPrice;
@ -665,7 +673,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
} }
unset($cart); unset($cart);
$merchantCart['order']['consumption_money'] = $useAmount ?? '0.00'; $merchantCart['order']['consumption_money'] = $orderConsumptionAmount;
$merchantCart['order']['pay_price'] = $orderPayPrice; $merchantCart['order']['pay_price'] = $orderPayPrice;
$merchantCart['order']['total_price'] = bcadd($merchantCart['order']['total_price'], $merchantCart['order']['svip_discount'], 2); $merchantCart['order']['total_price'] = bcadd($merchantCart['order']['total_price'], $merchantCart['order']['svip_discount'], 2);
$order_total_price = bcadd($order_total_price, $merchantCart['order']['svip_discount'], 2); $order_total_price = bcadd($order_total_price, $merchantCart['order']['svip_discount'], 2);
@ -1177,6 +1185,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository
'cart_info' => json_encode($order_cart), 'cart_info' => json_encode($order_cart),
'product_source_id' => $product_source_id, 'product_source_id' => $product_source_id,
'product_mer_id' => $product_mer_id, 'product_mer_id' => $product_mer_id,
'pay_price' => $cart['pay_price'],
'consumption_price' => $cart['consumption_price'],
]; ];
} }

View File

@ -1343,7 +1343,6 @@ class StoreRefundOrderRepository extends BaseRepository
$orderType = (isset($item['presell']) && $item['presell']) ? 'presell' : 'order'; $orderType = (isset($item['presell']) && $item['presell']) ? 'presell' : 'order';
if ($item['type'] == 0) { if ($item['type'] == 0) {
$this->refundBill($item, $res->uid, $id); $this->refundBill($item, $res->uid, $id);
app()->make(MerchantRepository::class)->subLockMoney($res->mer_id, $orderType, $item['id'], $refundPrice);
} else { } else {
if ($item['type'] == 10) $server = WechatService::create()->combinePay(); if ($item['type'] == 10) $server = WechatService::create()->combinePay();
if (in_array($item['type'], [2])) $server = MiniProgramService::create(); if (in_array($item['type'], [2])) $server = MiniProgramService::create();