Hello everyone, I have the following problem: I want to have analytic account defined in an invoice, and this analytic account is to be set in all new invoice lines bellow as I'm adding it.
I used decorator @api.onchange for the lines so that the last added line will be set. However i can not distinguish between adding and removing a line(since it is also a change), and this creates a bug.
I need the account to be set only on the newly added lines, so that if previous were set manually to something else or even unset in some cases, should not be affected.
The code bellow makes a bug, where it checks for all lines and sets the analytic account to the last line if it is empty. So if I add two lines and the first one I decide to set the account empty, then remove the second, the first will be set
class AccountInvoice(models.Model):
_inherit = "account.move"
account_analytic_id = fields.Many2one('account.analytic.account')@api.onchange('invoice_line_ids')
def line_adjust_line_account(self):
print(self._origin)
if self.account_analytic_id and len(self.invoice_line_ids):
if not self.invoice_line_ids[-1].analytic_account_id:
self.invoice_line_ids[-1].analytic_account_id = self.account_analytic_id
If I can catch only the insertion of the line that will be great.