If a surcharge or discount is applied to the shopping cart using a cart price rule (settings in the Magento admin area: Discount on prices excluding VAT
and Apply tax to amounts after discount) and the tax and total amounts are checked, errors in the Magento development become apparent. Magento always treats the discount amount as a gross amount when calculating VAT and totals. This means that even when the discount or surcharge is displayed as a net amount, the tax is deducted a second time before the tax calculation, meaning that the displayed VAT and total are too high when a discount is applied and too low when a surcharge is used. However, the displayed tax amount can be corrected, for example, as follows:
In the file \app\code\local\Mage\Tax\Model\Sales\Total\Quote\Tax.php, find this section:
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();
}
and insert the following lines directly afterwards:
$store = Mage::app()->getStore('default');
$request = Mage::getSingleton('tax/calculation')->getRateRequest(null, null, null, $store);
$taxclassid = Mage::helper('tax')->getShippingTaxClass();
$percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxclassid));
$discount = $discount*(1+$percent/100);
$baseDiscount = $baseDiscount*(1+$percent/100);
This ensures that the tax is displayed correctly. The total amount can be corrected by removing the "hidden tax":
In the file mentioned above, we now find the following section:
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,
);
}
and change the 2 lines highlighted in red there:
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' => 0,
'base_value' => 0,
'incl_tax' => $inclTax,
);
}
This should make the tax and total amounts of the Magento installation display correctly again. Tested with Magento 1.7.0.2.
