/** * Get information about tax rates applied to request * * @param Varien_Object $request * @return array */ public function getAppliedRates($request) { if ($this->_getDataHelper()->isServiceEnabled($this->_getStore($request))) { return array(); } return parent::getAppliedRates($request); }
/** * Calculate address total tax based on address subtotal * * @param Mage_Sales_Model_Quote_Address $address * @param Varien_Object $taxRateRequest * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _totalBaseCalculation(Mage_Sales_Model_Quote_Address $address, $taxRateRequest) { $items = $this->_getAddressItems($address); $store = $address->getQuote()->getStore(); $taxGroups = array(); $itemTaxGroups = array(); foreach ($items as $item) { if ($item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $taxRateRequest->setProductClassId($child->getProduct()->getTaxClassId()); $rate = $this->_calculator->getRate($taxRateRequest); $applied_rates = $this->_calculator->getAppliedRates($taxRateRequest); $taxGroups[(string) $rate]['applied_rates'] = $applied_rates; $taxGroups[(string) $rate]['incl_tax'] = $child->getIsPriceInclTax(); $this->_aggregateTaxPerRate($child, $rate, $taxGroups); if ($rate > 0) { $itemTaxGroups[$child->getId()] = $applied_rates; } } $this->_recalculateParent($item); } else { $taxRateRequest->setProductClassId($item->getProduct()->getTaxClassId()); $rate = $this->_calculator->getRate($taxRateRequest); $applied_rates = $this->_calculator->getAppliedRates($taxRateRequest); $taxGroups[(string) $rate]['applied_rates'] = $applied_rates; $taxGroups[(string) $rate]['incl_tax'] = $item->getIsPriceInclTax(); $this->_aggregateTaxPerRate($item, $rate, $taxGroups); if ($rate > 0) { $itemTaxGroups[$item->getId()] = $applied_rates; } } } if ($address->getQuote()->getTaxesForItems()) { $itemTaxGroups += $address->getQuote()->getTaxesForItems(); } $address->getQuote()->setTaxesForItems($itemTaxGroups); foreach ($taxGroups as $rateKey => $data) { $rate = (double) $rateKey; $inclTax = $data['incl_tax']; $totalTax = $this->_calculator->calcTaxAmount(array_sum($data['totals']), $rate, $inclTax, false); $totalTax += array_sum($data['weee_tax']); $baseTotalTax = $this->_calculator->calcTaxAmount(array_sum($data['base_totals']), $rate, $inclTax, false); $baseTotalTax += array_sum($data['base_weee_tax']); $this->_addAmount($totalTax); $this->_addBaseAmount($baseTotalTax); $totalTaxRounded = $this->_calculator->round($totalTax); $baseTotalTaxRounded = $this->_calculator->round($totalTaxRounded); $this->_saveAppliedTaxes($address, $data['applied_rates'], $totalTaxRounded, $baseTotalTaxRounded, $rate); } return $this; }
/** * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param Varien_Object $taxRateRequest * @param array $taxGroups * @param array $itemTaxGroups * @param boolean $catalogPriceInclTax */ protected function _totalBaseProcessItemTax($item, $taxRateRequest, &$taxGroups, &$itemTaxGroups, $catalogPriceInclTax) { $taxRateRequest->setProductClassId($item->getProduct()->getTaxClassId()); $rate = $this->_calculator->getRate($taxRateRequest); $item->setTaxAmount(0); $item->setBaseTaxAmount(0); $item->setHiddenTaxAmount(0); $item->setBaseHiddenTaxAmount(0); $item->setTaxPercent($rate); $item->setDiscountTaxCompensation(0); $rowTotalInclTax = $item->getRowTotalInclTax(); $recalculateRowTotalInclTax = false; if (!isset($rowTotalInclTax)) { $item->setRowTotalInclTax($item->getTaxableAmount()); $item->setBaseRowTotalInclTax($item->getBaseTaxableAmount()); $recalculateRowTotalInclTax = true; } $appliedRates = $this->_calculator->getAppliedRates($taxRateRequest); if ($catalogPriceInclTax) { $taxGroups[(string) $rate]['applied_rates'] = $appliedRates; $taxGroups[(string) $rate]['incl_tax'] = $item->getIsPriceInclTax(); $this->_aggregateTaxPerRate($item, $rate, $taxGroups); } else { //need to calculate each tax separately foreach ($appliedRates as $appliedTax) { $taxId = $appliedTax['id']; $taxRate = $appliedTax['percent']; $taxGroups[$taxId]['applied_rates'] = array($appliedTax); $taxGroups[$taxId]['incl_tax'] = $item->getIsPriceInclTax(); $this->_aggregateTaxPerRate($item, $taxRate, $taxGroups, $taxId, $recalculateRowTotalInclTax); } //We need to calculate weeeAmountInclTax using multiple tax rate here //because the _calculateWeeeTax and _calculateRowWeeeTax only take one tax rate if ($this->_weeeHelper->isEnabled() && $this->_weeeHelper->isTaxable()) { $this->_calculateWeeeAmountInclTax($item, $appliedRates, false); $this->_calculateWeeeAmountInclTax($item, $appliedRates, true); } } if ($rate > 0) { $itemTaxGroups[$item->getId()] = $appliedRates; } return; }
/** * Calculate address total tax based on address subtotal * * @param Mage_Sales_Model_Quote_Address $address * @param Varien_Object $taxRateRequest * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _totalBaseCalculation(Mage_Sales_Model_Quote_Address $address, $taxRateRequest) { $items = $address->getAllItems(); $store = $address->getQuote()->getStore(); $taxGroups = array(); foreach ($items as $item) { /** * Child item's tax we calculate for parent - that why we skip them */ if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $rate = $this->_calculator->getRate($taxRateRequest->setProductClassId($child->getProduct()->getTaxClassId())); $taxGroups[(string) $rate]['applied_rates'] = $this->_calculator->getAppliedRates($taxRateRequest); $this->_aggregateTaxPerRate($child, $rate, $taxGroups); } $this->_recalculateParent($item); } else { $rate = $this->_calculator->getRate($taxRateRequest->setProductClassId($item->getProduct()->getTaxClassId())); $taxGroups[(string) $rate]['applied_rates'] = $this->_calculator->getAppliedRates($taxRateRequest); $this->_aggregateTaxPerRate($item, $rate, $taxGroups); } } $inclTax = $this->_usePriceIncludeTax($store); foreach ($taxGroups as $rateKey => $data) { $rate = (double) $rateKey; $totalTax = $this->_calculator->calcTaxAmount(array_sum($data['totals']), $rate, $inclTax); $baseTotalTax = $this->_calculator->calcTaxAmount(array_sum($data['base_totals']), $rate, $inclTax); $this->_addAmount($totalTax); $this->_addBaseAmount($baseTotalTax); $this->_saveAppliedTaxes($address, $data['applied_rates'], $totalTax, $baseTotalTax, $rate); } return $this; }
/** * Calculate item price and row total including/excluding tax based on total price rounding level * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param Varien_Object $request * * @return Mage_Tax_Model_Sales_Total_Quote_Subtotal */ protected function _totalBaseCalculation($item, $request) { $calc = $this->_calculator; $request->setProductClassId($item->getProduct()->getTaxClassId()); $rate = $calc->getRate($request); $qty = $item->getTotalQty(); $price = $taxPrice = $this->_calculator->round($item->getCalculationPriceOriginal()); $basePrice = $baseTaxPrice = $this->_calculator->round($item->getBaseCalculationPriceOriginal()); $subtotal = $taxSubtotal = $this->_calculator->round($item->getRowTotal()); $baseSubtotal = $baseTaxSubtotal = $this->_calculator->round($item->getBaseRowTotal()); // if we have a custom price, determine if tax should be based on the original price $taxOnOrigPrice = !$this->_helper->applyTaxOnCustomPrice($this->_store) && $item->hasCustomPrice(); if ($taxOnOrigPrice) { $origSubtotal = $item->getOriginalPrice() * $qty; $baseOrigSubtotal = $item->getBaseOriginalPrice() * $qty; } $item->setTaxPercent($rate); if ($this->_config->priceIncludesTax($this->_store)) { if ($this->_sameRateAsStore($request)) { // determine which price to use when we calculate the tax if ($taxOnOrigPrice) { $taxable = $origSubtotal; $baseTaxable = $baseOrigSubtotal; } else { $taxable = $subtotal; $baseTaxable = $baseSubtotal; } $rowTaxExact = $calc->calcTaxAmount($taxable, $rate, true, false); $rowTax = $this->_deltaRound($rowTaxExact, $rate, true); $baseRowTaxExact = $calc->calcTaxAmount($baseTaxable, $rate, true, false); $baseRowTax = $this->_deltaRound($baseRowTaxExact, $rate, true, 'base'); $taxPrice = $price; $baseTaxPrice = $basePrice; $taxSubtotal = $subtotal; $baseTaxSubtotal = $baseSubtotal; $subtotal = $subtotal - $rowTax; $baseSubtotal = $baseSubtotal - $baseRowTax; $price = $calc->round($subtotal / $qty); $basePrice = $calc->round($baseSubtotal / $qty); $isPriceInclTax = true; //Save the tax calculated $item->setRowTax($rowTax); $item->setBaseRowTax($baseRowTax); } else { $storeRate = $calc->getStoreRate($request, $this->_store); if ($taxOnOrigPrice) { // the merchant already provided a customer's price that includes tax $taxPrice = $price; $baseTaxPrice = $basePrice; // determine which price to use when we calculate the tax $taxable = $this->_calculatePriceInclTax($item->getOriginalPrice(), $storeRate, $rate); $baseTaxable = $this->_calculatePriceInclTax($item->getBaseOriginalPrice(), $storeRate, $rate); } else { // determine the customer's price that includes tax $taxPrice = $this->_calculatePriceInclTax($price, $storeRate, $rate); $baseTaxPrice = $this->_calculatePriceInclTax($basePrice, $storeRate, $rate); // determine which price to use when we calculate the tax $taxable = $taxPrice; $baseTaxable = $baseTaxPrice; } // determine the customer's tax amount based on the taxable price $tax = $this->_calculator->calcTaxAmount($taxable, $rate, true, true); $baseTax = $this->_calculator->calcTaxAmount($baseTaxable, $rate, true, true); // determine the customer's price without taxes $price = $taxPrice - $tax; $basePrice = $baseTaxPrice - $baseTax; // determine subtotal amounts $taxable *= $qty; $baseTaxable *= $qty; $taxSubtotal = $taxPrice * $qty; $baseTaxSubtotal = $baseTaxPrice * $qty; $rowTax = $this->_deltaRound($calc->calcTaxAmount($taxable, $rate, true, false), $rate, true); $baseRowTax = $this->_deltaRound($calc->calcTaxAmount($baseTaxable, $rate, true, false), $rate, true, 'base'); $subtotal = $taxSubtotal - $rowTax; $baseSubtotal = $baseTaxSubtotal - $baseRowTax; $isPriceInclTax = true; $item->setRowTax($rowTax); $item->setBaseRowTax($baseRowTax); } } else { // determine which price to use when we calculate the tax if ($taxOnOrigPrice) { $taxable = $origSubtotal; $baseTaxable = $baseOrigSubtotal; } else { $taxable = $subtotal; $baseTaxable = $baseSubtotal; } $appliedRates = $this->_calculator->getAppliedRates($request); $rowTaxes = array(); $baseRowTaxes = array(); foreach ($appliedRates as $appliedRate) { $taxId = $appliedRate['id']; $taxRate = $appliedRate['percent']; $rowTaxes[] = $this->_deltaRound($calc->calcTaxAmount($taxable, $taxRate, false, false), $taxId, false); $baseRowTaxes[] = $this->_deltaRound($calc->calcTaxAmount($baseTaxable, $taxRate, false, false), $taxId, false, 'base'); } $taxSubtotal = $subtotal + array_sum($rowTaxes); $baseTaxSubtotal = $baseSubtotal + array_sum($baseRowTaxes); $taxPrice = $calc->round($taxSubtotal / $qty); $baseTaxPrice = $calc->round($baseTaxSubtotal / $qty); $isPriceInclTax = false; } if ($item->hasCustomPrice()) { /** * Initialize item original price before declaring custom price */ $item->getOriginalPrice(); $item->setCustomPrice($price); $item->setBaseCustomPrice($basePrice); } else { $item->setConvertedPrice($price); } $item->setPrice($basePrice); $item->setBasePrice($basePrice); $item->setRowTotal($subtotal); $item->setBaseRowTotal($baseSubtotal); $item->setPriceInclTax($taxPrice); $item->setBasePriceInclTax($baseTaxPrice); $item->setRowTotalInclTax($taxSubtotal); $item->setBaseRowTotalInclTax($baseTaxSubtotal); $item->setTaxableAmount($taxable); $item->setBaseTaxableAmount($baseTaxable); $item->setIsPriceInclTax($isPriceInclTax); if ($this->_config->discountTax($this->_store)) { $item->setDiscountCalculationPrice($taxSubtotal / $qty); $item->setBaseDiscountCalculationPrice($baseTaxSubtotal / $qty); } elseif ($isPriceInclTax) { $item->setDiscountCalculationPrice($subtotal / $qty); $item->setBaseDiscountCalculationPrice($baseSubtotal / $qty); } return $this; }