From b8a9bab95d0c3d92f4a0f15441344374db595965 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Thu, 14 Apr 2022 14:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=87=8F=E5=85=8D=E4=BC=98?= =?UTF-8?q?=E6=83=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Services/OrderService.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/Admin/Services/OrderService.php b/app/Admin/Services/OrderService.php index b4bb07b0..54b1e04b 100644 --- a/app/Admin/Services/OrderService.php +++ b/app/Admin/Services/OrderService.php @@ -7,7 +7,6 @@ use App\Events\OrderPaid; use App\Exceptions\BizException; use App\Models\Order; use App\Models\OrderLog; -use App\Models\OrderProduct; use App\Services\OrderService as EndpointOrderService; class OrderService @@ -33,29 +32,32 @@ class OrderService //分解到订单中除赠品外的商品 $orderProducts = $order->products; $needReduceOrderProducts = []; + $totalAmount = 0; foreach ($orderProducts as $orderProduct) { if (!$orderProduct->isGift()) { - $needReduceOrderProducts[$orderProduct->id] = $orderProduct->total_amount; + $needReduceOrderProducts[] = $orderProduct; + $totalAmount += $orderProduct->total_amount; } } $i = count($needReduceOrderProducts); - $totalAmount = array_sum($needReduceOrderProducts); - foreach ($needReduceOrderProducts as $key => $reduceOrderProductAmount) { + foreach ($needReduceOrderProducts as $reduceOrderProduct) { $i --; if ($i > 0) { - $amount = (int) bcdiv(bcmul($needReduceAmount, $reduceOrderProductAmount, 0), $totalAmount, 2); + $amount = (int) bcdiv(bcmul($needReduceAmount, $reduceOrderProduct->total_amount, 0), $totalAmount, 2); $needReduceAmount -= $amount; - $totalAmount -= $reduceOrderProductAmount; + $totalAmount -= $reduceOrderProduct->total_amount; } else { $amount = $needReduceAmount; } + $newTotalAmount = $reduceOrderProduct->total_amount + $reduceOrderProduct->reduced_amount - $amount; + //更新订单商品的金额 - OrderProduct::where('id', $key)->update([ + $reduceOrderProduct->update([ 'reduced_amount' => $amount, - 'total_amount' => $reduceOrderProductAmount - $amount, + 'total_amount'=> $newTotalAmount, ]); }