如何在保存Register Payment表单时将自定义字段存入account_invoice?
实现方案(适配Odoo 14+/15+/16+版本)
1. 搭建自定义模块基础
先创建一个自定义模块(例如命名为account_payment_tax_number),包含必填的__manifest__.py文件声明模块信息。
2. 继承account.payment模型添加字段并同步逻辑
在models/account_payment.py中编写如下代码:
from odoo import models, fields, api class AccountPayment(models.Model): _inherit = 'account.payment' tax_number = fields.Char(string='Tax Number', help='Customer tax number collected during payment') def action_post(self): # 执行原付款验证逻辑 res = super(AccountPayment, self).action_post() # 同步税号到关联发票 for payment in self: if payment.tax_number: # 遍历所有关联发票并写入税号 for invoice in payment.invoice_ids: invoice.write({'tax_number': payment.tax_number}) return res
补充:如果你的
account.move(Odoo14+发票模型)原本没有tax_number字段,需要先给发票模型添加该字段:class AccountMove(models.Model): _inherit = 'account.move' tax_number = fields.Char(string='Customer Tax Number')注:Odoo13及以下版本发票模型为
account.invoice,需替换对应模型名。
3. 修改付款弹窗视图显示字段
在views/account_payment_views.xml中继承付款表单视图,插入新增字段:
<odoo> <record id="view_account_payment_form_inherit" model="ir.ui.view"> <field name="name">account.payment.form.inherit</field> <field name="model">account.payment</field> <field name="inherit_id" ref="account.view_account_payment_form"/> <field name="arch" type="xml"> <!-- 在客户字段后插入税号输入框 --> <field name="partner_id" position="after"> <field name="tax_number" placeholder="Enter customer tax number..."/> </field> </field> </record> </odoo>
4. 安装测试
安装自定义模块后,打开任意发票的「Register Payment」弹窗,即可看到新增的税号字段。输入内容并点击「Validate」,值会自动同步到对应发票记录中。
额外说明
- 若付款对应多张发票,上述代码会将同一税号同步到所有关联发票;如需针对单张发票单独采集,可扩展弹窗逻辑添加发票级别的税号输入。
内容的提问来源于stack exchange,提问作者art
相关产品推荐
相关产品推荐

