Fixing Issues With Magento’s Massaction PDF Tax Values

There appears to be an issue in Magento’s Massaction Invoice PDF. The issue presents itself when outputting the total tax for the invoice, which will be completely different to the amount displayed when the PDF is created from the view invoice page.

The issue exists in the Mage_Tax_Helper_Data class’ getCalculatedTaxes method. The first few lines decide what the variable $current will be;

if ($this->_getFromRegistry('current_invoice')) {
    $current = $this->_getFromRegistry('current_invoice');
} elseif ($this->_getFromRegistry('current_creditmemo')) {
    $current = $this->_getFromRegistry('current_creditmemo');
} else {
    $current = $source;
}

In the single version of PDF creation, the current_invoice registry key is set and is used going forward. This is omitted from the massaction update, so the method falls back to the $source parameter, which happens to be an instance of an order object.

The fix is to override Mage_Sales_Model_Order_Pdf_Invoice and create a new getPdf method, with just a two line addition inside of the foreach:

public function getPdf($invoices = array())
{
    $this->_beforeGetPdf();
    $this->_initRenderer('invoice');

    $pdf = new Zend_Pdf();
    $this->_setPdf($pdf);
    $style = new Zend_Pdf_Style();
    $this->_setFontBold($style, 10);

    foreach ($invoices as $invoice) {
        // Set the invoice here, as not doing this (E.g. default Magento) causes the wrong value to be calculated
        // when using the massaction pdf action. Unregister first, otherwise only the first invoice will be set
        Mage::unregister('current_invoice');
        Mage::register('current_invoice', $invoice);
        
        if ($invoice->getStoreId()) {
            Mage::app()->getLocale()->emulate($invoice->getStoreId());
            Mage::app()->setCurrentStore($invoice->getStoreId());
        }
        $page  = $this->newPage();
        $order = $invoice->getOrder();
        /* Add image */
        $this->insertLogo($page, $invoice->getStore());
        /* Add address */
        $this->insertAddress($page, $invoice->getStore());
        /* Add head */
        $this->insertOrder(
            $page,
            $order,
            Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())
        );
        /* Add document text and number */
        $this->insertDocumentNumber(
            $page,
            Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId()
        );
        /* Add table */
        $this->_drawHeader($page);
        /* Add body */
        foreach ($invoice->getAllItems() as $item){
            if ($item->getOrderItem()->getParentItem()) {
                continue;
            }
            /* Draw item */
            $this->_drawItem($item, $page, $order);
            $page = end($pdf->pages);
        }
        /* Add totals */
        $this->insertTotals($page, $invoice);
        if ($invoice->getStoreId()) {
            Mage::app()->getLocale()->revert();
        }
    }
    $this->_afterGetPdf();
    return $pdf;
}

And massaction PDFs will once again have the correct tax amount.

Magento Tax Snippets

Getting the Customer’s Address Country For Tax Calculations

The following will perform the following fallbacks:

-The quote’s shipping address
-The customer’s shipping address
-The customer’s billing address
-The default system configuration country

class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract {
    const COUNTRY_CODE_GB = "GB";

    public function isDeliveryAddressUk(){
        /** @var Mage_Sales_Model_Quote $quote */
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $address = $quote->getShippingAddress();

        if($address && $address->getCountryId()){
            return $address->getCountryId() == self::COUNTRY_CODE_GB;
        }

        $cSession = Mage::getSingleton('customer/session');

        if($cSession->isLoggedIn()){
            $customer = $cSession->getCustomer();
            $address = $customer->getDefaultShippingAddress();

            if($address && $address->getCountryId()){
                return $address->getCountryId() == self::COUNTRY_CODE_GB;
            }

            $customer->getDefaultBillingAddress();

            if($address && $address->getCountryId()){
                return $address->getCountryId() == self::COUNTRY_CODE_GB;
            }
        }
        
        return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_COUNTRY) == self::COUNTRY_CODE_GB;
    }
}

Checking the Tax Display Type

// Including Tax
if(Mage::helper('tax')->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX){
    ...
}

// Excluding Tax
if(Mage::helper('tax')->getPriceDisplayType() == Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX){
    ...
}