From 4ff6f6f23b8de3e5e56d503f93503d6a9da567bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kali=C5=84ski?= Date: Wed, 15 Oct 2025 14:23:06 +0200 Subject: [PATCH] Add support for invoice modifiers in `InvoiceCreator` --- .gitignore | 1 + config/services/generators.xml | 1 + src/Creator/InvoiceCreator.php | 17 +++++++++++++++++ src/Modifier/InvoiceModifierInterface.php | 21 +++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/Modifier/InvoiceModifierInterface.php diff --git a/.gitignore b/.gitignore index 012f2eeb..830c0a03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/var/ /vendor/ /node_modules/ /composer.lock diff --git a/config/services/generators.xml b/config/services/generators.xml index cfca0642..87fc6a1f 100644 --- a/config/services/generators.xml +++ b/config/services/generators.xml @@ -60,6 +60,7 @@ %sylius_invoicing.pdf_generator.enabled% + diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index 15b18b96..3b674275 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -22,6 +22,7 @@ use Sylius\InvoicingPlugin\Generator\InvoiceGeneratorInterface; use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface; use Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface; +use Sylius\InvoicingPlugin\Modifier\InvoiceModifierInterface; final class InvoiceCreator implements InvoiceCreatorInterface { @@ -32,7 +33,17 @@ public function __construct( private readonly InvoicePdfFileGeneratorInterface $invoicePdfFileGenerator, private readonly InvoiceFileManagerInterface $invoiceFileManager, private readonly bool $hasEnabledPdfFileGenerator = true, + private readonly ?iterable $invoiceModifiers = null, ) { + if (null === $this->invoiceModifiers) { + trigger_deprecation( + 'sylius/invoicing-plugin', + '2.1', + 'Not passing a "%s" to "%s" is deprecated and will be required in Sylius Invoicing Plugin 3.0.', + InvoiceModifierInterface::class, + self::class, + ); + } } public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void @@ -49,6 +60,12 @@ public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): voi $invoice = $this->invoiceGenerator->generateForOrder($order, $dateTime); + if (null !== $this->invoiceModifiers) { + foreach ($this->invoiceModifiers as $modifier) { + $invoice = $modifier->modify($invoice); + } + } + if (!$this->hasEnabledPdfFileGenerator) { $this->invoiceRepository->add($invoice); diff --git a/src/Modifier/InvoiceModifierInterface.php b/src/Modifier/InvoiceModifierInterface.php new file mode 100644 index 00000000..dec08c3d --- /dev/null +++ b/src/Modifier/InvoiceModifierInterface.php @@ -0,0 +1,21 @@ +