Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tax.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Weee\Model;
7 
14 
21 {
25  const DISPLAY_INCL = 0;
26 
30  const DISPLAY_INCL_DESCR = 1;
31 
36 
40  const DISPLAY_EXCL = 3;
41 
45  protected $_allAttributes = null;
46 
52  protected $_taxData = null;
53 
57  protected $_attributeFactory;
58 
62  protected $_storeManager;
63 
68 
72  protected $_customerSession;
73 
79  protected $weeeConfig;
80 
84  protected $priceCurrency;
85 
89  protected $accountManagement;
90 
107  public function __construct(
108  \Magento\Framework\Model\Context $context,
109  \Magento\Framework\Registry $registry,
110  \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
112  \Magento\Tax\Model\CalculationFactory $calculationFactory,
113  \Magento\Customer\Model\Session $customerSession,
115  \Magento\Tax\Helper\Data $taxData,
116  \Magento\Weee\Model\ResourceModel\Tax $resource,
117  \Magento\Weee\Model\Config $weeeConfig,
119  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
120  array $data = []
121  ) {
122  $this->_attributeFactory = $attributeFactory;
123  $this->_storeManager = $storeManager;
124  $this->_calculationFactory = $calculationFactory;
125  $this->_customerSession = $customerSession;
126  $this->accountManagement = $accountManagement;
127  $this->_taxData = $taxData;
128  $this->weeeConfig = $weeeConfig;
129  $this->priceCurrency = $priceCurrency;
130  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
131  }
132 
138  protected function _construct()
139  {
140  $this->_init(\Magento\Weee\Model\ResourceModel\Tax::class);
141  }
142 
151  public function getWeeeAmount(
152  $product,
153  $shipping = null,
154  $billing = null,
155  $website = null,
156  $calculateTax = false
157  ) {
158  $amount = 0;
159  $attributes = $this->getProductWeeeAttributes(
160  $product,
161  $shipping,
162  $billing,
163  $website,
164  $calculateTax
165  );
166  foreach ($attributes as $attribute) {
167  $amount += $attribute->getAmount();
168  }
169  return $amount;
170  }
171 
179  public function getWeeeAmountExclTax(
180  $product,
181  $shipping = null,
182  $billing = null,
183  $website = null
184  ) {
185  $amountExclTax = 0;
186  $attributes = $this->getProductWeeeAttributes(
187  $product,
188  $shipping,
189  $billing,
190  $website,
191  true,
192  false
193  );
194  if (Type::TYPE_BUNDLE !== $product->getTypeId() || $product->getPriceType()) {
195  foreach ($attributes as $attribute) {
196  $amountExclTax += $attribute->getAmountExclTax();
197  }
198  }
199  return $amountExclTax;
200  }
201 
206  public function getWeeeAttributeCodes($forceEnabled = false)
207  {
208  return $this->getWeeeTaxAttributeCodes(null, $forceEnabled);
209  }
210 
218  public function getWeeeTaxAttributeCodes($store = null, $forceEnabled = false)
219  {
220  if (!$forceEnabled && !$this->weeeConfig->isEnabled($store)) {
221  return [];
222  }
223 
224  if ($this->_allAttributes === null) {
225  $this->_allAttributes = $this->_attributeFactory->create()->getAttributeCodesByFrontendType('weee');
226  }
227  return $this->_allAttributes;
228  }
229 
242  public function getProductWeeeAttributes(
243  $product,
244  $shipping = null,
245  $billing = null,
246  $website = null,
247  $calculateTax = null,
248  $round = true
249  ) {
250  $result = [];
251  $websiteId = null;
253  $store = null;
254  if (!$website) {
255  $store = $product->getStore();
256  if ($store) {
257  $websiteId = $store->getWebsiteId();
258  }
259  }
260  if (!$websiteId) {
261  $websiteObject = $this->_storeManager->getWebsite($website);
262  $websiteId = $websiteObject->getId();
263  $store = $websiteObject->getDefaultGroup()->getDefaultStore();
264  }
265 
266  $allWeee = $this->getWeeeTaxAttributeCodes($store);
267  if (!$allWeee) {
268  return $result;
269  }
270 
272  $calculator = $this->_calculationFactory->create();
273 
274  $customerId = $this->_customerSession->getCustomerId();
275  if ($shipping && $shipping->getCountryId()) {
276  $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
277  } else {
278  // if customer logged use it default shipping and billing address
279  if ($customerId) {
280  $shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
281  $billing = $this->accountManagement->getDefaultBillingAddress($customerId);
282  $customerTaxClass = null;
283  } else {
284  $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
285  $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
286  if (!empty($billingAddressArray)) {
287  $billing = new \Magento\Framework\DataObject($billingAddressArray);
288  }
289  if (!empty($shippingAddressArray)) {
290  $shipping = new \Magento\Framework\DataObject($shippingAddressArray);
291  }
292  $customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
293  }
294  }
295 
296  $rateRequest = $calculator->getRateRequest(
297  $shipping,
298  $billing,
300  $store,
302  );
303  $defaultRateRequest = $calculator->getDefaultRateRequest($store);
304 
305  $productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity(
306  $rateRequest->getCountryId(),
307  $rateRequest->getRegionId(),
308  $websiteId,
309  $store->getId(),
310  $product->getId()
311  );
312 
313  foreach ($productAttributes as $attribute) {
314  $value = $attribute['weee_value'];
315  if ($value) {
316  $taxAmount = $amount = 0;
317  $amount = $value;
318  $amountExclTax = $value;
319  if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
321  $defaultPercent = $calculator->getRate(
322  $defaultRateRequest->setProductClassId($product->getTaxClassId())
323  );
324  $currentPercent = $calculator->getRate(
325  $rateRequest->setProductClassId($product->getTaxClassId())
326  );
327  if ($this->_taxData->priceIncludesTax($store)) {
328  $amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
329  if ($round) {
330  $amountInclTax = $this->priceCurrency->round($amountInclTax);
331  }
332  $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
333  if ($round) {
334  $taxAmount = $this->priceCurrency->round($taxAmount);
335  }
336  $amountExclTax = $amountInclTax - $taxAmount;
337  } else {
338  $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
339  if (is_array($appliedRates) && count($appliedRates) > 1) {
340  $taxAmount = 0;
341  foreach ($appliedRates as $appliedRate) {
342  $taxRate = $appliedRate['percent'];
343  if ($round) {
344  $taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
345  } else {
346  $taxAmount += $value * $taxRate / 100;
347  }
348  }
349  } else {
350  if ($round) {
351  $taxAmount = $this->priceCurrency->round(
352  $value * $currentPercent / 100
353  );
354  } else {
355  $taxAmount = $value * $currentPercent / 100;
356  }
357  }
358  }
359  }
360 
361  $one = new \Magento\Framework\DataObject();
362  $one->setName(
363  $attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label'])
364  )
365  ->setAmount($amount)
366  ->setTaxAmount($taxAmount)
367  ->setAmountExclTax($amountExclTax)
368  ->setCode($attribute['attribute_code']);
369 
370  $result[] = $one;
371  }
372  }
373  return $result;
374  }
375 
382  public function isWeeeInLocation($countryId, $regionId, $websiteId)
383  {
384  return $this->getResource()->isWeeeInLocation($countryId, $regionId, $websiteId);
385  }
386 }
const DISPLAY_EXCL
Definition: Tax.php:40
getWeeeAmountExclTax( $product, $shipping=null, $billing=null, $website=null)
Definition: Tax.php:179
getWeeeTaxAttributeCodes($store=null, $forceEnabled=false)
Definition: Tax.php:218
$storeManager
const DISPLAY_INCL_DESCR
Definition: Tax.php:30
isWeeeInLocation($countryId, $regionId, $websiteId)
Definition: Tax.php:382
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$customerTaxClass
Definition: tax_rule.php:9
$amount
Definition: order.php:14
$value
Definition: gender.phtml:16
const DISPLAY_INCL
Definition: Tax.php:25
getWeeeAttributeCodes($forceEnabled=false)
Definition: Tax.php:206
$attributes
Definition: matrix.phtml:13
const DISPLAY_EXCL_DESCR_INCL
Definition: Tax.php:35
$taxRate
Definition: tax_rule.php:12
getWeeeAmount( $product, $shipping=null, $billing=null, $website=null, $calculateTax=false)
Definition: Tax.php:151
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Tax\Model\CalculationFactory $calculationFactory, \Magento\Customer\Model\Session $customerSession, AccountManagementInterface $accountManagement, \Magento\Tax\Helper\Data $taxData, \Magento\Weee\Model\ResourceModel\Tax $resource, \Magento\Weee\Model\Config $weeeConfig, PriceCurrencyInterface $priceCurrency, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Tax.php:107