new: restore and manage stock on order cancellation

Add ``restoreStock()``, ``decreaseStockForOrder()``, and
``checkStockForOrder()`` to ``OfferStocks``. When an order is cancelled
(status 4), stock is restored. When un-cancelling, stock availability is
checked first—insufficient stock blocks the transition with an error
message—then decremented.
This commit is contained in:
Valentin Lab
2026-02-20 11:38:21 +01:00
parent 7e9c3c6196
commit 5c10645af7
2 changed files with 76 additions and 0 deletions

View File

@@ -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');