94 lines
2.6 KiB
PHP
94 lines
2.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Mail\RegateOrderMail;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
use Domain\Exceptions\OrderTunnel\RegateExportFailedException;
|
||
|
|
use Domain\Models\ValueObjects\Matricule;
|
||
|
|
use Domain\Models\ValueObjects\OrderReference;
|
||
|
|
use Domain\Ports\Services\CsvGenerator;
|
||
|
|
use Domain\Ports\Services\RegateService;
|
||
|
|
use Illuminate\Contracts\Mail\Mailer;
|
||
|
|
use Illuminate\Mail\Mailables\Address;
|
||
|
|
use Illuminate\Support\Facades\Config;
|
||
|
|
|
||
|
|
final class MailRegateService implements RegateService
|
||
|
|
{
|
||
|
|
private readonly Address $regateEmail;
|
||
|
|
|
||
|
|
/** @var string[] */
|
||
|
|
private array $header = [
|
||
|
|
'CodeArt',
|
||
|
|
'Client livré code',
|
||
|
|
'ean13',
|
||
|
|
'Type de commande',
|
||
|
|
'Unité Elem. Appro',
|
||
|
|
'mode de commande',
|
||
|
|
'Pcb Art',
|
||
|
|
'Date livraison',
|
||
|
|
'Qté commandée',
|
||
|
|
'Num Cde Client',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
private readonly CsvGenerator $csvGenerator,
|
||
|
|
private readonly Mailer $mailer,
|
||
|
|
) {
|
||
|
|
if (! Config::has('mail.to.regate')) {
|
||
|
|
throw new \UnexpectedValueException('Regate email address is not configured.');
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->regateEmail = new Address(Config::string('mail.to.regate'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Domain\Events\Dto\OrderPlacedLine[] $lines
|
||
|
|
*
|
||
|
|
* @throws \Domain\Exceptions\OrderTunnel\RegateExportFailedException
|
||
|
|
*/
|
||
|
|
#[\Override]
|
||
|
|
public function processOrder(
|
||
|
|
OrderReference $reference,
|
||
|
|
Matricule $employeeMatricule,
|
||
|
|
CarbonImmutable $pickupDay,
|
||
|
|
array $lines,
|
||
|
|
): void {
|
||
|
|
$rows = [];
|
||
|
|
|
||
|
|
foreach ($lines as $line) {
|
||
|
|
$rows[] = [
|
||
|
|
(string) $line->productIncoId->value,
|
||
|
|
$employeeMatricule->value,
|
||
|
|
(string) $line->productIncoId->value,
|
||
|
|
'commande ferme',
|
||
|
|
(string) $line->productOrderUnit->value,
|
||
|
|
$line->productOrderUnit->toRegateCode(),
|
||
|
|
'',
|
||
|
|
$pickupDay->format('d/m/Y'),
|
||
|
|
(string) $line->quantity,
|
||
|
|
(string) $reference,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
$csvContent = $this->csvGenerator->generate($this->header, $rows);
|
||
|
|
|
||
|
|
$now = CarbonImmutable::now();
|
||
|
|
$fileName = sprintf(
|
||
|
|
'BTQ-%s-%s%s.csv',
|
||
|
|
$reference,
|
||
|
|
$now->format('Ymd_His'),
|
||
|
|
\microtime(true)
|
||
|
|
);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$this->mailer->to($this->regateEmail)
|
||
|
|
->send(new RegateOrderMail($csvContent, $fileName));
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
throw new RegateExportFailedException($e->getMessage(), $e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|