Skip to content

Commit

Permalink
Add scenarios for viewing details of credit memo with included and ex…
Browse files Browse the repository at this point in the history
…cluded taxes
  • Loading branch information
GSadee committed Mar 16, 2021
1 parent 5167435 commit 0d82b19
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@refunds
Feature: Viewing details of a credit memo with taxes excluded from price
In order to be aware of what order units have been refunded
As an Administrator
I want to be able to view details of a credit memo

Background:
Given the store operates on a single green channel in "United States"
And channel "United States" billing data is "Haas & Milan", "Pacific Coast Hwy", "90003" "Los Angeles", "United States" with "1100110011" tax ID
And default tax zone is "US"
And the store has "US VAT" tax rate of 10% for "Clothes" within the "US" zone
And the store has "VAT" tax rate of 20% for "Mugs" within the "US" zone
And the store has "SHIPPING_VAT" tax rate of 15% for "Shipments" within the "US" zone
And the store has "Galaxy Post" shipping method with "$10.00" fee within the "US" zone
And shipping method "Galaxy Post" belongs to "Shipments" tax category
And the store allows paying with "Space money"
And the store has a product "PHP T-Shirt" priced at "$10.00"
And it belongs to "Clothes" tax category
And the store has a product "Symfony Mug" priced at "$20.00"
And it belongs to "Mugs" tax category
And there is a customer "rick.sanchez@wubba-lubba-dub-dub.com" that placed an order "#00000022"
And the customer bought a "PHP T-Shirt" and a "Symfony Mug"
And the customer "Rick Sanchez" addressed it to "Seaside Fwy", "90802" "Los Angeles" in the "United States" with identical billing address
And the customer chose "Galaxy Post" shipping method with "Space money" payment
And the order "#00000022" is already paid
And I am logged in as an administrator

@ui @application
Scenario: Viewing details of a credit memo issued for a full refund
Given all units and shipment from the order "#00000022" are refunded with "Space money" payment
When I browse the details of the only credit memo generated for order "#00000022"
Then it should have sequential number generated from current date
And it should be issued in "United States" channel
And it should be issued to "Haas & Milan", "Pacific Coast Hwy", "90003" "Los Angeles" in the "United States" with "1100110011" tax ID
And it should be issued from "Rick Sanchez", "Seaside Fwy", "90802" "Los Angeles" in the "United States"
And it should contain 1 "PHP T-Shirt" product with "10.00" net value, "1.00" tax amount and "11.00" gross value in "USD" currency
And it should contain 1 "Symfony Mug" product with "20.00" net value, "4.00" tax amount and "24.00" gross value in "USD" currency
And it should contain 1 "Galaxy Post" shipment with "10.00" net value, "1.50" tax amount and "11.50" gross value in "USD" currency
And it should contain a tax item "10%" with amount "1.00" in "USD" currency
And it should contain a tax item "15%" with amount "1.50" in "USD" currency
And it should contain a tax item "20%" with amount "4.00" in "USD" currency
And its total should be "46.50" in "USD" currency
And its net total should be "40.00"
And its tax total should be "6.50"

@ui @application
Scenario: Viewing details of a credit memo issued for a partial refund
Given the 1st "PHP T-Shirt" product from order "#00000022" has a refund of "$5.50" with "Space money" payment
When I browse the details of the only credit memo generated for order "#00000022"
Then it should have sequential number generated from current date
And it should be issued in "United States" channel
And it should contain 1 "PHP T-Shirt" product with "5.00" net value, "0.50" tax amount and "5.50" gross value in "USD" currency
And it should contain a tax item "10%" with amount "0.50" in "USD" currency
And its total should be "5.50" in "USD" currency
And its net total should be "5.00"
And its tax total should be "0.50"

@ui @application
Scenario: Viewing details of a credit memo issued for a partial shipping cost refund
Given the "#00000022" order's shipping cost already has a refund of "$5.75" with "Space money" payment
When I browse the details of the only credit memo generated for order "#00000022"
Then it should have sequential number generated from current date
And it should contain 1 "Galaxy Post" shipment with "5.00" net value, "0.75" tax amount and "5.75" gross value in "USD" currency
And it should be issued in "United States" channel
And its total should be "5.75" in "USD" currency
And its net total should be "5.00"
And its tax total should be "0.75"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@refunds
Feature: Viewing details of a credit memo
Feature: Viewing details of a credit memo with taxes included in price
In order to be aware of what order units have been refunded
As an Administrator
I want to be able to view details of a credit memo
Expand All @@ -8,15 +8,15 @@ Feature: Viewing details of a credit memo
Given the store operates on a single green channel in "United States"
And channel "United States" billing data is "Haas & Milan", "Pacific Coast Hwy", "90003" "Los Angeles", "United States" with "1100110011" tax ID
And default tax zone is "US"
And the store has "US VAT" tax rate of 10% for "Clothes" within the "US" zone
And the store has included in price "US VAT" tax rate of 10% for "Clothes" within the "US" zone
And the store has included in price "VAT" tax rate of 20% for "Mugs" within the "US" zone
And the store has included in price "SHIPPING_VAT" tax rate of 15% for "Shipments" within the "US" zone
And the store has "Galaxy Post" shipping method with "$10.00" fee
And the store has "Galaxy Post" shipping method with "$11.50" fee within the "US" zone
And shipping method "Galaxy Post" belongs to "Shipments" tax category
And the store allows paying with "Space money"
And the store has a product "PHP T-Shirt" priced at "$10.00"
And the store has a product "PHP T-Shirt" priced at "$11.00"
And it belongs to "Clothes" tax category
And the store has a product "Symfony Mug" priced at "$21.00"
And the store has a product "Symfony Mug" priced at "$24.00"
And it belongs to "Mugs" tax category
And there is a customer "rick.sanchez@wubba-lubba-dub-dub.com" that placed an order "#00000022"
And the customer bought a "PHP T-Shirt" and a "Symfony Mug"
Expand All @@ -34,14 +34,14 @@ Feature: Viewing details of a credit memo
And it should be issued to "Haas & Milan", "Pacific Coast Hwy", "90003" "Los Angeles" in the "United States" with "1100110011" tax ID
And it should be issued from "Rick Sanchez", "Seaside Fwy", "90802" "Los Angeles" in the "United States"
And it should contain 1 "PHP T-Shirt" product with "10.00" net value, "1.00" tax amount and "11.00" gross value in "USD" currency
And it should contain 1 "Symfony Mug" product with "17.50" net value, "3.50" tax amount and "21.00" gross value in "USD" currency
And it should contain 1 "Galaxy Post" shipment with "8.50" net value, "1.50" tax amount and "10.00" gross value in "USD" currency
And it should contain 1 "Symfony Mug" product with "20.00" net value, "4.00" tax amount and "24.00" gross value in "USD" currency
And it should contain 1 "Galaxy Post" shipment with "10.00" net value, "1.50" tax amount and "11.50" gross value in "USD" currency
And it should contain a tax item "10%" with amount "1.00" in "USD" currency
And it should contain a tax item "20%" with amount "3.50" in "USD" currency
And it should contain a tax item "15%" with amount "1.50" in "USD" currency
And its total should be "42.00" in "USD" currency
And its net total should be "36.00"
And its tax total should be "6.00"
And it should contain a tax item "20%" with amount "4.00" in "USD" currency
And its total should be "46.50" in "USD" currency
And its net total should be "40.00"
And its tax total should be "6.50"

@ui @application
Scenario: Viewing details of a credit memo issued for a partial refund
Expand All @@ -52,12 +52,16 @@ Feature: Viewing details of a credit memo
And it should contain 1 "PHP T-Shirt" product with "5.00" net value, "0.50" tax amount and "5.50" gross value in "USD" currency
And it should contain a tax item "10%" with amount "0.50" in "USD" currency
And its total should be "5.50" in "USD" currency
And its net total should be "5.00"
And its tax total should be "0.50"

@ui @application
Scenario: Viewing details of a credit memo issued for a partial shipping cost refund
Given the "#00000022" order's shipping cost already has a refund of "$5.00" with "Space money" payment
Given the "#00000022" order's shipping cost already has a refund of "$5.75" with "Space money" payment
When I browse the details of the only credit memo generated for order "#00000022"
Then it should have sequential number generated from current date
And it should contain 1 "Galaxy Post" shipment with "4.25" net value, "0.75" tax amount and "5.00" gross value in "USD" currency
And it should contain 1 "Galaxy Post" shipment with "5.00" net value, "0.75" tax amount and "5.75" gross value in "USD" currency
And it should be issued in "United States" channel
And its total should be "5.00" in "USD" currency
And its total should be "5.75" in "USD" currency
And its net total should be "5.00"
And its tax total should be "0.75"
43 changes: 37 additions & 6 deletions spec/Converter/ShipmentLineItemsConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\ShipmentInterface;
use Sylius\RefundPlugin\Exception\MoreThanOneTaxAdjustment;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;

Expand All @@ -33,7 +34,7 @@ function it_converts_shipment_unit_refunds_to_line_items(
AdjustmentInterface $taxAdjustment,
ShipmentInterface $shipment
): void {
$shipmentRefund = new ShipmentRefund(1, 500);
$shipmentRefund = new ShipmentRefund(1, 575);

$adjustmentRepository
->findOneBy(['id' => 1, 'type' => AdjustmentInterface::SHIPPING_ADJUSTMENT])
Expand All @@ -42,23 +43,22 @@ function it_converts_shipment_unit_refunds_to_line_items(
$shippingAdjustment->getLabel()->willReturn('Galaxy post');
$shippingAdjustment->getShipment()->willReturn($shipment);

$shipment->getAdjustmentsTotal()->willReturn(1000);
$shipment->getAdjustmentsTotal(AdjustmentInterface::TAX_ADJUSTMENT)->willReturn(150);
$shipment->getAdjustmentsTotal()->willReturn(1150);
$shipment
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()]))
;

$taxAdjustment->getAmount()->willReturn(75);
$taxAdjustment->getAmount()->willReturn(150);
$taxRateProvider->provide($shipment)->willReturn('15%');

$this->convert([$shipmentRefund])->shouldBeLike([new LineItem(
'Galaxy post',
1,
425,
500,
425,
575,
500,
575,
75,
'15%'
)]);
Expand Down Expand Up @@ -100,4 +100,35 @@ function it_throws_an_exception_if_refund_amount_is_higher_than_shipping_amount(
->during('convert', [[$shipmentRefund]])
;
}

function it_throws_an_exception_if_shipment_has_more_tax_adjustments_than_one(
RepositoryInterface $adjustmentRepository,
TaxRateProviderInterface $taxRateProvider,
AdjustmentInterface $shippingAdjustment,
AdjustmentInterface $firstTaxAdjustment,
AdjustmentInterface $secondTaxAdjustment,
ShipmentInterface $shipment
): void {
$shipmentRefund = new ShipmentRefund(1, 575);

$adjustmentRepository
->findOneBy(['id' => 1, 'type' => AdjustmentInterface::SHIPPING_ADJUSTMENT])
->willReturn($shippingAdjustment)
;
$shippingAdjustment->getLabel()->willReturn('Galaxy post');
$shippingAdjustment->getShipment()->willReturn($shipment);

$shipment->getAdjustmentsTotal()->willReturn(1150);
$shipment
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)
->willReturn(new ArrayCollection([$firstTaxAdjustment->getWrappedObject(), $secondTaxAdjustment->getWrappedObject()]))
;

$taxRateProvider->provide($shipment)->shouldNotBeCalled();

$this
->shouldThrow(MoreThanOneTaxAdjustment::class)
->during('convert', [[$shipmentRefund]])
;
}
}
25 changes: 24 additions & 1 deletion src/Converter/ShipmentLineItemsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Entity\ShipmentInterface;
use Sylius\RefundPlugin\Exception\MoreThanOneTaxAdjustment;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -50,13 +52,17 @@ private function convertUnitRefundToLineItem(ShipmentRefund $shipmentRefund): Li
;
Assert::notNull($shippingAdjustment);

/** @var ShipmentInterface $shipment */
$shipment = $shippingAdjustment->getShipment();
Assert::notNull($shipment);
Assert::isInstanceOf($shipment, AdjustableInterface::class);
Assert::lessThanEq($shipmentRefund->total(), $shipment->getAdjustmentsTotal());

$taxAdjustment = $this->getTaxAdjustment($shipment);
$taxAdjustmentAmount = $taxAdjustment !== null ? $taxAdjustment->getAmount() : 0;

$grossValue = $shipmentRefund->total();
$taxAmount = (int) ($grossValue * $shipment->getAdjustmentsTotal(AdjustmentInterface::TAX_ADJUSTMENT) / $shipment->getAdjustmentsTotal());
$taxAmount = (int) ($grossValue * $taxAdjustmentAmount / $shipment->getAdjustmentsTotal());
$netValue = $grossValue - $taxAmount;

return new LineItem(
Expand All @@ -70,4 +76,21 @@ private function convertUnitRefundToLineItem(ShipmentRefund $shipmentRefund): Li
$this->taxRateProvider->provide($shipment)
);
}

private function getTaxAdjustment(ShipmentInterface $shipment): ?AdjustmentInterface
{
$taxAdjustments = $shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);
if ($taxAdjustments->isEmpty()) {
return null;
}

if ($taxAdjustments->count() > 1) {
throw MoreThanOneTaxAdjustment::occur();
}

/** @var AdjustmentInterface $taxAdjustment */
$taxAdjustment = $taxAdjustments->first();

return $taxAdjustment;
}
}
8 changes: 5 additions & 3 deletions tests/Behat/Context/Setup/RefundingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Entity\ShipmentInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -120,13 +121,14 @@ public function allUnitsAndShipmentFromOrderAreRefunded(
return new OrderItemUnitRefund($unit->getId(), $unit->getTotal());
}, $order->getItemUnits()->getValues());

/** @var AdjustmentInterface $shipment */
$shipment = $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->first();
/** @var ShipmentInterface $shipment */
$shipment = $order->getShipments()->first();
$shippingAdjustment = $shipment->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->first();

$this->commandBus->dispatch(new RefundUnits(
$orderNumber,
$units,
[new ShipmentRefund($shipment->getId(), $shipment->getAmount())],
[new ShipmentRefund($shippingAdjustment->getId(), $shipment->getAdjustmentsTotal())],
$paymentMethod->getId(),
''
));
Expand Down

0 comments on commit 0d82b19

Please sign in to comment.