@@ -79,6 +79,11 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
7979 ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
8080 ];
8181
82+ /**
83+ * @var string[]
84+ */
85+ private $ websiteCodesMap = [];
86+
8287 /**
8388 * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
8489 * @param \Magento\Eav\Model\Config $config
@@ -213,6 +218,7 @@ public function export()
213218 break ;
214219 }
215220 }
221+
216222 return $ writer ->getContents ();
217223 }
218224
@@ -255,70 +261,111 @@ 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+ $ productLinkIds = array_map (
273+ function (array $ productData ) {
274+ return $ productData [Store::DEFAULT_STORE_ID ][$ this ->getProductEntityLinkField ()];
275+ },
276+ $ productsByStores
277+ );
278+ $ tierPricesData = $ this ->getTierPrices (
279+ $ productLinkIds ,
280+ ImportAdvancedPricing::TABLE_TIER_PRICE
281+ );
282+
283+ $ exportData = $ this ->correctExportData (
284+ $ productsByStores ,
285+ $ tierPricesData
286+ );
287+ if (!empty ($ exportData )) {
288+ asort ($ exportData );
268289 }
269290 }
270- if ($ exportData ) {
271- $ exportData = $ this ->correctExportData ($ exportData );
272- }
273- if (isset ($ exportData )) {
274- asort ($ exportData );
275- }
276291 } catch (\Exception $ e ) {
277292 $ this ->_logger ->critical ($ e );
278293 }
294+
279295 return $ exportData ;
280296 }
281297
282298 /**
283- * Correct export data .
299+ * @param array $tierPriceData Tier price information .
284300 *
285- * @param array $exportData
286- * @return array
287- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
301+ * @return array Formatted for export tier price information.
288302 */
289- protected function correctExportData ( $ exportData )
303+ private function createExportRow ( array $ tierPriceData ): array
290304 {
291- $ customExportData = [];
292- foreach ($ exportData as $ key => $ row ) {
293- $ exportRow = $ this ->templateExportData ;
294- foreach ($ exportRow as $ keyTemplate => $ valueTemplate ) {
295- if (isset ($ row [$ keyTemplate ])) {
296- if (in_array ($ keyTemplate , $ this ->_priceWebsite )) {
297- $ exportRow [$ keyTemplate ] = $ this ->_getWebsiteCode (
298- $ row [$ keyTemplate ]
299- );
300- } elseif (in_array ($ keyTemplate , $ this ->_priceCustomerGroup )) {
301- $ exportRow [$ keyTemplate ] = $ this ->_getCustomerGroupById (
302- $ row [$ keyTemplate ],
303- isset ($ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ])
304- ? $ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ]
305- : null
306- );
307- unset($ exportRow [ImportAdvancedPricing::VALUE_ALL_GROUPS ]);
308- } elseif ($ keyTemplate === ImportAdvancedPricing::COL_TIER_PRICE ) {
309- $ exportRow [$ keyTemplate ] = $ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
310- ? $ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
311- : $ row [ImportAdvancedPricing::COL_TIER_PRICE ];
312- $ exportRow [ImportAdvancedPricing::COL_TIER_PRICE_TYPE ]
313- = $ this ->tierPriceTypeValue ($ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]);
314- } else {
315- $ exportRow [$ keyTemplate ] = $ row [$ keyTemplate ];
316- }
305+ $ exportRow = $ this ->templateExportData ;
306+ foreach (array_keys ($ exportRow ) as $ keyTemplate ) {
307+ if (array_key_exists ($ keyTemplate , $ tierPriceData )) {
308+ if (in_array ($ keyTemplate , $ this ->_priceWebsite )) {
309+ $ exportRow [$ keyTemplate ] = $ this ->_getWebsiteCode (
310+ $ tierPriceData [$ keyTemplate ]
311+ );
312+ } elseif (in_array ($ keyTemplate , $ this ->_priceCustomerGroup )) {
313+ $ exportRow [$ keyTemplate ] = $ this ->_getCustomerGroupById (
314+ $ tierPriceData [$ keyTemplate ],
315+ $ tierPriceData [ImportAdvancedPricing::VALUE_ALL_GROUPS ]
316+ );
317+ unset($ exportRow [ImportAdvancedPricing::VALUE_ALL_GROUPS ]);
318+ } elseif ($ keyTemplate
319+ === ImportAdvancedPricing::COL_TIER_PRICE
320+ ) {
321+ $ exportRow [$ keyTemplate ]
322+ = $ tierPriceData [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
323+ ? $ tierPriceData [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
324+ : $ tierPriceData [ImportAdvancedPricing::COL_TIER_PRICE ];
325+ $ exportRow [ImportAdvancedPricing::COL_TIER_PRICE_TYPE ]
326+ = $ this ->tierPriceTypeValue ($ tierPriceData );
327+ } else {
328+ $ exportRow [$ keyTemplate ] = $ tierPriceData [$ keyTemplate ];
317329 }
318330 }
331+ }
332+
333+ return $ exportRow ;
334+ }
319335
320- $ customExportData [$ key ] = $ exportRow ;
321- unset($ exportRow );
336+ /**
337+ * Correct export data.
338+ *
339+ * @param array $productsData
340+ * @param array $tierPricesData
341+ *
342+ * @return array
343+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
344+ */
345+ protected function correctExportData (
346+ array $ productsData ,
347+ array $ tierPricesData
348+ ): array {
349+ //Assigning SKUs to tier prices data.
350+ $ productLinkIdToSkuMap = [];
351+ foreach ($ productsData as $ productData ) {
352+ $ productLinkIdToSkuMap [$ productData [Store::DEFAULT_STORE_ID ][$ this ->getProductEntityLinkField ()]]
353+ = $ productData [Store::DEFAULT_STORE_ID ]['sku ' ];
354+ }
355+ unset($ productData );
356+ $ linkedTierPricesData = [];
357+ foreach ($ tierPricesData as $ tierPriceData ) {
358+ $ sku = $ productLinkIdToSkuMap [$ tierPriceData ['product_link_id ' ]];
359+ $ linkedTierPricesData [] = array_merge (
360+ $ tierPriceData ,
361+ [ImportAdvancedPricing::COL_SKU => $ sku ]
362+ );
363+ }
364+ unset($ sku , $ tierPriceData );
365+
366+ $ customExportData = [];
367+ foreach ($ linkedTierPricesData as $ row ) {
368+ $ customExportData [] = $ this ->createExportRow ($ row );
322369 }
323370
324371 return $ customExportData ;
@@ -327,67 +374,66 @@ protected function correctExportData($exportData)
327374 /**
328375 * Check type for tier price.
329376 *
330- * @param string $tierPricePercentage
377+ * @param array $tierPriceData
378+ *
331379 * @return string
332380 */
333- private function tierPriceTypeValue ($ tierPricePercentage )
381+ private function tierPriceTypeValue (array $ tierPriceData ): string
334382 {
335- return $ tierPricePercentage
383+ return $ tierPriceData [ImportAdvancedPricing:: COL_TIER_PRICE_PERCENTAGE_VALUE ]
336384 ? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337385 : ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED ;
338386 }
339387
340388 /**
341389 * Get tier prices.
342390 *
343- * @param array $listSku
391+ * @param string[] $productLinksIds
344392 * @param string $table
345393 * @return array|bool
346394 * @SuppressWarnings(PHPMD.NPathComplexity)
347395 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
348396 */
349- protected function getTierPrices (array $ listSku , $ table )
397+ protected function getTierPrices (array $ productLinksIds , $ table )
350398 {
399+ $ exportFilter = null ;
400+ $ price = null ;
351401 if (isset ($ this ->_parameters [\Magento \ImportExport \Model \Export::FILTER_ELEMENT_GROUP ])) {
352402 $ exportFilter = $ this ->_parameters [\Magento \ImportExport \Model \Export::FILTER_ELEMENT_GROUP ];
353403 }
404+ $ productEntityLinkField = $ this ->getProductEntityLinkField ();
405+
354406 if ($ table == ImportAdvancedPricing::TABLE_TIER_PRICE ) {
355407 $ selectFields = [
356- ImportAdvancedPricing::COL_SKU => 'cpe.sku ' ,
357- ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id ' ,
358- ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups ' ,
359- ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id ' ,
360- ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty ' ,
361- ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value ' ,
408+ ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id ' ,
409+ ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups ' ,
410+ ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id ' ,
411+ ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty ' ,
412+ ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value ' ,
362413 ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value ' ,
414+ 'product_link_id ' => 'ap. '
415+ .$ productEntityLinkField ,
363416 ];
364- if (isset ($ exportFilter ) && !empty ($ exportFilter )) {
365- $ price = $ exportFilter ['tier_price ' ];
366- }
367- }
368- if ($ listSku ) {
369- if (isset ($ exportFilter ) && !empty ($ exportFilter )) {
370- $ date = $ exportFilter [\Magento \Catalog \Model \Category::KEY_UPDATED_AT ];
371- if (isset ($ date [0 ]) && !empty ($ date [0 ])) {
372- $ updatedAtFrom = $ this ->_localeDate ->date ($ date [0 ], null , false )->format ('Y-m-d H:i:s ' );
373- }
374- if (isset ($ date [1 ]) && !empty ($ date [1 ])) {
375- $ updatedAtTo = $ this ->_localeDate ->date ($ date [1 ], null , false )->format ('Y-m-d H:i:s ' );
417+ if ($ exportFilter ) {
418+ if (array_key_exists ('tier_price ' , $ exportFilter )) {
419+ $ price = $ exportFilter ['tier_price ' ];
376420 }
377421 }
422+ } else {
423+ throw new \InvalidArgumentException ('Proper table name needed ' );
424+ }
425+
426+ if ($ productLinksIds ) {
378427 try {
379- $ productEntityLinkField = $ this ->getProductEntityLinkField ();
380428 $ select = $ this ->_connection ->select ()
381429 ->from (
382- ['cpe ' => $ this ->_resource ->getTableName ('catalog_product_entity ' )],
383- $ selectFields
384- )
385- ->joinInner (
386430 ['ap ' => $ this ->_resource ->getTableName ($ table )],
387- 'ap. ' . $ productEntityLinkField . ' = cpe. ' . $ productEntityLinkField ,
388- []
431+ $ selectFields
389432 )
390- ->where ('cpe.entity_id IN (?) ' , $ listSku );
433+ ->where (
434+ 'ap. ' .$ productEntityLinkField .' IN (?) ' ,
435+ $ productLinksIds
436+ );
391437
392438 if (isset ($ price [0 ]) && !empty ($ price [0 ])) {
393439 $ select ->where ('ap.value >= ? ' , $ price [0 ]);
@@ -398,18 +444,16 @@ protected function getTierPrices(array $listSku, $table)
398444 if (isset ($ price [0 ]) && !empty ($ price [0 ]) || isset ($ price [1 ]) && !empty ($ price [1 ])) {
399445 $ select ->orWhere ('ap.percentage_value IS NOT NULL ' );
400446 }
401- if (isset ($ updatedAtFrom ) && !empty ($ updatedAtFrom )) {
402- $ select ->where ('cpe.updated_at >= ? ' , $ updatedAtFrom );
403- }
404- if (isset ($ updatedAtTo ) && !empty ($ updatedAtTo )) {
405- $ select ->where ('cpe.updated_at <= ? ' , $ updatedAtTo );
406- }
447+
407448 $ exportData = $ this ->_connection ->fetchAll ($ select );
408449 } catch (\Exception $ e ) {
409450 return false ;
410451 }
452+
453+ return $ exportData ;
454+ } else {
455+ return false ;
411456 }
412- return $ exportData ;
413457 }
414458
415459 /**
@@ -418,35 +462,46 @@ protected function getTierPrices(array $listSku, $table)
418462 * @param int $websiteId
419463 * @return string
420464 */
421- protected function _getWebsiteCode ($ websiteId )
465+ protected function _getWebsiteCode (int $ websiteId ): string
422466 {
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 ;
467+ if (!array_key_exists ($ websiteId , $ this ->websiteCodesMap )) {
468+ $ storeName = ($ websiteId == 0 )
469+ ? ImportAdvancedPricing::VALUE_ALL_WEBSITES
470+ : $ this ->_storeManager ->getWebsite ($ websiteId )->getCode ();
471+ $ currencyCode = '' ;
472+ if ($ websiteId == 0 ) {
473+ $ currencyCode = $ this ->_storeManager ->getWebsite ($ websiteId )
474+ ->getBaseCurrencyCode ();
475+ }
476+
477+ if ($ storeName && $ currencyCode ) {
478+ $ code = $ storeName .' [ ' .$ currencyCode .'] ' ;
479+ } else {
480+ $ code = $ storeName ;
481+ }
482+ $ this ->websiteCodesMap [$ websiteId ] = $ code ;
434483 }
484+
485+ return $ this ->websiteCodesMap [$ websiteId ];
435486 }
436487
437488 /**
438489 * Get Customer Group By Id
439490 *
440491 * @param int $customerGroupId
441- * @param null $allGroups
492+ * @param int $allGroups
442493 * @return string
443494 */
444- protected function _getCustomerGroupById ($ customerGroupId , $ allGroups = null )
445- {
446- if ($ allGroups ) {
495+ protected function _getCustomerGroupById (
496+ int $ customerGroupId ,
497+ int $ allGroups = 0
498+ ): string {
499+ if ($ allGroups !== 0 ) {
447500 return ImportAdvancedPricing::VALUE_ALL_GROUPS ;
448501 } else {
449- return $ this ->_groupRepository ->getById ($ customerGroupId )->getCode ();
502+ return $ this ->_groupRepository
503+ ->getById ($ customerGroupId )
504+ ->getCode ();
450505 }
451506 }
452507
0 commit comments