Example #1
0
 /**
  * @param Product $product
  * @param null|false|\Magento\Sales\Model\Quote\Address $shipping
  * @param null|false|\Magento\Sales\Model\Quote\Address $billing
  * @param Website $website
  * @param bool $calculateTax
  * @param bool $ignoreDiscount
  * @return \Magento\Framework\Object[]
  */
 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null, $ignoreDiscount = false)
 {
     $result = array();
     $allWeee = $this->getWeeeTaxAttributeCodes();
     if (!$allWeee) {
         return $result;
     }
     $websiteId = $this->_storeManager->getWebsite($website)->getId();
     /** @var \Magento\Store\Model\Store $store */
     $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
     /** @var \Magento\Tax\Model\Calculation $calculator */
     $calculator = $this->_calculationFactory->create();
     if ($shipping) {
         $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
     } else {
         $customerTaxClass = null;
     }
     $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store);
     $defaultRateRequest = $calculator->getDefaultRateRequest($store);
     $discountPercent = 0;
     if (!$ignoreDiscount && $this->weeeConfig->isDiscounted($store)) {
         $discountPercent = $this->_getDiscountPercentForProduct($product);
     }
     $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(?)', array($websiteId, 0))->where('country = ?', $rateRequest->getCountryId())->where('state IN(?)', array($rateRequest->getRegionId(), '*'))->where('entity_id = ?', (int) $product->getId())->limit(1);
             $order = array('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) {
                 if ($discountPercent) {
                     $value = $this->_storeManager->getStore()->roundPrice($value - $value * $discountPercent / 100);
                 }
                 $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 = $store->roundPrice($amountInclTax);
                         $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
                         $taxAmount = $store->roundPrice($taxAmount);
                     } else {
                         $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
                         if (count($appliedRates) > 1) {
                             $taxAmount = 0;
                             foreach ($appliedRates as $appliedRate) {
                                 $taxRate = $appliedRate['percent'];
                                 $taxAmount += $this->_storeManager->getStore()->roundPrice($value * $taxRate / 100);
                             }
                         } else {
                             $taxAmount = $this->_storeManager->getStore()->roundPrice($value * $currentPercent / 100);
                         }
                         $taxAmount = $store->roundPrice($value * $currentPercent / 100);
                     }
                 }
                 $one = new \Magento\Framework\Object();
                 $one->setName(__($attribute->getFrontend()->getLabel()))->setAmount($amount)->setTaxAmount($taxAmount)->setCode($attribute->getAttributeCode());
                 $result[] = $one;
             }
         }
     }
     return $result;
 }
Example #2
0
 /**
  * Check if weee tax amount should be discounted
  *
  * @param   null|string|bool|int|Store $store
  * @return  bool
  */
 public function isDiscounted($store = null)
 {
     return $this->_weeeConfig->isDiscounted($store);
 }