Faza 2 (din continuare): Email notifications
4 Mailables auto-trigger pe model events: - WorkOrderReadyMail: la WO.status → 'ready', către client.email • Atașat PDF fișa lucru (via WorkOrderPdfService) • Total/achitat/rest, recomandări (warning box) - PaymentReceivedMail: la Payment::created, confirmare cu sumă/metodă/ref - AppointmentConfirmedMail: la Appointment::created status='scheduled' - ServiceReminderMail: dispatch manual (vehicle, type=itp/oil/general, note) Layout email branded (resources/views/emails/layout.blade.php): - Header cu logo tenant + theme_color border-bottom - Footer cu telefon/email/disclaimer - Stiluri inline (compatibil tot mail client) Settings page extins cu 4 toggle: - 'Mașina e gata de ridicat' - 'Confirmare plată primită' - 'Programare confirmată' - 'Reminder ITP / revizie' Salvate în companies.settings.notify (JSON), default true. NotificationDispatcher service centralizat: - Verifică isEnabled() pe settings.notify[$key] - Skip dacă client n-are email - Try/catch + Log::warning pe eșec (nu crapă request-ul) Mailables folosesc UsesTenantBranding trait pentru context unitar. Test prin Mailpit: https://mailpit.service.mir.md (capturează toate).
This commit is contained in:
@@ -34,6 +34,7 @@ class Settings extends Page
|
||||
$settings = (array) ($company->settings ?? []);
|
||||
|
||||
// Filament v5: fill via $this->form->fill() (initializes the schema state).
|
||||
$notify = (array) ($settings['notify'] ?? []);
|
||||
$this->form->fill([
|
||||
'display_name' => $company->display_name ?? $company->name,
|
||||
'city' => $company->city,
|
||||
@@ -45,6 +46,10 @@ class Settings extends Page
|
||||
'labor_rate' => $settings['labor_rate'] ?? 400,
|
||||
'services' => isset($settings['services']) ? implode(', ', (array) $settings['services']) : '',
|
||||
'cars' => isset($settings['cars']) ? implode(', ', (array) $settings['cars']) : '',
|
||||
'notify_wo_ready' => $notify['wo_ready'] ?? true,
|
||||
'notify_payment' => $notify['payment'] ?? true,
|
||||
'notify_appointment' => $notify['appointment'] ?? true,
|
||||
'notify_reminder' => $notify['reminder'] ?? true,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -106,6 +111,15 @@ class Settings extends Page
|
||||
->maxSize(512)
|
||||
->helperText('PNG/ICO, max 512 KB.'),
|
||||
]),
|
||||
Schemas\Components\Section::make('Notificări email')
|
||||
->description('Activează / dezactivează emailurile auto către clienți.')
|
||||
->columns(2)
|
||||
->schema([
|
||||
Forms\Components\Toggle::make('notify_wo_ready')->label('Mașina e gata de ridicat')->default(true),
|
||||
Forms\Components\Toggle::make('notify_payment')->label('Confirmare plată primită')->default(true),
|
||||
Forms\Components\Toggle::make('notify_appointment')->label('Programare confirmată')->default(true),
|
||||
Forms\Components\Toggle::make('notify_reminder')->label('Reminder ITP / revizie')->default(true),
|
||||
]),
|
||||
])
|
||||
->statePath('data');
|
||||
}
|
||||
@@ -131,6 +145,12 @@ class Settings extends Page
|
||||
'labor_rate' => (float) ($data['labor_rate'] ?? 400),
|
||||
'services' => array_values(array_filter(array_map('trim', explode(',', (string) ($data['services'] ?? ''))))),
|
||||
'cars' => array_values(array_filter(array_map('trim', explode(',', (string) ($data['cars'] ?? ''))))),
|
||||
'notify' => [
|
||||
'wo_ready' => (bool) ($data['notify_wo_ready'] ?? true),
|
||||
'payment' => (bool) ($data['notify_payment'] ?? true),
|
||||
'appointment' => (bool) ($data['notify_appointment'] ?? true),
|
||||
'reminder' => (bool) ($data['notify_reminder'] ?? true),
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Mail\Concerns\UsesTenantBranding;
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Tenant\Appointment;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class AppointmentConfirmedMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels, UsesTenantBranding;
|
||||
|
||||
public function __construct(public Appointment $appointment, Company $company)
|
||||
{
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: "[{$this->company->name}] Programare confirmată — " . $this->appointment->date?->format('d.m.Y'),
|
||||
from: new \Illuminate\Mail\Mailables\Address(
|
||||
config('mail.from.address'),
|
||||
$this->company->display_name ?? $this->company->name,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.appointment-confirmed',
|
||||
with: array_merge($this->buildBrandingContext(), [
|
||||
'appointment' => $this->appointment,
|
||||
'client' => $this->appointment->client,
|
||||
'vehicle' => $this->appointment->vehicle,
|
||||
'master' => $this->appointment->master,
|
||||
'post' => $this->appointment->post,
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail\Concerns;
|
||||
|
||||
use App\Models\Central\Company;
|
||||
|
||||
trait UsesTenantBranding
|
||||
{
|
||||
public Company $company;
|
||||
|
||||
public function buildBrandingContext(): array
|
||||
{
|
||||
return [
|
||||
'companyName' => $this->company->display_name ?? $this->company->name,
|
||||
'themeColor' => $this->company->settings['theme_color'] ?? '#3B82F6',
|
||||
'phone' => $this->company->phone,
|
||||
'email' => $this->company->email,
|
||||
'city' => $this->company->city,
|
||||
'logoUrl' => $this->company->getLogoUrl(),
|
||||
'tenantUrl' => $this->company->url('/app'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Mail\Concerns\UsesTenantBranding;
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Tenant\Payment;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class PaymentReceivedMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels, UsesTenantBranding;
|
||||
|
||||
public function __construct(public Payment $payment, Company $company)
|
||||
{
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: "[{$this->company->name}] Confirmare plată — " . number_format((float) $this->payment->amount, 2, '.', ' ') . ' ' . ($this->company->settings['currency'] ?? 'MDL'),
|
||||
from: new \Illuminate\Mail\Mailables\Address(
|
||||
config('mail.from.address'),
|
||||
$this->company->display_name ?? $this->company->name,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.payment-received',
|
||||
with: array_merge($this->buildBrandingContext(), [
|
||||
'payment' => $this->payment,
|
||||
'client' => $this->payment->client,
|
||||
'workOrder' => $this->payment->workOrder,
|
||||
'currency' => $this->company->settings['currency'] ?? 'MDL',
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Mail\Concerns\UsesTenantBranding;
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Tenant\Vehicle;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ServiceReminderMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels, UsesTenantBranding;
|
||||
|
||||
public function __construct(
|
||||
public Vehicle $vehicle,
|
||||
public string $reminderType, // 'itp' / 'oil' / 'general'
|
||||
public ?string $note,
|
||||
Company $company,
|
||||
) {
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
$titles = ['itp' => 'Reminder ITP', 'oil' => 'Reminder schimb ulei', 'general' => 'Reminder revizie'];
|
||||
$title = $titles[$this->reminderType] ?? 'Reminder';
|
||||
|
||||
return new Envelope(
|
||||
subject: "[{$this->company->name}] {$title} — " . trim(($this->vehicle->make ?? '') . ' ' . ($this->vehicle->model ?? '')),
|
||||
from: new \Illuminate\Mail\Mailables\Address(
|
||||
config('mail.from.address'),
|
||||
$this->company->display_name ?? $this->company->name,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.service-reminder',
|
||||
with: array_merge($this->buildBrandingContext(), [
|
||||
'vehicle' => $this->vehicle,
|
||||
'client' => $this->vehicle->client,
|
||||
'reminderType' => $this->reminderType,
|
||||
'note' => $this->note,
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Mail\Concerns\UsesTenantBranding;
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Tenant\WorkOrder;
|
||||
use App\Services\WorkOrderPdfService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Attachment;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class WorkOrderReadyMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels, UsesTenantBranding;
|
||||
|
||||
public function __construct(public WorkOrder $workOrder, Company $company)
|
||||
{
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: "[{$this->company->name}] Mașina dvs. este gata — fișa {$this->workOrder->number}",
|
||||
from: new \Illuminate\Mail\Mailables\Address(
|
||||
config('mail.from.address'),
|
||||
$this->company->display_name ?? $this->company->name,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.work-order-ready',
|
||||
with: array_merge($this->buildBrandingContext(), [
|
||||
'workOrder' => $this->workOrder,
|
||||
'client' => $this->workOrder->client,
|
||||
'vehicle' => $this->workOrder->vehicle,
|
||||
'paid' => (float) $this->workOrder->payments->sum('amount'),
|
||||
'total' => (float) $this->workOrder->total,
|
||||
'currency' => $this->company->settings['currency'] ?? 'MDL',
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
public function attachments(): array
|
||||
{
|
||||
try {
|
||||
$pdf = app(WorkOrderPdfService::class)->generate($this->workOrder);
|
||||
return [
|
||||
Attachment::fromData(fn () => $pdf->output(), app(WorkOrderPdfService::class)->filename($this->workOrder))
|
||||
->withMime('application/pdf'),
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,4 +52,14 @@ class Appointment extends Model
|
||||
{
|
||||
return $this->belongsTo(Deal::class);
|
||||
}
|
||||
|
||||
/** Auto-send confirmation email when a 'scheduled' appointment is created. */
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::created(function (self $a) {
|
||||
if ($a->status === 'scheduled') {
|
||||
app(\App\Services\NotificationDispatcher::class)->appointmentConfirmed($a);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,5 +75,10 @@ class Payment extends Model
|
||||
|
||||
static::saved($sync);
|
||||
static::deleted($sync);
|
||||
|
||||
// Auto-send confirmation email on new payment.
|
||||
static::created(function (self $payment) {
|
||||
app(\App\Services\NotificationDispatcher::class)->paymentReceived($payment);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,4 +108,18 @@ class WorkOrder extends Model
|
||||
->count();
|
||||
return sprintf('WO-%s-%04d', $year, $count + 1);
|
||||
}
|
||||
|
||||
/** Auto-send 'ready' email when status transitions to 'ready'. */
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::updated(function (self $wo) {
|
||||
if (
|
||||
$wo->wasChanged('status')
|
||||
&& $wo->status === 'ready'
|
||||
&& $wo->getOriginal('status') !== 'ready'
|
||||
) {
|
||||
app(\App\Services\NotificationDispatcher::class)->workOrderReady($wo);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Mail\AppointmentConfirmedMail;
|
||||
use App\Mail\PaymentReceivedMail;
|
||||
use App\Mail\ServiceReminderMail;
|
||||
use App\Mail\WorkOrderReadyMail;
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Tenant\Appointment;
|
||||
use App\Models\Tenant\Payment;
|
||||
use App\Models\Tenant\Vehicle;
|
||||
use App\Models\Tenant\WorkOrder;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
/**
|
||||
* Centralizes outbound email notifications, with per-tenant feature toggles
|
||||
* stored in companies.settings.notify (e.g., 'wo_ready' => true).
|
||||
*
|
||||
* Usage:
|
||||
* app(NotificationDispatcher::class)->workOrderReady($workOrder);
|
||||
*/
|
||||
class NotificationDispatcher
|
||||
{
|
||||
public function workOrderReady(WorkOrder $wo): bool
|
||||
{
|
||||
$company = $this->companyFor($wo);
|
||||
if (! $this->isEnabled($company, 'wo_ready')) return false;
|
||||
|
||||
$email = $wo->client?->email;
|
||||
if (! $email) return false;
|
||||
|
||||
try {
|
||||
Mail::to($email)->send(new WorkOrderReadyMail($wo, $company));
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('workOrderReady mail failed', ['wo' => $wo->id, 'err' => $e->getMessage()]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function paymentReceived(Payment $payment): bool
|
||||
{
|
||||
$company = $this->companyFor($payment);
|
||||
if (! $this->isEnabled($company, 'payment')) return false;
|
||||
|
||||
$email = $payment->client?->email;
|
||||
if (! $email) return false;
|
||||
|
||||
try {
|
||||
Mail::to($email)->send(new PaymentReceivedMail($payment, $company));
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('paymentReceived mail failed', ['payment' => $payment->id, 'err' => $e->getMessage()]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function appointmentConfirmed(Appointment $a): bool
|
||||
{
|
||||
$company = $this->companyFor($a);
|
||||
if (! $this->isEnabled($company, 'appointment')) return false;
|
||||
|
||||
$email = $a->client?->email;
|
||||
if (! $email) return false;
|
||||
|
||||
try {
|
||||
Mail::to($email)->send(new AppointmentConfirmedMail($a, $company));
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('appointmentConfirmed mail failed', ['appt' => $a->id, 'err' => $e->getMessage()]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function serviceReminder(Vehicle $v, string $type = 'general', ?string $note = null): bool
|
||||
{
|
||||
$company = $this->companyFor($v);
|
||||
if (! $this->isEnabled($company, 'reminder')) return false;
|
||||
|
||||
$email = $v->client?->email;
|
||||
if (! $email) return false;
|
||||
|
||||
try {
|
||||
Mail::to($email)->send(new ServiceReminderMail($v, $type, $note, $company));
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('serviceReminder mail failed', ['vehicle' => $v->id, 'err' => $e->getMessage()]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function companyFor($model): Company
|
||||
{
|
||||
return Company::withoutGlobalScopes()->findOrFail($model->company_id);
|
||||
}
|
||||
|
||||
protected function isEnabled(Company $company, string $key): bool
|
||||
{
|
||||
$settings = (array) ($company->settings ?? []);
|
||||
$notify = (array) ($settings['notify'] ?? []);
|
||||
// default: enabled (toate notificările active by default)
|
||||
return ($notify[$key] ?? true) === true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user