1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tax\Model\Calculation\Rate;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\FormatInterface;
/**
* Tax Rate Model converter.
*
* Converts a Tax Rate Model to a Data Object or vice versa.
*/
class Converter
{
/**
* @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory
*/
protected $taxRateDataObjectFactory;
/**
* @var \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory
*/
protected $taxRateTitleDataObjectFactory;
/**
* @var FormatInterface|null
*/
private $format;
/**
* @param \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory
* @param \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory
* @param FormatInterface|null $format
*/
public function __construct(
\Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory,
\Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory,
FormatInterface $format = null
) {
$this->taxRateDataObjectFactory = $taxRateDataObjectFactory;
$this->taxRateTitleDataObjectFactory = $taxRateTitleDataObjectFactory;
$this->format = $format ?: ObjectManager::getInstance()->get(FormatInterface::class);
}
/**
* Convert a tax rate data object to an array of associated titles
*
* @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate
* @return array
*/
public function createTitleArrayFromServiceObject(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
{
$titles = $taxRate->getTitles();
$titleData = [];
if ($titles) {
foreach ($titles as $title) {
$titleData[$title->getStoreId()] = $title->getValue();
}
}
return $titleData;
}
/**
* Extract tax rate data in a format which is
*
* @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate
* @param Boolean $returnNumericLogic
* @return array
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function createArrayFromServiceObject(
\Magento\Tax\Api\Data\TaxRateInterface $taxRate,
$returnNumericLogic = false
) {
$taxRateFormData = [
'tax_calculation_rate_id' => $taxRate->getId(),
'tax_country_id' => $taxRate->getTaxCountryId(),
'tax_region_id' => $taxRate->getTaxRegionId(),
'tax_postcode' => $taxRate->getTaxPostcode(),
'code' => $taxRate->getCode(),
'rate' => $taxRate->getRate(),
'zip_is_range' => $returnNumericLogic ? 0 : false,
];
if ($taxRateFormData['tax_region_id'] === '0') {
$taxRateFormData['tax_region_id'] = '';
}
if ($taxRate->getZipFrom() && $taxRate->getZipTo()) {
$taxRateFormData['zip_is_range'] = $returnNumericLogic ? 1 : true;
$taxRateFormData['zip_from'] = $taxRate->getZipFrom();
$taxRateFormData['zip_to'] = $taxRate->getZipTo();
}
if ($returnNumericLogic) {
//format for the ajax on multiple sites titles
$titleArray=($this->createTitleArrayFromServiceObject($taxRate));
if (is_array($titleArray)) {
foreach ($titleArray as $storeId => $title) {
$taxRateFormData['title[' . $storeId . ']']=$title;
}
}
} else {
//format for the form array on multiple sites titles
$titleArray=($this->createTitleArrayFromServiceObject($taxRate));
if (is_array($titleArray)) {
$titleData = [];
foreach ($titleArray as $storeId => $title) {
$titleData[] = [$storeId => $title];
}
if (count($titleArray)>0) {
$taxRateFormData['title'] = $titleData;
}
}
}
return $taxRateFormData;
}
/**
* Convert an array to a tax rate data object
*
* @param array $formData
* @return \Magento\Tax\Api\Data\TaxRateInterface
*/
public function populateTaxRateData($formData)
{
$taxRate = $this->taxRateDataObjectFactory->create();
$taxRate->setId($this->extractFormData($formData, 'tax_calculation_rate_id'))
->setTaxCountryId($this->extractFormData($formData, 'tax_country_id'))
->setTaxRegionId($this->extractFormData($formData, 'tax_region_id'))
->setTaxPostcode($this->extractFormData($formData, 'tax_postcode'))
->setCode($this->extractFormData($formData, 'code'))
->setRate($this->format->getNumber($this->extractFormData($formData, 'rate')));
if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
$taxRate->setZipFrom($this->extractFormData($formData, 'zip_from'))
->setZipTo($this->extractFormData($formData, 'zip_to'))->setZipIsRange(1);
}
if (isset($formData['title'])) {
$titles = [];
foreach ($formData['title'] as $storeId => $value) {
$titles[] = $this->taxRateTitleDataObjectFactory->create()->setStoreId($storeId)->setValue($value);
}
$taxRate->setTitles($titles);
}
return $taxRate;
}
/**
* Determines if an array value is set in the form data array and returns it.
*
* @param array $formData the form to get data from
* @param string $fieldName the key
* @return null|string
*/
protected function extractFormData($formData, $fieldName)
{
if (isset($formData[$fieldName])) {
return $formData[$fieldName];
}
return null;
}
}