Skip to Content
Menu
This question has been flagged
3 Replies
4974 Views

I have new field in sale order line as mrp. I need to calculate  price_unit as

price_unit= mrp- discount 

When i use  this calculation, discount always becomes 0.00 whatever the discount value is, then price_unit become the mrp value. But  when i  put 
price _unit= mrp- 25 

this  gives the actual answer. But  i need to make  calculation dynamic. How  can I make it possible sir

Avatar
Discard

Hi please add your code with the question.

Author

Code:

mrp=fields.Float(string='MRP')

price_unit = fields.Float('Unit Price',compute='compute_unit', required=True, default=0.0)

discount = fields.Float(string='Discount (Rs.)')

@api.depends('mrp','discount','price_unit')

def compute_unit(self):

for line in self

line.price_unit = line.mrp - line.discount

Best Answer

Hi Sarina,

I think it would be better to post your code next time, but the following code will solve your problem (ref: https://github.com/odoo/odoo/blob/65f5fc6bf8442fc7004d0fc8e006184359f0fe4e/addons/sale/models/sale.py#L988-L995).

@api.onchange('product_id', 'price_unit', 'product_uom', 'product_uom_qty', 'tax_id')

    def _onchange_discount(self):
        self.discount = 0.0
        if not (self.product_id and self.product_uom and
                self.order_id.partner_id and self.order_id.pricelist_id and
                self.order_id.pricelist_id.discount_policy == 'without_discount' and
                self.env.user.has_group('sale.group_discount_per_so_line')):
            return

Hence, whether you change the price_unit field, the above method will be triggered as well and the discount will be reset to 0.

Best regards,

Tim

Avatar
Discard
Related Posts Replies Views Activity
2
Nov 24
25081
2
May 24
5514
3
Mar 24
4964
0
Mar 24
262
3
Feb 24
11421