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

[Credit Memo] Fix shipping cost calculation #187

Closed
Closed
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
12 changes: 12 additions & 0 deletions features/viewing_details_of_credit_memo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Feature: Viewing details of a credit memo
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 "US VAT-SHIPPING" tax rate of 15% for "Shipping" 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 "Galaxy Post" shipping method with "$10.00" fee
And shipping method "Galaxy Post" belongs to "Shipping" 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
Expand Down Expand Up @@ -55,3 +57,13 @@ Feature: Viewing details of a credit memo
And it should contain 1 "Galaxy Post" shipment with "4.50" gross value in "USD" currency
And it should be issued in "United States" channel
And its total should be "4.50" in "USD" currency

@ui @application
Scenario: Viewing details of a credit memo issued for a shipping cost refund
Given the "#00000022" order's shipping cost already has a refund of "$11.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 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 be issued in "United States" channel
And it should contain a tax item "15%" with amount "1.50" in "USD" currency
And its total should be "11.50" in "USD" currency
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add before this step:

And it should contain a tax item "15%" with amount "1.50" in "USD" currency

21 changes: 18 additions & 3 deletions src/Converter/ShipmentLineItemsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Sylius\RefundPlugin\Converter;

use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Repository\ShipmentRepositoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\LineItemInterface;
Expand All @@ -16,9 +18,15 @@ final class ShipmentLineItemsConverter implements LineItemsConverterInterface
/** @var RepositoryInterface */
private $adjustmentRepository;

public function __construct(RepositoryInterface $adjustmentRepository)
{
/** @var ShipmentRepositoryInterface */
private $shipmentRepository;

public function __construct(
RepositoryInterface $adjustmentRepository,
ShipmentRepositoryInterface $shipmentRepository
) {
$this->adjustmentRepository = $adjustmentRepository;
$this->shipmentRepository = $shipmentRepository;
}

public function convert(array $units): array
Expand All @@ -43,14 +51,21 @@ private function convertUnitRefundToLineItem(UnitRefundInterface $unitRefund): L
Assert::notNull($shippingAdjustment);
Assert::lessThanEq($unitRefund->total(), $shippingAdjustment->getAmount());

/** @var ShipmentInterface $shipment */
$shipment = $this->shipmentRepository->find(['id' => $unitRefund->id()]);
Assert::notNull($shipment);
/** @var AdjustmentInterface $adjustment */
$adjustment = $shipment->getUnits()->first()->getAdjustments()->first();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that the first adjustment of the first unit of a shipment always contains information about shipping costs?

$adjustment->getAmount();

return new LineItem(
$shippingAdjustment->getLabel(),
1,
$unitRefund->total(),
$unitRefund->total(),
$unitRefund->total(),
$unitRefund->total(),
0
$adjustment->getAmount()
);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/converter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<service id="Sylius\RefundPlugin\Converter\ShipmentLineItemsConverterInterface" class="Sylius\RefundPlugin\Converter\ShipmentLineItemsConverter">
<argument type="service" id="sylius.repository.adjustment" />
<argument type="service" id="sylius.repository.shipment"/>
</service>
</services>
</container>