如何在 Odoo 9 中向发票表格添加新的货币归档?

how to add new monetary filed to invoice form in Odoo 9?

我正在学习再次定制Odoo 9系统。由于我自己业务的一些特点,我不得不更改帐户发票模板中的一些点。我创建了一个名为 total_residual 的新货币文件,其值由以下函数计算得出:

@api.multi
def _compute_all_residual(self):
    for invoice in self:
        invs = self.search([('state', '=', 'open'), ('partner_id', '=', invoice.partner_id.id)])
        out_invoice = 0
        in_invoice = 0
        out_refund = 0
        in_refund = 0
        for inv in invs:
            if inv.type == 'out_invoice':
                out_invoice += inv.residual
            if inv.type == 'in_invoice':
                in_invoice += inv.residual
            if inv.type == 'out_refund':
                out_refund += inv.residual
            if inv.type == 'in_refund':
                in_refund += inv.residual
        invoice.total_residual = out_invoice + in_refund - in_invoice - out_refund

现在我想添加一个新字段货币字段 (old_residual),其值为总剩余,不包括当前发票的金额。在模块上添加的正确功能是什么?以及我如何向 qweb 报告显示 old_residual 的值?感谢您的宝贵时间

只需创建新的货币字段并重写上面的方法:

@api.multi
def _compute_all_residual(self):
for invoice in self:
    invs = self.search([('state', '=', 'open'), ('partner_id', '=', invoice.partner_id.id)])
    out_invoice = 0
    in_invoice = 0
    out_refund = 0
    in_refund = 0
    for inv in invs:
        if inv.type == 'out_invoice':
            out_invoice += inv.residual
        if inv.type == 'in_invoice':
            in_invoice += inv.residual
        if inv.type == 'out_refund':
            out_refund += inv.residual
        if inv.type == 'in_refund':
            in_refund += inv.residual
    invoice.total_residual = out_invoice + in_refund - in_invoice - out_refund
    invoice.old_residual = out_invoice + in_refund - in_invoice - out_refund - invoice.amount_total

# Define new field
old_residual = fields.Monetary(string='Amount Due', compute='_compute_all_residual', store=True)

要在 qweb 报告中添加上述字段,您需要继承 qweb 模板并在其中添加此字段。

在您的 "views/report_invoice.xml".

中使用以下代码尝试
<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="report_invoice_document_custom" inherit_id="account.report_invoice_document">
            <xpath expr="//tr[@class='border-black']" position="after">
                <!-- Your Code --->
                <tr class="border-black">
                    <td><strong>Old Residual</strong></td>
                    <td class="text-right">
                         <span t-field="o.old_residual" t-field-options='{"widget": "monetary", "display_currency": "o.currency_id"}'/>
                    </td>
                </tr>
            </xpath>
        </template>
    </data>
</openerp>

在数据中的“openerp.py”文件中添加报告。

'data': ['views/report_invoice.xml',],