title: Magento - Apply Tax On Custom Quote Totals Field
body: <p>I have created a surcharge module for Magento which adds a custom total field to the quote. The surcharge is input into Magento including tax. I have successfully got the module adding the surcharge to the quote and the grand total is correct on the checkout page. </p>
  
  <p>My issue comes when trying to apply tax to the surcharge so that it gets included and displayed in the tax field on the checkout page. Currently it only includes the tax for the products and the shipping. </p>
  
  <p>I have managed to calculate the tax for the surcharge but cant't get the tax applied to the quote so that it is displayed in the tax field but also don't think my approach is quite correct either. Any help would be much appreciated.</p>
  
  <pre><code>class ********_Deposits_Model_Quote_Address_Total_Surcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract {
  
   protected $_config = null;
  
   public function __construct()
   {
      $this-&gt;setCode('surcharge_price');
      $this-&gt;_config      = Mage::getSingleton('tax/config');
      $this-&gt;_store = Mage::app()-&gt;getStore();
   }
  
   protected function _calculateTax(Mage_Sales_Model_Quote_Address $address)
  {
      $calculator     = Mage::getSingleton('tax/calculation');
      $inclTax        = $this-&gt;_config-&gt;priceIncludesTax($this-&gt;_store);
  
      $taxRateRequest = $calculator-&gt;getRateRequest(
          $address,
          $address-&gt;getQuote()-&gt;getBillingAddress(),
          $address-&gt;getQuote()-&gt;getCustomerTaxClassId(),
          $this-&gt;_store
      );
  
      $taxRateRequest-&gt;setProductClassId(Mage::getStoreConfig('********/surcharges/tax_class', $this-&gt;_store));
  
      $rate = $calculator-&gt;getRate($taxRateRequest);
  
      $baseTax = $tax = $calculator-&gt;calcTaxAmount($address-&gt;getBaseSurchargePriceAmount(), $rate, $inclTax, true);
  
  
  }
  
  /**
   * Collect address subtotal
   *
   * @param   ********_Surcharges_Model_Quote_Address $address
   * @return  ********_Surcharges_Model_Quote_Address_Total_Surcharge
   */
  public function collect(Mage_Sales_Model_Quote_Address $address)
  {
  
      parent::collect($address);
  
      $this-&gt;_setAmount(0)-&gt;_setBaseAmount(0);
  
      // If Surcharges Is Enabled Then Calculate Away :-)
      if(Mage::getStoreConfig('********/surcharges/surcharge_enabled')) {
  
        $items = $this-&gt;_getAddressItems($address);
        if (!count($items)) {
            return $this;
        }
  
        // Calculate Total Surcharge For Items In Quote (Base Prices!)
        $surcharge = 0.0;
        foreach ($items as $item) {
          $price = $item-&gt;getData('base_surcharge_price', null);
          if(isset($price)) {
            $surcharge += $item-&gt;getData('base_surcharge_price') * $item-&gt;getQty();
          }
        }
  
       $this-&gt;_setAmount($surcharge);
       $this-&gt;_setBaseAmount($surcharge);
  
       $this-&gt;_calculateTax($address);
      }
  
  
      return $this;
  }
  
  public function fetch(Mage_Sales_Model_Quote_Address $address)
  {
  
    if(Mage::getStoreConfig('********/surcharges/surcharge_enabled')) {
      $surcharge = $address-&gt;getSurchargePriceAmount();
      if(isset($surcharge) &amp;&amp; $surcharge &gt; 0) {
        $address-&gt;addTotal(array(
            'code'  =&gt; $this-&gt;getCode(),
            'title' =&gt; Mage::getStoreConfig('********/surcharges/surcharge_label'),
            'value' =&gt; $surcharge
        ));
      }
    }
  
    return $this;
  }
  
  public function getLabel()
  {
      return Mage::getStoreConfig('********/surcharges/surcharge_label');
  }
  </code></pre>