Skip to content

Commit a4bed5f

Browse files
authored
Merge pull request #94 from laurent35240/65_fix_partial_refund_shipping
#65 Fix for partial refund of shipping fees
2 parents 45fa882 + b871b52 commit a4bed5f

File tree

7 files changed

+80
-2
lines changed

7 files changed

+80
-2
lines changed

features/having_credit_memo_generated.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Feature: Having credit memo generated
1717
And there is a customer "rick.sanchez@wubba-lubba-dub-dub.com" that placed an order "#00000022"
1818
And the customer bought 2 "Mr. Meeseeks T-Shirt" products
1919
And the customer chose "Galaxy Post" shipping method to "United States" with "Space money" payment
20+
And there is a customer "morty.smith@look-at-me.com" that placed an order "#00000023"
21+
And the customer bought 2 "Mr. Meeseeks T-Shirt" products
22+
And the customer chose "Galaxy Post" shipping method to "United States" with "Space money" payment
23+
And the order "#00000023" is already paid
2024
And I am logged in as an administrator
2125
And the order "#00000022" is already paid
2226

@@ -44,3 +48,13 @@ Feature: Having credit memo generated
4448
Then this credit memo should contain 1 "Mr. Meeseeks T-Shirt" product with "$0.50" tax applied
4549
And it should be issued in "United States" channel
4650
And its total should be "$5.50"
51+
52+
53+
@ui @application
54+
Scenario: Seeing the details of generated credit memo with partial shipment price
55+
Given shipment from order "#00000023" has already "$4.50" refunded with "Space money" payment
56+
When I browse the details of the only credit memo generated for order "#00000023"
57+
And it should have sequential number generated from current date
58+
Then this credit memo should contain 1 "Galaxy Post" shipment with "$4.50" total
59+
And it should be issued in "United States" channel
60+
And its total should be "$4.50"

spec/Generator/ShipmentCreditMemoUnitGeneratorSpec.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,21 @@ function it_throws_exception_if_there_is_no_shipping_adjustment_with_given_id(
5050
->during('generate', [1])
5151
;
5252
}
53+
54+
function it_throws_exception_if_refund_amount_is_higher_than_shipping_amount(
55+
RepositoryInterface $adjustmentRepository,
56+
AdjustmentInterface $shippingAdjustment
57+
): void {
58+
$adjustmentRepository
59+
->findOneBy(['id' => 1, 'type' => AdjustmentInterface::SHIPPING_ADJUSTMENT])
60+
->willReturn($shippingAdjustment)
61+
;
62+
63+
$shippingAdjustment->getAmount()->willReturn(1000);
64+
65+
$this
66+
->shouldThrow(\InvalidArgumentException::class)
67+
->during('generate', [1, 1001])
68+
;
69+
}
5370
}

src/Generator/ShipmentCreditMemoUnitGenerator.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sylius\RefundPlugin\Generator;
66

77
use Sylius\Component\Core\Model\AdjustmentInterface;
8+
use Sylius\Component\Order\Model\AdjustmentInterface as OrderAdjustmentInterface;
89
use Sylius\Component\Resource\Repository\RepositoryInterface;
910
use Sylius\RefundPlugin\Entity\CreditMemoUnit;
1011
use Sylius\RefundPlugin\Entity\CreditMemoUnitInterface;
@@ -22,13 +23,23 @@ public function __construct(RepositoryInterface $adjustmentRepository)
2223

2324
public function generate(int $unitId, int $amount = null): CreditMemoUnitInterface
2425
{
25-
/** @var AdjustmentInterface $shippingAdjustment */
26+
/** @var OrderAdjustmentInterface $shippingAdjustment */
2627
$shippingAdjustment = $this
2728
->adjustmentRepository
2829
->findOneBy(['id' => $unitId, 'type' => AdjustmentInterface::SHIPPING_ADJUSTMENT])
2930
;
3031
Assert::notNull($shippingAdjustment);
3132

32-
return new CreditMemoUnit($shippingAdjustment->getLabel(), $shippingAdjustment->getAmount(), 0);
33+
$creditMemoUnitTotal = $this->getCreditMemoUnitTotal($shippingAdjustment, $amount);
34+
35+
return new CreditMemoUnit($shippingAdjustment->getLabel(), $creditMemoUnitTotal, 0);
36+
}
37+
38+
private function getCreditMemoUnitTotal(OrderAdjustmentInterface $shippingAdjustment, int $amount = null): int
39+
{
40+
Assert::lessThanEq($amount, $shippingAdjustment->getAmount());
41+
$creditMemoUnitTotal = null === $amount ? $shippingAdjustment->getAmount() : $amount;
42+
43+
return $creditMemoUnitTotal;
3344
}
3445
}

tests/Behat/Context/Application/CreditMemoContext.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ public function thisCreditMemoShouldContainProductWithTaxApplied(
7474
Assert::same($units[0]->getTaxesTotal(), $taxesTotal);
7575
}
7676

77+
/**
78+
* @Then /^this credit memo should contain (\d+) "([^"]+)" shipment with ("[^"]+") total$/
79+
*/
80+
public function thisCreditMemoShouldContainShipmentWithTotal(
81+
int $count,
82+
string $shipmentName,
83+
int $total
84+
): void {
85+
$units = $this->creditMemo->getUnits();
86+
87+
Assert::same(count($units), $count);
88+
Assert::same($units[0]->getProductName(), $shipmentName);
89+
Assert::same($units[0]->getTotal(), $total);
90+
}
91+
7792
/**
7893
* @Then it should be issued in :channelName channel
7994
*/

tests/Behat/Context/Ui/CreditMemoContext.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public function thisCreditMemoShouldContainProductWithTaxApplied(
125125
Assert::same($this->creditMemoDetailsPage->getUnitTax($count, $productName), $tax);
126126
}
127127

128+
/**
129+
* @Then this credit memo should contain :count :shipmentName shipment with :total total
130+
*/
131+
public function thisCreditMemoShouldContainShipmentWithTotal(
132+
int $count,
133+
string $shipmentName,
134+
string $total
135+
): void {
136+
Assert::same($this->creditMemoDetailsPage->countUnitsWithProduct($shipmentName), $count);
137+
Assert::same($this->creditMemoDetailsPage->getUnitTotal($count, $shipmentName), $total);
138+
}
139+
128140
/**
129141
* @Then it should have sequential number generated from current date
130142
*/

tests/Behat/Page/Admin/CreditMemoDetailsPage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public function getUnitTax(int $number, string $productName): string
3131
return $unit->find('css', '.credit-memo-unit-taxes-total')->getText();
3232
}
3333

34+
public function getUnitTotal(int $number, string $unitName): string
35+
{
36+
$unit = $this->getCreditMemoUnitsWithProduct($unitName)[0];
37+
38+
return $unit->find('css', '.credit-memo-unit-total')->getText();
39+
}
40+
3441
public function getNumber(): string
3542
{
3643
return $this->getDocument()->find('css', '#credit-memo-number')->getText();

tests/Behat/Page/Admin/CreditMemoDetailsPageInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function download(): void;
1414

1515
public function getUnitTax(int $number, string $productName): string;
1616

17+
public function getUnitTotal(int $number, string $unitName): string;
18+
1719
public function getNumber(): string;
1820

1921
public function getChannelName(): string;

0 commit comments

Comments
 (0)