Odoo16如何在销售订单表单中从总价扣除佣金?
Odoo16 销售订单扣除佣金后金额不生效的解决方案
核心问题分析
在Odoo16的销售订单体系中,_compute_amounts()是金额计算的基础方法,但仅重写它可能不生效,常见原因包括:方法依赖字段未正确关联、字段更新逻辑错误、视图未绑定正确字段、或被其他模块的重写覆盖。
具体解决步骤
1. 确保重写方法的正确性
正确继承sale.order模型,使用@api.depends绑定所有影响计算的字段(比如订单行金额、佣金金额),并确保更新对应的金额字段:
from odoo import models, fields, api class SaleOrder(models.Model): _inherit = 'sale.order' commission_amount = fields.Float(string='佣金', default=8.0) @api.depends('order_line.price_subtotal', 'commission_amount') def _compute_amounts(self): # 先调用父类方法计算原始金额 super(SaleOrder, self)._compute_amounts() for order in self: # 从税后总价中扣除佣金,可根据需求调整为税前扣除 order.amount_total = order.amount_total - order.commission_amount
2. 处理税前扣除的特殊场景
如果需要从税前金额中扣除佣金再计算税费,需先调整amount_untaxed,再调用父类方法重新计算:
@api.depends('order_line.price_subtotal', 'commission_amount') def _compute_amounts(self): for order in self: # 计算原始税前金额总和 original_untaxed = sum(line.price_subtotal for line in order.order_line) # 扣除佣金后的税前金额 order.amount_untaxed = original_untaxed - order.commission_amount # 调用父类方法重新计算税费和最终总价 super(SaleOrder, self)._compute_amounts()
3. 检查视图绑定与显示
如果不想修改原始amount_total字段,可以新增专门的扣除后金额字段,并在销售订单表单视图中添加:
# 新增扣除后金额字段 amount_after_commission = fields.Float(string='扣除佣金后总价', compute='_compute_after_commission') @api.depends('amount_total', 'commission_amount') def _compute_after_commission(self): for order in self: order.amount_after_commission = order.amount_total - order.commission_amount
在sale.order.form视图中添加该字段:
<field name="amount_after_commission" readonly="1"/>
4. 解决缓存与覆盖问题
- 升级自定义模块,确保代码生效
- 清除浏览器缓存,刷新Odoo后台视图
- 开启开发者模式,查看模型的方法继承链,确认是否有其他模块重写
_compute_amounts()导致逻辑被覆盖
内容的提问来源于stack exchange,提问作者Ajay Balachandran
相关产品推荐
相关产品推荐

