/** * @return mixed */ public function getAfterElementHtml() { $html = parent::getAfterElementHtml(); /** * getEntityAttribute - use __call */ $addJsObserver = false; if ($attribute = $this->getEntityAttribute()) { if (!($storeId = $attribute->getStoreId())) { $storeId = $this->getForm()->getDataObject()->getStoreId(); } $store = $this->_storeManager->getStore($storeId); $html .= '<strong>' . $this->_localeCurrency->getCurrency($store->getBaseCurrencyCode())->getSymbol() . '</strong>'; if ($this->_taxData->priceIncludesTax($store)) { if ($attribute->getAttributeCode() !== 'cost') { $addJsObserver = true; $html .= ' <strong>[' . __('Inc. Tax') . '<span id="dynamic-tax-' . $attribute->getAttributeCode() . '"></span>]</strong>'; } } } if ($addJsObserver) { $html .= $this->_getTaxObservingCode($attribute); } return $html; }
/** * Returns which product price to use as a basis for the Weee's final price * * @param int|null $storeId * @return string */ protected function getWhichCalcPriceToUse($storeId = null) { $calcPrice = 'finalPrice'; if ($this->weeeData->geDisplayExcl($storeId) || $this->weeeData->geDisplayExlDescIncl($storeId) || $this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) { $calcPrice = 'basePrice'; } return $calcPrice; }
/** * Extract adjustment amount from the given amount value * * @param float $amount * @param SaleableInterface $saleableItem * @param null|array $context * @return float */ public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = []) { if ($this->taxHelper->priceIncludesTax()) { $adjustedAmount = $this->catalogHelper->getTaxPrice($saleableItem, $amount, false, null, null, null, null, null, false); $result = $amount - $adjustedAmount; } else { $result = 0.0; } return $result; }
/** * 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; }
/** * Get including/excluding tax message * * @return \Magento\Framework\Phrase */ public function getInclExclTaxMessage() { if ($this->_taxData->priceIncludesTax($this->getStore())) { return __('* - Enter custom price including tax'); } else { return __('* - Enter custom price excluding tax'); } }
/** * @return string */ protected function apply() { if ($this->displayBothPrices()) { if ($this->getZone() !== \Magento\Framework\Pricing\Render::ZONE_ITEM_OPTION) { $this->amountRender->setPriceDisplayLabel(__('Incl. Tax')); } $this->amountRender->setPriceWrapperCss('price-including-tax'); $this->amountRender->setPriceId($this->buildIdWithPrefix('price-including-tax-')); } elseif ($this->displayPriceExcludingTax()) { $this->amountRender->setDisplayValue($this->amountRender->getAmount()->getValue($this->getAdjustmentCode())); if ($this->taxHelper->priceIncludesTax() && $this->amountRender->getPriceType() == 'finalPrice') { // for dynamic calculations of prices with any options, use the base price amount $this->amountRender->setPriceType('basePrice'); } } return $this->toHtml(); }
/** * Returns which product price to use as a basis for the Weee's final price * * @param int|null $storeId * @param array|null $weeeAttributesForBundle * @return string */ protected function getWhichCalcPriceToUse($storeId = null, $weeeAttributesForBundle = null) { $calcPrice = 'finalPrice'; if (!empty($weeeAttributesForBundle)) { if ($this->weeeData->isDisplayExcl($storeId) || $this->weeeData->isDisplayExclDescIncl($storeId) || $this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) { $calcPrice = 'basePrice'; } } return $calcPrice; }
/** * Change default JavaScript templates for options rendering * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $response = $observer->getEvent()->getResponseObject(); $options = $response->getAdditionalOptions(); $_product = $this->registry->registry('current_product'); if (!$_product) { return $this; } $algorithm = $this->taxData->getCalculationAlgorithm(); $options['calculationAlgorithm'] = $algorithm; // prepare correct template for options render if ($this->taxData->displayBothPrices()) { $options['optionTemplate'] = sprintf('<%%= data.label %%>' . '<%% if (data.finalPrice.value) { %%>' . ' +<%%= data.finalPrice.formatted %%> (%1$s <%%= data.basePrice.formatted %%>)' . '<%% } %%>', __('Excl. tax:')); } elseif ($this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) { $options['optionTemplate'] = sprintf('<%%= data.label %%>' . '<%% if (data.basePrice.value) { %%>' . ' +<%%= data.basePrice.formatted %%>' . '<%% } %%>'); } $response->setAdditionalOptions($options); return $this; }
/** * @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount */ public function testSetAmountWithoutTax() { $expectation = ['subtotal' => 12.04, 'tax' => 2.03, 'shipping' => 5.05]; $amount = array_sum($expectation); static::assertFalse($this->taxData->priceIncludesTax()); $payment = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Payment')->disableOriginalConstructor()->getMock(); $order = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock(); $payment->expects(static::once())->method('getBaseAmountAuthorized')->willReturn($amount); $order->expects(static::once())->method('getPayment')->willReturn($payment); $order->expects(static::atLeastOnce())->method('getBaseTaxAmount')->willReturn($expectation['tax']); $order->expects(static::atLeastOnce())->method('getBaseShippingAmount')->willReturn($expectation['shipping']); $this->_model->setAmount($order); static::assertEquals($expectation, $this->_model->getData()); }
/** * @covers \Magento\Paypal\Model\Hostedpro\Request::setAmount() * @param $total * @param $subtotal * @param $tax * @param $shipping * @param $discount * @dataProvider amountWithoutTaxZeroSubtotalDataProvider */ public function testSetAmountWithoutTaxZeroSubtotal($total, $subtotal, $tax, $shipping, $discount) { $expectation = ['subtotal' => $total, 'total' => $total, 'tax' => $tax, 'shipping' => $shipping, 'discount' => abs($discount)]; static::assertFalse($this->taxData->priceIncludesTax()); $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock(); $payment->expects(static::exactly(2))->method('getBaseAmountAuthorized')->willReturn($total); $order->expects(static::exactly(2))->method('getPayment')->willReturn($payment); $order->expects(static::once())->method('getBaseDiscountAmount')->willReturn($discount); $order->expects(static::once())->method('getBaseTaxAmount')->willReturn($tax); $order->expects(static::once())->method('getBaseShippingAmount')->willReturn($shipping); $order->expects(static::once())->method('getBaseSubtotal')->willReturn($subtotal); $this->_model->setAmount($order); static::assertEquals($expectation, $this->_model->getData()); }
/** * 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 * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getPriceConfiguration(\Magento\Framework\Event\Observer $observer) { if ($this->_weeeData->isEnabled()) { $priceConfigObj = $observer->getData('configObj'); try { $product = $this->_registry->registry('current_product'); $weeeAttributes = $this->_weeeData->getWeeAttributesForBundle($product); $calcPrice = 'finalPrice'; if ($this->_taxData->priceIncludesTax() && $this->_taxData->displayPriceExcludingTax()) { $calcPrice = 'basePrice'; } $priceConfig = $this->recurConfigAndInsertWeeePrice($priceConfigObj->getConfig(), 'prices', $calcPrice, $weeeAttributes); $priceConfigObj->setConfig($priceConfig); } catch (\Exception $e) { return $this; } } return $this; }
/** * @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; }
/** * Check if the FPT totals line(s) should be displayed with tax included * * @param null|string|bool|int|Store $store * @return bool */ public function displayTotalsInclTax($store = null) { // If catalog prices include tax, then display FPT totals with tax included return $this->_taxData->priceIncludesTax($store); }
/** * @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; }
/** * Apply adjustment amount and return result value * * @param float $amount * @param SaleableInterface $saleableItem * @return float */ public function applyAdjustment($amount, SaleableInterface $saleableItem) { $includingTax = !$this->taxHelper->priceIncludesTax(); return $this->taxHelper->getPriceUnrounded($saleableItem, $amount, $includingTax); }