stock_current; $offer->stock_current = $offer->stock_current - $item['quantity']; if ($offer->stock_current <= 0) { $offer->stock_current = 0; } $saved = $offer->save(); if ($saved) { self::checkStockAlert($offer, $previousStock); } return $saved; } public static function checkStockAlert($offer, $previousStock) { $threshold = (float) $offer->minimum_ondemand; if ($threshold <= 0) { return; } $crossedThreshold = $previousStock > $threshold && $offer->stock_current <= $threshold; if (! $crossedThreshold) { return; } try { $offer->load('article'); Mail::to('commande@jardinenvie.com') ->send(new AlerteStock($offer)); Log::info('Stock alert email sent', [ 'offer_id' => $offer->id, 'article' => $offer->article->name ?? $offer->article_id, 'stock_current' => $offer->stock_current, 'threshold' => $threshold, ]); } catch (\Throwable $e) { Log::error('Failed to send stock alert email', [ 'offer_id' => $offer->id, 'stock_current' => $offer->stock_current, 'exception' => $e->getMessage(), ]); } } public static function getStockCurrent($id) { return Offers::getField($id, 'stock_current'); } public static function getAlerts() { return Offer::active()->where('stock_current', '<', 15)->get(); } public static function countAlerts() { return Offer::active()->where('stock_current', '<', 15)->count(); } }