/** * Add row total item amount to subtotal * * @param Mage_Sales_Model_Quote_Address $address * @param Mage_Sales_Model_Quote_Item_Abstract $item * * @return Mage_Tax_Model_Sales_Total_Quote_Subtotal */ protected function _addSubtotalAmount(Mage_Sales_Model_Quote_Address $address, $item) { if ($this->_config->priceIncludesTax($this->_store)) { $subTotal = $item->getRowTotalInclTax() - $item->getRowTax(); $baseSubTotal = $item->getBaseRowTotalInclTax() - $item->getBaseRowTax(); $address->setTotalAmount('subtotal', $address->getTotalAmount('subtotal') + $subTotal); $address->setBaseTotalAmount('subtotal', $address->getBaseTotalAmount('subtotal') + $baseSubTotal); } else { $address->setTotalAmount('subtotal', $address->getTotalAmount('subtotal') + $item->getRowTotal()); $address->setBaseTotalAmount('subtotal', $address->getBaseTotalAmount('subtotal') + $item->getBaseRowTotal()); } $address->setSubtotalInclTax($address->getSubtotalInclTax() + $item->getRowTotalInclTax()); $address->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax() + $item->getBaseRowTotalInclTax()); return $this; }
/** * Aggregate row totals per tax rate in array * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @param array $taxGroups * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _aggregateTaxPerRate($item, $rate, &$taxGroups) { $inclTax = $item->getIsPriceInclTax(); $rateKey = (string) $rate; $subtotal = $item->getTaxableAmount() + $item->getExtraRowTaxableAmount(); $baseSubtotal = $item->getBaseTaxableAmount() + $item->getBaseExtraRowTaxableAmount(); $item->setTaxPercent($rate); if (!isset($taxGroups[$rateKey]['totals'])) { $taxGroups[$rateKey]['totals'] = array(); $taxGroups[$rateKey]['base_totals'] = array(); } $hiddenTax = null; $baseHiddenTax = null; switch ($this->_helper->getCalculationSequence($this->_store)) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $rowTax = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false); break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $discount = $item->getDiscountAmount(); $baseDiscount = $item->getBaseDiscountAmount(); $subtotal -= $discount; $baseSubtotal -= $baseDiscount; $rowTax = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false); break; } $rowTax = $this->_deltaRound($rowTax, $rateKey, $inclTax); $baseRowTax = $this->_deltaRound($baseRowTax, $rateKey, $inclTax, 'base'); if ($inclTax && !empty($discount)) { $hiddenTax = $item->getRowTotalInclTax() - $item->getRowTotal() - $rowTax; $baseHiddenTax = $item->getBaseRowTotalInclTax() - $item->getBaseRowTotal() - $baseRowTax; } $item->setTaxAmount(max(0, $rowTax)); $item->setBaseTaxAmount(max(0, $baseRowTax)); $item->setHiddenTaxAmount(max(0, $hiddenTax)); $item->setBaseHiddenTaxAmount(max(0, $baseHiddenTax)); $taxGroups[$rateKey]['totals'][] = max(0, $subtotal); $taxGroups[$rateKey]['base_totals'][] = max(0, $baseSubtotal); 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; }
/** * Add row total item amount to subtotal * * @param Mage_Sales_Model_Quote_Address $address * @param Mage_Sales_Model_Quote_Item_Abstract $item * @return Mage_Tax_Model_Sales_Total_Quote_Subtotal */ protected function _addSubtotalAmount(Mage_Sales_Model_Quote_Address $address, $item) { $address->setTotalAmount('subtotal', $address->getTotalAmount('subtotal') + $item->getRowTotal()); $address->setBaseTotalAmount('subtotal', $address->getBaseTotalAmount('subtotal') + $item->getBaseRowTotal()); $address->setSubtotalInclTax($address->getSubtotalInclTax() + $item->getRowTotalInclTax()); $address->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax() + $item->getBaseRowTotalInclTax()); return $this; }
/** * Aggregate row totals per tax rate in array * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @param array $taxGroups * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _aggregateTaxPerRate($item, $rate, &$taxGroups) { $store = $item->getStore(); $inclTax = $this->_usePriceIncludeTax($store); if ($inclTax) { $subtotal = $item->getTaxCalcRowTotal(); $baseSubtotal = $item->getBaseTaxCalcRowTotal(); } else { if ($item->hasCustomPrice() && $this->_helper->applyTaxOnCustomPrice($store)) { $subtotal = $item->getRowTotal(); $baseSubtotal = $item->getBaseRowTotal(); } else { $subtotal = $item->getTotalQty() * $item->getOriginalPrice(); $baseSubtotal = $item->getTotalQty() * $item->getBaseOriginalPrice(); } } $discountAmount = $item->getDiscountAmount(); $baseDiscountAmount = $item->getBaseDiscountAmount(); $qty = $item->getTotalQty(); $rateKey = (string) $rate; /** * Add extra amounts which can be taxable too */ $calcTotal = $subtotal + $item->getExtraRowTaxableAmount(); $baseCalcTotal = $baseSubtotal + $item->getBaseExtraRowTaxableAmount(); $item->setTaxPercent($rate); if (!isset($taxGroups[$rateKey]['totals'])) { $taxGroups[$rateKey]['totals'] = array(); } if (!isset($taxGroups[$rateKey]['totals'])) { $taxGroups[$rateKey]['base_totals'] = array(); } $calculationSequence = $this->_helper->getCalculationSequence($store); switch ($calculationSequence) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false); break; case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false); $discountPrice = $inclTax ? $subtotal / $qty : ($subtotal + $rowTax) / $qty; $baseDiscountPrice = $inclTax ? $baseSubtotal / $qty : ($baseSubtotal + $baseRowTax) / $qty; $item->setDiscountCalculationPrice($discountPrice); $item->setBaseDiscountCalculationPrice($baseDiscountPrice); break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $calcTotal = $calcTotal - $discountAmount; $baseCalcTotal = $baseCalcTotal - $baseDiscountAmount; $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false); break; } /** * "Delta" rounding */ $delta = isset($this->_roundingDeltas[$rateKey]) ? $this->_roundingDeltas[$rateKey] : 0; $baseDelta = isset($this->_baseRoundingDeltas[$rateKey]) ? $this->_baseRoundingDeltas[$rateKey] : 0; $rowTax += $delta; $baseRowTax += $baseDelta; $this->_roundingDeltas[$rateKey] = $rowTax - $this->_calculator->round($rowTax); $this->_baseRoundingDeltas[$rateKey] = $baseRowTax - $this->_calculator->round($baseRowTax); $rowTax = $this->_calculator->round($rowTax); $baseRowTax = $this->_calculator->round($baseRowTax); /** * Renew item amounts in case if we are working with price include tax */ if ($inclTax) { $unitTax = $this->_calculator->round($rowTax / $qty); $baseUnitTax = $this->_calculator->round($baseRowTax / $qty); if ($item->hasCustomPrice()) { $item->setCustomPrice($item->getPriceInclTax() - $unitTax); $item->setBaseCustomPrice($item->getBasePriceInclTax() - $baseUnitTax); } else { $item->setOriginalPrice($item->getPriceInclTax() - $unitTax); $item->setPrice($item->getBasePriceInclTax() - $baseUnitTax); $item->setBasePrice($item->getBasePriceInclTax() - $baseUnitTax); } $item->setRowTotal($item->getRowTotalInclTax() - $rowTax); $item->setBaseRowTotal($item->getBaseRowTotalInclTax() - $baseRowTax); } $item->setTaxAmount($rowTax); $item->setBaseTaxAmount($baseRowTax); $taxGroups[$rateKey]['totals'][] = $calcTotal; $taxGroups[$rateKey]['base_totals'][] = $baseCalcTotal; return $this; }
/** * Aggregate row totals per tax rate in array * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @param array $taxGroups * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _aggregateTaxPerRate($item, $rate, &$taxGroups) { $inclTax = $item->getIsPriceInclTax(); $rateKey = (string) $rate; $taxSubtotal = $subtotal = $item->getTaxableAmount(); $baseTaxSubtotal = $baseSubtotal = $item->getBaseTaxableAmount(); $isWeeeEnabled = $this->_weeeHelper->isEnabled(); $isWeeeTaxable = $this->_weeeHelper->isTaxable(); $item->setTaxPercent($rate); if (!isset($taxGroups[$rateKey]['totals'])) { $taxGroups[$rateKey]['totals'] = array(); $taxGroups[$rateKey]['base_totals'] = array(); $taxGroups[$rateKey]['weee_tax'] = array(); $taxGroups[$rateKey]['base_weee_tax'] = array(); } $hiddenTax = null; $baseHiddenTax = null; switch ($this->_helper->getCalculationSequence($this->_store)) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $rowTax = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false); if ($isWeeeEnabled && $isWeeeTaxable) { $weeeTax = $item->getWeeeTaxAppliedRowAmount() * $rate / 100; $baseWeeeTax = $item->getBaseWeeeTaxAppliedRowAmount() * $rate / 100; $rowTax += $weeeTax; $baseRowTax += $baseWeeeTax; $taxGroups[$rateKey]['weee_tax'][] = $this->_deltaRound($weeeTax, $rateKey, $inclTax); $taxGroups[$rateKey]['base_weee_tax'][] = $this->_deltaRound($baseWeeeTax, $rateKey, $inclTax); } break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: if ($this->_helper->applyTaxOnOriginalPrice($this->_store)) { $discount = $item->getOriginalDiscountAmount(); $baseDiscount = $item->getBaseOriginalDiscountAmount(); } else { $discount = $item->getDiscountAmount(); $baseDiscount = $item->getBaseDiscountAmount(); } //weee discount should be removed when calculating hidden tax if ($isWeeeEnabled) { $discount = $discount - $item->getWeeeDiscount(); $baseDiscount = $baseDiscount - $item->getBaseWeeeDiscount(); } $taxSubtotal = max($subtotal - $discount, 0); $baseTaxSubtotal = max($baseSubtotal - $baseDiscount, 0); $rowTax = $this->_calculator->calcTaxAmount($taxSubtotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseTaxSubtotal, $rate, $inclTax, false); if ($isWeeeEnabled && $this->_weeeHelper->isTaxable()) { $weeeTax = ($item->getWeeeTaxAppliedRowAmount() - $item->getWeeeDiscount()) * $rate / 100; $rowTax += $weeeTax; $baseWeeeTax = ($item->getBaseWeeeTaxAppliedRowAmount() - $item->getBaseWeeeDiscount()) * $rate / 100; $baseRowTax += $baseWeeeTax; $taxGroups[$rateKey]['weee_tax'][] = $weeeTax; $taxGroups[$rateKey]['base_weee_tax'][] = $baseWeeeTax; } if (!$item->getNoDiscount() && $item->getWeeeTaxApplied()) { $rowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false); $baseRowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false); if ($isWeeeTaxable) { $rowTaxBeforeDiscount += $item->getWeeeTaxAppliedRowAmount() * $rate / 100; $baseRowTaxBeforeDiscount += $item->getBaseWeeeTaxAppliedRowAmount() * $rate / 100; } } if ($inclTax && $discount > 0) { $hiddenTax = $this->_calculator->calcTaxAmount($discount, $rate, $inclTax, false); $baseHiddenTax = $this->_calculator->calcTaxAmount($baseDiscount, $rate, $inclTax, false); $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'qty' => 1, 'item' => $item, 'value' => $hiddenTax, 'base_value' => $baseHiddenTax, 'incl_tax' => $inclTax); } break; } $rowTax = $this->_deltaRound($rowTax, $rateKey, $inclTax); $baseRowTax = $this->_deltaRound($baseRowTax, $rateKey, $inclTax, 'base'); $item->setTaxAmount(max(0, $rowTax)); $item->setBaseTaxAmount(max(0, $baseRowTax)); if (isset($rowTaxBeforeDiscount) && isset($baseRowTaxBeforeDiscount)) { $taxBeforeDiscount = max(0, $this->_deltaRound($rowTaxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount')); $baseTaxBeforeDiscount = max(0, $this->_deltaRound($baseRowTaxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount_base')); $item->setDiscountTaxCompensation($taxBeforeDiscount - max(0, $rowTax)); $item->setBaseDiscountTaxCompensation($baseTaxBeforeDiscount - max(0, $baseRowTax)); } $rowTotalInclTax = $item->getRowTotalInclTax(); if (!isset($rowTotalInclTax)) { $weeeTaxBeforeDiscount = 0; $baseWeeeTaxBeforeDiscount = 0; if ($isWeeeTaxable) { $weeeTaxBeforeDiscount = $item->getWeeeTaxAppliedRowAmount() * $rate / 100; $baseWeeeTaxBeforeDiscount = $item->getBaseWeeeTaxAppliedRowAmount() * $rate / 100; } if ($this->_config->priceIncludesTax($this->_store)) { $item->setRowTotalInclTax($subtotal + $weeeTaxBeforeDiscount); $item->setBaseRowTotalInclTax($baseSubtotal + $baseWeeeTaxBeforeDiscount); } else { $taxCompensation = $item->getDiscountTaxCompensation() ? $item->getDiscountTaxCompensation() : 0; $item->setRowTotalInclTax($subtotal + $rowTax + $taxCompensation); $item->setBaseRowTotalInclTax($baseSubtotal + $baseRowTax + $item->getBaseDiscountTaxCompensation()); } } $taxGroups[$rateKey]['totals'][] = max(0, $taxSubtotal); $taxGroups[$rateKey]['base_totals'][] = max(0, $baseTaxSubtotal); return $this; }