Skip to content

Commit efd7f01

Browse files
committed
Format refund amount in the base currency
1 parent 26d89f4 commit efd7f01

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

app/code/Magento/Sales/Model/Order.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,17 @@ public function formatBasePricePrecision($price, $precision)
17841784
return $this->getBaseCurrency()->formatPrecision($price, $precision);
17851785
}
17861786

1787+
/**
1788+
* Retrieve text formatted base price value
1789+
*
1790+
* @param float $price
1791+
* @return string
1792+
*/
1793+
public function formatBasePriceTxt($price)
1794+
{
1795+
return $this->getBaseCurrency()->formatTxt($price);
1796+
}
1797+
17871798
/**
17881799
* Is currency different
17891800
*

app/code/Magento/Sales/Model/Service/CreditmemoService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected function validateForRefund(\Magento\Sales\Api\Data\CreditmemoInterface
202202
throw new \Magento\Framework\Exception\LocalizedException(
203203
__(
204204
'The most money available to refund is %1.',
205-
$creditmemo->getOrder()->formatPriceTxt($baseAvailableRefund)
205+
$creditmemo->getOrder()->formatBasePriceTxt($baseAvailableRefund)
206206
)
207207
);
208208
}

app/code/Magento/Sales/Test/Unit/Model/Service/CreditmemoServiceTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function testRefundExpectsMoneyAvailableToReturn()
351351
$order->method('getBaseTotalPaid')
352352
->willReturn($baseTotalPaid);
353353
$baseAvailableRefund = $baseTotalPaid - $baseTotalRefunded;
354-
$order->method('formatPriceTxt')
354+
$order->method('formatBasePriceTxt')
355355
->with($baseAvailableRefund)
356356
->willReturn($baseAvailableRefund);
357357
$this->creditmemoService->refund($creditMemo, true);
@@ -369,4 +369,51 @@ public function testRefundDoNotExpectsId()
369369
$creditMemoMock->expects($this->once())->method('getId')->willReturn(444);
370370
$this->creditmemoService->refund($creditMemoMock, true);
371371
}
372+
373+
/**
374+
* @expectedExceptionMessage The most money available to refund is $1.00.
375+
* @expectedException \Magento\Framework\Exception\LocalizedException
376+
*/
377+
public function testMultiCurrencyRefundExpectsMoneyAvailableToReturn()
378+
{
379+
$baseGrandTotal = 10.00;
380+
$baseTotalRefunded = 9.00;
381+
$baseTotalPaid = 10;
382+
$grandTotal = 8.81;
383+
$totalRefunded = 7.929;
384+
$totalPaid = 8.81;
385+
386+
/** @var CreditmemoInterface|MockObject $creditMemo */
387+
$creditMemo = $this->getMockBuilder(CreditmemoInterface::class)
388+
->setMethods(['getId', 'getOrder'])
389+
->getMockForAbstractClass();
390+
$creditMemo->method('getId')
391+
->willReturn(null);
392+
/** @var Order|MockObject $order */
393+
$order = $this->getMockBuilder(Order::class)
394+
->disableOriginalConstructor()
395+
->getMock();
396+
$creditMemo->method('getOrder')
397+
->willReturn($order);
398+
$creditMemo->method('getBaseGrandTotal')
399+
->willReturn($baseGrandTotal);
400+
$creditMemo->method('getGrandTotal')
401+
->willReturn($grandTotal);
402+
$order->method('getBaseTotalRefunded')
403+
->willReturn($baseTotalRefunded);
404+
$order->method('getTotalRefunded')
405+
->willReturn($totalRefunded);
406+
$this->priceCurrency->method('round')
407+
->withConsecutive([$baseTotalRefunded + $baseGrandTotal], [$baseTotalPaid])
408+
->willReturnOnConsecutiveCalls($baseTotalRefunded + $baseGrandTotal, $baseTotalPaid);
409+
$order->method('getBaseTotalPaid')
410+
->willReturn($baseTotalPaid);
411+
$order->method('getTotalPaid')
412+
->willReturn($totalPaid);
413+
$baseAvailableRefund = $baseTotalPaid - $baseTotalRefunded;
414+
$order->method('formatBasePriceTxt')
415+
->with($baseAvailableRefund)
416+
->willReturn(sprintf('$%.2f', $baseAvailableRefund));
417+
$this->creditmemoService->refund($creditMemo, true);
418+
}
372419
}

0 commit comments

Comments
 (0)