Skip to content

Commit 3174a69

Browse files
committed
MC-23915: Wrong currency symbol in creditmemo_grid & sales_order_view > creditmemo grid for subtotal & Shipping and Handling fee
1 parent 940d74a commit 3174a69

File tree

7 files changed

+168
-235
lines changed

7 files changed

+168
-235
lines changed

app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,32 @@ public function __construct(
3333
) {
3434
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
3535
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
protected function _translateCondition($field, $condition)
41+
{
42+
if ($field !== 'order_currency_code'
43+
&& !isset($this->_map['fields'][$field])
44+
) {
45+
$this->_map['fields'][$field] = 'main_table.' . $field;
46+
}
47+
48+
return parent::_translateCondition($field, $condition);
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
protected function _renderFiltersBefore()
55+
{
56+
$this->getSelect()->join(
57+
['cgf' => $this->getTable('sales_order_grid')],
58+
'main_table.order_id = cgf.entity_id',
59+
[
60+
'order_currency_code'=>'order_currency_code'
61+
]
62+
);
63+
}
3664
}

app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Order/Grid/Collection.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,32 @@ public function __construct(
3333
) {
3434
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
3535
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
protected function _translateCondition($field, $condition)
41+
{
42+
if ($field !== 'order_currency_code'
43+
&& !isset($this->_map['fields'][$field])
44+
) {
45+
$this->_map['fields'][$field] = 'main_table.' . $field;
46+
}
47+
48+
return parent::_translateCondition($field, $condition);
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
protected function _renderFiltersBefore()
55+
{
56+
$this->getSelect()->join(
57+
['cgf' => $this->getTable('sales_order_grid')],
58+
'main_table.order_id = cgf.entity_id',
59+
[
60+
'order_currency_code'=>'order_currency_code'
61+
]
62+
);
63+
}
3664
}

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

Lines changed: 18 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,25 @@
88
namespace Magento\Sales\Test\Unit\Ui\Component\Listing\Column;
99

1010
use Magento\Directory\Model\Currency;
11-
use Magento\Sales\Api\Data\OrderInterface;
12-
use Magento\Sales\Api\Data\OrderSearchResultInterface;
13-
use Magento\Framework\Api\SearchCriteria;
14-
use Magento\Framework\Api\SearchCriteriaBuilder;
1511
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1612
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1713
use Magento\Framework\View\Element\UiComponent\Processor;
18-
use Magento\Sales\Api\OrderRepositoryInterface;
1914
use Magento\Sales\Ui\Component\Listing\Column\Price;
2015
use Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice;
2116
use PHPUnit\Framework\MockObject\MockObject;
2217
use PHPUnit\Framework\TestCase;
2318

24-
/**
25-
* Test for \Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice
26-
*/
2719
class PurchasedPriceTest extends TestCase
2820
{
2921
/**
3022
* @var Price
3123
*/
32-
private $model;
24+
protected $model;
3325

3426
/**
3527
* @var Currency|MockObject
3628
*/
37-
private $currencyMock;
38-
39-
/**
40-
* @var OrderRepositoryInterface|MockObject
41-
*/
42-
private $orderRepository;
43-
44-
/**
45-
* @var SearchCriteria|MockObject
46-
*/
47-
private $searchCriteriaMock;
48-
49-
/**
50-
* @var OrderSearchResultInterface|MockObject
51-
*/
52-
private $orderSearchResultMock;
53-
54-
/**
55-
* @var OrderInterface|MockObject
56-
*/
57-
private $order;
29+
protected $currencyMock;
5830

5931
protected function setUp(): void
6032
{
@@ -69,116 +41,40 @@ protected function setUp(): void
6941
->setMethods(['load', 'format'])
7042
->disableOriginalConstructor()
7143
->getMock();
72-
$this->orderRepository = $this->getMockForAbstractClass(OrderRepositoryInterface::class);
73-
$this->order = $this->getMockForAbstractClass(OrderInterface::class);
74-
$this->orderSearchResultMock = $this->getMockForAbstractClass(OrderSearchResultInterface::class);
75-
$this->searchCriteriaMock = $this->createMock(SearchCriteria::class);
76-
$searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
77-
$searchCriteriaBuilderMock->expects($this->once())
78-
->method('addFilter')
79-
->willReturnSelf();
80-
81-
$searchCriteriaBuilderMock->expects($this->once())
82-
->method('create')
83-
->willReturn($this->searchCriteriaMock);
84-
8544
$this->model = $objectManager->getObject(
8645
PurchasedPrice::class,
87-
[
88-
'currency' => $this->currencyMock,
89-
'context' => $contextMock,
90-
'orderRepository' => $this->orderRepository,
91-
'order' => $this->order,
92-
'searchCriteriaBuilder' => $searchCriteriaBuilderMock,
93-
'searchCriteria' => $this->searchCriteriaMock,
94-
'orderSearchResult' => $this->orderSearchResultMock,
95-
]
46+
['currency' => $this->currencyMock, 'context' => $contextMock]
9647
);
9748
}
9849

99-
/**
100-
* @param array $orderData
101-
* @param array $dataSource
102-
* @dataProvider prepareDataSourceDataProvider
103-
*/
104-
public function testPrepareDataSource(array $orderData,array $dataSource): void
50+
public function testPrepareDataSource()
10551
{
52+
$itemName = 'itemName';
10653
$oldItemValue = 'oldItemValue';
10754
$newItemValue = 'newItemValue';
108-
109-
$this->orderRepository->expects($this->once())
110-
->method('getList')
111-
->with($this->searchCriteriaMock)
112-
->willReturn($this->orderSearchResultMock);
113-
114-
$this->orderSearchResultMock->expects($this->once())
115-
->method('getItems')
116-
->willReturn([$this->order]);
117-
118-
$this->order->expects($this->once())
119-
->method('getEntityId')
120-
->willReturn($orderData['entity_id']);
121-
122-
$this->order->expects($this->once())
123-
->method('getOrderCurrencyCode')
124-
->willReturn($orderData['order_currency_code']);
125-
126-
$currencyCode = $dataSource['data']['items'][0]['order_currency_code'] ?? $orderData['order_currency_code'];
55+
$dataSource = [
56+
'data' => [
57+
'items' => [
58+
[
59+
$itemName => $oldItemValue,
60+
'order_currency_code' => 'US'
61+
]
62+
]
63+
]
64+
];
12765

12866
$this->currencyMock->expects($this->once())
12967
->method('load')
130-
->with($currencyCode)
68+
->with($dataSource['data']['items'][0]['order_currency_code'])
13169
->willReturnSelf();
13270

13371
$this->currencyMock->expects($this->once())
13472
->method('format')
13573
->with($oldItemValue, [], false)
13674
->willReturn($newItemValue);
13775

138-
$this->model->setData('name', 'item_name');
76+
$this->model->setData('name', $itemName);
13977
$dataSource = $this->model->prepareDataSource($dataSource);
140-
$this->assertEquals($newItemValue, $dataSource['data']['items'][0]['item_name']);
141-
}
142-
143-
/**
144-
* @return array
145-
*/
146-
public function prepareDataSourceDataProvider(): array
147-
{
148-
return [
149-
[
150-
'orderData' => [
151-
'entity_id' => 1,
152-
'order_currency_code' => 'US',
153-
],
154-
'dataSource' => [
155-
'data' => [
156-
'items' => [
157-
[
158-
'item_name' => 'oldItemValue',
159-
'order_currency_code' => 'US',
160-
'order_id' => 1,
161-
]
162-
]
163-
]
164-
]
165-
],
166-
[
167-
'orderData' => [
168-
'entity_id' => 1,
169-
'order_currency_code' => 'FR',
170-
],
171-
'dataSource' => [
172-
'data' => [
173-
'items' => [
174-
[
175-
'item_name' => 'oldItemValue',
176-
'order_id' => 1,
177-
]
178-
]
179-
]
180-
]
181-
]
182-
];
78+
$this->assertEquals($newItemValue, $dataSource['data']['items'][0][$itemName]);
18379
}
18480
}

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

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
namespace Magento\Sales\Ui\Component\Listing\Column;
99

10-
use Magento\Framework\Api\SearchCriteriaBuilder;
1110
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1211
use Magento\Framework\View\Element\UiComponentFactory;
13-
use Magento\Sales\Api\OrderRepositoryInterface;
1412
use Magento\Ui\Component\Listing\Columns\Column;
1513
use Magento\Framework\Pricing\PriceCurrencyInterface;
1614
use Magento\Directory\Model\Currency;
@@ -30,16 +28,6 @@ class PurchasedPrice extends Column
3028
*/
3129
private $currency;
3230

33-
/**
34-
* @var OrderRepositoryInterface
35-
*/
36-
private $orderRepository;
37-
38-
/**
39-
* @var SearchCriteriaBuilder
40-
*/
41-
private $searchCriteriaBuilder;
42-
4331
/**
4432
* Constructor
4533
*
@@ -49,30 +37,21 @@ class PurchasedPrice extends Column
4937
* @param array $components
5038
* @param array $data
5139
* @param Currency $currency
52-
* @param OrderRepositoryInterface $orderRepository
53-
* @param SearchCriteriaBuilder $searchCriteriaBuilder
5440
*/
5541
public function __construct(
5642
ContextInterface $context,
5743
UiComponentFactory $uiComponentFactory,
5844
PriceCurrencyInterface $priceFormatter,
5945
array $components = [],
6046
array $data = [],
61-
Currency $currency = null,
62-
OrderRepositoryInterface $orderRepository = null,
63-
SearchCriteriaBuilder $searchCriteriaBuilder = null
47+
Currency $currency = null
6448
) {
6549
$this->priceFormatter = $priceFormatter;
6650
$this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
6751
->create(Currency::class);
68-
$this->orderRepository = $orderRepository ?: \Magento\Framework\App\ObjectManager::getInstance()
69-
->create(OrderRepositoryInterface::class);
70-
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: \Magento\Framework\App\ObjectManager::getInstance()
71-
->create(SearchCriteriaBuilder::class);
7252
parent::__construct($context, $uiComponentFactory, $components, $data);
7353
}
7454

75-
7655
/**
7756
* Prepare Data Source
7857
*
@@ -82,10 +61,8 @@ public function __construct(
8261
public function prepareDataSource(array $dataSource)
8362
{
8463
if (isset($dataSource['data']['items'])) {
85-
$orderIds = array_column($dataSource['data']['items'],'order_id');
86-
$orderCurrencyCodes = $this->getOrdersCurrency($orderIds);
8764
foreach ($dataSource['data']['items'] as & $item) {
88-
$currencyCode = $item['order_currency_code'] ?? $orderCurrencyCodes[$item['order_id']];
65+
$currencyCode = isset($item['order_currency_code']) ? $item['order_currency_code'] : null;
8966
$purchaseCurrency = $this->currency->load($currencyCode);
9067
$item[$this->getData('name')] = $purchaseCurrency
9168
->format($item[$this->getData('name')], [], false);
@@ -94,23 +71,4 @@ public function prepareDataSource(array $dataSource)
9471

9572
return $dataSource;
9673
}
97-
98-
/**
99-
* @param array $orderIds
100-
* @return array
101-
*/
102-
private function getOrdersCurrency(array $orderIds): array
103-
{
104-
$orderCurrencyCodes = [];
105-
106-
$searchCriteria = $this->searchCriteriaBuilder
107-
->addFilter('entity_id', $orderIds ,'in')
108-
->create();
109-
110-
foreach ($this->orderRepository->getList($searchCriteria)->getItems() as $order) {
111-
$orderCurrencyCodes[$order->getEntityId()] = $order->getOrderCurrencyCode();
112-
}
113-
114-
return $orderCurrencyCodes;
115-
}
11674
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\ResourceModel\Order\Creditmemo\Grid;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Test Magento\Sales\Model\ResourceModel\Order\Creditmemo\Grid
16+
*/
17+
class CollectionTest extends TestCase
18+
{
19+
/**
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
protected function setUp(): void
28+
{
29+
$this->objectManager = Bootstrap::getObjectManager();
30+
}
31+
32+
/**
33+
* @magentoDataFixture Magento/Sales/_files/creditmemo_list.php
34+
*
35+
* @return void
36+
*/
37+
public function testCollectionOrderCurrencyCodeExist(): void
38+
{
39+
/** @var $collection Collection */
40+
$collection = $this->objectManager->get(Collection::class);
41+
$collection->addFieldToFilter('increment_id', ['eq' => '456']);
42+
foreach ($collection as $item) {
43+
$this->assertNotNull($item->getOrderCurrencyCode());
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)