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, ]); }