Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxRulesFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
12 use Magento\Tax\Api\Data\TaxRateInterfaceFactory;
14 use Magento\Tax\Api\Data\TaxRuleInterfaceFactory;
18 use Magento\Tax\Model\ResourceModel\Calculation\Rate\CollectionFactory;
19 
26 class TaxRulesFixture extends Fixture
27 {
29 
31 
32  const DEFAULT_TAX_MODE = 'VAT';
33 
34  const DEFAULT_TAX_RATE = 5;
35 
36  const DEFAULT_TAX_COUNTRY = 'US';
37 
41  private $configs = [
42  'VAT' => [
43  Config::CONFIG_XML_PATH_SHIPPING_INCLUDES_TAX => 1,
44  Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX => 1,
45  Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT => 1,
46  Config::XML_PATH_DISPLAY_SALES_PRICE => Config::DISPLAY_TYPE_INCLUDING_TAX,
47  Config::XML_PATH_DISPLAY_SALES_SUBTOTAL => Config::DISPLAY_TYPE_INCLUDING_TAX,
48  Config::XML_PATH_DISPLAY_SALES_SHIPPING => Config::DISPLAY_TYPE_INCLUDING_TAX,
49  Config::XML_PATH_DISPLAY_SALES_DISCOUNT => Config::DISPLAY_TYPE_INCLUDING_TAX,
50  Config::XML_PATH_DISPLAY_SALES_GRANDTOTAL => Config::DISPLAY_TYPE_INCLUDING_TAX,
51  Config::XML_PATH_DISPLAY_SALES_FULL_SUMMARY => Config::DISPLAY_TYPE_INCLUDING_TAX,
52  Config::CONFIG_XML_PATH_DISPLAY_SHIPPING => Config::DISPLAY_TYPE_INCLUDING_TAX,
53  Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE => Config::DISPLAY_TYPE_INCLUDING_TAX,
54  Config::XML_PATH_DISPLAY_CART_PRICE => Config::DISPLAY_TYPE_INCLUDING_TAX,
55  Config::XML_PATH_DISPLAY_CART_SUBTOTAL => Config::DISPLAY_TYPE_INCLUDING_TAX,
56  Config::XML_PATH_DISPLAY_CART_SHIPPING => Config::DISPLAY_TYPE_INCLUDING_TAX,
58  ]
59  ];
60 
64  protected $priority = 101;
65 
69  private $taxRuleRepository;
70 
74  private $taxRuleFactory;
75 
79  private $taxRateFactory;
80 
84  private $taxRateCollectionFactory;
85 
89  private $taxRateRepository;
90 
94  private $configWriter;
95 
105  public function __construct(
107  TaxRuleRepositoryInterface $taxRuleRepository,
108  TaxRuleInterfaceFactory $taxRuleFactory,
109  CollectionFactory $taxRateCollectionFactory,
110  TaxRateInterfaceFactory $taxRateFactory,
111  TaxRateRepositoryInterface $taxRateRepository,
112  ConfigWriter $configWriter
113  ) {
114  parent::__construct($fixtureModel);
115 
116  $this->taxRuleRepository = $taxRuleRepository;
117  $this->taxRuleFactory = $taxRuleFactory;
118  $this->taxRateCollectionFactory = $taxRateCollectionFactory;
119  $this->taxRateFactory = $taxRateFactory;
120  $this->taxRateRepository = $taxRateRepository;
121  $this->configWriter = $configWriter;
122  }
123 
127  public function execute()
128  {
129  //Getting config values
130  $taxMode = $this->fixtureModel->getValue('tax_mode', null);
131  $taxRules = $this->fixtureModel->getValue('tax_rules', 0);
132 
133  if ($taxMode && in_array($taxMode, array_keys($this->configs))) {
134  $this->setTaxMode($taxMode);
135  }
136 
137  $taxRateIds = $this->taxRateCollectionFactory->create()->getAllIds();
138  $taxRatesCount = count($taxRateIds);
139 
140  while ($taxRules) {
142  $taxRuleDataObject = $this->taxRuleFactory->create();
143  $taxRuleDataObject->setCode('Tax_Rule_' . $taxRules)
144  ->setTaxRateIds([$taxRateIds[$taxRules % $taxRatesCount]])
145  ->setCustomerTaxClassIds([self::DEFAULT_CUSTOMER_TAX_CLASS_ID])
146  ->setProductTaxClassIds([self::DEFAULT_PRODUCT_TAX_CLASS_ID])
147  ->setPriority(0)
148  ->setPosition(0);
149 
150  $this->taxRuleRepository->save($taxRuleDataObject);
151 
152  $taxRules--;
153  }
154  }
155 
162  private function setTaxMode($taxMode)
163  {
164  //Add Tax Rate for selected Tax Mode
166  $taxRate = $this->taxRateFactory->create();
167  $taxRate->setCode($taxMode)
168  ->setRate(self::DEFAULT_TAX_RATE)
169  ->setTaxCountryId(self::DEFAULT_TAX_COUNTRY)
170  ->setTaxPostcode('*');
171 
172  $taxRateData = $this->taxRateRepository->save($taxRate);
173 
174  //Add Tax Rule for Tax Mode
176  $taxRuleDataObject = $this->taxRuleFactory->create();
177  $taxRuleDataObject->setCode($taxMode)
178  ->setTaxRateIds([$taxRateData->getId()])
179  ->setCustomerTaxClassIds([self::DEFAULT_CUSTOMER_TAX_CLASS_ID])
180  ->setProductTaxClassIds([self::DEFAULT_PRODUCT_TAX_CLASS_ID])
181  ->setPriority(0)
182  ->setPosition(0);
183 
184  $this->taxRuleRepository->save($taxRuleDataObject);
185 
186  //Set Tax Mode configs
187  $this->setConfigByTaxMode($taxMode);
188  }
189 
196  private function setConfigByTaxMode($mode = self::DEFAULT_TAX_MODE)
197  {
198  if (isset($this->configs[$mode]) && is_array($this->configs[$mode])) {
199  foreach ($this->configs[$mode] as $configPath => $value) {
200  $this->configWriter->save(
201  $configPath,
202  $value
203  );
204  }
205  }
206  }
207 
211  public function getActionTitle()
212  {
213  return 'Generating tax rules';
214  }
215 
219  public function introduceParamLabels()
220  {
221  return [
222  'tax_rules' => 'Tax Rules Count',
223  'tax_mode' => 'Tax Mode',
224  ];
225  }
226 }
$value
Definition: gender.phtml:16
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
__construct(FixtureModel $fixtureModel, TaxRuleRepositoryInterface $taxRuleRepository, TaxRuleInterfaceFactory $taxRuleFactory, CollectionFactory $taxRateCollectionFactory, TaxRateInterfaceFactory $taxRateFactory, TaxRateRepositoryInterface $taxRateRepository, ConfigWriter $configWriter)
$taxRate
Definition: tax_rule.php:12