ldc-boutique-cnc-sable/domain/Services/RegateService.php

80 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Domain\Services;
use Carbon\CarbonImmutable;
use Domain\Events\Dto\RegateOrderCsvGenerated;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Ports\Services\CsvGenerator;
use Shared\DDD\DomainEventDispatcher;
class RegateService
{
/** @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 DomainEventDispatcher $dispatcher
) {
}
/**
* @param \Domain\Events\Dto\OrderPlacedLine[] $lines
*/
public function processOrder(
OrderReference $reference,
Matricule $employeeMatricule,
CarbonImmutable $pickupDay,
array $lines
): void {
$rows = [];
foreach ($lines as $line) {
array_push(
$rows,
[
(string) $line->productIncoId->value,
$employeeMatricule->value,
(string) $line->productIncoId->value,
'commande ferme',
(string) $line->productOrderUnit->value,
$line->productOrderUnit->toRegateCode(),
2026-06-14 21:32:07 +02:00
'',
$pickupDay->format('d/m/Y'),
(string) $line->quantity,
(string) $reference,
2026-06-14 21:32:07 +02:00
]
);
}
$csvContent = $this->csvGenerator->generate($this->header, $rows);
$now = CarbonImmutable::now();
$fileName = sprintf(
'BTQ-%s-%s%s.csv',
$reference,
$now->format('Ymd_His'),
\microtime(true)
);
$this->dispatcher->dispatch(
new RegateOrderCsvGenerated($csvContent, $fileName)
);
}
}