Skip to content

Commit 1de3514

Browse files
author
Yu Tang
committed
Merge branch 'FearlessKiwis-MAGETWO-18815-Tax-Rules-creation-is-extremely-slow' into develop
2 parents b6b0382 + 8a8286c commit 1de3514

File tree

13 files changed

+672
-205
lines changed

13 files changed

+672
-205
lines changed

app/code/Magento/Tax/Block/Adminhtml/Rate/Form.php

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class Form extends \Magento\Backend\Block\Widget\Form\Generic
6565
*/
6666
protected $_taxRateCollection;
6767

68+
/**
69+
* @var \Magento\Tax\Model\Calculation\Rate\Converter
70+
*/
71+
protected $_taxRateConverter;
72+
6873
/**
6974
* @param \Magento\Backend\Block\Template\Context $context
7075
* @param \Magento\Framework\Registry $registry
@@ -75,6 +80,7 @@ class Form extends \Magento\Backend\Block\Widget\Form\Generic
7580
* @param \Magento\Tax\Helper\Data $taxData
7681
* @param \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
7782
* @param \Magento\Tax\Model\TaxRateCollection $taxRateCollection
83+
* @param \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter
7884
* @param array $data
7985
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8086
*/
@@ -88,6 +94,7 @@ public function __construct(
8894
\Magento\Tax\Helper\Data $taxData,
8995
\Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
9096
\Magento\Tax\Model\TaxRateCollection $taxRateCollection,
97+
\Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
9198
array $data = []
9299
) {
93100
$this->_regionFactory = $regionFactory;
@@ -96,6 +103,7 @@ public function __construct(
96103
$this->_taxData = $taxData;
97104
$this->_taxRateRepository = $taxRateRepository;
98105
$this->_taxRateCollection = $taxRateCollection;
106+
$this->_taxRateConverter = $taxRateConverter;
99107
parent::__construct($context, $registry, $formFactory, $data);
100108
}
101109

@@ -127,7 +135,7 @@ protected function _prepareForm()
127135
}
128136

129137
$sessionFormValues = (array)$this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_FORM_DATA);
130-
$formData = isset($taxRateDataObject) ? $this->extractTaxRateData($taxRateDataObject) : [];
138+
$formData = isset($taxRateDataObject) ? $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject) : [];
131139
$formData = array_merge($formData, $sessionFormValues);
132140

133141
if (isset($formData['zip_is_range']) && $formData['zip_is_range'] && !isset($formData['tax_postcode'])) {
@@ -286,65 +294,4 @@ protected function _prepareForm()
286294

287295
return parent::_prepareForm();
288296
}
289-
290-
/**
291-
* Get Tax Rates Collection
292-
*
293-
* @return mixed
294-
*/
295-
public function getRateCollection()
296-
{
297-
if ($this->getData('rate_collection') == null) {
298-
$items = $this->_taxRateCollection->getItems();
299-
$rates = [];
300-
foreach ($items as $rate) {
301-
$rateData = $rate->getData();
302-
if (isset($rateData['titles'])) {
303-
foreach ($rateData['titles'] as $storeId => $value) {
304-
$rateData['title[' . $storeId . ']'] = $value;
305-
}
306-
}
307-
unset($rateData['titles']);
308-
$rates[] = $rateData;
309-
}
310-
311-
$this->setRateCollection($rates);
312-
}
313-
return $this->getData('rate_collection');
314-
}
315-
316-
/**
317-
* Extract tax rate data in a format which is
318-
*
319-
* @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate
320-
* @return array
321-
*/
322-
protected function extractTaxRateData($taxRate)
323-
{
324-
$formData = [
325-
'tax_calculation_rate_id' => $taxRate->getId(),
326-
'tax_country_id' => $taxRate->getTaxCountryId(),
327-
'tax_region_id' => $taxRate->getTaxRegionId(),
328-
'tax_postcode' => $taxRate->getTaxPostcode(),
329-
'code' => $taxRate->getCode(),
330-
'rate' => $taxRate->getRate(),
331-
'zip_is_range' => false,
332-
];
333-
334-
if ($taxRate->getZipFrom() && $taxRate->getZipTo()) {
335-
$formData['zip_is_range'] = true;
336-
$formData['zip_from'] = $taxRate->getZipFrom();
337-
$formData['zip_to'] = $taxRate->getZipTo();
338-
}
339-
340-
if ($taxRate->getTitles()) {
341-
$titleData = [];
342-
foreach ($taxRate->getTitles() as $title) {
343-
$titleData[] = [$title->getStoreId() => $title->getValue()];
344-
}
345-
$formData['title'] = $titleData;
346-
}
347-
348-
return $formData;
349-
}
350297
}

app/code/Magento/Tax/Block/Adminhtml/Rule/Edit/Form.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,16 @@ public function getTaxRateSaveUrl()
297297
return $this->getUrl('tax/rate/ajaxSave/');
298298
}
299299

300+
/**
301+
* Retrieve Tax Rate load URL
302+
*
303+
* @return string
304+
*/
305+
public function getTaxRateLoadUrl()
306+
{
307+
return $this->getUrl('tax/rate/ajaxLoad/');
308+
}
309+
300310
/**
301311
* Extract tax rule data in a format which is
302312
*

app/code/Magento/Tax/Controller/Adminhtml/Rate.php

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,30 @@ class Rate extends \Magento\Backend\App\Action
2121
protected $_coreRegistry;
2222

2323
/**
24-
* @var \Magento\Tax\Api\TaxRateRepositoryInterface
24+
* @var \Magento\Tax\Model\Calculation\Rate\Converter
2525
*/
26-
protected $_taxRateRepository;
26+
protected $_taxRateConverter;
2727

2828
/**
29-
* @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory
30-
*/
31-
protected $_taxRateDataObjectFactory;
32-
33-
/**
34-
* @var \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory
29+
* @var \Magento\Tax\Api\TaxRateRepositoryInterface
3530
*/
36-
protected $_taxRateTitleDataObjectFactory;
31+
protected $_taxRateRepository;
3732

3833
/**
3934
* @param \Magento\Backend\App\Action\Context $context
4035
* @param \Magento\Framework\Registry $coreRegistry
36+
* @param \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter
4137
* @param \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
42-
* @param \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory
43-
* @param \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory
4438
*/
4539
public function __construct(
4640
\Magento\Backend\App\Action\Context $context,
4741
\Magento\Framework\Registry $coreRegistry,
48-
\Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
49-
\Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory,
50-
\Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory
42+
\Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
43+
\Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
5144
) {
5245
$this->_coreRegistry = $coreRegistry;
46+
$this->_taxRateConverter = $taxRateConverter;
5347
$this->_taxRateRepository = $taxRateRepository;
54-
$this->_taxRateDataObjectFactory = $taxRateDataObjectFactory;
55-
$this->_taxRateTitleDataObjectFactory = $taxRateTitleDataObjectFactory;
5648
parent::__construct($context);
5749
}
5850

@@ -96,50 +88,4 @@ protected function _isAllowed()
9688
{
9789
return $this->_authorization->isAllowed('Magento_Tax::manage_tax');
9890
}
99-
100-
/**
101-
* Populate a tax rate data object
102-
*
103-
* @param array $formData
104-
* @return \Magento\Tax\Api\Data\TaxRateInterface
105-
*/
106-
protected function populateTaxRateData($formData)
107-
{
108-
$taxRate = $this->_taxRateDataObjectFactory->create();
109-
$taxRate->setId($this->extractFormData($formData, 'tax_calculation_rate_id'))
110-
->setTaxCountryId($this->extractFormData($formData, 'tax_country_id'))
111-
->setTaxRegionId($this->extractFormData($formData, 'tax_region_id'))
112-
->setTaxPostcode($this->extractFormData($formData, 'tax_postcode'))
113-
->setCode($this->extractFormData($formData, 'code'))
114-
->setRate($this->extractFormData($formData, 'rate'));
115-
if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
116-
$taxRate->setZipFrom($this->extractFormData($formData, 'zip_from'))
117-
->setZipTo($this->extractFormData($formData, 'zip_to'))->setZipIsRange(1);
118-
}
119-
120-
if (isset($formData['title'])) {
121-
$titles = [];
122-
foreach ($formData['title'] as $storeId => $value) {
123-
$titles[] = $this->_taxRateTitleDataObjectFactory->create()->setStoreId($storeId)->setValue($value);
124-
}
125-
$taxRate->setTitles($titles);
126-
}
127-
128-
return $taxRate;
129-
}
130-
131-
/**
132-
* Determines if an array value is set in the form data array and returns it.
133-
*
134-
* @param array $formData the form to get data from
135-
* @param string $fieldName the key
136-
* @return null|string
137-
*/
138-
protected function extractFormData($formData, $fieldName)
139-
{
140-
if (isset($formData[$fieldName])) {
141-
return $formData[$fieldName];
142-
}
143-
return null;
144-
}
14591
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Tax\Controller\Adminhtml\Rate;
8+
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Controller\ResultFactory;
11+
12+
class AjaxLoad extends \Magento\Tax\Controller\Adminhtml\Rate
13+
{
14+
/**
15+
* Json needed for the Ajax Edit Form
16+
*
17+
* @return void
18+
*/
19+
public function execute()
20+
{
21+
$rateId = (int)$this->getRequest()->getParam('id');
22+
try {
23+
/* @var \Magento\Tax\Api\Data\TaxRateInterface */
24+
$taxRateDataObject = $this->_taxRateRepository->get($rateId);
25+
/* @var array */
26+
$resultArray= $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject, true);
27+
28+
$responseContent = [
29+
'success' => true,
30+
'error_message' => '',
31+
'result'=>$resultArray,
32+
];
33+
} catch (NoSuchEntityException $e) {
34+
$responseContent = [
35+
'success' => false,
36+
'error_message' => $e->getMessage(),
37+
];
38+
} catch (\Exception $e) {
39+
$responseContent = [
40+
'success' => false,
41+
'error_message' => __('An error occurred while loading this tax rate.'),
42+
];
43+
}
44+
45+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
46+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
47+
$resultJson->setData($responseContent);
48+
return $resultJson;
49+
}
50+
}

app/code/Magento/Tax/Controller/Adminhtml/Rate/AjaxSave.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function execute()
2020
try {
2121
$rateData = $this->_processRateData($this->getRequest()->getPostValue());
2222
/** @var \Magento\Tax\Api\Data\TaxRateInterface $taxRate */
23-
$taxRate = $this->populateTaxRateData($rateData);
23+
$taxRate = $this->_taxRateConverter->populateTaxRateData($rateData);
2424
$this->_taxRateRepository->save($taxRate);
2525
$responseContent = [
2626
'success' => true,

app/code/Magento/Tax/Controller/Adminhtml/Rate/Save.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function execute()
3232
}
3333

3434
try {
35-
$taxData = $this->populateTaxRateData($ratePost);
35+
$taxData = $this->_taxRateConverter->populateTaxRateData($ratePost);
3636
$this->_taxRateRepository->save($taxData);
3737

3838
$this->messageManager->addSuccess(__('The tax rate has been saved.'));

0 commit comments

Comments
 (0)