diff --git a/app/common/dao/store/consumption/StoreConsumptionUserDao.php b/app/common/dao/store/consumption/StoreConsumptionUserDao.php index 33b4d457..e1d33ed1 100755 --- a/app/common/dao/store/consumption/StoreConsumptionUserDao.php +++ b/app/common/dao/store/consumption/StoreConsumptionUserDao.php @@ -16,7 +16,18 @@ use think\facade\Db; 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; protected function getModel(): string { @@ -281,4 +292,31 @@ class StoreConsumptionUserDao extends BaseDao } } + /** + * 根据计算商品实际金额和红包金额 + * @return array + */ + public function calculate() + { + // 把所有金额转换成分,避免红包金额产生误差 + $orderTotalPrice = $this->orderTotalPrice * 100; + $orderPayPrice = $this->orderPayPrice * 100; + $productPrice = $this->orderProductPrice * 100; + $realPriceTotal = $this->realPriceTotal * 100; + $consumptionAmount = 0; + if ($orderTotalPrice == $orderPayPrice) { + $realPrice = $productPrice; + } else { + $rate = bcdiv($productPrice, $orderTotalPrice, 5); + $realPrice = $this->isLast ? bcsub($orderPayPrice, $realPriceTotal, 2) : ceil(bcmul($orderPayPrice, $rate, 5)); + $consumptionAmount = $productPrice - $realPrice; + $realPriceTotal += $realPrice; + } + $realPrice = bcdiv($realPrice, 100, 2); + $consumptionAmount = bcdiv($consumptionAmount, 100, 2); + $realPriceTotal = bcdiv($realPriceTotal, 100, 2); + /** $realPrice:实际支付的金额,$realPriceTotal:实际支付金额总计,$consumptionAmount:红包金额总计 */ + return [$realPrice, $realPriceTotal, $consumptionAmount]; + } + } diff --git a/app/common/repositories/store/order/StoreRefundOrderRepository.php b/app/common/repositories/store/order/StoreRefundOrderRepository.php index 761b3730..66a8ff81 100755 --- a/app/common/repositories/store/order/StoreRefundOrderRepository.php +++ b/app/common/repositories/store/order/StoreRefundOrderRepository.php @@ -14,6 +14,7 @@ namespace app\common\repositories\store\order; +use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\order\StoreRefundOrderDao; use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreOrder; @@ -230,7 +231,7 @@ class StoreRefundOrderRepository extends BaseRepository * 按订单实付金额退款 * @param $order * @param $products - * @return string + * @return array */ public function getOrderRefundPrice($order, $products) { @@ -238,9 +239,17 @@ class StoreRefundOrderRepository extends BaseRepository $totalPostage = 0; $totalRefundPrice = 0; $realPriceTotal = 0; + $consumptionTotal = 0; foreach ($products as $k => $product) { $isLast = count($products->toArray()) == ($k + 1); - [$realPrice, $realPriceTotal] = $this->calculate($order['total_price'], $order['pay_price'], $product['product_price'], $realPriceTotal, $isLast); + $storeConsumptionUserDao = new StoreConsumptionUserDao(); + $storeConsumptionUserDao->orderTotalPrice = $order['total_price']; + $storeConsumptionUserDao->orderPayPrice = $order['pay_price']; + $storeConsumptionUserDao->orderProductPrice = $product['product_price']; + $storeConsumptionUserDao->realPriceTotal = $realPriceTotal; + $storeConsumptionUserDao->isLast = $isLast; + [$realPrice, $realPriceTotal, $consumptionPrice] = $storeConsumptionUserDao->calculate(); + $consumptionTotal = bcadd($consumptionTotal, $consumptionPrice, 2); $productRefundPrice = $productRefundPrices[$product['order_product_id']] ?? []; $postagePrice = (!$order->status || $order->status == 9) ? bcsub($product['postage_price'], $productRefundPrice['refund_postage'] ?? 0, 2) : 0; $refundPrice = 0; @@ -250,7 +259,7 @@ class StoreRefundOrderRepository extends BaseRepository $totalPostage = bcadd($totalPostage, $postagePrice, 2); $totalRefundPrice = bcadd($totalRefundPrice, $refundPrice, 2); } - return bcadd($totalPostage, $totalRefundPrice, 2); + return [bcadd($totalPostage, $totalRefundPrice, 2), $consumptionTotal]; } public function getRefundsTotalPrice($order, $products) @@ -276,28 +285,15 @@ class StoreRefundOrderRepository extends BaseRepository $productRefundPrices = app()->make(StoreRefundProductRepository::class)->userRefundPrice($products->column('order_product_id')); $product = $products[0]; $productRefundPrice = $productRefundPrices[$product['order_product_id']] ?? []; - [$realPrice, $realPriceTotal] = $this->calculate($order['total_price'], $order['pay_price'], $product['product_price'], 0); + $storeConsumptionUserDao = new StoreConsumptionUserDao(); + $storeConsumptionUserDao->orderTotalPrice = $order['total_price']; + $storeConsumptionUserDao->orderPayPrice = $order['pay_price']; + $storeConsumptionUserDao->orderProductPrice = $product['product_price']; + [$realPrice, $realPriceTotal, $consumptionPrice] = $storeConsumptionUserDao->calculate(); $total_refund_price = bcsub($realPrice, bcsub($productRefundPrice['refund_price'] ?? 0, $productRefundPrice['refund_postage'] ?? 0, 2), 2); $postage_price = (!$order->status || $order->status == 9) ? bcsub($product['postage_price'], $productRefundPrice['refund_postage'] ?? 0, 2) : 0; - return compact('total_refund_price', 'postage_price'); - } - - /** - * 计算退款金额 - * @param float $orderTotalPrice 订单总金额 - * @param float $orderPayPrice 订单实付金额 - * @param float $productPrice 商品总金额 - * @param float $realPriceTotal 实付金额合计 - * @param bool $isLast 是否最后一条 - * @return array - */ - public function calculate($orderTotalPrice, $orderPayPrice, $productPrice, $realPriceTotal, $isLast = false) - { - $rate = bcdiv($productPrice, $orderTotalPrice, 5); - $realPrice = $isLast ? bcsub($orderPayPrice, $realPriceTotal, 2) : bcmul($orderPayPrice, $rate, 2); - $realPriceTotal = bcadd($realPriceTotal, $realPrice, 2); - return [$realPrice, $realPriceTotal]; + return compact('total_refund_price', 'postage_price', 'consumptionPrice'); } /** @@ -331,7 +327,8 @@ class StoreRefundOrderRepository extends BaseRepository $totalPostage = 0; $refundProduct = []; $refund_order_id = 0; - foreach ($products as $product) { + $$consumptionTotal = 0; + foreach ($products as $k => $product) { $productRefundPrice = $productRefundPrices[$product['order_product_id']] ?? []; if ($product['extension_one'] > 0) $total_extension_one = bcadd($total_extension_one, bcmul($product['refund_num'], $product['extension_one'], 2), 2); @@ -341,9 +338,18 @@ class StoreRefundOrderRepository extends BaseRepository $totalRefundNum += $product['refund_num']; $refundPrice = 0; //计算可退金额 - if ($product['product_price'] > 0) { - $refundPrice = bcsub($product['product_price'], bcsub($productRefundPrice['refund_price'] ?? 0, $productRefundPrice['refund_postage'] ?? 0, 2), 2); + if ($order['total_price'] > 0) { + $isLast = count($products->toArray()) == ($k + 1); + $storeConsumptionUserDao = new StoreConsumptionUserDao(); + $storeConsumptionUserDao->orderTotalPrice = $order['total_price']; + $storeConsumptionUserDao->orderPayPrice = $order['pay_price']; + $storeConsumptionUserDao->orderProductPrice = $product['product_price']; + $storeConsumptionUserDao->realPriceTotal = $totalRefundPrice; + $storeConsumptionUserDao->isLast = $isLast; + [$refundPrice, $totalRefundPrice, $consumptionPrice] = $storeConsumptionUserDao->calculate(); + $consumptionTotal = bcadd($consumptionTotal, $consumptionPrice, 2); } + $platform_refund_price = 0; //计算退的平台优惠券金额 if ($product['platform_coupon_price'] > 0) { @@ -355,7 +361,6 @@ class StoreRefundOrderRepository extends BaseRepository } $totalPostage = bcadd($totalPostage, $postagePrice, 2); - $totalRefundPrice = bcadd($totalRefundPrice, $refundPrice, 2); $totalPlatformRefundPrice = bcadd($totalPlatformRefundPrice, $platform_refund_price, 2); $totalIntegral = bcadd($totalIntegral, $integral, 2); @@ -366,6 +371,7 @@ class StoreRefundOrderRepository extends BaseRepository 'platform_refund_price' => $platform_refund_price, 'refund_integral' => $integral, 'refund_price' => $refundPrice, + 'refund_consumption' => $consumptionPrice, 'refund_postage' => $postagePrice, ]; } @@ -377,6 +383,7 @@ class StoreRefundOrderRepository extends BaseRepository $data['extension_one'] = $total_extension_one; $data['extension_two'] = $total_extension_two; $data['refund_price'] = bcadd($totalPostage, $totalRefundPrice, 2); + $data['refund_consumption'] = $consumptionTotal; $data['integral'] = $totalIntegral; $data['platform_refund_price'] = $totalPlatformRefundPrice; $data['refund_postage'] = $totalPostage; diff --git a/app/controller/api/store/order/StoreRefundOrder.php b/app/controller/api/store/order/StoreRefundOrder.php index 7d68afac..7582f58c 100755 --- a/app/controller/api/store/order/StoreRefundOrder.php +++ b/app/controller/api/store/order/StoreRefundOrder.php @@ -91,16 +91,17 @@ class StoreRefundOrder extends BaseController if (count($product) != count($ids)) return app('json')->fail('请选择正确的退款商品'); if ($type == 2) { - $total_refund_price = $this->repository->getOrderRefundPrice($order, $product); + [$total_refund_price, $consumptionPrice] = $this->repository->getOrderRefundPrice($order, $product); $postage_price = 0; } else { $data = $this->repository->getRefundTotalPrice($order, $product); $total_refund_price = (float)$data['total_refund_price']; $postage_price = (float)$data['postage_price']; + $consumptionPrice = (float)$data['consumptionPrice']; } $status = (!$order->status || $order->status == 9) ? 0 : $order->status; $activity_type = $order->activity_type; - return app('json')->success(compact('activity_type', 'total_refund_price', 'product', 'postage_price', 'status')); + return app('json')->success(compact('activity_type', 'total_refund_price', 'product', 'postage_price', 'status', 'consumptionPrice')); } /**