6
6
7
7
namespace Magento \Catalog \Model \Product \Price \Validation ;
8
8
9
+ use Magento \Catalog \Api \Data \TierPriceInterface ;
10
+ use Magento \Catalog \Model \Product \Price \TierPricePersistence ;
11
+ use Magento \Catalog \Model \Product \Type ;
12
+ use Magento \Catalog \Model \ProductIdLocatorInterface ;
13
+ use Magento \Customer \Api \GroupRepositoryInterface ;
14
+ use Magento \Framework \Api \FilterBuilder ;
15
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
16
+ use Magento \Framework \Exception \LocalizedException ;
17
+ use Magento \Store \Api \WebsiteRepositoryInterface ;
18
+
9
19
/**
10
- * Tier Price Validator.
20
+ * Validate Tier Price and check duplication
21
+ *
22
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11
23
*/
12
24
class TierPriceValidator
13
25
{
14
26
/**
15
- * @var \Magento\Catalog\Model\ ProductIdLocatorInterface
27
+ * @var ProductIdLocatorInterface
16
28
*/
17
29
private $ productIdLocator ;
18
30
19
31
/**
20
- * @var \Magento\Framework\Api\ SearchCriteriaBuilder
32
+ * @var SearchCriteriaBuilder
21
33
*/
22
34
private $ searchCriteriaBuilder ;
23
35
24
36
/**
25
- * @var \Magento\Framework\Api\ FilterBuilder
37
+ * @var FilterBuilder
26
38
*/
27
39
private $ filterBuilder ;
28
40
29
41
/**
30
- * @var \Magento\Customer\Api\ GroupRepositoryInterface
42
+ * @var GroupRepositoryInterface
31
43
*/
32
44
private $ customerGroupRepository ;
33
45
34
46
/**
35
- * @var \Magento\Store\Api\ WebsiteRepositoryInterface
47
+ * @var WebsiteRepositoryInterface
36
48
*/
37
49
private $ websiteRepository ;
38
50
39
51
/**
40
- * @var \Magento\Catalog\Model\Product\Price\Validation\ Result
52
+ * @var Result
41
53
*/
42
54
private $ validationResult ;
43
55
44
56
/**
45
- * @var \Magento\Catalog\Model\Product\Price\ TierPricePersistence
57
+ * @var TierPricePersistence
46
58
*/
47
59
private $ tierPricePersistence ;
48
60
@@ -54,54 +66,48 @@ class TierPriceValidator
54
66
private $ customerGroupsByCode = [];
55
67
56
68
/**
57
- * @var \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor
69
+ * @var InvalidSkuProcessor
58
70
*/
59
71
private $ invalidSkuProcessor ;
60
72
61
73
/**
62
- * All groups value.
63
- *
64
74
* @var string
65
75
*/
66
76
private $ allGroupsValue = 'all groups ' ;
67
77
68
78
/**
69
- * All websites value.
70
- *
71
79
* @var string
72
80
*/
73
81
private $ allWebsitesValue = "0 " ;
74
82
75
83
/**
76
- * Allowed product types.
77
- *
78
84
* @var array
79
85
*/
80
86
private $ allowedProductTypes = [];
81
87
82
88
/**
83
89
* TierPriceValidator constructor.
84
90
*
85
- * @param \Magento\Catalog\Model\ ProductIdLocatorInterface $productIdLocator
86
- * @param \Magento\Framework\Api\ SearchCriteriaBuilder $searchCriteriaBuilder
87
- * @param \Magento\Framework\Api\ FilterBuilder $filterBuilder
88
- * @param \Magento\Customer\Api\ GroupRepositoryInterface $customerGroupRepository
89
- * @param \Magento\Store\Api\ WebsiteRepositoryInterface $websiteRepository
90
- * @param \Magento\Catalog\Model\Product\Price\ TierPricePersistence $tierPricePersistence
91
- * @param \Magento\Catalog\Model\Product\Price\Validation\ Result $validationResult
92
- * @param \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor $invalidSkuProcessor
91
+ * @param ProductIdLocatorInterface $productIdLocator
92
+ * @param SearchCriteriaBuilder $searchCriteriaBuilder
93
+ * @param FilterBuilder $filterBuilder
94
+ * @param GroupRepositoryInterface $customerGroupRepository
95
+ * @param WebsiteRepositoryInterface $websiteRepository
96
+ * @param TierPricePersistence $tierPricePersistence
97
+ * @param Result $validationResult
98
+ * @param InvalidSkuProcessor $invalidSkuProcessor
93
99
* @param array $allowedProductTypes [optional]
94
100
*/
95
101
public function __construct (
96
- \ Magento \ Catalog \ Model \ ProductIdLocatorInterface $ productIdLocator ,
97
- \ Magento \ Framework \ Api \ SearchCriteriaBuilder $ searchCriteriaBuilder ,
98
- \ Magento \ Framework \ Api \ FilterBuilder $ filterBuilder ,
99
- \ Magento \ Customer \ Api \ GroupRepositoryInterface $ customerGroupRepository ,
100
- \ Magento \ Store \ Api \ WebsiteRepositoryInterface $ websiteRepository ,
101
- \ Magento \ Catalog \ Model \ Product \ Price \ TierPricePersistence $ tierPricePersistence ,
102
- \ Magento \ Catalog \ Model \ Product \ Price \ Validation \ Result $ validationResult ,
103
- \ Magento \ Catalog \ Model \ Product \ Price \ Validation \ InvalidSkuProcessor $ invalidSkuProcessor ,
104
- array $ allowedProductTypes = []
102
+ ProductIdLocatorInterface $ productIdLocator ,
103
+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
104
+ FilterBuilder $ filterBuilder ,
105
+ GroupRepositoryInterface $ customerGroupRepository ,
106
+ WebsiteRepositoryInterface $ websiteRepository ,
107
+ TierPricePersistence $ tierPricePersistence ,
108
+ Result $ validationResult ,
109
+ InvalidSkuProcessor $ invalidSkuProcessor ,
110
+ array $ allowedProductTypes = []
105
111
) {
106
112
$ this ->productIdLocator = $ productIdLocator ;
107
113
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -130,7 +136,7 @@ public function validateSkus(array $skus)
130
136
*
131
137
* @param array $prices
132
138
* @param array $existingPrices
133
- * @return \Magento\Catalog\Model\Product\Price\Validation\ Result $validationResult
139
+ * @return Result $validationResult
134
140
*/
135
141
public function retrieveValidationResult (array $ prices , array $ existingPrices = [])
136
142
{
@@ -159,7 +165,7 @@ public function retrieveValidationResult(array $prices, array $existingPrices =
159
165
if (isset ($ pricesBySku [$ price ->getSku ()])) {
160
166
$ this ->checkUnique ($ price , $ pricesBySku , $ key , $ validationResult );
161
167
}
162
- $ this ->checkUnique ($ price , $ existingPrices , $ key , $ validationResult );
168
+ $ this ->checkUnique ($ price , $ existingPrices , $ key , $ validationResult, true );
163
169
$ this ->checkGroup ($ price , $ key , $ validationResult );
164
170
}
165
171
@@ -169,14 +175,14 @@ public function retrieveValidationResult(array $prices, array $existingPrices =
169
175
/**
170
176
* Check that sku value is correct.
171
177
*
172
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
178
+ * @param TierPriceInterface $price
173
179
* @param int $key
174
180
* @param array $invalidSkus
175
181
* @param Result $validationResult
176
182
* @return void
177
183
*/
178
184
private function checkSku (
179
- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price ,
185
+ TierPriceInterface $ price ,
180
186
$ key ,
181
187
array $ invalidSkus ,
182
188
Result $ validationResult
@@ -207,16 +213,16 @@ private function checkSku(
207
213
/**
208
214
* Verify that price value is correct.
209
215
*
210
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
216
+ * @param TierPriceInterface $price
211
217
* @param int $key
212
218
* @param Result $validationResult
213
219
* @return void
214
220
*/
215
- private function checkPrice (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
221
+ private function checkPrice (TierPriceInterface $ price , $ key , Result $ validationResult )
216
222
{
217
223
if (null === $ price ->getPrice ()
218
224
|| $ price ->getPrice () < 0
219
- || ($ price ->getPriceType () === \ Magento \ Catalog \ Api \ Data \ TierPriceInterface::PRICE_TYPE_DISCOUNT
225
+ || ($ price ->getPriceType () === TierPriceInterface::PRICE_TYPE_DISCOUNT
220
226
&& $ price ->getPrice () > 100
221
227
)
222
228
) {
@@ -247,27 +253,27 @@ private function checkPrice(\Magento\Catalog\Api\Data\TierPriceInterface $price,
247
253
/**
248
254
* Verify that price type is correct.
249
255
*
250
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
256
+ * @param TierPriceInterface $price
251
257
* @param array $ids
252
258
* @param int $key
253
259
* @param Result $validationResult
254
260
* @return void
255
261
*/
256
262
private function checkPriceType (
257
- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price ,
263
+ TierPriceInterface $ price ,
258
264
array $ ids ,
259
265
$ key ,
260
266
Result $ validationResult
261
267
) {
262
268
if (!in_array (
263
269
$ price ->getPriceType (),
264
270
[
265
- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface::PRICE_TYPE_FIXED ,
266
- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface::PRICE_TYPE_DISCOUNT
271
+ TierPriceInterface::PRICE_TYPE_FIXED ,
272
+ TierPriceInterface::PRICE_TYPE_DISCOUNT
267
273
]
268
274
)
269
- || (array_search (\ Magento \ Catalog \ Model \ Product \ Type::TYPE_BUNDLE , $ ids )
270
- && $ price ->getPriceType () !== \ Magento \ Catalog \ Api \ Data \ TierPriceInterface::PRICE_TYPE_DISCOUNT )
275
+ || (array_search (Type::TYPE_BUNDLE , $ ids ) !== false
276
+ && $ price ->getPriceType () !== TierPriceInterface::PRICE_TYPE_DISCOUNT )
271
277
) {
272
278
$ validationResult ->addFailedItem (
273
279
$ key ,
@@ -296,12 +302,12 @@ private function checkPriceType(
296
302
/**
297
303
* Verify that product quantity is correct.
298
304
*
299
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
305
+ * @param TierPriceInterface $price
300
306
* @param int $key
301
307
* @param Result $validationResult
302
308
* @return void
303
309
*/
304
- private function checkQuantity (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
310
+ private function checkQuantity (TierPriceInterface $ price , $ key , Result $ validationResult )
305
311
{
306
312
if ($ price ->getQuantity () < 1 ) {
307
313
$ validationResult ->addFailedItem (
@@ -329,12 +335,12 @@ private function checkQuantity(\Magento\Catalog\Api\Data\TierPriceInterface $pri
329
335
/**
330
336
* Verify that website exists.
331
337
*
332
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
338
+ * @param TierPriceInterface $price
333
339
* @param int $key
334
340
* @param Result $validationResult
335
341
* @return void
336
342
*/
337
- private function checkWebsite (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
343
+ private function checkWebsite (TierPriceInterface $ price , $ key , Result $ validationResult )
338
344
{
339
345
try {
340
346
$ this ->websiteRepository ->getById ($ price ->getWebsiteId ());
@@ -364,49 +370,51 @@ private function checkWebsite(\Magento\Catalog\Api\Data\TierPriceInterface $pric
364
370
/**
365
371
* Check website value is unique.
366
372
*
367
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $tierPrice
373
+ * @param TierPriceInterface $tierPrice
368
374
* @param array $prices
369
375
* @param int $key
370
376
* @param Result $validationResult
377
+ * @param bool $isExistingPrice
371
378
* @return void
372
379
*/
373
380
private function checkUnique (
374
- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ tierPrice ,
381
+ TierPriceInterface $ tierPrice ,
375
382
array $ prices ,
376
383
$ key ,
377
- Result $ validationResult
384
+ Result $ validationResult ,
385
+ bool $ isExistingPrice = false
378
386
) {
379
387
if (isset ($ prices [$ tierPrice ->getSku ()])) {
380
388
foreach ($ prices [$ tierPrice ->getSku ()] as $ price ) {
381
- if (strtolower ($ price ->getCustomerGroup ()) === strtolower ($ tierPrice ->getCustomerGroup ())
382
- && $ price ->getQuantity () == $ tierPrice ->getQuantity ()
383
- && (
384
- ($ price ->getWebsiteId () == $ this ->allWebsitesValue
385
- || $ tierPrice ->getWebsiteId () == $ this ->allWebsitesValue )
386
- && $ price ->getWebsiteId () != $ tierPrice ->getWebsiteId ()
387
- )
388
- ) {
389
- $ validationResult ->addFailedItem (
390
- $ key ,
391
- __ (
392
- 'We found a duplicate website, tier price, customer group and quantity: '
393
- . 'Customer Group = %customerGroup, Website ID = %websiteId, Quantity = %qty. '
394
- . 'Row ID: SKU = %SKU, Website ID: %websiteId, '
395
- . 'Customer Group: %customerGroup, Quantity: %qty. ' ,
389
+ if ($ price !== $ tierPrice ) {
390
+ $ checkWebsiteValue = $ isExistingPrice ? $ this ->compareWebsiteValue ($ price , $ tierPrice )
391
+ : ($ price ->getWebsiteId () == $ tierPrice ->getWebsiteId ());
392
+ if (strtolower ($ price ->getCustomerGroup ()) === strtolower ($ tierPrice ->getCustomerGroup ())
393
+ && $ price ->getQuantity () == $ tierPrice ->getQuantity ()
394
+ && $ checkWebsiteValue
395
+ ) {
396
+ $ validationResult ->addFailedItem (
397
+ $ key ,
398
+ __ (
399
+ 'We found a duplicate website, tier price, customer group and quantity: '
400
+ . 'Customer Group = %customerGroup, Website ID = %websiteId, Quantity = %qty. '
401
+ . 'Row ID: SKU = %SKU, Website ID: %websiteId, '
402
+ . 'Customer Group: %customerGroup, Quantity: %qty. ' ,
403
+ [
404
+ 'SKU ' => '%SKU ' ,
405
+ 'websiteId ' => '%websiteId ' ,
406
+ 'customerGroup ' => '%customerGroup ' ,
407
+ 'qty ' => '%qty '
408
+ ]
409
+ ),
396
410
[
397
- 'SKU ' => ' %SKU ' ,
398
- 'websiteId ' => ' %websiteId ' ,
399
- 'customerGroup ' => ' %customerGroup ' ,
400
- 'qty ' => ' %qty '
411
+ 'SKU ' => $ price -> getSku () ,
412
+ 'websiteId ' => $ price -> getWebsiteId () ,
413
+ 'customerGroup ' => $ price -> getCustomerGroup () ,
414
+ 'qty ' => $ price -> getQuantity ()
401
415
]
402
- ),
403
- [
404
- 'SKU ' => $ price ->getSku (),
405
- 'websiteId ' => $ price ->getWebsiteId (),
406
- 'customerGroup ' => $ price ->getCustomerGroup (),
407
- 'qty ' => $ price ->getQuantity ()
408
- ]
409
- );
416
+ );
417
+ }
410
418
}
411
419
}
412
420
}
@@ -415,12 +423,13 @@ private function checkUnique(
415
423
/**
416
424
* Check customer group exists and has correct value.
417
425
*
418
- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
426
+ * @param TierPriceInterface $price
419
427
* @param int $key
420
428
* @param Result $validationResult
421
429
* @return void
430
+ * @throws LocalizedException
422
431
*/
423
- private function checkGroup (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
432
+ private function checkGroup (TierPriceInterface $ price , $ key , Result $ validationResult )
424
433
{
425
434
$ customerGroup = strtolower ($ price ->getCustomerGroup ());
426
435
@@ -452,8 +461,9 @@ private function checkGroup(\Magento\Catalog\Api\Data\TierPriceInterface $price,
452
461
*
453
462
* @param string $code
454
463
* @return int|bool
464
+ * @throws LocalizedException
455
465
*/
456
- private function retrieveGroupValue ($ code )
466
+ private function retrieveGroupValue (string $ code )
457
467
{
458
468
if (!isset ($ this ->customerGroupsByCode [$ code ])) {
459
469
$ searchCriteria = $ this ->searchCriteriaBuilder ->addFilters (
@@ -473,4 +483,20 @@ private function retrieveGroupValue($code)
473
483
474
484
return $ this ->customerGroupsByCode [$ code ];
475
485
}
486
+
487
+ /**
488
+ * Compare Website Values between price and tier price
489
+ *
490
+ * @param TierPriceInterface $price
491
+ * @param TierPriceInterface $tierPrice
492
+ * @return bool
493
+ */
494
+ private function compareWebsiteValue (TierPriceInterface $ price , TierPriceInterface $ tierPrice ): bool
495
+ {
496
+ return (
497
+ $ price ->getWebsiteId () == $ this ->allWebsitesValue
498
+ || $ tierPrice ->getWebsiteId () == $ this ->allWebsitesValue
499
+ )
500
+ && $ price ->getWebsiteId () != $ tierPrice ->getWebsiteId ();
501
+ }
476
502
}
0 commit comments