forked from avadev/Avalara-AvaTax-for-Magento2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaxClass.php
289 lines (260 loc) · 9.19 KB
/
TaxClass.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* ClassyLlama_AvaTax
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @copyright Copyright (c) 2016 Avalara, Inc.
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace ClassyLlama\AvaTax\Helper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
/**
* Class TaxClass
*/
class TaxClass
{
/**
* Avatax gift certificate tax code
*/
const GIFT_CARD_LINE_AVATAX_TAX_CODE = 'PG050000';
/**
* UPC Format
*/
const UPC_FORMAT = 'UPC: %s';
/**
* Type code for Gift Card (@see \Magento\GiftCard\Model\Catalog\Product\Type\Giftcard::TYPE_GIFTCARD)
*/
const PRODUCT_TYPE_GIFTCARD = 'giftcard';
/**
* Gift wrapping tax class
*
* Copied from \Magento\GiftWrapping\Helper\Data since it is an Enterprise-only module
*/
const XML_PATH_TAX_CLASS_GIFT_WRAPPING = 'tax/classes/wrapping_tax_class';
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig = null;
/**
* @var \Magento\Tax\Api\TaxClassRepositoryInterface
*/
protected $taxClassRepository;
/**
* @var \Magento\Customer\Api\GroupRepositoryInterface
*/
protected $customerGroupRepository;
/**
* @var \ClassyLlama\AvaTax\Helper\Config
*/
protected $config;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $productFactory;
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
private $productRepository;
/**
* @var \ClassyLlama\AvaTax\Model\GetSkusByProductIds
*/
private $getSkusByProductIds;
/**
* Class constructor
*
* @param ScopeConfigInterface $scopeConfig
* @param \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepository
* @param \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository
* @param \ClassyLlama\AvaTax\Helper\Config $config
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Catalog\Model\ProductRepository $productRepository
* @param \ClassyLlama\AvaTax\Model\GetSkusByProductIds $getSkusByProductIds
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
\Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepository,
\Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository,
\ClassyLlama\AvaTax\Helper\Config $config,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Catalog\Model\ProductRepository $productRepository,
\ClassyLlama\AvaTax\Model\GetSkusByProductIds $getSkusByProductIds
) {
$this->scopeConfig = $scopeConfig;
$this->taxClassRepository = $taxClassRepository;
$this->customerGroupRepository = $customerGroupRepository;
$this->config = $config;
$this->productFactory = $productFactory;
$this->productRepository = $productRepository;
$this->getSkusByProductIds = $getSkusByProductIds;
}
/**
* Get the AvaTax Tax Code for a product
*
* @param int $taxClassId
* @return string|null
*/
protected function getAvaTaxTaxCode($taxClassId)
{
try {
$taxClass = $this->taxClassRepository->get($taxClassId);
return $taxClass->getAvataxCode();
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
return null;
}
}
/**
* Get AvaTax Tax Code for a product
*
* @param \Magento\Catalog\Model\Product $product
* @param string $storeId
* @return null|string
*/
public function getAvataxTaxCodeForProduct(\Magento\Catalog\Model\Product $product, $storeId)
{
if ($product->getTypeId() == self::PRODUCT_TYPE_GIFTCARD) {
return self::GIFT_CARD_LINE_AVATAX_TAX_CODE;
} else {
try {
$itemSkuArray = $this->getSkusByProductIds->execute(
[$product->getId()]
);
$itemSku = $itemSkuArray[$product->getId()] ?? $product->getSku();
$simpleProduct = $this->productRepository->get($itemSku);
} catch (\Throwable $e) {
$simpleProduct = $product;
}
return $this->getAvaTaxTaxCode($simpleProduct->getTaxClassId() ?: $product->getTaxClassId());
}
}
/**
* Get AvaTax ItemCode for product if UPC is configured and product contains UPC
*
* @param \Magento\Catalog\Model\Product $product
* @return null|string
*/
public function getItemCodeOverride(\Magento\Catalog\Model\Product $product)
{
if ($this->config->getUpcAttribute() && $product->getData($this->config->getUpcAttribute())) {
$upcCode = $product->getData($this->config->getUpcAttribute());
if ($upcCode) {
return sprintf(self::UPC_FORMAT, $upcCode);
}
}
return null;
}
/**
* Get Ref1 code for product
*
* @param \Magento\Catalog\Model\Product $product
* @return mixed|null
*/
public function getRef1ForProduct(\Magento\Catalog\Model\Product $product)
{
if ($this->config->getRef1Attribute() && $product->getData($this->config->getRef1Attribute())) {
return $product->getData($this->config->getRef1Attribute());
}
return null;
}
/**
* Get Ref2 code for product
*
* @param \Magento\Catalog\Model\Product $product
* @return mixed|null
*/
public function getRef2ForProduct(\Magento\Catalog\Model\Product $product)
{
if ($this->config->getRef2Attribute() && $product->getData($this->config->getRef2Attribute())) {
return $product->getData($this->config->getRef2Attribute());
}
return null;
}
/**
* Get AvaTax Tax Code for shipping
* Default Configuration Setting: FR020100
*
* @return string
*/
public function getAvataxTaxCodeForShipping()
{
return $this->config->getShippingTaxCode();
}
/**
* @param $store
* @return null|string
*/
public function getAvataxTaxCodeForGiftOptions($store)
{
$taxClassId = $this->scopeConfig->getValue(
self::XML_PATH_TAX_CLASS_GIFT_WRAPPING,
ScopeInterface::SCOPE_STORE,
$store
);
return $this->getAvaTaxTaxCode($taxClassId);
}
/**
* Get AvaTax Customer Usage Type for customer
*
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
* @return null|string
*/
public function getAvataxTaxCodeForCustomer(\Magento\Customer\Api\Data\CustomerInterface $customer)
{
$customerGroupId = $customer->getGroupId();
if (!$customerGroupId) {
return null;
}
try {
$customerGroup = $this->customerGroupRepository->getById($customerGroupId);
$taxClassId = $customerGroup->getTaxClassId();
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
return null;
}
return $this->getAvaTaxTaxCode($taxClassId);
}
/**
* Populate correct tax class IDs on products that are loaded from an order item collection
*
* When a product is loaded from the context of an order item, a Magento bug/quirk causes the product to get loaded
* at the global configuration scope, rather than the scope of the order/order item. See this Github isue for
* more context: https://github.com/classyllama/ClassyLlama_AvaTax/issues/34
*
* @param $items \Magento\Sales\Api\Data\InvoiceItemInterface[]|\Magento\Sales\Api\Data\CreditmemoItemInterface[]
* @param $storeId
*/
public function populateCorrectTaxClasses($items, $storeId)
{
$productIds = [];
foreach ($items as $item) {
if ($item->getParentItem()) {
continue;
}
$productIds[] = $item->getOrderItem()->getProductId();
}
// Loading products via a collection rather than a repository as it's not possible to load a repository with
// a store view scope applied. See http://magento.stackexchange.com/q/91278/2142
$products = $this->productFactory->create()->getCollection()
->addAttributeToSelect('tax_class_id')
->addStoreFilter($storeId)
->addFieldToFilter('entity_id', ['in' => $productIds]);
$productsById = [];
foreach ($products as $product) {
$productsById[$product->getId()] = $product;
}
foreach ($items as $item) {
$productId = $item->getOrderItem()->getProductId();
if (isset($productsById[$productId])) {
$productWithCorrectTaxClassId = $productsById[$productId];
if ($productWithCorrectTaxClassId->getTaxClassId()) {
$item->getOrderItem()->getProduct()->setTaxClassId($productWithCorrectTaxClassId->getTaxClassId());
}
}
}
}
}