Skip to content

Commit 5c9e57d

Browse files
Merge remote-tracking branch 'origin/MAGETWO-59801' into MPI-PR-705
2 parents aa4ae30 + 6a2752c commit 5c9e57d

File tree

25 files changed

+1492
-72
lines changed

25 files changed

+1492
-72
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ protected function _prepareForm()
185185
true
186186
);
187187

188+
$selectConfig = $this->getTaxRatesSelectConfig($formValues);
188189
$fieldset->addField(
189190
'tax_rate',
190191
'editablemultiselect',
@@ -196,7 +197,7 @@ protected function _prepareForm()
196197
'value' => isset($formValues['tax_rate']) ? $formValues['tax_rate'] : [],
197198
'required' => true,
198199
'element_js_class' => 'TaxRateEditableMultiselect',
199-
'select_config' => ['is_entity_editable' => true]
200+
'select_config' => $selectConfig
200201
]
201202
);
202203

@@ -257,6 +258,22 @@ protected function _prepareForm()
257258
return parent::_prepareForm();
258259
}
259260

261+
/**
262+
* Retrieve configuration options for tax rates editable multiselect
263+
*
264+
* @param array $formValues
265+
* @return array
266+
*/
267+
public function getTaxRatesSelectConfig($formValues)
268+
{
269+
$config = [
270+
'is_entity_editable' => true,
271+
'selected_values' => isset($formValues['tax_rate']) ? $formValues['tax_rate'] : []
272+
];
273+
274+
return $config;
275+
}
276+
260277
/**
261278
* Retrieve configuration options for tax class editable multiselect
262279
*
@@ -310,6 +327,16 @@ public function getTaxRateLoadUrl()
310327
return $this->getUrl('tax/rate/ajaxLoad/');
311328
}
312329

330+
/**
331+
* Retrieve next Tax Rates page URL
332+
*
333+
* @return string
334+
*/
335+
public function getTaxRatesPageUrl()
336+
{
337+
return $this->getUrl('tax/rule/ajaxLoadRates/');
338+
}
339+
313340
/**
314341
* Extract tax rule data in a format which is
315342
*
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tax\Block\Grid\Renderer;
7+
8+
/**
9+
* Provides tax rates codes for each tax rule in the grid.
10+
*/
11+
class Codes extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
12+
{
13+
/**
14+
* Renders rates codes grid column
15+
*
16+
* @param \Magento\Framework\DataObject $row
17+
* @return string
18+
*/
19+
public function render(\Magento\Framework\DataObject $row)
20+
{
21+
$ratesCodes = $row->getTaxRatesCodes();
22+
23+
return is_array($ratesCodes) ? implode(', ', $ratesCodes) : '';
24+
}
25+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tax\Controller\Adminhtml\Rule;
7+
8+
use Magento\Framework\App\Action\Context;
9+
use Magento\Framework\App\Action\Action;
10+
use Magento\Framework\Controller\Result\Json;
11+
use Magento\Framework\Controller\ResultFactory;
12+
use Magento\Tax\Model\Rate\Provider as RatesProvider;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
14+
use Magento\Tax\Model\Calculation\Rate;
15+
16+
/**
17+
* Class AjaxLoadRates is intended to load existing
18+
* Tax rates as options for a select element.
19+
*/
20+
class AjaxLoadRates extends Action
21+
{
22+
/**
23+
* @var RatesProvider
24+
*/
25+
private $ratesProvider;
26+
27+
/**
28+
* @var SearchCriteriaBuilder
29+
*/
30+
private $searchCriteriaBuilder;
31+
32+
/**
33+
* @param Context $context
34+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
35+
* @param RatesProvider $ratesProvider
36+
*/
37+
public function __construct(
38+
Context $context,
39+
SearchCriteriaBuilder $searchCriteriaBuilder,
40+
RatesProvider $ratesProvider
41+
) {
42+
parent::__construct($context);
43+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
44+
$this->ratesProvider = $ratesProvider;
45+
}
46+
47+
/**
48+
* Get rates page via AJAX
49+
*
50+
* @return Json
51+
*/
52+
public function execute()
53+
{
54+
$ratesPage = (int) $this->getRequest()->getParam('p');
55+
$ratesFilter = trim($this->getRequest()->getParam('s'));
56+
57+
try {
58+
if (!empty($ratesFilter)) {
59+
$this->searchCriteriaBuilder->addFilter(
60+
Rate::KEY_CODE,
61+
'%'.$ratesFilter.'%',
62+
'like'
63+
);
64+
}
65+
66+
$searchCriteria = $this->searchCriteriaBuilder
67+
->setPageSize($this->ratesProvider->getPageSize())
68+
->setCurrentPage($ratesPage)
69+
->create();
70+
71+
$options = $this->ratesProvider->toOptionArray($searchCriteria);
72+
73+
$response = [
74+
'success' => true,
75+
'errorMessage' => '',
76+
'result'=> $options,
77+
];
78+
} catch (\Exception $e) {
79+
$response = [
80+
'success' => false,
81+
'errorMessage' => __('An error occurred while loading tax rates.')
82+
];
83+
}
84+
85+
/** @var Json $resultJson */
86+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
87+
$resultJson->setData($response);
88+
89+
return $resultJson;
90+
}
91+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tax\Model\Api\SearchCriteria\JoinProcessor;
7+
8+
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\JoinProcessor\CustomJoinInterface;
9+
use Magento\Framework\Data\Collection\AbstractDb;
10+
11+
/**
12+
* Provides additional SQL JOIN to ensure search of required
13+
* tax rule by tax rate code in Tax Rules grid.
14+
*/
15+
class RateCode implements CustomJoinInterface
16+
{
17+
/**
18+
* @param AbstractDb $collection
19+
* @return true
20+
*/
21+
public function apply(AbstractDb $collection)
22+
{
23+
$taxCalculationTableAlias = 'tc';
24+
25+
$collection->joinCalculationData($taxCalculationTableAlias);
26+
27+
$collection->getSelect()->joinLeft(
28+
['rc' => $collection->getTable('tax_calculation_rate')],
29+
"{$taxCalculationTableAlias}.tax_calculation_rate_id = rc.tax_calculation_rate_id",
30+
[]
31+
);
32+
33+
return true;
34+
}
35+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tax\Model\Rate;
7+
8+
use Magento\Framework\Convert\DataObject as Converter;
9+
use Magento\Tax\Api\TaxRateRepositoryInterface;
10+
use Magento\Framework\Api\SearchCriteriaInterface;
11+
use Magento\Tax\Model\Calculation\Rate;
12+
13+
/**
14+
* Provides filtered tax rates models
15+
* as options for select element.
16+
*/
17+
class Provider
18+
{
19+
/**
20+
* @var TaxRateRepositoryInterface
21+
*/
22+
private $taxRateRepository;
23+
24+
/**
25+
* @var Converter
26+
*/
27+
private $converter;
28+
29+
/**
30+
* @var int
31+
*/
32+
private $pageSize = 100;
33+
34+
/**
35+
* Initialize dependencies.
36+
*
37+
* @param TaxRateRepositoryInterface $taxRateRepository
38+
* @param Converter $converter
39+
*/
40+
public function __construct(
41+
TaxRateRepositoryInterface $taxRateRepository,
42+
Converter $converter
43+
) {
44+
$this->taxRateRepository = $taxRateRepository;
45+
$this->converter = $converter;
46+
}
47+
48+
/**
49+
* Retrieve all tax rates as an options array.
50+
*
51+
* @param SearchCriteriaInterface $searchCriteria
52+
* @return array
53+
*/
54+
public function toOptionArray(SearchCriteriaInterface $searchCriteria)
55+
{
56+
$searchResults = $this->taxRateRepository->getList($searchCriteria);
57+
58+
return $this->converter->toOptionArray(
59+
$searchResults->getItems(),
60+
Rate::KEY_ID,
61+
Rate::KEY_CODE
62+
);
63+
}
64+
65+
/**
66+
* Returns predefined size of tax rates list
67+
*
68+
* @return int
69+
*/
70+
public function getPageSize()
71+
{
72+
return (int) $this->pageSize;
73+
}
74+
}

app/code/Magento/Tax/Model/Rate/Source.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
namespace Magento\Tax\Model\Rate;
88

99
use Magento\Framework\Api\SearchCriteriaBuilder;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\Convert\DataObject as Converter;
1112
use Magento\Tax\Api\TaxRateRepositoryInterface;
12-
use Magento\Tax\Model\Calculation\Rate;
13+
use Magento\Tax\Model\Rate\Provider as RateProvider;
1314

1415
/**
1516
* Tax rate source model.
@@ -28,21 +29,27 @@ class Source implements \Magento\Framework\Data\OptionSourceInterface
2829
/** @var Converter */
2930
protected $converter;
3031

32+
/** @var RateProvider */
33+
protected $rateProvider;
34+
3135
/**
3236
* Initialize dependencies.
3337
*
3438
* @param TaxRateRepositoryInterface $taxRateRepository
3539
* @param SearchCriteriaBuilder $searchCriteriaBuilder
3640
* @param Converter $converter
41+
* @param RateProvider $rateProvider
3742
*/
3843
public function __construct(
3944
TaxRateRepositoryInterface $taxRateRepository,
4045
SearchCriteriaBuilder $searchCriteriaBuilder,
41-
Converter $converter
46+
Converter $converter,
47+
RateProvider $rateProvider = null
4248
) {
4349
$this->taxRateRepository = $taxRateRepository;
4450
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
4551
$this->converter = $converter;
52+
$this->rateProvider = $rateProvider ?: ObjectManager::getInstance()->get(RateProvider::class);
4653
}
4754

4855
/**
@@ -53,14 +60,14 @@ public function __construct(
5360
public function toOptionArray()
5461
{
5562
if (!$this->options) {
56-
$searchCriteria = $this->searchCriteriaBuilder->create();
57-
$searchResults = $this->taxRateRepository->getList($searchCriteria);
58-
$this->options = $this->converter->toOptionArray(
59-
$searchResults->getItems(),
60-
Rate::KEY_ID,
61-
Rate::KEY_CODE
62-
);
63+
$searchCriteria = $this->searchCriteriaBuilder
64+
->setPageSize($this->rateProvider->getPageSize())
65+
->setCurrentPage(1)
66+
->create();
67+
68+
$this->options = $this->rateProvider->toOptionArray($searchCriteria);
6369
}
70+
6471
return $this->options;
6572
}
6673
}

app/code/Magento/Tax/Model/ResourceModel/Calculation/Rule/Collection.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ public function joinCalculationData($alias)
6565
* @param string $secondaryJoinField
6666
* @param string $titleField
6767
* @param string $dataField
68+
* @param string $dataTitleField
6869
* @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
6970
*/
70-
protected function _add($itemTable, $primaryJoinField, $secondaryJoinField, $titleField, $dataField)
71-
{
71+
protected function _add(
72+
$itemTable,
73+
$primaryJoinField,
74+
$secondaryJoinField,
75+
$titleField,
76+
$dataField,
77+
$dataTitleField = ''
78+
) {
7279
$children = [];
7380
foreach ($this as $rule) {
7481
$children[$rule->getId()] = [];
@@ -98,6 +105,9 @@ protected function _add($itemTable, $primaryJoinField, $secondaryJoinField, $tit
98105
foreach ($this as $rule) {
99106
if (isset($children[$rule->getId()])) {
100107
$rule->setData($dataField, array_keys($children[$rule->getId()]));
108+
if (!empty($dataTitleField)) {
109+
$rule->setData($dataTitleField, $children[$rule->getId()]);
110+
}
101111
}
102112
}
103113

@@ -136,7 +146,8 @@ public function addRatesToResult()
136146
'tax_calculation_rate_id',
137147
'tax_calculation_rate_id',
138148
'code',
139-
'tax_rates'
149+
'tax_rates',
150+
'tax_rates_codes'
140151
);
141152
}
142153

app/code/Magento/Tax/Model/TaxRuleCollection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ protected function createTaxRuleCollectionItem(TaxRuleInterface $taxRule)
7878
$collectionItem->setCalculateSubtotal($taxRule->getCalculateSubtotal() ? '1' : '0');
7979
$collectionItem->setCustomerTaxClasses($taxRule->getCustomerTaxClassIds());
8080
$collectionItem->setProductTaxClasses($taxRule->getProductTaxClassIds());
81+
$collectionItem->setTaxRatesCodes($taxRule->getTaxRatesCodes());
8182
$collectionItem->setTaxRates($taxRule->getTaxRateIds());
8283
return $collectionItem;
8384
}

0 commit comments

Comments
 (0)