6
0
Fork 0

调整后台减免金额

release
vine_liutk 2022-04-14 13:28:26 +08:00
parent e58bc41af7
commit 7379446690
1 changed files with 32 additions and 2 deletions

View File

@ -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,