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

Navigation via breadcrumbs between credit memo - Order and refund page - order #172

Merged
merged 2 commits into from
Jan 14, 2020
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
4 changes: 2 additions & 2 deletions features/browsing_list_of_all_credit_memos.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Feature: Browsing list of all credit memos
Scenario: Having all credit memos listed in the system
When I browse credit memos
Then there should be 2 credit memos generated
And 1st credit memo should be generated for order "#00000022", have total "$10.00" and date of being issued
And 1st credit memo should be generated for the order "#00000022", have total "$10.00" and date of being issued
And 1st credit memo should be issued in "United States" channel
And 2nd credit memo should be generated for order "#00000022", have total "$20.00" and date of being issued
And 2nd credit memo should be generated for the order "#00000022", have total "$20.00" and date of being issued
And 2nd credit memo should be issued in "United States" channel
51 changes: 51 additions & 0 deletions migrations/Version20191230121402.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20191230121402 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('DROP INDEX IDX_5C4F3331551F0F81 ON sylius_refund_credit_memo');
$this->addSql('ALTER TABLE sylius_refund_credit_memo ADD order_id INT DEFAULT NULL');

$this->addSql('
UPDATE sylius_refund_credit_memo AS cm
INNER JOIN sylius_order o
ON cm.order_number = o.number
SET cm.order_id = o.id
WHERE cm.order_number IS NOT NULL
');

$this->addSql('ALTER TABLE sylius_refund_credit_memo DROP order_number');
$this->addSql('ALTER TABLE sylius_refund_credit_memo ADD CONSTRAINT FK_5C4F33318D9F6D38 FOREIGN KEY (order_id) REFERENCES sylius_order (id)');
$this->addSql('CREATE INDEX IDX_5C4F33318D9F6D38 ON sylius_refund_credit_memo (order_id)');
}

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 DROP FOREIGN KEY FK_5C4F33318D9F6D38');
$this->addSql('DROP INDEX IDX_5C4F33318D9F6D38 ON sylius_refund_credit_memo');
$this->addSql('ALTER TABLE sylius_refund_credit_memo ADD order_number VARCHAR(255) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`');

$this->addSql('
UPDATE sylius_refund_credit_memo AS cm
INNER JOIN sylius_order o
ON cm.order_id = o.id
SET cm.order_number = o.number
WHERE cm.order_id IS NOT NULL
');

$this->addSql('ALTER TABLE sylius_refund_credit_memo DROP order_id');
$this->addSql('CREATE INDEX IDX_5C4F3331551F0F81 ON sylius_refund_credit_memo (order_number)');
}
}
16 changes: 4 additions & 12 deletions spec/Checker/CreditMemoCustomerRelationCheckerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Checker\CreditMemoCustomerRelationChecker;
Expand All @@ -19,10 +18,9 @@ final class CreditMemoCustomerRelationCheckerSpec extends ObjectBehavior
{
function let(
CustomerContextInterface $customerContext,
RepositoryInterface $creditMemoRepository,
OrderRepositoryInterface $orderRepository
RepositoryInterface $creditMemoRepository
): void {
$this->beConstructedWith($customerContext, $creditMemoRepository, $orderRepository);
$this->beConstructedWith($customerContext, $creditMemoRepository);
}

function it_is_initializable(): void
Expand All @@ -38,16 +36,13 @@ function it_implements_credit_memo_customer_relation_checker_interface(): void
function it_checks_if_customer_id_from_order_is_equal_to_customer_id_from_customer_context(
CustomerContextInterface $customerContext,
RepositoryInterface $creditMemoRepository,
OrderRepositoryInterface $orderRepository,
CreditMemo $creditMemo,
OrderInterface $order,
CustomerInterface $customer
): void {
$creditMemoRepository->find('00001')->willReturn($creditMemo);

$creditMemo->getOrderNumber()->willReturn('00002');

$orderRepository->findOneByNumber('00002')->willReturn($order);
$creditMemo->getOrder()->willReturn($order);

$order->getCustomer()->willReturn($customer);
$customerContext->getCustomer()->willReturn($customer);
Expand All @@ -60,17 +55,14 @@ function it_checks_if_customer_id_from_order_is_equal_to_customer_id_from_custom
function it_throws_exception_if_customer_id_from_order_is_not_equal_to_id_from_context(
CustomerContextInterface $customerContext,
RepositoryInterface $creditMemoRepository,
OrderRepositoryInterface $orderRepository,
CreditMemo $creditMemo,
OrderInterface $order,
CustomerInterface $firstCustomer,
CustomerInterface $secondCustomer
): void {
$creditMemoRepository->find('00001')->willReturn($creditMemo);

$creditMemo->getOrderNumber()->willReturn('00002');

$orderRepository->findOneByNumber('00002')->willReturn($order);
$creditMemo->getOrder()->willReturn($order);

$order->getCustomer()->willReturn($firstCustomer);
$customerContext->getCustomer()->willReturn($secondCustomer);
Expand Down
15 changes: 11 additions & 4 deletions spec/CommandHandler/GenerateCreditMemoHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\Common\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Command\GenerateCreditMemo;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Event\CreditMemoGenerated;
Expand All @@ -20,21 +22,26 @@ final class GenerateCreditMemoHandlerSpec extends ObjectBehavior
function let(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository
) {
$this->beConstructedWith($creditMemoGenerator, $creditMemoManager, $eventBus);
$this->beConstructedWith($creditMemoGenerator, $creditMemoManager, $eventBus, $orderRepository);
}

function it_generates_credit_memo(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus,
CreditMemoInterface $creditMemo
OrderRepositoryInterface $orderRepository,
CreditMemoInterface $creditMemo,
OrderInterface $order
): void {
$orderItemUnitRefunds = [new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(3, 2000), new OrderItemUnitRefund(5, 3000)];
$shipmentRefunds = [new ShipmentRefund(3, 1000)];

$creditMemoGenerator->generate('000666', 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment')->willReturn($creditMemo);
$orderRepository->findOneByNumber('000666')->willReturn($order);

$creditMemoGenerator->generate($order, 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment')->willReturn($creditMemo);

$creditMemo->getNumber()->willReturn('2018/01/000001');

Expand Down
9 changes: 5 additions & 4 deletions spec/Entity/CreditMemoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Entity\CreditMemoUnit;
use Sylius\RefundPlugin\Entity\CustomerBillingData;
use Sylius\RefundPlugin\Entity\ShopBillingData;

final class CreditMemoSpec extends ObjectBehavior
{
function let(ChannelInterface $channel): void
function let(ChannelInterface $channel, OrderInterface $order): void
{
$creditMemoUnit = new CreditMemoUnit('Portal gun', 1000, 50);

$this->beConstructedWith(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2018/07/00003333',
'0000222',
$order,
1000,
'USD',
'en_US',
Expand Down Expand Up @@ -48,9 +49,9 @@ function it_has_number(): void
$this->getNumber()->shouldReturn('2018/07/00003333');
}

function it_has_order_number(): void
function it_has_an_order(OrderInterface $order): void
{
$this->getOrderNumber()->shouldReturn('0000222');
$this->getOrder()->shouldReturn($order);
}

function it_has_total(): void
Expand Down
17 changes: 2 additions & 15 deletions spec/Generator/CreditMemoGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Sylius\RefundPlugin\Entity\CreditMemoUnit;
use Sylius\RefundPlugin\Entity\CustomerBillingData;
use Sylius\RefundPlugin\Entity\ShopBillingData;
use Sylius\RefundPlugin\Exception\OrderNotFound;
use Sylius\RefundPlugin\Generator\CreditMemoGeneratorInterface;
use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface;
use Sylius\RefundPlugin\Generator\CreditMemoUnitGeneratorInterface;
Expand Down Expand Up @@ -49,7 +48,6 @@ function it_implements_credit_memo_generator_interface(): void
}

function it_generates_credit_memo_basing_on_event_data(
OrderRepositoryInterface $orderRepository,
NumberGenerator $creditMemoNumberGenerator,
OrderInterface $order,
ChannelInterface $channel,
Expand All @@ -65,7 +63,6 @@ function it_generates_credit_memo_basing_on_event_data(
$secondUnitRefund = new OrderItemUnitRefund(3, 500);
$shipmentRefund = new ShipmentRefund(3, 400);

$orderRepository->findOneByNumber('000666')->willReturn($order);
$order->getCurrencyCode()->willReturn('GBP');
$order->getLocaleCode()->willReturn('en_US');

Expand Down Expand Up @@ -108,10 +105,10 @@ function it_generates_credit_memo_basing_on_event_data(

$creditMemoIdentifierGenerator->generate()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');

$this->generate('000666', 1400, [$firstUnitRefund, $secondUnitRefund], [$shipmentRefund], 'Comment')->shouldBeLike(new CreditMemo(
$this->generate($order, 1400, [$firstUnitRefund, $secondUnitRefund], [$shipmentRefund], 'Comment')->shouldBeLike(new CreditMemo(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2018/07/00001111',
'000666',
$order->getWrappedObject(),
1400,
'GBP',
'en_US',
Expand All @@ -127,14 +124,4 @@ function it_generates_credit_memo_basing_on_event_data(
new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'New York', '90222')
));
}

function it_throws_exception_if_there_is_no_order_with_given_id(OrderRepositoryInterface $orderRepository): void
{
$orderRepository->findOneByNumber('000666')->willReturn(null);

$this
->shouldThrow(OrderNotFound::withNumber('000666'))
->during('generate', ['000666', 1000, [], [], 'Comment'])
;
}
}
13 changes: 2 additions & 11 deletions src/Checker/CreditMemoCustomerRelationChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Sylius\RefundPlugin\Checker;

use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
Expand All @@ -20,26 +18,19 @@ final class CreditMemoCustomerRelationChecker implements CreditMemoCustomerRelat
/** @var RepositoryInterface */
private $creditMemoRepository;

/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(
CustomerContextInterface $customerContext,
RepositoryInterface $creditMemoRepository,
OrderRepositoryInterface $orderRepository
RepositoryInterface $creditMemoRepository
) {
$this->customerContext = $customerContext;
$this->creditMemoRepository = $creditMemoRepository;
$this->orderRepository = $orderRepository;
}

public function check(string $creditMemoId): void
{
/** @var CreditMemoInterface $creditMemo */
$creditMemo = $this->creditMemoRepository->find($creditMemoId);

/** @var OrderInterface $order */
$order = $this->orderRepository->findOneByNumber($creditMemo->getOrderNumber());
$order = $creditMemo->getOrder();

/** @var CustomerInterface $orderCustomer */
$orderCustomer = $order->getCustomer();
Expand Down
13 changes: 11 additions & 2 deletions src/CommandHandler/GenerateCreditMemoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Sylius\RefundPlugin\CommandHandler;

use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Command\GenerateCreditMemo;
use Sylius\RefundPlugin\Event\CreditMemoGenerated;
use Sylius\RefundPlugin\Generator\CreditMemoGeneratorInterface;
Expand All @@ -21,22 +23,29 @@ final class GenerateCreditMemoHandler
/** @var MessageBusInterface */
private $eventBus;

/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository
) {
$this->creditMemoGenerator = $creditMemoGenerator;
$this->creditMemoManager = $creditMemoManager;
$this->eventBus = $eventBus;
$this->orderRepository = $orderRepository;
}

public function __invoke(GenerateCreditMemo $command): void
{
$orderNumber = $command->orderNumber();
/** @var OrderInterface $order */
$order = $this->orderRepository->findOneByNumber($orderNumber);

$creditMemo = $this->creditMemoGenerator->generate(
$orderNumber,
$order,
$command->total(),
$command->units(),
$command->shipments(),
Expand Down
18 changes: 2 additions & 16 deletions src/CommandHandler/SendCreditMemoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace Sylius\RefundPlugin\CommandHandler;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Command\SendCreditMemo;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Exception\CreditMemoNotFound;
use Sylius\RefundPlugin\Exception\OrderNotFound;
use Sylius\RefundPlugin\Sender\CreditMemoEmailSenderInterface;
use Webmozart\Assert\Assert;

Expand All @@ -22,17 +19,12 @@ final class SendCreditMemoHandler
/** @var CreditMemoEmailSenderInterface */
private $creditMemoEmailSender;

/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(
RepositoryInterface $creditMemoRepository,
CreditMemoEmailSenderInterface $creditMemoEmailSender,
OrderRepositoryInterface $orderRepository
CreditMemoEmailSenderInterface $creditMemoEmailSender
) {
$this->creditMemoRepository = $creditMemoRepository;
$this->creditMemoEmailSender = $creditMemoEmailSender;
$this->orderRepository = $orderRepository;
}

public function __invoke(SendCreditMemo $command): void
Expand All @@ -45,13 +37,7 @@ public function __invoke(SendCreditMemo $command): void
throw CreditMemoNotFound::withNumber($creditMemoNumber);
}

$orderNumber = $creditMemo->getOrderNumber();

/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOneByNumber($orderNumber);
if ($order === null) {
throw OrderNotFound::withNumber($orderNumber);
}
$order = $creditMemo->getOrder();

Assert::notNull($order->getCustomer(), 'Credit memo order has no customer');

Expand Down
Loading