修改订单金额的错误

This commit is contained in:
luofei 2024-01-26 18:01:29 +08:00
parent 67131a8e5a
commit 19c9cf3c3b
2 changed files with 51 additions and 14 deletions

View File

@ -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];
}
}

View File

@ -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);