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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Fixtures;
use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Framework\App\Config\Storage\Writer as ConfigWriter;
use Magento\Tax\Api\Data\TaxRateInterface;
use Magento\Tax\Api\Data\TaxRateInterfaceFactory;
use Magento\Tax\Api\Data\TaxRuleInterface;
use Magento\Tax\Api\Data\TaxRuleInterfaceFactory;
use Magento\Tax\Api\TaxRateRepositoryInterface;
use Magento\Tax\Api\TaxRuleRepositoryInterface;
use Magento\Tax\Model\Config;
use Magento\Tax\Model\ResourceModel\Calculation\Rate\CollectionFactory;
/**
* Tax rules fixture generator
* Tax Config Settings setter for different Tax Modes (for example VAT)
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TaxRulesFixture extends Fixture
{
const DEFAULT_CUSTOMER_TAX_CLASS_ID = 3;
const DEFAULT_PRODUCT_TAX_CLASS_ID = 2;
const DEFAULT_TAX_MODE = 'VAT';
const DEFAULT_TAX_RATE = 5;
const DEFAULT_TAX_COUNTRY = 'US';
/**
* @var array config paths and values for tax modes
*/
private $configs = [
'VAT' => [
Config::CONFIG_XML_PATH_SHIPPING_INCLUDES_TAX => 1,
Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX => 1,
Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT => 1,
Config::XML_PATH_DISPLAY_SALES_PRICE => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_SALES_SUBTOTAL => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_SALES_SHIPPING => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_SALES_DISCOUNT => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_SALES_GRANDTOTAL => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_SALES_FULL_SUMMARY => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::CONFIG_XML_PATH_DISPLAY_SHIPPING => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_CART_PRICE => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_CART_SUBTOTAL => Config::DISPLAY_TYPE_INCLUDING_TAX,
Config::XML_PATH_DISPLAY_CART_SHIPPING => Config::DISPLAY_TYPE_INCLUDING_TAX,
Custom::XML_PATH_TAX_WEEE_ENABLE => 1,
]
];
/**
* @var int
*/
protected $priority = 101;
/**
* @var TaxRuleRepositoryInterface
*/
private $taxRuleRepository;
/**
* @var TaxRuleInterfaceFactory
*/
private $taxRuleFactory;
/**
* @var TaxRateInterfaceFactory
*/
private $taxRateFactory;
/**
* @var CollectionFactory
*/
private $taxRateCollectionFactory;
/**
* @var TaxRateRepositoryInterface
*/
private $taxRateRepository;
/**
* @var ConfigWriter
*/
private $configWriter;
/**
* @param FixtureModel $fixtureModel
* @param TaxRuleRepositoryInterface $taxRuleRepository
* @param TaxRuleInterfaceFactory $taxRuleFactory
* @param CollectionFactory $taxRateCollectionFactory
* @param TaxRateInterfaceFactory $taxRateFactory
* @param TaxRateRepositoryInterface $taxRateRepository
* @param ConfigWriter $configWriter
*/
public function __construct(
FixtureModel $fixtureModel,
TaxRuleRepositoryInterface $taxRuleRepository,
TaxRuleInterfaceFactory $taxRuleFactory,
CollectionFactory $taxRateCollectionFactory,
TaxRateInterfaceFactory $taxRateFactory,
TaxRateRepositoryInterface $taxRateRepository,
ConfigWriter $configWriter
) {
parent::__construct($fixtureModel);
$this->taxRuleRepository = $taxRuleRepository;
$this->taxRuleFactory = $taxRuleFactory;
$this->taxRateCollectionFactory = $taxRateCollectionFactory;
$this->taxRateFactory = $taxRateFactory;
$this->taxRateRepository = $taxRateRepository;
$this->configWriter = $configWriter;
}
/**
* {@inheritdoc}
*/
public function execute()
{
//Getting config values
$taxMode = $this->fixtureModel->getValue('tax_mode', null);
$taxRules = $this->fixtureModel->getValue('tax_rules', 0);
if ($taxMode && in_array($taxMode, array_keys($this->configs))) {
$this->setTaxMode($taxMode);
}
$taxRateIds = $this->taxRateCollectionFactory->create()->getAllIds();
$taxRatesCount = count($taxRateIds);
while ($taxRules) {
/** @var $taxRuleDataObject TaxRuleInterface */
$taxRuleDataObject = $this->taxRuleFactory->create();
$taxRuleDataObject->setCode('Tax_Rule_' . $taxRules)
->setTaxRateIds([$taxRateIds[$taxRules % $taxRatesCount]])
->setCustomerTaxClassIds([self::DEFAULT_CUSTOMER_TAX_CLASS_ID])
->setProductTaxClassIds([self::DEFAULT_PRODUCT_TAX_CLASS_ID])
->setPriority(0)
->setPosition(0);
$this->taxRuleRepository->save($taxRuleDataObject);
$taxRules--;
}
}
/**
* Adding appropriate Tax Rate, Tax Rule and Config Settings for selected Tax Mode (for example EU/VAT)
*
* @param string $taxMode
* @return void
*/
private function setTaxMode($taxMode)
{
//Add Tax Rate for selected Tax Mode
/** @var $taxRate TaxRateInterface */
$taxRate = $this->taxRateFactory->create();
$taxRate->setCode($taxMode)
->setRate(self::DEFAULT_TAX_RATE)
->setTaxCountryId(self::DEFAULT_TAX_COUNTRY)
->setTaxPostcode('*');
$taxRateData = $this->taxRateRepository->save($taxRate);
//Add Tax Rule for Tax Mode
/** @var $taxRuleDataObject TaxRuleInterface */
$taxRuleDataObject = $this->taxRuleFactory->create();
$taxRuleDataObject->setCode($taxMode)
->setTaxRateIds([$taxRateData->getId()])
->setCustomerTaxClassIds([self::DEFAULT_CUSTOMER_TAX_CLASS_ID])
->setProductTaxClassIds([self::DEFAULT_PRODUCT_TAX_CLASS_ID])
->setPriority(0)
->setPosition(0);
$this->taxRuleRepository->save($taxRuleDataObject);
//Set Tax Mode configs
$this->setConfigByTaxMode($taxMode);
}
/**
* Set appropriate Tax Config Settings for selected Tax Mode
*
* @param string $mode
* @return void
*/
private function setConfigByTaxMode($mode = self::DEFAULT_TAX_MODE)
{
if (isset($this->configs[$mode]) && is_array($this->configs[$mode])) {
foreach ($this->configs[$mode] as $configPath => $value) {
$this->configWriter->save(
$configPath,
$value
);
}
}
}
/**
* {@inheritdoc}
*/
public function getActionTitle()
{
return 'Generating tax rules';
}
/**
* {@inheritdoc}
*/
public function introduceParamLabels()
{
return [
'tax_rules' => 'Tax Rules Count',
'tax_mode' => 'Tax Mode',
];
}
}