Skip to Content
Menu
This question has been flagged
2 Replies
3105 Views

I'm trying to override an onchange function of account move line model in order to update the value of a custom field I've added in account.move model:

class SoftOneInvoiceLine(models.Model):

_inherit= "account.move.line"


change_from_user=fields.Boolean('Change by user',default=True)


@api.onchange('quantity', 'discount', 'price_unit', 'tax_ids')
def _onchange_price_subtotal(self):
super(SoftOneInvoiceLine,self)._onchange_price_subtotal()
for rec in self:
if (rec.change_from_user):

rec.move_id.global_discount=0

rec.move_id.update({'global_discount':0})
print (rec.move_id," ",rec.move_id.global_discount)
rec.change_from_user=True


When above code runs, the change doesn't reflect on the form field.


Print command displays:  account.move(,) 0.0


What is the problem and the field isn't updated on the form?

Avatar
Discard

There is no code.

Author

I somehow erased code before posting. Here is the code:

class SoftOneInvoiceLine(models.Model):

_inherit= "account.move.line"

change_from_user=fields.Boolean('Change by user',default=True)

@api.onchange('quantity', 'discount', 'price_unit', 'tax_ids')
def _onchange_price_subtotal(self):
super(SoftOneInvoiceLine,self)._onchange_price_subtotal()
for rec in self:
if (rec.change_from_user):
rec.move_id.global_discount=0;
rec.move_id.update({'global_discount':0})
print (rec.move_id," ",rec.move_id.global_discount)
rec.change_from_user=True

Author

I was getting error 403 forbidden when I was posting the code. Managed to post the code in a comment  but it is displayed messed up. 

Author Best Answer

I managed to find an alternative solution. I created another custom field global_discount_check in account.move which is set as invisible, computed and store=false. The field depends on invoice_line_ids.discount and whenever an invoice line discount changes it compares it to the global_discount field. If the two values are different then it sets global discount=0.

global_discount_check=fields.Float("discount checker",digits='Discount',store=False,compute="_check_global_discount") 

@api.depends('invoice_line_ids.discount')
def _check_global_discount(self):
    for line in self.invoice_line_ids:
            if (line.discount!=self.global_discount):
                self.global_discount=0.0       
    self.global_discount_check=0.0
Avatar
Discard
Best Answer

Hi 

try to add return 

@api.onchange('quantity', 'discount', 'price_unit', 'tax_ids')
def _onchange_price_subtotal(self):
res=super(SoftOneInvoiceLine,self)._onchange_price_subtotal()
for rec in self:
if (rec.change_from_user):
rec.move_id.global_discount=0;
rec.move_id.update({'global_discount':0})
print (rec.move_id," ",rec.move_id.global_discount)
rec.change_from_user=True

return res

Avatar
Discard
Author

Thanks for your answer!

I've tried what you suggested and unfortunately, the problem persists