-
-
Notifications
You must be signed in to change notification settings - Fork 94
Add support for configurable invoice sequence scopes #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1
Are you sure you want to change the base?
Changes from 4 commits
d4636b4
92fa666
3353c9a
ff5ed37
efd462a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /var/ | ||
| /vendor/ | ||
| /node_modules/ | ||
| /composer.lock | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ | |
| </id> | ||
| <field name="index" column="idx" type="integer" /> | ||
| <field name="version" type="integer" version="true" /> | ||
| <field name="year" type="integer" nullable="true"/> | ||
| <field name="month" type="integer"/> | ||
|
|
||
|
||
| </mapped-superclass> | ||
|
|
||
| </doctrine-mapping> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\InvoicingPlugin\Enum; | ||
|
|
||
| enum InvoiceSequenceScopeEnum: string | ||
| { | ||
| case GLOBAL = 'global'; | ||
| case MONTHLY = 'monthly'; | ||
| case ANNUALLY = 'annually'; | ||
|
|
||
| public static function fromString(?string $value): self | ||
| { | ||
| return match ($value) { | ||
| 'monthly' => self::MONTHLY, | ||
| 'annually' => self::ANNUALLY, | ||
| default => self::GLOBAL, | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| use Sylius\Component\Resource\Factory\FactoryInterface; | ||
| use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
| use Sylius\InvoicingPlugin\Entity\InvoiceSequenceInterface; | ||
| use Sylius\InvoicingPlugin\Enum\InvoiceSequenceScopeEnum; | ||
| use Symfony\Component\Clock\ClockInterface; | ||
|
|
||
| final class SequentialInvoiceNumberGenerator implements InvoiceNumberGenerator | ||
|
|
@@ -29,7 +30,17 @@ public function __construct( | |
| private readonly ClockInterface $clock, | ||
| private readonly int $startNumber = 1, | ||
| private readonly int $numberLength = 9, | ||
| private readonly ?string $scope = null, | ||
| ) { | ||
| if (null === $this->scope) { | ||
| trigger_deprecation( | ||
| 'sylius/invoicing-plugin', | ||
| '2.1', | ||
| 'Not passing the "%s" argument to "%s::__construct()" is deprecated and will be required in version 3.0. Pass a valid scope explicitly (e.g. "monthly", "annually", or "global").', | ||
| 'scope', | ||
| self::class, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function generate(): string | ||
|
|
@@ -56,15 +67,41 @@ private function generateNumber(int $index): string | |
|
|
||
| private function getSequence(): InvoiceSequenceInterface | ||
| { | ||
| /** @var InvoiceSequenceInterface $sequence */ | ||
| $sequence = $this->sequenceRepository->findOneBy([]); | ||
|
|
||
| if (null != $sequence) { | ||
| $now = $this->clock->now(); | ||
| $scope = InvoiceSequenceScopeEnum::tryFrom($this->scope ?? '') ?? InvoiceSequenceScopeEnum::GLOBAL; | ||
|
|
||
| $criteria = match ($scope) { | ||
| InvoiceSequenceScopeEnum::MONTHLY => [ | ||
| 'year' => (int) $now->format('Y'), | ||
| 'month' => (int) $now->format('m'), | ||
|
Comment on lines
+75
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get why you have values here. You wrote "monthly" or "yearly". You didn't specify that we could chose when it happens precisely. |
||
| ], | ||
| InvoiceSequenceScopeEnum::ANNUALLY => [ | ||
| 'year' => (int) $now->format('Y'), | ||
| ], | ||
| InvoiceSequenceScopeEnum::GLOBAL => [ | ||
| 'year' => null, | ||
| 'month' => null, | ||
| ], | ||
| }; | ||
|
|
||
| /** @var InvoiceSequenceInterface|null $sequence */ | ||
| $sequence = $this->sequenceRepository->findOneBy($criteria); | ||
|
|
||
| if (null !== $sequence) { | ||
| return $sequence; | ||
| } | ||
|
|
||
| /** @var InvoiceSequenceInterface $sequence */ | ||
| $sequence = $this->sequenceFactory->createNew(); | ||
|
|
||
| if (isset($criteria['year'])) { | ||
| $sequence->setYear($criteria['year']); | ||
| } | ||
|
|
||
| if (isset($criteria['month'])) { | ||
| $sequence->setMonth($criteria['month']); | ||
| } | ||
|
|
||
| $this->sequenceManager->persist($sequence); | ||
|
|
||
| return $sequence; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\InvoicingPlugin\Migrations; | ||
|
|
||
| use Doctrine\DBAL\Schema\Schema; | ||
| use Doctrine\Migrations\AbstractMigration; | ||
|
|
||
| final class Version20251021074051 extends AbstractMigration | ||
| { | ||
| public function getDescription(): string | ||
| { | ||
| return 'Add year and month columns to sylius_invoicing_plugin_sequence table'; | ||
| } | ||
|
|
||
| public function up(Schema $schema): void | ||
| { | ||
| $this->addSql('ALTER TABLE sylius_invoicing_plugin_sequence ADD year INT DEFAULT NULL, ADD month INT NOT NULL'); | ||
| } | ||
|
|
||
| public function down(Schema $schema): void | ||
| { | ||
| $this->addSql('ALTER TABLE sylius_invoicing_plugin_sequence DROP year, DROP month'); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with the
(anything else or empty): one global sequence.Either it's
globalor empty, but "anything else" could lead to future issues with custom developments.