title: Adding A Custom Discount Order Total in Magento Does Not Change Sales Tax
body: <p>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).</p>
  
  <p>Take for example the following:</p>
  
  <pre><code>Subtotal:              $856.49
  Multi Unit Discounts: -$22.50
  Shipping:              $10.96
  Tax:                   $52.05
  Grand Total:           $897.00
  </code></pre>
  
  <p>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).</p>
  
  <p>In my config.xml file I have the following to get my order total working in the system:</p>
  
  <pre><code>     &lt;sales&gt;
          &lt;quote&gt;
              &lt;totals&gt;
                  &lt;mud&gt;
                      &lt;class&gt;Wpe_Multiunitdiscount_Model_Multiunitdiscount&lt;/class&gt;
                      &lt;before&gt;tax&lt;/before&gt;
                  &lt;/mud&gt;
              &lt;/totals&gt;
          &lt;/quote&gt;
      &lt;/sales&gt;    
  </code></pre>
  
  <p>The following is the contents of my order total class:</p>
  
  <pre><code>class Wpe_Multiunitdiscount_Model_Multiunitdiscount extends Mage_Sales_Model_Quote_Address_Total_Abstract {
  
  public function collect(Mage_Sales_Model_Quote_Address $address) {
  
      if ($address-&gt;getData('address_type')=='billing') return $this;
  
      $items = $address-&gt;getAllItems();
  
      $total_discount = 0;
  
      foreach($items as $item) {
          $product_discounts = Mage::helper("multiunitdiscount")-&gt;findDiscounts($item);
          if($product_discounts &gt; 0) {
              $total_discount += $product_discounts;
          }
      }
  
      $address-&gt;setMudAmount($total_discount);
  
      $address-&gt;setGrandTotal($address-&gt;getGrandTotal() - $address-&gt;getMudAmount() );
  $address-&gt;setBaseGrandTotal($address-&gt;getBaseGrandTotal() - $address-&gt;getMudAmount());
      return $this;
  }
  
  public function fetch(Mage_Sales_Model_Quote_Address $address) {
  
      if ($address-&gt;getData('address_type')=='billing') return $this;
  
      if($address-&gt;getMudAmount() &gt; 0) {
  
          $address-&gt;addTotal(array(
              'code'  =&gt; $this-&gt;getCode(),
              'title' =&gt; Mage::helper('sales')-&gt;__('Multi Unit Discounts'),
              'value' =&gt; -$address-&gt;getMudAmount(),
          ));
      }
      return $this;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>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.</p>
  
  <p>Can someone help point me in the right direction for getting the sales tax calculation correct?</p>
  
  <p>EDIT:</p>
  
  <p>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.</p>
  
  <pre><code>public function collect(Mage_Sales_Model_Quote_Address $address) {
  
      if ($address-&gt;getData('address_type')=='billing') return $this;
  
      $address-&gt;setMudDiscount(10);
      $address-&gt;setDiscountAmount($address-&gt;getDiscountAmount() + $address-&gt;getMudDiscount());
      $address-&gt;setBaseDiscountAmount($address-&gt;getBaseDiscountAmount() + $address-&gt;getMudDiscount());
  
      return $this;
  }
  
  public function fetch(Mage_Sales_Model_Quote_Address $address) {
  
      if ($address-&gt;getData('address_type')=='billing') return $this;
  
      $address-&gt;addTotal(array(
          'code'  =&gt; $this-&gt;getCode(),
          'title' =&gt; Mage::helper('sales')-&gt;__('Multi Unit Discounts'),
          'value' =&gt; -$address-&gt;getMudDiscount(),
      ));
      return $this;
  }
  </code></pre>