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); }
/** * 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; }
/** * {@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; }
/** * Save Tax Rate * * @param TaxRateDataObject $taxRate * @throws InputException * @throws ModelException * @return RateModel */ protected function saveTaxRate(TaxRateDataObject $taxRate) { $this->validate($taxRate); $taxRateModel = $this->converter->createTaxRateModel($taxRate); $taxRateTitles = $this->converter->createTitleArrayFromServiceObject($taxRate); try { $taxRateModel->save(); $taxRateModel->saveTitles($taxRateTitles); } catch (ModelException $e) { if ($e->getCode() == ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS) { throw new InputException($e->getMessage()); } else { throw $e; } } $this->rateRegistry->registerTaxRate($taxRateModel); return $taxRateModel; }
public function testCreateTitlesFromServiceObjectWhenTitlesAreNotProvided() { $taxRateMock = $this->getMock('Magento\\Tax\\Api\\Data\\TaxRateInterface'); $taxRateMock->expects($this->once())->method('getTitles')->willReturn([]); $this->assertEquals([], $this->converter->createTitleArrayFromServiceObject($taxRateMock)); }
/** * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _prepareForm() { $taxRateId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_ID); try { if ($taxRateId) { $taxRateDataObject = $this->_taxRateRepository->get($taxRateId); } } catch (NoSuchEntityException $e) { /* tax rate not found */ } $sessionFormValues = (array) $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_FORM_DATA); $formData = isset($taxRateDataObject) ? $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject) : []; $formData = array_merge($formData, $sessionFormValues); if (isset($formData['zip_is_range']) && $formData['zip_is_range'] && !isset($formData['tax_postcode'])) { $formData['tax_postcode'] = $formData['zip_from'] . '-' . $formData['zip_to']; } /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $countries = $this->_country->toOptionArray(false, 'US'); unset($countries[0]); if (!isset($formData['tax_country_id'])) { $formData['tax_country_id'] = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } if (!isset($formData['tax_region_id'])) { $formData['tax_region_id'] = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } $regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($formData['tax_country_id']); $regions = $regionCollection->toOptionArray(); if ($regions) { $regions[0]['label'] = '*'; } else { $regions = [['value' => '', 'label' => '*']]; } $legend = $this->getShowLegend() ? __('Tax Rate Information') : ''; $fieldset = $form->addFieldset('base_fieldset', ['legend' => $legend, 'class' => 'form-inline']); if (isset($formData['tax_calculation_rate_id']) && $formData['tax_calculation_rate_id'] > 0) { $fieldset->addField('tax_calculation_rate_id', 'hidden', ['name' => 'tax_calculation_rate_id', 'value' => $formData['tax_calculation_rate_id']]); } $fieldset->addField('code', 'text', ['name' => 'code', 'label' => __('Tax Identifier'), 'title' => __('Tax Identifier'), 'class' => 'required-entry', 'required' => true]); $fieldset->addField('zip_is_range', 'checkbox', ['name' => 'zip_is_range', 'label' => __('Zip/Post is Range'), 'value' => '1']); if (!isset($formData['tax_postcode'])) { $formData['tax_postcode'] = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } $fieldset->addField('tax_postcode', 'text', ['name' => 'tax_postcode', 'label' => __('Zip/Post Code'), 'note' => __("'*' - matches any; 'xyz*' - matches any that begins on 'xyz' and are not longer than %1.", $this->_taxData->getPostCodeSubStringLength())]); $fieldset->addField('zip_from', 'text', ['name' => 'zip_from', 'label' => __('Range From'), 'required' => true, 'maxlength' => 9, 'class' => 'validate-digits', 'css_class' => 'hidden']); $fieldset->addField('zip_to', 'text', ['name' => 'zip_to', 'label' => __('Range To'), 'required' => true, 'maxlength' => 9, 'class' => 'validate-digits', 'css_class' => 'hidden']); $fieldset->addField('tax_region_id', 'select', ['name' => 'tax_region_id', 'label' => __('State'), 'values' => $regions]); $fieldset->addField('tax_country_id', 'select', ['name' => 'tax_country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]); $fieldset->addField('rate', 'text', ['name' => 'rate', 'label' => __('Rate Percent'), 'title' => __('Rate Percent'), 'required' => true, 'class' => 'validate-not-negative-number']); $form->setAction($this->getUrl('tax/rate/save')); $form->setUseContainer(true); $form->setId(self::FORM_ELEMENT_ID); $form->setMethod('post'); if (!$this->_storeManager->hasSingleStore()) { $form->addElement($this->_fieldsetFactory->create()->setLegend(__('Tax Titles'))); } if (isset($formData['zip_is_range']) && $formData['zip_is_range']) { list($formData['zip_from'], $formData['zip_to']) = explode('-', $formData['tax_postcode']); } $form->setValues($formData); $this->setForm($form); $this->setChild('form_after', $this->getLayout()->createBlock('Magento\\Framework\\View\\Element\\Template')->setTemplate('Magento_Tax::rate/js.phtml')); return parent::_prepareForm(); }