ldc-boutique-cnc-sable/app/Jobs/SendReminderMail.php
2026-10-27 09:58:12 +01:00

96 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Repositories\EmployeeRepository;
use App\Contracts\Repositories\OrderRepository;
use App\Dto\OrderReminderDto;
use App\Dto\OrderReminderLineDto;
use App\Models\Employee;
use App\Models\Order;
use App\Notifications\OrderTodayReminderNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Money\MoneyFormatter;
class SendReminderMail implements ShouldQueue
{
use Queueable;
public function __construct(
private readonly MoneyFormatter $moneyFormatter
) {
}
/**
* Execute the job.
*
* @return mixed
*/
public function handle(
OrderRepository $orderRepository,
EmployeeRepository $employeeRepository
) {
$orders = $orderRepository->getOrdersWithLinesAndShopForPickupDay(now());
$matricules = collect($orders)
->map(fn ($order) => $order->employee->matricule)
->filter()
->uniqueValues();
/** @var array<int, \Domain\Models\ValueObjects\Matricule> $matriculesArray */
$matriculesArray = $matricules->values()->toArray();
$employees = collect($employeeRepository->getWithCustomerWhereMatriculeIn($matriculesArray));
foreach ($orders as $order) {
$matricule = $order->employee->matricule->value;
if (! $employees->has($matricule)) {
continue;
}
$employee = $employees->get($matricule);
if (! $employee) {
continue;
}
$this->notifyEmployee(
$employee,
$this->toDto($order, $employee)
);
}
}
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 : ''
);
}
}