/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing taxes:'); $fixtureFile = 'Tax/tax_rate.csv'; $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile); /** @var \Magento\Tools\SampleData\Helper\Csv\Reader $csvReader */ $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']); foreach ($csvReader as $data) { $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\Tools\SampleData\Helper\Csv\Reader $csvReader */ $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']); foreach ($csvReader as $data) { $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('.'); } }
public function testPopulateTaxRateData() { $rateTitles = [$this->objectManager->getObject('\\Magento\\Tax\\Model\\Calculation\\Rate\\Title', ['data' => ['store_id' => 1, 'value' => 'texas']])]; $dataArray = ['tax_country_id' => 'US', 'tax_region_id' => 2, 'tax_postcode' => null, 'rate' => 7.5, 'code' => 'Tax Rate Code', 'titles' => $rateTitles]; $taxRate = $this->objectManager->getObject('Magento\\Tax\\Model\\Calculation\\Rate', ['data' => $dataArray]); $this->taxRateDataObjectFactory->expects($this->once())->method('create')->willReturn($taxRate); $this->assertSame($taxRate, $this->converter->populateTaxRateData($dataArray)); $this->assertEquals($taxRate->getTitles(), $rateTitles); }
/** * {@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('.'); } }
/** * Populate a tax rate data object * * @param array $formData * @return \Magento\Tax\Api\Data\TaxRateInterface */ protected 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->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; }
/** * {@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); } } }
/** * @magentoDbIsolation enabled */ public function testDeleteThrowsExceptionIfTargetTaxRateDoesNotExist() { // Create a new tax rate $taxRateData = $this->taxRateFactory->create(); $taxRateData->setCode('TX')->setTaxCountryId('US')->setRate(6)->setTaxPostcode(77001)->setTaxRegionId(1); $taxRateId = $this->rateRepository->save($taxRateData)->getId(); // Delete the new tax rate $this->assertTrue($this->rateRepository->deleteById($taxRateId)); // Delete the new tax rate again, this should fail try { $this->rateRepository->deleteById($taxRateId); $this->fail('NoSuchEntityException expected but not thrown'); } catch (NoSuchEntityException $e) { $expectedParams = ['fieldName' => 'taxRateId', 'fieldValue' => $taxRateId]; $this->assertEquals($expectedParams, $e->getParameters()); } catch (\Exception $e) { $this->fail('Caught unexpected exception'); } }