odoo11 审批流中行总额与申请单总额的计算问题
一. 问题的描述
在做审批流的过程中,涉及到这样一个问题,用户申请的行总额需要根据当前行的数量和单价相乘计算得出,这本来是一个很简单的功能需求,利用odoo的计算方法就可以轻松实现,但是在在view页面给amount设置了readonly=“1”属性之后,再次修改申请单单价或数量的时候,amount字段的值却无法跟新到数据库中。
在view层的代码如下:
"workflow_ebilling_request_form_view" model="ir.ui.view"> "name">Workflow e-Billing "type">form "model">ebilling.request.header "arch" type="xml">
二 解决问题的方案:
经过查询相关资料,了解到如果前端的field字段一旦设置了readonly=”1“字段,在后台create/write的时候,其参数values里面就不会包含此字段,对应的解决方案也就是重写这两个方法。
@api.model def write(self, values): if "unit_price" in values and "qty" not in values: values['amount'] = self.qty * values['unit_price'] if "unit_price" not in values and "qty" in values: values['amount'] = self.unit_price * values['qty'] if "unit_price" in values and "qty" in values: values['amount'] = values['unit_price'] * values['qty'] return super(BillingRequestDetail, self).write(values) @api.model def create(self, values): if "unit_price" in values and "qty" in values: values['amount'] = values['unit_price'] * values['qty'] return super(BillingRequestDetail, self).create(values)
三 对比sale模块订单行的总价处理情况
在解决以上问题的时候,发现销售订单模块也完全涉及到类似的情况,那么官方模块是如何处理这种情况的呢?
发现其并没有在view端设置readonly=“1”这个属性,而是设置了groups属性,也就是说只有有权限的才可以修改这个行总价,和我们的完全只读属性还是不太一样的。