Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RateRepository.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Directory\Model\CountryFactory;
18 
23 {
24  const MESSAGE_TAX_RATE_ID_IS_NOT_ALLOWED = 'id is not expected for this request.';
25 
31  protected $converter;
32 
38  protected $rateRegistry;
39 
43  private $taxRateSearchResultsFactory;
44 
48  private $rateFactory;
49 
53  protected $countryFactory;
54 
58  protected $regionFactory;
59 
63  protected $resourceModel;
64 
68  protected $joinProcessor;
69 
73  private $collectionProcessor;
74 
86  public function __construct(
89  \Magento\Tax\Api\Data\TaxRuleSearchResultsInterfaceFactory $taxRateSearchResultsFactory,
90  RateFactory $rateFactory,
91  CountryFactory $countryFactory,
93  \Magento\Tax\Model\ResourceModel\Calculation\Rate $rateResource,
94  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
95  CollectionProcessorInterface $collectionProcessor = null
96  ) {
97  $this->converter = $converter;
98  $this->rateRegistry = $rateRegistry;
99  $this->taxRateSearchResultsFactory = $taxRateSearchResultsFactory;
100  $this->rateFactory = $rateFactory;
101  $this->countryFactory = $countryFactory;
102  $this->regionFactory = $regionFactory;
103  $this->resourceModel = $rateResource;
104  $this->joinProcessor = $joinProcessor;
105  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
106  }
107 
111  public function save(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
112  {
113  if ($taxRate->getId()) {
114  $this->rateRegistry->retrieveTaxRate($taxRate->getId());
115  }
116  $this->validate($taxRate);
117  $taxRateTitles = $this->converter->createTitleArrayFromServiceObject($taxRate);
118  try {
119  $this->resourceModel->save($taxRate);
120  $taxRate->saveTitles($taxRateTitles);
121  } catch (LocalizedException $e) {
122  throw $e;
123  }
124  $this->rateRegistry->registerTaxRate($taxRate);
125  return $taxRate;
126  }
127 
131  public function get($rateId)
132  {
133  return $this->rateRegistry->retrieveTaxRate($rateId);
134  }
135 
140  {
141  return $this->resourceModel->delete($taxRate);
142  }
143 
147  public function deleteById($rateId)
148  {
149  $rateModel = $this->rateRegistry->retrieveTaxRate($rateId);
150  $this->delete($rateModel);
151  $this->rateRegistry->removeTaxRate($rateId);
152  return true;
153  }
154 
158  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
159  {
161  $collection = $this->rateFactory->create()->getCollection();
162  $this->joinProcessor->process($collection);
163  $collection->joinRegionTable();
164 
165  $this->collectionProcessor->process($searchCriteria, $collection);
166  $taxRate = [];
167 
169  foreach ($collection as $taxRateModel) {
170  $taxRate[] = $taxRateModel;
171  }
172 
173  return $this->taxRateSearchResultsFactory->create()
174  ->setItems($taxRate)
175  ->setTotalCount($collection->getSize())
176  ->setSearchCriteria($searchCriteria);
177  }
178 
189  {
190  $fields = [];
191  $conditions = [];
192  foreach ($filterGroup->getFilters() as $filter) {
193  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
194  $fields[] = $this->translateField($filter->getField());
195  $conditions[] = [$condition => $filter->getValue()];
196  }
197  if ($fields) {
198  $collection->addFieldToFilter($fields, $conditions);
199  }
200  }
201 
209  protected function translateField($field)
210  {
211  switch ($field) {
213  return 'region_table.code';
214  default:
215  return "main_table." . $field;
216  }
217  }
218 
229  private function validate(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
230  {
231  $exception = new InputException();
232 
233  $countryCode = $taxRate->getTaxCountryId();
234  if (!\Zend_Validate::is($countryCode, 'NotEmpty')) {
235  $exception->addError(__('"%fieldName" is required. Enter and try again.', ['fieldName' => 'country_id']));
237  $this->countryFactory->create()->loadByCode($countryCode)->getId(),
238  'NotEmpty'
239  )) {
240  $exception->addError(
241  __(
242  'Invalid value of "%value" provided for the %fieldName field.',
243  [
244  'fieldName' => 'country_id',
245  'value' => $countryCode
246  ]
247  )
248  );
249  }
250 
251  $regionCode = $taxRate->getTaxRegionId();
252  // if regionCode eq 0 (all regions *), do not validate with existing region list
253  if (\Zend_Validate::is($regionCode, 'NotEmpty') &&
254  $regionCode != "0" && !\Zend_Validate::is(
255  $this->regionFactory->create()->load($regionCode)->getId(),
256  'NotEmpty'
257  )
258  ) {
259  $exception->addError(
260  __(
261  'Invalid value of "%value" provided for the %fieldName field.',
262  ['fieldName' => 'region_id', 'value' => $regionCode]
263  )
264  );
265  }
266 
267  if (!is_numeric($taxRate->getRate()) || $taxRate->getRate() < 0) {
268  $exception->addError(
269  __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'percentage_rate'])
270  );
271  }
272 
273  if (!\Zend_Validate::is(trim($taxRate->getCode()), 'NotEmpty')) {
274  $exception->addError(__('"%fieldName" is required. Enter and try again.', ['fieldName' => 'code']));
275  }
276 
277  if ($taxRate->getZipIsRange()) {
278  $zipRangeFromTo = [
279  'zip_from' => $taxRate->getZipFrom(),
280  'zip_to' => $taxRate->getZipTo(),
281  ];
282  foreach ($zipRangeFromTo as $key => $value) {
283  if (!is_numeric($value) || $value < 0) {
284  $exception->addError(
285  __(
286  'Invalid value of "%value" provided for the %fieldName field.',
287  ['fieldName' => $key, 'value' => $value]
288  )
289  );
290  }
291  }
292  if ($zipRangeFromTo['zip_from'] > $zipRangeFromTo['zip_to']) {
293  $exception->addError(__('Range To should be equal or greater than Range From.'));
294  }
295  } else {
296  if (!\Zend_Validate::is(trim($taxRate->getTaxPostcode()), 'NotEmpty')) {
297  $exception->addError(__('"%fieldName" is required. Enter and try again.', ['fieldName' => 'postcode']));
298  }
299  }
300 
301  if ($exception->wasErrorAdded()) {
302  throw $exception;
303  }
304  }
305 
312  private function getCollectionProcessor()
313  {
314  if (!$this->collectionProcessor) {
315  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
316  'Magento\Tax\Model\Api\SearchCriteria\TaxRateCollectionProcessor'
317  );
318  }
319  return $this->collectionProcessor;
320  }
321 }
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
$searchCriteria
$value
Definition: gender.phtml:16
addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
__construct(Converter $converter, RateRegistry $rateRegistry, \Magento\Tax\Api\Data\TaxRuleSearchResultsInterfaceFactory $taxRateSearchResultsFactory, RateFactory $rateFactory, CountryFactory $countryFactory, RegionFactory $regionFactory, \Magento\Tax\Model\ResourceModel\Calculation\Rate $rateResource, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, CollectionProcessorInterface $collectionProcessor=null)
$taxRate
Definition: tax_rule.php:12
save(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)