From 19c9cf3c3b4545b5d99071e14bf2ce4bd3532443 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Fri, 26 Jan 2024 18:01:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A2=E5=8D=95=E9=87=91?= =?UTF-8?q?=E9=A2=9D=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consumption/StoreConsumptionUserDao.php | 41 ++++++++++++++++++- .../order/StoreOrderCreateRepository.php | 24 +++++------ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/app/common/dao/store/consumption/StoreConsumptionUserDao.php b/app/common/dao/store/consumption/StoreConsumptionUserDao.php index e1d33ed1..f12edffb 100755 --- a/app/common/dao/store/consumption/StoreConsumptionUserDao.php +++ b/app/common/dao/store/consumption/StoreConsumptionUserDao.php @@ -18,17 +18,28 @@ class StoreConsumptionUserDao extends BaseDao /** @var float $maxAmount 单笔订单计算红包的最大有效金额 */ public $maxAmount = 20000; + /** @var float $orderTotalPrice 订单总金额 */ public $orderTotalPrice; + /** @var float $orderPayPrice 订单实付金额 */ public $orderPayPrice; + /** @var float $orderProductPrice 当前商品金额 */ public $orderProductPrice; + /** @var float $realPriceTotal 扣除红包后实际支付金额 */ public $realPriceTotal = 0; + /** @var float $isLast 是否最后一条数据 */ public $isLast = false; + /** @var float $groupOrderTotalPrice 订单组总金额 */ + public $groupOrderTotalPrice; + + /** @var float $consumptionTotalAmount 红包总金额 */ + public $consumptionTotalAmount; + protected function getModel(): string { return StoreConsumptionUser::class; @@ -293,7 +304,7 @@ class StoreConsumptionUserDao extends BaseDao } /** - * 根据计算商品实际金额和红包金额 + * 根据订单商品计算实际金额和红包金额 * @return array */ public function calculate() @@ -319,4 +330,32 @@ class StoreConsumptionUserDao extends BaseDao return [$realPrice, $realPriceTotal, $consumptionAmount]; } + /** + * 根据订单计算实际金额和红包金额 + * @return array + */ + public function calculateByOrder() + { + // 把所有金额转换成分,避免红包金额产生误差 + $orderTotalPrice = $this->orderTotalPrice * 100; + $groupOrderTotalPrice = $this->groupOrderTotalPrice * 100; + $consumptionBalance = $this->consumptionTotalAmount * 100; + $rate = bcdiv($orderTotalPrice, $groupOrderTotalPrice, 5); + if ($consumptionBalance >= $orderTotalPrice) { + $useAmount = $orderTotalPrice; + } else { + $useAmount = $this->isLast ? $consumptionBalance : bcmul($consumptionBalance, $rate, 5); + } + $consumptionBalance -= $useAmount; + $payPrice = $orderTotalPrice - $useAmount; + $groupOrderTotalPrice -= $useAmount; + + $payPrice = bcdiv($payPrice, 100, 2); + $useAmount = bcdiv($useAmount, 100, 2); + $consumptionBalance = bcdiv($consumptionBalance, 100, 2); + $groupOrderTotalPrice = bcdiv($groupOrderTotalPrice, 100, 2); + /** $payPrice:实际支付的金额,$groupOrderTotalPrice:实际支付金额总计,$useAmount:红包金额总计,$consumptionAmount:红包余额总计 */ + return [$payPrice, $groupOrderTotalPrice, $useAmount, $consumptionBalance]; + } + } diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index c40c227e..5869e120 100755 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -2,6 +2,7 @@ namespace app\common\repositories\store\order; +use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\order\StoreCartDao; use app\common\dao\store\StoreActivityDao; use app\common\model\store\order\StoreGroupOrder; @@ -638,30 +639,28 @@ class StoreOrderCreateRepository extends StoreOrderRepository } $consumptionTotal = min($consumptionTotal, $this->balance); } + $groupOrderPayPrice = $order_total_price; foreach ($merchantCartList as $k => &$merchantCart) { $isLast = count($merchantCartList) == $k + 1; - $merchantPrice = 0; + $orderPayPrice = 0; foreach ($merchantCart['list'] as &$cart) { $cart['total_price'] = bcadd($cart['total_price'], $cart['svip_discount'], 2); } if ($consumptionTotal) { - // 按当前店铺的商品金额计算使用的红包金额 - $rate = bcdiv($merchantCart['order']['total_price'], $order_total_price, 6); - if ($consumptionTotal >= $order_total_price) { - $useAmount = $merchantCart['order']['total_price']; - } else { - $useAmount = $isLast ? bcsub($consumptionTotal, $this->consumption_money, 2) : bcmul($consumptionTotal, $rate, 2); - } - $merchantProductPrice = bcsub($merchantCart['order']['total_price'], $useAmount, 2); - $merchantPrice = bcadd($merchantPrice, $merchantProductPrice, 2); + $storeConsumptionUserDao = new StoreConsumptionUserDao(); + $storeConsumptionUserDao->groupOrderTotalPrice = $groupOrderPayPrice; + $storeConsumptionUserDao->orderTotalPrice = $merchantCart['order']['total_price']; + $storeConsumptionUserDao->consumptionTotalAmount = $consumptionTotal; + $storeConsumptionUserDao->isLast = $isLast; + [$orderPayPrice, $groupOrderPayPrice, $useAmount, $consumptionTotal] = $storeConsumptionUserDao->calculateByOrder(); $this->consumption_money = bcadd($this->consumption_money, $useAmount, 2); $this->balance = bcsub($this->balance, $useAmount, 2); } unset($cart); $merchantCart['order']['consumption_money'] = $useAmount ?? '0.00'; - $merchantCart['order']['real_price'] = $merchantPrice; + $merchantCart['order']['pay_price'] = $orderPayPrice; $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); } @@ -669,8 +668,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $status = ($address || $order_model || $allow_no_address) ? ($noDeliver ? 'noDeliver' : 'finish') : 'noAddress'; $order = $merchantCartList; $consumption_money = $this->consumption_money; - $order_total_price = bcsub($order_total_price, $this->consumption_money, 2); - $order_price = $order_total_price; + $order_price = $groupOrderPayPrice; $total_price = $order_total_price; $openIntegral = $merIntegralFlag && !$order_type && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0; $total_coupon = bcadd($order_svip_discount, bcadd(bcadd($total_platform_coupon_price, $order_coupon_price, 2), $order_total_integral_price, 2), 2);