title: Magento: Incorrect tax calculation on invoice totals
body: <p>I am running Magento 1.5.1.0 and used to have problems with the tax calculation on invoice totals. While the calculation would be correct for all totals in my store, the backend invoice view and pdf invoices would display incorrect totals.</p>
  
  <p>The difference between the wrong, displayed value and the correct value can be seen on this picture:
  <em>(The short version: the subtotal will include shipping tax, although shipping tax is already icluded in shipping)</em>
  <a href="http://i731.photobucket.com/albums/ww318/vitamin6/orderview_fixed.jpg" rel="noreferrer">http://i731.photobucket.com/albums/ww318/vitamin6/orderview_fixed.jpg</a></p>
  
  <p>So I posted this issue on freelancer.com and someone managed to fix it. BUT as I found out later, the fix doesn't cover every scenario - if the order has free shipping, the invoice subtotal will still be incorrect. Here is a screenshot to show the difference:
  <a href="http://i731.photobucket.com/albums/ww318/vitamin6/orderview_freeship.jpg" rel="noreferrer">http://i731.photobucket.com/albums/ww318/vitamin6/orderview_freeship.jpg</a></p>
  
  <p>The freelancer edited the following file to fix the wrong tax calculation:
  <strong>app\code\local\Mage\Sales\Model\Order\Invoice\Total\Subtotal.php</strong></p>
  
  <p>In there the following code:</p>
  
  <pre><code>    if ($invoice-&gt;isLast()) {
          $subtotal = $allowedSubtotal;
          $baseSubtotal = $baseAllowedSubtotal;
          $subtotalInclTax = $allowedSubtotalInclTax;
          $baseSubtotalInclTax  = $baseAllowedSubtotalInclTax;
  </code></pre>
  
  <p>was replaced with this one:</p>
  
  <pre><code>    if ($invoice-&gt;isLast()) {
          $subtotal = $allowedSubtotal;
          $baseSubtotal = $baseAllowedSubtotal;
          //$subtotalInclTax = $allowedSubtotalInclTax;
          //$baseSubtotalInclTax  = $baseAllowedSubtotalInclTax;
          $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
          $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
  </code></pre>
  
  <p>Could somebody point me into the right direction, how I would have to further alter the file to make the fix work for orders with free shipping? 
  More details about tax settings, etc can be given if needed - thank you in advance!</p>