TaxRuleRepository.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model;
  7. use Magento\Framework\Api\Search\FilterGroup;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Exception\CouldNotSaveException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\Exception\AlreadyExistsException;
  13. use Magento\Tax\Api\Data\TaxRuleInterface;
  14. use Magento\Tax\Api\TaxRuleRepositoryInterface;
  15. use Magento\Tax\Api\Data\TaxRuleSearchResultsInterfaceFactory;
  16. use Magento\Tax\Model\Calculation\RuleFactory;
  17. use Magento\Tax\Model\Calculation\TaxRuleRegistry;
  18. use Magento\Tax\Model\ResourceModel\Calculation\Rule as ResourceRule;
  19. use Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection;
  20. use Magento\Tax\Model\ResourceModel\Calculation\Rule\CollectionFactory;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class TaxRuleRepository implements TaxRuleRepositoryInterface
  25. {
  26. /**
  27. * @var TaxRuleRegistry
  28. */
  29. protected $taxRuleRegistry;
  30. /**
  31. * @var TaxRuleSearchResultsInterfaceFactory
  32. */
  33. protected $taxRuleSearchResultsFactory;
  34. /**
  35. * @var RuleFactory
  36. */
  37. protected $taxRuleModelFactory;
  38. /**
  39. * @var CollectionFactory
  40. */
  41. protected $collectionFactory;
  42. /**
  43. * @var ResourceRule
  44. */
  45. protected $resource;
  46. /**
  47. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  48. */
  49. protected $joinProcessor;
  50. /**
  51. * @var CollectionProcessorInterface
  52. */
  53. private $collectionProcessor;
  54. /**
  55. * @param TaxRuleRegistry $taxRuleRegistry
  56. * @param TaxRuleSearchResultsInterfaceFactory $searchResultsFactory
  57. * @param RuleFactory $ruleFactory
  58. * @param CollectionFactory $collectionFactory
  59. * @param ResourceRule $resource
  60. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
  61. * @param CollectionProcessorInterface | null $collectionProcessor
  62. */
  63. public function __construct(
  64. TaxRuleRegistry $taxRuleRegistry,
  65. TaxRuleSearchResultsInterfaceFactory $searchResultsFactory,
  66. RuleFactory $ruleFactory,
  67. CollectionFactory $collectionFactory,
  68. ResourceRule $resource,
  69. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
  70. CollectionProcessorInterface $collectionProcessor = null
  71. ) {
  72. $this->taxRuleRegistry = $taxRuleRegistry;
  73. $this->taxRuleSearchResultsFactory = $searchResultsFactory;
  74. $this->taxRuleModelFactory = $ruleFactory;
  75. $this->collectionFactory = $collectionFactory;
  76. $this->resource = $resource;
  77. $this->joinProcessor = $joinProcessor;
  78. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function get($ruleId)
  84. {
  85. return $this->taxRuleRegistry->retrieveTaxRule($ruleId);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function save(TaxRuleInterface $rule)
  91. {
  92. try {
  93. $ruleId = $rule->getId();
  94. if ($ruleId) {
  95. $this->taxRuleRegistry->retrieveTaxRule($ruleId);
  96. }
  97. $this->resource->save($rule);
  98. } catch (AlreadyExistsException $e) {
  99. throw $e;
  100. } catch (NoSuchEntityException $e) {
  101. throw $e;
  102. } catch (LocalizedException $e) {
  103. throw new CouldNotSaveException(__($e->getMessage()));
  104. }
  105. $this->taxRuleRegistry->registerTaxRule($rule);
  106. return $rule;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function delete(TaxRuleInterface $rule)
  112. {
  113. $ruleId = $rule->getId();
  114. $this->resource->delete($rule);
  115. $this->taxRuleRegistry->removeTaxRule($ruleId);
  116. return true;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function deleteById($ruleId)
  122. {
  123. $rule = $this->taxRuleRegistry->retrieveTaxRule($ruleId);
  124. return $this->delete($rule);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  130. {
  131. $searchResults = $this->taxRuleSearchResultsFactory->create();
  132. $searchResults->setSearchCriteria($searchCriteria);
  133. $collection = $this->collectionFactory->create();
  134. $this->joinProcessor->process($collection);
  135. $this->collectionProcessor->process($searchCriteria, $collection);
  136. $searchResults->setTotalCount($collection->getSize());
  137. $searchResults->setItems($collection->getItems());
  138. return $searchResults;
  139. }
  140. /**
  141. * Helper function that adds a FilterGroup to the collection.
  142. *
  143. * @param FilterGroup $filterGroup
  144. * @param Collection $collection
  145. * @return void
  146. * @deprecated 100.2.0
  147. * @throws \Magento\Framework\Exception\InputException
  148. */
  149. protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
  150. {
  151. $fields = [];
  152. $conditions = [];
  153. foreach ($filterGroup->getFilters() as $filter) {
  154. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  155. $field = $this->translateField($filter->getField());
  156. $fields[] = $field;
  157. $conditions[] = [$condition => $filter->getValue()];
  158. switch ($field) {
  159. case 'rate.tax_calculation_rate_id':
  160. $collection->joinCalculationData('rate');
  161. break;
  162. case 'ctc.customer_tax_class_id':
  163. $collection->joinCalculationData('ctc');
  164. break;
  165. case 'ptc.product_tax_class_id':
  166. $collection->joinCalculationData('ptc');
  167. break;
  168. }
  169. }
  170. if ($fields) {
  171. $collection->addFieldToFilter($fields, $conditions);
  172. }
  173. }
  174. /**
  175. * Translates a field name to a DB column name for use in collection queries.
  176. *
  177. * @param string $field a field name that should be translated to a DB column name.
  178. * @deprecated 100.2.0
  179. * @return string
  180. */
  181. protected function translateField($field)
  182. {
  183. switch ($field) {
  184. case "id":
  185. return 'tax_calculation_rule_id';
  186. case 'tax_rate_ids':
  187. return 'tax_calculation_rate_id';
  188. case 'customer_tax_class_ids':
  189. return 'cd.customer_tax_class_id';
  190. case 'product_tax_class_ids':
  191. return 'cd.product_tax_class_id';
  192. case 'sort_order':
  193. return 'position';
  194. default:
  195. return $field;
  196. }
  197. }
  198. /**
  199. * Retrieve collection processor
  200. *
  201. * @deprecated 100.2.0
  202. * @return CollectionProcessorInterface
  203. */
  204. private function getCollectionProcessor()
  205. {
  206. if (!$this->collectionProcessor) {
  207. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  208. 'Magento\Tax\Model\Api\SearchCriteria\TaxRuleCollectionProcessor'
  209. );
  210. }
  211. return $this->collectionProcessor;
  212. }
  213. }