Skip to content

Commit 59886f4

Browse files
authored
ENGCOM-3294: Resolves 6731 by adding a custom message for refunding shipping #18844
2 parents c96b8af + e8efe57 commit 59886f4

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo/Total/Discount.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Discount extends AbstractTotal
1010
/**
1111
* @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
1212
* @return $this
13+
* @throws \Magento\Framework\Exception\LocalizedException
1314
*/
1415
public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
1516
{
@@ -26,6 +27,16 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
2627
* basing on how much shipping should be refunded.
2728
*/
2829
$baseShippingAmount = $this->getBaseShippingAmount($creditmemo);
30+
31+
/**
32+
* If credit memo's shipping amount is set and Order's shipping amount is 0,
33+
* throw exception with different message
34+
*/
35+
if ($baseShippingAmount && $order->getBaseShippingAmount() <= 0) {
36+
throw new \Magento\Framework\Exception\LocalizedException(
37+
new \Magento\Framework\Phrase("You can not refund shipping if there is no shipping amount.", [])
38+
);
39+
}
2940
if ($baseShippingAmount) {
3041
$baseShippingDiscount = $baseShippingAmount *
3142
$order->getBaseShippingDiscountAmount() /

app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/Total/DiscountTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testCollect()
7474
$this->orderMock->expects($this->once())
7575
->method('getBaseShippingDiscountAmount')
7676
->willReturn(1);
77-
$this->orderMock->expects($this->exactly(2))
77+
$this->orderMock->expects($this->exactly(3))
7878
->method('getBaseShippingAmount')
7979
->willReturn(1);
8080
$this->orderMock->expects($this->once())
@@ -150,7 +150,7 @@ public function testCollectNoBaseShippingAmount()
150150
$this->orderMock->expects($this->once())
151151
->method('getBaseShippingDiscountAmount')
152152
->willReturn(1);
153-
$this->orderMock->expects($this->exactly(2))
153+
$this->orderMock->expects($this->exactly(3))
154154
->method('getBaseShippingAmount')
155155
->willReturn(1);
156156
$this->orderMock->expects($this->once())
@@ -269,4 +269,30 @@ public function testCollectZeroShipping()
269269
);
270270
$this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
271271
}
272+
273+
/**
274+
* @expectedException \Magento\Framework\Exception\LocalizedException
275+
* @expectedExceptionMessage You can not refund shipping if there is no shipping amount.
276+
*/
277+
public function testCollectNonZeroShipping()
278+
{
279+
$this->creditmemoMock->expects($this->once())
280+
->method('setDiscountAmount')
281+
->willReturnSelf();
282+
$this->creditmemoMock->expects($this->once())
283+
->method('setBaseDiscountAmount')
284+
->willReturnSelf();
285+
$this->creditmemoMock->expects($this->once())
286+
->method('getOrder')
287+
->willReturn($this->orderMock);
288+
$this->creditmemoMock->expects($this->once())
289+
->method('getBaseShippingAmount')
290+
->willReturn('10.0000');
291+
$this->orderMock->expects($this->never())
292+
->method('getBaseShippingDiscountAmount');
293+
$this->orderMock->expects($this->once())
294+
->method('getBaseShippingAmount')
295+
->willReturn( '0.0000');
296+
$this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
297+
}
272298
}

0 commit comments

Comments
 (0)