简体   繁体   English

Magento-总计将在发票上收取

[英]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". 当客户从Magento商店购买商品时,他们会收到一封电子邮件确认,该确认信的底部包含一个“要收取的总额”行。 This shows the total in the stores base currency. 这显示了商店基本货币的总数。

I would like to add this figure to the invoices which get printed from 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. 从网址(www.example.com/index.php/admin/sales_invoice/view/invoice_id/[some_id]/)中可以看到, Mage_Adminhtml_Sales_Order_InvoiceControllerview操作已执行,这与<adminhtml_sales_order_invoice_view>布局相对应(app /设计/ adminhtml /默认/默认/布局/sales.xml)节点。 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 . 我们可以看到顶部的块是Mage_Adminhtml_Block_Sales_Order_Invoice_View There we can see (at the end of __construct() method): 在那里,我们可以看到(在__construct()方法的结尾):

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: 在这里,我们可以看到发票PDF是根据当前模块/控制器的print操作创建的,因此让Mage_Adminhtml_Sales_Order_InvoiceController再次查看Mage_Adminhtml_Sales_Order_InvoiceController并搜索所需的操作方法:

/**
 * Create pdf for current invoice
 */
public function printAction()
{
    $this->_initInvoice();
    parent::printAction();
}

and in parent Mage_Adminhtml_Controller_Sales_Invoice : 并在父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) . 如我们所见,PDF是由sales/order_pdf_invoice模型(或Mage_Sales_Model_Order_Pdf_Invoice类)创建的 Inside of a getPdf() method is a call $this->insertTotals($page, $invoice); getPdf()方法内部是调用$this->insertTotals($page, $invoice); which is defined in Mage_Sales_Model_Order_Pdf_Abstract class. Mage_Sales_Model_Order_Pdf_Abstract类中定义。 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. 我的建议是覆盖Mage_Sales_Model_Order_Pdf_Invoice类,并在那里定义一个自定义insertTotals($page, $invoice)方法来实现所需的功能。 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 有用的链接- 在Magento中覆盖模型类

In short all you need to do is add some XML: 简而言之,您需要做的就是添加一些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. 我将在app/code/local/Yourcompanyname/PDFBasetotal下创建一个单独的模块,并通过etc/modules/启用该模块,然后在Yourcompanyname/PDFBasetotal/etc/下创建一个config.xml文件,此处我将添加上述XML以免任何涉及核心功能的内容。

As to why this works see the method below from the file: app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php 至于为什么有效,请参见以下文件中的方法: app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

Note the $totals = Mage::getConfig()->getNode('global/pdf/totals')->asArray(); 注意$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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM