/** * Convert a tax rate data object to an array of associated titles * * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate * @return array */ public function createTitleArrayFromServiceObject(\Magento\Tax\Api\Data\TaxRateInterface $taxRate) { $titles = $taxRate->getTitles(); $titleData = []; if ($titles) { foreach ($titles as $title) { $titleData[$title->getStoreId()] = $title->getValue(); } } return $titleData; }
/** * Creates a collection item that represents a tax rate for the tax rates grid. * * @param TaxRate $taxRate Input data for creating the item. * @return \Magento\Framework\Object Collection item that represents a tax rate */ protected function createTaxRateCollectionItem(TaxRate $taxRate) { $collectionItem = new \Magento\Framework\Object(); $collectionItem->setTaxCalculationRateId($taxRate->getId()); $collectionItem->setCode($taxRate->getCode()); $collectionItem->setTaxCountryId($taxRate->getTaxCountryId()); $collectionItem->setTaxRegionId($taxRate->getTaxRegionId()); $collectionItem->setRegionName($taxRate->getRegionName()); $collectionItem->setTaxPostcode($taxRate->getTaxPostcode()); $collectionItem->setRate($taxRate->getRate()); $collectionItem->setTitles($this->rateConverter->createTitleArrayFromServiceObject($taxRate)); if ($taxRate->getZipTo() != null && $taxRate->getZipFrom() != null) { /* must be a "1" for existing code (e.g. JavaScript) to work */ $collectionItem->setZipIsRange("1"); $collectionItem->setZipFrom($taxRate->getZipFrom()); $collectionItem->setZipTo($taxRate->getZipTo()); } else { $collectionItem->setZipIsRange(null); $collectionItem->setZipFrom(null); $collectionItem->setZipTo(null); } return $collectionItem; }
/** * Validate tax rate * * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate * @throws InputException * @return void * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(\Magento\Tax\Api\Data\TaxRateInterface $taxRate) { $exception = new InputException(); $countryCode = $taxRate->getTaxCountryId(); if (!\Zend_Validate::is($countryCode, 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'country_id'])); } elseif (!\Zend_Validate::is($this->countryFactory->create()->loadByCode($countryCode)->getId(), 'NotEmpty')) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'country_id', 'value' => $countryCode])); } $regionCode = $taxRate->getTaxRegionId(); // if regionCode eq 0 (all regions *), do not validate with existing region list if (\Zend_Validate::is($regionCode, 'NotEmpty') && $regionCode != "0" && !\Zend_Validate::is($this->regionFactory->create()->load($regionCode)->getId(), 'NotEmpty')) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'region_id', 'value' => $regionCode])); } if (!\Zend_Validate::is($taxRate->getRate(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'percentage_rate'])); } if (!\Zend_Validate::is(trim($taxRate->getCode()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'code'])); } if ($taxRate->getZipIsRange()) { $zipRangeFromTo = ['zip_from' => $taxRate->getZipFrom(), 'zip_to' => $taxRate->getZipTo()]; foreach ($zipRangeFromTo as $key => $value) { if (!is_numeric($value) || $value < 0) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => $key, 'value' => $value])); } } if ($zipRangeFromTo['zip_from'] > $zipRangeFromTo['zip_to']) { $exception->addError(__('Range To should be equal or greater than Range From.')); } } else { if (!\Zend_Validate::is(trim($taxRate->getTaxPostcode()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode'])); } } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Extract tax rate data in a format which is * * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate * @return array */ protected function extractTaxRateData($taxRate) { $formData = ['tax_calculation_rate_id' => $taxRate->getId(), 'tax_country_id' => $taxRate->getTaxCountryId(), 'tax_region_id' => $taxRate->getTaxRegionId(), 'tax_postcode' => $taxRate->getTaxPostcode(), 'code' => $taxRate->getCode(), 'rate' => $taxRate->getRate(), 'zip_is_range' => false]; if ($taxRate->getZipFrom() && $taxRate->getZipTo()) { $formData['zip_is_range'] = true; $formData['zip_from'] = $taxRate->getZipFrom(); $formData['zip_to'] = $taxRate->getZipTo(); } if ($taxRate->getTitles()) { $titleData = []; foreach ($taxRate->getTitles() as $title) { $titleData[] = [$title->getStoreId() => $title->getValue()]; } $formData['title'] = $titleData; } return $formData; }
/** * Extract tax rate data in a format which is * * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate * @param Boolean $returnNumericLogic * @return array * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function createArrayFromServiceObject(\Magento\Tax\Api\Data\TaxRateInterface $taxRate, $returnNumericLogic = false) { $taxRateFormData = ['tax_calculation_rate_id' => $taxRate->getId(), 'tax_country_id' => $taxRate->getTaxCountryId(), 'tax_region_id' => $taxRate->getTaxRegionId(), 'tax_postcode' => $taxRate->getTaxPostcode(), 'code' => $taxRate->getCode(), 'rate' => $taxRate->getRate(), 'zip_is_range' => $returnNumericLogic ? 0 : false]; if ($taxRateFormData['tax_region_id'] === '0') { $taxRateFormData['tax_region_id'] = ''; } if ($taxRate->getZipFrom() && $taxRate->getZipTo()) { $taxRateFormData['zip_is_range'] = $returnNumericLogic ? 1 : true; $taxRateFormData['zip_from'] = $taxRate->getZipFrom(); $taxRateFormData['zip_to'] = $taxRate->getZipTo(); } if ($returnNumericLogic) { //format for the ajax on multiple sites titles $titleArray = $this->createTitleArrayFromServiceObject($taxRate); if (is_array($titleArray)) { foreach ($titleArray as $storeId => $title) { $taxRateFormData['title[' . $storeId . ']'] = $title; } } } else { //format for the form array on multiple sites titles $titleArray = $this->createTitleArrayFromServiceObject($taxRate); if (is_array($titleArray)) { $titleData = []; foreach ($titleArray as $storeId => $title) { $titleData[] = [$storeId => $title]; } if (count($titleArray) > 0) { $taxRateFormData['title'] = $titleData; } } } return $taxRateFormData; }