1

Magento CE 1.7. Say, I have a product, $1799.99 (incl. 8.25% tax) and a 10% off coupon (-$180.00 discount amount). The "Apply Customer Tax" option is set to After Discount, and I am using the "Unit Price" tax calculation method. While debugging I landed at this code that calculates the tax total:

case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL:
    $discountAmount     = $item->getDiscountAmount() / $qty;
    $baseDiscountAmount = $item->getBaseDiscountAmount() / $qty;

    $unitTax = $this->_calculator->calcTaxAmount($price, $rate, $inclTax);
    $discountRate = ($unitTax/$price) * 100;
    $unitTaxDiscount = $this->_calculator->calcTaxAmount($discountAmount, $discountRate, $inclTax, false);
    $unitTax = max($unitTax - $unitTaxDiscount, 0);
    $baseUnitTax = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax);
    $baseDiscountRate = ($baseUnitTax/$basePrice) * 100;
    $baseUnitTaxDiscount = $this->_calculator
        ->calcTaxAmount($baseDiscountAmount, $baseDiscountRate, $inclTax, false);
    $baseUnitTax = max($baseUnitTax - $baseUnitTaxDiscount, 0);

app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php, line 379

Note how $discountRate is obtained: instead of using the tax rate applied to calculate the unit tax it calculates its own value that can be expressed in terms of $rate as $discountRate = $rate / (1 + $rate), or, given $rate = 8.25% (0.0825): 0.0825 / 1.0825 = 0.0762. The discount tax calculated with this rate is then $12.75 and leads to the tax total of $137.18 - $12.75 = $124.43.

Interesting is that in the same routine a few lines below, the hidden tax (i.e. discount amount tax) is calculated using the $rate value (0.0825 in my example):

if ($inclTax && $discountAmount > 0) {
    $hiddenTax      = $this->_calculator->calcTaxAmount($discountAmount, $rate, $inclTax, false);
    $baseHiddenTax  = $this->_calculator->calcTaxAmount($baseDiscountAmount, $rate, $inclTax, false);
    $this->_hiddenTaxes[] = array(
        'rate_key'   => $rateKey,
        'qty'        => $qty,
        'item'       => $item,
        'value'      => $hiddenTax,
        'base_value' => $baseHiddenTax,
        'incl_tax'   => $inclTax,
    );

app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php, line 393

This “hidden tax” total (in my example: $180 - $180 / (100 + 8.25%) / 100 = $13.72) is later used to calculate the grand total, as in sum of net subtotal, tax and hidden tax, minus discount: $1662.81 + $124.43 + $13.72 - $180.00 = $1,620.96. The problem here is that while calculating the tax total the system has used two different rates on the same discount amount! Is it a bug or a feature?

2

Apparently, the issue has been addressed in version CE 1.8.0.0 - the weird $discountRate variable is gone, and both unit tax and hidden tax use the same rate.

  • 1
    I found this pretty useful, thanks. Interesting it was a year ago to the day. – pspahn Oct 29 '14 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.