Collection.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\ResourceModel\Calculation\Rule;
  7. /**
  8. * Tax rule collection
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  13. {
  14. /**
  15. * Resource initialization
  16. *
  17. * @return void
  18. */
  19. protected function _construct()
  20. {
  21. $this->_init(
  22. \Magento\Tax\Model\Calculation\Rule::class,
  23. \Magento\Tax\Model\ResourceModel\Calculation\Rule::class
  24. );
  25. }
  26. /**
  27. * Process loaded collection data
  28. *
  29. * @return $this
  30. */
  31. protected function _afterLoadData()
  32. {
  33. parent::_afterLoadData();
  34. $this->addCustomerTaxClassesToResult();
  35. $this->addProductTaxClassesToResult();
  36. $this->addRatesToResult();
  37. return $this;
  38. }
  39. /**
  40. * Join calculation data to result
  41. *
  42. * @param string $alias table alias
  43. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
  44. */
  45. public function joinCalculationData($alias)
  46. {
  47. $this->getSelect()->joinLeft(
  48. [$alias => $this->getTable('tax_calculation')],
  49. "main_table.tax_calculation_rule_id = {$alias}.tax_calculation_rule_id",
  50. []
  51. );
  52. $this->getSelect()->group('main_table.tax_calculation_rule_id');
  53. return $this;
  54. }
  55. /**
  56. * Join tax data to collection
  57. *
  58. * @param string $itemTable
  59. * @param string $primaryJoinField
  60. * @param string $secondaryJoinField
  61. * @param string $titleField
  62. * @param string $dataField
  63. * @param string $dataTitleField
  64. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
  65. */
  66. protected function _add(
  67. $itemTable,
  68. $primaryJoinField,
  69. $secondaryJoinField,
  70. $titleField,
  71. $dataField,
  72. $dataTitleField = ''
  73. ) {
  74. $children = [];
  75. foreach ($this as $rule) {
  76. $children[$rule->getId()] = [];
  77. }
  78. if (!empty($children)) {
  79. $joinCondition = sprintf('item.%s = calculation.%s', $secondaryJoinField, $primaryJoinField);
  80. $select = $this->getConnection()->select()->from(
  81. ['calculation' => $this->getTable('tax_calculation')],
  82. ['calculation.tax_calculation_rule_id']
  83. )->join(
  84. ['item' => $this->getTable($itemTable)],
  85. $joinCondition,
  86. ["item.{$titleField}", "item.{$secondaryJoinField}"]
  87. )->where(
  88. 'calculation.tax_calculation_rule_id IN (?)',
  89. array_keys($children)
  90. )->distinct(
  91. true
  92. );
  93. $data = $this->getConnection()->fetchAll($select);
  94. foreach ($data as $row) {
  95. $children[$row['tax_calculation_rule_id']][$row[$secondaryJoinField]] = $row[$titleField];
  96. }
  97. }
  98. foreach ($this as $rule) {
  99. if (isset($children[$rule->getId()])) {
  100. $rule->setData($dataField, array_keys($children[$rule->getId()]));
  101. if (!empty($dataTitleField)) {
  102. $rule->setData($dataTitleField, $children[$rule->getId()]);
  103. }
  104. }
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Add product tax classes to result
  110. *
  111. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
  112. */
  113. public function addProductTaxClassesToResult()
  114. {
  115. return $this->_add('tax_class', 'product_tax_class_id', 'class_id', 'class_name', 'product_tax_classes');
  116. }
  117. /**
  118. * Add customer tax classes to result
  119. *
  120. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
  121. */
  122. public function addCustomerTaxClassesToResult()
  123. {
  124. return $this->_add('tax_class', 'customer_tax_class_id', 'class_id', 'class_name', 'customer_tax_classes');
  125. }
  126. /**
  127. * Add rates to result
  128. *
  129. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
  130. */
  131. public function addRatesToResult()
  132. {
  133. return $this->_add(
  134. 'tax_calculation_rate',
  135. 'tax_calculation_rate_id',
  136. 'tax_calculation_rate_id',
  137. 'code',
  138. 'tax_rates',
  139. 'tax_rates_codes'
  140. );
  141. }
  142. /**
  143. * Add class type filter
  144. *
  145. * @param string $type
  146. * @param int $id
  147. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection
  148. * @throws \Magento\Framework\Exception\LocalizedException
  149. */
  150. public function setClassTypeFilter($type, $id)
  151. {
  152. switch ($type) {
  153. case \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT:
  154. $field = 'cd.product_tax_class_id';
  155. break;
  156. case \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER:
  157. $field = 'cd.customer_tax_class_id';
  158. break;
  159. default:
  160. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid type supplied'));
  161. break;
  162. }
  163. $this->joinCalculationData('cd');
  164. $this->addFieldToFilter($field, $id);
  165. return $this;
  166. }
  167. }