diff --git a/app/Http/Controllers/Admin/Shop/OrderController.php b/app/Http/Controllers/Admin/Shop/OrderController.php index f47892ad..0257ba8a 100644 --- a/app/Http/Controllers/Admin/Shop/OrderController.php +++ b/app/Http/Controllers/Admin/Shop/OrderController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin\Shop; use App\Datatables\Admin\Shop\OrdersDataTable; use App\Http\Controllers\Controller; +use App\Repositories\Shop\OfferStocks; use App\Repositories\Shop\OrderMails; use App\Repositories\Shop\Orders; use Illuminate\Http\Request; @@ -36,6 +37,29 @@ class OrderController extends Controller public function store(Request $request) { + $previousStatus = null; + if ($request->has('id')) { + $previousStatus = Orders::get($request->input('id'))->status; + } + + $newStatus = $request->input('status'); + + // Vérifier le stock avant de dés-annuler une commande + if ($previousStatus == 4 && $newStatus != 4) { + $insufficients = OfferStocks::checkStockForOrder($request->input('id')); + if (! empty($insufficients)) { + $messages = []; + foreach ($insufficients as $item) { + $messages[] = $item['name'].' (stock : '.$item['stock'].', requis : '.$item['quantity'].')'; + } + + return redirect()->back()->withInput()->with( + 'growl', + ['Impossible de réactiver cette commande, stock insuffisant : '.implode(' ; ', $messages), 'danger'] + ); + } + } + $order = Orders::store($request->all()); if ($order->wasChanged('status')) { switch ($order->status) { @@ -45,7 +69,13 @@ class OrderController extends Controller case 2: OrderMails::sendShipping($order->id); break; + case 4: + OfferStocks::restoreStock($order->id); + break; default: + if ($previousStatus == 4) { + OfferStocks::decreaseStockForOrder($order->id); + } } } diff --git a/app/Repositories/Shop/OfferStocks.php b/app/Repositories/Shop/OfferStocks.php index b786debd..737287dc 100644 --- a/app/Repositories/Shop/OfferStocks.php +++ b/app/Repositories/Shop/OfferStocks.php @@ -60,6 +60,52 @@ class OfferStocks } } + public static function restoreStock($orderId) + { + $details = \App\Models\Shop\OrderDetail::where('order_id', $orderId)->get(); + + foreach ($details as $detail) { + $offer = Offers::get($detail->offer_id); + if ($offer) { + $offer->stock_current = $offer->stock_current + $detail->quantity; + $offer->save(); + } + } + } + + public static function checkStockForOrder($orderId) + { + $details = \App\Models\Shop\OrderDetail::where('order_id', $orderId)->get(); + $insufficients = []; + + foreach ($details as $detail) { + $offer = Offers::get($detail->offer_id); + if ($offer && $offer->stock_current < $detail->quantity) { + $offer->load('article'); + $insufficients[] = [ + 'name' => $offer->article->name ?? 'Offre #'.$offer->id, + 'stock' => $offer->stock_current, + 'quantity' => $detail->quantity, + ]; + } + } + + return $insufficients; + } + + public static function decreaseStockForOrder($orderId) + { + $details = \App\Models\Shop\OrderDetail::where('order_id', $orderId)->get(); + + foreach ($details as $detail) { + $offer = Offers::get($detail->offer_id); + if ($offer) { + $offer->stock_current = max(0, $offer->stock_current - $detail->quantity); + $offer->save(); + } + } + } + public static function getStockCurrent($id) { return Offers::getField($id, 'stock_current');