/** * Change default JavaScript templates for options rendering * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { $response = $observer->getEvent()->getResponseObject(); $options = $response->getAdditionalOptions(); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->registry->registry('current_product'); if (!$product) { return $this; } if ($this->weeeData->isEnabled() && !$this->weeeData->geDisplayIncl($product->getStoreId()) && !$this->weeeData->geDisplayExcl($product->getStoreId())) { // only do processing on bundle product if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { if (!array_key_exists('optionTemplate', $options)) { $calcPrice = $this->getWhichCalcPriceToUse($product->getStoreId()); $options['optionTemplate'] = '<%- data.label %>' . '<% if (data.' . $calcPrice . '.value) { %>' . ' +<%- data.' . $calcPrice . '.formatted %>' . '<% } %>'; } foreach ($this->weeeData->getWeeeAttributesForBundle($product) as $weeeAttributes) { foreach ($weeeAttributes as $weeeAttribute) { if (!preg_match('/' . $weeeAttribute->getCode() . '/', $options['optionTemplate'])) { $options['optionTemplate'] .= sprintf(' <%% if (data.weeePrice' . $weeeAttribute->getCode() . ') { %%>' . ' (' . $weeeAttribute->getName() . ': <%%- data.weeePrice' . $weeeAttribute->getCode() . '.formatted %%>)' . '<%% } %%>'); } } } if ($this->weeeData->geDisplayExlDescIncl($product->getStoreId())) { $options['optionTemplate'] .= sprintf(' <%% if (data.weeePrice) { %%>' . '<%%- data.weeePrice.formatted %%>' . '<%% } %%>'); } } } $response->setAdditionalOptions($options); return $this; }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->weeeHelper->isEnabled()) { /** @var \Magento\Customer\Model\Data\Customer $customer */ $customer = $observer->getData('customer'); /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ $addresses = $customer->getAddresses(); if (isset($addresses)) { $defaultShippingFound = false; $defaultBillingFound = false; foreach ($addresses as $address) { if ($address->isDefaultBilling()) { $defaultBillingFound = true; $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($address->isDefaultShipping()) { $defaultShippingFound = true; $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($defaultShippingFound && $defaultBillingFound) { break; } } } } }
/** * Collect Weee amounts for the quote / order * * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment * @param \Magento\Quote\Model\Quote\Address\Total $total * @return $this */ public function collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total) { AbstractTotal::collect($quote, $shippingAssignment, $total); $this->_store = $quote->getStore(); if (!$this->weeeData->isEnabled($this->_store)) { return $this; } $address = $shippingAssignment->getShipping()->getAddress(); $items = $shippingAssignment->getItems(); if (!count($items)) { return $this; } $this->weeeTotalExclTax = 0; $this->weeeBaseTotalExclTax = 0; foreach ($items as $item) { if ($item->getParentItem()) { continue; } $this->resetItemData($item); if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $this->resetItemData($child); $this->process($address, $total, $child); } $this->recalculateParent($item); } else { $this->process($address, $total, $item); } } $total->setWeeeCodeToItemMap($this->weeeCodeToItemMap); $total->setWeeeTotalExclTax($this->weeeTotalExclTax); $total->setWeeeBaseTotalExclTax($this->weeeBaseTotalExclTax); return $this; }
/** * Collect Weee amounts for the quote / order * * @param \Magento\Quote\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Quote\Model\Quote\Address $address) { AbstractTotal::collect($address); $this->_store = $address->getQuote()->getStore(); if (!$this->weeeData->isEnabled($this->_store)) { return $this; } $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $this->weeeTotalExclTax = 0; $this->weeeBaseTotalExclTax = 0; foreach ($items as $item) { if ($item->getParentItemId()) { continue; } $this->_resetItemData($item); if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $this->_resetItemData($child); $this->_process($address, $child); } $this->_recalculateParent($item); } else { $this->_process($address, $item); } } $address->setWeeeCodeToItemMap($this->weeeCodeToItemMap); $address->setWeeeTotalExclTax($this->weeeTotalExclTax); $address->setWeeeBaseTotalExclTax($this->weeeBaseTotalExclTax); return $this; }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->weeeHelper->isEnabled()) { /** @var $customerAddress Address */ $address = $observer->getCustomerAddress(); // Check if the address is either the default billing, shipping, or both if ($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) { $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) { $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } }
/** * Modify the options config for the front end to resemble the weee final price * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->weeeData->isEnabled()) { $priceConfigObj = $observer->getData('configObj'); try { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->registry->registry('current_product'); $weeeAttributesForBundle = $this->weeeData->getWeeeAttributesForBundle($product); $priceConfig = $this->recurConfigAndInsertWeeePrice($priceConfigObj->getConfig(), 'prices', $this->getWhichCalcPriceToUse($product->getStoreId(), $weeeAttributesForBundle), $weeeAttributesForBundle); $priceConfigObj->setConfig($priceConfig); } catch (\Exception $e) { return $this; } } return $this; }
/** * Modify the options config for the front end to resemble the weee final price * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getPriceConfiguration(\Magento\Framework\Event\Observer $observer) { if ($this->_weeeData->isEnabled()) { $priceConfigObj = $observer->getData('configObj'); $priceConfig = $priceConfigObj->getConfig(); try { if (is_array($priceConfig)) { foreach ($priceConfig as $keyConfigs => $configs) { if (is_array($configs)) { foreach ($configs as $keyConfig => $config) { $calcPrice = 'finalPrice'; if ($this->_taxData->priceIncludesTax() && $this->_taxData->displayPriceExcludingTax()) { $calcPrice = 'basePrice'; } if (array_key_exists('prices', $configs)) { $priceConfig[$keyConfigs]['prices']['weeePrice'] = ['amount' => $configs['prices'][$calcPrice]['amount']]; } else { foreach ($configs as $keyConfig => $config) { $priceConfig[$keyConfigs][$keyConfig]['prices']['weeePrice'] = ['amount' => $config['prices'][$calcPrice]['amount']]; } } } } } } $priceConfigObj->setConfig($priceConfig); } catch (Exception $e) { return $this; } } return $this; }
/** * @param \Magento\Framework\App\ActionInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function aroundExecute( \Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { if (!$this->weeeHelper->isEnabled() || !$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled()) { return $proceed($request); } $basedOn = $this->taxHelper->getTaxBasedOn(); if ($basedOn != 'shipping' && $basedOn != 'billing') { return $proceed($request); } $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $countryId = $weeeTaxRegion['countryId']; $regionId = $weeeTaxRegion['regionId']; if (!$countryId && !$regionId) { // country and region does not exist return $proceed($request); } else if ($countryId && !$regionId) { // country exist and region does not exist $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } else { // country and region exist $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); if (!$exist) { // just check the country for weee $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } } if ($exist) { $this->httpContext->setValue( 'weee_tax_region', ['countryId' => $countryId, 'regionId' => $regionId], 0 ); } return $proceed($request); }
/** * Process bundle options selection for prepare view json * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function updateBundleProductOptions(\Magento\Framework\Event\Observer $observer) { if (!$this->_weeeData->isEnabled()) { return $this; } $response = $observer->getEvent()->getResponseObject(); $selection = $observer->getEvent()->getSelection(); $options = $response->getAdditionalOptions(); $_product = $this->_registry->registry('current_product'); $typeDynamic = \Magento\Bundle\Block\Adminhtml\Catalog\Product\Edit\Tab\Attributes\Extend::DYNAMIC; if (!$_product || $_product->getPriceType() != $typeDynamic) { return $this; } $amount = $this->_weeeData->getAmount($selection); $attributes = $this->_weeeData->getProductWeeeAttributes($_product, null, null, null, $this->_weeeData->isTaxable()); $amountInclTaxes = $this->_weeeData->getAmountInclTaxes($attributes); $taxes = $amountInclTaxes - $amount; $options['plusDisposition'] = $amount; $options['plusDispositionTax'] = $taxes < 0 ? 0 : $taxes; // Exclude Weee amount from excluding tax amount if (!$this->_weeeData->typeOfDisplay(array(0, 1, 4))) { $options['exclDisposition'] = true; } $response->setAdditionalOptions($options); return $this; }
/** * Get base final row display price excluding tax, this will add Weee amount to rowTotal * * @return float */ public function getBaseFinalRowDisplayPriceExclTax() { $baseRowTotalExclTax = $this->getItem()->getBaseRowTotal(); if (!$this->weeeHelper->isEnabled()) { return $baseRowTotalExclTax; } return $baseRowTotalExclTax + $this->getItem()->getBaseWeeeTaxAppliedRowAmnt(); }
/** * Define if adjustment is included in display price * * @return bool */ public function isIncludedInDisplayPrice() { if ($this->taxHelper->displayPriceExcludingTax()) { return false; } if ($this->weeeHelper->isEnabled() == true && $this->weeeHelper->isTaxable() == true && $this->weeeHelper->typeOfDisplay([\Magento\Weee\Model\Tax::DISPLAY_EXCL]) == false) { return true; } else { return false; } }
/** * Get display price for unit price excluding tax. The Weee amount will be added to unit price * depending on Weee display setting * * @param \Magento\Quote\Model\Quote\Item $item * @return float */ private function getUnitDisplayPriceExclTax($item) { $priceExclTax = $item->getCalculationPrice(); if (!$this->weeeHelper->isEnabled($this->getStoreId())) { return $priceExclTax; } if ($this->getIncludeWeeeFlag()) { return $priceExclTax + $item->getWeeeTaxAppliedAmount(); } return $priceExclTax; }
/** * Change default JavaScript templates for options rendering * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { $response = $observer->getEvent()->getResponseObject(); $options = $response->getAdditionalOptions(); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->registry->registry('current_product'); if (!$product) { return $this; } // if the Weee module is enabled, then only do processing on bundle products if ($this->weeeData->isEnabled() && $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { if ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) { // the Tax module might have set up a default, but we will re-decide which calcPrice field to use unset($options['optionTemplate']); } if (!array_key_exists('optionTemplate', $options)) { $calcPrice = $this->getWhichCalcPriceToUse($product->getStoreId()); $options['optionTemplate'] = '<%- data.label %>' . '<% if (data.' . $calcPrice . '.value) { %>' . ' +<%- data.' . $calcPrice . '.formatted %>' . '<% } %>'; } if (!$this->weeeData->isDisplayIncl($product->getStoreId()) && !$this->weeeData->isDisplayExcl($product->getStoreId())) { // we need to display the individual Weee amounts foreach ($this->weeeData->getWeeeAttributesForBundle($product) as $weeeAttributes) { foreach ($weeeAttributes as $weeeAttribute) { if (!preg_match('/' . $weeeAttribute->getCode() . '/', $options['optionTemplate'])) { $options['optionTemplate'] .= sprintf(' <%% if (data.weeePrice' . $weeeAttribute->getCode() . ') { %%>' . ' (' . $weeeAttribute->getName() . ': <%%- data.weeePrice' . $weeeAttribute->getCode() . '.formatted %%>)' . '<%% } %%>'); } } } } if ($this->weeeData->isDisplayExclDescIncl($product->getStoreId())) { $options['optionTemplate'] .= sprintf(' <%% if (data.weeePrice) { %%>' . '<%%- data.weeePrice.formatted %%>' . '<%% } %%>'); } } $response->setAdditionalOptions($options); return $this; }
/** * Modify the options config for the front end to resemble the weee final price * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function getPriceConfiguration(\Magento\Framework\Event\Observer $observer) { if ($this->_weeeData->isEnabled()) { $priceConfigObj = $observer->getData('configObj'); $priceConfig = $priceConfigObj->getConfig(); if (is_array($priceConfig)) { foreach ($priceConfig as $keyConfigs => $configs) { if (is_array($configs)) { if (array_key_exists('prices', $configs)) { $priceConfig[$keyConfigs]['prices']['weeePrice'] = ['amount' => $configs['prices']['finalPrice']['amount']]; } else { foreach ($configs as $keyConfig => $config) { $priceConfig[$keyConfigs][$keyConfig]['prices']['weeePrice'] = ['amount' => $config['prices']['finalPrice']['amount']]; } } } } } $priceConfigObj->setConfig($priceConfig); } return $this; }
/** * Check if fixed taxes are used in system * * @return bool */ public function isWeeeEnabled() { return $this->weeeHelper->isEnabled($this->storeManager->getStore()->getId()); }
/** * Calculate item fixed tax and prepare information for discount and regular taxation * * @param \Magento\Sales\Model\Quote\Address $address * @param \Magento\Sales\Model\Quote\Item\AbstractItem $item * @return void|$this */ protected function _process(\Magento\Sales\Model\Quote\Address $address, $item) { if (!$this->_weeeData->isEnabled($this->_store)) { return $this; } $attributes = $this->_weeeData->getProductWeeeAttributes($item->getProduct(), $address, $address->getQuote()->getBillingAddress(), $this->_store->getWebsiteId()); $applied = array(); $productTaxes = array(); $defaultRateRequest = $this->_calculator->getRateOriginRequest($this->_store); $rateRequest = $this->_calculator->getRateRequest($address, $address->getQuote()->getBillingAddress(), $address->getQuote()->getCustomerTaxClassId(), $this->_store); $totalValueInclTax = 0; $baseTotalValueInclTax = 0; $totalRowValueInclTax = 0; $baseTotalRowValueInclTax = 0; $totalValueExclTax = 0; $baseTotalValueExclTax = 0; $totalRowValueExclTax = 0; $baseTotalRowValueExclTax = 0; $priceIncludesTax = $this->_taxData->priceIncludesTax($this->_store); $calculationAlgorithm = $this->_taxData->getCalculationAgorithm($this->_store); $defaultPercent = $currentPercent = 0; //when FPT is not taxable foreach ($attributes as $key => $attribute) { $title = $attribute->getName(); $baseValue = $attribute->getAmount(); $value = $this->_store->convertPrice($baseValue); $value = $this->_store->roundPrice($value); if ($this->_weeeData->isTaxable($this->_store)) { $defaultPercent = $this->_calculator->getRate($defaultRateRequest->setProductClassId($item->getProduct()->getTaxClassId())); $currentPercent = $this->_calculator->getRate($rateRequest->setProductClassId($item->getProduct()->getTaxClassId())); } if ($priceIncludesTax) { //Make sure that price including tax is rounded first $baseValueInclTax = $baseValue / (100 + $defaultPercent) * (100 + $currentPercent); $baseValueInclTax = $this->_store->roundPrice($baseValueInclTax); $valueInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent); $valueInclTax = $this->_store->roundPrice($valueInclTax); $baseValueExclTax = $baseValueInclTax / (100 + $currentPercent) * 100; $valueExclTax = $valueInclTax / (100 + $currentPercent) * 100; if ($calculationAlgorithm == Calculation::CALC_UNIT_BASE) { $baseValueExclTax = $this->_store->roundPrice($baseValueExclTax); $valueExclTax = $this->_store->roundPrice($valueExclTax); } } else { $valueExclTax = $value; $baseValueExclTax = $baseValue; $valueInclTax = $valueExclTax * (100 + $currentPercent) / 100; $baseValueInclTax = $baseValueExclTax * (100 + $currentPercent) / 100; if ($calculationAlgorithm == Calculation::CALC_UNIT_BASE) { $baseValueInclTax = $this->_store->roundPrice($baseValueInclTax); $valueInclTax = $this->_store->roundPrice($valueInclTax); } } $rowValueInclTax = $this->_store->roundPrice($valueInclTax * $item->getTotalQty()); $baseRowValueInclTax = $this->_store->roundPrice($baseValueInclTax * $item->getTotalQty()); $rowValueExclTax = $this->_store->roundPrice($valueExclTax * $item->getTotalQty()); $baseRowValueExclTax = $this->_store->roundPrice($baseValueExclTax * $item->getTotalQty()); //Now, round the unit price just in case $valueExclTax = $this->_store->roundPrice($valueExclTax); $baseValueExclTax = $this->_store->roundPrice($baseValueExclTax); $valueInclTax = $this->_store->roundPrice($valueInclTax); $baseValueInclTax = $this->_store->roundPrice($baseValueInclTax); $totalValueInclTax += $valueInclTax; $baseTotalValueInclTax += $baseValueInclTax; $totalRowValueInclTax += $rowValueInclTax; $baseTotalRowValueInclTax += $baseRowValueInclTax; $totalValueExclTax += $valueExclTax; $baseTotalValueExclTax += $baseValueExclTax; $totalRowValueExclTax += $rowValueExclTax; $baseTotalRowValueExclTax += $baseRowValueExclTax; $productTaxes[] = array('title' => $title, 'base_amount' => $baseValueExclTax, 'amount' => $valueExclTax, 'row_amount' => $rowValueExclTax, 'base_row_amount' => $baseRowValueExclTax, 'base_amount_incl_tax' => $baseValueInclTax, 'amount_incl_tax' => $valueInclTax, 'row_amount_incl_tax' => $rowValueInclTax, 'base_row_amount_incl_tax' => $baseRowValueInclTax); //This include FPT as applied tax, since tax on FPT is calculated separately, we use value excluding tax $applied[] = array('id' => $attribute->getCode(), 'percent' => null, 'hidden' => $this->_weeeData->includeInSubtotal($this->_store), 'rates' => array(array('base_real_amount' => $baseRowValueExclTax, 'base_amount' => $baseRowValueExclTax, 'amount' => $rowValueExclTax, 'code' => $attribute->getCode(), 'title' => $title, 'percent' => null, 'position' => 1, 'priority' => -1000 + $key))); } $item->setWeeeTaxAppliedAmount($totalValueExclTax)->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax); $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax); if ($priceIncludesTax) { $this->_processTaxSettings($item, $totalValueInclTax, $baseTotalValueInclTax, $totalRowValueInclTax, $baseTotalRowValueInclTax); } else { $this->_processTaxSettings($item, $totalValueExclTax, $baseTotalValueExclTax, $totalRowValueExclTax, $baseTotalRowValueExclTax); } $this->_processTotalAmount($address, $totalRowValueExclTax, $baseTotalRowValueExclTax, $totalRowValueInclTax, $baseTotalRowValueInclTax); $this->_weeeData->setApplied($item, array_merge($this->_weeeData->getApplied($item), $productTaxes)); //Update the applied taxes for the quote if ($applied) { $this->_saveAppliedTaxes($address, $applied, $item->getWeeeTaxAppliedAmount(), $item->getBaseWeeeTaxAppliedAmount(), null); } }