Skip to content

Commit 933de94

Browse files
committed
ACP2E-1068: Incorrect Dashboard YTD values
1 parent 4b1887b commit 933de94

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

app/code/Magento/Backend/Model/Dashboard/Chart/Date.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,39 @@ public function getByPeriod(string $period): array
5555
true
5656
);
5757

58+
$timezoneLocal = $this->localeDate->getConfigTimezone();
59+
$localStartDate = new \DateTime($dateStart->format('Y-m-d H:i:s'), new \DateTimeZone($timezoneLocal));
60+
$localEndDate = new \DateTime($dateEnd->format('Y-m-d H:i:s'), new \DateTimeZone($timezoneLocal));
61+
5862
if ($period === Period::PERIOD_24_HOURS) {
59-
$dateEnd->modify('-1 hour');
63+
$localEndDate = new \DateTime('now', new \DateTimeZone($timezoneLocal));
64+
$localStartDate = clone $localEndDate;
65+
$localStartDate->modify('-1 day');
66+
$localStartDate->modify('+1 hour');
6067
} elseif ($period === Period::PERIOD_TODAY) {
61-
$dateEnd->modify('now');
68+
$localEndDate->modify('now');
6269
} else {
63-
$dateEnd->setTime(23, 59, 59);
64-
$dateStart->setTime(0, 0, 0);
70+
$localEndDate->setTime(23, 59, 59);
71+
$localStartDate->setTime(0, 0, 0);
6572
}
6673

6774
$dates = [];
6875

69-
while ($dateStart <= $dateEnd) {
76+
while ($localStartDate <= $localEndDate) {
7077
switch ($period) {
7178
case Period::PERIOD_7_DAYS:
7279
case Period::PERIOD_1_MONTH:
73-
$d = $dateStart->format('Y-m-d');
74-
$dateStart->modify('+1 day');
80+
$d = $localStartDate->format('Y-m-d');
81+
$localStartDate->modify('+1 day');
7582
break;
7683
case Period::PERIOD_1_YEAR:
7784
case Period::PERIOD_2_YEARS:
78-
$d = $dateStart->format('Y-m');
79-
$dateStart->modify('first day of next month');
85+
$d = $localStartDate->format('Y-m');
86+
$localStartDate->modify('first day of next month');
8087
break;
8188
default:
82-
$d = $dateStart->format('Y-m-d H:00');
83-
$dateStart->modify('+1 hour');
89+
$d = $localStartDate->format('Y-m-d H:00');
90+
$localStartDate->modify('+1 hour');
8491
}
8592

8693
$dates[] = $d;

app/code/Magento/Reports/Model/ResourceModel/Order/Collection.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,8 @@ protected function _getTZRangeExpressionForAttribute($range, $attribute, $tzFrom
410410
*/
411411
public function getDateRange($range, $customStart, $customEnd, $returnObjects = false)
412412
{
413-
$timezoneLocal = $this->_localeDate->getConfigTimezone();
414-
415-
$dateEnd = new \DateTime('now', new \DateTimeZone($timezoneLocal));
416-
$dateStart = new \DateTime('now', new \DateTimeZone($timezoneLocal));
413+
$dateEnd = new \DateTime();
414+
$dateStart = new \DateTime();
417415

418416
// go to the end of a day
419417
$dateEnd->setTime(23, 59, 59);
@@ -425,8 +423,8 @@ public function getDateRange($range, $customStart, $customEnd, $returnObjects =
425423
$dateEnd->modify('now');
426424
break;
427425
case '24h':
428-
$dateEnd = new \DateTime('now', new \DateTimeZone($timezoneLocal));
429-
$dateEnd->modify('+1 hour');
426+
$dateEnd = new \DateTime();
427+
$dateStart = new \DateTime();$dateEnd->modify('+1 hour');
430428
$dateStart = clone $dateEnd;
431429
$dateStart->modify('-1 day');
432430
break;
@@ -701,8 +699,8 @@ public function setStoreIds($storeIds)
701699
'invoiced' => 'SUM(main_table.base_total_paid)',
702700
'refunded' => 'SUM(main_table.base_total_refunded)',
703701
'profit' => "SUM({$baseSubtotalInvoiced}) " .
704-
"+ SUM({$baseDiscountRefunded}) - SUM({$baseSubtotalRefunded}) " .
705-
"- SUM({$baseDiscountInvoiced}) - SUM({$baseTotalInvocedCost})",
702+
"+ SUM({$baseDiscountRefunded}) - SUM({$baseSubtotalRefunded}) " .
703+
"- SUM({$baseDiscountInvoiced}) - SUM({$baseTotalInvocedCost})",
706704
]
707705
);
708706
} else {
@@ -716,10 +714,10 @@ public function setStoreIds($storeIds)
716714
'invoiced' => 'SUM(main_table.base_total_paid * main_table.base_to_global_rate)',
717715
'refunded' => 'SUM(main_table.base_total_refunded * main_table.base_to_global_rate)',
718716
'profit' => "SUM({$baseSubtotalInvoiced} * main_table.base_to_global_rate) " .
719-
"+ SUM({$baseDiscountRefunded} * main_table.base_to_global_rate) " .
720-
"- SUM({$baseSubtotalRefunded} * main_table.base_to_global_rate) " .
721-
"- SUM({$baseDiscountInvoiced} * main_table.base_to_global_rate) " .
722-
"- SUM({$baseTotalInvocedCost} * main_table.base_to_global_rate)",
717+
"+ SUM({$baseDiscountRefunded} * main_table.base_to_global_rate) " .
718+
"- SUM({$baseSubtotalRefunded} * main_table.base_to_global_rate) " .
719+
"- SUM({$baseDiscountInvoiced} * main_table.base_to_global_rate) " .
720+
"- SUM({$baseTotalInvocedCost} * main_table.base_to_global_rate)",
723721
]
724722
);
725723
}
@@ -857,7 +855,7 @@ private function getTotalsExpressionWithDiscountRefunded(
857855
$template = ($storeId != 0)
858856
? '(main_table.base_subtotal - %2$s - %1$s - (ABS(main_table.base_discount_amount) - %3$s - %4$s))'
859857
: '((main_table.base_subtotal - %1$s - %2$s - (ABS(main_table.base_discount_amount) - %3$s - %4$s)) '
860-
. ' * main_table.base_to_global_rate)';
858+
. ' * main_table.base_to_global_rate)';
861859
return sprintf(
862860
$template,
863861
$baseSubtotalRefunded,

app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Order/CollectionTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ protected function setUp(): void
138138
->getMock();
139139
$this->timezoneMock = $this->getMockBuilder(TimezoneInterface::class)
140140
->getMock();
141-
$this->timezoneMock
142-
->expects($this->any())
143-
->method('getConfigTimezone')
144-
->willReturn('America/Chicago');
145-
146141
$this->configMock = $this->getMockBuilder(Config::class)
147142
->disableOriginalConstructor()
148143
->getMock();
@@ -286,8 +281,11 @@ public function testPrepareSummary($useAggregatedData, $mainTable, $isFilter, $g
286281
*/
287282
public function testGetDateRangeFirstPart($range, $customStart, $customEnd, $expectedInterval): void
288283
{
284+
$timeZoneToReturn = date_default_timezone_get();
285+
date_default_timezone_set('UTC');
289286
$result = $this->collection->getDateRange($range, $customStart, $customEnd);
290287
$interval = $result['to']->diff($result['from']);
288+
date_default_timezone_set($timeZoneToReturn);
291289
$intervalResult = $interval->format('%y %m %d %h:%i:%s');
292290
$this->assertEquals($expectedInterval, $intervalResult);
293291
}

0 commit comments

Comments
 (0)