Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxRuleRepository.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Tax\Model;
8 
17 use Magento\Tax\Api\Data\TaxRuleSearchResultsInterfaceFactory;
18 use Magento\Tax\Model\Calculation\RuleFactory;
22 use Magento\Tax\Model\ResourceModel\Calculation\Rule\CollectionFactory;
23 
28 {
32  protected $taxRuleRegistry;
33 
38 
43 
47  protected $collectionFactory;
48 
52  protected $resource;
53 
57  protected $joinProcessor;
58 
62  private $collectionProcessor;
63 
73  public function __construct(
75  TaxRuleSearchResultsInterfaceFactory $searchResultsFactory,
76  RuleFactory $ruleFactory,
77  CollectionFactory $collectionFactory,
79  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
80  CollectionProcessorInterface $collectionProcessor = null
81  ) {
82  $this->taxRuleRegistry = $taxRuleRegistry;
83  $this->taxRuleSearchResultsFactory = $searchResultsFactory;
84  $this->taxRuleModelFactory = $ruleFactory;
85  $this->collectionFactory = $collectionFactory;
86  $this->resource = $resource;
87  $this->joinProcessor = $joinProcessor;
88  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
89  }
90 
94  public function get($ruleId)
95  {
96  return $this->taxRuleRegistry->retrieveTaxRule($ruleId);
97  }
98 
102  public function save(TaxRuleInterface $rule)
103  {
104  try {
105  $ruleId = $rule->getId();
106  if ($ruleId) {
107  $this->taxRuleRegistry->retrieveTaxRule($ruleId);
108  }
109  $this->resource->save($rule);
110  } catch (AlreadyExistsException $e) {
111  throw $e;
112  } catch (NoSuchEntityException $e) {
113  throw $e;
114  } catch (LocalizedException $e) {
115  throw new CouldNotSaveException(__($e->getMessage()));
116  }
117  $this->taxRuleRegistry->registerTaxRule($rule);
118  return $rule;
119  }
120 
124  public function delete(TaxRuleInterface $rule)
125  {
126  $ruleId = $rule->getId();
127  $this->resource->delete($rule);
128  $this->taxRuleRegistry->removeTaxRule($ruleId);
129  return true;
130  }
131 
135  public function deleteById($ruleId)
136  {
137  $rule = $this->taxRuleRegistry->retrieveTaxRule($ruleId);
138  return $this->delete($rule);
139  }
140 
144  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
145  {
146  $searchResults = $this->taxRuleSearchResultsFactory->create();
147  $searchResults->setSearchCriteria($searchCriteria);
148  $collection = $this->collectionFactory->create();
149  $this->joinProcessor->process($collection);
150 
151  $this->collectionProcessor->process($searchCriteria, $collection);
152  $searchResults->setTotalCount($collection->getSize());
153 
154  $searchResults->setItems($collection->getItems());
155  return $searchResults;
156  }
157 
168  {
169  $fields = [];
170  $conditions = [];
171  foreach ($filterGroup->getFilters() as $filter) {
172  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
173  $field = $this->translateField($filter->getField());
174  $fields[] = $field;
175  $conditions[] = [$condition => $filter->getValue()];
176  switch ($field) {
177  case 'rate.tax_calculation_rate_id':
178  $collection->joinCalculationData('rate');
179  break;
180 
181  case 'ctc.customer_tax_class_id':
182  $collection->joinCalculationData('ctc');
183  break;
184 
185  case 'ptc.product_tax_class_id':
186  $collection->joinCalculationData('ptc');
187  break;
188  }
189  }
190  if ($fields) {
191  $collection->addFieldToFilter($fields, $conditions);
192  }
193  }
194 
202  protected function translateField($field)
203  {
204  switch ($field) {
205  case "id":
206  return 'tax_calculation_rule_id';
207  case 'tax_rate_ids':
208  return 'tax_calculation_rate_id';
209  case 'customer_tax_class_ids':
210  return 'cd.customer_tax_class_id';
211  case 'product_tax_class_ids':
212  return 'cd.product_tax_class_id';
213  case 'sort_order':
214  return 'position';
215  default:
216  return $field;
217  }
218  }
219 
226  private function getCollectionProcessor()
227  {
228  if (!$this->collectionProcessor) {
229  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
230  'Magento\Tax\Model\Api\SearchCriteria\TaxRuleCollectionProcessor'
231  );
232  }
233  return $this->collectionProcessor;
234  }
235 }
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
$searchCriteria
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
__construct(TaxRuleRegistry $taxRuleRegistry, TaxRuleSearchResultsInterfaceFactory $searchResultsFactory, RuleFactory $ruleFactory, CollectionFactory $collectionFactory, ResourceRule $resource, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, CollectionProcessorInterface $collectionProcessor=null)