feat : ajout de la génération du bon de reception, correction de la base du formulaire multi-etape de reception et ajout d'une gestion d'erreur global
This commit is contained in:
@@ -11,6 +11,7 @@ use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
|
||||
use App\Dto\PontBasculeReading;
|
||||
use App\State\ReceptionReceiptProvider;
|
||||
use App\State\ReceptionWeighingProvider;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
@@ -51,6 +52,16 @@ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
output: PontBasculeReading::class,
|
||||
provider: ReceptionWeighingProvider::class,
|
||||
),
|
||||
new Get(
|
||||
uriTemplate: '/receptions/{id}/receipt',
|
||||
requirements: ['id' => '\d+'],
|
||||
openapi: new OpenApiOperation(
|
||||
summary: 'Render a reception receipt',
|
||||
description: 'Returns a PDF receipt for the reception.',
|
||||
),
|
||||
output: false,
|
||||
provider: ReceptionReceiptProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
class Reception
|
||||
|
||||
63
src/State/ReceptionReceiptProvider.php
Normal file
63
src/State/ReceptionReceiptProvider.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Entity\Reception;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Dompdf\Dompdf;
|
||||
use Dompdf\Options;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
|
||||
final readonly class ReceptionReceiptProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Environment $twig,
|
||||
private EntityManagerInterface $entityManager,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws RuntimeError
|
||||
* @throws SyntaxError
|
||||
* @throws LoaderError
|
||||
*/
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response
|
||||
{
|
||||
$id = $uriVariables['id'] ?? null;
|
||||
if (null === $id) {
|
||||
throw new NotFoundHttpException('Reception not found.');
|
||||
}
|
||||
|
||||
$reception = $this->entityManager->getRepository(Reception::class)->find($id);
|
||||
if (!$reception instanceof Reception) {
|
||||
throw new NotFoundHttpException('Reception not found.');
|
||||
}
|
||||
|
||||
$options = new Options();
|
||||
$options->set('isRemoteEnabled', true);
|
||||
|
||||
$dompdf = new Dompdf($options);
|
||||
$html = $this->twig->render('reception_voucher.html.twig', [
|
||||
'reception' => $reception,
|
||||
]);
|
||||
|
||||
$dompdf->loadHtml($html);
|
||||
$dompdf->setPaper('A4');
|
||||
$dompdf->render();
|
||||
|
||||
$filename = sprintf('bon-reception-%d.pdf', $reception->getId());
|
||||
|
||||
return new Response($dompdf->output(), Response::HTTP_OK, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$filename.'"',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user