/**
  * @param Product $product
  * @param null|false|\Magento\Quote\Model\Quote\Address $shipping
  * @param null|false|\Magento\Quote\Model\Quote\Address $billing
  * @param Website $website
  * @param bool $calculateTax
  * @param bool $round
  * @return \Magento\Framework\DataObject[]
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null, $round = true)
 {
     $result = [];
     $websiteId = $this->_storeManager->getWebsite($website)->getId();
     /** @var \Magento\Store\Model\Store $store */
     $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
     $allWeee = $this->getWeeeTaxAttributeCodes($store);
     if (!$allWeee) {
         return $result;
     }
     /** @var \Magento\Tax\Model\Calculation $calculator */
     $calculator = $this->_calculationFactory->create();
     $customerId = $this->_customerSession->getCustomerId();
     if ($shipping && $shipping->getCountryId()) {
         $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
     } else {
         // if customer logged use it default shipping and billing address
         if ($customerId) {
             $shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
             $billing = $this->accountManagement->getDefaultBillingAddress($customerId);
             $customerTaxClass = null;
         } else {
             $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
             $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
             if (!empty($billingAddressArray)) {
                 $billing = new \Magento\Framework\DataObject($billingAddressArray);
             }
             if (!empty($shippingAddressArray)) {
                 $shipping = new \Magento\Framework\DataObject($shippingAddressArray);
             }
             $customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
         }
     }
     $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store, $customerId);
     $defaultRateRequest = $calculator->getDefaultRateRequest($store);
     $productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity($rateRequest->getCountryId(), $rateRequest->getRegionId(), $websiteId, $store->getId(), $product->getId());
     foreach ($productAttributes as $attribute) {
         $value = $attribute['weee_value'];
         if ($value) {
             $taxAmount = $amount = 0;
             $amount = $value;
             $amountExclTax = $value;
             if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
                 /** @var \Magento\Tax\Model\Calculation $calculator */
                 $defaultPercent = $calculator->getRate($defaultRateRequest->setProductClassId($product->getTaxClassId()));
                 $currentPercent = $calculator->getRate($rateRequest->setProductClassId($product->getTaxClassId()));
                 if ($this->_taxData->priceIncludesTax($store)) {
                     $amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
                     if ($round) {
                         $amountInclTax = $this->priceCurrency->round($amountInclTax);
                     }
                     $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
                     if ($round) {
                         $taxAmount = $this->priceCurrency->round($taxAmount);
                     }
                     $amountExclTax = $amountInclTax - $taxAmount;
                 } else {
                     $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
                     if (count($appliedRates) > 1) {
                         $taxAmount = 0;
                         foreach ($appliedRates as $appliedRate) {
                             $taxRate = $appliedRate['percent'];
                             if ($round) {
                                 $taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
                             } else {
                                 $taxAmount += $value * $taxRate / 100;
                             }
                         }
                     } else {
                         if ($round) {
                             $taxAmount = $this->priceCurrency->round($value * $currentPercent / 100);
                         } else {
                             $taxAmount = $value * $currentPercent / 100;
                         }
                     }
                 }
             }
             $one = new \Magento\Framework\DataObject();
             $one->setName($attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label']))->setAmount($amount)->setTaxAmount($taxAmount)->setAmountExclTax($amountExclTax)->setCode($attribute['attribute_code']);
             $result[] = $one;
         }
     }
     return $result;
 }
Example #2
0
 /**
  * @param Product $product
  * @param null|false|\Magento\Quote\Model\Quote\Address $shipping
  * @param null|false|\Magento\Quote\Model\Quote\Address $billing
  * @param Website $website
  * @param bool $calculateTax
  * @return \Magento\Framework\Object[]
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null)
 {
     $result = [];
     $websiteId = $this->_storeManager->getWebsite($website)->getId();
     /** @var \Magento\Store\Model\Store $store */
     $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
     $allWeee = $this->getWeeeTaxAttributeCodes($store);
     if (!$allWeee) {
         return $result;
     }
     /** @var \Magento\Tax\Model\Calculation $calculator */
     $calculator = $this->_calculationFactory->create();
     if ($shipping && $shipping->getCountryId()) {
         $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
     } else {
         // if customer logged use it default shipping and billing address
         if ($customerId = $this->_customerSession->getCustomerId()) {
             $shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
             $billing = $this->accountManagement->getDefaultBillingAddress($customerId);
             $customerTaxClass = null;
         } else {
             $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
             $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
             if (!empty($billingAddressArray)) {
                 $billing = new \Magento\Framework\Object($billingAddressArray);
             }
             if (!empty($shippingAddressArray)) {
                 $shipping = new \Magento\Framework\Object($shippingAddressArray);
             }
             $customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
         }
     }
     $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store);
     $defaultRateRequest = $calculator->getDefaultRateRequest($store);
     $productAttributes = $product->getTypeInstance()->getSetAttributes($product);
     foreach ($productAttributes as $code => $attribute) {
         if (in_array($code, $allWeee)) {
             $attributeSelect = $this->getResource()->getReadConnection()->select();
             $attributeSelect->from($this->getResource()->getTable('weee_tax'), 'value')->where('attribute_id = ?', (int) $attribute->getId())->where('website_id IN(?)', [$websiteId, 0])->where('country = ?', $rateRequest->getCountryId())->where('state IN(?)', [$rateRequest->getRegionId(), 0])->where('entity_id = ?', (int) $product->getId())->limit(1);
             $order = ['state ' . \Magento\Framework\DB\Select::SQL_DESC, 'website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
             $attributeSelect->order($order);
             $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect);
             if ($value) {
                 $taxAmount = $amount = 0;
                 $amount = $value;
                 if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
                     /** @var \Magento\Tax\Model\Calculation $calculator */
                     $defaultPercent = $calculator->getRate($defaultRateRequest->setProductClassId($product->getTaxClassId()));
                     $currentPercent = $calculator->getRate($rateRequest->setProductClassId($product->getTaxClassId()));
                     if ($this->_taxData->priceIncludesTax($store)) {
                         $amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
                         //round the "golden price"
                         $amountInclTax = $this->priceCurrency->round($amountInclTax);
                         $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
                         $taxAmount = $this->priceCurrency->round($taxAmount);
                     } else {
                         $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
                         if (count($appliedRates) > 1) {
                             $taxAmount = 0;
                             foreach ($appliedRates as $appliedRate) {
                                 $taxRate = $appliedRate['percent'];
                                 $taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
                             }
                         } else {
                             $taxAmount = $this->priceCurrency->round($value * $currentPercent / 100);
                         }
                         $taxAmount = $this->priceCurrency->round($value * $currentPercent / 100);
                     }
                 }
                 $one = new \Magento\Framework\Object();
                 $one->setName(__($attribute->getFrontend()->getLabel()))->setAmount($amount)->setTaxAmount($taxAmount)->setCode($attribute->getAttributeCode());
                 $result[] = $one;
             }
         }
     }
     return $result;
 }