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
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype https://www.mediotype.com/
*/
namespace Vertex\Tax\Model\Repository;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Tax\Api\TaxClassRepositoryInterface;
use Vertex\Tax\Model\ExceptionLogger;
/**
* Repository of Tax Class Names
*/
class TaxClassNameRepository
{
/** @var SearchCriteriaBuilderFactory */
private $criteriaBuilderFactory;
/** @var GroupRepositoryInterface */
private $customerGroupRepository;
/** @var ExceptionLogger */
private $logger;
/** @var TaxClassRepositoryInterface */
private $repository;
/**
* @param TaxClassRepositoryInterface $repository
* @param ExceptionLogger $logger
* @param SearchCriteriaBuilderFactory $criteriaBuilderFactory
* @param GroupRepositoryInterface $customerGroupRepository
*/
public function __construct(
TaxClassRepositoryInterface $repository,
ExceptionLogger $logger,
SearchCriteriaBuilderFactory $criteriaBuilderFactory,
GroupRepositoryInterface $customerGroupRepository
) {
$this->repository = $repository;
$this->logger = $logger;
$this->criteriaBuilderFactory = $criteriaBuilderFactory;
$this->customerGroupRepository = $customerGroupRepository;
}
/**
* Get the name of a Tax Class given a Customer Group it's assigned to
*
* @param int $customerGroupId
* @return string
*/
public function getByCustomerGroupId($customerGroupId)
{
try {
$classId = $this->customerGroupRepository->getById($customerGroupId)->getTaxClassId();
} catch (\Exception $e) {
$this->logger->warning($e);
$classId = 0;
}
return $this->getById($classId);
}
/**
* Get the Tax Class Name given it's ID - or None if it could not be found
*
* @param int $taxClassId
* @return string
*/
public function getById($taxClassId)
{
if (!$taxClassId) {
return 'None';
}
try {
return $this->repository->get($taxClassId)
->getClassName();
} catch (NoSuchEntityException $exception) {
$this->logger->critical($exception);
return 'None';
}
}
/**
* Get an array of Tax Class Names given an array of Tax Class IDs
*
* Will provide "None" when a tax class is not found for any given ID
*
* @param int[] $taxClassIds
* @return string[] Indexed by tax class id
*/
public function getListByIds(array $taxClassIds)
{
/** @var SearchCriteriaBuilder $criteriaBuilder */
$criteriaBuilder = $this->criteriaBuilderFactory->create();
$criteriaBuilder->addFilter('class_id', $taxClassIds, 'in');
$criteria = $criteriaBuilder->create();
try {
$list = $this->repository->getList($criteria);
} catch (InputException $exception) {
$this->logger->critical($exception);
}
$result = [];
foreach ($list->getItems() as $item) {
$result[$item->getClassId()] = $item->getClassName();
}
foreach ($taxClassIds as $classId) {
if (!isset($result[$classId])) {
$result[$classId] = 'None';
}
}
return $result;
}
}