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