Skip to content

Commit 713c8a2

Browse files
committed
ACP2E-4239: Date Filter not working according to store timezone on Credit Memos grid
1 parent 0f44b6f commit 713c8a2

File tree

6 files changed

+57
-147
lines changed

6 files changed

+57
-147
lines changed

app/code/Magento/Customer/Model/ResourceModel/Grid/Collection.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2016 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Customer\Model\ResourceModel\Grid;
77

88
use Magento\Customer\Model\ResourceModel\Customer;
99
use Magento\Customer\Ui\Component\DataProvider\Document;
10-
use Magento\Framework\App\ObjectManager;
1110
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
1211
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
1312
use Magento\Framework\Event\ManagerInterface as EventManager;
13+
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Framework\Locale\ResolverInterface;
15-
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1615
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
1716
use Psr\Log\LoggerInterface as Logger;
1817

@@ -26,11 +25,6 @@ class Collection extends SearchResult
2625
*/
2726
private $localeResolver;
2827

29-
/**
30-
* @var TimezoneInterface
31-
*/
32-
private $timeZone;
33-
3428
/**
3529
* @inheritdoc
3630
*/
@@ -49,7 +43,7 @@ class Collection extends SearchResult
4943
* @param ResolverInterface $localeResolver
5044
* @param string $mainTable
5145
* @param string $resourceModel
52-
* @param TimezoneInterface|null $timeZone
46+
* @throws LocalizedException
5347
*/
5448
public function __construct(
5549
EntityFactory $entityFactory,
@@ -58,13 +52,10 @@ public function __construct(
5852
EventManager $eventManager,
5953
ResolverInterface $localeResolver,
6054
$mainTable = 'customer_grid_flat',
61-
$resourceModel = Customer::class,
62-
?TimezoneInterface $timeZone = null
55+
$resourceModel = Customer::class
6356
) {
6457
$this->localeResolver = $localeResolver;
6558
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
66-
$this->timeZone = $timeZone ?: ObjectManager::getInstance()
67-
->get(TimezoneInterface::class);
6859
}
6960

7061
/**
@@ -92,14 +83,6 @@ public function addFieldToFilter($field, $condition = null)
9283
return $this;
9384
}
9485

95-
if ($field === 'created_at') {
96-
if (is_array($condition)) {
97-
foreach ($condition as $key => $value) {
98-
$condition[$key] = $this->timeZone->convertConfigTimeToUtc($value);
99-
}
100-
}
101-
}
102-
10386
if (is_string($field) && count(explode('.', $field)) === 1) {
10487
$field = 'main_table.' . $field;
10588
}
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2016 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Sales\Model\ResourceModel\Order\Grid;
77

8-
use Magento\Framework\App\ObjectManager;
98
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
109
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
1110
use Magento\Framework\Event\ManagerInterface as EventManager;
12-
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11+
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
1413
use Magento\Sales\Model\ResourceModel\Order;
1514
use Psr\Log\LoggerInterface as Logger;
@@ -19,11 +18,6 @@
1918
*/
2019
class Collection extends SearchResult
2120
{
22-
/**
23-
* @var TimezoneInterface
24-
*/
25-
private $timeZone;
26-
2721
/**
2822
* Initialize dependencies.
2923
*
@@ -33,20 +27,17 @@ class Collection extends SearchResult
3327
* @param EventManager $eventManager
3428
* @param string $mainTable
3529
* @param string $resourceModel
36-
* @param TimezoneInterface|null $timeZone
30+
* @throws LocalizedException
3731
*/
3832
public function __construct(
3933
EntityFactory $entityFactory,
4034
Logger $logger,
4135
FetchStrategy $fetchStrategy,
4236
EventManager $eventManager,
4337
$mainTable = 'sales_order_grid',
44-
$resourceModel = Order::class,
45-
?TimezoneInterface $timeZone = null
38+
$resourceModel = Order::class
4639
) {
4740
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
48-
$this->timeZone = $timeZone ?: ObjectManager::getInstance()
49-
->get(TimezoneInterface::class);
5041
}
5142

5243
/**
@@ -63,20 +54,4 @@ protected function _initSelect()
6354

6455
return $this;
6556
}
66-
67-
/**
68-
* @inheritDoc
69-
*/
70-
public function addFieldToFilter($field, $condition = null)
71-
{
72-
if ($field === 'created_at') {
73-
if (is_array($condition)) {
74-
foreach ($condition as $key => $value) {
75-
$condition[$key] = $this->timeZone->convertConfigTimeToUtc($value);
76-
}
77-
}
78-
}
79-
80-
return parent::addFieldToFilter($field, $condition);
81-
}
8257
}

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

Lines changed: 0 additions & 84 deletions
This file was deleted.

app/code/Magento/Sales/etc/adminhtml/di.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
</arguments>
5050
<plugin name="orderGridExportFilterColumnPlugin" type="Magento\Sales\Plugin\Model\Export\OrderGridExportFilterColumn"/>
5151
</type>
52-
<type name="Magento\Sales\Model\ResourceModel\Order\Grid\Collection">
53-
<plugin name="orderGridCollectionFilterPlugin" type="Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter"/>
54-
</type>
5552
<type name="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection">
5653
<arguments>
5754
<argument name="collectionFactory" xsi:type="object">\Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory</argument>

app/code/Magento/Ui/Component/Filters/Type/Date.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Ui\Component\Filters\Type;
@@ -16,9 +16,9 @@
1616
*/
1717
class Date extends AbstractFilter
1818
{
19-
const NAME = 'filter_date';
19+
public const NAME = 'filter_date';
2020

21-
const COMPONENT = 'date';
21+
public const COMPONENT = 'date';
2222

2323
/**
2424
* Wrapped component
@@ -128,7 +128,7 @@ protected function applyFilterByType($type, $value)
128128
* @param int $hour
129129
* @param int $minute
130130
* @param int $second
131-
* @return \DateTime
131+
* @return \DateTime|null
132132
*/
133133
private function convertDatetime(string $value, int $hour = 0, int $minute = 0, int $second = 0): ?\DateTime
134134
{
@@ -137,7 +137,7 @@ private function convertDatetime(string $value, int $hour = 0, int $minute = 0,
137137
$value,
138138
!$this->getData('config/skipTimeZoneConversion')
139139
)
140-
: $this->wrappedComponent->convertDate(
140+
: $this->wrappedComponent->convertDateWithTimezone(
141141
$value,
142142
$hour,
143143
$minute,

app/code/Magento/Ui/Component/Form/Element/DataType/Date.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Ui\Component\Form\Element\DataType;
77

@@ -110,9 +110,48 @@ public function getComponentName()
110110
return static::NAME;
111111
}
112112

113+
/**
114+
* Convert given date to default (UTC) timezone, considering configured store timezone
115+
*
116+
* @param string $date
117+
* @param int $hour
118+
* @param int $minute
119+
* @param int $second
120+
* @param bool $setUtcTimeZone
121+
* @param bool $useStoreTimeZone
122+
* @return \DateTime|null
123+
*/
124+
public function convertDateWithTimezone(
125+
string $date,
126+
int $hour = 0,
127+
int $minute = 0,
128+
int $second = 0,
129+
bool $setUtcTimeZone = true,
130+
bool $useStoreTimeZone = true
131+
): ?\DateTime {
132+
if (!$useStoreTimeZone) {
133+
$this->convertDate($date, $hour, $minute, $second, $setUtcTimeZone);
134+
}
135+
136+
try {
137+
$date = $this->convertDateFormat($date);
138+
$dateObj = $this->localeDate->date($date, $this->getLocale(), $useStoreTimeZone, false);
139+
$dateObj->setTime($hour, $minute, $second);
140+
//convert store date to default date in UTC timezone without DST
141+
if ($setUtcTimeZone) {
142+
$dateObj->setTimezone(new \DateTimeZone('UTC'));
143+
}
144+
return $dateObj;
145+
} catch (Exception $e) {
146+
return null;
147+
}
148+
}
149+
113150
/**
114151
* Convert given date to default (UTC) timezone
115152
*
153+
* @deprecated
154+
* @see convertDateWithTimezone
116155
* @param int $date
117156
* @param int $hour
118157
* @param int $minute

0 commit comments

Comments
 (0)