Skip to content

Commit

Permalink
[TaxItem] Make tax item a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Jan 31, 2020
1 parent 757c53f commit 84c667d
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 53 deletions.
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions migrations/Version20200131082149.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20200131082149 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->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)\'');
}
}
18 changes: 10 additions & 8 deletions spec/Entity/CreditMemoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'),
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/Entity/TaxItemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions spec/Generator/CreditMemoGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -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!'),
Expand Down
13 changes: 4 additions & 9 deletions src/Entity/CreditMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CreditMemo implements CreditMemoInterface
/** @var Collection|LineItemInterface[] */
protected $lineItems;

/** @var array */
/** @var Collection|TaxItemInterface[] */
protected $taxItems;

/** @var string */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/CreditMemoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
27 changes: 11 additions & 16 deletions src/Entity/TaxItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
/** @final */
class TaxItem implements TaxItemInterface
{
/** @var int */
protected $id;

/** @var string */
protected $label;

Expand All @@ -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;
}
}
10 changes: 6 additions & 4 deletions src/Entity/TaxItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 1 addition & 8 deletions src/Generator/CreditMemoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -82,7 +75,7 @@ public function generate(
$order->getLocaleCode(),
$channel,
$lineItems,
$taxItems,
$this->taxItemsGenerator->generate($lineItems),
$comment,
$this->currentDateTimeProvider->now(),
$this->getFromAddress($order->getBillingAddress()),
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/app/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/Resources/config/doctrine/CreditMemo.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

<field name="number" />
<field name="total" type="integer" />
<field name="taxItems" column="tax_items" type="json" nullable="true" />
<field name="currencyCode" column="currency_code" />
<field name="localeCode" column="locale_code" />
<field name="comment" type="text" />
Expand Down Expand Up @@ -39,6 +38,20 @@
</join-table>
</many-to-many>

<many-to-many field="taxItems" target-entity="Sylius\RefundPlugin\Entity\TaxItemInterface">
<cascade>
<cascade-all />
</cascade>
<join-table name="sylius_refund_credit_memo_tax_items">
<join-columns>
<join-column name="credit_memo_id" />
</join-columns>
<inverse-join-columns>
<join-column name="tax_item_id" unique="true" />
</inverse-join-columns>
</join-table>
</many-to-many>

<one-to-one field="from" target-entity="Sylius\RefundPlugin\Entity\CustomerBillingData">
<cascade>
<cascade-all />
Expand Down
15 changes: 15 additions & 0 deletions src/Resources/config/doctrine/TaxItem.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
<mapped-superclass name="Sylius\RefundPlugin\Entity\TaxItem" table="sylius_refund_tax_item">
<id name="id" column="id" type="integer">
<generator />
</id>

<field name="label" />
<field name="amount" type="integer" />
</mapped-superclass>
</doctrine-mapping>

0 comments on commit 84c667d

Please sign in to comment.