Form.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Admin product tax class add form
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Tax\Block\Adminhtml\Rate;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Tax\Controller\RegistryConstants;
  14. /**
  15. * @api
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * @since 100.0.2
  18. */
  19. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  20. {
  21. const FORM_ELEMENT_ID = 'rate-form';
  22. /**
  23. * @var null
  24. */
  25. protected $_titles = null;
  26. /**
  27. * @var string
  28. */
  29. protected $_template = 'Magento_Tax::rate/form.phtml';
  30. /**
  31. * Tax data
  32. *
  33. * @var \Magento\Tax\Helper\Data|null
  34. */
  35. protected $_taxData = null;
  36. /**
  37. * @var \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory
  38. */
  39. protected $_fieldsetFactory;
  40. /**
  41. * @var \Magento\Directory\Model\Config\Source\Country
  42. */
  43. protected $_country;
  44. /**
  45. * @var \Magento\Directory\Model\RegionFactory
  46. */
  47. protected $_regionFactory;
  48. /**
  49. * @var \Magento\Tax\Api\TaxRateRepositoryInterface
  50. */
  51. protected $_taxRateRepository;
  52. /**
  53. * @var \Magento\Tax\Model\TaxRateCollection
  54. */
  55. protected $_taxRateCollection;
  56. /**
  57. * @var \Magento\Tax\Model\Calculation\Rate\Converter
  58. */
  59. protected $_taxRateConverter;
  60. /**
  61. * @param \Magento\Backend\Block\Template\Context $context
  62. * @param \Magento\Framework\Registry $registry
  63. * @param \Magento\Framework\Data\FormFactory $formFactory
  64. * @param \Magento\Directory\Model\RegionFactory $regionFactory
  65. * @param \Magento\Directory\Model\Config\Source\Country $country
  66. * @param \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory
  67. * @param \Magento\Tax\Helper\Data $taxData
  68. * @param \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
  69. * @param \Magento\Tax\Model\TaxRateCollection $taxRateCollection
  70. * @param \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter
  71. * @param array $data
  72. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  73. */
  74. public function __construct(
  75. \Magento\Backend\Block\Template\Context $context,
  76. \Magento\Framework\Registry $registry,
  77. \Magento\Framework\Data\FormFactory $formFactory,
  78. \Magento\Directory\Model\RegionFactory $regionFactory,
  79. \Magento\Directory\Model\Config\Source\Country $country,
  80. \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory,
  81. \Magento\Tax\Helper\Data $taxData,
  82. \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
  83. \Magento\Tax\Model\TaxRateCollection $taxRateCollection,
  84. \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
  85. array $data = []
  86. ) {
  87. $this->_regionFactory = $regionFactory;
  88. $this->_country = $country;
  89. $this->_fieldsetFactory = $fieldsetFactory;
  90. $this->_taxData = $taxData;
  91. $this->_taxRateRepository = $taxRateRepository;
  92. $this->_taxRateCollection = $taxRateCollection;
  93. $this->_taxRateConverter = $taxRateConverter;
  94. parent::__construct($context, $registry, $formFactory, $data);
  95. }
  96. /**
  97. * @return void
  98. */
  99. protected function _construct()
  100. {
  101. parent::_construct();
  102. $this->setDestElementId(self::FORM_ELEMENT_ID);
  103. }
  104. /**
  105. * @return $this
  106. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  107. * @SuppressWarnings(PHPMD.NPathComplexity)
  108. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  109. */
  110. protected function _prepareForm()
  111. {
  112. $taxRateId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_ID);
  113. try {
  114. if ($taxRateId) {
  115. $taxRateDataObject = $this->_taxRateRepository->get($taxRateId);
  116. }
  117. } catch (NoSuchEntityException $e) {
  118. /* tax rate not found */
  119. }
  120. $sessionFormValues = (array)$this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_FORM_DATA);
  121. $formData = isset($taxRateDataObject)
  122. ? $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject)
  123. : [];
  124. $formData = array_merge($formData, $sessionFormValues);
  125. if (isset($formData['zip_is_range']) && $formData['zip_is_range'] && !isset($formData['tax_postcode'])) {
  126. $formData['tax_postcode'] = $formData['zip_from'] . '-' . $formData['zip_to'];
  127. }
  128. /** @var \Magento\Framework\Data\Form $form */
  129. $form = $this->_formFactory->create();
  130. $countries = $this->_country->toOptionArray(false, 'US');
  131. unset($countries[0]);
  132. if (!isset($formData['tax_country_id'])) {
  133. $formData['tax_country_id'] = $this->_scopeConfig->getValue(
  134. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
  135. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  136. );
  137. }
  138. if (!isset($formData['tax_region_id'])) {
  139. $formData['tax_region_id'] = $this->_scopeConfig->getValue(
  140. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
  141. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  142. );
  143. }
  144. $regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
  145. $formData['tax_country_id']
  146. );
  147. $regions = $regionCollection->toOptionArray();
  148. if ($regions) {
  149. $regions[0]['label'] = '*';
  150. } else {
  151. $regions = [['value' => '', 'label' => '*']];
  152. }
  153. $legend = $this->getShowLegend() ? __('Tax Rate Information') : '';
  154. $fieldset = $form->addFieldset('base_fieldset', ['legend' => $legend, 'class' => 'form-inline']);
  155. if (isset($formData['tax_calculation_rate_id']) && $formData['tax_calculation_rate_id'] > 0) {
  156. $fieldset->addField(
  157. 'tax_calculation_rate_id',
  158. 'hidden',
  159. ['name' => 'tax_calculation_rate_id', 'value' => $formData['tax_calculation_rate_id']]
  160. );
  161. }
  162. $fieldset->addField(
  163. 'code',
  164. 'text',
  165. [
  166. 'name' => 'code',
  167. 'label' => __('Tax Identifier'),
  168. 'title' => __('Tax Identifier'),
  169. 'class' => 'required-entry',
  170. 'required' => true
  171. ]
  172. );
  173. $fieldset->addField(
  174. 'zip_is_range',
  175. 'checkbox',
  176. ['name' => 'zip_is_range', 'label' => __('Zip/Post is Range'), 'value' => '1']
  177. );
  178. if (!isset($formData['tax_postcode'])) {
  179. $formData['tax_postcode'] = $this->_scopeConfig->getValue(
  180. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE,
  181. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  182. );
  183. }
  184. $fieldset->addField(
  185. 'tax_postcode',
  186. 'text',
  187. [
  188. 'name' => 'tax_postcode',
  189. 'label' => __('Zip/Post Code'),
  190. 'note' => __(
  191. "'*' - matches any; 'xyz*' - matches any that begins on 'xyz' and are not longer than %1.",
  192. $this->_taxData->getPostCodeSubStringLength()
  193. )
  194. ]
  195. );
  196. $fieldset->addField(
  197. 'zip_from',
  198. 'text',
  199. [
  200. 'name' => 'zip_from',
  201. 'label' => __('Range From'),
  202. 'required' => true,
  203. 'maxlength' => 9,
  204. 'class' => 'validate-digits',
  205. 'css_class' => 'hidden'
  206. ]
  207. );
  208. $fieldset->addField(
  209. 'zip_to',
  210. 'text',
  211. [
  212. 'name' => 'zip_to',
  213. 'label' => __('Range To'),
  214. 'required' => true,
  215. 'maxlength' => 9,
  216. 'class' => 'validate-digits',
  217. 'css_class' => 'hidden'
  218. ]
  219. );
  220. $fieldset->addField(
  221. 'tax_region_id',
  222. 'select',
  223. ['name' => 'tax_region_id', 'label' => __('State'), 'values' => $regions]
  224. );
  225. $fieldset->addField(
  226. 'tax_country_id',
  227. 'select',
  228. ['name' => 'tax_country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
  229. );
  230. $fieldset->addField(
  231. 'rate',
  232. 'text',
  233. [
  234. 'name' => 'rate',
  235. 'label' => __('Rate Percent'),
  236. 'title' => __('Rate Percent'),
  237. 'required' => true,
  238. 'class' => 'validate-not-negative-number'
  239. ]
  240. );
  241. $form->setAction($this->getUrl('tax/rate/save'));
  242. $form->setUseContainer(true);
  243. $form->setId(self::FORM_ELEMENT_ID);
  244. $form->setMethod('post');
  245. if (!$this->_storeManager->hasSingleStore()) {
  246. $form->addElement($this->_fieldsetFactory->create()->setLegend(__('Tax Titles')));
  247. }
  248. if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
  249. list($formData['zip_from'], $formData['zip_to']) = explode('-', $formData['tax_postcode']);
  250. }
  251. $form->setValues($formData);
  252. $this->setForm($form);
  253. $this->setChild(
  254. 'form_after',
  255. $this->getLayout()->createBlock(
  256. \Magento\Framework\View\Element\Template::class
  257. )->setTemplate('Magento_Tax::rate/js.phtml')
  258. );
  259. return parent::_prepareForm();
  260. }
  261. }