Collection.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Tax rate collection
  8. */
  9. namespace Magento\Tax\Model\ResourceModel\Calculation\Rate;
  10. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  11. {
  12. /**
  13. * Value of fetched from DB of rules per cycle
  14. */
  15. const TAX_RULES_CHUNK_SIZE = 1000;
  16. /**
  17. * @var \Magento\Store\Model\StoreManagerInterface
  18. */
  19. protected $_storeManager;
  20. /**
  21. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  22. * @param \Psr\Log\LoggerInterface $logger
  23. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  24. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  25. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  26. * @param mixed $connection
  27. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  28. */
  29. public function __construct(
  30. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  31. \Psr\Log\LoggerInterface $logger,
  32. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  33. \Magento\Framework\Event\ManagerInterface $eventManager,
  34. \Magento\Store\Model\StoreManagerInterface $storeManager,
  35. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  36. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  37. ) {
  38. $this->_storeManager = $storeManager;
  39. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  40. }
  41. /**
  42. * Resource initialization
  43. *
  44. * @return void
  45. */
  46. protected function _construct()
  47. {
  48. $this->_init(
  49. \Magento\Tax\Model\Calculation\Rate::class,
  50. \Magento\Tax\Model\ResourceModel\Calculation\Rate::class
  51. );
  52. }
  53. /**
  54. * Join country table to result
  55. *
  56. * @return $this
  57. */
  58. public function joinCountryTable()
  59. {
  60. $this->_select->join(
  61. ['country_table' => $this->getTable('directory_country')],
  62. 'main_table.tax_country_id = country_table.country_id',
  63. ['country_name' => 'iso2_code']
  64. );
  65. return $this;
  66. }
  67. /**
  68. * Join Region Table
  69. *
  70. * @return $this
  71. */
  72. public function joinRegionTable()
  73. {
  74. $this->_select->joinLeft(
  75. ['region_table' => $this->getTable('directory_country_region')],
  76. 'main_table.tax_region_id = region_table.region_id',
  77. ['region_name' => 'code']
  78. );
  79. return $this;
  80. }
  81. /**
  82. * Join rate title for specified store
  83. *
  84. * @param \Magento\Store\Model\Store|string|int $store
  85. * @return $this
  86. */
  87. public function joinTitle($store = null)
  88. {
  89. $storeId = (int)$this->_storeManager->getStore($store)->getId();
  90. $this->_select->joinLeft(
  91. ['title_table' => $this->getTable('tax_calculation_rate_title')],
  92. $this->getConnection()->quoteInto(
  93. 'main_table.tax_calculation_rate_id = title_table.tax_calculation_rate_id AND title_table.store_id = ?',
  94. $storeId
  95. ),
  96. ['title' => 'value']
  97. );
  98. return $this;
  99. }
  100. /**
  101. * Joins store titles for rates
  102. *
  103. * @return $this
  104. */
  105. public function joinStoreTitles()
  106. {
  107. $storeCollection = $this->_storeManager->getStores(true);
  108. foreach ($storeCollection as $store) {
  109. $tableAlias = sprintf('title_table_%s', $store->getId());
  110. $joinCondition = implode(
  111. ' AND ',
  112. [
  113. "main_table.tax_calculation_rate_id = {$tableAlias}.tax_calculation_rate_id",
  114. $this->getConnection()->quoteInto($tableAlias . '.store_id = ?', $store->getId())
  115. ]
  116. );
  117. $this->_select->joinLeft(
  118. [$tableAlias => $this->getTable('tax_calculation_rate_title')],
  119. $joinCondition,
  120. [$tableAlias => 'value']
  121. );
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Add rate filter
  127. *
  128. * @param int $rateId
  129. * @return $this
  130. */
  131. public function addRateFilter($rateId)
  132. {
  133. if (is_int($rateId) && $rateId > 0) {
  134. return $this->addFieldToFilter('main_table.tax_rate_id', $rateId);
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Retrieve option array
  140. *
  141. * @return array
  142. */
  143. public function toOptionArray()
  144. {
  145. return $this->_toOptionArray('tax_calculation_rate_id', 'code');
  146. }
  147. /**
  148. * Retrieve option hash
  149. *
  150. * @return array
  151. */
  152. public function toOptionHash()
  153. {
  154. return $this->_toOptionHash('tax_calculation_rate_id', 'code');
  155. }
  156. /**
  157. * Convert items array to hash for select options
  158. * using fetchItem method
  159. *
  160. * @see fetchItem()
  161. *
  162. * @return array
  163. */
  164. public function toOptionHashOptimized()
  165. {
  166. $result = [];
  167. while ($item = $this->fetchItem()) {
  168. $result[$item->getData('tax_calculation_rate_id')] = $item->getData('code');
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Get rates array without memory leak
  174. *
  175. * @return array
  176. */
  177. public function getOptionRates()
  178. {
  179. $size = self::TAX_RULES_CHUNK_SIZE;
  180. $page = 1;
  181. $rates = [];
  182. do {
  183. $offset = $size * ($page - 1);
  184. $this->getSelect()->reset();
  185. $this->getSelect()
  186. ->from(
  187. ['rates' => $this->getMainTable()],
  188. ['tax_calculation_rate_id', 'code']
  189. )
  190. ->limit($size, $offset);
  191. $rates = array_merge($rates, $this->toOptionArray());
  192. $this->clear();
  193. $page++;
  194. } while ($this->getSize() > $offset);
  195. return $rates;
  196. }
  197. }