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.
This commit is contained in:
Valentin Lab
2026-02-13 07:13:56 +01:00
parent cbd8e33f3b
commit 936c9473a7
3 changed files with 162 additions and 1 deletions

43
app/Mail/AlerteStock.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Mail;
use App\Models\Core\Mail\MailTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Spatie\MailTemplates\TemplateMailable;
class AlerteStock extends TemplateMailable
{
use Queueable, SerializesModels;
public $subject;
public $article;
public $offre;
public $stock_restant;
public $seuil;
protected static $templateModelClass = MailTemplate::class;
public function __construct($offer)
{
$this->article = $offer->article->title ?? 'Article #'.$offer->article_id;
$this->offre = $offer->id;
$this->stock_restant = $offer->stock_current;
$this->seuil = $offer->minimum_ondemand;
}
public function envelope()
{
return new Envelope(
from: new Address('boutique@jardinenvie.com', 'Jardin\'en\'Vie'),
subject: $this->subject,
);
}
}