简体   繁体   中英

how to compute field on odoo 13 sale order line?

I'm trying to calculate discount field on sale order line, and the method was working well in odoo 12 but in odoo 13 i get this error every time I try to add line

sale.order.line(<NewId 0x7f3dd0d624a8>,).discount_mount

here is the what I've done

class discount_cycle(models.Model):
_inherit = 'sale.order.line'

discount_mount = fields.Float(string="",  required=False , compute='discount_calculation')


@api.depends('product_id','discount','price_subtotal')
def discount_calculation(self):
    for rec in self:
        if rec.discount:
            if rec.product_uom_qty > 1:
                rec.discount_mount = ((rec.price_unit * rec.product_uom_qty) * (rec.discount / 100))
            else:
                rec.discount_mount = (rec.price_unit * (rec.discount / 100))
        pass

Note that was @api.one in odoo V 12, so how can I solve this issue and what is replacement for @api.one in this case

In odoo V13 you must assing the value to the computed field instead of pass you need to add else statement and assign the default value

     else:
             self.discount_mount = 0.0

I know this is clear if we don't have a discount then the field should be 0.0 but odoo want you you to do that

You need to assign a value in any case to the non-stored computed field, even if it is a falsy one, computed stored fields will keep their previous value if not assigned during the compute method, so don't rely on any expected default value.

The api.one decorator was removed, now it is multi-record by default. You have just to remove the decorator from your code and loop over self (which is already done in your example).

If it uses the values of other fields, it should specify those fields using depends() .

You need to replace product_id and price_subtotal with price_unit and product_uom_qty .

When discount is 0.0 , the discount_mount should also be 0.0 and in your expressions, you are dividing the discount by 100 then doing multiplications. If the value of the discount is 0.0 , it will not be a problem, the expressions will be evaluated to 0.0 and the discount_mount field will be set to 0.0 which mean that you can remove the if expression:

if rec.discount:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM