TaxRateManagement.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\FilterBuilder;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Tax\Api\TaxRateManagementInterface;
  10. use Magento\Tax\Api\TaxRateRepositoryInterface;
  11. use Magento\Tax\Api\TaxRuleRepositoryInterface;
  12. class TaxRateManagement implements TaxRateManagementInterface
  13. {
  14. /**
  15. * @var SearchCriteriaBuilder
  16. */
  17. protected $searchCriteriaBuilder;
  18. /**
  19. * @var FilterBuilder
  20. */
  21. protected $filterBuilder;
  22. /**
  23. * @var TaxRuleRepositoryInterface
  24. */
  25. protected $taxRuleRepository;
  26. /**
  27. * @var TaxRateRepositoryInterface
  28. */
  29. protected $taxRateRepository;
  30. /**
  31. * @param TaxRuleRepositoryInterface $taxRuleRepository
  32. * @param TaxRateRepositoryInterface $taxRateRepository
  33. * @param FilterBuilder $filterBuilder
  34. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  35. */
  36. public function __construct(
  37. TaxRuleRepositoryInterface $taxRuleRepository,
  38. TaxRateRepositoryInterface $taxRateRepository,
  39. FilterBuilder $filterBuilder,
  40. SearchCriteriaBuilder $searchCriteriaBuilder
  41. ) {
  42. $this->taxRuleRepository = $taxRuleRepository;
  43. $this->taxRateRepository = $taxRateRepository;
  44. $this->filterBuilder = $filterBuilder;
  45. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId)
  51. {
  52. $this->searchCriteriaBuilder->addFilters(
  53. [
  54. $this->filterBuilder
  55. ->setField('customer_tax_class_ids')
  56. ->setValue([$customerTaxClassId])
  57. ->create(),
  58. ]
  59. );
  60. $this->searchCriteriaBuilder->addFilters(
  61. [
  62. $this->filterBuilder
  63. ->setField('product_tax_class_ids')
  64. ->setValue([$productTaxClassId])
  65. ->create(),
  66. ]
  67. );
  68. $searchResults = $this->taxRuleRepository->getList($this->searchCriteriaBuilder->create());
  69. $taxRules = $searchResults->getItems();
  70. $rates = [];
  71. foreach ($taxRules as $taxRule) {
  72. $rateIds = $taxRule->getTaxRateIds();
  73. if (!empty($rateIds)) {
  74. foreach ($rateIds as $rateId) {
  75. $rates[] = $this->taxRateRepository->get($rateId);
  76. }
  77. }
  78. }
  79. return $rates;
  80. }
  81. }