如何在Odoo 17社区版的ubl_bis3.xml中注入指定字段?
Odoo 17社区版PEPPOL电子发票注入自定义字段实现方案
一、模块基础结构
先搭好自定义模块的目录结构,核心文件如下:
__manifest__.py:模块配置入口models/account_move.py:扩展发票模型,添加对应字段(如果原生模型没有的话)views/account_move_views.xml:把字段加到发票表单,方便用户录入data/ubl_bis3_invoice.xml:继承原生UBL模板,注入目标字段
二、模块配置文件(manifest.py)
{ 'name': 'PEPPOL发票自定义字段注入', 'version': '1.0', 'category': 'Accounting', 'author': '自定义作者', 'depends': ['account', 'account_ubl'], 'data': [ 'views/account_move_views.xml', 'data/ubl_bis3_invoice.xml', ], 'installable': True, 'auto_install': False, }
三、扩展发票模型(models/account_move.py)
如果account_move原生没有这三个字段,先添加进去:
from odoo import models, fields class AccountMove(models.Model): _inherit = 'account.move' buyer_reference = fields.Char(string='买方采购订单标识') accounting_cost = fields.Char(string='财务记账位置') invoice_note = fields.Text(string='发票备注')
四、添加字段到发票视图(views/account_move_views.xml)
把新字段加到发票表单的合适位置,方便用户填写:
<odoo> <record id="view_move_form_inherit" model="ir.ui.view"> <field name="name">account.move.form.inherit</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_move_form"/> <field name="arch" type="xml"> <!-- 把字段加在发票参考字段后面 --> <xpath expr="//field[@name='ref']" position="after"> <field name="buyer_reference" placeholder="输入买方采购订单标识"/> <field name="accounting_cost" placeholder="输入财务记账位置"/> <field name="invoice_note" placeholder="输入发票备注"/> </xpath> </field> </record> </odoo>
五、修改UBL发票模板注入字段(data/ubl_bis3_invoice.xml)
继承Odoo原生的PEPPOL UBL BIS3模板,按规范注入三个目标字段:
<odoo> <template id="ubl_bis3_invoice_inherit" inherit_id="account_ubl.ubl_bis3_invoice"> <!-- 注入bt13: 买方采购订单标识 <cbc:ID> --> <xpath expr="//cac:OrderReference" position="inside"> <cbc:ID t-if="record.buyer_reference" t-esc="record.buyer_reference"/> </xpath> <!-- 注入bt19: 财务记账位置 <cbc:AccountingCost> --> <xpath expr="//cac:AccountingSupplierParty" position="after"> <cbc:AccountingCost t-if="record.accounting_cost" t-esc="record.accounting_cost"/> </xpath> <!-- 注入bt22: 发票备注 <cbc:Note> --> <xpath expr="//cbc:InvoiceTypeCode" position="after"> <cbc:Note t-if="record.invoice_note" t-esc="record.invoice_note"/> </xpath> </template> </odoo>
六、安装测试
- 把整个模块目录放到Odoo的
custom_addons文件夹下 - 在Odoo后台刷新模块列表,找到这个模块并安装
- 新建或编辑一张供应商发票,填写三个新字段的值
- 导出PEPPOL电子发票,打开生成的
ubl_bis3.xml文件,检查是否包含目标字段
内容的提问来源于stack exchange,提问作者geom
相关产品推荐
相关产品推荐

