/** * Convert a rate model to a TaxRate data object * * @param TaxRateModel $rateModel * @return TaxRateDataObject * @SuppressWarnings(PHPMD.NPathComplexity) */ public function createTaxRateDataObjectFromModel(TaxRateModel $rateModel) { $this->taxRateDataObjectBuilder->populateWithArray([]); if ($rateModel->getId()) { $this->taxRateDataObjectBuilder->setId($rateModel->getId()); } if ($rateModel->getTaxCountryId()) { $this->taxRateDataObjectBuilder->setCountryId($rateModel->getTaxCountryId()); } /* tax region id may be 0 which is "*" which would fail an if check */ if ($rateModel->getTaxRegionId() !== null) { $this->taxRateDataObjectBuilder->setRegionId($rateModel->getTaxRegionId()); $regionName = $this->directoryRegion->load($rateModel->getTaxRegionId())->getCode(); $this->taxRateDataObjectBuilder->setRegionName($regionName); } if ($rateModel->getRegionName()) { $this->taxRateDataObjectBuilder->setRegionName($rateModel->getRegionName()); } if ($rateModel->getTaxPostcode()) { $this->taxRateDataObjectBuilder->setPostcode($rateModel->getTaxPostcode()); } if ($rateModel->getCode()) { $this->taxRateDataObjectBuilder->setCode($rateModel->getCode()); } if ($rateModel->getRate()) { $this->taxRateDataObjectBuilder->setPercentageRate((double) $rateModel->getRate()); } if ($rateModel->getZipIsRange()) { $zipRange = $this->zipRangeDataObjectBuilder->populateWithArray([])->setFrom($rateModel->getZipFrom())->setTo($rateModel->getZipTo())->create(); $this->taxRateDataObjectBuilder->setZipRange($zipRange); } $this->taxRateDataObjectBuilder->setTitles($this->createTitleArrayFromModel($rateModel)); return $this->taxRateDataObjectBuilder->create(); }
/** * Populate a tax rate data object * * @param array $formData * @return \Magento\Tax\Service\V1\Data\TaxRate */ protected function populateTaxRateData($formData) { $this->_taxRateBuilder->setId($this->extractFormData($formData, 'tax_calculation_rate_id'))->setCountryId($this->extractFormData($formData, 'tax_country_id'))->setRegionId($this->extractFormData($formData, 'tax_region_id'))->setPostcode($this->extractFormData($formData, 'tax_postcode'))->setCode($this->extractFormData($formData, 'code'))->setPercentageRate($this->extractFormData($formData, 'rate')); if (isset($formData['zip_is_range']) && $formData['zip_is_range']) { $this->_zipRangeBuilder->setFrom($this->extractFormData($formData, 'zip_from'))->setTo($this->extractFormData($formData, 'zip_to')); $zipRange = $this->_zipRangeBuilder->create(); $this->_taxRateBuilder->setZipRange($zipRange); } if (isset($formData['title'])) { $titles = []; foreach ($formData['title'] as $storeId => $value) { $titles[] = $this->_taxRateTitleBuilder->setStoreId($storeId)->setValue($value)->create(); } $this->_taxRateBuilder->setTitles($titles); } return $this->_taxRateBuilder->create(); }
/** * @magentoDbIsolation enabled */ public function testDeleteTaxRateException() { // Create a new tax rate $taxRateData = $this->taxRateBuilder->setCode('TX')->setCountryId('US')->setPercentageRate(6)->setPostcode(77001)->setRegionId(1)->create(); $taxRateId = $this->taxRateService->createTaxRate($taxRateData)->getId(); // Delete the new tax rate $this->assertTrue($this->taxRateService->deleteTaxRate($taxRateId)); // Delete the new tax rate again, this should fail try { $this->taxRateService->deleteTaxRate($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'); } }