diff --git a/src/Controller/InvoiceController.php b/src/Controller/InvoiceController.php index f8e31455..0c089c3b 100644 --- a/src/Controller/InvoiceController.php +++ b/src/Controller/InvoiceController.php @@ -447,6 +447,22 @@ final class InvoiceController extends AbstractController return $this->renderTemplateForm($template, $request); } + #[Route(path: '/document_download/{document}', name: 'admin_invoice_document_download', methods: ['GET'])] + #[IsGranted('upload_invoice_template')] + public function downloadDocument(string $document, Environment $twig): Response + { + $event = new InvoiceDocumentsEvent($this->service->getDocuments(true)); + $this->dispatcher->dispatch($event); + + foreach ($event->getInvoiceDocuments() as $doc) { + if ($document === $doc->getId()) { + return $this->file($doc->getFilename()); + } + } + + throw $this->createNotFoundException('Unknown document: ' . $document); + } + #[Route(path: '/document_reload/{document}', name: 'admin_invoice_document_reload', methods: ['GET', 'POST'])] #[IsGranted('upload_invoice_template')] public function reloadDocument(string $document, Environment $twig): Response diff --git a/src/EventSubscriber/Actions/InvoiceDocumentSubscriber.php b/src/EventSubscriber/Actions/InvoiceDocumentSubscriber.php index c7a319f6..939a4299 100644 --- a/src/EventSubscriber/Actions/InvoiceDocumentSubscriber.php +++ b/src/EventSubscriber/Actions/InvoiceDocumentSubscriber.php @@ -43,12 +43,14 @@ final class InvoiceDocumentSubscriber extends AbstractActionsSubscriber /** @var string $token */ $token = \array_key_exists('token', $payload) ? $payload['token'] : null; - if (!$inUse) { - $event->addDelete($this->path('invoice_document_delete', ['id' => $document->getId(), 'token' => $token]), false); - } + $event->addAction('download', ['url' => $this->path('admin_invoice_document_download', ['document' => $document->getId()])]); if ($document->isTwig()) { $event->addAction('Reload', ['url' => $this->path('admin_invoice_document_reload', ['document' => $document->getId()])]); } + + if (!$inUse) { + $event->addDelete($this->path('invoice_document_delete', ['id' => $document->getId(), 'token' => $token]), false); + } } }