Skip to content

Commit

Permalink
refactor shipment line converter
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp authored and GSadee committed Mar 16, 2021
1 parent e7f9af0 commit 3f75385
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion features/viewing_details_of_credit_memo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 11 additions & 4 deletions spec/Converter/ShipmentLineItemsConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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%'
)]);
}

Expand Down
27 changes: 22 additions & 5 deletions src/Converter/ShipmentLineItemsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 3f75385

Please sign in to comment.