5

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.

The difference between the wrong, displayed value and the correct value can be seen on this picture: (The short version: the subtotal will include shipping tax, although shipping tax is already icluded in shipping) http://i731.photobucket.com/albums/ww318/vitamin6/orderview_fixed.jpg

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: http://i731.photobucket.com/albums/ww318/vitamin6/orderview_freeship.jpg

The freelancer edited the following file to fix the wrong tax calculation: app\code\local\Mage\Sales\Model\Order\Invoice\Total\Subtotal.php

In there the following code:

    if ($invoice->isLast()) {
        $subtotal = $allowedSubtotal;
        $baseSubtotal = $baseAllowedSubtotal;
        $subtotalInclTax = $allowedSubtotalInclTax;
        $baseSubtotalInclTax  = $baseAllowedSubtotalInclTax;

was replaced with this one:

    if ($invoice->isLast()) {
        $subtotal = $allowedSubtotal;
        $baseSubtotal = $baseAllowedSubtotal;
        //$subtotalInclTax = $allowedSubtotalInclTax;
        //$baseSubtotalInclTax  = $baseAllowedSubtotalInclTax;
        $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
        $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);

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!

loeffel
  • 475
  • 1
  • 5
  • 14

2 Answers2

0

There is a bug with the sorting of totals which can cause pretty weired issues.

Do you have any modules that add totals?

Have a look at this one: https://stackoverflow.com/a/11954867/288568

Community
  • 1
  • 1
Alex
  • 32,506
  • 16
  • 106
  • 171
0

This was a long time ago and for me the issue got resolved with one of the magento updates (I am on 1.8.1.0 at the moment). I went through my old files and all I could find is this edit:

app\code\core\Mage\Sales\Model\Order\Invoice\Total\Subtotal.php (taken from 1.7.0.2)

<?php /**  * Magento  *  * NOTICE OF LICENSE  *  * This source file is subject to the Open Software License (OSL 3.0)  * that is bundled with this package in the file LICENSE.txt.  * It is also available through the world-wide-web at this URL:  * http://opensource.org/licenses/osl-3.0.php  * If you did not receive a copy of the license and are unable to  * obtain it through the world-wide-web, please send an email  * to license@magentocommerce.com so we can send you a copy immediately.  *  * DISCLAIMER  *  * Do not edit or add to this file if you wish to upgrade Magento to newer  * versions in the future. If you wish to customize Magento for your  * needs please refer to http://www.magentocommerce.com for more information.  *  * @category    Mage  * @package     Mage_Sales  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL
3.0)  */


class Mage_Sales_Model_Order_Invoice_Total_Subtotal extends Mage_Sales_Model_Order_Invoice_Total_Abstract {
    /**
     * Collect invoice subtotal
     *
     * @param   Mage_Sales_Model_Order_Invoice $invoice
     * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
     */
    public function collect(Mage_Sales_Model_Order_Invoice $invoice)
    {
        $subtotal       = 0;
        $baseSubtotal   = 0;
        $subtotalInclTax= 0;
        $baseSubtotalInclTax = 0;

        $order = $invoice->getOrder();

        foreach ($invoice->getAllItems() as $item) {
            if ($item->getOrderItem()->isDummy()) {
                continue;
            }

            $item->calcRowTotal();

            $subtotal       += $item->getRowTotal();
            $baseSubtotal   += $item->getBaseRowTotal();
            $subtotalInclTax+= $item->getRowTotalInclTax();
            $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
        }

        $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
        $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
        $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount()
                + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced();
        $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount()
                + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced();

        /**
         * Check if shipping tax calculation is included to current invoice.
         */
        $includeShippingTax = true;
        foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
            if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
                $includeShippingTax = false;
                break;
            }
        }

        if ($includeShippingTax) {
            $allowedSubtotalInclTax     -= $order->getShippingTaxAmount();
            $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount();
        } else {
            $allowedSubtotalInclTax     += $order->getShippingHiddenTaxAmount();
            $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount();
        }

        if ($invoice->isLast()) {
            $subtotal = $allowedSubtotal;
            $baseSubtotal = $baseAllowedSubtotal;
            $subtotalInclTax = $allowedSubtotalInclTax;
            $baseSubtotalInclTax  = $baseAllowedSubtotalInclTax;
        } else {
            $subtotal = min($allowedSubtotal, $subtotal);
            $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
            $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
            $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
        }

        $invoice->setSubtotal($subtotal);
        $invoice->setBaseSubtotal($baseSubtotal);
        $invoice->setSubtotalInclTax($subtotalInclTax);
        $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);

        $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
        $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
        return $this;
    } }
loeffel
  • 475
  • 1
  • 5
  • 14