调整优惠券余额扣除

This commit is contained in:
luofei 2024-02-23 15:50:29 +08:00
parent b9211cbb48
commit b87c5a9f79

View File

@ -391,6 +391,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
$pay_price = max(bcsub($valid_total_price, $coupon_price, 2), 0); $pay_price = max(bcsub($valid_total_price, $coupon_price, 2), 0);
$_pay_price = $pay_price; $_pay_price = $pay_price;
$cartCoupon = []; //购物车各商品的优惠券信息 $cartCoupon = []; //购物车各商品的优惠券信息
$userUsedCoupon = []; //用户优惠券使用信息,用于下单时扣除优惠券余额
//计算店铺券 //计算店铺券
foreach ($merchantCart['coupon'] as &$coupon) { foreach ($merchantCart['coupon'] as &$coupon) {
if (!in_array($coupon['coupon']['type'], [0, 2])) continue; if (!in_array($coupon['coupon']['type'], [0, 2])) continue;
@ -420,7 +421,12 @@ class StoreOrderCreateRepository extends StoreOrderRepository
[$payPrice, $useAmount, $couponUsed] = $storeCouponUserDao->couponByProduct($coupon['coupon_type']); [$payPrice, $useAmount, $couponUsed] = $storeCouponUserDao->couponByProduct($coupon['coupon_type']);
$coupon_price = bcadd($coupon_price, $useAmount, 2); $coupon_price = bcadd($coupon_price, $useAmount, 2);
$_pay_price = bcsub($_pay_price, $useAmount, 2); $_pay_price = bcsub($_pay_price, $useAmount, 2);
$cartCoupon[$cart['cart_id']][] = ['coupon_user_id' => $coupon['coupon_user_id'], 'coupon_price' => $useAmount, 'true_price' => $payPrice]; $cartCoupon[$cart['cart_id']] = ['coupon_price' => $useAmount, 'true_price' => $payPrice];
if (!isset($userUsedCoupon[$coupon['coupon_user_id']])) {
$userUsedCoupon[$coupon['coupon_user_id']] = ['coupon_user_id' => $coupon['coupon_user_id'], 'coupon_price' => $useAmount];
} else {
$userUsedCoupon[$coupon['coupon_user_id']]['coupon_price'] = bcadd($userUsedCoupon[$coupon['coupon_user_id']]['coupon_price'], $useAmount, 2);
}
} }
} else { } else {
$coupon_price = bcadd($coupon_price, $coupon['coupon_price'], 2); $coupon_price = bcadd($coupon_price, $coupon['coupon_price'], 2);
@ -438,6 +444,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
$useCouponIds = []; $useCouponIds = [];
//计算优惠占比 //计算优惠占比
foreach ($use_coupon_product as $productId => $coupon) { foreach ($use_coupon_product as $productId => $coupon) {
$userUsedCoupon[$coupon['coupon_user_id']] = ['coupon_user_id' => $coupon['coupon_user_id'], 'coupon_price' => $coupon['coupon_price']];
$productCouponRate[$productId] = [ $productCouponRate[$productId] = [
'rate' => $product_price[$productId] > 0 ? bcdiv($coupon['coupon_price'], $product_price[$productId], 4) : 1, 'rate' => $product_price[$productId] > 0 ? bcdiv($coupon['coupon_price'], $product_price[$productId], 4) : 1,
'coupon_price' => $coupon['coupon_price'], 'coupon_price' => $coupon['coupon_price'],
@ -469,14 +476,12 @@ class StoreOrderCreateRepository extends StoreOrderRepository
} else { } else {
array_pop($product_cart); array_pop($product_cart);
if (!count($product_cart)) { if (!count($product_cart)) {
$cartCoupon[$cart['cart_id']][] = ['coupon_user_id' => $productCouponRate[$cart['product_id']]['coupon_user_id'], 'coupon_price' => $productCouponRate[$cart['product_id']]['coupon_price']];
$cartTotalPrice = bcsub($cartTotalPrice, $productCouponRate[$cart['product_id']]['coupon_price'], 2); $cartTotalPrice = bcsub($cartTotalPrice, $productCouponRate[$cart['product_id']]['coupon_price'], 2);
$productCouponRate[$cart['product_id']]['coupon_price'] = 0; $productCouponRate[$cart['product_id']]['coupon_price'] = 0;
} else { } else {
$couponPrice = $productCouponRate[$cart['product_id']]['coupon_price']; $couponPrice = $productCouponRate[$cart['product_id']]['coupon_price'];
$cartTotalPrice = bcsub($cartTotalPrice, $couponPrice, 2); $cartTotalPrice = bcsub($cartTotalPrice, $couponPrice, 2);
$productCouponRate[$cart['product_id']]['coupon_price'] = bcsub($productCouponRate[$cart['product_id']]['coupon_price'], $couponPrice, 2); $productCouponRate[$cart['product_id']]['coupon_price'] = bcsub($productCouponRate[$cart['product_id']]['coupon_price'], $couponPrice, 2);
$cartCoupon[$cart['cart_id']][] = ['coupon_user_id' => $productCouponRate[$cart['product_id']]['coupon_user_id'], 'coupon_price' => $couponPrice];
} }
} }
} }
@ -484,7 +489,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
//(店铺券) //(店铺券)
if ($storeCouponRate && $cartTotalPrice > 0) { if ($storeCouponRate && $cartTotalPrice > 0) {
if (isset($cartCoupon[$cart['cart_id']])) { if (isset($cartCoupon[$cart['cart_id']])) {
$couponPrice = $cartCoupon[$cart['cart_id']][0]['coupon_price']; $couponPrice = $cartCoupon[$cart['cart_id']]['coupon_price'];
$cartTotalPrice = bcsub($cartTotalPrice, $couponPrice, 2); $cartTotalPrice = bcsub($cartTotalPrice, $couponPrice, 2);
$storeCouponRate['coupon_price'] = bcsub($storeCouponRate['coupon_price'], $couponPrice, 2); $storeCouponRate['coupon_price'] = bcsub($storeCouponRate['coupon_price'], $couponPrice, 2);
} else { } else {
@ -532,7 +537,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
'svip_discount' => $total_svip_discount, 'svip_discount' => $total_svip_discount,
'use_svip' => $use_svip, 'use_svip' => $use_svip,
'order_refund_switch' => $order_refund_switch, 'order_refund_switch' => $order_refund_switch,
'store_coupon' => $cartCoupon, 'user_used_coupon' => $userUsedCoupon,
]; ];
$order_total_postage = bcadd($order_total_postage, $postage_price, 2); $order_total_postage = bcadd($order_total_postage, $postage_price, 2);
$order_svip_discount = bcadd($total_svip_discount, $order_svip_discount, 2); $order_svip_discount = bcadd($total_svip_discount, $order_svip_discount, 2);