Skip to content

Commit fce6f4c

Browse files
ENGCOM-5529: Resolve Incorrect base currency in order grid for historical orders issue 23805 #23817
- Merge Pull Request #23817 from edenduong/magento2:2.3-bugfix/incorrent_currency_format_grid_issue23805 - Merged commits: 1. a52ae7e
2 parents cea3e79 + a52ae7e commit fce6f4c

File tree

4 files changed

+70
-36
lines changed

4 files changed

+70
-36
lines changed

app/code/Magento/Sales/Test/Unit/Ui/Component/Listing/Column/PriceTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Test\Unit\Ui\Component\Listing\Column;
79

8-
use Magento\Framework\Pricing\PriceCurrencyInterface;
10+
use Magento\Directory\Model\Currency;
911
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1012
use Magento\Sales\Ui\Component\Listing\Column\Price;
1113

@@ -20,9 +22,9 @@ class PriceTest extends \PHPUnit\Framework\TestCase
2022
protected $model;
2123

2224
/**
23-
* @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
25+
* @var Currency|\PHPUnit_Framework_MockObject_MockObject
2426
*/
25-
protected $priceFormatterMock;
27+
protected $currencyMock;
2628

2729
protected function setUp()
2830
{
@@ -33,12 +35,13 @@ protected function setUp()
3335
->disableOriginalConstructor()
3436
->getMock();
3537
$contextMock->expects($this->never())->method('getProcessor')->willReturn($processor);
36-
$this->priceFormatterMock = $this->getMockForAbstractClass(
37-
\Magento\Framework\Pricing\PriceCurrencyInterface::class
38-
);
38+
$this->currencyMock = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
39+
->setMethods(['load', 'format'])
40+
->disableOriginalConstructor()
41+
->getMock();
3942
$this->model = $objectManager->getObject(
4043
\Magento\Sales\Ui\Component\Listing\Column\Price::class,
41-
['priceFormatter' => $this->priceFormatterMock, 'context' => $contextMock]
44+
['currency' => $this->currencyMock, 'context' => $contextMock]
4245
);
4346
}
4447

@@ -50,14 +53,22 @@ public function testPrepareDataSource()
5053
$dataSource = [
5154
'data' => [
5255
'items' => [
53-
[$itemName => $oldItemValue]
56+
[
57+
$itemName => $oldItemValue,
58+
'base_currency_code' => 'US'
59+
]
5460
]
5561
]
5662
];
5763

58-
$this->priceFormatterMock->expects($this->once())
64+
$this->currencyMock->expects($this->once())
65+
->method('load')
66+
->with($dataSource['data']['items'][0]['base_currency_code'])
67+
->willReturnSelf();
68+
69+
$this->currencyMock->expects($this->once())
5970
->method('format')
60-
->with($oldItemValue, false)
71+
->with($oldItemValue, [], false)
6172
->willReturn($newItemValue);
6273

6374
$this->model->setData('name', $itemName);

app/code/Magento/Sales/Test/Unit/Ui/Component/Listing/Column/PurchasedPriceTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Test\Unit\Ui\Component\Listing\Column;
79

8-
use Magento\Framework\Pricing\PriceCurrencyInterface;
10+
use Magento\Directory\Model\Currency;
911
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1012
use Magento\Sales\Ui\Component\Listing\Column\Price;
1113

@@ -20,9 +22,9 @@ class PurchasedPriceTest extends \PHPUnit\Framework\TestCase
2022
protected $model;
2123

2224
/**
23-
* @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
25+
* @var Currency|\PHPUnit_Framework_MockObject_MockObject
2426
*/
25-
protected $priceFormatterMock;
27+
protected $currencyMock;
2628

2729
protected function setUp()
2830
{
@@ -33,12 +35,13 @@ protected function setUp()
3335
->disableOriginalConstructor()
3436
->getMock();
3537
$contextMock->expects($this->never())->method('getProcessor')->willReturn($processor);
36-
$this->priceFormatterMock = $this->getMockForAbstractClass(
37-
\Magento\Framework\Pricing\PriceCurrencyInterface::class
38-
);
38+
$this->currencyMock = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
39+
->setMethods(['load', 'format'])
40+
->disableOriginalConstructor()
41+
->getMock();
3942
$this->model = $objectManager->getObject(
4043
\Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice::class,
41-
['priceFormatter' => $this->priceFormatterMock, 'context' => $contextMock]
44+
['currency' => $this->currencyMock, 'context' => $contextMock]
4245
);
4346
}
4447

@@ -58,9 +61,14 @@ public function testPrepareDataSource()
5861
]
5962
];
6063

61-
$this->priceFormatterMock->expects($this->once())
64+
$this->currencyMock->expects($this->once())
65+
->method('load')
66+
->with($dataSource['data']['items'][0]['order_currency_code'])
67+
->willReturnSelf();
68+
69+
$this->currencyMock->expects($this->once())
6270
->method('format')
63-
->with($oldItemValue, false, null, null, 'US')
71+
->with($oldItemValue, [], false)
6472
->willReturn($newItemValue);
6573

6674
$this->model->setData('name', $itemName);

app/code/Magento/Sales/Ui/Component/Listing/Column/Price.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Ui\Component\Listing\Column;
79

810
use Magento\Framework\View\Element\UiComponent\ContextInterface;
911
use Magento\Framework\View\Element\UiComponentFactory;
1012
use Magento\Ui\Component\Listing\Columns\Column;
1113
use Magento\Framework\Pricing\PriceCurrencyInterface;
14+
use Magento\Directory\Model\Currency;
1215

1316
/**
1417
* Class Price
@@ -20,6 +23,11 @@ class Price extends Column
2023
*/
2124
protected $priceFormatter;
2225

26+
/**
27+
* @var Currency
28+
*/
29+
private $currency;
30+
2331
/**
2432
* Constructor
2533
*
@@ -28,15 +36,19 @@ class Price extends Column
2836
* @param PriceCurrencyInterface $priceFormatter
2937
* @param array $components
3038
* @param array $data
39+
* @param Currency $currency
3140
*/
3241
public function __construct(
3342
ContextInterface $context,
3443
UiComponentFactory $uiComponentFactory,
3544
PriceCurrencyInterface $priceFormatter,
3645
array $components = [],
37-
array $data = []
46+
array $data = [],
47+
Currency $currency = null
3848
) {
3949
$this->priceFormatter = $priceFormatter;
50+
$this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
51+
->create(Currency::class);
4052
parent::__construct($context, $uiComponentFactory, $components, $data);
4153
}
4254

@@ -51,13 +63,9 @@ public function prepareDataSource(array $dataSource)
5163
if (isset($dataSource['data']['items'])) {
5264
foreach ($dataSource['data']['items'] as & $item) {
5365
$currencyCode = isset($item['base_currency_code']) ? $item['base_currency_code'] : null;
54-
$item[$this->getData('name')] = $this->priceFormatter->format(
55-
$item[$this->getData('name')],
56-
false,
57-
null,
58-
null,
59-
$currencyCode
60-
);
66+
$basePurchaseCurrency = $this->currency->load($currencyCode);
67+
$item[$this->getData('name')] = $basePurchaseCurrency
68+
->format($item[$this->getData('name')], [], false);
6169
}
6270
}
6371

app/code/Magento/Sales/Ui/Component/Listing/Column/PurchasedPrice.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Ui\Component\Listing\Column;
79

810
use Magento\Framework\View\Element\UiComponent\ContextInterface;
911
use Magento\Framework\View\Element\UiComponentFactory;
1012
use Magento\Ui\Component\Listing\Columns\Column;
1113
use Magento\Framework\Pricing\PriceCurrencyInterface;
14+
use Magento\Directory\Model\Currency;
1215

1316
/**
1417
* Class Price
@@ -20,6 +23,11 @@ class PurchasedPrice extends Column
2023
*/
2124
protected $priceFormatter;
2225

26+
/**
27+
* @var Currency
28+
*/
29+
private $currency;
30+
2331
/**
2432
* Constructor
2533
*
@@ -28,15 +36,19 @@ class PurchasedPrice extends Column
2836
* @param PriceCurrencyInterface $priceFormatter
2937
* @param array $components
3038
* @param array $data
39+
* @param Currency $currency
3140
*/
3241
public function __construct(
3342
ContextInterface $context,
3443
UiComponentFactory $uiComponentFactory,
3544
PriceCurrencyInterface $priceFormatter,
3645
array $components = [],
37-
array $data = []
46+
array $data = [],
47+
Currency $currency = null
3848
) {
3949
$this->priceFormatter = $priceFormatter;
50+
$this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
51+
->create(Currency::class);
4052
parent::__construct($context, $uiComponentFactory, $components, $data);
4153
}
4254

@@ -51,14 +63,9 @@ public function prepareDataSource(array $dataSource)
5163
if (isset($dataSource['data']['items'])) {
5264
foreach ($dataSource['data']['items'] as & $item) {
5365
$currencyCode = isset($item['order_currency_code']) ? $item['order_currency_code'] : null;
54-
$item[$this->getData('name')] =
55-
$this->priceFormatter->format(
56-
$item[$this->getData('name')],
57-
false,
58-
null,
59-
null,
60-
$currencyCode
61-
);
66+
$purchaseCurrency = $this->currency->load($currencyCode);
67+
$item[$this->getData('name')] = $purchaseCurrency
68+
->format($item[$this->getData('name')], [], false);
6269
}
6370
}
6471

0 commit comments

Comments
 (0)