Skip to content

Commit 762fbd4

Browse files
author
Rykh Oleksandr
committed
2 parents 104b5f8 + 78ef991 commit 762fbd4

File tree

25 files changed

+1223
-72
lines changed

25 files changed

+1223
-72
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
7676
ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => '',
7777
ImportAdvancedPricing::COL_TIER_PRICE_QTY => '',
7878
ImportAdvancedPricing::COL_TIER_PRICE => '',
79+
ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
7980
];
8081

8182
/**
@@ -279,6 +280,8 @@ protected function getExportData()
279280
}
280281

281282
/**
283+
* Correct export data.
284+
*
282285
* @param array $exportData
283286
* @return array
284287
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
@@ -302,6 +305,12 @@ protected function correctExportData($exportData)
302305
: null
303306
);
304307
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]);
305314
} else {
306315
$exportRow[$keyTemplate] = $row[$keyTemplate];
307316
}
@@ -311,11 +320,25 @@ protected function correctExportData($exportData)
311320
$customExportData[$key] = $exportRow;
312321
unset($exportRow);
313322
}
323+
314324
return $customExportData;
315325
}
316326

317327
/**
318-
* Get Tier and Group Pricing
328+
* Check type for tier price.
329+
*
330+
* @param string $tierPricePercentage
331+
* @return string
332+
*/
333+
private function tierPriceTypeValue($tierPricePercentage)
334+
{
335+
return $tierPricePercentage
336+
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337+
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED;
338+
}
339+
340+
/**
341+
* Get tier prices.
319342
*
320343
* @param array $listSku
321344
* @param string $table
@@ -336,6 +359,7 @@ protected function getTierPrices(array $listSku, $table)
336359
ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id',
337360
ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty',
338361
ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value',
362+
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
339363
];
340364
if (isset($exportFilter) && !empty($exportFilter)) {
341365
$price = $exportFilter['tier_price'];
@@ -371,6 +395,9 @@ protected function getTierPrices(array $listSku, $table)
371395
if (isset($price[1]) && !empty($price[1])) {
372396
$select->where('ap.value <= ?', $price[1]);
373397
}
398+
if (isset($price[0]) && !empty($price[0]) || isset($price[1]) && !empty($price[1])) {
399+
$select->orWhere('ap.percentage_value IS NOT NULL');
400+
}
374401
if (isset($updatedAtFrom) && !empty($updatedAtFrom)) {
375402
$select->where('cpe.updated_at >= ?', $updatedAtFrom);
376403
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
3333

3434
const COL_TIER_PRICE = 'tier_price';
3535

36+
const COL_TIER_PRICE_PERCENTAGE_VALUE = 'percentage_value';
37+
38+
const COL_TIER_PRICE_TYPE = 'tier_price_value_type';
39+
40+
const TIER_PRICE_TYPE_FIXED = 'Fixed';
41+
42+
const TIER_PRICE_TYPE_PERCENT = 'Discount';
43+
3644
const TABLE_TIER_PRICE = 'catalog_product_entity_tier_price';
3745

3846
const DEFAULT_ALL_GROUPS_GROUPED_PRICE_VALUE = '0';
@@ -46,7 +54,7 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
4654
const VALIDATOR_TEAR_PRICE = 'validator_tear_price';
4755

4856
/**
49-
* Validation failure message template definitions
57+
* Validation failure message template definitions.
5058
*
5159
* @var array
5260
*/
@@ -57,6 +65,8 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
5765
ValidatorInterface::ERROR_INVALID_TIER_PRICE_QTY => 'Tier Price data price or quantity value is invalid',
5866
ValidatorInterface::ERROR_INVALID_TIER_PRICE_SITE => 'Tier Price data website is invalid',
5967
ValidatorInterface::ERROR_INVALID_TIER_PRICE_GROUP => 'Tier Price customer group is invalid',
68+
ValidatorInterface::ERROR_INVALID_TIER_PRICE_TYPE => 'Value for \'tier_price_value_type\' ' .
69+
'attribute contains incorrect value, acceptable values are Fixed, Discount',
6070
ValidatorInterface::ERROR_TIER_DATA_INCOMPLETE => 'Tier Price data is incomplete',
6171
ValidatorInterface::ERROR_INVALID_ATTRIBUTE_DECIMAL =>
6272
'Value for \'%s\' attribute contains incorrect value, acceptable values are in decimal format',
@@ -70,7 +80,7 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
7080
protected $needColumnCheck = true;
7181

7282
/**
73-
* Valid column names
83+
* Valid column names.
7484
*
7585
* @array
7686
*/
@@ -80,6 +90,7 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
8090
self::COL_TIER_PRICE_CUSTOMER_GROUP,
8191
self::COL_TIER_PRICE_QTY,
8292
self::COL_TIER_PRICE,
93+
self::COL_TIER_PRICE_TYPE
8394
];
8495

8596
/**
@@ -379,7 +390,10 @@ protected function saveAndReplaceAdvancedPrices()
379390
$rowData[self::COL_TIER_PRICE_CUSTOMER_GROUP]
380391
),
381392
'qty' => $rowData[self::COL_TIER_PRICE_QTY],
382-
'value' => $rowData[self::COL_TIER_PRICE],
393+
'value' => $rowData[self::COL_TIER_PRICE_TYPE] === self::TIER_PRICE_TYPE_FIXED
394+
? $rowData[self::COL_TIER_PRICE] : 0,
395+
'percentage_value' => $rowData[self::COL_TIER_PRICE_TYPE] === self::TIER_PRICE_TYPE_PERCENT
396+
? $rowData[self::COL_TIER_PRICE] : null,
383397
'website_id' => $this->getWebsiteId($rowData[self::COL_TIER_PRICE_WEBSITE])
384398
];
385399
}
@@ -429,7 +443,7 @@ protected function saveProductPrices(array $priceData, $table)
429443
}
430444
}
431445
if ($priceIn) {
432-
$this->_connection->insertOnDuplicate($tableName, $priceIn, ['value']);
446+
$this->_connection->insertOnDuplicate($tableName, $priceIn, ['value', 'percentage_value']);
433447
}
434448
}
435449
return $this;

app/code/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricing/Validator/TierPrice.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class TierPrice extends \Magento\CatalogImportExport\Model\Import\Product\Valida
2222
AdvancedPricing::COL_TIER_PRICE_WEBSITE,
2323
AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP,
2424
AdvancedPricing::COL_TIER_PRICE_QTY,
25-
AdvancedPricing::COL_TIER_PRICE
25+
AdvancedPricing::COL_TIER_PRICE,
26+
AdvancedPricing::COL_TIER_PRICE_TYPE
2627
];
2728

2829
/**
@@ -101,6 +102,7 @@ public function isValid($value)
101102
|| !isset($value[AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP])
102103
|| !isset($value[AdvancedPricing::COL_TIER_PRICE_QTY])
103104
|| !isset($value[AdvancedPricing::COL_TIER_PRICE])
105+
|| !isset($value[AdvancedPricing::COL_TIER_PRICE_TYPE])
104106
|| $this->hasEmptyColumns($value)
105107
) {
106108
$this->_addMessages([self::ERROR_TIER_DATA_INCOMPLETE]);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing\Validator;
8+
9+
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing;
10+
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
11+
12+
/**
13+
* Class TierPriceType validates tier price type.
14+
*/
15+
class TierPriceType extends \Magento\CatalogImportExport\Model\Import\Product\Validator\AbstractImportValidator
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function init($context)
21+
{
22+
return parent::init($context);
23+
}
24+
25+
/**
26+
* Validate tier price type.
27+
*
28+
* @param array $value
29+
* @return bool
30+
*/
31+
public function isValid($value)
32+
{
33+
$isValid = true;
34+
35+
if (isset($value[AdvancedPricing::COL_TIER_PRICE_TYPE])
36+
&& !empty($value[AdvancedPricing::COL_TIER_PRICE_TYPE])
37+
&& !in_array(
38+
$value[AdvancedPricing::COL_TIER_PRICE_TYPE],
39+
[AdvancedPricing::TIER_PRICE_TYPE_FIXED, AdvancedPricing::TIER_PRICE_TYPE_PERCENT]
40+
)
41+
) {
42+
$this->_addMessages([RowValidatorInterface::ERROR_INVALID_TIER_PRICE_TYPE]);
43+
$isValid = false;
44+
}
45+
46+
return $isValid;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\AdvancedPricingImportExport\Test\Unit\Model\Import\AdvancedPricing\Validator;
8+
9+
use \Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as AdvancedPricing;
10+
11+
/**
12+
* Class TierPriceTypeTest.
13+
*/
14+
class TierPriceTypeTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var AdvancedPricing\Validator\TierPriceType
18+
*/
19+
private $tierPriceType;
20+
21+
/**
22+
* Set up.
23+
*
24+
* @return void
25+
*/
26+
protected function setUp()
27+
{
28+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
29+
$this->tierPriceType = $objectManager->getObject(
30+
AdvancedPricing\Validator\TierPriceType::class,
31+
[]
32+
);
33+
}
34+
35+
/**
36+
* Test for isValid() method.
37+
*
38+
* @dataProvider isValidDataProvider
39+
* @param array $value
40+
* @param bool $expectedResult
41+
*/
42+
public function testIsValid(array $value, $expectedResult)
43+
{
44+
$result = $this->tierPriceType->isValid($value);
45+
$this->assertEquals($expectedResult, $result);
46+
}
47+
48+
/**
49+
* Data Provider for testIsValid().
50+
*
51+
* @return array
52+
*/
53+
public function isValidDataProvider()
54+
{
55+
return [
56+
[
57+
[AdvancedPricing::COL_TIER_PRICE_TYPE => AdvancedPricing::TIER_PRICE_TYPE_FIXED],
58+
true
59+
],
60+
[
61+
[AdvancedPricing::COL_TIER_PRICE_TYPE => AdvancedPricing::TIER_PRICE_TYPE_PERCENT],
62+
true
63+
],
64+
[
65+
[],
66+
true
67+
],
68+
[
69+
[AdvancedPricing::COL_TIER_PRICE_TYPE => null],
70+
true
71+
],
72+
[
73+
[AdvancedPricing::COL_TIER_PRICE_TYPE => 'wrong type'],
74+
false
75+
]
76+
];
77+
}
78+
}

0 commit comments

Comments
 (0)