Skip to content

Commit 4dad4bc

Browse files
committed
ACP2E-56 : [On Prem] Magento invoice order date filter not working
1 parent 315445e commit 4dad4bc

File tree

2 files changed

+172
-4
lines changed

2 files changed

+172
-4
lines changed

app/code/Magento/Sales/Plugin/Model/ResourceModel/Order/OrderGridCollectionFilter.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,41 @@ public function aroundAddFieldToFilter(
4444
$field,
4545
$condition = null
4646
) {
47+
$fieldMap = $this->getFilterFieldsMap();
48+
$fieldName = $fieldMap['fields'][$field] ?? null;
49+
if (!$fieldName) {
50+
return $proceed($field, $condition);
51+
}
4752

4853
if ($field === 'created_at' || $field === 'order_created_at') {
4954
if (is_array($condition)) {
5055
foreach ($condition as $key => $value) {
5156
$condition[$key] = $this->timeZone->convertConfigTimeToUtc($value);
5257
}
5358
}
59+
60+
$fieldName = $subject->getConnection()->quoteIdentifier($field);
61+
$condition = $subject->getConnection()->prepareSqlCondition($fieldName, $condition);
62+
$subject->getSelect()->where($condition, null, Select::TYPE_CONDITION);
63+
64+
return $subject;
5465
}
5566

56-
$fieldName = $subject->getConnection()->quoteIdentifier($field);
57-
$condition = $subject->getConnection()->prepareSqlCondition($fieldName, $condition);
58-
$subject->getSelect()->where($condition, null, Select::TYPE_CONDITION);
67+
return $proceed();
68+
}
5969

60-
return $subject;
70+
/**
71+
* Map the columns needs to filter out
72+
*
73+
* @return \string[][]
74+
*/
75+
private function getFilterFieldsMap(): array
76+
{
77+
return [
78+
'fields' => [
79+
'created_at' => 'main_table.created_at',
80+
'order_created_at' => 'main_table.order_created_at',
81+
],
82+
];
6183
}
6284
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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\Plugin\Model\ResourceModel\Order;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
12+
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
13+
use Magento\Sales\Model\ResourceModel\Order\Creditmemo;
14+
use Magento\Sales\Model\ResourceModel\Order\Invoice;
15+
use Magento\Sales\Model\ResourceModel\Order\Shipment;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class OrderGridCollectionFilterTest extends TestCase
20+
{
21+
/**
22+
* @var ObjectManagerInterface
23+
*/
24+
private ObjectManagerInterface $objectManager;
25+
26+
/**
27+
* @var TimezoneInterface
28+
*/
29+
private $timeZone;
30+
31+
/**
32+
* @var OrderGridCollectionFilter
33+
*/
34+
private $plugin;
35+
36+
/**
37+
* @var SearchResult
38+
*/
39+
private $searchResult;
40+
41+
/**
42+
* @var \Closure
43+
*/
44+
private $proceed;
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
protected function setUp(): void
50+
{
51+
$this->objectManager = Bootstrap::getObjectManager();
52+
$this->timeZone = $this->objectManager->get(TimezoneInterface::class);
53+
$this->proceed = function () {
54+
$this->proceed;
55+
};
56+
$this->plugin = $this->objectManager->create(
57+
OrderGridCollectionFilter::class,
58+
[
59+
'timezoneInterface' => $this->timeZone
60+
]
61+
);
62+
}
63+
64+
/**
65+
* Verifies that filter condition date is being converted to config timezone before select sql query
66+
*
67+
* @dataProvider getCollectionFiltersDataProvider
68+
* @param $mainTable
69+
* @param $resourceModel
70+
* @param $field
71+
* @throws \Magento\Framework\Exception\LocalizedException
72+
*/
73+
public function testAroundAddFieldToFilter($mainTable, $resourceModel, $field): void
74+
{
75+
$filterDate = "2021-12-13 00:00:00";
76+
$convertedDate = $this->timeZone->convertConfigTimeToUtc($filterDate);
77+
78+
$this->searchResult = $this->objectManager->create(
79+
SearchResult::class,
80+
[
81+
'mainTable' => $mainTable,
82+
'resourceModel' => $resourceModel
83+
]
84+
);
85+
$result = $this->plugin->aroundAddFieldToFilter($this->searchResult, $this->proceed, $field, ['qteq' => $filterDate]);
86+
87+
$expectedSelect = "SELECT `main_table`.* FROM `{$mainTable}` AS `main_table` " .
88+
"WHERE (((`{$field}` = '{$convertedDate}')))";
89+
90+
$this->assertEquals($expectedSelect, $result->getSelectSql(true));
91+
}
92+
93+
/**
94+
* @return array
95+
*/
96+
public function getCollectionFiltersDataProvider2(): array
97+
{
98+
return [
99+
'shipment_grid_collection_for_created_at' => [
100+
'mainTable' => 'sales_shipment_grid',
101+
'resourceModel' => Shipment::class,
102+
'field' => 'created_at',
103+
],
104+
'shipment_grid_collection_for_order_created_at' => [
105+
'mainTable' => 'sales_shipment_grid',
106+
'resourceModel' => Shipment::class,
107+
'field' => 'order_created_at',
108+
]
109+
];
110+
}
111+
public function getCollectionFiltersDataProvider(): array
112+
{
113+
return [
114+
'invoice_grid_collection_for_created_at' => [
115+
'mainTable' => 'sales_invoice_grid',
116+
'resourceModel' => Invoice::class,
117+
'field' => 'created_at',
118+
],
119+
'invoice_grid_collection_for_order_created_at' => [
120+
'mainTable' => 'sales_invoice_grid',
121+
'resourceModel' => Invoice::class,
122+
'field' => 'order_created_at',
123+
],
124+
'shipment_grid_collection_for_created_at' => [
125+
'mainTable' => 'sales_shipment_grid',
126+
'resourceModel' => Shipment::class,
127+
'field' => 'created_at',
128+
],
129+
'shipment_grid_collection_for_order_created_at' => [
130+
'mainTable' => 'sales_shipment_grid',
131+
'resourceModel' => Shipment::class,
132+
'field' => 'order_created_at',
133+
],
134+
'creditmemo_grid_collection_for_created_at' => [
135+
'mainTable' => 'sales_creditmemo_grid',
136+
'resourceModel' => Creditmemo::class,
137+
'field' => 'created_at',
138+
],
139+
'creditmemo_grid_collection_for_order_created_at' => [
140+
'mainTable' => 'sales_creditmemo_grid',
141+
'resourceModel' => Creditmemo::class,
142+
'field' => 'order_created_at',
143+
]
144+
];
145+
}
146+
}

0 commit comments

Comments
 (0)