From 3f753852f6dee3601dcf46b6da48cb654c9a889f Mon Sep 17 00:00:00 2001 From: Adam Kasperczak Date: Wed, 10 Mar 2021 14:29:14 +0100 Subject: [PATCH] refactor shipment line converter --- .../viewing_details_of_credit_memo.feature | 2 +- .../ShipmentLineItemsConverterSpec.php | 15 ++++++++--- src/Converter/ShipmentLineItemsConverter.php | 27 +++++++++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/features/viewing_details_of_credit_memo.feature b/features/viewing_details_of_credit_memo.feature index 0e6d8f72..b6e418c7 100644 --- a/features/viewing_details_of_credit_memo.feature +++ b/features/viewing_details_of_credit_memo.feature @@ -25,7 +25,7 @@ Feature: Viewing details of a credit memo And the order "#00000022" is already paid And I am logged in as an administrator - @ui + @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" diff --git a/spec/Converter/ShipmentLineItemsConverterSpec.php b/spec/Converter/ShipmentLineItemsConverterSpec.php index d725ced4..64d8fc0f 100644 --- a/spec/Converter/ShipmentLineItemsConverterSpec.php +++ b/spec/Converter/ShipmentLineItemsConverterSpec.php @@ -4,6 +4,7 @@ namespace spec\Sylius\RefundPlugin\Converter; +use Doctrine\Common\Collections\ArrayCollection; use PhpSpec\ObjectBehavior; use Sylius\Component\Resource\Repository\RepositoryInterface; use Sylius\RefundPlugin\Converter\LineItemsConverterInterface; @@ -36,19 +37,25 @@ function it_converts_shipment_unit_refunds_to_line_items( ->willReturn($shippingAdjustment) ; - $shippingAdjustment->getLabel()->willReturn('Galaxy post'); $shippingAdjustment->getShipment()->willReturn($shipment); + $shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->willReturn( new ArrayCollection([$shippingAdjustment->getWrappedObject()])); + + $shippingAdjustment->getDetails()->willReturn(['taxRateAmount' => 0.15]); + + $shippingAdjustment->getLabel()->willReturn('Galaxy post'); + $shipment->getAdjustmentsTotal()->willReturn(1000); $this->convert([$shipmentRefund])->shouldBeLike([new LineItem( 'Galaxy post', 1, + 425, 500, + 425, 500, - 500, - 500, - 0 + 75, + '15%' )]); } diff --git a/src/Converter/ShipmentLineItemsConverter.php b/src/Converter/ShipmentLineItemsConverter.php index faaffeab..ed23e47a 100644 --- a/src/Converter/ShipmentLineItemsConverter.php +++ b/src/Converter/ShipmentLineItemsConverter.php @@ -50,14 +50,31 @@ private function convertUnitRefundToLineItem(ShipmentRefund $shipmentRefund): Li Assert::isInstanceOf($shipment, AdjustableInterface::class); Assert::lessThanEq($shipmentRefund->total(), $shipment->getAdjustmentsTotal()); + $grossValue = $shipmentRefund->total(); + + $taxAdjustment = $shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->first(); + $taxRate = $this->getTaxRateAmount($taxAdjustment) . '%'; + $taxAmount = (int) ($grossValue * $this->getTaxRateAmount($taxAdjustment)); + $netValue = $grossValue - $taxAmount; + return new LineItem( $shippingAdjustment->getLabel(), 1, - $shipmentRefund->total(), - $shipmentRefund->total(), - $shipmentRefund->total(), - $shipmentRefund->total(), - 0 + $netValue, + $grossValue, + $netValue, + $grossValue, + $taxAmount, + $taxRate ); } + + private function getTaxRateAmount(AdjustmentInterface $adjustment): ?float + { + if (key_exists('taxRateAmount', $adjustment->getDetails())) { + return $adjustment->getDetails()['taxRateAmount']; + } + + return null; + } }