Esempio n. 1
0
 public function testCreateArrayFromServiceObject()
 {
     $taxRateMock = $this->getMock('Magento\\Tax\\Api\\Data\\TaxRateInterface');
     $titlesMock = $this->getMock('Magento\\Tax\\Api\\Data\\TaxRateTitleInterface');
     $taxRateMock->expects($this->atLeastOnce())->method('getTitles')->willReturn([$titlesMock]);
     $titlesMock->expects($this->atLeastOnce())->method('getStoreId')->willReturn(1);
     $titlesMock->expects($this->atLeastOnce())->method('getValue')->willReturn('Value');
     $this->assertArrayHasKey('title[1]', $this->converter->createArrayFromServiceObject($taxRateMock, true));
     $this->assertArrayHasKey('title', $this->converter->createArrayFromServiceObject($taxRateMock));
     $this->assertTrue(is_array($this->converter->createArrayFromServiceObject($taxRateMock)));
 }
Esempio n. 2
0
 /**
  * @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();
 }