feat(back) : bon de pesée PDF via template Twig (ERP-192)

Endpoint API Platform GET /api/weighing_tickets/{id}/print.pdf (provider
renvoyant un binaire, pas de controller) sécurisé par
logistique.weighing_tickets.view. Rendu d'un template Twig hydraté avec le
ticket converti en PDF via Dompdf. Reproduit le modèle fourni : en-tête fixe
(logo + identité société, indépendant du site), pesées à vide/plein avec le
numéro de pesée affiché comme un DSD, poids net = plein − vide.
This commit is contained in:
2026-06-24 10:01:49 +02:00
parent 681fca9aeb
commit 36149dd521
8 changed files with 792 additions and 1 deletions
@@ -12,6 +12,7 @@ use ApiPlatform\Metadata\Post;
use App\Module\Commercial\Domain\Entity\Client; // relation ORM partagee (§ 2.1)
use App\Module\Commercial\Domain\Entity\Supplier; // relation ORM partagee (§ 2.1)
use App\Module\Logistique\Infrastructure\ApiPlatform\State\Processor\WeighingTicketProcessor;
use App\Module\Logistique\Infrastructure\ApiPlatform\State\Provider\WeighingTicketPrintProvider;
use App\Module\Logistique\Infrastructure\ApiPlatform\State\Provider\WeighingTicketProvider;
use App\Module\Logistique\Infrastructure\Doctrine\DoctrineWeighingTicketRepository;
use App\Module\Sites\Domain\Entity\Site; // relation ORM partagee (§ 2.1)
@@ -84,6 +85,18 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
]],
provider: WeighingTicketProvider::class,
),
// Bon de pesee PDF (RG-5.08, spec § 2.12 / § 4.6) : operation dediee qui
// sert un binaire (pas une representation Hydra). Le provider retourne une
// Response -> la serialisation est court-circuitee. Pas de controller
// (decision spec § 4.6). Pas de format API Platform negocie : `.pdf` est
// litteral dans l'URI.
new Get(
uriTemplate: '/weighing_tickets/{id}/print.pdf',
security: "is_granted('logistique.weighing_tickets.view')",
provider: WeighingTicketPrintProvider::class,
output: false,
read: true,
),
new Post(
security: "is_granted('logistique.weighing_tickets.manage')",
normalizationContext: ['groups' => [
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Infrastructure\ApiPlatform\State\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Module\Logistique\Domain\Entity\WeighingTicket;
use App\Module\Logistique\Domain\Repository\WeighingTicketRepositoryInterface;
use App\Module\Logistique\Infrastructure\Pdf\WeighingTicketPdfRenderer;
use App\Module\Sites\Application\Service\CurrentSiteProviderInterface;
use App\Module\Sites\Domain\Entity\Site;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provider de l'operation `GET /api/weighing_tickets/{id}/print.pdf` : sert le bon
* de pesee en PDF (M5, spec-back § 2.12 / § 4.6 — RG-5.08). Operation API Platform
* dediee (pas de controller, decision spec § 4.6) ; le binaire est genere par
* {@see WeighingTicketPdfRenderer} (template Twig -> Dompdf).
*
* Le provider retourne directement une {@see Response} : API Platform court-circuite
* alors la serialisation Hydra (le SerializeListener/RespondListener detectent une
* Response et la renvoient telle quelle). `Content-Type: application/pdf`,
* disposition `inline` (le front ouvre l'apercu — RG-5.08).
*
* Securite & visibilite — miroir de {@see WeighingTicketProvider::provideItem()} :
* - permission `logistique.weighing_tickets.view` portee par l'operation (403) ;
* - 404 si ticket introuvable, soft-delete (non expose au M5 — § 2.13), ou hors
* perimetre du site courant (anti-enumeration, § 2.3 / RG-5.09).
*
* @implements ProviderInterface<WeighingTicket>
*/
final class WeighingTicketPrintProvider implements ProviderInterface
{
public function __construct(
#[Autowire(service: 'App\Module\Logistique\Infrastructure\Doctrine\DoctrineWeighingTicketRepository')]
private readonly WeighingTicketRepositoryInterface $repository,
private readonly WeighingTicketPdfRenderer $renderer,
private readonly CurrentSiteProviderInterface $currentSiteProvider,
private readonly Security $security,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response
{
$ticket = $this->findVisibleTicket($uriVariables['id'] ?? null);
if (null === $ticket) {
throw new NotFoundHttpException('Ticket de pesée introuvable.');
}
$pdf = $this->renderer->render($ticket);
$response = new Response($pdf);
$response->headers->set('Content-Type', 'application/pdf');
$response->headers->set(
'Content-Disposition',
sprintf('inline; filename="bon-pesee-%s.pdf"', $ticket->getNumber() ?? (string) $ticket->getId()),
);
return $response;
}
/**
* Charge le ticket visible par l'utilisateur courant, ou null (-> 404) :
* introuvable, soft-delete, ou hors perimetre du site courant. Logique
* identique a WeighingTicketProvider::provideItem() (cloisonnement § 2.3).
*/
private function findVisibleTicket(mixed $id): ?WeighingTicket
{
if (!is_int($id) && !(is_string($id) && ctype_digit($id))) {
return null;
}
$ticket = $this->repository->findById((int) $id);
if (null === $ticket || null !== $ticket->getDeletedAt()) {
return null;
}
$scopeSite = $this->currentScopeSite();
if (null !== $scopeSite && $ticket->getSite()?->getId() !== $scopeSite->getId()) {
return null;
}
return $ticket;
}
/**
* Site servant a cloisonner, ou null si aucun cloisonnement ne s'applique
* (user `sites.bypass_scope`, ou pas de site courant). Miroir de
* WeighingTicketProvider::currentScopeSite().
*/
private function currentScopeSite(): ?Site
{
if ($this->security->isGranted('sites.bypass_scope')) {
return null;
}
return $this->currentSiteProvider->get();
}
}
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Infrastructure\Pdf;
use App\Module\Logistique\Domain\Entity\WeighingTicket;
use Dompdf\Dompdf;
use Dompdf\Options;
use Twig\Environment;
/**
* Rend le ticket de pesee (M5, spec-back § 2.12 / § 4.6 — RG-5.08) : hydrate le
* template Twig `logistique/weighing_ticket_print.html.twig` avec le ticket, puis
* convertit le HTML en PDF via Dompdf (pur PHP, aucune dependance systeme — choix
* valide avec Matthieu, ERP-192).
*
* Le gabarit reproduit le modele fourni (ticket_pesee.pdf) : en-tete FIXE (logo +
* identite societe), titre, les deux pesees (poids / N° pesee / DSD + date) et le
* poids net. Le rendu ne depend PAS du site (decision Tristan, ERP-192) : le logo
* et l'identite societe sont constants.
*
* Service technique d'infrastructure (pas de logique metier) : le contenu/affiche
* est decide par le template ; ICI on ne fait que charger le logo et generer le
* binaire.
*/
final class WeighingTicketPdfRenderer
{
/** Logo societe embarque dans l'en-tete (fixe, hors versioning par site). */
private const string LOGO_PATH = __DIR__.'/assets/logo-lpc-liot.png';
public function __construct(
private readonly Environment $twig,
) {}
/**
* Genere le binaire PDF du ticket de pesee pour un ticket donne.
*
* Dompdf : remote desactive (aucune ressource externe chargee — securite ; le
* logo passe en data-URI), A4 portrait, police par defaut DejaVu Sans (UTF-8
* -> accents FR et « ° » corrects).
*/
public function render(WeighingTicket $ticket): string
{
$html = $this->twig->render('logistique/weighing_ticket_print.html.twig', [
'ticket' => $ticket,
'logoSrc' => $this->logoDataUri(),
]);
$options = new Options();
$options->set('isRemoteEnabled', false);
$options->set('defaultFont', 'DejaVu Sans');
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html, 'UTF-8');
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
return (string) $dompdf->output();
}
/**
* Logo societe encode en data-URI base64, ou null s'il est introuvable (le
* template degrade alors sans bloquer la generation du PDF).
*/
private function logoDataUri(): ?string
{
$binary = @file_get_contents(self::LOGO_PATH);
if (false === $binary) {
return null;
}
return 'data:image/png;base64,'.base64_encode($binary);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB