According to my requirement i need the Analytic Account field as mandatory in invoice line items and also journal entry line items. I have inherited and defined both of them as required=True.
Now when i validate a invoice the default odoo behavior saves some items in account_move_line table and as i defined the field analytic_account_id as required, when it tries to save or insert the data, it is throwing me a not null constraint error.
As a workaround i have removed the required=True in journal entry screen and i override the create and write methods to check for the same, below is my code
@api.model
def create(self, vals):
res = super(AccountMoveInherited, self).create(vals=vals)
lines = vals.get('line_ids')
if lines:
for line in lines:
values = line[2]
if values.get('analytic_account_id'):
continue
if not values.get('analytic_account_id'):
raise UserError(_("Some Journal Entry items are missing "
"Analytic Account field, Please add them and try again."))
return res
@api.multi
def write(self, vals):
res = super(AccountMoveInherited, self).write(vals=vals)
lines = vals.get('line_ids')
if lines:
for line in lines:
values = line[2]
if values.get('analytic_account_id'):
continue
if not values.get('analytic_account_id'):
raise UserError(_("Journal Entry item(s) are missing "
"Analytic Account field, Please add them and try again."))
return res
It works just fine but now when i try to validate an invoice, it is throwing me this error. I have wasted my 4 hours of time figuring this and implementing it and now i'm right at where i started.
Can anyone tell me a good approach to achieve my required functionality as i'm out of ideas and don't know how can i achieve the functionality anymore