1111 */
1212namespace Magento \Reports \Model \ResourceModel ;
1313
14+ use Magento \Framework \App \ResourceConnection ;
15+ use Magento \Store \Model \StoreManagerInterface ;
16+
1417class 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}
0 commit comments