diff --git a/UPGRADE.md b/UPGRADE.md index bd0e29a7d..e03803aa9 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -33,7 +33,11 @@ always generated after credit memo. Technical changes: facilitate post-units refunding process. * Their `__invoke` methods were replaced by `Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessStepInterface::next(UnitsRefunded $unitsRefunded)`. -1. `Sylius\RefundPlugin\Generator\NumberGenerator` has been changed to `Sylius\RefundPlugin\Generator\NumberGeneratorInterface`. +1. `Sylius\RefundPlugin\Generator\NumberGenerator` has been changed to `Sylius\RefundPlugin\Generator\CreditMemoNumberGeneratorInterface` +and its method has been changed from `public function generate(): string` to `public function generate(OrderInterface $order, \DateTimeInterface $issuedAt): string`. + +1. Service name and definition has been changed from `Sylius\RefundPlugin\Generator\SequentialNumberGenerator` to `Sylius\RefundPlugin\Generator\SequentialCreditMemoNumberGenerator` +and its last argument `CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider` has been removed from constructor. ### UPGRADE FROM 1.0.0-RC.7 TO 1.0.0-RC.8 diff --git a/spec/Factory/CreditMemoFactorySpec.php b/spec/Factory/CreditMemoFactorySpec.php index 16f93b6c2..900a2d217 100644 --- a/spec/Factory/CreditMemoFactorySpec.php +++ b/spec/Factory/CreditMemoFactorySpec.php @@ -25,7 +25,7 @@ use Sylius\RefundPlugin\Entity\TaxItemInterface; use Sylius\RefundPlugin\Factory\CreditMemoFactoryInterface; use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface; -use Sylius\RefundPlugin\Generator\NumberGeneratorInterface; +use Sylius\RefundPlugin\Generator\CreditMemoNumberGeneratorInterface; use Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface; final class CreditMemoFactorySpec extends ObjectBehavior @@ -33,7 +33,7 @@ final class CreditMemoFactorySpec extends ObjectBehavior function let( FactoryInterface $creditMemoFactory, CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator, - NumberGeneratorInterface $creditMemoNumberGenerator, + CreditMemoNumberGeneratorInterface $creditMemoNumberGenerator, CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider ): void { $this->beConstructedWith( @@ -61,7 +61,7 @@ function it_creates_a_new_credit_memo( function it_creates_a_new_credit_memo_with_data( FactoryInterface $creditMemoFactory, CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator, - NumberGeneratorInterface $creditMemoNumberGenerator, + CreditMemoNumberGeneratorInterface $creditMemoNumberGenerator, CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider, CreditMemoInterface $creditMemo, OrderInterface $order, @@ -73,7 +73,7 @@ function it_creates_a_new_credit_memo_with_data( ShopBillingDataInterface $to ): void { $creditMemoIdentifierGenerator->generate()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353'); - $creditMemoNumberGenerator->generate()->willReturn('2018/07/00001111'); + $creditMemoNumberGenerator->generate($order, new \DateTimeImmutable('01-01-2020 10:10:10'))->willReturn('2018/07/00001111'); $currentDateTimeImmutableProvider->now()->willReturn(new \DateTimeImmutable('01-01-2020 10:10:10')); $order->getChannel()->willReturn($channel); diff --git a/spec/Generator/CreditMemoGeneratorSpec.php b/spec/Generator/CreditMemoGeneratorSpec.php index 8289509b6..355db2a76 100644 --- a/spec/Generator/CreditMemoGeneratorSpec.php +++ b/spec/Generator/CreditMemoGeneratorSpec.php @@ -10,7 +10,6 @@ use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\ShopBillingDataInterface; use Sylius\RefundPlugin\Converter\LineItemsConverterInterface; -use Sylius\RefundPlugin\Entity\CreditMemo; use Sylius\RefundPlugin\Entity\CreditMemoInterface; use Sylius\RefundPlugin\Entity\CustomerBillingData; use Sylius\RefundPlugin\Entity\LineItemInterface; @@ -18,12 +17,9 @@ use Sylius\RefundPlugin\Entity\TaxItemInterface; use Sylius\RefundPlugin\Factory\CreditMemoFactoryInterface; use Sylius\RefundPlugin\Generator\CreditMemoGeneratorInterface; -use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface; -use Sylius\RefundPlugin\Generator\NumberGeneratorInterface; use Sylius\RefundPlugin\Generator\TaxItemsGeneratorInterface; use Sylius\RefundPlugin\Model\OrderItemUnitRefund; use Sylius\RefundPlugin\Model\ShipmentRefund; -use Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface; final class CreditMemoGeneratorSpec extends ObjectBehavior { diff --git a/spec/Generator/SequentialNumberGeneratorSpec.php b/spec/Generator/SequentialCreditMemoNumberGeneratorSpec.php similarity index 65% rename from spec/Generator/SequentialNumberGeneratorSpec.php rename to spec/Generator/SequentialCreditMemoNumberGeneratorSpec.php index b04223938..f600a573e 100644 --- a/spec/Generator/SequentialNumberGeneratorSpec.php +++ b/spec/Generator/SequentialCreditMemoNumberGeneratorSpec.php @@ -8,24 +8,22 @@ use Doctrine\DBAL\LockMode; use Doctrine\ORM\EntityManagerInterface; use PhpSpec\ObjectBehavior; +use Sylius\Component\Core\Model\OrderInterface; use Sylius\RefundPlugin\Entity\CreditMemoSequenceInterface; use Sylius\RefundPlugin\Factory\CreditMemoSequenceFactoryInterface; -use Sylius\RefundPlugin\Generator\NumberGeneratorInterface; -use Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface; +use Sylius\RefundPlugin\Generator\CreditMemoNumberGeneratorInterface; -final class SequentialNumberGeneratorSpec extends ObjectBehavior +final class SequentialCreditMemoNumberGeneratorSpec extends ObjectBehavior { function let( ObjectRepository $sequenceRepository, CreditMemoSequenceFactoryInterface $sequenceFactory, - EntityManagerInterface $sequenceManager, - CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider + EntityManagerInterface $sequenceManager ): void { $this->beConstructedWith( $sequenceRepository, $sequenceFactory, $sequenceManager, - $currentDateTimeImmutableProvider, 1, 9 ); @@ -33,18 +31,17 @@ function let( function it_is_number_generator_interface(): void { - $this->shouldImplement(NumberGeneratorInterface::class); + $this->shouldImplement(CreditMemoNumberGeneratorInterface::class); } function it_generates_sequential_number( ObjectRepository $sequenceRepository, EntityManagerInterface $sequenceManager, - CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider, CreditMemoSequenceInterface $sequence, - \DateTimeImmutable $now + \DateTimeImmutable $issuedAt, + OrderInterface $order ): void { - $currentDateTimeImmutableProvider->now()->willReturn($now); - $now->format('Y/m')->willReturn('2018/05'); + $issuedAt->format('Y/m')->willReturn('2018/05'); $sequenceRepository->findOneBy([])->willReturn($sequence); @@ -55,19 +52,18 @@ function it_generates_sequential_number( $sequence->incrementIndex()->shouldBeCalled(); - $this->generate()->shouldReturn('2018/05/000000006'); + $this->generate($order, $issuedAt)->shouldReturn('2018/05/000000006'); } function it_generates_invoice_number_when_sequence_is_null( ObjectRepository $sequenceRepository, CreditMemoSequenceFactoryInterface $sequenceFactory, EntityManagerInterface $sequenceManager, - CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider, CreditMemoSequenceInterface $sequence, - \DateTimeImmutable $now + \DateTimeImmutable $issuedAt, + OrderInterface $order ): void { - $currentDateTimeImmutableProvider->now()->willReturn($now); - $now->format('Y/m')->willReturn('2018/05'); + $issuedAt->format('Y/m')->willReturn('2018/05'); $sequenceRepository->findOneBy([])->willReturn(null); @@ -82,6 +78,6 @@ function it_generates_invoice_number_when_sequence_is_null( $sequence->incrementIndex()->shouldBeCalled(); - $this->generate()->shouldReturn('2018/05/000000001'); + $this->generate($order, $issuedAt)->shouldReturn('2018/05/000000001'); } } diff --git a/src/Factory/CreditMemoFactory.php b/src/Factory/CreditMemoFactory.php index f7e40e402..681a2742a 100644 --- a/src/Factory/CreditMemoFactory.php +++ b/src/Factory/CreditMemoFactory.php @@ -21,7 +21,7 @@ use Sylius\RefundPlugin\Entity\CustomerBillingDataInterface; use Sylius\RefundPlugin\Entity\ShopBillingDataInterface; use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface; -use Sylius\RefundPlugin\Generator\NumberGeneratorInterface; +use Sylius\RefundPlugin\Generator\CreditMemoNumberGeneratorInterface; use Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface; use Webmozart\Assert\Assert; @@ -33,7 +33,7 @@ final class CreditMemoFactory implements CreditMemoFactoryInterface /** @var CreditMemoIdentifierGeneratorInterface */ private $creditMemoIdentifierGenerator; - /** @var NumberGeneratorInterface */ + /** @var CreditMemoNumberGeneratorInterface */ private $creditMemoNumberGenerator; /** @var CurrentDateTimeImmutableProviderInterface */ @@ -42,7 +42,7 @@ final class CreditMemoFactory implements CreditMemoFactoryInterface public function __construct( FactoryInterface $creditMemoFactory, CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator, - NumberGeneratorInterface $creditMemoNumberGenerator, + CreditMemoNumberGeneratorInterface $creditMemoNumberGenerator, CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider ) { $this->creditMemoFactory = $creditMemoFactory; @@ -80,9 +80,11 @@ public function createWithData( $localeCode = $order->getLocaleCode(); Assert::notNull($localeCode); + $issuedAt = $this->currentDateTimeImmutableProvider->now(); + $creditMemo = $this->createNew(); $creditMemo->setId($this->creditMemoIdentifierGenerator->generate()); - $creditMemo->setNumber($this->creditMemoNumberGenerator->generate()); + $creditMemo->setNumber($this->creditMemoNumberGenerator->generate($order, $issuedAt)); $creditMemo->setOrder($order); $creditMemo->setChannel($channel); $creditMemo->setCurrencyCode($currencyCode); @@ -91,7 +93,7 @@ public function createWithData( $creditMemo->setLineItems(new ArrayCollection($lineItems)); $creditMemo->setTaxItems(new ArrayCollection($taxItems)); $creditMemo->setComment($comment); - $creditMemo->setIssuedAt($this->currentDateTimeImmutableProvider->now()); + $creditMemo->setIssuedAt($issuedAt); $creditMemo->setFrom($from); $creditMemo->setTo($to); diff --git a/src/Generator/CreditMemoNumberGeneratorInterface.php b/src/Generator/CreditMemoNumberGeneratorInterface.php new file mode 100644 index 000000000..8b96b63f0 --- /dev/null +++ b/src/Generator/CreditMemoNumberGeneratorInterface.php @@ -0,0 +1,12 @@ +sequenceRepository = $sequenceRepository; $this->sequenceFactory = $sequenceFactory; $this->sequenceManager = $sequenceManager; - $this->currentDateTimeImmutableProvider = $currentDateTimeImmutableProvider; $this->startNumber = $startNumber; $this->numberLength = $numberLength; } - public function generate(): string + public function generate(OrderInterface $order, \DateTimeInterface $issuedAt): string { - $identifierPrefix = $this->currentDateTimeImmutableProvider->now()->format('Y/m') . '/'; + $identifierPrefix = $issuedAt->format('Y/m') . '/'; /** @var CreditMemoSequenceInterface $sequence */ $sequence = $this->getSequence(); diff --git a/src/Resources/config/services/generator.xml b/src/Resources/config/services/generator.xml index a468da6af..1cd02885c 100644 --- a/src/Resources/config/services/generator.xml +++ b/src/Resources/config/services/generator.xml @@ -4,11 +4,10 @@ - + -