Skip to content

Commit 177b61d

Browse files
author
Joan He
committed
MAGETWO-91710: Creditmemo Grand Total is incorrect if discount is applied on shipping
1 parent 919dd8c commit 177b61d

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
2525
* Calculate how much shipping discount should be applied
2626
* basing on how much shipping should be refunded.
2727
*/
28-
$baseShippingAmount = (float)$creditmemo->getBaseShippingAmount();
28+
$baseShippingAmount = $this->getBaseShippingAmount($creditmemo);
2929
if ($baseShippingAmount) {
3030
$baseShippingDiscount = $baseShippingAmount *
3131
$order->getBaseShippingDiscountAmount() /
@@ -75,4 +75,21 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
7575
$creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalDiscountAmount);
7676
return $this;
7777
}
78+
79+
/**
80+
* Get base shipping amount
81+
*
82+
* @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
83+
* @return float
84+
*/
85+
private function getBaseShippingAmount(\Magento\Sales\Model\Order\Creditmemo $creditmemo): float
86+
{
87+
$baseShippingAmount = (float)$creditmemo->getBaseShippingAmount();
88+
if (!$baseShippingAmount) {
89+
$baseShippingInclTax = (float)$creditmemo->getBaseShippingInclTax();
90+
$baseShippingTaxAmount = (float)$creditmemo->getBaseShippingTaxAmount();
91+
$baseShippingAmount = $baseShippingInclTax - $baseShippingTaxAmount;
92+
}
93+
return $baseShippingAmount;
94+
}
7895
}

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

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function setUp()
4848
]);
4949
$this->creditmemoMock = $this->createPartialMock(\Magento\Sales\Model\Order\Creditmemo::class, [
5050
'setBaseCost', 'getAllItems', 'getOrder', 'getBaseShippingAmount', 'roundPrice',
51-
'setDiscountAmount', 'setBaseDiscountAmount'
51+
'setDiscountAmount', 'setBaseDiscountAmount', 'getBaseShippingInclTax', 'getBaseShippingTaxAmount'
5252
]);
5353
$this->creditmemoItemMock = $this->createPartialMock(\Magento\Sales\Model\Order\Creditmemo\Item::class, [
5454
'getHasChildren', 'getBaseCost', 'getQty', 'getOrderItem', 'setDiscountAmount',
@@ -127,6 +127,82 @@ public function testCollect()
127127
$this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
128128
}
129129

130+
public function testCollectNoBaseShippingAmount()
131+
{
132+
$this->creditmemoMock->expects($this->exactly(2))
133+
->method('setDiscountAmount')
134+
->willReturnSelf();
135+
$this->creditmemoMock->expects($this->exactly(2))
136+
->method('setBaseDiscountAmount')
137+
->willReturnSelf();
138+
$this->creditmemoMock->expects($this->once())
139+
->method('getOrder')
140+
->willReturn($this->orderMock);
141+
$this->creditmemoMock->expects($this->once())
142+
->method('getBaseShippingAmount')
143+
->willReturn(0);
144+
$this->creditmemoMock->expects($this->once())
145+
->method('getBaseShippingInclTax')
146+
->willReturn(1);
147+
$this->creditmemoMock->expects($this->once())
148+
->method('getBaseShippingTaxAmount')
149+
->willReturn(0);
150+
$this->orderMock->expects($this->once())
151+
->method('getBaseShippingDiscountAmount')
152+
->willReturn(1);
153+
$this->orderMock->expects($this->exactly(2))
154+
->method('getBaseShippingAmount')
155+
->willReturn(1);
156+
$this->orderMock->expects($this->once())
157+
->method('getShippingAmount')
158+
->willReturn(1);
159+
$this->creditmemoMock->expects($this->once())
160+
->method('getAllItems')
161+
->willReturn([$this->creditmemoItemMock]);
162+
$this->creditmemoItemMock->expects($this->atLeastOnce())
163+
->method('getOrderItem')
164+
->willReturn($this->orderItemMock);
165+
$this->orderItemMock->expects($this->once())
166+
->method('isDummy')
167+
->willReturn(false);
168+
$this->orderItemMock->expects($this->once())
169+
->method('getDiscountInvoiced')
170+
->willReturn(1);
171+
$this->orderItemMock->expects($this->once())
172+
->method('getBaseDiscountInvoiced')
173+
->willReturn(1);
174+
$this->orderItemMock->expects($this->once())
175+
->method('getQtyInvoiced')
176+
->willReturn(1);
177+
$this->orderItemMock->expects($this->once())
178+
->method('getDiscountRefunded')
179+
->willReturn(1);
180+
$this->orderItemMock->expects($this->once())
181+
->method('getQtyRefunded')
182+
->willReturn(0);
183+
$this->creditmemoItemMock->expects($this->once())
184+
->method('isLast')
185+
->willReturn(false);
186+
$this->creditmemoItemMock->expects($this->atLeastOnce())
187+
->method('getQty')
188+
->willReturn(1);
189+
$this->creditmemoItemMock->expects($this->exactly(1))
190+
->method('setDiscountAmount')
191+
->willReturnSelf();
192+
$this->creditmemoItemMock->expects($this->exactly(1))
193+
->method('setBaseDiscountAmount')
194+
->willReturnSelf();
195+
$this->creditmemoMock->expects($this->exactly(2))
196+
->method('roundPrice')
197+
->willReturnMap(
198+
[
199+
[1, 'regular', true, 1],
200+
[1, 'base', true, 1]
201+
]
202+
);
203+
$this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
204+
}
205+
130206
public function testCollectZeroShipping()
131207
{
132208
$this->creditmemoMock->expects($this->exactly(2))

0 commit comments

Comments
 (0)