Skip to content

Commit

Permalink
Merge pull request #585 from akonosov/ie
Browse files Browse the repository at this point in the history
[EPAM] Import/Export Phase #2 Sprint 1-2
  • Loading branch information
Konosov, Andrey(akonosov) committed Sep 10, 2015
2 parents 6f02d36 + 90ac346 commit c60e131
Show file tree
Hide file tree
Showing 127 changed files with 7,711 additions and 1,893 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\Framework\App\Resource;

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

const ENTITY_TYPE_CODE = 'advanced_pricing';

const VALIDATOR_MAIN = 'validator';

const VALIDATOR_WEBSITE = 'validator_website';

const VALIDATOR_GROUP_PRICE = 'validator_group_price';

/**
* Validation failure message template definitions
*
Expand All @@ -61,6 +68,31 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
ValidatorInterface::ERROR_INVALID_GROUP_PRICE_SITE => 'Group Price data website is invalid',
ValidatorInterface::ERROR_INVALID_GROUP_PRICE_GROUP => 'Group Price customer group is invalid',
ValidatorInterface::ERROR_GROUP_PRICE_DATA_INCOMPLETE => 'Group Price data is incomplete',
ValidatorInterface::ERROR_INVALID_ATTRIBUTE_DECIMAL =>
'Value for \'%s\' attribute contains incorrect value, acceptable values are in decimal format',
];

/**
* If we should check column names
*
* @var bool
*/
protected $needColumnCheck = true;

/**
* Valid column names
*
* @array
*/
protected $validColumnNames = [
self::COL_SKU,
self::COL_TIER_PRICE_WEBSITE,
self::COL_TIER_PRICE_CUSTOMER_GROUP,
self::COL_TIER_PRICE_QTY,
self::COL_TIER_PRICE,
self::COL_GROUP_PRICE_WEBSITE,
self::COL_GROUP_PRICE_CUSTOMER_GROUP,
self::COL_GROUP_PRICE,
];

/**
Expand Down Expand Up @@ -96,9 +128,9 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
protected $_importProduct;

/**
* @var AdvancedPricing\Validator
* @var array
*/
protected $_validator;
protected $_validators = [];

/**
* @var array
Expand All @@ -110,16 +142,6 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
*/
protected $_oldSkus;

/**
* @var AdvancedPricing\Validator\Website
*/
protected $websiteValidator;

/**
* @var AdvancedPricing\Validator\GroupPrice
*/
protected $groupPriceValidator;

/**
* Permanent entity columns.
*
Expand All @@ -141,12 +163,15 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\ImportExport\Helper\Data $importExportData
* @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper
* @param \Magento\ImportExport\Model\Resource\Import\Data $importData
* @param \Magento\Eav\Model\Config $config
* @param \Magento\Framework\App\Resource $resource
* @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceFactory $resourceFactory
* @param \Magento\Catalog\Model\Product $productModel
* @param \Magento\Catalog\Helper\Data $catalogData
Expand All @@ -155,14 +180,18 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
* @param AdvancedPricing\Validator $validator
* @param AdvancedPricing\Validator\Website $websiteValidator
* @param AdvancedPricing\Validator\GroupPrice $groupPriceValidator
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\ImportExport\Helper\Data $importExportData,
\Magento\ImportExport\Model\Resource\Helper $resourceHelper,
\Magento\ImportExport\Model\Resource\Import\Data $importData,
\Magento\Eav\Model\Config $config,
\Magento\Framework\App\Resource $resource,
\Magento\ImportExport\Model\Resource\Helper $resourceHelper,
\Magento\Framework\Stdlib\StringUtils $string,
ProcessingErrorAggregatorInterface $errorAggregator,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceFactory $resourceFactory,
\Magento\Catalog\Model\Product $productModel,
\Magento\Catalog\Helper\Data $catalogData,
Expand All @@ -177,17 +206,33 @@ public function __construct(
$this->_importExportData = $importExportData;
$this->_resourceHelper = $resourceHelper;
$this->_dataSourceModel = $importData;
$this->_connection = $resource->getConnection();
$this->_connection = $resource->getConnection('write');
$this->_resourceFactory = $resourceFactory;
$this->_productModel = $productModel;
$this->_catalogData = $catalogData;
$this->_storeResolver = $storeResolver;
$this->_importProduct = $importProduct;
$this->_validator = $validator;
$this->_validators[self::VALIDATOR_MAIN] = $validator->init($this);
$this->_oldSkus = $this->retrieveOldSkus();
$this->websiteValidator = $websiteValidator;
$this->groupPriceValidator = $groupPriceValidator;
$this->_validators[self::VALIDATOR_WEBSITE] = $websiteValidator;
$this->_validators[self::VALIDATOR_GROUP_PRICE] = $groupPriceValidator;
$this->errorAggregator = $errorAggregator;
$this->_catalogProductEntity = $this->_resourceFactory->create()->getTable('catalog_product_entity');

foreach (array_merge($this->errorMessageTemplates, $this->_messageTemplates) as $errorCode => $message) {
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
}
}

/**
* Validator object getter.
*
* @param string $type
* @return AdvancedPricing\Validator|AdvancedPricing\Validator\Website|AdvancedPricing\Validator\GroupPrice
*/
protected function _getValidator($type)
{
return $this->_validators[$type];
}

/**
Expand All @@ -211,7 +256,7 @@ public function validateRow(array $rowData, $rowNum)
{
$sku = false;
if (isset($this->_validatedRows[$rowNum])) {
return !isset($this->_invalidRows[$rowNum]);
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}
$this->_validatedRows[$rowNum] = true;
// BEHAVIOR_DELETE use specific validation logic
Expand All @@ -222,8 +267,8 @@ public function validateRow(array $rowData, $rowNum)
}
return true;
}
if (!$this->_validator->isValid($rowData)) {
foreach ($this->_validator->getMessages() as $message) {
if (!$this->_getValidator(self::VALIDATOR_MAIN)->isValid($rowData)) {
foreach ($this->_getValidator(self::VALIDATOR_MAIN)->getMessages() as $message) {
$this->addRowError($message, $rowNum);
}
}
Expand All @@ -233,7 +278,7 @@ public function validateRow(array $rowData, $rowNum)
if (false === $sku) {
$this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum);
}
return !isset($this->_invalidRows[$rowNum]);
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}

/**
Expand Down Expand Up @@ -277,10 +322,14 @@ public function deleteAdvancedPricing()
$listSku = [];
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
foreach ($bunch as $rowNum => $rowData) {
if ($this->validateRow($rowData, $rowNum)) {
$this->validateRow($rowData, $rowNum);
if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) {
$rowSku = $rowData[self::COL_SKU];
$listSku[] = $rowSku;
}
if ($this->getErrorAggregator()->hasToBeTerminated()) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
}
}
}
if ($listSku) {
Expand All @@ -307,6 +356,7 @@ public function replaceAdvancedPricing()
*
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function saveAndReplaceAdvancedPrices()
{
Expand All @@ -323,6 +373,11 @@ protected function saveAndReplaceAdvancedPrices()
$this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum);
continue;
}
if ($this->getErrorAggregator()->hasToBeTerminated()) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
}

$rowSku = $rowData[self::COL_SKU];
$listSku[] = $rowSku;
if (!empty($rowData[self::COL_TIER_PRICE_WEBSITE])) {
Expand Down Expand Up @@ -359,7 +414,7 @@ protected function saveAndReplaceAdvancedPrices()
}
} elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $behavior) {
$this->processCountExistingPrices($tierPrices, self::TABLE_TIER_PRICE)
->processcountExistingPrices($groupPrices, self::TABLE_GROUPED_PRICE)
->processCountExistingPrices($groupPrices, self::TABLE_GROUPED_PRICE)
->processCountNewPrices($tierPrices, $groupPrices);
$this->saveProductPrices($tierPrices, self::TABLE_TIER_PRICE)
->saveProductPrices($groupPrices, self::TABLE_GROUPED_PRICE);
Expand Down Expand Up @@ -462,7 +517,7 @@ protected function setUpdatedAt(array $listSku)
*/
protected function getWebSiteId($websiteCode)
{
$result = $websiteCode == $this->websiteValidator->getAllWebsitesValue() ||
$result = $websiteCode == $this->_getValidator(self::VALIDATOR_WEBSITE)->getAllWebsitesValue() ||
$this->_catalogData->isPriceGlobal() ? 0 : $this->_storeResolver->getWebsiteCodeToId($websiteCode);
return $result;
}
Expand All @@ -475,7 +530,7 @@ protected function getWebSiteId($websiteCode)
*/
protected function getCustomerGroupId($customerGroup)
{
$customerGroups = $this->groupPriceValidator->getCustomerGroups();
$customerGroups = $this->_getValidator(self::VALIDATOR_GROUP_PRICE)->getCustomerGroups();
return $customerGroup == self::VALUE_ALL_GROUPS ? 0 : $customerGroups[$customerGroup];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function isValid($value)
}

/**
* Init validators
*
* @return void
* @param \Magento\CatalogImportExport\Model\Import\Product $context
* @return $this
*/
public function init()
public function init($context)
{
foreach ($this->validators as $validator) {
$validator->init();
$validator->init($context);
}
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing\Validator;

use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;

class GroupPrice extends \Magento\CatalogImportExport\Model\Import\Product\Validator\AbstractPrice
{
Expand Down Expand Up @@ -38,15 +39,42 @@ public function __construct(
}

/**
* Call parent init()
*
* @return $this
* {@inheritdoc}
*/
public function init()
public function init($context)
{
foreach ($this->groupRepository->getList($this->searchCriteriaBuilder->create())->getItems() as $group) {
$this->customerGroups[$group->getCode()] = $group->getId();
}
$this->context = $context;
}

/**
* @param string $attribute
* @return void
*/
protected function addDecimalError($attribute)
{
$this->_addMessages(
[
sprintf(
$this->context->retrieveMessageTemplate(
RowValidatorInterface::ERROR_INVALID_ATTRIBUTE_DECIMAL
),
$attribute
)
]
);
}

/**
* @return void
*/
protected function initCustomerGroups()
{
if (!$this->customerGroups) {
$this->init($this->context);
}
}

/**
Expand All @@ -58,22 +86,27 @@ public function init()
public function isValid($value)
{
$this->_clearMessages();
if (!$this->customerGroups) {
$this->init();
$this->initCustomerGroups();
if (!$this->isValidValueAndLength($value)) {
return true;
}
if ($this->isValidValueAndLength($value)) {
if (!isset($value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE])
|| !isset($value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP])
|| $this->hasEmptyColumns($value)) {
$this->_addMessages([self::ERROR_GROUP_PRICE_DATA_INCOMPLETE]);
return false;
} elseif (
$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP] == AdvancedPricing::VALUE_ALL_GROUPS
|| !isset($this->customerGroups[$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP]])
) {
$this->_addMessages([self::ERROR_INVALID_GROUP_PRICE_GROUP]);
return false;
}
if (!isset($value[AdvancedPricing::COL_GROUP_PRICE_WEBSITE])
|| !isset($value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP])
|| $this->hasEmptyColumns($value)) {
$this->_addMessages([self::ERROR_GROUP_PRICE_DATA_INCOMPLETE]);
return false;
} elseif (
$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP] == AdvancedPricing::VALUE_ALL_GROUPS
|| !isset($this->customerGroups[$value[AdvancedPricing::COL_GROUP_PRICE_CUSTOMER_GROUP]])
) {
$this->_addMessages([self::ERROR_INVALID_GROUP_PRICE_GROUP]);
return false;
}
if (!is_numeric($value[AdvancedPricing::COL_GROUP_PRICE])
|| $value[AdvancedPricing::COL_GROUP_PRICE] < 0
) {
$this->addDecimalError(AdvancedPricing::COL_GROUP_PRICE);
return false;
}
return true;
}
Expand All @@ -86,7 +119,7 @@ public function isValid($value)
public function getCustomerGroups()
{
if (!$this->customerGroups) {
$this->init();
$this->init($this->context);
}
return $this->customerGroups;
}
Expand Down
Loading

0 comments on commit c60e131

Please sign in to comment.