Magento - Grand Total To Be Charged on Invoice
When a customer buys something from a Magento store they receive an email confirmation which includes a line at the bottom for "Grand Total To Be Charged". This shows the total in the stores base currency.
I would like to add this figure to the invoices which get printed from开发者_JAVA技巧 the backend. How can this be done?
Lets see what is going on.
As we can see from the url (www.example.com/index.php/admin/sales_invoice/view/invoice_id/[some_id]/) Mage_Adminhtml_Sales_Order_InvoiceController
's view
action is executed and this is corresponds to <adminhtml_sales_order_invoice_view>
layout (app/design/adminhtml/default/default/layout/sales.xml) node. There we can see this:
<adminhtml_sales_order_invoice_view>
<reference name="content">
<block type="adminhtml/sales_order_invoice_view" name="sales_invoice_view">
...
We can see there that top block is Mage_Adminhtml_Block_Sales_Order_Invoice_View
. There we can see (at the end of __construct()
method):
if ($this->getInvoice()->getId()) {
$this->_addButton('print', array(
'label' => Mage::helper('sales')->__('Print'),
'class' => 'save',
'onclick' => 'setLocation(\''.$this->getPrintUrl().'\')'
)
);
}
and
public function getPrintUrl()
{
return $this->getUrl('*/*/print', array(
'invoice_id' => $this->getInvoice()->getId()
));
}
Here we can see that our invoice PDF is created from a print
action of current module/controller so lets see Mage_Adminhtml_Sales_Order_InvoiceController
again and search for a required action method:
/**
* Create pdf for current invoice
*/
public function printAction()
{
$this->_initInvoice();
parent::printAction();
}
and in parent Mage_Adminhtml_Controller_Sales_Invoice
:
public function printAction()
{
if ($invoiceId = $this->getRequest()->getParam('invoice_id')) {
if ($invoice = Mage::getModel('sales/order_invoice')->load($invoiceId)) {
$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($invoice));
$this->_prepareDownloadResponse('invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
'.pdf', $pdf->render(), 'application/pdf');
}
}
else {
$this->_forward('noRoute');
}
}
As we can see the PDF is created by sales/order_pdf_invoice
model (or Mage_Sales_Model_Order_Pdf_Invoice
class). Inside of a getPdf()
method is a call $this->insertTotals($page, $invoice);
which is defined in Mage_Sales_Model_Order_Pdf_Abstract
class. My suggestion is to overwrite Mage_Sales_Model_Order_Pdf_Invoice
class and define there a custom insertTotals($page, $invoice)
method to achieve what you need. I decided to give a full picture of the situation to let you decide how to solve this task.
Useful link - Overriding a model class in Magento
In short all you need to do is add some XML:
<global>
<pdf>
<totals>
<mygrand_total translate="title">
<title>Grand Total To Be Charged</title>
<source_field>base_grand_total</source_field>
<font_size>8</font_size>
<display_zero>1</display_zero>
<sort_order>705</sort_order>
</mygrand_total>
</totals>
</pdf>
</global>
I would create a separate module under app/code/local/Yourcompanyname/PDFBasetotal
and enable the module via etc/modules/
after which create a config.xml
file under Yourcompanyname/PDFBasetotal/etc/
here I would add the above XML as to avoid any touching of core functionality.
As to why this works see the method below from the file: app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php
Note the $totals = Mage::getConfig()->getNode('global/pdf/totals')->asArray();
as this is what is looped over when creating the total output.
protected function _getTotalsList($source)
{
$totals = Mage::getConfig()->getNode('global/pdf/totals')->asArray();
usort($totals, array($this, '_sortTotalsList'));
$totalModels = array();
foreach ($totals as $index => $totalInfo) {
if (!empty($totalInfo['model'])) {
$totalModel = Mage::getModel($totalInfo['model']);
if ($totalModel instanceof Mage_Sales_Model_Order_Pdf_Total_Default) {
$totalInfo['model'] = $totalModel;
} else {
Mage::throwException(
Mage::helper('sales')->__('PDF total model should extend Mage_Sales_Model_Order_Pdf_Total_Default')
);
}
} else {
$totalModel = Mage::getModel($this->_defaultTotalModel);
}
$totalModel->setData($totalInfo);
$totalModels[] = $totalModel;
}
return $totalModels;
}
精彩评论