Skip to content
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

Refactor credit memo number generator #299

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions spec/Factory/CreditMemoFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
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
{
function let(
FactoryInterface $creditMemoFactory,
CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator,
NumberGeneratorInterface $creditMemoNumberGenerator,
CreditMemoNumberGeneratorInterface $creditMemoNumberGenerator,
CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider
): void {
$this->beConstructedWith(
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions spec/Generator/CreditMemoGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
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;
use Sylius\RefundPlugin\Entity\ShopBillingData;
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,40 @@
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
);
}

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);

Expand All @@ -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);

Expand All @@ -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');
}
}
12 changes: 7 additions & 5 deletions src/Factory/CreditMemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +33,7 @@ final class CreditMemoFactory implements CreditMemoFactoryInterface
/** @var CreditMemoIdentifierGeneratorInterface */
private $creditMemoIdentifierGenerator;

/** @var NumberGeneratorInterface */
/** @var CreditMemoNumberGeneratorInterface */
private $creditMemoNumberGenerator;

/** @var CurrentDateTimeImmutableProviderInterface */
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions src/Generator/CreditMemoNumberGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Sylius\RefundPlugin\Generator;

use Sylius\Component\Core\Model\OrderInterface;

interface CreditMemoNumberGeneratorInterface
{
public function generate(OrderInterface $order, \DateTimeInterface $issuedAt): string;
}
10 changes: 0 additions & 10 deletions src/Generator/NumberGeneratorInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Doctrine\Persistence\ObjectRepository;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Entity\CreditMemoSequenceInterface;
use Sylius\RefundPlugin\Factory\CreditMemoSequenceFactoryInterface;
use Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface;

final class SequentialNumberGenerator implements NumberGeneratorInterface
final class SequentialCreditMemoNumberGenerator implements CreditMemoNumberGeneratorInterface
{
/** @var ObjectRepository */
private $sequenceRepository;
Expand All @@ -22,9 +22,6 @@ final class SequentialNumberGenerator implements NumberGeneratorInterface
/** @var EntityManagerInterface */
private $sequenceManager;

/** @var CurrentDateTimeImmutableProviderInterface */
private $currentDateTimeImmutableProvider;

/** @var int */
private $startNumber;

Expand All @@ -35,21 +32,19 @@ public function __construct(
ObjectRepository $sequenceRepository,
CreditMemoSequenceFactoryInterface $sequenceFactory,
EntityManagerInterface $sequenceManager,
CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider,
int $startNumber = 1,
int $numberLength = 9
) {
$this->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();
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
>
<argument type="service" id="Sylius\RefundPlugin\Factory\CreditMemoFactory.inner" />
<argument type="service" id="Sylius\RefundPlugin\Generator\UuidCreditMemoIdentifierGenerator" />
<argument type="service" id="Sylius\RefundPlugin\Generator\SequentialNumberGenerator" />
<argument type="service" id="Sylius\RefundPlugin\Generator\SequentialCreditMemoNumberGenerator" />
<argument type="service" id="Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface" />
</service>
</services>
Expand Down
3 changes: 1 addition & 2 deletions src/Resources/config/services/generator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
<services>
<defaults autowire="false" autoconfigure="false" public="true" />

<service id="Sylius\RefundPlugin\Generator\SequentialNumberGenerator">
<service id="Sylius\RefundPlugin\Generator\SequentialCreditMemoNumberGenerator">
<argument type="service" id="sylius_refund_plugin.repository.credit_memo_sequence" />
<argument type="service" id="Sylius\RefundPlugin\Factory\CreditMemoSequenceFactory" />
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface" />
</service>

<service id="Sylius\RefundPlugin\Generator\CreditMemoGenerator">
Expand Down