Skip to content

Commit ec7d56d

Browse files
committed
ACP2E-2129: customer is reporting their table rates are not updating via CSV any longer
- addressed static report
1 parent 0f6b12c commit ec7d56d

File tree

3 files changed

+86
-48
lines changed

3 files changed

+86
-48
lines changed

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

Lines changed: 83 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
use Magento\Framework\App\DeploymentConfig;
1818
use Magento\Framework\App\ObjectManager;
1919
use Magento\Framework\App\RequestFactory;
20+
use Magento\Framework\DataObject;
2021
use Magento\Framework\Exception\FileSystemException;
22+
use Magento\Framework\Exception\LocalizedException;
2123
use Magento\Framework\Filesystem;
2224
use Magento\Framework\Model\ResourceModel\Db\Context;
2325
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
2426
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQuery;
2527
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
2628
use Magento\Store\Model\StoreManagerInterface;
2729
use Psr\Log\LoggerInterface;
30+
use Magento\Framework\Filesystem\Io\File as IoFile;
2831

2932
/**
3033
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -118,7 +121,7 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
118121
protected $storeManager;
119122

120123
/**
121-
* @var \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
124+
* @var Tablerate
122125
* @since 100.1.0
123126
*/
124127
protected $carrierTablerate;
@@ -151,6 +154,11 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
151154
*/
152155
private RequestFactory $requestFactory;
153156

157+
/**
158+
* @var IoFile
159+
*/
160+
private IoFile $ioFile;
161+
154162
/**
155163
* Tablerate constructor.
156164
* @param Context $context
@@ -161,9 +169,12 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
161169
* @param Filesystem $filesystem
162170
* @param Import $import
163171
* @param RateQueryFactory $rateQueryFactory
164-
* @param null $connectionName
172+
* @param string|null $connectionName
165173
* @param DeploymentConfig|null $deploymentConfig
166174
* @param RequestFactory|null $requestFactory
175+
* @param IoFile|null $ioFile
176+
*
177+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
167178
*/
168179
public function __construct(
169180
\Magento\Framework\Model\ResourceModel\Db\Context $context,
@@ -174,9 +185,10 @@ public function __construct(
174185
\Magento\Framework\Filesystem $filesystem,
175186
Import $import,
176187
RateQueryFactory $rateQueryFactory,
177-
$connectionName = null,
188+
?string $connectionName = null,
178189
?DeploymentConfig $deploymentConfig = null,
179-
?RequestFactory $requestFactory = null
190+
?RequestFactory $requestFactory = null,
191+
?IoFile $ioFile = null
180192
) {
181193
parent::__construct($context, $connectionName);
182194
$this->coreConfig = $coreConfig;
@@ -188,6 +200,7 @@ public function __construct(
188200
$this->rateQueryFactory = $rateQueryFactory;
189201
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
190202
$this->requestFactory = $requestFactory ?: ObjectManager::getInstance()->get(RequestFactory::class);
203+
$this->ioFile = $ioFile ?: ObjectManager::getInstance()->get(IoFile::class);
191204
}
192205

193206
/**
@@ -205,6 +218,7 @@ protected function _construct()
205218
*
206219
* @param \Magento\Quote\Model\Quote\Address\RateRequest $request
207220
* @return array|bool
221+
* @throws LocalizedException
208222
*/
209223
public function getRate(\Magento\Quote\Model\Quote\Address\RateRequest $request)
210224
{
@@ -227,9 +241,11 @@ public function getRate(\Magento\Quote\Model\Quote\Address\RateRequest $request)
227241
}
228242

229243
/**
244+
* Delete elements from database using condition
245+
*
230246
* @param array $condition
231247
* @return $this
232-
* @throws \Magento\Framework\Exception\LocalizedException
248+
* @throws LocalizedException
233249
*/
234250
private function deleteByCondition(array $condition)
235251
{
@@ -241,10 +257,12 @@ private function deleteByCondition(array $condition)
241257
}
242258

243259
/**
260+
* Insert import data
261+
*
244262
* @param array $fields
245263
* @param array $values
246264
* @return void
247-
* @throws \Magento\Framework\Exception\LocalizedException
265+
* @throws LocalizedException
248266
*/
249267
private function importData(array $fields, array $values)
250268
{
@@ -256,13 +274,13 @@ private function importData(array $fields, array $values)
256274
$this->getConnection()->insertArray($this->getMainTable(), $fields, $values);
257275
$this->_importedRows += count($values);
258276
}
259-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
277+
} catch (LocalizedException $e) {
260278
$connection->rollBack();
261-
throw new \Magento\Framework\Exception\LocalizedException(__('Unable to import data'), $e);
279+
throw new LocalizedException(__('Unable to import data'), $e);
262280
} catch (\Exception $e) {
263281
$connection->rollBack();
264282
$this->logger->critical($e);
265-
throw new \Magento\Framework\Exception\LocalizedException(
283+
throw new LocalizedException(
266284
__('Something went wrong while importing table rates.')
267285
);
268286
}
@@ -272,39 +290,15 @@ private function importData(array $fields, array $values)
272290
/**
273291
* Upload table rate file and import data from it
274292
*
275-
* @param \Magento\Framework\DataObject $object
276-
* @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
277-
* @throws \Magento\Framework\Exception\LocalizedException
293+
* @param DataObject $object
294+
* @return Tablerate
295+
* @throws LocalizedException
278296
* @todo: this method should be refactored as soon as updated design will be provided
279297
* @see https://wiki.corp.x.com/display/MCOMS/Magento+Filesystem+Decisions
280-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
281-
* @SuppressWarnings(PHPMD.NPathComplexity)
282298
*/
283-
public function uploadAndImport(\Magento\Framework\DataObject $object)
299+
public function uploadAndImport(DataObject $object): Tablerate
284300
{
285-
/**
286-
* @var \Magento\Framework\App\Config\Value $object
287-
*/
288-
if ($this->deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_ASYNC_CONFIG_SAVE)) {
289-
if (
290-
empty($object->getFieldsetData()['import']['name']) ||
291-
empty($object->getFieldsetData()['import']['full_path'])
292-
) {
293-
return $this;
294-
} else {
295-
$filePath = $object->getFieldsetData()['import']['full_path']
296-
. $object->getFieldsetData()['import']['name'];
297-
}
298-
} else {
299-
$request = $this->requestFactory->create();
300-
$files = (array)$request->getFiles();
301-
302-
if (empty($files['groups']['tablerate']['fields']['import']['value'])) {
303-
return $this;
304-
} else {
305-
$filePath = $files['groups']['tablerate']['fields']['import']['value']['tmp_name'];
306-
}
307-
}
301+
$filePath = $this->getFilePath($object);
308302
if (!$filePath) {
309303
return $this;
310304
}
@@ -313,7 +307,6 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
313307
$conditionName = $this->getConditionName($object);
314308
$file = $this->getCsvFile($filePath);
315309
try {
316-
// delete old data by website and condition name
317310
$condition = [
318311
'website_id = ?' => $websiteId,
319312
'condition_name = ?' => $conditionName,
@@ -327,7 +320,7 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
327320
}
328321
} catch (\Exception $e) {
329322
$this->logger->critical($e);
330-
throw new \Magento\Framework\Exception\LocalizedException(
323+
throw new LocalizedException(
331324
__('Something went wrong while importing table rates.')
332325
);
333326
} finally {
@@ -340,16 +333,20 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
340333
'We couldn\'t import this file because of these errors: %1',
341334
implode(" \n", $this->import->getErrors())
342335
);
343-
throw new \Magento\Framework\Exception\LocalizedException($error);
336+
throw new LocalizedException($error);
344337
}
338+
339+
return $this;
345340
}
346341

347342
/**
348-
* @param \Magento\Framework\DataObject $object
343+
* Extract condition name
344+
*
345+
* @param DataObject $object
349346
* @return mixed|string
350347
* @since 100.1.0
351348
*/
352-
public function getConditionName(\Magento\Framework\DataObject $object)
349+
public function getConditionName(DataObject $object)
353350
{
354351
if ($object->getData('groups/tablerate/fields/condition_name/inherit') == '1') {
355352
$conditionName = (string)$this->coreConfig->getValue('carriers/tablerate/condition_name', 'default');
@@ -360,13 +357,49 @@ public function getConditionName(\Magento\Framework\DataObject $object)
360357
}
361358

362359
/**
360+
* Determine table rate upload file path
361+
*
362+
* @param DataObject $object
363+
* @return string
364+
* @throws FileSystemException
365+
* @throws \Magento\Framework\Exception\RuntimeException
366+
*/
367+
private function getFilePath(DataObject $object): string
368+
{
369+
$filePath = '';
370+
371+
/**
372+
* @var \Magento\Framework\App\Config\Value $object
373+
*/
374+
if ($this->deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_ASYNC_CONFIG_SAVE)) {
375+
if (!empty($object->getFieldsetData()['import']['name']) &&
376+
!empty($object->getFieldsetData()['import']['full_path'])
377+
) {
378+
$filePath = $object->getFieldsetData()['import']['full_path']
379+
. $object->getFieldsetData()['import']['name'];
380+
}
381+
} else {
382+
$request = $this->requestFactory->create();
383+
$files = (array)$request->getFiles();
384+
385+
if (!empty($files['groups']['tablerate']['fields']['import']['value'])) {
386+
$filePath = $files['groups']['tablerate']['fields']['import']['value']['tmp_name'];
387+
}
388+
}
389+
390+
return $filePath;
391+
}
392+
393+
/**
394+
* Open CSV file for reading
395+
*
363396
* @param string $filePath
364397
* @return \Magento\Framework\Filesystem\File\ReadInterface
365398
* @throws FileSystemException
366399
*/
367400
private function getCsvFile($filePath)
368401
{
369-
$pathInfo = pathinfo($filePath);
402+
$pathInfo = $this->ioFile->getPathInfo($filePath);
370403
$dirName = $pathInfo['dirname'] ?? '';
371404
$fileName = $pathInfo['basename'] ?? '';
372405

@@ -376,12 +409,14 @@ private function getCsvFile($filePath)
376409
}
377410

378411
/**
412+
* Remove file
413+
*
379414
* @param string $filePath
380415
* @return bool
381416
*/
382417
private function removeFile(string $filePath): bool
383418
{
384-
$pathInfo = pathinfo($filePath);
419+
$pathInfo = $this->ioFile->getPathInfo($filePath);
385420
$dirName = $pathInfo['dirname'] ?? '';
386421
$fileName = $pathInfo['basename'] ?? '';
387422

@@ -398,6 +433,7 @@ private function removeFile(string $filePath): bool
398433
*
399434
* @param string $conditionName
400435
* @return string
436+
* @throws LocalizedException
401437
*/
402438
protected function _getConditionFullName($conditionName)
403439
{
@@ -413,7 +449,8 @@ protected function _getConditionFullName($conditionName)
413449
* Save import data batch
414450
*
415451
* @param array $data
416-
* @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
452+
* @return Tablerate
453+
* @throws LocalizedException
417454
*/
418455
protected function _saveImportData(array $data)
419456
{

app/code/Magento/OfflineShipping/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"magento/module-sales": "*",
1616
"magento/module-sales-rule": "*",
1717
"magento/module-shipping": "*",
18-
"magento/module-store": "*"
18+
"magento/module-store": "*",
19+
"magento/module-async-config": "*"
1920
},
2021
"suggest": {
2122
"magento/module-checkout": "*",

app/code/Magento/OfflineShipping/etc/adminhtml/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<config>
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<type name="Magento\AsyncConfig\Model\AsyncConfigPublisher">
1010
<plugin name="async_config_file_upload_management"
1111
type="Magento\OfflineShipping\Model\Plugin\AsyncConfig\Model\AsyncConfigPublisherPlugin" sortOrder="1"

0 commit comments

Comments
 (0)