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

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

Avatar
Discard
Author Best Answer

I have noticed that when data is entered into invoice line items, the tax and ar/ap account data is being fed and stored in the account_move_line table and since tax is being  aggregated and stored as a single entry for all the invoice line items of a particular invoice, the analytic account wasn't being passed and stored into the table and since we made analytic_account_id as a required field, it was throwing an error. So as a temporary workaround i have hardcoded to add the analytic account of id 1 to all the items that are getting stored in account_move_line when an invoice is validated.

Avatar
Discard
Best Answer

Hello,

you have to decide where exactly you need to check for analytic account. (create, edit, validate)

check before applying the validate

--> if in validate then you can just inherit the method which create the entry data.

I hope this is helpful for you


Avatar
Discard
Author

I'm sorry but i'm afraid you didn't get my question correctly

Related Posts Replies Views Activity
1
Mar 19
6202
0
Dec 24
9
0
Dec 24
37
1
Dec 24
66
1
Dec 24
83