Tax.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Block\Renderer\Weee;
  7. use Magento\Framework\Data\Form\Element\AbstractElement;
  8. /**
  9. * Adminhtml weee tax item renderer
  10. */
  11. class Tax extends \Magento\Backend\Block\Widget implements
  12. \Magento\Framework\Data\Form\Element\Renderer\RendererInterface
  13. {
  14. /**
  15. * @var AbstractElement|null
  16. */
  17. protected $_element = null;
  18. /**
  19. * @var array|null
  20. */
  21. protected $_countries = null;
  22. /**
  23. * @var array|null
  24. */
  25. protected $_websites = null;
  26. /**
  27. * @var string
  28. */
  29. protected $_template = 'Magento_Weee::renderer/tax.phtml';
  30. /**
  31. * Core registry
  32. *
  33. * @var \Magento\Framework\Registry
  34. */
  35. protected $_coreRegistry;
  36. /**
  37. * @var \Magento\Directory\Model\Config\Source\Country
  38. */
  39. protected $_sourceCountry;
  40. /**
  41. * @var \Magento\Directory\Helper\Data
  42. */
  43. protected $_directoryHelper;
  44. /**
  45. * @param \Magento\Backend\Block\Template\Context $context
  46. * @param \Magento\Directory\Model\Config\Source\Country $sourceCountry
  47. * @param \Magento\Directory\Helper\Data $directoryHelper
  48. * @param \Magento\Framework\Registry $registry
  49. * @param array $data
  50. */
  51. public function __construct(
  52. \Magento\Backend\Block\Template\Context $context,
  53. \Magento\Directory\Model\Config\Source\Country $sourceCountry,
  54. \Magento\Directory\Helper\Data $directoryHelper,
  55. \Magento\Framework\Registry $registry,
  56. array $data = []
  57. ) {
  58. $this->_sourceCountry = $sourceCountry;
  59. $this->_directoryHelper = $directoryHelper;
  60. $this->_coreRegistry = $registry;
  61. parent::__construct($context, $data);
  62. }
  63. /**
  64. * @return \Magento\Framework\DataObject
  65. */
  66. public function getProduct()
  67. {
  68. return $this->_coreRegistry->registry('product');
  69. }
  70. /**
  71. * @param AbstractElement $element
  72. * @return string
  73. */
  74. public function render(AbstractElement $element)
  75. {
  76. $this->setElement($element);
  77. return $this->toHtml();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function _prepareLayout()
  83. {
  84. $this->addChild(
  85. 'add_button',
  86. \Magento\Backend\Block\Widget\Button::class,
  87. ['label' => __('Add Tax'), 'data_attribute' => ['action' => 'add-fpt-item'], 'class' => 'add']
  88. );
  89. $this->addChild(
  90. 'delete_button',
  91. \Magento\Backend\Block\Widget\Button::class,
  92. [
  93. 'label' => __('Delete Tax'),
  94. 'data_attribute' => ['action' => 'delete-fpt-item'],
  95. 'class' => 'delete'
  96. ]
  97. );
  98. return parent::_prepareLayout();
  99. }
  100. /**
  101. * @param AbstractElement $element
  102. * @return $this
  103. */
  104. public function setElement(AbstractElement $element)
  105. {
  106. $this->_element = $element;
  107. return $this;
  108. }
  109. /**
  110. * @return AbstractElement|null
  111. */
  112. public function getElement()
  113. {
  114. return $this->_element;
  115. }
  116. /**
  117. * @return array
  118. */
  119. public function getValues()
  120. {
  121. $values = [];
  122. $data = $this->getElement()->getEscapedValue();
  123. if (is_array($data) && count($data)) {
  124. usort($data, [$this, '_sortWeeeTaxes']);
  125. $values = $data;
  126. }
  127. return $values;
  128. }
  129. /**
  130. * @param array $firstItem
  131. * @param array $secondItem
  132. * @return int
  133. */
  134. protected function _sortWeeeTaxes($firstItem, $secondItem)
  135. {
  136. if ($firstItem['website_id'] != $secondItem['website_id']) {
  137. return $firstItem['website_id'] < $secondItem['website_id'] ? -1 : 1;
  138. }
  139. if ($firstItem['country'] != $secondItem['country']) {
  140. return $firstItem['country'] < $secondItem['country'] ? -1 : 1;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * @return int
  146. */
  147. public function getWebsiteCount()
  148. {
  149. return count($this->getWebsites());
  150. }
  151. /**
  152. * @return bool
  153. */
  154. public function isMultiWebsites()
  155. {
  156. return !$this->_storeManager->hasSingleStore();
  157. }
  158. /**
  159. * @return array|null
  160. */
  161. public function getCountries()
  162. {
  163. if (null === $this->_countries) {
  164. $this->_countries = $this->_sourceCountry->toOptionArray();
  165. }
  166. return $this->_countries;
  167. }
  168. /**
  169. * @return array|null
  170. */
  171. public function getWebsites()
  172. {
  173. if (null !== $this->_websites) {
  174. return $this->_websites;
  175. }
  176. $websites = [];
  177. $websites[0] = [
  178. 'name' => __('All Websites'),
  179. 'currency' => $this->_directoryHelper->getBaseCurrencyCode(),
  180. ];
  181. if (!$this->_storeManager->hasSingleStore() && !$this->getElement()->getEntityAttribute()->isScopeGlobal()) {
  182. if ($storeId = $this->getProduct()->getStoreId()) {
  183. $website = $this->_storeManager->getStore($storeId)->getWebsite();
  184. $websites[$website->getId()] = [
  185. 'name' => $website->getName(),
  186. 'currency' => $website->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE),
  187. ];
  188. } else {
  189. foreach ($this->_storeManager->getWebsites() as $website) {
  190. if (!in_array($website->getId(), $this->getProduct()->getWebsiteIds())) {
  191. continue;
  192. }
  193. $websites[$website->getId()] = [
  194. 'name' => $website->getName(),
  195. 'currency' => $website->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE),
  196. ];
  197. }
  198. }
  199. }
  200. $this->_websites = $websites;
  201. return $this->_websites;
  202. }
  203. /**
  204. * @return string
  205. */
  206. public function getAddButtonHtml()
  207. {
  208. return $this->getChildHtml('add_button');
  209. }
  210. }