5
5
*/
6
6
namespace Magento \AdvancedPricingImportExport \Model \Export ;
7
7
8
+ use Magento \ImportExport \Model \Export ;
8
9
use Magento \Store \Model \Store ;
9
10
use Magento \CatalogImportExport \Model \Import \Product as ImportProduct ;
10
11
use Magento \AdvancedPricingImportExport \Model \Import \AdvancedPricing as ImportAdvancedPricing ;
@@ -79,6 +80,11 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
79
80
ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
80
81
];
81
82
83
+ /**
84
+ * @var string[]
85
+ */
86
+ private $ websiteCodesMap = [];
87
+
82
88
/**
83
89
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
84
90
* @param \Magento\Eav\Model\Config $config
@@ -255,36 +261,131 @@ public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entit
255
261
*/
256
262
protected function getExportData ()
257
263
{
264
+ if ($ this ->_passTierPrice ) {
265
+ return [];
266
+ }
267
+
258
268
$ exportData = [];
259
269
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 );
268
286
}
269
287
}
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 ) {
277
289
$ this ->_logger ->critical ($ e );
278
290
}
291
+
279
292
return $ exportData ;
280
293
}
281
294
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
+
282
381
/**
283
382
* Correct export data.
284
383
*
285
384
* @param array $exportData
286
385
* @return array
287
386
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
387
+ * @deprecated
388
+ * @see prepareExportData
288
389
*/
289
390
protected function correctExportData ($ exportData )
290
391
{
@@ -327,16 +428,83 @@ protected function correctExportData($exportData)
327
428
/**
328
429
* Check type for tier price.
329
430
*
330
- * @param string $tierPricePercentage
431
+ * @param array $tierPriceData
331
432
* @return string
332
433
*/
333
- private function tierPriceTypeValue ($ tierPricePercentage )
434
+ private function tierPriceTypeValue (array $ tierPriceData ): string
334
435
{
335
- return $ tierPricePercentage
436
+ return $ tierPriceData [ImportAdvancedPricing:: COL_TIER_PRICE_PERCENTAGE_VALUE ]
336
437
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337
438
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED ;
338
439
}
339
440
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
+
340
508
/**
341
509
* Get tier prices.
342
510
*
@@ -345,6 +513,8 @@ private function tierPriceTypeValue($tierPricePercentage)
345
513
* @return array|bool
346
514
* @SuppressWarnings(PHPMD.NPathComplexity)
347
515
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
516
+ * @deprecated
517
+ * @see fetchTierPrices
348
518
*/
349
519
protected function getTierPrices (array $ listSku , $ table )
350
520
{
@@ -413,40 +583,51 @@ protected function getTierPrices(array $listSku, $table)
413
583
}
414
584
415
585
/**
416
- * Get Website code
586
+ * Get Website code.
417
587
*
418
588
* @param int $websiteId
589
+ *
419
590
* @return string
420
591
*/
421
- protected function _getWebsiteCode ($ websiteId )
592
+ protected function _getWebsiteCode (int $ websiteId ): string
422
593
{
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 ;
434
610
}
611
+
612
+ return $ this ->websiteCodesMap [$ websiteId ];
435
613
}
436
614
437
615
/**
438
- * Get Customer Group By Id
616
+ * Get Customer Group By Id.
617
+ *
618
+ * @param int $groupId
619
+ * @param int $allGroups
439
620
*
440
- * @param int $customerGroupId
441
- * @param null $allGroups
442
621
* @return string
443
622
*/
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 ) {
447
628
return ImportAdvancedPricing::VALUE_ALL_GROUPS ;
448
629
} else {
449
- return $ this ->_groupRepository ->getById ($ customerGroupId )->getCode ();
630
+ return $ this ->_groupRepository ->getById ($ groupId )->getCode ();
450
631
}
451
632
}
452
633
0 commit comments