/** * 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->getCountryId()); $collectionItem->setTaxRegionId($taxRate->getRegionId()); $collectionItem->setRegionName($taxRate->getRegionName()); $collectionItem->setTaxPostcode($taxRate->getPostcode()); $collectionItem->setRate($taxRate->getPercentageRate()); $collectionItem->setTitles($this->rateConverter->createTitleArrayFromServiceObject($taxRate)); if ($taxRate->getZipRange() != null) { $zipRange = $taxRate->getZipRange(); /* must be a "1" for existing code (e.g. JavaScript) to work */ $collectionItem->setZipIsRange("1"); $collectionItem->setZipFrom($zipRange->getFrom()); $collectionItem->setZipTo($zipRange->getTo()); } else { $collectionItem->setZipIsRange(null); $collectionItem->setZipFrom(null); $collectionItem->setZipTo(null); } return $collectionItem; }
/** * Validate tax rate * * @param TaxRateDataObject $taxRate * @throws InputException * @return void * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(TaxRateDataObject $taxRate) { $exception = new InputException(); $countryCode = $taxRate->getCountryId(); if (!\Zend_Validate::is($countryCode, 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'country_id']); } else { if (!\Zend_Validate::is($this->countryFactory->create()->loadByCode($countryCode)->getId(), 'NotEmpty')) { $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'country_id', 'value' => $countryCode]); } } $regionCode = $taxRate->getRegionId(); if (\Zend_Validate::is($regionCode, 'NotEmpty') && !\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->getPercentageRate(), '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->getZipRange()) { $zipRangeFromTo = ['zip_from' => $taxRate->getZipRange()->getFrom(), 'zip_to' => $taxRate->getZipRange()->getTo()]; 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->getPostcode()), '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\Service\V1\Data\TaxRate $taxRate * @return array */ protected function extractTaxRateData($taxRate) { $zipRange = $taxRate->getZipRange(); $formData = ['tax_calculation_rate_id' => $taxRate->getId(), 'tax_country_id' => $taxRate->getCountryId(), 'tax_region_id' => $taxRate->getRegionId(), 'tax_postcode' => $taxRate->getPostcode(), 'code' => $taxRate->getCode(), 'rate' => $taxRate->getPercentageRate(), 'zip_is_range' => false]; if ($zipRange) { $formData['zip_is_range'] = true; $formData['zip_from'] = $zipRange->getFrom(); $formData['zip_to'] = $zipRange->getTo(); } if ($taxRate->getTitles()) { $titleData = []; foreach ($taxRate->getTitles() as $title) { $titleData[] = [$title->getStoreId() => $title->getValue()]; } $formData['title'] = $titleData; } return $formData; }
/** * Convert a tax rate data object to an array of associated titles * * @param TaxRateDataObject $taxRate * @return array */ public function createTitleArrayFromServiceObject(TaxRateDataObject $taxRate) { $titles = $taxRate->getTitles(); $titleData = []; if ($titles) { foreach ($titles as $title) { $titleData[$title->getStoreId()] = $title->getValue(); } } return $titleData; }