Skip to content

Commit 5d57110

Browse files
committed
MC-25260: A wrong behaviour of a chart order
1 parent 06a0dd0 commit 5d57110

File tree

5 files changed

+220
-25
lines changed

5 files changed

+220
-25
lines changed

app/code/Magento/Backend/Block/Dashboard/Graph.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
*/
1515
class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
1616
{
17-
/**
18-
* Api URL
19-
*/
2017
const API_URL = 'https://image-charts.com/chart';
2118

2219
/**
@@ -190,8 +187,8 @@ public function getChartUrl($directUrl = true)
190187
$params = [
191188
'cht' => 'lc',
192189
'chls' => '7',
193-
'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
194-
'chm' => 'B,f4d4b2,0,0,0',
190+
'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
191+
'chm' => 'B,f4d4b2,0,0,0',
195192
'chco' => 'db4814',
196193
'chxs' => '0,0,11|1,0,11',
197194
'chma' => '15,15,15,15'
@@ -300,20 +297,23 @@ public function getChartUrl($directUrl = true)
300297
$minvalue = min($localminvalue);
301298

302299
// default values
303-
$yLabels = [];
304300
$miny = 0;
305301
$maxy = 0;
306302
$yorigin = 0;
303+
$xAxis = 'x';
304+
$xAxisIndex = 0;
305+
$yAxisIndex = 1;
307306

308307
if ($minvalue >= 0 && $maxvalue >= 0) {
309308
if ($maxvalue > 10) {
310-
$p = pow(10, $this->_getPow((int) $maxvalue));
309+
$p = pow(10, $this->_getPow((int)$maxvalue));
311310
$maxy = ceil($maxvalue / $p) * $p;
312-
$yLabels = range($miny, $maxy, $p);
311+
$yRange = "$yAxisIndex,$miny,$maxy,$p";
313312
} else {
314313
$maxy = ceil($maxvalue + 1);
315-
$yLabels = range($miny, $maxy, 1);
314+
$yRange = "$yAxisIndex,$miny,$maxy,1";
316315
}
316+
$params['chxr'] = $yRange;
317317
$yorigin = 0;
318318
}
319319

@@ -341,22 +341,11 @@ public function getChartUrl($directUrl = true)
341341

342342
$params['chd'] .= $buffer;
343343

344-
$valueBuffer = [];
345-
346344
if (count($this->_axisLabels) > 0) {
347345
$params['chxt'] = implode(',', array_keys($this->_axisLabels));
348-
$indexid = 0;
349-
foreach ($this->_axisLabels as $idx => $labels) {
350-
if ($idx == 'x') {
351-
$this->formatAxisLabelDate((string) $idx, (string) $timezoneLocal);
352-
$tmpstring = implode('|', $this->_axisLabels[$idx]);
353-
$valueBuffer[] = $indexid . ":|" . $tmpstring;
354-
} elseif ($idx == 'y') {
355-
$valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
356-
}
357-
$indexid++;
358-
}
359-
$params['chxl'] = implode('|', $valueBuffer);
346+
$this->formatAxisLabelDate($xAxis, (string)$timezoneLocal);
347+
$customAxisLabels = $xAxisIndex . ":|" . implode('|', $this->_axisLabels[$xAxis]);
348+
$params['chxl'] = $customAxisLabels . $dataSetdelimiter;
360349
}
361350

362351
// chart size
@@ -368,7 +357,7 @@ public function getChartUrl($directUrl = true)
368357
foreach ($params as $name => $value) {
369358
$p[] = $name . '=' . urlencode($value);
370359
}
371-
return (string) self::API_URL . '?' . implode('&', $p);
360+
return (string)self::API_URL . '?' . implode('&', $p);
372361
}
373362
$gaData = urlencode(base64_encode(json_encode($params)));
374363
$gaHash = $this->_dashboardData->getChartDataHash($gaData);
@@ -392,7 +381,7 @@ private function formatAxisLabelDate($idx, $timezoneLocal)
392381
switch ($this->getDataHelper()->getParam('period')) {
393382
case '24h':
394383
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
395-
$period->setTime((int) $period->format('H'), 0, 0),
384+
$period->setTime((int)$period->format('H'), 0, 0),
396385
\IntlDateFormatter::NONE,
397386
\IntlDateFormatter::SHORT
398387
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ public function getDateRange($range, $customStart, $customEnd, $returnObjects =
461461
$startMonth = isset($startMonthDay[0]) ? (int)$startMonthDay[0] : 1;
462462
$startDay = isset($startMonthDay[1]) ? (int)$startMonthDay[1] : 1;
463463
$dateStart->setDate($dateStart->format('Y'), $startMonth, $startDay);
464+
$dateStart->modify('-1 year');
464465
if ($range == '2y') {
465466
$dateStart->modify('-1 year');
466467
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Backend\Block\Dashboard\Tab;
9+
10+
use Magento\Backend\Block\Dashboard\Graph;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test for \Magento\Backend\Block\Dashboard\Tab\Orders class.
18+
*
19+
* @magentoAppArea adminhtml
20+
*/
21+
class OrdersTest extends TestCase
22+
{
23+
/**
24+
* @var ObjectManagerInterface
25+
*/
26+
private $objectManager;
27+
28+
/**
29+
* @var LayoutInterface
30+
*/
31+
private $layout;
32+
33+
/**
34+
* @var Graph
35+
*/
36+
private $graphBlock;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->layout = $this->objectManager->get(LayoutInterface::class);
45+
$this->graphBlock = $this->layout->createBlock(Graph::class);
46+
}
47+
48+
/**
49+
* @magentoDbIsolation disabled
50+
* @magentoDataFixture Magento/Sales/_files/order_list_with_invoice.php
51+
* @dataProvider chartUrlDataProvider
52+
* @param string $period
53+
* @param string $expectedAxisRange
54+
* @return void
55+
*/
56+
public function testGetChartUrl(string $period, string $expectedAxisRange): void
57+
{
58+
$this->graphBlock->getRequest()->setParams(['period' => $period]);
59+
$ordersBlock = $this->layout->createBlock(Orders::class);
60+
$decodedChartUrl = urldecode($ordersBlock->getChartUrl());
61+
$chartUrlSegments = explode('&', $decodedChartUrl);
62+
$this->assertEquals($expectedAxisRange, $this->getUrlParamData($chartUrlSegments, 'chxr'));
63+
}
64+
65+
/**
66+
* @return array
67+
*/
68+
public function chartUrlDataProvider(): array
69+
{
70+
return [
71+
'Last 24 Hours' => ['24h', '1,0,2,1'],
72+
'Last 7 Days' => ['7d', '1,0,3,1'],
73+
'Current Month' => ['1m', '1,0,3,1'],
74+
'YTD' => ['1y', '1,0,4,1'],
75+
'2YTD' => ['2y', '1,0,4,1'],
76+
];
77+
}
78+
79+
/**
80+
* @param array $chartUrlSegments
81+
* @param string $paramName
82+
* @return string
83+
*/
84+
private function getUrlParamData(array $chartUrlSegments, string $paramName): string
85+
{
86+
$urlParams = [];
87+
foreach ($chartUrlSegments as $chartUrlSegment) {
88+
list($paramKey, $paramValue) = explode('=', $chartUrlSegment);
89+
$urlParams[$paramKey] = $paramValue;
90+
}
91+
92+
return $urlParams[$paramName] ?? '';
93+
}
94+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
use Magento\Framework\DB\Transaction;
9+
use Magento\Framework\Stdlib\DateTime;
10+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
11+
use Magento\Sales\Api\InvoiceManagementInterface;
12+
use Magento\Sales\Api\OrderRepositoryInterface;
13+
use Magento\Sales\Model\Order;
14+
use Magento\Sales\Model\Order\Invoice;
15+
use Magento\Sales\Model\OrderFactory;
16+
17+
require __DIR__ . '/order.php';
18+
19+
/** @var OrderFactory $orderFactory */
20+
$orderFactory = $objectManager->get(OrderInterfaceFactory::class);
21+
/** @var OrderRepositoryInterface $orderRepository */
22+
$orderRepository = $objectManager->get(OrderRepositoryInterface::class);
23+
/** @var InvoiceManagementInterface $invoiceManagement */
24+
$invoiceManagement = $objectManager->get(InvoiceManagementInterface::class);
25+
/** @var Transaction $transaction */
26+
$transaction = $objectManager->get(Transaction::class);
27+
28+
$dateTime = new \DateTimeImmutable();
29+
$ordersData = [
30+
[
31+
'increment_id' => '100000002',
32+
'state' => Order::STATE_PROCESSING,
33+
'status' => 'processing',
34+
'base_to_global_rate' => 1,
35+
'base_grand_total' => 120.00,
36+
'grand_total' => 120.00,
37+
'subtotal' => 120.00,
38+
'created_at' => $dateTime->modify('-1 hour')->format(DateTime::DATETIME_PHP_FORMAT),
39+
],
40+
[
41+
'increment_id' => '100000003',
42+
'state' => Order::STATE_PROCESSING,
43+
'status' => 'processing',
44+
'base_to_global_rate' => 1,
45+
'base_grand_total' => 130.00,
46+
'grand_total' => 130.00,
47+
'subtotal' => 130.00,
48+
'created_at' => $dateTime->modify('-1 day')->format(DateTime::DATETIME_PHP_FORMAT),
49+
],
50+
[
51+
'increment_id' => '100000004',
52+
'state' => Order::STATE_PROCESSING,
53+
'status' => 'processing',
54+
'base_to_global_rate' => 1,
55+
'base_grand_total' => 140.00,
56+
'grand_total' => 140.00,
57+
'subtotal' => 140.00,
58+
'created_at' => $dateTime->modify('-1 month')->format(DateTime::DATETIME_PHP_FORMAT),
59+
],
60+
[
61+
'increment_id' => '100000005',
62+
'state' => Order::STATE_PROCESSING,
63+
'status' => 'processing',
64+
'base_to_global_rate' => 1,
65+
'base_grand_total' => 150.00,
66+
'grand_total' => 150.00,
67+
'subtotal' => 150.00,
68+
'created_at' => $dateTime->modify('-1 year')->format(DateTime::DATETIME_PHP_FORMAT),
69+
],
70+
[
71+
'increment_id' => '100000006',
72+
'state' => Order::STATE_PROCESSING,
73+
'status' => 'processing',
74+
'base_to_global_rate' => 1,
75+
'base_grand_total' => 160.00,
76+
'grand_total' => 160.00,
77+
'subtotal' => 160.00,
78+
'created_at' => $dateTime->modify('-2 year')->format(DateTime::DATETIME_PHP_FORMAT),
79+
],
80+
];
81+
82+
foreach ($ordersData as $orderData) {
83+
/** @var Order $order */
84+
$order = $orderFactory->create();
85+
$order
86+
->setData($orderData)
87+
->addItem($orderItem)
88+
->setCustomerIsGuest(true)
89+
->setCustomerEmail('customer@null.com')
90+
->setBillingAddress($billingAddress)
91+
->setShippingAddress($shippingAddress)
92+
->setPayment($payment);
93+
$orderRepository->save($order);
94+
95+
/** @var Invoice $invoice */
96+
$invoice = $invoiceManagement->prepareInvoice($order);
97+
$invoice->register();
98+
$order->setIsInProcess(true);
99+
$transaction
100+
->addObject($order)
101+
->addObject($invoice)
102+
->save();
103+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
require __DIR__ . '/default_rollback.php';

0 commit comments

Comments
 (0)