Skip to content

Commit 6f0e5aa

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-91105' into 2.2-develop-pr49
2 parents dde109d + e91010a commit 6f0e5aa

File tree

5 files changed

+173
-45
lines changed

5 files changed

+173
-45
lines changed

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/CSV/RowParser.php

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Framework\Phrase;
1010
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\LocationDirectory;
1111

12+
/**
13+
* Row parser.
14+
*/
1215
class RowParser
1316
{
1417
/**
@@ -26,6 +29,8 @@ public function __construct(LocationDirectory $locationDirectory)
2629
}
2730

2831
/**
32+
* Retrieve columns.
33+
*
2934
* @return array
3035
*/
3136
public function getColumns()
@@ -42,6 +47,8 @@ public function getColumns()
4247
}
4348

4449
/**
50+
* Parse provided row data.
51+
*
4552
* @param array $rowData
4653
* @param int $rowNumber
4754
* @param int $websiteId
@@ -62,27 +69,39 @@ public function parse(
6269
) {
6370
// validate row
6471
if (count($rowData) < 5) {
65-
throw new RowException(__('Please correct Table Rates format in the Row #%1.', $rowNumber));
72+
throw new RowException(
73+
__(
74+
'The Table Rates File Format is incorrect in row number "%1". Verify the format and try again.',
75+
$rowNumber
76+
)
77+
);
6678
}
6779

6880
$countryId = $this->getCountryId($rowData, $rowNumber, $columnResolver);
69-
$regionId = $this->getRegionId($rowData, $rowNumber, $columnResolver, $countryId);
81+
$regionIds = $this->getRegionIds($rowData, $rowNumber, $columnResolver, $countryId);
7082
$zipCode = $this->getZipCode($rowData, $columnResolver);
7183
$conditionValue = $this->getConditionValue($rowData, $rowNumber, $conditionFullName, $columnResolver);
7284
$price = $this->getPrice($rowData, $rowNumber, $columnResolver);
7385

74-
return [
75-
'website_id' => $websiteId,
76-
'dest_country_id' => $countryId,
77-
'dest_region_id' => $regionId,
78-
'dest_zip' => $zipCode,
79-
'condition_name' => $conditionShortName,
80-
'condition_value' => $conditionValue,
81-
'price' => $price,
82-
];
86+
$rates = [];
87+
foreach ($regionIds as $regionId) {
88+
$rates[] = [
89+
'website_id' => $websiteId,
90+
'dest_country_id' => $countryId,
91+
'dest_region_id' => $regionId,
92+
'dest_zip' => $zipCode,
93+
'condition_name' => $conditionShortName,
94+
'condition_value' => $conditionValue,
95+
'price' => $price,
96+
];
97+
}
98+
99+
return $rates;
83100
}
84101

85102
/**
103+
* Get country id from provided row data.
104+
*
86105
* @param array $rowData
87106
* @param int $rowNumber
88107
* @param ColumnResolver $columnResolver
@@ -99,34 +118,53 @@ private function getCountryId(array $rowData, $rowNumber, ColumnResolver $column
99118
} elseif ($countryCode === '*' || $countryCode === '') {
100119
$countryId = '0';
101120
} else {
102-
throw new RowException(__('Please correct Country "%1" in the Row #%2.', $countryCode, $rowNumber));
121+
throw new RowException(
122+
__(
123+
'The "%1" country in row number "%2" is incorrect. Verify the country and try again.',
124+
$countryCode,
125+
$rowNumber
126+
)
127+
);
103128
}
129+
104130
return $countryId;
105131
}
106132

107133
/**
134+
* Retrieve region id from provided row data.
135+
*
108136
* @param array $rowData
109137
* @param int $rowNumber
110138
* @param ColumnResolver $columnResolver
111139
* @param int $countryId
112-
* @return int|string
140+
* @return array
113141
* @throws ColumnNotFoundException
114142
* @throws RowException
115143
*/
116-
private function getRegionId(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId)
144+
private function getRegionIds(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId): array
117145
{
118146
$regionCode = $columnResolver->getColumnValue(ColumnResolver::COLUMN_REGION, $rowData);
119147
if ($countryId !== '0' && $this->locationDirectory->hasRegionId($countryId, $regionCode)) {
120-
$regionId = $this->locationDirectory->getRegionId($countryId, $regionCode);
148+
$regionIds = $this->locationDirectory->getRegionIds($countryId, $regionCode);
121149
} elseif ($regionCode === '*' || $regionCode === '') {
122-
$regionId = 0;
150+
$regionIds = [0];
123151
} else {
124-
throw new RowException(__('Please correct Region/State "%1" in the Row #%2.', $regionCode, $rowNumber));
152+
throw new RowException(
153+
__(
154+
'The "%1" region or state in row number "%2" is incorrect. '
155+
. 'Verify the region or state and try again.',
156+
$regionCode,
157+
$rowNumber
158+
)
159+
);
125160
}
126-
return $regionId;
161+
162+
return $regionIds;
127163
}
128164

129165
/**
166+
* Retrieve zip code from provided row data.
167+
*
130168
* @param array $rowData
131169
* @param ColumnResolver $columnResolver
132170
* @return float|int|null|string
@@ -138,10 +176,13 @@ private function getZipCode(array $rowData, ColumnResolver $columnResolver)
138176
if ($zipCode === '') {
139177
$zipCode = '*';
140178
}
179+
141180
return $zipCode;
142181
}
143182

144183
/**
184+
* Get condition value form provided row data.
185+
*
145186
* @param array $rowData
146187
* @param int $rowNumber
147188
* @param string $conditionFullName
@@ -165,10 +206,13 @@ private function getConditionValue(array $rowData, $rowNumber, $conditionFullNam
165206
)
166207
);
167208
}
209+
168210
return $value;
169211
}
170212

171213
/**
214+
* Retrieve price from provided row data.
215+
*
172216
* @param array $rowData
173217
* @param int $rowNumber
174218
* @param ColumnResolver $columnResolver
@@ -181,13 +225,21 @@ private function getPrice(array $rowData, $rowNumber, ColumnResolver $columnReso
181225
$priceValue = $columnResolver->getColumnValue(ColumnResolver::COLUMN_PRICE, $rowData);
182226
$price = $this->_parseDecimalValue($priceValue);
183227
if ($price === false) {
184-
throw new RowException(__('Please correct Shipping Price "%1" in the Row #%2.', $priceValue, $rowNumber));
228+
throw new RowException(
229+
__(
230+
'The "%1" shipping price in row number "%2" is incorrect. Verify the shipping price and try again.',
231+
$priceValue,
232+
$rowNumber
233+
)
234+
);
185235
}
236+
186237
return $price;
187238
}
188239

189240
/**
190241
* Parse and validate positive decimal value
242+
*
191243
* Return false if value is not decimal or is not positive
192244
*
193245
* @param string $value
@@ -202,6 +254,7 @@ private function _parseDecimalValue($value)
202254
$result = $value;
203255
}
204256
}
257+
205258
return $result;
206259
}
207260
}

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/Import.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\CSV\RowParser;
1717
use Magento\Store\Model\StoreManagerInterface;
1818

19+
/**
20+
* Import offline shipping.
21+
*
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
*/
1924
class Import
2025
{
2126
/**
@@ -84,6 +89,8 @@ public function __construct(
8489
}
8590

8691
/**
92+
* Check if there are errors.
93+
*
8794
* @return bool
8895
*/
8996
public function hasErrors()
@@ -92,6 +99,8 @@ public function hasErrors()
9299
}
93100

94101
/**
102+
* Get errors.
103+
*
95104
* @return array
96105
*/
97106
public function getErrors()
@@ -100,6 +109,8 @@ public function getErrors()
100109
}
101110

102111
/**
112+
* Retrieve columns.
113+
*
103114
* @return array
104115
*/
105116
public function getColumns()
@@ -108,6 +119,8 @@ public function getColumns()
108119
}
109120

110121
/**
122+
* Get data from file.
123+
*
111124
* @param ReadInterface $file
112125
* @param int $websiteId
113126
* @param string $conditionShortName
@@ -132,7 +145,7 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
132145
if (empty($csvLine)) {
133146
continue;
134147
}
135-
$rowData = $this->rowParser->parse(
148+
$rowsData = $this->rowParser->parse(
136149
$csvLine,
137150
$rowNumber,
138151
$websiteId,
@@ -141,20 +154,25 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
141154
$columnResolver
142155
);
143156

144-
// protect from duplicate
145-
$hash = $this->dataHashGenerator->getHash($rowData);
146-
if (array_key_exists($hash, $this->uniqueHash)) {
147-
throw new RowException(
148-
__(
149-
'Duplicate Row #%1 (duplicates row #%2)',
150-
$rowNumber,
151-
$this->uniqueHash[$hash]
152-
)
153-
);
157+
foreach ($rowsData as $rowData) {
158+
// protect from duplicate
159+
$hash = $this->dataHashGenerator->getHash($rowData);
160+
if (array_key_exists($hash, $this->uniqueHash)) {
161+
throw new RowException(
162+
__(
163+
'Duplicate Row #%1 (duplicates row #%2)',
164+
$rowNumber,
165+
$this->uniqueHash[$hash]
166+
)
167+
);
168+
}
169+
$this->uniqueHash[$hash] = $rowNumber;
170+
171+
$items[] = $rowData;
172+
}
173+
if (count($rowsData) > 1) {
174+
$bunchSize += count($rowsData) - 1;
154175
}
155-
$this->uniqueHash[$hash] = $rowNumber;
156-
157-
$items[] = $rowData;
158176
if (count($items) === $bunchSize) {
159177
yield $items;
160178
$items = [];
@@ -169,6 +187,8 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
169187
}
170188

171189
/**
190+
* Retrieve column headers.
191+
*
172192
* @param ReadInterface $file
173193
* @return array|bool
174194
* @throws LocalizedException
@@ -178,8 +198,11 @@ private function getHeaders(ReadInterface $file)
178198
// check and skip headers
179199
$headers = $file->readCsv();
180200
if ($headers === false || count($headers) < 5) {
181-
throw new LocalizedException(__('Please correct Table Rates File Format.'));
201+
throw new LocalizedException(
202+
__('The Table Rates File Format is incorrect. Verify the format and try again.')
203+
);
182204
}
205+
183206
return $headers;
184207
}
185208
}

0 commit comments

Comments
 (0)