Skip to content

Commit

Permalink
Merge pull request #1575 from magento-qwerty/Qwerty-PR20171006
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-71552: Attribute values on store view level not searchable - for 2.2
- MAGETWO-72866: Redundant indexers invalidation - RIATCS-340
- MAGETWO-75458: [Backport] - Fix overwrite default value image/file with NULL #10253 - for 2.2
- MAGETWO-75460: [Backport] - LowestPriceOptionsProvider returns products without attributes which are used for price calculation (e.g. tax adjustment)
- MAGETWO-80193: [2.2.x] - Add cast to string for CUST_GROUP_ALL #10475
- MAGETWO-80204: [2.2.x] - Grammar fix for #9533 #10627
- MAGETWO-71549: Impossible to export Advanced Prices on a medium profile
- MAGETWO-80198: [2.2.x] - Fix issue #10565 #10575
- MAGETWO-80197: [2.2.x] - Fix JS translation search #10445
- MAGETWO-80195: [2.2.x] - Send different base currency in Google analytics #10508
  • Loading branch information
dvoskoboinikov authored Oct 10, 2017
2 parents cc71ccf + ffa0f42 commit 9b87eb0
Show file tree
Hide file tree
Showing 25 changed files with 522 additions and 165 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.metadata
/.project
/.settings
/.vscode
atlassian*
/nbproject
/robots.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
];

/**
* @var string[]
*/
private $websiteCodesMap = [];

/**
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Eav\Model\Config $config
Expand Down Expand Up @@ -213,6 +218,7 @@ public function export()
break;
}
}

return $writer->getContents();
}

Expand Down Expand Up @@ -255,70 +261,111 @@ public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entit
*/
protected function getExportData()
{
if ($this->_passTierPrice) {
return [];
}

$exportData = [];
try {
$rawData = $this->collectRawData();
$productIds = array_keys($rawData);
if (isset($productIds)) {
if (!$this->_passTierPrice) {
$exportData = array_merge(
$exportData,
$this->getTierPrices($productIds, ImportAdvancedPricing::TABLE_TIER_PRICE)
);
$productsByStores = $this->loadCollection();
if (!empty($productsByStores)) {
$productLinkIds = array_map(
function (array $productData) {
return $productData[Store::DEFAULT_STORE_ID][$this->getProductEntityLinkField()];
},
$productsByStores
);
$tierPricesData = $this->getTierPrices(
$productLinkIds,
ImportAdvancedPricing::TABLE_TIER_PRICE
);

$exportData = $this->correctExportData(
$productsByStores,
$tierPricesData
);
if (!empty($exportData)) {
asort($exportData);
}
}
if ($exportData) {
$exportData = $this->correctExportData($exportData);
}
if (isset($exportData)) {
asort($exportData);
}
} catch (\Exception $e) {
$this->_logger->critical($e);
}

return $exportData;
}

/**
* Correct export data.
* @param array $tierPriceData Tier price information.
*
* @param array $exportData
* @return array
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @return array Formatted for export tier price information.
*/
protected function correctExportData($exportData)
private function createExportRow(array $tierPriceData): array
{
$customExportData = [];
foreach ($exportData as $key => $row) {
$exportRow = $this->templateExportData;
foreach ($exportRow as $keyTemplate => $valueTemplate) {
if (isset($row[$keyTemplate])) {
if (in_array($keyTemplate, $this->_priceWebsite)) {
$exportRow[$keyTemplate] = $this->_getWebsiteCode(
$row[$keyTemplate]
);
} elseif (in_array($keyTemplate, $this->_priceCustomerGroup)) {
$exportRow[$keyTemplate] = $this->_getCustomerGroupById(
$row[$keyTemplate],
isset($row[ImportAdvancedPricing::VALUE_ALL_GROUPS])
? $row[ImportAdvancedPricing::VALUE_ALL_GROUPS]
: null
);
unset($exportRow[ImportAdvancedPricing::VALUE_ALL_GROUPS]);
} elseif ($keyTemplate === ImportAdvancedPricing::COL_TIER_PRICE) {
$exportRow[$keyTemplate] = $row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
? $row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
: $row[ImportAdvancedPricing::COL_TIER_PRICE];
$exportRow[ImportAdvancedPricing::COL_TIER_PRICE_TYPE]
= $this->tierPriceTypeValue($row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]);
} else {
$exportRow[$keyTemplate] = $row[$keyTemplate];
}
$exportRow = $this->templateExportData;
foreach (array_keys($exportRow) as $keyTemplate) {
if (array_key_exists($keyTemplate, $tierPriceData)) {
if (in_array($keyTemplate, $this->_priceWebsite)) {
$exportRow[$keyTemplate] = $this->_getWebsiteCode(
$tierPriceData[$keyTemplate]
);
} elseif (in_array($keyTemplate, $this->_priceCustomerGroup)) {
$exportRow[$keyTemplate] = $this->_getCustomerGroupById(
$tierPriceData[$keyTemplate],
$tierPriceData[ImportAdvancedPricing::VALUE_ALL_GROUPS]
);
unset($exportRow[ImportAdvancedPricing::VALUE_ALL_GROUPS]);
} elseif ($keyTemplate
=== ImportAdvancedPricing::COL_TIER_PRICE
) {
$exportRow[$keyTemplate]
= $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
? $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
: $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE];
$exportRow[ImportAdvancedPricing::COL_TIER_PRICE_TYPE]
= $this->tierPriceTypeValue($tierPriceData);
} else {
$exportRow[$keyTemplate] = $tierPriceData[$keyTemplate];
}
}
}

return $exportRow;
}

$customExportData[$key] = $exportRow;
unset($exportRow);
/**
* Correct export data.
*
* @param array $productsData
* @param array $tierPricesData
*
* @return array
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
protected function correctExportData(
array $productsData,
array $tierPricesData
): array {
//Assigning SKUs to tier prices data.
$productLinkIdToSkuMap = [];
foreach ($productsData as $productData) {
$productLinkIdToSkuMap[$productData[Store::DEFAULT_STORE_ID][$this->getProductEntityLinkField()]]
= $productData[Store::DEFAULT_STORE_ID]['sku'];
}
unset($productData);
$linkedTierPricesData = [];
foreach ($tierPricesData as $tierPriceData) {
$sku = $productLinkIdToSkuMap[$tierPriceData['product_link_id']];
$linkedTierPricesData[] = array_merge(
$tierPriceData,
[ImportAdvancedPricing::COL_SKU => $sku]
);
}
unset($sku, $tierPriceData);

$customExportData = [];
foreach ($linkedTierPricesData as $row) {
$customExportData[] = $this->createExportRow($row);
}

return $customExportData;
Expand All @@ -327,67 +374,66 @@ protected function correctExportData($exportData)
/**
* Check type for tier price.
*
* @param string $tierPricePercentage
* @param array $tierPriceData
*
* @return string
*/
private function tierPriceTypeValue($tierPricePercentage)
private function tierPriceTypeValue(array $tierPriceData): string
{
return $tierPricePercentage
return $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED;
}

/**
* Get tier prices.
*
* @param array $listSku
* @param string[] $productLinksIds
* @param string $table
* @return array|bool
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getTierPrices(array $listSku, $table)
protected function getTierPrices(array $productLinksIds, $table)
{
$exportFilter = null;
$price = null;
if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP])) {
$exportFilter = $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP];
}
$productEntityLinkField = $this->getProductEntityLinkField();

if ($table == ImportAdvancedPricing::TABLE_TIER_PRICE) {
$selectFields = [
ImportAdvancedPricing::COL_SKU => 'cpe.sku',
ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id',
ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups',
ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id',
ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id',
ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups',
ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id',
ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
'product_link_id' => 'ap.'
.$productEntityLinkField,
];
if (isset($exportFilter) && !empty($exportFilter)) {
$price = $exportFilter['tier_price'];
}
}
if ($listSku) {
if (isset($exportFilter) && !empty($exportFilter)) {
$date = $exportFilter[\Magento\Catalog\Model\Category::KEY_UPDATED_AT];
if (isset($date[0]) && !empty($date[0])) {
$updatedAtFrom = $this->_localeDate->date($date[0], null, false)->format('Y-m-d H:i:s');
}
if (isset($date[1]) && !empty($date[1])) {
$updatedAtTo = $this->_localeDate->date($date[1], null, false)->format('Y-m-d H:i:s');
if ($exportFilter) {
if (array_key_exists('tier_price', $exportFilter)) {
$price = $exportFilter['tier_price'];
}
}
} else {
throw new \InvalidArgumentException('Proper table name needed');
}

if ($productLinksIds) {
try {
$productEntityLinkField = $this->getProductEntityLinkField();
$select = $this->_connection->select()
->from(
['cpe' => $this->_resource->getTableName('catalog_product_entity')],
$selectFields
)
->joinInner(
['ap' => $this->_resource->getTableName($table)],
'ap.' . $productEntityLinkField . ' = cpe.' . $productEntityLinkField,
[]
$selectFields
)
->where('cpe.entity_id IN (?)', $listSku);
->where(
'ap.'.$productEntityLinkField.' IN (?)',
$productLinksIds
);

if (isset($price[0]) && !empty($price[0])) {
$select->where('ap.value >= ?', $price[0]);
Expand All @@ -398,18 +444,16 @@ protected function getTierPrices(array $listSku, $table)
if (isset($price[0]) && !empty($price[0]) || isset($price[1]) && !empty($price[1])) {
$select->orWhere('ap.percentage_value IS NOT NULL');
}
if (isset($updatedAtFrom) && !empty($updatedAtFrom)) {
$select->where('cpe.updated_at >= ?', $updatedAtFrom);
}
if (isset($updatedAtTo) && !empty($updatedAtTo)) {
$select->where('cpe.updated_at <= ?', $updatedAtTo);
}

$exportData = $this->_connection->fetchAll($select);
} catch (\Exception $e) {
return false;
}

return $exportData;
} else {
return false;
}
return $exportData;
}

/**
Expand All @@ -418,35 +462,46 @@ protected function getTierPrices(array $listSku, $table)
* @param int $websiteId
* @return string
*/
protected function _getWebsiteCode($websiteId)
protected function _getWebsiteCode(int $websiteId): string
{
$storeName = ($websiteId == 0)
? ImportAdvancedPricing::VALUE_ALL_WEBSITES
: $this->_storeManager->getWebsite($websiteId)->getCode();
$currencyCode = '';
if ($websiteId == 0) {
$currencyCode = $this->_storeManager->getWebsite($websiteId)->getBaseCurrencyCode();
}
if ($storeName && $currencyCode) {
return $storeName . ' [' . $currencyCode . ']';
} else {
return $storeName;
if (!array_key_exists($websiteId, $this->websiteCodesMap)) {
$storeName = ($websiteId == 0)
? ImportAdvancedPricing::VALUE_ALL_WEBSITES
: $this->_storeManager->getWebsite($websiteId)->getCode();
$currencyCode = '';
if ($websiteId == 0) {
$currencyCode = $this->_storeManager->getWebsite($websiteId)
->getBaseCurrencyCode();
}

if ($storeName && $currencyCode) {
$code = $storeName.' ['.$currencyCode.']';
} else {
$code = $storeName;
}
$this->websiteCodesMap[$websiteId] = $code;
}

return $this->websiteCodesMap[$websiteId];
}

/**
* Get Customer Group By Id
*
* @param int $customerGroupId
* @param null $allGroups
* @param int $allGroups
* @return string
*/
protected function _getCustomerGroupById($customerGroupId, $allGroups = null)
{
if ($allGroups) {
protected function _getCustomerGroupById(
int $customerGroupId,
int $allGroups = 0
): string {
if ($allGroups !== 0) {
return ImportAdvancedPricing::VALUE_ALL_GROUPS;
} else {
return $this->_groupRepository->getById($customerGroupId)->getCode();
return $this->_groupRepository
->getById($customerGroupId)
->getCode();
}
}

Expand Down
Loading

0 comments on commit 9b87eb0

Please sign in to comment.