Skip to content

Commit

Permalink
Add Order relation on Invoice (and remove order number field)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 7, 2021
1 parent dea611b commit 081b080
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 25 deletions.
13 changes: 8 additions & 5 deletions spec/Entity/InvoiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\InvoicingPlugin\Entity\BillingDataInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
Expand All @@ -30,14 +31,15 @@ public function let(
LineItemInterface $lineItem,
TaxItemInterface $taxItem,
ChannelInterface $channel,
InvoiceShopBillingDataInterface $shopBillingData
InvoiceShopBillingDataInterface $shopBillingData,
OrderInterface $order
): void {
$issuedAt = \DateTimeImmutable::createFromFormat('Y-m', '2019-01');

$this->beConstructedWith(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
$issuedAt->format('Y/m') . '/000000001',
'007',
$order,
$issuedAt,
$billingData,
'USD',
Expand All @@ -60,16 +62,17 @@ public function it_implements_resource_interface(): void
$this->shouldImplement(ResourceInterface::class);
}

public function it_has_an_id(
public function it_has_data(
BillingDataInterface $billingData,
LineItemInterface $lineItem,
TaxItemInterface $taxItem,
ChannelInterface $channel,
InvoiceShopBillingDataInterface $shopBillingData
InvoiceShopBillingDataInterface $shopBillingData,
OrderInterface $order
): void {
$this->id()->shouldReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');
$this->number()->shouldReturn('2019/01/000000001');
$this->orderNumber()->shouldReturn('007');
$this->order()->shouldReturn($order);
$this->billingData()->shouldReturn($billingData);
$this->currencyCode()->shouldReturn('USD');
$this->localeCode()->shouldReturn('en_US');
Expand Down
11 changes: 7 additions & 4 deletions spec/Factory/InvoiceFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\InvoicingPlugin\Entity\BillingDataInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceShopBillingDataInterface;
Expand All @@ -31,14 +32,15 @@ public function it_implements_invoice_factory_interface(): void
public function it_creates_an_invoice_for_given_data(
BillingDataInterface $billingData,
ChannelInterface $channel,
InvoiceShopBillingDataInterface $invoiceShopBillingData
InvoiceShopBillingDataInterface $invoiceShopBillingData,
OrderInterface $order
): void {
$date = new \DateTimeImmutable('2019-03-06');

$this->createForData(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2019/03/0000001',
'007',
$order,
$date,
$billingData,
'USD',
Expand All @@ -53,14 +55,15 @@ public function it_creates_an_invoice_for_given_data(

public function it_allows_for_nullable_shop_billing_data(
BillingDataInterface $billingData,
ChannelInterface $channel
ChannelInterface $channel,
OrderInterface $order
): void {
$date = new \DateTimeImmutable('2019-03-06');

$this->createForData(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2019/03/0000001',
'007',
$order,
$date,
$billingData,
'USD',
Expand Down
3 changes: 1 addition & 2 deletions spec/Generator/InvoiceGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function it_generates_an_invoice_for_a_given_order(
$uuidInvoiceIdentifierGenerator->generate()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');
$sequentialInvoiceNumberGenerator->generate()->willReturn($date->format('Y/m') . '/0000001');

$order->getNumber()->willReturn('007');
$order->getCurrencyCode()->willReturn('USD');
$order->getLocaleCode()->willReturn('en_US');
$order->getTotal()->willReturn(10300);
Expand All @@ -95,7 +94,7 @@ public function it_generates_an_invoice_for_a_given_order(
$invoiceFactory->createForData(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2019/03/0000001',
'007',
$order,
$date,
$billingData,
'USD',
Expand Down
16 changes: 11 additions & 5 deletions src/Entity/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;

/** @final */
class Invoice implements InvoiceInterface
Expand All @@ -25,8 +26,8 @@ class Invoice implements InvoiceInterface
/** @var string */
protected $number;

/** @var string */
protected $orderNumber;
/** @var OrderInterface */
protected $order;

/** @var \DateTimeInterface */
protected $issuedAt;
Expand Down Expand Up @@ -58,7 +59,7 @@ class Invoice implements InvoiceInterface
public function __construct(
string $id,
string $number,
string $orderNumber,
OrderInterface $order,
\DateTimeInterface $issuedAt,
BillingDataInterface $billingData,
string $currencyCode,
Expand All @@ -71,7 +72,7 @@ public function __construct(
) {
$this->id = $id;
$this->number = $number;
$this->orderNumber = $orderNumber;
$this->order = $order;
$this->issuedAt = clone $issuedAt;
$this->billingData = $billingData;
$this->currencyCode = $currencyCode;
Expand Down Expand Up @@ -108,9 +109,14 @@ public function number(): string
return $this->number;
}

public function order(): OrderInterface
{
return $this->order;
}

public function orderNumber(): string
{
return $this->orderNumber;
return $this->order->getNumber();
}

public function issuedAt(): \DateTimeInterface
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/InvoiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Resource\Model\ResourceInterface;

interface InvoiceInterface extends ResourceInterface
Expand All @@ -23,6 +24,9 @@ public function id(): string;

public function number(): string;

public function order(): OrderInterface;

/** @deprecated this method is deprecated an will be remove in v1.0 - use InvoiceInterface::order() instead */
public function orderNumber(): string;

public function issuedAt(): \DateTimeInterface;
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/InvoiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\InvoicingPlugin\Entity\BillingDataInterface;
use Sylius\InvoicingPlugin\Entity\Invoice;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
Expand All @@ -26,7 +27,7 @@ final class InvoiceFactory implements InvoiceFactoryInterface
public function createForData(
string $id,
string $number,
string $orderNumber,
OrderInterface $order,
\DateTimeInterface $issuedAt,
BillingDataInterface $billingData,
string $currencyCode,
Expand All @@ -40,7 +41,7 @@ public function createForData(
return new Invoice(
$id,
$number,
$orderNumber,
$order,
$issuedAt,
$billingData,
$currencyCode,
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/InvoiceFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\InvoicingPlugin\Entity\BillingDataInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceShopBillingDataInterface;
Expand All @@ -24,7 +25,7 @@ interface InvoiceFactoryInterface
public function createForData(
string $id,
string $number,
string $orderNumber,
OrderInterface $order,
\DateTimeInterface $issuedAt,
BillingDataInterface $billingData,
string $currencyCode,
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/InvoiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function generateForOrder(OrderInterface $order, \DateTimeInterface $date
return $this->invoiceFactory->createForData(
$this->uuidInvoiceIdentifierGenerator->generate(),
$this->sequentialInvoiceNumberGenerator->generate(),
$order->getNumber(),
$order,
$date,
$this->billingDataConverter->convert($billingAddress),
$order->getCurrencyCode(),
Expand Down
51 changes: 51 additions & 0 deletions src/Migrations/Version20210607115930.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Sylius\InvoicingPlugin\Migrations;

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

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210607115930 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX IDX_3AA279BF551F0F81 ON sylius_invoicing_plugin_invoice');
$this->addSql('ALTER TABLE sylius_invoicing_plugin_invoice ADD order_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE sylius_invoicing_plugin_invoice ADD CONSTRAINT FK_3AA279BF8D9F6D38 FOREIGN KEY (order_id) REFERENCES sylius_order (id)');
$this->addSql('CREATE INDEX IDX_3AA279BF8D9F6D38 ON sylius_invoicing_plugin_invoice (order_id)');
$this->addSql(
'UPDATE sylius_invoicing_plugin_invoice
SET sylius_invoicing_plugin_invoice.order_id = (
SELECT sylius_order.id FROM sylius_order WHERE sylius_order.number = sylius_invoicing_plugin_invoice.order_number
);'
);
$this->addSql('ALTER TABLE sylius_invoicing_plugin_invoice DROP order_number');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE sylius_invoicing_plugin_invoice DROP FOREIGN KEY FK_3AA279BF8D9F6D38');
$this->addSql('DROP INDEX IDX_3AA279BF8D9F6D38 ON sylius_invoicing_plugin_invoice');
$this->addSql('ALTER TABLE sylius_invoicing_plugin_invoice ADD order_number VARCHAR(255) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`');
$this->addSql('CREATE INDEX IDX_3AA279BF551F0F81 ON sylius_invoicing_plugin_invoice (order_number)');
$this->addSql(
'UPDATE sylius_invoicing_plugin_invoice
SET sylius_invoicing_plugin_invoice.order_number = (
SELECT sylius_order.number FROM sylius_order WHERE sylius_order.id = sylius_invoicing_plugin_invoice.order_id
);'
);
$this->addSql('ALTER TABLE sylius_invoicing_plugin_invoice DROP order_id');
}
}
9 changes: 4 additions & 5 deletions src/Resources/config/doctrine/Invoice.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<id name="id" column="id" type="string" />

<field name="number" column="number" />
<field name="orderNumber" column="order_number" />
<field name="issuedAt" column="issued_at" type="datetime" />
<field name="currencyCode" column="currency_code" length="3" />
<field name="localeCode" column="locale_code" />
Expand Down Expand Up @@ -34,15 +33,15 @@
<join-column name="channel_id" />
</many-to-one>

<many-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface">
<join-column name="order_id" />
</many-to-one>

<one-to-one field="shopBillingData" target-entity="Sylius\InvoicingPlugin\Entity\InvoiceShopBillingDataInterface">
<cascade>
<cascade-all />
</cascade>
<join-column name="shop_billing_data_id" referenced-column-name="id" />
</one-to-one>

<indexes>
<index columns="order_number" />
</indexes>
</mapped-superclass>
</doctrine-mapping>

0 comments on commit 081b080

Please sign in to comment.