/** * {@inheritdoc} */ public function loadData($printQuery = false, $logQuery = false) { if (!$this->isLoaded()) { $searchCriteria = $this->getSearchCriteria(); $searchResults = $this->ruleService->getList($searchCriteria); $this->_totalRecords = $searchResults->getTotalCount(); foreach ($searchResults->getItems() as $taxRule) { $this->_addItem($this->createTaxRuleCollectionItem($taxRule)); } $this->_setIsLoaded(); } return $this; }
/** * * @param Filter[] $filters * @param Filter[] $filterGroup * @param string[] $expectedRuleCodes The codes of the tax rules that are expected to be found * * @magentoDbIsolation enabled * @dataProvider searchTaxRulesDataProvider */ public function testGetList($filters, $filterGroup, $expectedRuleCodes) { $this->setUpDefaultRules(); /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */ $searchBuilder = Bootstrap::getObjectManager()->create('Magento\\Framework\\Api\\SearchCriteriaBuilder'); foreach ($filters as $filter) { $searchBuilder->addFilters([$filter]); } if ($filterGroup !== null) { $searchBuilder->addFilters($filterGroup); } $searchCriteria = $searchBuilder->create(); $searchResults = $this->taxRuleRepository->getList($searchCriteria); $items = []; foreach ($expectedRuleCodes as $ruleCode) { $ruleId = $this->taxRules[$ruleCode]; $items[] = $this->taxRuleRepository->get($ruleId); } $this->assertEquals($searchCriteria, $searchResults->getSearchCriteria()); $this->assertEquals(count($expectedRuleCodes), $searchResults->getTotalCount()); foreach ($searchResults->getItems() as $rule) { $this->assertContains($rule->getCode(), $expectedRuleCodes); } $this->tearDownDefaultRules(); }
/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing taxes:'); $fixtureFile = 'Tax/tax_rate.csv'; $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile); /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']); foreach ($csvReader as $data) { if ($this->rateFactory->create()->loadByCode($data['code'])->getId()) { continue; } $taxRate = $this->rateFactory->create(); $taxRate->setCode($data['code'])->setTaxCountryId($data['tax_country_id'])->setTaxRegionId($data['tax_region_id'])->setTaxPostcode($data['tax_postcode'])->setRate($data['rate']); $this->taxRateRepository->save($taxRate); $this->logger->logInline('.'); } $fixtureFile = 'Tax/tax_rule.csv'; $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile); /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']); foreach ($csvReader as $data) { $filter = $this->filterBuilder->setField('code')->setConditionType('=')->setValue($data['code'])->create(); $criteria = $this->criteriaBuilder->addFilters([$filter])->create(); $existingRates = $this->taxRuleRepository->getList($criteria)->getItems(); if (!empty($existingRates)) { continue; } $taxRate = $this->taxRateFactory->create()->loadByCode($data['tax_rate']); $taxRule = $this->ruleFactory->create(); $taxRule->setCode($data['code'])->setTaxRateIds([$taxRate->getId()])->setCustomerTaxClassIds([$data['tax_customer_class']])->setProductTaxClassIds([$data['tax_product_class']])->setPriority($data['priority'])->setCalculateSubtotal($data['calculate_subtotal'])->setPosition($data['position']); $this->taxRuleRepository->save($taxRule); $this->logger->logInline('.'); } }
/** * {@inheritdoc} */ public function getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId) { $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField('customer_tax_class_ids')->setValue([$customerTaxClassId])->create()]); $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField('product_tax_class_ids')->setValue([$productTaxClassId])->create()]); $searchResults = $this->taxRuleRepository->getList($this->searchCriteriaBuilder->create()); $taxRules = $searchResults->getItems(); $rates = []; foreach ($taxRules as $taxRule) { $rateIds = $taxRule->getTaxRateIds(); if (!empty($rateIds)) { foreach ($rateIds as $rateId) { $rates[] = $this->taxRateRepository->get($rateId); } } } return $rates; }
/** * Check to see if there are any native tax rules created that may affect AvaTax * * @return array */ public function checkNativeTaxRules() { $errors = []; if ($this->avaTaxConfig->isModuleEnabled() && $this->avaTaxConfig->getTaxMode($this->storeManager->getDefaultStoreView()) != Config::TAX_MODE_NO_ESTIMATE_OR_SUBMIT && !$this->avaTaxConfig->isNativeTaxRulesIgnored()) { $taxRules = $this->taxRuleRepository->getList($this->searchCriteriaBuilder->create()); if (count($taxRules->getItems())) { $errors[] = __('You have %1 native Magento Tax Rule(s) configured. ' . 'Please <a href="%2">review the tax rule(s)</a> and delete any that you do not specifically want enabled. ' . 'You should only have rules setup if you want to use them as backup rules in case of AvaTax ' . 'errors (see <a href="#row_tax_avatax_error_handling_header">Error Action setting</a>) ' . 'or if you need to support VAT tax. ' . '<a href="%3">Ignore this notification</a>.', count($taxRules->getItems()), $this->backendUrl->getUrl('tax/rule'), $this->backendUrl->getUrl('avatax/tax/ignoreTaxRuleNotification')); } } return $errors; }
/** * {@inheritdoc} */ public function install(array $fixtures) { foreach ($fixtures as $fileName) { $fileName = $this->fixtureManager->getFixture($fileName); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } if ($this->rateFactory->create()->loadByCode($data['code'])->getId()) { continue; } $taxRate = $this->rateFactory->create(); $taxRate->setCode($data['code'])->setTaxCountryId($data['tax_country_id'])->setTaxRegionId($data['tax_region_id'])->setTaxPostcode($data['tax_postcode'])->setRate($data['rate']); $this->taxRateRepository->save($taxRate); } $fixtureFile = 'Magento_TaxSampleData::fixtures/tax_rule.csv'; $fixtureFileName = $this->fixtureManager->getFixture($fixtureFile); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fixtureFileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $filter = $this->filterBuilder->setField('code')->setConditionType('=')->setValue($data['code'])->create(); $criteria = $this->criteriaBuilder->addFilters([$filter])->create(); $existingRates = $this->taxRuleRepository->getList($criteria)->getItems(); if (!empty($existingRates)) { continue; } $taxRate = $this->taxRateFactory->create()->loadByCode($data['tax_rate']); $taxRule = $this->ruleFactory->create(); $taxRule->setCode($data['code'])->setTaxRateIds([$taxRate->getId()])->setCustomerTaxClassIds([$data['tax_customer_class']])->setProductTaxClassIds([$data['tax_product_class']])->setPriority($data['priority'])->setCalculateSubtotal($data['calculate_subtotal'])->setPosition($data['position']); $this->taxRuleRepository->save($taxRule); } } }