From 7379446690da1af5f0b725ab6ab1789fa43d149e Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Thu, 14 Apr 2022 13:28:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=90=8E=E5=8F=B0=E5=87=8F?= =?UTF-8?q?=E5=85=8D=E9=87=91=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Services/OrderService.php | 34 +++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/app/Admin/Services/OrderService.php b/app/Admin/Services/OrderService.php index fd2b78b1..b4bb07b0 100644 --- a/app/Admin/Services/OrderService.php +++ b/app/Admin/Services/OrderService.php @@ -7,6 +7,7 @@ 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 @@ -19,15 +20,44 @@ class OrderService public function adminReduceOrder(Order $order, int $reduceAmount) { if ($order->isPending()) { + $needReduceAmount = $order->total_amount - $reduceAmount + $order->reduced_amount; $res = $order->where('updated_at', $order->updated_at)->update([ - 'reduced_amount' => $order->total_amount - $reduceAmount + $order->reduced_amount, + 'reduced_amount' => $needReduceAmount, 'total_amount' => $reduceAmount, ]); if ($res === 0) { throw new BizException('订单已发生改变'); } - //更新订单商品优惠金额-todo + //更新订单商品优惠金额 + //分解到订单中除赠品外的商品 + $orderProducts = $order->products; + $needReduceOrderProducts = []; + foreach ($orderProducts as $orderProduct) { + if (!$orderProduct->isGift()) { + $needReduceOrderProducts[$orderProduct->id] = $orderProduct->total_amount; + } + } + + $i = count($needReduceOrderProducts); + $totalAmount = array_sum($needReduceOrderProducts); + foreach ($needReduceOrderProducts as $key => $reduceOrderProductAmount) { + $i --; + + if ($i > 0) { + $amount = (int) bcdiv(bcmul($needReduceAmount, $reduceOrderProductAmount, 0), $totalAmount, 2); + $needReduceAmount -= $amount; + $totalAmount -= $reduceOrderProductAmount; + } else { + $amount = $needReduceAmount; + } + + //更新订单商品的金额 + OrderProduct::where('id', $key)->update([ + 'reduced_amount' => $amount, + 'total_amount' => $reduceOrderProductAmount - $amount, + ]); + } OrderLog::create([ 'order_id'=>$order->id,