Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit d036fc8

Browse files
author
Joan He
committed
Merge remote-tracking branch 'engcom/libs-upgrade' into fix-calls-count
2 parents d4996eb + 43b0a5f commit d036fc8

File tree

40 files changed

+495
-619
lines changed

40 files changed

+495
-619
lines changed

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

Lines changed: 219 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\AdvancedPricingImportExport\Model\Export;
77

8+
use Magento\ImportExport\Model\Export;
89
use Magento\Store\Model\Store;
910
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
1011
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as ImportAdvancedPricing;
@@ -79,6 +80,11 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
7980
ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
8081
];
8182

83+
/**
84+
* @var string[]
85+
*/
86+
private $websiteCodesMap = [];
87+
8288
/**
8389
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
8490
* @param \Magento\Eav\Model\Config $config
@@ -255,36 +261,131 @@ public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entit
255261
*/
256262
protected function getExportData()
257263
{
264+
if ($this->_passTierPrice) {
265+
return [];
266+
}
267+
258268
$exportData = [];
259269
try {
260-
$rawData = $this->collectRawData();
261-
$productIds = array_keys($rawData);
262-
if (isset($productIds)) {
263-
if (!$this->_passTierPrice) {
264-
$exportData = array_merge(
265-
$exportData,
266-
$this->getTierPrices($productIds, ImportAdvancedPricing::TABLE_TIER_PRICE)
267-
);
270+
$productsByStores = $this->loadCollection();
271+
if (!empty($productsByStores)) {
272+
$linkField = $this->getProductEntityLinkField();
273+
$productLinkIds = [];
274+
275+
foreach ($productsByStores as $product) {
276+
$productLinkIds[array_pop($product)[$linkField]] = true;
277+
}
278+
$productLinkIds = array_keys($productLinkIds);
279+
$tierPricesData = $this->fetchTierPrices($productLinkIds);
280+
$exportData = $this->prepareExportData(
281+
$productsByStores,
282+
$tierPricesData
283+
);
284+
if (!empty($exportData)) {
285+
asort($exportData);
268286
}
269287
}
270-
if ($exportData) {
271-
$exportData = $this->correctExportData($exportData);
272-
}
273-
if (isset($exportData)) {
274-
asort($exportData);
275-
}
276-
} catch (\Exception $e) {
288+
} catch (\Throwable $e) {
277289
$this->_logger->critical($e);
278290
}
291+
279292
return $exportData;
280293
}
281294

295+
/**
296+
* Creating export-formatted row from tier price.
297+
*
298+
* @param array $tierPriceData Tier price information.
299+
*
300+
* @return array Formatted for export tier price information.
301+
*/
302+
private function createExportRow(array $tierPriceData): array
303+
{
304+
//List of columns to display in export row.
305+
$exportRow = $this->templateExportData;
306+
307+
foreach (array_keys($exportRow) as $keyTemplate) {
308+
if (array_key_exists($keyTemplate, $tierPriceData)) {
309+
if (in_array($keyTemplate, $this->_priceWebsite)) {
310+
//If it's website column then getting website code.
311+
$exportRow[$keyTemplate] = $this->_getWebsiteCode(
312+
$tierPriceData[$keyTemplate]
313+
);
314+
} elseif (in_array($keyTemplate, $this->_priceCustomerGroup)) {
315+
//If it's customer group column then getting customer
316+
//group name by ID.
317+
$exportRow[$keyTemplate] = $this->_getCustomerGroupById(
318+
$tierPriceData[$keyTemplate],
319+
$tierPriceData[ImportAdvancedPricing::VALUE_ALL_GROUPS]
320+
);
321+
unset($exportRow[ImportAdvancedPricing::VALUE_ALL_GROUPS]);
322+
} elseif ($keyTemplate
323+
=== ImportAdvancedPricing::COL_TIER_PRICE
324+
) {
325+
//If it's price column then getting value and type
326+
//of tier price.
327+
$exportRow[$keyTemplate]
328+
= $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
329+
? $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
330+
: $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE];
331+
$exportRow[ImportAdvancedPricing::COL_TIER_PRICE_TYPE]
332+
= $this->tierPriceTypeValue($tierPriceData);
333+
} else {
334+
//Any other column just goes as is.
335+
$exportRow[$keyTemplate] = $tierPriceData[$keyTemplate];
336+
}
337+
}
338+
}
339+
340+
return $exportRow;
341+
}
342+
343+
/**
344+
* Prepare data for export.
345+
*
346+
* @param array $productsData Products to export.
347+
* @param array $tierPricesData Their tier prices.
348+
*
349+
* @return array Export rows to display.
350+
*/
351+
private function prepareExportData(
352+
array $productsData,
353+
array $tierPricesData
354+
): array {
355+
//Assigning SKUs to tier prices data.
356+
$productLinkIdToSkuMap = [];
357+
foreach ($productsData as $productData) {
358+
$productLinkIdToSkuMap[$productData[Store::DEFAULT_STORE_ID][$this->getProductEntityLinkField()]]
359+
= $productData[Store::DEFAULT_STORE_ID]['sku'];
360+
}
361+
362+
//Adding products' SKUs to tier price data.
363+
$linkedTierPricesData = [];
364+
foreach ($tierPricesData as $tierPriceData) {
365+
$sku = $productLinkIdToSkuMap[$tierPriceData['product_link_id']];
366+
$linkedTierPricesData[] = array_merge(
367+
$tierPriceData,
368+
[ImportAdvancedPricing::COL_SKU => $sku]
369+
);
370+
}
371+
372+
//Formatting data for export.
373+
$customExportData = [];
374+
foreach ($linkedTierPricesData as $row) {
375+
$customExportData[] = $this->createExportRow($row);
376+
}
377+
378+
return $customExportData;
379+
}
380+
282381
/**
283382
* Correct export data.
284383
*
285384
* @param array $exportData
286385
* @return array
287386
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
387+
* @deprecated
388+
* @see prepareExportData
288389
*/
289390
protected function correctExportData($exportData)
290391
{
@@ -327,16 +428,83 @@ protected function correctExportData($exportData)
327428
/**
328429
* Check type for tier price.
329430
*
330-
* @param string $tierPricePercentage
431+
* @param array $tierPriceData
331432
* @return string
332433
*/
333-
private function tierPriceTypeValue($tierPricePercentage)
434+
private function tierPriceTypeValue(array $tierPriceData): string
334435
{
335-
return $tierPricePercentage
436+
return $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
336437
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337438
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED;
338439
}
339440

441+
/**
442+
* Load tier prices for given products.
443+
*
444+
* @param string[] $productIds Link IDs of products to find tier prices for.
445+
*
446+
* @return array Tier prices data.
447+
*
448+
* @SuppressWarnings(PHPMD.NPathComplexity)
449+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
450+
*/
451+
private function fetchTierPrices(array $productIds): array
452+
{
453+
if (empty($productIds)) {
454+
throw new \InvalidArgumentException(
455+
'Can only load tier prices for specific products'
456+
);
457+
}
458+
459+
$pricesTable = ImportAdvancedPricing::TABLE_TIER_PRICE;
460+
$exportFilter = null;
461+
$priceFromFilter = null;
462+
$priceToFilter = null;
463+
if (isset($this->_parameters[Export::FILTER_ELEMENT_GROUP])) {
464+
$exportFilter = $this->_parameters[Export::FILTER_ELEMENT_GROUP];
465+
}
466+
$productEntityLinkField = $this->getProductEntityLinkField();
467+
$selectFields = [
468+
ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id',
469+
ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups',
470+
ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id',
471+
ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
472+
ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
473+
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
474+
'product_link_id' => 'ap.' .$productEntityLinkField,
475+
];
476+
if ($exportFilter && array_key_exists('tier_price', $exportFilter)) {
477+
if (!empty($exportFilter['tier_price'][0])) {
478+
$priceFromFilter = $exportFilter['tier_price'][0];
479+
}
480+
if (!empty($exportFilter['tier_price'][1])) {
481+
$priceToFilter = $exportFilter['tier_price'][1];
482+
}
483+
}
484+
485+
$select = $this->_connection->select()
486+
->from(
487+
['ap' => $this->_resource->getTableName($pricesTable)],
488+
$selectFields
489+
)
490+
->where(
491+
'ap.'.$productEntityLinkField.' IN (?)',
492+
$productIds
493+
);
494+
495+
if ($priceFromFilter !== null) {
496+
$select->where('ap.value >= ?', $priceFromFilter);
497+
}
498+
if ($priceToFilter !== null) {
499+
$select->where('ap.value <= ?', $priceToFilter);
500+
}
501+
if ($priceFromFilter || $priceToFilter) {
502+
$select->orWhere('ap.percentage_value IS NOT NULL');
503+
}
504+
505+
return $this->_connection->fetchAll($select);
506+
}
507+
340508
/**
341509
* Get tier prices.
342510
*
@@ -345,6 +513,8 @@ private function tierPriceTypeValue($tierPricePercentage)
345513
* @return array|bool
346514
* @SuppressWarnings(PHPMD.NPathComplexity)
347515
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
516+
* @deprecated
517+
* @see fetchTierPrices
348518
*/
349519
protected function getTierPrices(array $listSku, $table)
350520
{
@@ -413,40 +583,51 @@ protected function getTierPrices(array $listSku, $table)
413583
}
414584

415585
/**
416-
* Get Website code
586+
* Get Website code.
417587
*
418588
* @param int $websiteId
589+
*
419590
* @return string
420591
*/
421-
protected function _getWebsiteCode($websiteId)
592+
protected function _getWebsiteCode(int $websiteId): string
422593
{
423-
$storeName = ($websiteId == 0)
424-
? ImportAdvancedPricing::VALUE_ALL_WEBSITES
425-
: $this->_storeManager->getWebsite($websiteId)->getCode();
426-
$currencyCode = '';
427-
if ($websiteId == 0) {
428-
$currencyCode = $this->_storeManager->getWebsite($websiteId)->getBaseCurrencyCode();
429-
}
430-
if ($storeName && $currencyCode) {
431-
return $storeName . ' [' . $currencyCode . ']';
432-
} else {
433-
return $storeName;
594+
if (!array_key_exists($websiteId, $this->websiteCodesMap)) {
595+
$storeName = ($websiteId == 0)
596+
? ImportAdvancedPricing::VALUE_ALL_WEBSITES
597+
: $this->_storeManager->getWebsite($websiteId)->getCode();
598+
$currencyCode = '';
599+
if ($websiteId == 0) {
600+
$currencyCode = $this->_storeManager->getWebsite($websiteId)
601+
->getBaseCurrencyCode();
602+
}
603+
604+
if ($storeName && $currencyCode) {
605+
$code = $storeName.' ['.$currencyCode.']';
606+
} else {
607+
$code = $storeName;
608+
}
609+
$this->websiteCodesMap[$websiteId] = $code;
434610
}
611+
612+
return $this->websiteCodesMap[$websiteId];
435613
}
436614

437615
/**
438-
* Get Customer Group By Id
616+
* Get Customer Group By Id.
617+
*
618+
* @param int $groupId
619+
* @param int $allGroups
439620
*
440-
* @param int $customerGroupId
441-
* @param null $allGroups
442621
* @return string
443622
*/
444-
protected function _getCustomerGroupById($customerGroupId, $allGroups = null)
445-
{
446-
if ($allGroups) {
623+
protected function _getCustomerGroupById(
624+
int $groupId,
625+
int $allGroups = 0
626+
): string {
627+
if ($allGroups !== 0) {
447628
return ImportAdvancedPricing::VALUE_ALL_GROUPS;
448629
} else {
449-
return $this->_groupRepository->getById($customerGroupId)->getCode();
630+
return $this->_groupRepository->getById($groupId)->getCode();
450631
}
451632
}
452633

app/code/Magento/Backend/Block/System/Store/Grid/Render/Group.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function render(\Magento\Framework\DataObject $row)
2727
$this->getUrl('adminhtml/*/editGroup', ['group_id' => $row->getGroupId()]) .
2828
'">' .
2929
$this->escapeHtml($row->getData($this->getColumn()->getIndex())) .
30-
'</a>';
30+
'</a><br />'
31+
. '(' . __('Code') . ': ' . $row->getGroupCode() . ')';
3132
}
3233
}

app/code/Magento/Backend/Block/System/Store/Grid/Render/Store.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function render(\Magento\Framework\DataObject $row)
2727
$this->getUrl('adminhtml/*/editStore', ['store_id' => $row->getStoreId()]) .
2828
'">' .
2929
$this->escapeHtml($row->getData($this->getColumn()->getIndex())) .
30-
'</a>';
30+
'</a><br />' .
31+
'(' . __('Code') . ': ' . $row->getStoreCode() . ')';
3132
}
3233
}

app/code/Magento/Backend/Block/System/Store/Grid/Render/Website.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function render(\Magento\Framework\DataObject $row)
2424
$this->getUrl('adminhtml/*/editWebsite', ['website_id' => $row->getWebsiteId()]) .
2525
'">' .
2626
$this->escapeHtml($row->getData($this->getColumn()->getIndex())) .
27-
'</a>';
27+
'</a><br />' .
28+
'(' . __('Code') . ': ' . $row->getCode() . ')';
2829
}
2930
}

0 commit comments

Comments
 (0)