From d6a388e8cc6ecba422f53e873c8c14a9c419aeea Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 31 Jan 2020 09:30:31 +0100 Subject: [PATCH] [TaxItem] Make tax item a resource --- UPGRADE.md | 2 ++ migrations/Version20200131082149.php | 32 +++++++++++++++++++ spec/Entity/CreditMemoSpec.php | 18 ++++++----- spec/Entity/TaxItemSpec.php | 4 +-- spec/Generator/CreditMemoGeneratorSpec.php | 6 ++-- src/Entity/CreditMemo.php | 13 +++----- src/Entity/CreditMemoInterface.php | 4 +-- src/Entity/TaxItem.php | 27 +++++++--------- src/Entity/TaxItemInterface.php | 10 +++--- src/Generator/CreditMemoGenerator.php | 9 +----- src/Resources/config/app/config.yml | 4 +++ .../config/doctrine/CreditMemo.orm.xml | 15 ++++++++- src/Resources/config/doctrine/TaxItem.orm.xml | 15 +++++++++ .../Context/Application/CreditMemoContext.php | 2 +- 14 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 migrations/Version20200131082149.php create mode 100644 src/Resources/config/doctrine/TaxItem.orm.xml diff --git a/UPGRADE.md b/UPGRADE.md index 71964695..6d98ec3c 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,8 @@ 4. `Sylius\RefundPlugin\Generator\ShipmentCreditMemoUnitGenerator` was changed to `Sylius\RefundPlugin\Converter\ShipmentLineItemsConverter`. +5. `Sylius\RefundPlugin\Entity\TaxItem` became a resource entity. + There are no migrations that provide backward compatibility, save current credit memos before upgrading the version of plugin. ### UPGRADE FROM 0.10.1 TO 1.0.0-RC.1 diff --git a/migrations/Version20200131082149.php b/migrations/Version20200131082149.php new file mode 100644 index 00000000..89c22fd5 --- /dev/null +++ b/migrations/Version20200131082149.php @@ -0,0 +1,32 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE sylius_refund_credit_memo_tax_items (credit_memo_id VARCHAR(255) NOT NULL, tax_item_id INT NOT NULL, INDEX IDX_9BBDFBE28E574316 (credit_memo_id), UNIQUE INDEX UNIQ_9BBDFBE25327F254 (tax_item_id), PRIMARY KEY(credit_memo_id, tax_item_id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE sylius_refund_tax_item (id INT AUTO_INCREMENT NOT NULL, `label` VARCHAR(255) NOT NULL, amount INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE sylius_refund_credit_memo_tax_items ADD CONSTRAINT FK_9BBDFBE28E574316 FOREIGN KEY (credit_memo_id) REFERENCES sylius_refund_credit_memo (id)'); + $this->addSql('ALTER TABLE sylius_refund_credit_memo_tax_items ADD CONSTRAINT FK_9BBDFBE25327F254 FOREIGN KEY (tax_item_id) REFERENCES sylius_refund_tax_item (id)'); + $this->addSql('ALTER TABLE sylius_refund_credit_memo DROP tax_items'); + } + + public function down(Schema $schema): void + { + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE sylius_refund_credit_memo_tax_items DROP FOREIGN KEY FK_9BBDFBE25327F254'); + $this->addSql('DROP TABLE sylius_refund_credit_memo_tax_items'); + $this->addSql('DROP TABLE sylius_refund_tax_item'); + $this->addSql('ALTER TABLE sylius_refund_credit_memo ADD tax_items LONGTEXT CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci` COMMENT \'(DC2Type:json)\''); + } +} diff --git a/spec/Entity/CreditMemoSpec.php b/spec/Entity/CreditMemoSpec.php index 5023bc8c..3cede3b2 100644 --- a/spec/Entity/CreditMemoSpec.php +++ b/spec/Entity/CreditMemoSpec.php @@ -12,14 +12,16 @@ use Sylius\RefundPlugin\Entity\CustomerBillingData; use Sylius\RefundPlugin\Entity\LineItemInterface; use Sylius\RefundPlugin\Entity\ShopBillingData; -use Sylius\RefundPlugin\Entity\TaxItem; +use Sylius\RefundPlugin\Entity\TaxItemInterface; final class CreditMemoSpec extends ObjectBehavior { - function let(LineItemInterface $lineItem, ChannelInterface $channel, OrderInterface $order): void - { - $taxItem = new TaxItem('VAT', 50); - + function let( + OrderInterface $order, + ChannelInterface $channel, + LineItemInterface $lineItem, + TaxItemInterface $taxItem + ): void { $this->beConstructedWith( '7903c83a-4c5e-4bcf-81d8-9dc304c6a353', '2018/07/00003333', @@ -29,7 +31,7 @@ function let(LineItemInterface $lineItem, ChannelInterface $channel, OrderInterf 'en_US', $channel, [$lineItem->getWrappedObject()], - [$taxItem->serialize()], + [$taxItem->getWrappedObject()], 'Comment', new \DateTime('01-01-2020 10:10:10'), new CustomerBillingData('Rick Sanchez', 'Main St. 3322', '90802', 'US', 'Curse Purge Plus!', 'Los Angeles', 'Baldwin Hills', '323'), @@ -82,9 +84,9 @@ function it_has_line_items(LineItemInterface $lineItem): void $this->getLineItems()->shouldBeLike(new ArrayCollection([$lineItem->getWrappedObject()])); } - function it_has_tax_items(): void + function it_has_tax_items(TaxItemInterface $taxItem): void { - $this->getTaxItems()->shouldBeLike([new TaxItem('VAT', 50)]); + $this->getTaxItems()->shouldBeLike(new ArrayCollection([$taxItem->getWrappedObject()])); } function it_has_a_date_of_creation(): void diff --git a/spec/Entity/TaxItemSpec.php b/spec/Entity/TaxItemSpec.php index 652028b5..a4729369 100644 --- a/spec/Entity/TaxItemSpec.php +++ b/spec/Entity/TaxItemSpec.php @@ -21,11 +21,11 @@ function it_implements_tax_item_interface(): void function it_has_a_label(): void { - $this->getLabel()->shouldReturn('VAT'); + $this->label()->shouldReturn('VAT'); } function it_has_an_amount(): void { - $this->getAmount()->shouldReturn(100); + $this->amount()->shouldReturn(100); } } diff --git a/spec/Generator/CreditMemoGeneratorSpec.php b/spec/Generator/CreditMemoGeneratorSpec.php index 372b9dfb..b2208f4a 100644 --- a/spec/Generator/CreditMemoGeneratorSpec.php +++ b/spec/Generator/CreditMemoGeneratorSpec.php @@ -14,7 +14,7 @@ use Sylius\RefundPlugin\Entity\CustomerBillingData; use Sylius\RefundPlugin\Entity\LineItemInterface; use Sylius\RefundPlugin\Entity\ShopBillingData; -use Sylius\RefundPlugin\Entity\TaxItem; +use Sylius\RefundPlugin\Entity\TaxItemInterface; use Sylius\RefundPlugin\Generator\CreditMemoGeneratorInterface; use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface; use Sylius\RefundPlugin\Generator\NumberGenerator; @@ -61,6 +61,7 @@ function it_generates_credit_memo_basing_on_event_data( AddressInterface $customerBillingAddress, LineItemInterface $firstLineItem, LineItemInterface $secondLineItem, + TaxItemInterface $taxItem, \DateTime $dateTime ): void { $firstUnitRefund = new OrderItemUnitRefund(1, 500); @@ -97,7 +98,6 @@ function it_generates_credit_memo_basing_on_event_data( $lineItemsConverter->convert([$firstUnitRefund, $secondUnitRefund])->willReturn([$firstLineItem]); $shipmentLineItemsConverter->convert([$shipmentRefund])->willReturn([$secondLineItem]); - $taxItem = new TaxItem('VAT', 100); $taxItemsGenerator->generate([$firstLineItem, $secondLineItem])->willReturn([$taxItem]); $creditMemoNumberGenerator->generate()->willReturn('2018/07/00001111'); @@ -115,7 +115,7 @@ function it_generates_credit_memo_basing_on_event_data( 'en_US', $channel->getWrappedObject(), [$firstLineItem->getWrappedObject(), $secondLineItem->getWrappedObject()], - [$taxItem->serialize()], + [$taxItem->getWrappedObject()], 'Comment', $dateTime->getWrappedObject(), new CustomerBillingData('Rick Sanchez', 'Universe St. 444', '000333', 'US', 'Los Angeles', 'Curse Purge Plus!'), diff --git a/src/Entity/CreditMemo.php b/src/Entity/CreditMemo.php index e1a32989..f1ebd05d 100644 --- a/src/Entity/CreditMemo.php +++ b/src/Entity/CreditMemo.php @@ -36,7 +36,7 @@ class CreditMemo implements CreditMemoInterface /** @var Collection|LineItemInterface[] */ protected $lineItems; - /** @var array */ + /** @var Collection|TaxItemInterface[] */ protected $taxItems; /** @var string */ @@ -74,7 +74,7 @@ public function __construct( $this->localeCode = $localeCode; $this->channel = $channel; $this->lineItems = new ArrayCollection($lineItems); - $this->taxItems = $taxItems; + $this->taxItems = new ArrayCollection($taxItems); $this->comment = $comment; $this->issuedAt = $issuedAt; $this->from = $from; @@ -121,14 +121,9 @@ public function getLineItems(): Collection return $this->lineItems; } - public function getTaxItems(): array + public function getTaxItems(): Collection { - $taxItems = []; - foreach ($this->taxItems as $taxItem) { - $taxItems[] = TaxItem::unserialize($taxItem); - } - - return $taxItems; + return $this->taxItems; } public function getComment(): string diff --git a/src/Entity/CreditMemoInterface.php b/src/Entity/CreditMemoInterface.php index aebad620..3a673a23 100644 --- a/src/Entity/CreditMemoInterface.php +++ b/src/Entity/CreditMemoInterface.php @@ -29,9 +29,9 @@ public function getChannel(): ChannelInterface; public function getLineItems(): Collection; /** - * @return TaxItemInterface[] + * @return Collection|TaxItemInterface[] */ - public function getTaxItems(): array; + public function getTaxItems(): Collection; public function getComment(): string; diff --git a/src/Entity/TaxItem.php b/src/Entity/TaxItem.php index 14c25295..22493788 100644 --- a/src/Entity/TaxItem.php +++ b/src/Entity/TaxItem.php @@ -7,6 +7,9 @@ /** @final */ class TaxItem implements TaxItemInterface { + /** @var int */ + protected $id; + /** @var string */ protected $label; @@ -19,31 +22,23 @@ public function __construct(string $label, int $amount) $this->amount = $amount; } - public function getLabel(): string + public function getId(): int { - return $this->label; + return $this->id(); } - public function getAmount(): int + public function id(): int { - return $this->amount; + return $this->id; } - public function serialize(): string + public function label(): string { - $serialized = json_encode(['label' => $this->label, 'amount' => $this->amount]); - - if ($serialized === false) { - throw new \Exception('Tax item cannot be serialized.'); - } - - return $serialized; + return $this->label; } - public static function unserialize(string $serialized): self + public function amount(): int { - $data = json_decode($serialized, true); - - return new self($data['label'], $data['amount']); + return $this->amount; } } diff --git a/src/Entity/TaxItemInterface.php b/src/Entity/TaxItemInterface.php index 2671113f..f7ee4a2e 100644 --- a/src/Entity/TaxItemInterface.php +++ b/src/Entity/TaxItemInterface.php @@ -4,11 +4,13 @@ namespace Sylius\RefundPlugin\Entity; -interface TaxItemInterface +use Sylius\Component\Resource\Model\ResourceInterface; + +interface TaxItemInterface extends ResourceInterface { - public function getLabel(): string; + public function id(): int; - public function getAmount(): int; + public function label(): string; - public function serialize(): string; + public function amount(): int; } diff --git a/src/Generator/CreditMemoGenerator.php b/src/Generator/CreditMemoGenerator.php index 244acf66..619037ed 100644 --- a/src/Generator/CreditMemoGenerator.php +++ b/src/Generator/CreditMemoGenerator.php @@ -13,7 +13,6 @@ use Sylius\RefundPlugin\Entity\CreditMemoInterface; use Sylius\RefundPlugin\Entity\CustomerBillingData; use Sylius\RefundPlugin\Entity\ShopBillingData; -use Sylius\RefundPlugin\Entity\TaxItemInterface; use Sylius\RefundPlugin\Provider\CurrentDateTimeProviderInterface; final class CreditMemoGenerator implements CreditMemoGeneratorInterface @@ -67,12 +66,6 @@ public function generate( $this->shipmentLineItemsConverter->convert($shipments) ); - $taxItems = []; - /** @var TaxItemInterface $taxItem */ - foreach ($this->taxItemsGenerator->generate($lineItems) as $taxItem) { - $taxItems[] = $taxItem->serialize(); - } - return new CreditMemo( $this->uuidCreditMemoIdentifierGenerator->generate(), $this->creditMemoNumberGenerator->generate(), @@ -82,7 +75,7 @@ public function generate( $order->getLocaleCode(), $channel, $lineItems, - $taxItems, + $this->taxItemsGenerator->generate($lineItems), $comment, $this->currentDateTimeProvider->now(), $this->getFromAddress($order->getBillingAddress()), diff --git a/src/Resources/config/app/config.yml b/src/Resources/config/app/config.yml index b3eebf77..d3470652 100644 --- a/src/Resources/config/app/config.yml +++ b/src/Resources/config/app/config.yml @@ -7,6 +7,10 @@ sylius_resource: classes: model: Sylius\RefundPlugin\Entity\LineItem interface: Sylius\RefundPlugin\Entity\LineItemInterface + sylius_refund.tax_item: + classes: + model: Sylius\RefundPlugin\Entity\TaxItem + interface: Sylius\RefundPlugin\Entity\TaxItemInterface sylius_refund.refund: classes: model: Sylius\RefundPlugin\Entity\Refund diff --git a/src/Resources/config/doctrine/CreditMemo.orm.xml b/src/Resources/config/doctrine/CreditMemo.orm.xml index 39f6e209..72923f68 100644 --- a/src/Resources/config/doctrine/CreditMemo.orm.xml +++ b/src/Resources/config/doctrine/CreditMemo.orm.xml @@ -11,7 +11,6 @@ - @@ -39,6 +38,20 @@ + + + + + + + + + + + + + + diff --git a/src/Resources/config/doctrine/TaxItem.orm.xml b/src/Resources/config/doctrine/TaxItem.orm.xml new file mode 100644 index 00000000..6c06fa2e --- /dev/null +++ b/src/Resources/config/doctrine/TaxItem.orm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/tests/Behat/Context/Application/CreditMemoContext.php b/tests/Behat/Context/Application/CreditMemoContext.php index 4f5668b9..17bc52df 100644 --- a/tests/Behat/Context/Application/CreditMemoContext.php +++ b/tests/Behat/Context/Application/CreditMemoContext.php @@ -123,7 +123,7 @@ public function itShouldContainATaxItemWithAmountInCurrency(string $label, int $ { /** @var TaxItemInterface $taxItem */ foreach ($this->creditMemo->getTaxItems() as $item) { - if ($item->getLabel() === $label && $item->getAmount() === $amount) { + if ($item->label() === $label && $item->amount() === $amount) { return; } }