Skip to content

Commit c60e131

Browse files
author
Konosov, Andrey(akonosov)
committed
Merge pull request #585 from akonosov/ie
[EPAM] Import/Export Phase #2 Sprint 1-2
2 parents 6f02d36 + 90ac346 commit c60e131

File tree

127 files changed

+7711
-1893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+7711
-1893
lines changed

app/code/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricing.php

Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
99
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
10+
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1011
use Magento\Framework\App\Resource;
1112

1213
/**
@@ -45,6 +46,12 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
4546

4647
const ENTITY_TYPE_CODE = 'advanced_pricing';
4748

49+
const VALIDATOR_MAIN = 'validator';
50+
51+
const VALIDATOR_WEBSITE = 'validator_website';
52+
53+
const VALIDATOR_GROUP_PRICE = 'validator_group_price';
54+
4855
/**
4956
* Validation failure message template definitions
5057
*
@@ -61,6 +68,31 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
6168
ValidatorInterface::ERROR_INVALID_GROUP_PRICE_SITE => 'Group Price data website is invalid',
6269
ValidatorInterface::ERROR_INVALID_GROUP_PRICE_GROUP => 'Group Price customer group is invalid',
6370
ValidatorInterface::ERROR_GROUP_PRICE_DATA_INCOMPLETE => 'Group Price data is incomplete',
71+
ValidatorInterface::ERROR_INVALID_ATTRIBUTE_DECIMAL =>
72+
'Value for \'%s\' attribute contains incorrect value, acceptable values are in decimal format',
73+
];
74+
75+
/**
76+
* If we should check column names
77+
*
78+
* @var bool
79+
*/
80+
protected $needColumnCheck = true;
81+
82+
/**
83+
* Valid column names
84+
*
85+
* @array
86+
*/
87+
protected $validColumnNames = [
88+
self::COL_SKU,
89+
self::COL_TIER_PRICE_WEBSITE,
90+
self::COL_TIER_PRICE_CUSTOMER_GROUP,
91+
self::COL_TIER_PRICE_QTY,
92+
self::COL_TIER_PRICE,
93+
self::COL_GROUP_PRICE_WEBSITE,
94+
self::COL_GROUP_PRICE_CUSTOMER_GROUP,
95+
self::COL_GROUP_PRICE,
6496
];
6597

6698
/**
@@ -96,9 +128,9 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
96128
protected $_importProduct;
97129

98130
/**
99-
* @var AdvancedPricing\Validator
131+
* @var array
100132
*/
101-
protected $_validator;
133+
protected $_validators = [];
102134

103135
/**
104136
* @var array
@@ -110,16 +142,6 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
110142
*/
111143
protected $_oldSkus;
112144

113-
/**
114-
* @var AdvancedPricing\Validator\Website
115-
*/
116-
protected $websiteValidator;
117-
118-
/**
119-
* @var AdvancedPricing\Validator\GroupPrice
120-
*/
121-
protected $groupPriceValidator;
122-
123145
/**
124146
* Permanent entity columns.
125147
*
@@ -141,12 +163,15 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
141163

142164
/**
143165
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
144-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
145166
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
146167
* @param \Magento\ImportExport\Helper\Data $importExportData
147-
* @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper
148168
* @param \Magento\ImportExport\Model\Resource\Import\Data $importData
169+
* @param \Magento\Eav\Model\Config $config
149170
* @param \Magento\Framework\App\Resource $resource
171+
* @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper
172+
* @param \Magento\Framework\Stdlib\StringUtils $string
173+
* @param ProcessingErrorAggregatorInterface $errorAggregator
174+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
150175
* @param \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceFactory $resourceFactory
151176
* @param \Magento\Catalog\Model\Product $productModel
152177
* @param \Magento\Catalog\Helper\Data $catalogData
@@ -155,14 +180,18 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
155180
* @param AdvancedPricing\Validator $validator
156181
* @param AdvancedPricing\Validator\Website $websiteValidator
157182
* @param AdvancedPricing\Validator\GroupPrice $groupPriceValidator
183+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
158184
*/
159185
public function __construct(
160-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
161186
\Magento\Framework\Json\Helper\Data $jsonHelper,
162187
\Magento\ImportExport\Helper\Data $importExportData,
163-
\Magento\ImportExport\Model\Resource\Helper $resourceHelper,
164188
\Magento\ImportExport\Model\Resource\Import\Data $importData,
189+
\Magento\Eav\Model\Config $config,
165190
\Magento\Framework\App\Resource $resource,
191+
\Magento\ImportExport\Model\Resource\Helper $resourceHelper,
192+
\Magento\Framework\Stdlib\StringUtils $string,
193+
ProcessingErrorAggregatorInterface $errorAggregator,
194+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
166195
\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceFactory $resourceFactory,
167196
\Magento\Catalog\Model\Product $productModel,
168197
\Magento\Catalog\Helper\Data $catalogData,
@@ -177,17 +206,33 @@ public function __construct(
177206
$this->_importExportData = $importExportData;
178207
$this->_resourceHelper = $resourceHelper;
179208
$this->_dataSourceModel = $importData;
180-
$this->_connection = $resource->getConnection();
209+
$this->_connection = $resource->getConnection('write');
181210
$this->_resourceFactory = $resourceFactory;
182211
$this->_productModel = $productModel;
183212
$this->_catalogData = $catalogData;
184213
$this->_storeResolver = $storeResolver;
185214
$this->_importProduct = $importProduct;
186-
$this->_validator = $validator;
215+
$this->_validators[self::VALIDATOR_MAIN] = $validator->init($this);
187216
$this->_oldSkus = $this->retrieveOldSkus();
188-
$this->websiteValidator = $websiteValidator;
189-
$this->groupPriceValidator = $groupPriceValidator;
217+
$this->_validators[self::VALIDATOR_WEBSITE] = $websiteValidator;
218+
$this->_validators[self::VALIDATOR_GROUP_PRICE] = $groupPriceValidator;
219+
$this->errorAggregator = $errorAggregator;
190220
$this->_catalogProductEntity = $this->_resourceFactory->create()->getTable('catalog_product_entity');
221+
222+
foreach (array_merge($this->errorMessageTemplates, $this->_messageTemplates) as $errorCode => $message) {
223+
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
224+
}
225+
}
226+
227+
/**
228+
* Validator object getter.
229+
*
230+
* @param string $type
231+
* @return AdvancedPricing\Validator|AdvancedPricing\Validator\Website|AdvancedPricing\Validator\GroupPrice
232+
*/
233+
protected function _getValidator($type)
234+
{
235+
return $this->_validators[$type];
191236
}
192237

193238
/**
@@ -211,7 +256,7 @@ public function validateRow(array $rowData, $rowNum)
211256
{
212257
$sku = false;
213258
if (isset($this->_validatedRows[$rowNum])) {
214-
return !isset($this->_invalidRows[$rowNum]);
259+
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
215260
}
216261
$this->_validatedRows[$rowNum] = true;
217262
// BEHAVIOR_DELETE use specific validation logic
@@ -222,8 +267,8 @@ public function validateRow(array $rowData, $rowNum)
222267
}
223268
return true;
224269
}
225-
if (!$this->_validator->isValid($rowData)) {
226-
foreach ($this->_validator->getMessages() as $message) {
270+
if (!$this->_getValidator(self::VALIDATOR_MAIN)->isValid($rowData)) {
271+
foreach ($this->_getValidator(self::VALIDATOR_MAIN)->getMessages() as $message) {
227272
$this->addRowError($message, $rowNum);
228273
}
229274
}
@@ -233,7 +278,7 @@ public function validateRow(array $rowData, $rowNum)
233278
if (false === $sku) {
234279
$this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum);
235280
}
236-
return !isset($this->_invalidRows[$rowNum]);
281+
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
237282
}
238283

239284
/**
@@ -277,10 +322,14 @@ public function deleteAdvancedPricing()
277322
$listSku = [];
278323
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
279324
foreach ($bunch as $rowNum => $rowData) {
280-
if ($this->validateRow($rowData, $rowNum)) {
325+
$this->validateRow($rowData, $rowNum);
326+
if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) {
281327
$rowSku = $rowData[self::COL_SKU];
282328
$listSku[] = $rowSku;
283329
}
330+
if ($this->getErrorAggregator()->hasToBeTerminated()) {
331+
$this->getErrorAggregator()->addRowToSkip($rowNum);
332+
}
284333
}
285334
}
286335
if ($listSku) {
@@ -307,6 +356,7 @@ public function replaceAdvancedPricing()
307356
*
308357
* @return $this
309358
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
359+
* @SuppressWarnings(PHPMD.NPathComplexity)
310360
*/
311361
protected function saveAndReplaceAdvancedPrices()
312362
{
@@ -323,6 +373,11 @@ protected function saveAndReplaceAdvancedPrices()
323373
$this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum);
324374
continue;
325375
}
376+
if ($this->getErrorAggregator()->hasToBeTerminated()) {
377+
$this->getErrorAggregator()->addRowToSkip($rowNum);
378+
continue;
379+
}
380+
326381
$rowSku = $rowData[self::COL_SKU];
327382
$listSku[] = $rowSku;
328383
if (!empty($rowData[self::COL_TIER_PRICE_WEBSITE])) {
@@ -359,7 +414,7 @@ protected function saveAndReplaceAdvancedPrices()
359414
}
360415
} elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $behavior) {
361416
$this->processCountExistingPrices($tierPrices, self::TABLE_TIER_PRICE)
362-
->processcountExistingPrices($groupPrices, self::TABLE_GROUPED_PRICE)
417+
->processCountExistingPrices($groupPrices, self::TABLE_GROUPED_PRICE)
363418
->processCountNewPrices($tierPrices, $groupPrices);
364419
$this->saveProductPrices($tierPrices, self::TABLE_TIER_PRICE)
365420
->saveProductPrices($groupPrices, self::TABLE_GROUPED_PRICE);
@@ -462,7 +517,7 @@ protected function setUpdatedAt(array $listSku)
462517
*/
463518
protected function getWebSiteId($websiteCode)
464519
{
465-
$result = $websiteCode == $this->websiteValidator->getAllWebsitesValue() ||
520+
$result = $websiteCode == $this->_getValidator(self::VALIDATOR_WEBSITE)->getAllWebsitesValue() ||
466521
$this->_catalogData->isPriceGlobal() ? 0 : $this->_storeResolver->getWebsiteCodeToId($websiteCode);
467522
return $result;
468523
}
@@ -475,7 +530,7 @@ protected function getWebSiteId($websiteCode)
475530
*/
476531
protected function getCustomerGroupId($customerGroup)
477532
{
478-
$customerGroups = $this->groupPriceValidator->getCustomerGroups();
533+
$customerGroups = $this->_getValidator(self::VALIDATOR_GROUP_PRICE)->getCustomerGroups();
479534
return $customerGroup == self::VALUE_ALL_GROUPS ? 0 : $customerGroups[$customerGroup];
480535
}
481536

app/code/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricing/Validator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function isValid($value)
4343
}
4444

4545
/**
46-
* Init validators
47-
*
48-
* @return void
46+
* @param \Magento\CatalogImportExport\Model\Import\Product $context
47+
* @return $this
4948
*/
50-
public function init()
49+
public function init($context)
5150
{
5251
foreach ($this->validators as $validator) {
53-
$validator->init();
52+
$validator->init($context);
5453
}
54+
return $this;
5555
}
5656
}

app/code/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricing/Validator/GroupPrice.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing\Validator;
77

88
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing;
9+
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
910

1011
class GroupPrice extends \Magento\CatalogImportExport\Model\Import\Product\Validator\AbstractPrice
1112
{
@@ -38,15 +39,42 @@ public function __construct(
3839
}
3940

4041
/**
41-
* Call parent init()
42-
*
43-
* @return $this
42+
* {@inheritdoc}
4443
*/
45-
public function init()
44+
public function init($context)
4645
{
4746
foreach ($this->groupRepository->getList($this->searchCriteriaBuilder->create())->getItems() as $group) {
4847
$this->customerGroups[$group->getCode()] = $group->getId();
4948
}
49+
$this->context = $context;
50+
}
51+
52+
/**
53+
* @param string $attribute
54+
* @return void
55+
*/
56+
protected function addDecimalError($attribute)
57+
{
58+
$this->_addMessages(
59+
[
60+
sprintf(
61+
$this->context->retrieveMessageTemplate(
62+
RowValidatorInterface::ERROR_INVALID_ATTRIBUTE_DECIMAL
63+
),
64+
$attribute
65+
)
66+
]
67+
);
68+
}
69+
70+
/**
71+
* @return void
72+
*/
73+
protected function initCustomerGroups()
74+
{
75+
if (!$this->customerGroups) {
76+
$this->init($this->context);
77+
}
5078
}
5179

5280
/**
@@ -58,22 +86,27 @@ public function init()
5886
public function isValid($value)
5987
{
6088
$this->_clearMessages();
61-
if (!$this->customerGroups) {
62-
$this->init();
89+
$this->initCustomerGroups();
90+
if (!$this->isValidValueAndLength($value)) {
91+
return true;
6392
}
64-
if ($this->isValidValueAndLength($value)) {
65-
if (!isset($value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE])
66-
|| !isset($value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP])
67-
|| $this->hasEmptyColumns($value)) {
68-
$this->_addMessages([self::ERROR_GROUP_PRICE_DATA_INCOMPLETE]);
69-
return false;
70-
} elseif (
71-
$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP] == AdvancedPricing::VALUE_ALL_GROUPS
72-
|| !isset($this->customerGroups[$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP]])
73-
) {
74-
$this->_addMessages([self::ERROR_INVALID_GROUP_PRICE_GROUP]);
75-
return false;
76-
}
93+
if (!isset($value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE])
94+
|| !isset($value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP])
95+
|| $this->hasEmptyColumns($value)) {
96+
$this->_addMessages([self::ERROR_GROUP_PRICE_DATA_INCOMPLETE]);
97+
return false;
98+
} elseif (
99+
$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP] == AdvancedPricing::VALUE_ALL_GROUPS
100+
|| !isset($this->customerGroups[$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP]])
101+
) {
102+
$this->_addMessages([self::ERROR_INVALID_GROUP_PRICE_GROUP]);
103+
return false;
104+
}
105+
if (!is_numeric($value[AdvancedPricing::COL_GROUP_PRICE])
106+
|| $value[AdvancedPricing::COL_GROUP_PRICE] < 0
107+
) {
108+
$this->addDecimalError(AdvancedPricing::COL_GROUP_PRICE);
109+
return false;
77110
}
78111
return true;
79112
}
@@ -86,7 +119,7 @@ public function isValid($value)
86119
public function getCustomerGroups()
87120
{
88121
if (!$this->customerGroups) {
89-
$this->init();
122+
$this->init($this->context);
90123
}
91124
return $this->customerGroups;
92125
}

0 commit comments

Comments
 (0)