/** * {@inheritdoc} */ public function save(\Magento\Tax\Api\Data\TaxRateInterface $taxRate) { if ($taxRate->getId()) { $this->rateRegistry->retrieveTaxRate($taxRate->getId()); } $this->validate($taxRate); $taxRateTitles = $this->converter->createTitleArrayFromServiceObject($taxRate); try { $this->resourceModel->save($taxRate); $taxRate->saveTitles($taxRateTitles); } catch (LocalizedException $e) { throw $e; } $this->rateRegistry->registerTaxRate($taxRate); return $taxRate; }
/** * 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; }
/** * 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; }
/** * 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; }