|
|
|
@@ -18,13 +18,15 @@ use PhpOffice\PhpSpreadsheet\Style\Border;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @implements ProviderInterface<Response>
|
|
|
|
* @implements ProviderInterface<Response>
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
final class BovineInventoryExportProvider implements ProviderInterface
|
|
|
|
final readonly class BovineInventoryExportProvider implements ProviderInterface
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private const FARM_NAME = 'FERME SCEA LES NAUDS';
|
|
|
|
private const FARM_NAME = 'FERME SCEA LES NAUDS';
|
|
|
|
|
|
|
|
|
|
|
|
@@ -70,6 +72,7 @@ final class BovineInventoryExportProvider implements ProviderInterface
|
|
|
|
public function __construct(
|
|
|
|
public function __construct(
|
|
|
|
private BovineRepository $bovineRepository,
|
|
|
|
private BovineRepository $bovineRepository,
|
|
|
|
private RequestStack $requestStack,
|
|
|
|
private RequestStack $requestStack,
|
|
|
|
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
) {}
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response
|
|
|
|
@@ -253,7 +256,16 @@ final class BovineInventoryExportProvider implements ProviderInterface
|
|
|
|
// Lignes de données
|
|
|
|
// Lignes de données
|
|
|
|
$rowNumber = 5;
|
|
|
|
$rowNumber = 5;
|
|
|
|
foreach ($bovines as $bovine) {
|
|
|
|
foreach ($bovines as $bovine) {
|
|
|
|
$this->writeBovineRow($sheet, $rowNumber, $bovine);
|
|
|
|
try {
|
|
|
|
|
|
|
|
$this->writeBovineRow($sheet, $rowNumber, $bovine);
|
|
|
|
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
|
|
|
$this->logger->warning('Export inventaire bovin : ligne ignorée suite à une erreur.', [
|
|
|
|
|
|
|
|
'bovineId' => $bovine->getId(),
|
|
|
|
|
|
|
|
'nationalNumber' => $bovine->getNationalNumber(),
|
|
|
|
|
|
|
|
'row' => $rowNumber,
|
|
|
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
++$rowNumber;
|
|
|
|
++$rowNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -284,22 +296,25 @@ final class BovineInventoryExportProvider implements ProviderInterface
|
|
|
|
? (int) $bovine->getWorkNumber()
|
|
|
|
? (int) $bovine->getWorkNumber()
|
|
|
|
: ($bovine->getWorkNumber() ?? ''));
|
|
|
|
: ($bovine->getWorkNumber() ?? ''));
|
|
|
|
$sheet->setCellValue('C'.$row, $isCharo ? 'X' : '');
|
|
|
|
$sheet->setCellValue('C'.$row, $isCharo ? 'X' : '');
|
|
|
|
$sheet->setCellValue('D'.$row, 'FR '.$bovine->getNationalNumber());
|
|
|
|
$national = $bovine->getNationalNumber();
|
|
|
|
|
|
|
|
$sheet->setCellValue('D'.$row, '' === $national ? '' : 'FR '.$national);
|
|
|
|
$sheet->setCellValue('E'.$row, 'B1' === $code ? 'X' : '');
|
|
|
|
$sheet->setCellValue('E'.$row, 'B1' === $code ? 'X' : '');
|
|
|
|
$sheet->setCellValue('F'.$row, 'B2' === $code ? 'X' : '');
|
|
|
|
$sheet->setCellValue('F'.$row, 'B2' === $code ? 'X' : '');
|
|
|
|
$sheet->setCellValue('G'.$row, 'B3' === $code ? 'X' : '');
|
|
|
|
$sheet->setCellValue('G'.$row, 'B3' === $code ? 'X' : '');
|
|
|
|
$sheet->setCellValue('H'.$row, $bovine->getBuildingCase()?->getCaseNumber() ?? '');
|
|
|
|
$sheet->setCellValue('H'.$row, $bovine->getBuildingCase()?->getCaseNumber() ?? '');
|
|
|
|
$sheet->setCellValue('I'.$row, $bovine->getSupplier()?->getName() ?? '');
|
|
|
|
$sheet->setCellValue('I'.$row, $bovine->getSupplier()?->getName() ?? '');
|
|
|
|
|
|
|
|
|
|
|
|
$birth = $bovine->getBirthDate();
|
|
|
|
$birth = $bovine->getBirthDate();
|
|
|
|
$arrival = $bovine->getArrivalDate();
|
|
|
|
$arrival = $bovine->getArrivalDate();
|
|
|
|
if (null !== $birth) {
|
|
|
|
$birthExcel = $this->safePhpToExcel($birth);
|
|
|
|
$sheet->setCellValue('J'.$row, ExcelDate::PHPToExcel($birth));
|
|
|
|
$arrivalExcel = $this->safePhpToExcel($arrival);
|
|
|
|
|
|
|
|
if (null !== $birthExcel) {
|
|
|
|
|
|
|
|
$sheet->setCellValue('J'.$row, $birthExcel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (null !== $arrival) {
|
|
|
|
if (null !== $arrivalExcel) {
|
|
|
|
$sheet->setCellValue('K'.$row, ExcelDate::PHPToExcel($arrival));
|
|
|
|
$sheet->setCellValue('K'.$row, $arrivalExcel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (null !== $birth && null !== $arrival) {
|
|
|
|
if (null !== $birth && null !== $arrival && $birth <= $arrival) {
|
|
|
|
$diff = $birth->diff($arrival);
|
|
|
|
$diff = $birth->diff($arrival);
|
|
|
|
$sheet->setCellValue('L'.$row, ($diff->y * 12) + $diff->m);
|
|
|
|
$sheet->setCellValue('L'.$row, ($diff->y * 12) + $diff->m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -343,6 +358,24 @@ final class BovineInventoryExportProvider implements ProviderInterface
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Convertit une date PHP en numéro de série Excel, ou null si la date est absente / hors plage Excel (< 1900).
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private function safePhpToExcel(?DateTimeImmutable $date): ?float
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (null === $date) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$value = ExcelDate::PHPToExcel($date);
|
|
|
|
|
|
|
|
} catch (Throwable) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return is_float($value) ? $value : null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Sous-titre dynamique selon les tranches d'âge cochées.
|
|
|
|
* Sous-titre dynamique selon les tranches d'âge cochées.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|