ldc-cocotte/app/Jobs/SendReminderMail.php

90 lines
2.8 KiB
PHP
Raw Normal View History

2023-07-10 15:09:06 +00:00
<?php
2026-06-14 21:32:07 +02:00
declare(strict_types=1);
2023-07-10 15:09:06 +00:00
namespace App\Jobs;
2026-06-14 21:32:07 +02:00
use App\Dto\OrderReminderDto;
use App\Dto\OrderReminderLineDto;
use App\Models\Employee;
2023-07-10 15:09:06 +00:00
use App\Models\Order;
2026-06-14 21:32:07 +02:00
use App\Notifications\OrderTodayReminderNotification;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\Matricule;
2023-07-10 15:09:06 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
2026-06-14 21:32:07 +02:00
use Illuminate\Foundation\Queue\Queueable;
use Money\MoneyFormatter;
2023-07-10 15:09:06 +00:00
class SendReminderMail implements ShouldQueue
{
use Queueable;
2026-06-14 21:32:07 +02:00
public function __construct(
private readonly MoneyFormatter $moneyFormatter
) {
}
2023-07-10 15:09:06 +00:00
public function handle(): void
{
$orders = Order::query()
->with(['lines', 'shop'])
->whereDate('pickup_day', now()->format('Y-m-d'))
->where('status', OrderStatus::VALIDATED)
->get();
2026-06-14 21:32:07 +02:00
$matricules = $orders
->map(fn (Order $order): Matricule => $order->employee->matricule)
->unique();
2023-07-10 15:09:06 +00:00
$employees = Employee::query()
->with('customer')
->whereIn('matricule', $matricules)
->get()
->keyBy(fn (Employee $employee): string => $employee->matricule->value);
2026-06-14 21:32:07 +02:00
foreach ($orders as $order) {
$employee = $employees->get($order->employee->matricule->value);
2026-06-14 21:32:07 +02:00
if (! $employee instanceof Employee) {
2026-06-14 21:32:07 +02:00
continue;
}
$this->notifyEmployee(
$employee,
$this->toDto($order, $employee)
);
2023-07-10 15:09:06 +00:00
}
}
2026-06-14 21:32:07 +02:00
private function notifyEmployee(Employee $employee, OrderReminderDto $dto): void
{
$employee->notify(new OrderTodayReminderNotification($dto));
}
private function toDto(Order $order, Employee $employee): OrderReminderDto
{
$linesDto = [];
foreach ($order->lines as $line) {
$linesDto[] = new OrderReminderLineDto(
name: $line->product->name ?? '',
quantity: $line->quantity,
priceKgTtc: $this->moneyFormatter->format($line->product->priceKgTtc),
totalTtc: $this->moneyFormatter->format($line->total_ttc),
);
}
return new OrderReminderDto(
firstname: $employee->prenom,
lastname: $employee->nom,
reference: (string) $order->reference,
total: $this->moneyFormatter->format($order->total),
pickupDayFormatted: $order->pickup_day ? $order->pickup_day->translatedFormat('l j F Y') : '',
lines: $linesDto,
shopName: $order->shop ? $order->shop->name : '',
shopAddress: $order->shop ? $order->shop->address : '',
shopPostcode: $order->shop ? $order->shop->postcode : '',
shopCity: $order->shop ? $order->shop->city : ''
);
}
2023-07-10 15:09:06 +00:00
}