Files
opensem/app/Repositories/Shop/OfferStocks.php
Valentin Lab 936c9473a7 new: send stock alert email when offer stock crosses threshold
When a purchase causes an offer's ``stock_current`` to drop to or below
its ``minimum_ondemand`` threshold, an email is sent to
``commande@jardinenvie.com`` using an editable mail template (Spatie
``MailTemplate``).

The check runs in ``OfferStocks::decreaseStock()`` after updating stock.
Only threshold-crossing events trigger the alert (not every low-stock
sale).  Failures are caught and logged to avoid disrupting the order
flow.
2026-02-13 07:13:56 +01:00

78 lines
2.1 KiB
PHP

<?php
namespace App\Repositories\Shop;
use App\Mail\AlerteStock;
use App\Models\Shop\Offer;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class OfferStocks
{
public static function decreaseStock($item)
{
$offer = Offers::get($item['offer_id']);
$previousStock = $offer->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();
}
}