7
7
namespace Magento \CatalogRule \Model \Indexer ;
8
8
9
9
use Magento \Catalog \Model \Product ;
10
+ use Magento \Catalog \Model \ProductFactory ;
11
+ use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
12
+ use Magento \CatalogRule \Model \Indexer \IndexBuilder \ProductLoader ;
13
+ use Magento \CatalogRule \Model \Indexer \IndexerTableSwapperInterface as TableSwapper ;
10
14
use Magento \CatalogRule \Model \ResourceModel \Rule \Collection as RuleCollection ;
11
15
use Magento \CatalogRule \Model \ResourceModel \Rule \CollectionFactory as RuleCollectionFactory ;
12
16
use Magento \CatalogRule \Model \Rule ;
17
+ use Magento \Eav \Model \Config ;
13
18
use Magento \Framework \App \ObjectManager ;
19
+ use Magento \Framework \App \ResourceConnection ;
20
+ use Magento \Framework \Exception \LocalizedException ;
14
21
use Magento \Framework \Pricing \PriceCurrencyInterface ;
15
- use Magento \CatalogRule \Model \Indexer \IndexBuilder \ProductLoader ;
16
- use Magento \CatalogRule \Model \Indexer \IndexerTableSwapperInterface as TableSwapper ;
22
+ use Magento \Framework \Stdlib \DateTime ;
23
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
24
+ use Magento \Store \Model \ScopeInterface ;
25
+ use Magento \Store \Model \StoreManagerInterface ;
26
+ use Psr \Log \LoggerInterface ;
17
27
18
28
/**
19
29
* Catalog rule index builder
@@ -46,12 +56,12 @@ class IndexBuilder
46
56
protected $ _catalogRuleGroupWebsiteColumnsList = ['rule_id ' , 'customer_group_id ' , 'website_id ' ];
47
57
48
58
/**
49
- * @var \Magento\Framework\App\ ResourceConnection
59
+ * @var ResourceConnection
50
60
*/
51
61
protected $ resource ;
52
62
53
63
/**
54
- * @var \Magento\Store\Model\ StoreManagerInterface
64
+ * @var StoreManagerInterface
55
65
*/
56
66
protected $ storeManager ;
57
67
@@ -61,7 +71,7 @@ class IndexBuilder
61
71
protected $ ruleCollectionFactory ;
62
72
63
73
/**
64
- * @var \Psr\Log\ LoggerInterface
74
+ * @var LoggerInterface
65
75
*/
66
76
protected $ logger ;
67
77
@@ -71,22 +81,22 @@ class IndexBuilder
71
81
protected $ priceCurrency ;
72
82
73
83
/**
74
- * @var \Magento\Eav\Model\ Config
84
+ * @var Config
75
85
*/
76
86
protected $ eavConfig ;
77
87
78
88
/**
79
- * @var \Magento\Framework\Stdlib\ DateTime
89
+ * @var DateTime
80
90
*/
81
91
protected $ dateFormat ;
82
92
83
93
/**
84
- * @var \Magento\Framework\Stdlib\ DateTime\DateTime
94
+ * @var DateTime\DateTime
85
95
*/
86
96
protected $ dateTime ;
87
97
88
98
/**
89
- * @var \Magento\Catalog\Model\ ProductFactory
99
+ * @var ProductFactory
90
100
*/
91
101
protected $ productFactory ;
92
102
@@ -136,7 +146,12 @@ class IndexBuilder
136
146
private $ pricesPersistor ;
137
147
138
148
/**
139
- * @var \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher
149
+ * @var TimezoneInterface|mixed
150
+ */
151
+ private $ localeDate ;
152
+
153
+ /**
154
+ * @var ActiveTableSwitcher|mixed
140
155
*/
141
156
private $ activeTableSwitcher ;
142
157
@@ -146,52 +161,55 @@ class IndexBuilder
146
161
private $ tableSwapper ;
147
162
148
163
/**
149
- * @var ProductLoader
164
+ * @var ProductLoader|mixed
150
165
*/
151
166
private $ productLoader ;
152
167
153
168
/**
154
169
* @param RuleCollectionFactory $ruleCollectionFactory
155
170
* @param PriceCurrencyInterface $priceCurrency
156
- * @param \Magento\Framework\App\ ResourceConnection $resource
157
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
158
- * @param \Psr\Log\ LoggerInterface $logger
159
- * @param \Magento\Eav\Model\ Config $eavConfig
160
- * @param \Magento\Framework\Stdlib\ DateTime $dateFormat
161
- * @param \Magento\Framework\Stdlib\ DateTime\DateTime $dateTime
162
- * @param \Magento\Catalog\Model\ ProductFactory $productFactory
171
+ * @param ResourceConnection $resource
172
+ * @param StoreManagerInterface $storeManager
173
+ * @param LoggerInterface $logger
174
+ * @param Config $eavConfig
175
+ * @param DateTime $dateFormat
176
+ * @param DateTime\DateTime $dateTime
177
+ * @param ProductFactory $productFactory
163
178
* @param int $batchCount
164
179
* @param ProductPriceCalculator|null $productPriceCalculator
165
180
* @param ReindexRuleProduct|null $reindexRuleProduct
166
181
* @param ReindexRuleGroupWebsite|null $reindexRuleGroupWebsite
167
182
* @param RuleProductsSelectBuilder|null $ruleProductsSelectBuilder
168
183
* @param ReindexRuleProductPrice|null $reindexRuleProductPrice
169
184
* @param RuleProductPricesPersistor|null $pricesPersistor
170
- * @param \Magento\Catalog\Model\ResourceModel\Indexer\ ActiveTableSwitcher|null $activeTableSwitcher
185
+ * @param ActiveTableSwitcher|null $activeTableSwitcher
171
186
* @param ProductLoader|null $productLoader
172
187
* @param TableSwapper|null $tableSwapper
188
+ * @param TimezoneInterface|null $localeDate
173
189
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
190
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
174
191
*/
175
192
public function __construct (
176
193
RuleCollectionFactory $ ruleCollectionFactory ,
177
194
PriceCurrencyInterface $ priceCurrency ,
178
- \ Magento \ Framework \ App \ ResourceConnection $ resource ,
179
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
180
- \ Psr \ Log \ LoggerInterface $ logger ,
181
- \ Magento \ Eav \ Model \ Config $ eavConfig ,
182
- \ Magento \ Framework \ Stdlib \ DateTime $ dateFormat ,
183
- \ Magento \ Framework \ Stdlib \ DateTime \DateTime $ dateTime ,
184
- \ Magento \ Catalog \ Model \ ProductFactory $ productFactory ,
195
+ ResourceConnection $ resource ,
196
+ StoreManagerInterface $ storeManager ,
197
+ LoggerInterface $ logger ,
198
+ Config $ eavConfig ,
199
+ DateTime $ dateFormat ,
200
+ DateTime \DateTime $ dateTime ,
201
+ ProductFactory $ productFactory ,
185
202
$ batchCount = 1000 ,
186
203
ProductPriceCalculator $ productPriceCalculator = null ,
187
204
ReindexRuleProduct $ reindexRuleProduct = null ,
188
205
ReindexRuleGroupWebsite $ reindexRuleGroupWebsite = null ,
189
206
RuleProductsSelectBuilder $ ruleProductsSelectBuilder = null ,
190
207
ReindexRuleProductPrice $ reindexRuleProductPrice = null ,
191
208
RuleProductPricesPersistor $ pricesPersistor = null ,
192
- \ Magento \ Catalog \ Model \ ResourceModel \ Indexer \ ActiveTableSwitcher $ activeTableSwitcher = null ,
209
+ ActiveTableSwitcher $ activeTableSwitcher = null ,
193
210
ProductLoader $ productLoader = null ,
194
- TableSwapper $ tableSwapper = null
211
+ TableSwapper $ tableSwapper = null ,
212
+ TimezoneInterface $ localeDate = null
195
213
) {
196
214
$ this ->resource = $ resource ;
197
215
$ this ->connection = $ resource ->getConnection ();
@@ -224,19 +242,22 @@ public function __construct(
224
242
RuleProductPricesPersistor::class
225
243
);
226
244
$ this ->activeTableSwitcher = $ activeTableSwitcher ?? ObjectManager::getInstance ()->get (
227
- \ Magento \ Catalog \ Model \ ResourceModel \ Indexer \ ActiveTableSwitcher::class
245
+ ActiveTableSwitcher::class
228
246
);
229
247
$ this ->productLoader = $ productLoader ?? ObjectManager::getInstance ()->get (
230
248
ProductLoader::class
231
249
);
232
250
$ this ->tableSwapper = $ tableSwapper ??
233
251
ObjectManager::getInstance ()->get (TableSwapper::class);
252
+ $ this ->localeDate = $ localeDate ??
253
+ ObjectManager::getInstance ()->get (TimezoneInterface::class);
234
254
}
235
255
236
256
/**
237
257
* Reindex by id
238
258
*
239
259
* @param int $id
260
+ * @throws LocalizedException
240
261
* @return void
241
262
* @api
242
263
*/
@@ -254,7 +275,7 @@ public function reindexById($id)
254
275
$ this ->reindexRuleGroupWebsite ->execute ();
255
276
} catch (\Exception $ e ) {
256
277
$ this ->critical ($ e );
257
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
278
+ throw new LocalizedException (
258
279
__ ('Catalog rule indexing failed. See details in exception log. ' )
259
280
);
260
281
}
@@ -264,7 +285,7 @@ public function reindexById($id)
264
285
* Reindex by ids
265
286
*
266
287
* @param array $ids
267
- * @throws \Magento\Framework\Exception\ LocalizedException
288
+ * @throws LocalizedException
268
289
* @return void
269
290
* @api
270
291
*/
@@ -274,7 +295,7 @@ public function reindexByIds(array $ids)
274
295
$ this ->doReindexByIds ($ ids );
275
296
} catch (\Exception $ e ) {
276
297
$ this ->critical ($ e );
277
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
298
+ throw new LocalizedException (
278
299
__ ("Catalog rule indexing failed. See details in exception log. " )
279
300
);
280
301
}
@@ -308,7 +329,7 @@ protected function doReindexByIds($ids)
308
329
/**
309
330
* Full reindex
310
331
*
311
- * @throws \Magento\Framework\Exception\ LocalizedException
332
+ * @throws LocalizedException
312
333
* @return void
313
334
* @api
314
335
*/
@@ -318,7 +339,7 @@ public function reindexFull()
318
339
$ this ->doReindexFull ();
319
340
} catch (\Exception $ e ) {
320
341
$ this ->critical ($ e );
321
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
342
+ throw new LocalizedException (
322
343
__ ("Catalog rule indexing failed. See details in exception log. " )
323
344
);
324
345
}
@@ -404,16 +425,22 @@ private function assignProductToRule(Rule $rule, int $productEntityId, array $we
404
425
);
405
426
406
427
$ customerGroupIds = $ rule ->getCustomerGroupIds ();
407
- $ fromTime = strtotime ($ rule ->getFromDate ());
408
- $ toTime = strtotime ($ rule ->getToDate ());
409
- $ toTime = $ toTime ? $ toTime + self ::SECONDS_IN_DAY - 1 : 0 ;
410
428
$ sortOrder = (int )$ rule ->getSortOrder ();
411
429
$ actionOperator = $ rule ->getSimpleAction ();
412
430
$ actionAmount = $ rule ->getDiscountAmount ();
413
431
$ actionStop = $ rule ->getStopRulesProcessing ();
414
432
415
433
$ rows = [];
416
434
foreach ($ websiteIds as $ websiteId ) {
435
+ $ scopeTz = new \DateTimeZone (
436
+ $ this ->localeDate ->getConfigTimezone (ScopeInterface::SCOPE_WEBSITE , $ websiteId )
437
+ );
438
+ $ fromTime = $ rule ->getFromDate ()
439
+ ? (new \DateTime ($ rule ->getFromDate (), $ scopeTz ))->getTimestamp ()
440
+ : 0 ;
441
+ $ toTime = $ rule ->getToDate ()
442
+ ? (new \DateTime ($ rule ->getToDate (), $ scopeTz ))->getTimestamp () + IndexBuilder::SECONDS_IN_DAY - 1
443
+ : 0 ;
417
444
foreach ($ customerGroupIds as $ customerGroupId ) {
418
445
$ rows [] = [
419
446
'rule_id ' => $ ruleId ,
0 commit comments