Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding A Custom Discount Order Total in Magento Does Not Change Sales Tax

Tags:

php

magento

I have created a custom order total that gives a discount in certain situations. The grand total always comes out correct, however the sales tax calculation is not taking my discount into account when calculating (so if I was giving a discount of $10, the sales tax amount was calculated on the entire amount before my discount).

Take for example the following:

Subtotal:              $856.49
Multi Unit Discounts: -$22.50
Shipping:              $10.96
Tax:                   $52.05
Grand Total:           $897.00

My custom discount is the Multi Unit Discounts. The tax rate is 6%. As you can see the grand total is correct based on all the line items, but the tax amount itself is not correct (it is based on all the line items except my discount).

In my config.xml file I have the following to get my order total working in the system:

     <sales>
        <quote>
            <totals>
                <mud>
                    <class>Wpe_Multiunitdiscount_Model_Multiunitdiscount</class>
                    <before>tax</before>
                </mud>
            </totals>
        </quote>
    </sales>    

The following is the contents of my order total class:

class Wpe_Multiunitdiscount_Model_Multiunitdiscount extends Mage_Sales_Model_Quote_Address_Total_Abstract {

public function collect(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    $items = $address->getAllItems();

    $total_discount = 0;

    foreach($items as $item) {
        $product_discounts = Mage::helper("multiunitdiscount")->findDiscounts($item);
        if($product_discounts > 0) {
            $total_discount += $product_discounts;
        }
    }

    $address->setMudAmount($total_discount);

    $address->setGrandTotal($address->getGrandTotal() - $address->getMudAmount() );
$address->setBaseGrandTotal($address->getBaseGrandTotal() - $address->getMudAmount());
    return $this;
}

public function fetch(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    if($address->getMudAmount() > 0) {

        $address->addTotal(array(
            'code'  => $this->getCode(),
            'title' => Mage::helper('sales')->__('Multi Unit Discounts'),
            'value' => -$address->getMudAmount(),
        ));
    }
    return $this;
}

}

For the sake of not posting a huge chunk of code in here that I am not sure is necessary, I can tell you that the helper in the above code simply returns the amount of money the discount is for that particular item in the quote.

Can someone help point me in the right direction for getting the sales tax calculation correct?

EDIT:

In order to keep this simple, I have removed a lot of my logic behind calculating the discount and am now trying to simple take $10 off the order total as a discount. As suggested I did not modify the Grand Total of the address and am now only setting the Discount Amount and Base Discount Amount. Now the sales tax does not add up and the grand total is off. Maybe if there is a good tutorial out there that someone can point me towards would help? I do not seem to be grasping how the order totals all interact with each other.

public function collect(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    $address->setMudDiscount(10);
    $address->setDiscountAmount($address->getDiscountAmount() + $address->getMudDiscount());
    $address->setBaseDiscountAmount($address->getBaseDiscountAmount() + $address->getMudDiscount());

    return $this;
}

public function fetch(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    $address->addTotal(array(
        'code'  => $this->getCode(),
        'title' => Mage::helper('sales')->__('Multi Unit Discounts'),
        'value' => -$address->getMudDiscount(),
    ));
    return $this;
}
like image 492
Josh Pennington Avatar asked Sep 22 '11 15:09

Josh Pennington


People also ask

Does grand total include discount?

On sales order mail and pdf the grand total is total without discount. On the invoices the grand total is correct.


1 Answers

Go to System > Configuration. Select "Tax" from the left hand navigation, then open the "Calculation Settings" group if it isn't already.

Try changing the "Apply Customer Tax" parameter to "After Discount"

like image 103
Jim OHalloran Avatar answered Sep 16 '22 15:09

Jim OHalloran