Skip to content

Commit 8b8a054

Browse files
authored
feat: add "Last 3 Months" and "Last 6 Months" to dashboard charts (#4739)
* feat: add "Last 3 Months" and "Last 6 Months" to dashboard charts * fix: fix the issue pointed out by sonarqube * fix: add missing translations * fix: fix weekly data aggregation for 3 and 6 month periods * fix: fix php cs errors * fix: fix weekly data aggregation for 3 and 6 month periods * fix: fix php cs errors
1 parent 3359e14 commit 8b8a054

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

app/code/core/Mage/Adminhtml/Block/Dashboard/Graph.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public function generateChart(): array
201201
break;
202202
case Mage_Reports_Helper_Data::PERIOD_7_DAYS:
203203
case Mage_Reports_Helper_Data::PERIOD_1_MONTH:
204+
case Mage_Reports_Helper_Data::PERIOD_3_MONTHS:
205+
case Mage_Reports_Helper_Data::PERIOD_6_MONTHS:
204206
$this->_axisLabels[$idx][$_index] = $this->formatDate(
205207
new Zend_Date($_label, 'yyyy-MM-dd'),
206208
);
@@ -243,9 +245,10 @@ private function getChartDatasAndDates(): array
243245
$date = '';
244246
$dates = [];
245247
$datas = [];
248+
$period = $this->getDataHelper()->getParam('period');
246249

247250
while ($dateStart->compare($dateEnd) < 0) {
248-
switch ($this->getDataHelper()->getParam('period')) {
251+
switch ($period) {
249252
case Mage_Reports_Helper_Data::PERIOD_24_HOURS:
250253
$date = $dateStart->toString('yyyy-MM-dd HH:00');
251254
$dateStart->addHour(1);
@@ -255,22 +258,52 @@ private function getChartDatasAndDates(): array
255258
$date = $dateStart->toString('yyyy-MM-dd');
256259
$dateStart->addDay(1);
257260
break;
261+
case Mage_Reports_Helper_Data::PERIOD_3_MONTHS:
262+
case Mage_Reports_Helper_Data::PERIOD_6_MONTHS:
263+
$date = $dateStart->toString('yyyy-MM-dd');
264+
$dateStart->addWeek(1);
265+
break;
258266
case Mage_Reports_Helper_Data::PERIOD_1_YEAR:
259267
case Mage_Reports_Helper_Data::PERIOD_2_YEARS:
260268
$date = $dateStart->toString('yyyy-MM');
261269
$dateStart->addMonth(1);
262270
break;
263271
}
272+
if (in_array($period, [
273+
Mage_Reports_Helper_Data::PERIOD_3_MONTHS,
274+
Mage_Reports_Helper_Data::PERIOD_6_MONTHS,
275+
])) {
276+
$axisTimestamps = [];
277+
foreach ($this->_axisLabels['x'] as $axisDate) {
278+
$axisTimestamps[] = (new Zend_Date($axisDate, 'yyyy-MM-dd'))->getTimestamp();
279+
}
280+
}
264281
foreach (array_keys($this->getAllSeries()) as $index) {
265-
if (in_array($date, $this->_axisLabels['x'])) {
266-
$datas[$index][] = (float) array_shift($this->_allSeries[$index]);
282+
if (isset($axisTimestamps)) {
283+
$dateObj = new Zend_Date($date, 'yyyy-MM-dd');
284+
$weekStartTs = $dateObj->getTimestamp();
285+
$weekEndTs = $dateObj->addWeek(1)->getTimestamp();
286+
287+
$found = false;
288+
foreach ($axisTimestamps as $axisTs) {
289+
if ($axisTs >= $weekStartTs && $axisTs < $weekEndTs) {
290+
$datas[$index][] = (float) array_shift($this->_allSeries[$index]);
291+
$found = true;
292+
break;
293+
}
294+
}
295+
296+
if (!$found) {
297+
$datas[$index][] = 0;
298+
}
267299
} else {
268-
$datas[$index][] = 0;
300+
$datas[$index][] = in_array($date, $this->_axisLabels['x'])
301+
? (float) array_shift($this->_allSeries[$index])
302+
: 0;
269303
}
270304
}
271305
$dates[] = $date;
272306
}
273-
274307
return [$datas, $dates];
275308
}
276309

app/code/core/Mage/Adminhtml/Helper/Dashboard/Data.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function getDatePeriods()
7777
Mage_Reports_Helper_Data::PERIOD_24_HOURS => $this->__('Last 24 Hours'),
7878
Mage_Reports_Helper_Data::PERIOD_7_DAYS => $this->__('Last 7 Days'),
7979
Mage_Reports_Helper_Data::PERIOD_1_MONTH => $this->__('Current Month'),
80+
Mage_Reports_Helper_Data::PERIOD_3_MONTHS => $this->__('Last 3 Months'),
81+
Mage_Reports_Helper_Data::PERIOD_6_MONTHS => $this->__('Last 6 Months'),
8082
Mage_Reports_Helper_Data::PERIOD_1_YEAR => $this->__('YTD'),
8183
Mage_Reports_Helper_Data::PERIOD_2_YEARS => $this->__('2YTD'),
8284
];

app/code/core/Mage/Reports/Model/Resource/Order/Collection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ protected function _getRangeExpression($range)
236236
break;
237237
case Mage_Reports_Helper_Data::PERIOD_7_DAYS:
238238
case Mage_Reports_Helper_Data::PERIOD_1_MONTH:
239+
case Mage_Reports_Helper_Data::PERIOD_3_MONTHS:
240+
case Mage_Reports_Helper_Data::PERIOD_6_MONTHS:
239241
$expression = $this->getConnection()->getDateFormatSql('{{attribute}}', '%Y-%m-%d');
240242
break;
241243
case Mage_Reports_Helper_Data::PERIOD_1_YEAR:
@@ -341,7 +343,14 @@ public function getDateRange($range, $customStart, $customEnd, $returnObjects =
341343
break;
342344

343345
case Mage_Reports_Helper_Data::PERIOD_1_MONTH:
346+
case Mage_Reports_Helper_Data::PERIOD_3_MONTHS:
347+
case Mage_Reports_Helper_Data::PERIOD_6_MONTHS:
344348
$dateStart->setDay(Mage::getStoreConfig('reports/dashboard/mtd_start'));
349+
if ($range === Mage_Reports_Helper_Data::PERIOD_3_MONTHS) {
350+
$dateStart->subMonth(2);
351+
} elseif ($range === Mage_Reports_Helper_Data::PERIOD_6_MONTHS) {
352+
$dateStart->subMonth(5);
353+
}
345354
break;
346355

347356
case Mage_Reports_Helper_Data::PERIOD_CUSTOM:

app/locale/en_US/Mage_Adminhtml.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@
527527
"Key %s does not contain scalar value","Key %s does not contain scalar value"
528528
"Key %s does not exist in array","Key %s does not exist in array"
529529
"Last 24 Hours","Last 24 Hours"
530+
"Last 3 Months","Last 3 Months"
531+
"Last 6 Months","Last 6 Months"
530532
"Last 5 Orders","Last 5 Orders"
531533
"Last 5 Search Terms","Last 5 Search Terms"
532534
"Last 7 Days","Last 7 Days"

tests/unit/Mage/Adminhtml/Helper/Dashboard/DataTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function testGetDatePeriods(): void
6363
'24h' => $this->subject->__('Last 24 Hours'),
6464
'7d' => $this->subject->__('Last 7 Days'),
6565
'1m' => $this->subject->__('Current Month'),
66+
'3m' => $this->subject->__('Last 3 Months'),
67+
'6m' => $this->subject->__('Last 6 Months'),
6668
'1y' => $this->subject->__('YTD'),
6769
'2y' => $this->subject->__('2YTD'),
6870
];

0 commit comments

Comments
 (0)