Skip to content
Merged
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
11 changes: 11 additions & 0 deletions app/code/Magento/Sales/Model/Order/Creditmemo/Total/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Discount extends AbstractTotal
/**
* @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
{
Expand All @@ -26,6 +27,16 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
* basing on how much shipping should be refunded.
*/
$baseShippingAmount = $this->getBaseShippingAmount($creditmemo);

/**
* If credit memo's shipping amount is set and Order's shipping amount is 0,
* throw exception with different message
*/
if ($baseShippingAmount && $order->getBaseShippingAmount() <= 0) {
throw new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase("You can not refund shipping if there is no shipping amount.", [])
);
}
if ($baseShippingAmount) {
$baseShippingDiscount = $baseShippingAmount *
$order->getBaseShippingDiscountAmount() /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testCollect()
$this->orderMock->expects($this->once())
->method('getBaseShippingDiscountAmount')
->willReturn(1);
$this->orderMock->expects($this->exactly(2))
$this->orderMock->expects($this->exactly(3))
->method('getBaseShippingAmount')
->willReturn(1);
$this->orderMock->expects($this->once())
Expand Down Expand Up @@ -150,7 +150,7 @@ public function testCollectNoBaseShippingAmount()
$this->orderMock->expects($this->once())
->method('getBaseShippingDiscountAmount')
->willReturn(1);
$this->orderMock->expects($this->exactly(2))
$this->orderMock->expects($this->exactly(3))
->method('getBaseShippingAmount')
->willReturn(1);
$this->orderMock->expects($this->once())
Expand Down Expand Up @@ -269,4 +269,30 @@ public function testCollectZeroShipping()
);
$this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
}

/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage You can not refund shipping if there is no shipping amount.
*/
public function testCollectNonZeroShipping()
{
$this->creditmemoMock->expects($this->once())
->method('setDiscountAmount')
->willReturnSelf();
$this->creditmemoMock->expects($this->once())
->method('setBaseDiscountAmount')
->willReturnSelf();
$this->creditmemoMock->expects($this->once())
->method('getOrder')
->willReturn($this->orderMock);
$this->creditmemoMock->expects($this->once())
->method('getBaseShippingAmount')
->willReturn('10.0000');
$this->orderMock->expects($this->never())
->method('getBaseShippingDiscountAmount');
$this->orderMock->expects($this->once())
->method('getBaseShippingAmount')
->willReturn( '0.0000');
$this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
}
}