TaxRateCollection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Tax\Api\TaxRateRepositoryInterface;
  8. use Magento\Framework\Data\Collection\EntityFactory;
  9. use Magento\Framework\Api\AbstractServiceCollection;
  10. use Magento\Framework\Api\FilterBuilder;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\Framework\Api\SortOrderBuilder;
  13. use Magento\Tax\Api\Data\TaxRateInterface as TaxRate;
  14. /**
  15. * Tax rate collection for a grid backed by Services
  16. */
  17. class TaxRateCollection extends AbstractServiceCollection
  18. {
  19. /**
  20. * @var TaxRateRepositoryInterface
  21. */
  22. protected $taxRateRepository;
  23. /**
  24. * @var \Magento\Tax\Model\Calculation\Rate\Converter
  25. */
  26. protected $rateConverter;
  27. /**
  28. * Initialize dependencies.
  29. *
  30. * @param EntityFactory $entityFactory
  31. * @param FilterBuilder $filterBuilder
  32. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  33. * @param SortOrderBuilder $sortOrderBuilder
  34. * @param TaxRateRepositoryInterface $rateService
  35. * @param \Magento\Tax\Model\Calculation\Rate\Converter $rateConverter
  36. */
  37. public function __construct(
  38. EntityFactory $entityFactory,
  39. FilterBuilder $filterBuilder,
  40. SearchCriteriaBuilder $searchCriteriaBuilder,
  41. SortOrderBuilder $sortOrderBuilder,
  42. TaxRateRepositoryInterface $rateService,
  43. \Magento\Tax\Model\Calculation\Rate\Converter $rateConverter
  44. ) {
  45. parent::__construct($entityFactory, $filterBuilder, $searchCriteriaBuilder, $sortOrderBuilder);
  46. $this->taxRateRepository = $rateService;
  47. $this->rateConverter = $rateConverter;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function loadData($printQuery = false, $logQuery = false)
  53. {
  54. if (!$this->isLoaded()) {
  55. $searchCriteria = $this->getSearchCriteria();
  56. $searchResults = $this->taxRateRepository->getList($searchCriteria);
  57. $this->_totalRecords = $searchResults->getTotalCount();
  58. foreach ($searchResults->getItems() as $taxRate) {
  59. $this->_addItem($this->createTaxRateCollectionItem($taxRate));
  60. }
  61. $this->_setIsLoaded();
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Creates a collection item that represents a tax rate for the tax rates grid.
  67. *
  68. * @param TaxRate $taxRate Input data for creating the item.
  69. * @return \Magento\Framework\DataObject Collection item that represents a tax rate
  70. */
  71. protected function createTaxRateCollectionItem(TaxRate $taxRate)
  72. {
  73. $collectionItem = new \Magento\Framework\DataObject();
  74. $collectionItem->setTaxCalculationRateId($taxRate->getId());
  75. $collectionItem->setCode($taxRate->getCode());
  76. $collectionItem->setTaxCountryId($taxRate->getTaxCountryId());
  77. $collectionItem->setTaxRegionId($taxRate->getTaxRegionId());
  78. $collectionItem->setRegionName($taxRate->getRegionName());
  79. $collectionItem->setTaxPostcode($taxRate->getTaxPostcode());
  80. $collectionItem->setRate($taxRate->getRate());
  81. $collectionItem->setTitles($this->rateConverter->createTitleArrayFromServiceObject($taxRate));
  82. if ($taxRate->getZipTo() != null && $taxRate->getZipFrom() != null) {
  83. /* must be a "1" for existing code (e.g. JavaScript) to work */
  84. $collectionItem->setZipIsRange("1");
  85. $collectionItem->setZipFrom($taxRate->getZipFrom());
  86. $collectionItem->setZipTo($taxRate->getZipTo());
  87. } else {
  88. $collectionItem->setZipIsRange(null);
  89. $collectionItem->setZipFrom(null);
  90. $collectionItem->setZipTo(null);
  91. }
  92. return $collectionItem;
  93. }
  94. }