关于在Odoo 15发票中显示两种不同币种总计的技术请求
在Odoo 15发票页面显示本位币和业务币种双总计的实现方案
嘿,刚好我前段时间帮客户实现过一模一样的需求!不用动Odoo核心代码,搞个自定义模块就能搞定,步骤很清晰,给你一步步拆解:
1. 先搭个基础自定义模块
如果还没自己的开发模块,先建一个,比如叫account_invoice_multi_total,结构就按Odoo标准来:
account_invoice_multi_total/ ├── __init__.py ├── __manifest__.py ├── models/ │ ├── __init__.py │ └── account_move.py └── views/ ├── __init__.py └── account_move_views.xml
然后把__manifest__.py填好,记得依赖account模块:
{ 'name': 'Invoice Multi Currency Total', 'version': '1.0', 'depends': ['account'], 'author': 'Your Name', 'category': 'Accounting', 'description': 'Show both company currency and transaction currency totals on invoice forms', 'data': [ 'views/account_move_views.xml', ], 'installable': True, 'auto_install': False, }
2. 模型层(可选,按需添加)
其实Odoo 15原生就有我们需要的字段:amount_total是系统本位币的总计,amount_total_in_currency就是业务币种的总计(自动处理汇率转换)。如果不需要自定义计算逻辑,这一步可以直接跳过。
要是你有特殊需求(比如加个自定义的合计规则),可以继承account.move模型加个计算字段:
# models/account_move.py from odoo import models, fields, api class AccountMove(models.Model): _inherit = 'account.move' transaction_currency_total = fields.Monetary( string='Transaction Currency Total', compute='_compute_transaction_currency_total', currency_field='currency_id', ) @api.depends('amount_total', 'currency_id', 'company_id.currency_id') def _compute_transaction_currency_total(self): for move in self: if move.currency_id == move.company_id.currency_id: move.transaction_currency_total = move.amount_total else: # 直接用Odoo原生计算好的外币总计 move.transaction_currency_total = move.amount_total_in_currency
3. 修改发票表单视图,显示双总计
这是核心步骤,继承原生的发票表单视图,把两个总计字段加进去。我给你两种方案,选自己顺手的:
方案一:在原总计下方新增双总计行
<!-- views/account_move_views.xml --> <odoo> <record id="view_move_form_inherit" model="ir.ui.view"> <field name="name">account.move.form.inherit.multi.total</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_move_form"/> <field name="arch" type="xml"> <!-- 找到原总计所在的div,在它后面插入新的双总计行 --> <xpath expr="//div[@name='totals']/div[last()]" position="after"> <div class="row mt-3"> <div class="col-md-6"> <div class="form-group"> <label class="font-weight-bold">Company Currency Total</label> <div class="text-right"> <field name="amount_total" class="font-weight-bold" readonly="1"/> </div> </div> </div> <div class="col-md-6"> <div class="form-group"> <label class="font-weight-bold">Transaction Currency Total</label> <div class="text-right"> <field name="amount_total_in_currency" class="font-weight-bold" readonly="1"/> </div> </div> </div> </div> </xpath> </field> </record> </odoo>
方案二:直接替换原有的单总计显示
如果你想把原来的单个总计换成两个并排的,用这个:
<!-- views/account_move_views.xml --> <odoo> <record id="view_move_form_inherit" model="ir.ui.view"> <field name="name">account.move.form.inherit.multi.total</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='amount_total']" position="replace"> <div class="row"> <div class="col-md-6"> <field name="amount_total" class="font-weight-bold" readonly="1"/> <span class="text-muted d-block text-right">Company Currency</span> </div> <div class="col-md-6"> <field name="amount_total_in_currency" class="font-weight-bold" readonly="1"/> <span class="text-muted d-block text-right">Transaction Currency</span> </div> </div> </xpath> </field> </record> </odoo>
4. 安装模块测试
把模块放到Odoo的addons目录,开启开发者模式,找到这个模块安装就行。打开外币发票就能看到两个不同的总计,本位币发票的话两个值会一致,完全符合需求。
额外小提示
- 要是想在发票列表视图也显示双总计,同样继承列表视图,把
amount_total和amount_total_in_currency加进去就行。 - 确保你的用户权限能看到这两个字段,一般会计权限都没问题。
内容的提问来源于stack exchange,提问作者henry suarez




