Skip to Content
Menu
This question has been flagged
5 Replies
12028 Views

Hello,

I experience a problem when overriding onchange method in sale order line. I can edit vals['name'] for example, but when trying to edit price_unit in any way it doesnt change a bit, and somehow it always stays on product price. I dont even know how is it computed. Should I override something more? Or maybe I override wrong method? Please help.

Avatar
Discard
Best Answer

Hi John Doe,

Try like this:

    @api.multi

@api.onchange('product_id')

def product_id_change(self):

res = super(sale_order_line, self).product_id_change()

for line in self:

             .............

             ............. # do your stuff

             line.price_unit = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

return res

Avatar
Discard
Best Answer

Hi John,

The price_unit is set by using two onchange methods. One is "product_id_change" and another one is product_uom_change. You must override both methods.


Thanks & Regards

Avatar
Discard
Author Best Answer

Hi nikesh,

I think I did what you said, but in my case the method looks different:

 @api.multi

@api.onchange('product_id')

def product_id_change(self):

if not self.product_id:

return {'domain': {'product_uom': []}}

vals = {}

domain = {'product_uom': [('category_id', '=', self.product_id.uom_id.category_id.id)]}

if not self.product_uom or (self.product_id.uom_id.category_id.id != self.product_uom.category_id.id):

vals['product_uom'] = self.product_id.uom_id

product = self.product_id.with_context(

lang=self.order_id.partner_id.lang,

partner=self.order_id.partner_id.id,

quantity=self.product_uom_qty,

date=self.order_id.date_order,

pricelist=self.order_id.pricelist_id.id,

uom=self.product_uos.id

)

name = product.name_get()[0][1]

if product.description_sale:

name += '\n' + product.description_sale

vals['name'] = name

self._compute_tax_id()

if self.order_id.pricelist_id and self.order_id.partner_id:

vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

self.update(vals)

return {'domain': domain}

Avatar
Discard

@John, Here the line vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id) decides the value of price_unit. You can either override method _fix_tax_included_price or product_id_change itself. To override the methodyou must call super.

Author

@nikesh, Thanks for all the answers. I got one more little problem, when I change the value of the price_unit and I add the product, it doesnt show the new one. The code I changed looks like this: price1 = product.price / product.uos_coeff vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(price1, product.taxes_id, self.tax_id)

@John The price_unit is set by using two onchange methods. One is "product_id_change" and another one is product_uom_change. You must override both methods. @api.onchange('product_uom', 'product_uom_qty') def product_uom_change(self): if not self.product_uom: self.price_unit = 0.0 return if self.order_id.pricelist_id and self.order_id.partner_id: product = self.product_id.with_context( lang=self.order_id.partner_id.lang, partner=self.order_id.partner_id.id, quantity=self.product_uom_qty, date_order=self.order_id.date_order, pricelist=self.order_id.pricelist_id.id, uom=self.product_uom.id, fiscal_position=self.env.context.get('fiscal_position') ) self.price_unit = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

I edited my answer above

Author

@nikesh Thank you very much, it works perfectly from the odoo side, but I still got problems when creating a sale order from website shop. It still doesnt change the price this way.