Skip to content

Commit 3f9bb54

Browse files
committed
Merge remote-tracking branch 'l3/ACP2E-2457' into Tier4-PR-Delivery-11-18-23
2 parents 19e04f7 + 34de0dc commit 3f9bb54

File tree

2 files changed

+91
-63
lines changed

2 files changed

+91
-63
lines changed

app/code/Magento/Reports/Model/ResourceModel/Helper.php

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@
1111
*/
1212
namespace Magento\Reports\Model\ResourceModel;
1313

14+
use Magento\Framework\App\ResourceConnection;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
1417
class Helper extends \Magento\Framework\DB\Helper implements \Magento\Reports\Model\ResourceModel\HelperInterface
1518
{
1619
/**
17-
* @param \Magento\Framework\App\ResourceConnection $resource
20+
* @var StoreManagerInterface
21+
*/
22+
private StoreManagerInterface $storeManager;
23+
24+
/**
25+
* @param ResourceConnection $resource
26+
* @param StoreManagerInterface $storeManager
1827
* @param string $modulePrefix
1928
*/
20-
public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix = 'reports')
21-
{
29+
public function __construct(
30+
ResourceConnection $resource,
31+
StoreManagerInterface $storeManager,
32+
string $modulePrefix = 'reports'
33+
) {
34+
$this->storeManager = $storeManager;
2235
parent::__construct($resource, $modulePrefix);
2336
}
2437

@@ -42,63 +55,67 @@ public function mergeVisitorProductIndex($mainTable, $data, $matchFields)
4255
*/
4356
public function updateReportRatingPos($connection, $type, $column, $mainTable, $aggregationTable)
4457
{
45-
$periodSubSelect = $connection->select();
46-
$ratingSubSelect = $connection->select();
47-
$ratingSelect = $connection->select();
58+
foreach ($this->storeManager->getStores(true) as $store) {
59+
$periodSubSelect = $connection->select();
60+
$ratingSubSelect = $connection->select();
61+
$ratingSelect = $connection->select();
4862

49-
switch ($type) {
50-
case 'year':
51-
$periodCol = $connection->getDateFormatSql('t.period', '%Y-01-01');
52-
break;
53-
case 'month':
54-
$periodCol = $connection->getDateFormatSql('t.period', '%Y-%m-01');
55-
break;
56-
default:
57-
$periodCol = 't.period';
58-
break;
59-
}
63+
switch ($type) {
64+
case 'year':
65+
$periodCol = $connection->getDateFormatSql('t.period', '%Y-01-01');
66+
break;
67+
case 'month':
68+
$periodCol = $connection->getDateFormatSql('t.period', '%Y-%m-01');
69+
break;
70+
default:
71+
$periodCol = 't.period';
72+
break;
73+
}
6074

61-
$columns = [
62-
'period' => 't.period',
63-
'store_id' => 't.store_id',
64-
'product_id' => 't.product_id',
65-
'product_name' => 't.product_name',
66-
'product_price' => 't.product_price',
67-
];
75+
$columns = [
76+
'period' => 't.period',
77+
'store_id' => 't.store_id',
78+
'product_id' => 't.product_id',
79+
'product_name' => 't.product_name',
80+
'product_price' => 't.product_price',
81+
];
6882

69-
if ($type == 'day') {
70-
$columns['id'] = 't.id'; // to speed-up insert on duplicate key update
71-
}
83+
if ($type == 'day') {
84+
$columns['id'] = 't.id'; // to speed-up insert on duplicate key update
85+
}
7286

73-
$cols = array_keys($columns);
74-
$cols['total_qty'] = new \Zend_Db_Expr('SUM(t.' . $column . ')');
75-
$periodSubSelect->from(
76-
['t' => $mainTable],
77-
$cols
78-
)->group(
79-
['t.store_id', $periodCol, 't.product_id']
80-
)->order(
81-
['t.store_id', $periodCol, 'total_qty DESC']
82-
);
87+
$cols = array_keys($columns);
88+
$cols['total_qty'] = new \Zend_Db_Expr('SUM(t.' . $column . ')');
89+
$periodSubSelect->from(
90+
['t' => $mainTable],
91+
$cols
92+
)->group(
93+
['t.store_id', $periodCol, 't.product_id']
94+
)->order(
95+
['t.store_id', $periodCol, 'total_qty DESC']
96+
);
8397

84-
$cols = $columns;
85-
$cols[$column] = 't.total_qty';
86-
$cols['rating_pos'] = new \Zend_Db_Expr(
87-
"(@pos := IF(t.`store_id` <> @prevStoreId OR {$periodCol} <> @prevPeriod, 1, @pos+1))"
88-
);
89-
$cols['prevStoreId'] = new \Zend_Db_Expr('(@prevStoreId := t.`store_id`)');
90-
$cols['prevPeriod'] = new \Zend_Db_Expr("(@prevPeriod := {$periodCol})");
91-
$ratingSubSelect->from($periodSubSelect, $cols);
98+
$cols = $columns;
99+
$cols[$column] = 't.total_qty';
100+
$cols['rating_pos'] = new \Zend_Db_Expr(
101+
"(@pos := IF(t.`store_id` <> @prevStoreId OR {$periodCol} <> @prevPeriod, 1, @pos+1))"
102+
);
103+
$cols['prevStoreId'] = new \Zend_Db_Expr('(@prevStoreId := t.`store_id`)');
104+
$cols['prevPeriod'] = new \Zend_Db_Expr("(@prevPeriod := {$periodCol})");
105+
$ratingSubSelect->from($periodSubSelect, $cols);
92106

93-
$cols = $columns;
94-
$cols['period'] = $periodCol;
95-
$cols[$column] = 't.' . $column;
96-
$cols['rating_pos'] = 't.rating_pos';
97-
$ratingSelect->from($ratingSubSelect, $cols);
107+
$cols = $columns;
108+
$cols['period'] = $periodCol;
109+
$cols[$column] = 't.' . $column;
110+
$cols['rating_pos'] = 't.rating_pos';
111+
112+
$ratingSubSelect->where('t.store_id = ' . $store->getId());
113+
$ratingSelect->from($ratingSubSelect, $cols);
114+
$sql = $ratingSelect->insertFromSelect($aggregationTable, array_keys($cols));
115+
$connection->query("SET @pos = 0, @prevStoreId = -1, @prevPeriod = '0000-00-00'");
116+
$connection->query($sql);
117+
}
98118

99-
$sql = $ratingSelect->insertFromSelect($aggregationTable, array_keys($cols));
100-
$connection->query("SET @pos = 0, @prevStoreId = -1, @prevPeriod = '0000-00-00'");
101-
$connection->query($sql);
102119
return $this;
103120
}
104121
}

app/code/Magento/Reports/Test/Unit/Model/ResourceModel/HelperTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
1212
use Magento\Framework\DB\Select;
1313
use Magento\Reports\Model\ResourceModel\Helper;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
1416
use PHPUnit\Framework\MockObject\MockObject;
1517
use PHPUnit\Framework\TestCase;
1618

1719
class HelperTest extends TestCase
1820
{
19-
/**
20-
* @var Helper
21-
*/
22-
protected $helper;
23-
2421
/**
2522
* @var ResourceConnection|MockObject
2623
*/
@@ -31,6 +28,11 @@ class HelperTest extends TestCase
3128
*/
3229
protected $connectionMock;
3330

31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private StoreManagerInterface $storeManager;
35+
3436
/**
3537
* {@inheritDoc}
3638
*/
@@ -48,9 +50,7 @@ protected function setUp(): void
4850
->method('getConnection')
4951
->willReturn($this->connectionMock);
5052

51-
$this->helper = new Helper(
52-
$this->resourceMock
53-
);
53+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
5454
}
5555

5656
/**
@@ -67,7 +67,11 @@ public function testMergeVisitorProductIndex()
6767
->method('insertOnDuplicate')
6868
->with($mainTable, $data, array_keys($data));
6969

70-
$this->helper->mergeVisitorProductIndex($mainTable, $data, $matchFields);
70+
$helper = new Helper(
71+
$this->resourceMock,
72+
$this->storeManager
73+
);
74+
$helper->mergeVisitorProductIndex($mainTable, $data, $matchFields);
7175
}
7276

7377
/**
@@ -82,6 +86,9 @@ public function testUpdateReportRatingPos($type, $result)
8286
$column = 'column';
8387
$aggregationTable = 'aggregationTable';
8488

89+
$store = $this->createMock(StoreInterface::class);
90+
$store->expects($this->once())->method('getId')->willReturn(1);
91+
$this->storeManager->expects($this->once())->method('getStores')->willReturn([$store]);
8592
$selectMock = $this->getMockBuilder(Select::class)
8693
->disableOriginalConstructor()
8794
->getMock();
@@ -108,7 +115,11 @@ public function testUpdateReportRatingPos($type, $result)
108115
->method('select')
109116
->willReturn($selectMock);
110117

111-
$this->helper->updateReportRatingPos($this->connectionMock, $type, $column, $mainTable, $aggregationTable);
118+
$helper = new Helper(
119+
$this->resourceMock,
120+
$this->storeManager
121+
);
122+
$helper->updateReportRatingPos($this->connectionMock, $type, $column, $mainTable, $aggregationTable);
112123
}
113124

114125
/**

0 commit comments

Comments
 (0)