66
77namespace Magento \Catalog \Model \Product \Price \Validation ;
88
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+
919/**
10- * Tier Price Validator.
20+ * Validate Tier Price and check duplication
21+ *
22+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1123 */
1224class TierPriceValidator
1325{
1426 /**
15- * @var \Magento\Catalog\Model\ ProductIdLocatorInterface
27+ * @var ProductIdLocatorInterface
1628 */
1729 private $ productIdLocator ;
1830
1931 /**
20- * @var \Magento\Framework\Api\ SearchCriteriaBuilder
32+ * @var SearchCriteriaBuilder
2133 */
2234 private $ searchCriteriaBuilder ;
2335
2436 /**
25- * @var \Magento\Framework\Api\ FilterBuilder
37+ * @var FilterBuilder
2638 */
2739 private $ filterBuilder ;
2840
2941 /**
30- * @var \Magento\Customer\Api\ GroupRepositoryInterface
42+ * @var GroupRepositoryInterface
3143 */
3244 private $ customerGroupRepository ;
3345
3446 /**
35- * @var \Magento\Store\Api\ WebsiteRepositoryInterface
47+ * @var WebsiteRepositoryInterface
3648 */
3749 private $ websiteRepository ;
3850
3951 /**
40- * @var \Magento\Catalog\Model\Product\Price\Validation\ Result
52+ * @var Result
4153 */
4254 private $ validationResult ;
4355
4456 /**
45- * @var \Magento\Catalog\Model\Product\Price\ TierPricePersistence
57+ * @var TierPricePersistence
4658 */
4759 private $ tierPricePersistence ;
4860
@@ -54,54 +66,48 @@ class TierPriceValidator
5466 private $ customerGroupsByCode = [];
5567
5668 /**
57- * @var \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor
69+ * @var InvalidSkuProcessor
5870 */
5971 private $ invalidSkuProcessor ;
6072
6173 /**
62- * All groups value.
63- *
6474 * @var string
6575 */
6676 private $ allGroupsValue = 'all groups ' ;
6777
6878 /**
69- * All websites value.
70- *
7179 * @var string
7280 */
7381 private $ allWebsitesValue = "0 " ;
7482
7583 /**
76- * Allowed product types.
77- *
7884 * @var array
7985 */
8086 private $ allowedProductTypes = [];
8187
8288 /**
8389 * TierPriceValidator constructor.
8490 *
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
9399 * @param array $allowedProductTypes [optional]
94100 */
95101 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 = []
105111 ) {
106112 $ this ->productIdLocator = $ productIdLocator ;
107113 $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -130,7 +136,7 @@ public function validateSkus(array $skus)
130136 *
131137 * @param array $prices
132138 * @param array $existingPrices
133- * @return \Magento\Catalog\Model\Product\Price\Validation\ Result $validationResult
139+ * @return Result $validationResult
134140 */
135141 public function retrieveValidationResult (array $ prices , array $ existingPrices = [])
136142 {
@@ -159,7 +165,7 @@ public function retrieveValidationResult(array $prices, array $existingPrices =
159165 if (isset ($ pricesBySku [$ price ->getSku ()])) {
160166 $ this ->checkUnique ($ price , $ pricesBySku , $ key , $ validationResult );
161167 }
162- $ this ->checkUnique ($ price , $ existingPrices , $ key , $ validationResult );
168+ $ this ->checkUnique ($ price , $ existingPrices , $ key , $ validationResult, true );
163169 $ this ->checkGroup ($ price , $ key , $ validationResult );
164170 }
165171
@@ -169,14 +175,14 @@ public function retrieveValidationResult(array $prices, array $existingPrices =
169175 /**
170176 * Check that sku value is correct.
171177 *
172- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
178+ * @param TierPriceInterface $price
173179 * @param int $key
174180 * @param array $invalidSkus
175181 * @param Result $validationResult
176182 * @return void
177183 */
178184 private function checkSku (
179- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price ,
185+ TierPriceInterface $ price ,
180186 $ key ,
181187 array $ invalidSkus ,
182188 Result $ validationResult
@@ -207,16 +213,16 @@ private function checkSku(
207213 /**
208214 * Verify that price value is correct.
209215 *
210- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
216+ * @param TierPriceInterface $price
211217 * @param int $key
212218 * @param Result $validationResult
213219 * @return void
214220 */
215- private function checkPrice (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
221+ private function checkPrice (TierPriceInterface $ price , $ key , Result $ validationResult )
216222 {
217223 if (null === $ price ->getPrice ()
218224 || $ price ->getPrice () < 0
219- || ($ price ->getPriceType () === \ Magento \ Catalog \ Api \ Data \ TierPriceInterface::PRICE_TYPE_DISCOUNT
225+ || ($ price ->getPriceType () === TierPriceInterface::PRICE_TYPE_DISCOUNT
220226 && $ price ->getPrice () > 100
221227 )
222228 ) {
@@ -247,27 +253,27 @@ private function checkPrice(\Magento\Catalog\Api\Data\TierPriceInterface $price,
247253 /**
248254 * Verify that price type is correct.
249255 *
250- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
256+ * @param TierPriceInterface $price
251257 * @param array $ids
252258 * @param int $key
253259 * @param Result $validationResult
254260 * @return void
255261 */
256262 private function checkPriceType (
257- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price ,
263+ TierPriceInterface $ price ,
258264 array $ ids ,
259265 $ key ,
260266 Result $ validationResult
261267 ) {
262268 if (!in_array (
263269 $ price ->getPriceType (),
264270 [
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
267273 ]
268274 )
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 )
271277 ) {
272278 $ validationResult ->addFailedItem (
273279 $ key ,
@@ -296,12 +302,12 @@ private function checkPriceType(
296302 /**
297303 * Verify that product quantity is correct.
298304 *
299- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
305+ * @param TierPriceInterface $price
300306 * @param int $key
301307 * @param Result $validationResult
302308 * @return void
303309 */
304- private function checkQuantity (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
310+ private function checkQuantity (TierPriceInterface $ price , $ key , Result $ validationResult )
305311 {
306312 if ($ price ->getQuantity () < 1 ) {
307313 $ validationResult ->addFailedItem (
@@ -329,12 +335,12 @@ private function checkQuantity(\Magento\Catalog\Api\Data\TierPriceInterface $pri
329335 /**
330336 * Verify that website exists.
331337 *
332- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
338+ * @param TierPriceInterface $price
333339 * @param int $key
334340 * @param Result $validationResult
335341 * @return void
336342 */
337- private function checkWebsite (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
343+ private function checkWebsite (TierPriceInterface $ price , $ key , Result $ validationResult )
338344 {
339345 try {
340346 $ this ->websiteRepository ->getById ($ price ->getWebsiteId ());
@@ -364,49 +370,51 @@ private function checkWebsite(\Magento\Catalog\Api\Data\TierPriceInterface $pric
364370 /**
365371 * Check website value is unique.
366372 *
367- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $tierPrice
373+ * @param TierPriceInterface $tierPrice
368374 * @param array $prices
369375 * @param int $key
370376 * @param Result $validationResult
377+ * @param bool $isExistingPrice
371378 * @return void
372379 */
373380 private function checkUnique (
374- \ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ tierPrice ,
381+ TierPriceInterface $ tierPrice ,
375382 array $ prices ,
376383 $ key ,
377- Result $ validationResult
384+ Result $ validationResult ,
385+ bool $ isExistingPrice = false
378386 ) {
379387 if (isset ($ prices [$ tierPrice ->getSku ()])) {
380388 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+ ),
396410 [
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 ()
401415 ]
402- ),
403- [
404- 'SKU ' => $ price ->getSku (),
405- 'websiteId ' => $ price ->getWebsiteId (),
406- 'customerGroup ' => $ price ->getCustomerGroup (),
407- 'qty ' => $ price ->getQuantity ()
408- ]
409- );
416+ );
417+ }
410418 }
411419 }
412420 }
@@ -415,12 +423,13 @@ private function checkUnique(
415423 /**
416424 * Check customer group exists and has correct value.
417425 *
418- * @param \Magento\Catalog\Api\Data\ TierPriceInterface $price
426+ * @param TierPriceInterface $price
419427 * @param int $key
420428 * @param Result $validationResult
421429 * @return void
430+ * @throws LocalizedException
422431 */
423- private function checkGroup (\ Magento \ Catalog \ Api \ Data \ TierPriceInterface $ price , $ key , Result $ validationResult )
432+ private function checkGroup (TierPriceInterface $ price , $ key , Result $ validationResult )
424433 {
425434 $ customerGroup = strtolower ($ price ->getCustomerGroup ());
426435
@@ -452,8 +461,9 @@ private function checkGroup(\Magento\Catalog\Api\Data\TierPriceInterface $price,
452461 *
453462 * @param string $code
454463 * @return int|bool
464+ * @throws LocalizedException
455465 */
456- private function retrieveGroupValue ($ code )
466+ private function retrieveGroupValue (string $ code )
457467 {
458468 if (!isset ($ this ->customerGroupsByCode [$ code ])) {
459469 $ searchCriteria = $ this ->searchCriteriaBuilder ->addFilters (
@@ -473,4 +483,20 @@ private function retrieveGroupValue($code)
473483
474484 return $ this ->customerGroupsByCode [$ code ];
475485 }
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+ }
476502}
0 commit comments