Product.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\TaxClass\Source;
  7. use Magento\Framework\DB\Ddl\Table;
  8. use Magento\Tax\Api\TaxClassManagementInterface;
  9. use Magento\Tax\Model\ClassModel;
  10. /**
  11. * Product tax class source model.
  12. */
  13. class Product extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
  14. {
  15. /**
  16. * @var \Magento\Tax\Api\TaxClassRepositoryInterface
  17. */
  18. protected $_taxClassRepository;
  19. /**
  20. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  21. */
  22. protected $_searchCriteriaBuilder;
  23. /**
  24. * @var \Magento\Framework\Api\FilterBuilder
  25. */
  26. protected $_filterBuilder;
  27. /**
  28. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory
  29. */
  30. protected $_optionFactory;
  31. /**
  32. * Initialize dependencies.
  33. *
  34. * @param \Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory $classesFactory
  35. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $optionFactory
  36. * @param \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepository
  37. * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
  38. * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
  39. */
  40. public function __construct(
  41. \Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory $classesFactory,
  42. \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $optionFactory,
  43. \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepository,
  44. \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
  45. \Magento\Framework\Api\FilterBuilder $filterBuilder
  46. ) {
  47. $this->_classesFactory = $classesFactory;
  48. $this->_optionFactory = $optionFactory;
  49. $this->_taxClassRepository = $taxClassRepository;
  50. $this->_filterBuilder = $filterBuilder;
  51. $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
  52. }
  53. /**
  54. * Retrieve all product tax class options.
  55. *
  56. * @param bool $withEmpty
  57. * @return array
  58. */
  59. public function getAllOptions($withEmpty = true)
  60. {
  61. if (!$this->_options) {
  62. $filter = $this->_filterBuilder
  63. ->setField(ClassModel::KEY_TYPE)
  64. ->setValue(TaxClassManagementInterface::TYPE_PRODUCT)
  65. ->create();
  66. $searchCriteria = $this->_searchCriteriaBuilder->addFilters([$filter])->create();
  67. $searchResults = $this->_taxClassRepository->getList($searchCriteria);
  68. foreach ($searchResults->getItems() as $taxClass) {
  69. $this->_options[] = [
  70. 'value' => $taxClass->getClassId(),
  71. 'label' => $taxClass->getClassName(),
  72. ];
  73. }
  74. }
  75. if ($withEmpty) {
  76. if (!$this->_options) {
  77. return [['value' => '0', 'label' => __('None')]];
  78. } else {
  79. return array_merge([['value' => '0', 'label' => __('None')]], $this->_options);
  80. }
  81. }
  82. return $this->_options;
  83. }
  84. /**
  85. * Get a text for option value
  86. *
  87. * @param string|integer $value
  88. * @return string
  89. */
  90. public function getOptionText($value)
  91. {
  92. $options = $this->getAllOptions();
  93. foreach ($options as $item) {
  94. if ($item['value'] == $value) {
  95. return $item['label'];
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * Retrieve flat column definition
  102. *
  103. * @return array
  104. */
  105. public function getFlatColumns()
  106. {
  107. $attributeCode = $this->getAttribute()->getAttributeCode();
  108. return [
  109. $attributeCode => [
  110. 'unsigned' => true,
  111. 'default' => null,
  112. 'extra' => null,
  113. 'type' => Table::TYPE_INTEGER,
  114. 'nullable' => true,
  115. 'comment' => $attributeCode . ' tax column',
  116. ],
  117. ];
  118. }
  119. /**
  120. * Retrieve Select for update attribute value in flat table
  121. *
  122. * @param int $store
  123. * @return \Magento\Framework\DB\Select|null
  124. */
  125. public function getFlatUpdateSelect($store)
  126. {
  127. /** @var $option \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option */
  128. $option = $this->_optionFactory->create();
  129. return $option->getFlatUpdateSelect($this->getAttribute(), $store, false);
  130. }
  131. }