Skip to content

Commit

Permalink
Merge pull request #2101 from magento-qwerty/2.1-bugfixes
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-85206: SKUs are unassigned from Catalog Price Rule conditions
- MAGETWO-83426: [Performance] Customer Import check data does not complete
  • Loading branch information
dvoskoboinikov authored Feb 19, 2018
2 parents 70d7ca4 + c3ff94a commit afbc7d6
Show file tree
Hide file tree
Showing 14 changed files with 603 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Magento\Backend\Block\Widget\Form;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Ui\Component\Layout\Tabs\TabInterface;
use Magento\CatalogRule\Api\Data\RuleInterface;
use Magento\Rule\Model\Condition\AbstractCondition;

class Conditions extends Generic implements TabInterface
{
Expand Down Expand Up @@ -134,7 +136,7 @@ protected function _prepareForm()
}

/**
* @param \Magento\CatalogRule\Api\Data\RuleInterface $model
* @param RuleInterface $model
* @param string $fieldsetId
* @param string $formName
* @return \Magento\Framework\Data\Form
Expand Down Expand Up @@ -175,22 +177,30 @@ protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $fo
->setRenderer($this->_conditions);

$form->setValues($model->getData());
$this->setConditionFormName($model->getConditions(), $formName);
$this->setConditionFormName($model, $model->getConditions(), $formName);
return $form;
}

/**
* @param \Magento\Rule\Model\Condition\AbstractCondition $conditions
* @param RuleInterface $rule
* @param AbstractCondition $conditions
* @param string $formName
* @return void
*/
private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName)
{
private function setConditionFormName(
RuleInterface $rule,
AbstractCondition $conditions,
$formName
) {
$conditions->setFormName($formName);
$conditions->setJsFormObject($formName);
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
//For every fieldset there's a different form object.
$conditions->setJsFormObject(
$rule->getConditionsFieldSetId($formName)
);
$childConditions = $conditions->getCondition();
if ($childConditions && is_array($childConditions)) {
foreach ($conditions->getConditions() as $condition) {
$this->setConditionFormName($condition, $formName);
$this->setConditionFormName($rule, $condition, $formName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entit

const COLUMN_DEFAULT_SHIPPING = 'default_shipping';


/**#@-*/

/**#@+
Expand Down
104 changes: 91 additions & 13 deletions app/code/Magento/CustomerImportExport/Model/Import/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Store\Model\Store;
use Magento\ImportExport\Model\Import;
use Magento\CustomerImportExport\Model\ResourceModel\Import\Address\Storage as AddressStorage;

/**
* @SuppressWarnings(PHPMD.TooManyFields)
Expand Down Expand Up @@ -100,6 +101,8 @@ class Address extends AbstractCustomer
* )
*
* @var array
* @deprected
* @see $addressStorage
*/
protected $_addresses = [];

Expand Down Expand Up @@ -256,6 +259,11 @@ class Address extends AbstractCustomer
*/
private $optionsByWebsite = [];

/**
* @var AddressStorage
*/
private $addressStorage;

/**
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
Expand All @@ -276,6 +284,7 @@ class Address extends AbstractCustomer
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
* @param array $data
* @param Sources\CountryWithWebsites|null $countryWithWebsites
* @param AddressStorage|null $addressStorage
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -299,7 +308,8 @@ public function __construct(
DateTime $dateTime,
\Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator,
array $data = [],
Sources\CountryWithWebsites $countryWithWebsites = null
Sources\CountryWithWebsites $countryWithWebsites = null,
AddressStorage $addressStorage = null
) {
$this->_customerFactory = $customerFactory;
$this->_addressFactory = $addressFactory;
Expand Down Expand Up @@ -352,9 +362,11 @@ public function __construct(
self::ERROR_DUPLICATE_PK,
__('We found another row with this email, website and address ID combination.')
);
$this->addressStorage = $addressStorage
?: ObjectManager::getInstance()->get(AddressStorage::class);

$this->_initAttributes();
$this->_initAddresses()->_initCountryRegions();
$this->_initCountryRegions();
}

/**
Expand Down Expand Up @@ -455,6 +467,8 @@ protected function _getNextEntityId()
* Initialize existent addresses data
*
* @return $this
* @deprecated
* @see prepareCustomerData
*/
protected function _initAddresses()
{
Expand All @@ -472,6 +486,57 @@ protected function _initAddresses()
return $this;
}

/**
* Pre-loading customers for existing customers checks in order
* to perform mass validation/import efficiently.
* Also loading existing addresses for requested customers.
*
* @param \Traversable $rows Each row must contain data from columns email
* and website code.
*
* @return void
*/
public function prepareCustomerData(\Traversable $rows)
{
$customersPresent = [];
foreach ($rows as $rowData) {
$email = isset($rowData[static::COLUMN_EMAIL])
? $rowData[static::COLUMN_EMAIL] : null;
$websiteId = isset($rowData[static::COLUMN_WEBSITE])
? $this->getWebsiteId($rowData[static::COLUMN_WEBSITE]) : false;
if ($email && $websiteId !== false) {
$customersPresent[] = [
'email' => $email,
'website_id' => $websiteId
];
}
}
$this->getCustomerStorage()->prepareCustomers($customersPresent);

$ids = [];
foreach ($customersPresent as $customerData) {
$id = $this->getCustomerStorage()->getCustomerId(
$customerData['email'],
$customerData['website_id']
);
if ($id) {
$ids[] = $id;
}
}

$this->addressStorage->prepareAddresses($ids);
}

/**
* @inheritDoc
*/
public function validateData()
{
$this->prepareCustomerData($this->getSource());

return parent::validateData();
}

/**
* Initialize country regions hash for clever recognition
*
Expand Down Expand Up @@ -500,6 +565,16 @@ protected function _initCountryRegions()
*/
protected function _importData()
{
//Preparing data for mass validation/import.
$rows = [];
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$rows = array_merge($rows, $bunch);
}
$this->prepareCustomerData(new \ArrayObject($rows));
unset($bunch, $rows);
$this->_dataSourceModel->getIterator()->rewind();

//Importing
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$newRows = [];
$updateRows = [];
Expand Down Expand Up @@ -588,9 +663,10 @@ protected function _prepareDataForUpdate(array $rowData)
$defaults = [];
$newAddress = true;
// get address id
if (isset($this->_addresses[$customerId])
&& in_array($rowData[self::COLUMN_ADDRESS_ID], $this->_addresses[$customerId])
) {
if ($this->addressStorage->doesExist(
$rowData[self::COLUMN_ADDRESS_ID],
$customerId
)) {
$newAddress = false;
$addressId = $rowData[self::COLUMN_ADDRESS_ID];
} else {
Expand Down Expand Up @@ -845,12 +921,11 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
$rowNumber,
$multiSeparator
);
} elseif ($attributeParams['is_required'] && (!isset(
$this->_addresses[$customerId]
) || !in_array(
$addressId,
$this->_addresses[$customerId]
))
} elseif ($attributeParams['is_required']
&& !$this->addressStorage->doesExist(
$addressId,
$customerId
)
) {
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
}
Expand Down Expand Up @@ -906,7 +981,10 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
} else {
if (!strlen($addressId)) {
$this->addRowError(self::ERROR_ADDRESS_ID_IS_EMPTY, $rowNumber);
} elseif (!in_array($addressId, $this->_addresses[$customerId])) {
} elseif (!$this->addressStorage->doesExist(
$addressId,
$customerId
)) {
$this->addRowError(self::ERROR_ADDRESS_NOT_FOUND, $rowNumber);
}
}
Expand All @@ -922,7 +1000,7 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
*/
protected function _checkRowDuplicate($customerId, $addressId)
{
if (isset($this->_addresses[$customerId]) && in_array($addressId, $this->_addresses[$customerId])) {
if ($this->addressStorage->doesExist($addressId, $customerId)) {
if (!isset($this->_importedRowPks[$customerId][$addressId])) {
$this->_importedRowPks[$customerId][$addressId] = true;
return false;
Expand Down
38 changes: 38 additions & 0 deletions app/code/Magento/CustomerImportExport/Model/Import/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,43 @@ protected function _getNextEntityId()
return $this->_nextEntityId++;
}

/**
* Pre-loading customers for existing customers checks in order
* to perform mass validation/import efficiently.
*
* @param \Traversable $rows Each row must contain data from columns email
* and website code.
*
* @return void
*/
public function prepareCustomerData(\Traversable $rows)
{
$customersPresent = [];
foreach ($rows as $rowData) {
$email = isset($rowData[static::COLUMN_EMAIL])
? $rowData[static::COLUMN_EMAIL] : null;
$websiteId = isset($rowData[static::COLUMN_WEBSITE])
? $this->getWebsiteId($rowData[static::COLUMN_WEBSITE]) : false;
if ($email && $websiteId !== false) {
$customersPresent[] = [
'email' => $email,
'website_id' => $websiteId
];
}
}
$this->getCustomerStorage()->prepareCustomers($customersPresent);
}

/**
* @inheritDoc
*/
public function validateData()
{
$this->prepareCustomerData($this->getSource());

return parent::validateData();
}

/**
* Prepare customer data for update
*
Expand Down Expand Up @@ -428,6 +465,7 @@ protected function _prepareDataForUpdate(array $rowData)
protected function _importData()
{
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$this->prepareCustomerData(new \ArrayObject($bunch));
$entitiesToCreate = [];
$entitiesToUpdate = [];
$entitiesToDelete = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,28 @@ public function getEntityTypeCode()
return 'customer_composite';
}

/**
* @inheritDoc
*/
public function validateData()
{
//Preparing both customer and address imports for mass validation.
$source = $this->getSource();
$this->_customerEntity->prepareCustomerData($source);
$source->rewind();
$rows = [];
foreach ($source as $row) {
$rows[] = [
Address::COLUMN_EMAIL => $row[Customer::COLUMN_EMAIL],
Address::COLUMN_WEBSITE => $row[Customer::COLUMN_WEBSITE]
];
}
$source->rewind();
$this->_addressEntity->prepareCustomerData(new \ArrayObject($rows));

return parent::validateData();
}

/**
* Validate data row
*
Expand Down
Loading

0 comments on commit afbc7d6

Please sign in to comment.