Form.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Adminhtml Tax Rule Edit Form
  8. */
  9. namespace Magento\Tax\Block\Adminhtml\Rule\Edit;
  10. use Magento\Tax\Api\TaxClassManagementInterface;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  16. {
  17. /**
  18. * @var \Magento\Tax\Model\Rate\Source
  19. */
  20. protected $rateSource;
  21. /**
  22. * @var \Magento\Framework\Data\Form\FormKey
  23. */
  24. protected $formKey;
  25. /**
  26. * @var \Magento\Tax\Api\TaxRuleRepositoryInterface
  27. */
  28. protected $ruleService;
  29. /**
  30. * @var \Magento\Tax\Api\TaxClassRepositoryInterface
  31. */
  32. protected $taxClassService;
  33. /**
  34. * @var \Magento\Tax\Model\TaxClass\Source\Customer
  35. */
  36. protected $customerTaxClassSource;
  37. /**
  38. * @var \Magento\Tax\Model\TaxClass\Source\Product
  39. */
  40. protected $productTaxClassSource;
  41. /**
  42. * @param \Magento\Backend\Block\Template\Context $context
  43. * @param \Magento\Framework\Registry $registry
  44. * @param \Magento\Framework\Data\FormFactory $formFactory
  45. * @param \Magento\Tax\Model\Rate\Source $rateSource
  46. * @param \Magento\Tax\Api\TaxRuleRepositoryInterface $ruleService
  47. * @param \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassService
  48. * @param \Magento\Tax\Model\TaxClass\Source\Customer $customerTaxClassSource
  49. * @param \Magento\Tax\Model\TaxClass\Source\Product $productTaxClassSource
  50. * @param array $data
  51. */
  52. public function __construct(
  53. \Magento\Backend\Block\Template\Context $context,
  54. \Magento\Framework\Registry $registry,
  55. \Magento\Framework\Data\FormFactory $formFactory,
  56. \Magento\Tax\Model\Rate\Source $rateSource,
  57. \Magento\Tax\Api\TaxRuleRepositoryInterface $ruleService,
  58. \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassService,
  59. \Magento\Tax\Model\TaxClass\Source\Customer $customerTaxClassSource,
  60. \Magento\Tax\Model\TaxClass\Source\Product $productTaxClassSource,
  61. array $data = []
  62. ) {
  63. $this->rateSource = $rateSource;
  64. $this->formKey = $context->getFormKey();
  65. $this->ruleService = $ruleService;
  66. $this->taxClassService = $taxClassService;
  67. $this->customerTaxClassSource = $customerTaxClassSource;
  68. $this->productTaxClassSource = $productTaxClassSource;
  69. parent::__construct($context, $registry, $formFactory, $data);
  70. }
  71. /**
  72. * Init class
  73. *
  74. * @return void
  75. */
  76. protected function _construct()
  77. {
  78. parent::_construct();
  79. $this->setId('taxRuleForm');
  80. $this->setTitle(__('Tax Rule Information'));
  81. $this->setUseContainer(true);
  82. }
  83. /**
  84. * @return $this
  85. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  86. * @SuppressWarnings(PHPMD.NPathComplexity)
  87. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  88. */
  89. protected function _prepareForm()
  90. {
  91. $taxRuleId = $this->_coreRegistry->registry('tax_rule_id');
  92. try {
  93. $taxRule = $this->ruleService->get($taxRuleId);
  94. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  95. /** Tax rule not found */
  96. }
  97. /** @var \Magento\Framework\Data\Form $form */
  98. $form = $this->_formFactory->create(
  99. ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
  100. );
  101. $sessionFormValues = (array)$this->_coreRegistry->registry('tax_rule_form_data');
  102. $taxRuleData = isset($taxRule) ? $this->extractTaxRuleData($taxRule) : [];
  103. $formValues = array_merge($taxRuleData, $sessionFormValues);
  104. $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Tax Rule Information')]);
  105. $fieldset->addField(
  106. 'code',
  107. 'text',
  108. [
  109. 'name' => 'code',
  110. 'value' => isset($formValues['code']) ? $formValues['code'] : '',
  111. 'label' => __('Name'),
  112. 'class' => 'required-entry',
  113. 'required' => true
  114. ]
  115. );
  116. // Editable multiselect for customer tax class
  117. $selectConfig = $this->getTaxClassSelectConfig(TaxClassManagementInterface::TYPE_CUSTOMER);
  118. $options = $this->customerTaxClassSource->getAllOptions(false);
  119. if (!empty($options)) {
  120. $selected = $options[0];
  121. } else {
  122. $selected = null;
  123. }
  124. // Use the rule data or pick the first class in the list
  125. $selectedCustomerTax = isset($formValues['tax_customer_class'])
  126. ? $formValues['tax_customer_class']
  127. : $selected;
  128. $fieldset->addField(
  129. 'tax_customer_class',
  130. 'editablemultiselect',
  131. [
  132. 'name' => 'tax_customer_class',
  133. 'label' => __('Customer Tax Class'),
  134. 'class' => 'required-entry',
  135. 'values' => $options,
  136. 'value' => $selectedCustomerTax,
  137. 'required' => true,
  138. 'select_config' => $selectConfig
  139. ],
  140. false,
  141. true
  142. );
  143. // Editable multiselect for product tax class
  144. $selectConfig = $this->getTaxClassSelectConfig(TaxClassManagementInterface::TYPE_PRODUCT);
  145. $options = $this->productTaxClassSource->getAllOptions(false);
  146. if (!empty($options)) {
  147. $selected = $options[0];
  148. } else {
  149. $selected = null;
  150. }
  151. // Use the rule data or pick the first class in the list
  152. $selectedProductTax = isset($formValues['tax_product_class'])
  153. ? $formValues['tax_product_class']
  154. : $selected;
  155. $fieldset->addField(
  156. 'tax_product_class',
  157. 'editablemultiselect',
  158. [
  159. 'name' => 'tax_product_class',
  160. 'label' => __('Product Tax Class'),
  161. 'class' => 'required-entry',
  162. 'values' => $options,
  163. 'value' => $selectedProductTax,
  164. 'required' => true,
  165. 'select_config' => $selectConfig
  166. ],
  167. false,
  168. true
  169. );
  170. $selectConfig = $this->getTaxRatesSelectConfig($formValues);
  171. $fieldset->addField(
  172. 'tax_rate',
  173. 'editablemultiselect',
  174. [
  175. 'name' => 'tax_rate',
  176. 'label' => __('Tax Rate'),
  177. 'class' => 'required-entry',
  178. 'values' => $this->rateSource->toOptionArray(),
  179. 'value' => isset($formValues['tax_rate']) ? $formValues['tax_rate'] : [],
  180. 'required' => true,
  181. 'element_js_class' => 'TaxRateEditableMultiselect',
  182. 'select_config' => $selectConfig
  183. ]
  184. );
  185. $fieldset->addField(
  186. 'priority',
  187. 'text',
  188. [
  189. 'name' => 'priority',
  190. 'label' => __('Priority'),
  191. 'class' => 'validate-not-negative-number',
  192. 'value' => isset($formValues['priority']) ? $formValues['priority'] : 0,
  193. 'required' => true,
  194. 'note' => __('Tax rates at the same priority are added, others are compounded.')
  195. ],
  196. false,
  197. true
  198. );
  199. $fieldset->addField(
  200. 'calculate_subtotal',
  201. 'checkbox',
  202. [
  203. 'name' => 'calculate_subtotal',
  204. 'label' => __('Calculate Off Subtotal Only'),
  205. 'onclick' => 'this.value = this.checked ? 1 : 0;',
  206. 'checked' => isset($formValues['calculate_subtotal']) ? $formValues['calculate_subtotal'] : 0
  207. ],
  208. false,
  209. true
  210. );
  211. $fieldset->addField(
  212. 'position',
  213. 'text',
  214. [
  215. 'name' => 'position',
  216. 'label' => __('Sort Order'),
  217. 'class' => 'validate-not-negative-number',
  218. 'value' => isset($formValues['position']) ? $formValues['position'] : 0,
  219. 'required' => true
  220. ],
  221. false,
  222. true
  223. );
  224. if (isset($taxRule)) {
  225. $fieldset->addField(
  226. 'tax_calculation_rule_id',
  227. 'hidden',
  228. ['name' => 'tax_calculation_rule_id', 'value' => $taxRule->getId(), 'no_span' => true]
  229. );
  230. }
  231. $form->setAction($this->getUrl('tax/rule/save'));
  232. $form->setUseContainer($this->getUseContainer());
  233. $this->setForm($form);
  234. return parent::_prepareForm();
  235. }
  236. /**
  237. * Retrieve configuration options for tax rates editable multiselect
  238. *
  239. * @param array $formValues
  240. * @return array
  241. * @since 100.2.0
  242. */
  243. public function getTaxRatesSelectConfig($formValues)
  244. {
  245. $config = [
  246. 'is_entity_editable' => true,
  247. 'selected_values' => isset($formValues['tax_rate']) ? $formValues['tax_rate'] : []
  248. ];
  249. return $config;
  250. }
  251. /**
  252. * Retrieve configuration options for tax class editable multiselect
  253. *
  254. * @param string $classType
  255. * @return array
  256. */
  257. public function getTaxClassSelectConfig($classType)
  258. {
  259. $config = [
  260. 'new_url' => $this->getUrl('tax/tax/ajaxSave/'),
  261. 'save_url' => $this->getUrl('tax/tax/ajaxSave/'),
  262. 'delete_url' => $this->getUrl('tax/tax/ajaxDelete/'),
  263. 'delete_confirm_message' => __('Do you really want to delete this tax class?'),
  264. 'target_select_id' => 'tax_' . strtolower($classType) . '_class',
  265. 'add_button_caption' => __('Add New Tax Class'),
  266. 'submit_data' => ['class_type' => $classType, 'form_key' => $this->formKey->getFormKey()],
  267. 'entity_id_name' => 'class_id',
  268. 'entity_value_name' => 'class_name',
  269. 'is_entity_editable' => true,
  270. ];
  271. return $config;
  272. }
  273. /**
  274. * Retrieve Tax Rate delete URL
  275. *
  276. * @return string
  277. */
  278. public function getTaxRateDeleteUrl()
  279. {
  280. return $this->getUrl('tax/rate/ajaxDelete/');
  281. }
  282. /**
  283. * Retrieve Tax Rate save URL
  284. *
  285. * @return string
  286. */
  287. public function getTaxRateSaveUrl()
  288. {
  289. return $this->getUrl('tax/rate/ajaxSave/');
  290. }
  291. /**
  292. * Retrieve Tax Rate load URL
  293. *
  294. * @return string
  295. */
  296. public function getTaxRateLoadUrl()
  297. {
  298. return $this->getUrl('tax/rate/ajaxLoad/');
  299. }
  300. /**
  301. * Retrieve next Tax Rates page URL
  302. *
  303. * @return string
  304. * @since 100.2.0
  305. */
  306. public function getTaxRatesPageUrl()
  307. {
  308. return $this->getUrl('tax/rule/ajaxLoadRates/');
  309. }
  310. /**
  311. * Extract tax rule data in a format which is
  312. *
  313. * @param \Magento\Tax\Api\Data\TaxRuleInterface $taxRule
  314. * @return array
  315. */
  316. protected function extractTaxRuleData($taxRule)
  317. {
  318. $taxRuleData = [
  319. 'code' => $taxRule->getCode(),
  320. 'tax_customer_class' => $taxRule->getCustomerTaxClassIds(),
  321. 'tax_product_class' => $taxRule->getProductTaxClassIds(),
  322. 'tax_rate' => $taxRule->getTaxRateIds(),
  323. 'priority' => $taxRule->getPriority(),
  324. 'position' => $taxRule->getPosition(),
  325. 'calculate_subtotal' => $taxRule->getCalculateSubtotal(),
  326. ];
  327. return $taxRuleData;
  328. }
  329. }