/** * Add extra amount which should be taxable by regular tax * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $value * @param float $baseValue * @param float $rowValue * @param float $baseRowValue * @return Mage_Weee_Model_Total_Quote_Weee */ protected function _processTaxSettings($item, $value, $baseValue, $rowValue, $baseRowValue) { if ($this->_helper->isTaxable($this->_store) && $rowValue) { if (!$this->_config->priceIncludesTax($this->_store)) { $item->setExtraTaxableAmount($value)->setBaseExtraTaxableAmount($baseValue)->setExtraRowTaxableAmount($rowValue)->setBaseExtraRowTaxableAmount($baseRowValue); } $item->unsRowTotalInclTax()->unsBaseRowTotalInclTax()->unsPriceInclTax()->unsBasePriceInclTax(); $this->_isTaxAffected = true; } 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, $taxId = null, $recalculateRowTotalInclTax = false) { $inclTax = $item->getIsPriceInclTax(); $rateKey = $taxId == null ? (string) $rate : $taxId; $taxSubtotal = $subtotal = $item->getTaxableAmount(); $baseTaxSubtotal = $baseSubtotal = $item->getBaseTaxableAmount(); $isWeeeEnabled = $this->_weeeHelper->isEnabled(); $isWeeeTaxable = $this->_weeeHelper->isTaxable(); 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; $weeeTax = null; $baseWeeeTax = null; $discount = 0; $rowTaxBeforeDiscount = 0; $baseRowTaxBeforeDiscount = 0; $weeeRowTaxBeforeDiscount = 0; $baseWeeeRowTaxBeforeDiscount = 0; 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: $rowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false); $baseRowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false); if ($isWeeeEnabled && $isWeeeTaxable) { $weeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate, false); $baseWeeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate); $rowTaxBeforeDiscount += $weeeRowTaxBeforeDiscount; $baseRowTaxBeforeDiscount += $baseWeeeRowTaxBeforeDiscount; $taxGroups[$rateKey]['weee_tax'][] = $this->_deltaRound($weeeRowTaxBeforeDiscount, $rateKey, $inclTax); $taxGroups[$rateKey]['base_weee_tax'][] = $this->_deltaRound($baseWeeeRowTaxBeforeDiscount, $rateKey, $inclTax); } $taxBeforeDiscountRounded = $rowTax = $this->_deltaRound($rowTaxBeforeDiscount, $rateKey, $inclTax); $baseTaxBeforeDiscountRounded = $baseRowTax = $this->_deltaRound($baseRowTaxBeforeDiscount, $rateKey, $inclTax, 'base'); $item->setTaxAmount($item->getTaxAmount() + max(0, $rowTax)); $item->setBaseTaxAmount($item->getBaseTaxAmount() + max(0, $baseRowTax)); 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(); } //We remove weee discount from discount if weee is not taxed if ($isWeeeEnabled && $this->_weeeHelper->includeInSubtotal()) { $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 = $this->_calculateRowWeeeTax($item->getWeeeDiscount(), $item, $rate, false); $rowTax += $weeeTax; $baseWeeeTax = $this->_calculateRowWeeeTax($item->getBaseWeeeDiscount(), $item, $rate); $baseRowTax += $baseWeeeTax; $taxGroups[$rateKey]['weee_tax'][] = $weeeTax; $taxGroups[$rateKey]['base_weee_tax'][] = $baseWeeeTax; } $rowTax = $this->_deltaRound($rowTax, $rateKey, $inclTax); $baseRowTax = $this->_deltaRound($baseRowTax, $rateKey, $inclTax, 'base'); $item->setTaxAmount($item->getTaxAmount() + max(0, $rowTax)); $item->setBaseTaxAmount($item->getBaseTaxAmount() + max(0, $baseRowTax)); //Calculate the Row taxes before discount $rowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false); $baseRowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false); if ($isWeeeTaxable) { $weeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate, false); $rowTaxBeforeDiscount += $weeeRowTaxBeforeDiscount; $baseWeeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate); $baseRowTaxBeforeDiscount += $baseWeeeRowTaxBeforeDiscount; } $taxBeforeDiscountRounded = max(0, $this->_deltaRound($rowTaxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount')); $baseTaxBeforeDiscountRounded = max(0, $this->_deltaRound($baseRowTaxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount_base')); if (!$item->getNoDiscount()) { if ($item->getWeeeTaxApplied()) { $item->setDiscountTaxCompensation($item->getDiscountTaxCompensation() + $taxBeforeDiscountRounded - max(0, $rowTax)); } } if ($inclTax && $discount > 0) { $roundedHiddenTax = $taxBeforeDiscountRounded - max(0, $rowTax); $baseRoundedHiddenTax = $baseTaxBeforeDiscountRounded - max(0, $baseRowTax); $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'qty' => 1, 'item' => $item, 'value' => $roundedHiddenTax, 'base_value' => $baseRoundedHiddenTax, 'incl_tax' => $inclTax); } break; } $rowTotalInclTax = $item->getRowTotalInclTax(); if (!isset($rowTotalInclTax) || $recalculateRowTotalInclTax) { if ($this->_config->priceIncludesTax($this->_store)) { $item->setRowTotalInclTax($subtotal); $item->setBaseRowTotalInclTax($baseSubtotal); } else { $item->setRowTotalInclTax($item->getRowTotalInclTax() + $taxBeforeDiscountRounded - $weeeRowTaxBeforeDiscount); $item->setBaseRowTotalInclTax($item->getBaseRowTotalInclTax() + $baseTaxBeforeDiscountRounded - $baseWeeeRowTaxBeforeDiscount); } } $taxGroups[$rateKey]['totals'][] = max(0, $taxSubtotal); $taxGroups[$rateKey]['base_totals'][] = max(0, $baseTaxSubtotal); $taxGroups[$rateKey]['tax'][] = max(0, $rowTax); $taxGroups[$rateKey]['base_tax'][] = max(0, $baseRowTax); return $this; }
/** * Process item fixed taxes * * @deprecated since 1.3.2.3 * @param Mage_Sales_Model_Quote_Address $address * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param bool $updateParent * @return Mage_Weee_Model_Total_Quote_Weee */ protected function _processItem(Mage_Sales_Model_Quote_Address $address, $item, $updateParent = false) { $store = $address->getQuote()->getStore(); if (!$this->_helper->isEnabled($store)) { return $this; } $custTaxClassId = $address->getQuote()->getCustomerTaxClassId(); $taxCalculationModel = Mage::getSingleton('tax/calculation'); /* @var $taxCalculationModel Mage_Tax_Model_Calculation */ $request = $taxCalculationModel->getRateRequest($address, $address->getQuote()->getBillingAddress(), $custTaxClassId, $store); $defaultRateRequest = $taxCalculationModel->getRateRequest(false, false, false, $store); $attributes = $this->_helper->getProductWeeeAttributes($item->getProduct(), $address, $address->getQuote()->getBillingAddress(), $store->getWebsiteId()); $applied = array(); $productTaxes = array(); foreach ($attributes as $k => $attribute) { $baseValue = $attribute->getAmount(); $value = $store->convertPrice($baseValue); $rowValue = $value * $item->getQty(); $baseRowValue = $baseValue * $item->getQty(); $title = $attribute->getName(); /** * Apply discount to fixed tax */ if ($item->getDiscountPercent() && $this->_helper->isDiscounted($store)) { $valueDiscount = $value / 100 * $item->getDiscountPercent(); $baseValueDiscount = $baseValue / 100 * $item->getDiscountPercent(); $rowValueDiscount = $rowValue / 100 * $item->getDiscountPercent(); $baseRowValueDiscount = $baseRowValue / 100 * $item->getDiscountPercent(); $address->setDiscountAmount($address->getDiscountAmount() + $rowValueDiscount); $address->setBaseDiscountAmount($address->getBaseDiscountAmount() + $baseRowValueDiscount); $address->setGrandTotal($address->getGrandTotal() - $rowValueDiscount); $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseRowValueDiscount); } $oneDisposition = $baseOneDisposition = $disposition = $baseDisposition = 0; /** * Apply tax percent to fixed tax */ if ($this->_helper->isTaxable($store)) { $currentPercent = $item->getTaxPercent(); $defaultPercent = $taxCalculationModel->getRate($defaultRateRequest->setProductClassId($item->getProduct()->getTaxClassId())); $valueBeforeVAT = $rowValue; $baseValueBeforeVAT = $baseRowValue; $oneDisposition = $store->roundPrice($value / (100 + $defaultPercent) * $currentPercent); $baseOneDisposition = $store->roundPrice($baseValue / (100 + $defaultPercent) * $currentPercent); $disposition = $store->roundPrice($rowValue / (100 + $defaultPercent) * $currentPercent); $baseDisposition = $store->roundPrice($baseRowValue / (100 + $defaultPercent) * $currentPercent); $item->setBaseTaxAmount($item->getBaseTaxAmount() + $baseDisposition); $item->setTaxAmount($item->getTaxAmount() + $disposition); $value -= $oneDisposition; $baseValue -= $baseOneDisposition; $rowValue -= $baseDisposition; $baseRowValue -= $disposition; $item->setWeeeTaxDisposition($item->getWeeeTaxDisposition() + $oneDisposition); $item->setBaseWeeeTaxDisposition($item->getBaseWeeeTaxDisposition() + $baseOneDisposition); $item->setWeeeTaxRowDisposition($item->getWeeeTaxRowDisposition() + $disposition); $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxRowDisposition() + $baseDisposition); // $item->setTaxBeforeDiscount($item->getTaxBeforeDiscount() + $disposition); // $item->setBaseTaxBeforeDiscount($item->getBaseTaxBeforeDiscount() + $baseDisposition); $address->setTaxAmount($address->getTaxAmount() + $disposition); $address->setBaseTaxAmount($address->getBaseTaxAmount() + $baseDisposition); $rate = $taxCalculationModel->getRate($request->setProductClassId($item->getProduct()->getTaxClassId())); $this->_saveAppliedTaxes($address, $taxCalculationModel->getAppliedRates($request), $store->roundPrice($valueBeforeVAT - $rowValue), $store->roundPrice($baseValueBeforeVAT - $baseRowValue), $rate); $address->setGrandTotal($address->getGrandTotal() + $store->roundPrice($valueBeforeVAT - $rowValue)); $address->setBaseGrandTotal($address->getBaseGrandTotal() + $store->roundPrice($baseValueBeforeVAT - $baseRowValue)); } /** * Check if need include fixed tax amount to subtotal */ if ($this->_helper->includeInSubtotal($store)) { $address->setSubtotal($address->getSubtotal() + $rowValue); $address->setBaseSubtotal($address->getBaseSubtotal() + $baseRowValue); // $address->setSubtotalWithDiscount($address->getSubtotalWithDiscount() + $rowValue); // $address->setBaseSubtotalWithDiscount($address->getBaseSubtotalWithDiscount() + $baseRowValue); } else { $address->setTaxAmount($address->getTaxAmount() + $rowValue); $address->setBaseTaxAmount($address->getBaseTaxAmount() + $baseRowValue); } $productTaxes[] = array('title' => $title, 'base_amount' => $baseValue, 'amount' => $value, 'row_amount' => $rowValue, 'base_row_amount' => $baseRowValue, 'base_amount_incl_tax' => $baseValue + $baseOneDisposition, 'amount_incl_tax' => $value + $oneDisposition, 'row_amount_incl_tax' => $rowValue + $disposition, 'base_row_amount_incl_tax' => $baseRowValue + $baseDisposition); $applied[] = array('id' => $attribute->getCode(), 'percent' => null, 'hidden' => $this->_helper->includeInSubtotal($store), 'rates' => array(array('amount' => $rowValue, 'base_amount' => $baseRowValue, 'base_real_amount' => $baseRowValue, 'code' => $attribute->getCode(), 'title' => $title, 'percent' => null, 'position' => 1, 'priority' => -1000 + $k))); $item->setBaseWeeeTaxAppliedAmount($item->getBaseWeeeTaxAppliedAmount() + $baseValue); $item->setBaseWeeeTaxAppliedRowAmount($item->getBaseWeeeTaxAppliedRowAmount() + $baseRowValue); $item->setWeeeTaxAppliedAmount($item->getWeeeTaxAppliedAmount() + $value); $item->setWeeeTaxAppliedRowAmount($item->getWeeeTaxAppliedRowAmount() + $rowValue); } $this->_helper->setApplied($item, array_merge($this->_helper->getApplied($item), $productTaxes)); if ($updateParent) { $parent = $item->getParentItem(); $parent->setBaseWeeeTaxDisposition($parent->getBaseWeeeTaxDisposition() + $item->getBaseWeeeTaxDisposition()); $parent->setWeeeTaxDisposition($parent->getWeeeTaxDisposition() + $item->getWeeeTaxDisposition()); $parent->setBaseWeeeTaxRowDisposition($parent->getBaseWeeeTaxRowDisposition() + $item->getBaseWeeeTaxRowDisposition()); $parent->setWeeeTaxRowDisposition($parent->getWeeeTaxRowDisposition() + $item->getWeeeTaxRowDisposition()); $parent->setBaseWeeeTaxAppliedAmount($parent->getBaseWeeeTaxAppliedAmount() + $item->getBaseWeeeTaxAppliedAmount()); $parent->setBaseWeeeTaxAppliedRowAmount($parent->getBaseWeeeTaxAppliedRowAmount() + $item->getBaseWeeeTaxAppliedRowAmount()); $parent->setWeeeTaxAppliedAmount($parent->getWeeeTaxAppliedAmount() + $item->getWeeeTaxAppliedAmount()); $parent->setWeeeTaxAppliedRowAmount($parent->getWeeeTaxAppliedRowAmount() + $item->getWeeeTaxAppliedRowAmount()); } if ($applied) { $this->_saveAppliedTaxes($address, $applied, $item->getWeeeTaxAppliedAmount(), $item->getBaseWeeeTaxAppliedAmount(), null); } }