Skip to content

Commit

Permalink
Minor fixes in tax rate providers
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Mar 16, 2021
1 parent f068570 commit af6d25e
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 27 deletions.
48 changes: 40 additions & 8 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\TaxRateAmountProviderInterface;

Expand All @@ -28,26 +29,28 @@ function it_implements_line_items_converter_interface(): void

function it_converts_shipment_unit_refunds_to_line_items(
RepositoryInterface $adjustmentRepository,
TaxRateAmountProviderInterface $taxRateAmountProvider,
AdjustmentInterface $shippingAdjustment,
ShipmentInterface $shipment,
TaxRateAmountProviderInterface $taxRateAmountProvider
AdjustmentInterface $taxAdjustment,
ShipmentInterface $shipment
): void {
$shipmentRefund = new ShipmentRefund(1, 500);

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

$shippingAdjustment->getLabel()->willReturn('Galaxy post');
$shippingAdjustment->getShipment()->willReturn($shipment);

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

$shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->willReturn( new ArrayCollection([$shippingAdjustment->getWrappedObject()]));

$taxRateAmountProvider->provide($shippingAdjustment)->willReturn(0.15);

$shippingAdjustment->getLabel()->willReturn('Galaxy post');
$taxAdjustment->getAmount()->willReturn(75);
$taxRateAmountProvider->provide($taxAdjustment)->willReturn(0.15);

$this->convert([$shipmentRefund])->shouldBeLike([new LineItem(
'Galaxy post',
Expand Down Expand Up @@ -97,4 +100,33 @@ function it_throws_an_exception_if_refund_amount_is_higher_than_shipping_amount(
->during('convert', [[$shipmentRefund]])
;
}

function it_throws_an_exception_if_there_is_more_tax_adjustments_than_one(
RepositoryInterface $adjustmentRepository,
TaxRateAmountProviderInterface $taxRateAmountProvider,
AdjustmentInterface $shippingAdjustment,
AdjustmentInterface $firstTaxAdjustment,
AdjustmentInterface $secondTaxAdjustment,
ShipmentInterface $shipment
): void {
$shipmentRefund = new ShipmentRefund(1, 500);

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

$shippingAdjustment->getShipment()->willReturn($shipment);

$shipment->getAdjustmentsTotal()->willReturn(1000);

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

$taxRateAmountProvider->provide($shippingAdjustment)->shouldNotBeCalled();

$this->shouldThrow(MoreThanOneTaxAdjustment::class)->during('convert', [[$shipmentRefund]]);
}
}
8 changes: 3 additions & 5 deletions spec/Provider/TaxRateProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Sylius\RefundPlugin\Exception\MoreThanOneTaxAdjustment;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;

final class TaxRateProviderSpec extends ObjectBehavior
Expand Down Expand Up @@ -55,7 +56,7 @@ function it_returns_null_if_there_is_no_adjustment_with_details_with_tax_rate_am
$this->provide($orderItemUnit)->shouldReturn(null);
}

function it_throws_exception_if_order_item_unit_has_more_than_1_tax_adjustment(
function it_throws_an_exception_if_order_item_unit_has_more_adjustments_than_one(
OrderItemUnitInterface $orderItemUnit,
AdjustmentInterface $firstTaxAdjustment,
AdjustmentInterface $secondTaxAdjustment
Expand All @@ -65,9 +66,6 @@ function it_throws_exception_if_order_item_unit_has_more_than_1_tax_adjustment(
->willReturn(new ArrayCollection([$firstTaxAdjustment->getWrappedObject(), $secondTaxAdjustment->getWrappedObject()]))
;

$this
->shouldThrow(\InvalidArgumentException::class)
->during('provide', [$orderItemUnit])
;
$this->shouldThrow(MoreThanOneTaxAdjustment::class)->during('provide', [$orderItemUnit]);
}
}
18 changes: 13 additions & 5 deletions src/Converter/ShipmentLineItemsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Exception\MoreThanOneTaxAdjustment;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Provider\TaxRateAmountProviderInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -61,13 +62,20 @@ private function convertUnitRefundToLineItem(ShipmentRefund $shipmentRefund): Li

$taxAdjustments = $shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);

$taxAdjustmentAmount = 0;
if (count($taxAdjustments) > 0) {
$taxAdjustmentAmount = $this->taxRateAmountProvider->provide($taxAdjustments->first());
if (count($taxAdjustments) > 1) {
throw MoreThanOneTaxAdjustment::occur();
}

$taxRate = $taxAdjustmentAmount * 100 . '%';
$taxAmount = (int) ($grossValue * $taxAdjustmentAmount);
$taxRateAmount = 0;
$taxAmount = 0;
if (count($taxAdjustments) === 1) {
/** @var AdjustmentInterface $taxAdjustment */
$taxAdjustment = $taxAdjustments->first();
$taxRateAmount = $this->taxRateAmountProvider->provide($taxAdjustment);
$taxAmount = $taxAdjustment->getAmount();
}

$taxRate = $taxRateAmount * 100 . '%';
$netValue = $grossValue - $taxAmount;

return new LineItem(
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/CreditMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function getNetValueTotal(): int

/** @var LineItemInterface $lineItem */
foreach ($this->getLineItems() as $lineItem) {
$sum = $sum + $lineItem->netValue();
$sum += $lineItem->netValue();
}

return $sum;
Expand All @@ -163,7 +163,7 @@ public function getTaxTotal(): int

/** @var LineItemInterface $lineItem */
foreach ($this->getLineItems() as $lineItem) {
$sum = $sum + $lineItem->taxAmount();
$sum += $lineItem->taxAmount();
}

return $sum;
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/MoreThanOneTaxAdjustment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Sylius\RefundPlugin\Exception;

final class MoreThanOneTaxAdjustment extends \InvalidArgumentException
{
public static function occur(): self
{
return new self('Each order item unit must not have more than 1 tax adjustment');
}
}
5 changes: 4 additions & 1 deletion src/Provider/TaxRateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Sylius\RefundPlugin\Exception\MoreThanOneTaxAdjustment;
use Webmozart\Assert\Assert;

final class TaxRateProvider implements TaxRateProviderInterface
Expand All @@ -16,7 +17,9 @@ public function provide(OrderItemUnitInterface $orderItemUnit): ?string
/** @var Collection|AdjustmentInterface[] $taxAdjustments */
$taxAdjustments = $orderItemUnit->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);

Assert::maxCount($taxAdjustments, 1, 'Every Order Item Unit Should have max 1 tax adjustment');
if (count($taxAdjustments) > 1) {
throw MoreThanOneTaxAdjustment::occur();
}

if ($taxAdjustments->isEmpty() || !key_exists('taxRateAmount', $taxAdjustments->first()->getDetails())) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<service
id="Sylius\RefundPlugin\Provider\TaxRateAmountProviderInterface"
class="Sylius\RefundPlugin\Provider\TaxRateAmountProvider"
class="Sylius\RefundPlugin\Provider\TaxRateAmountProvider"
/>
</services>
</container>
4 changes: 2 additions & 2 deletions tests/Behat/Context/Application/CreditMemoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ public function creditMemoTotalShouldBe(int $total, string $currencyCode): void
}

/**
* @Given /^its net total should be ("[^"]+")$/
* @Then /^its net total should be ("[^"]+")$/
*/
public function itsNetTotalShouldBe(int $total): void
{
Assert::same($this->creditMemo->getNetValueTotal(), $total);
}

/**
* @Given /^its tax total should be ("[^"]+")$/
* @Then /^its tax total should be ("[^"]+")$/
*/
public function itsTaxTotalShouldBe(int $total): void
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Behat/Context/Ui/CreditMemoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ public function itShouldContainProductWithNetValueTaxAmountAndGrossValueInCurren
}

/**
* @Then it should contain :quantity :productName shipment(s) with :netValue net value, :taxAmount tax amount and :grossValue gross value in :currencyCode currency
* @Then it should contain :quantity :shipmentName shipment(s) with :netValue net value, :taxAmount tax amount and :grossValue gross value in :currencyCode currency
*/
public function itShouldContainShipmentWithNetValueTaxAmountAndGrossValueInCurrency(
int $quantity,
string $productName,
string $shipmentName,
string $netValue,
string $taxAmount,
string $grossValue,
string $currencyCode
): void {
Assert::true(
$this->creditMemoDetailsPage->hasItem($quantity, $productName, $netValue, $grossValue, $taxAmount, $currencyCode)
$this->creditMemoDetailsPage->hasItem($quantity, $shipmentName, $netValue, $grossValue, $taxAmount, $currencyCode)
);
}

Expand Down

0 comments on commit af6d25e

Please sign in to comment.