Analytic account lines not being created for account moves

Bug #582988 reported by Borja López Soilán (NeoPolus)
34
This bug affects 6 people
Affects Status Importance Assigned to Milestone
Odoo Addons (MOVED TO GITHUB)
Status tracked in Trunk
5.0
Fix Released
High
Vinay Rana (OpenERP)
Trunk
Fix Released
High
Vinay Rana (OpenERP)

Bug Description

The account move validation (validate method of the account.move object) is broken on both 5.0 and 6.0 (worse in the latter one), preventing the creation of analytic account lines (on some cases in 5.0, every case on 6.0).

--- On 5.0 ------------------------------------------------

On 5.0 we have some code like this:

    def validate(self, cr, uid, ids, context={}):
        ...
        ok = True
        for move in self.browse(cr, uid, ids, context):
        ...
        if ok:
            list_ids = []
            for tmp in move.line_id:
                list_ids.append(tmp.id)
            self.pool.get('account.move.line').create_analytic_lines(cr, uid, list_ids, context)

As you can see, that "if ok" conditional branch is invalid:
  1 - Is only executed once! not for each move.
  2 - The "move" variable, may be undefined (it's defined on the loop); but being lucky (suppose that ids is never empty) it will be just the last move of the collection. Only the last move (if "ids" contains more than one move) gets it's analytic lines created.

So, it works just out of luck!

--- On 6.0 ------------------------------------------------

P. Christeas saw this bug when working on the 6.0, and tried to fix it (on 2009-01-22, revno 1628.3.67, revid <email address hidden>: "Fix apparent indentation error until the world collapses." http://bazaar.launchpad.net/~openerp/openobject-addons/trunk/revision/1628.3.67), and the world 'almost' collapsed... ;)

With that fix, the 6.0 will no longer create analytic lines!

Why? Cause it never reaches that code!

If you take a deeper look at the validated method, you can see that each conditional branch skipes the end of the loop (either by doing a 'continue', or by doing a 'ok=False'), so yes, create_analytic_lines is never called!!

    def validate(self, cr, uid, ids, context={}):
        ...
        ok = True
        for move in self.browse(cr, uid, ids, context):
            ...
            for line in move.line_id:
                ...
            if abs(amount) < 0.0001:
                ...
                continue
            if journal.centralisation:
                ...
                continue
            else:
                ...
                ok = False
            if ok:
                list_ids = []
                for tmp in move.line_id:
                    list_ids.append(tmp.id)
                self.pool.get('account.move.line').create_analytic_lines(cr, uid, list_ids, context)
        return ok

Conclusion: the account.move validate method code is severely broken and must be fixed (on 5.0) / refactored (on 6.0) ASAP!

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Added patch for the trunk version, that fixes the validate method and makes the code cleaner (no continues but branches, more comments...)

Revision history for this message
Raphaël Valyi - http://www.akretion.com (rvalyi) wrote :

Hello,

I just tested on trunk revision #3482 and I confirm that no account move line is created no matter the analytic account you set.

Still I had an other bug when trying to fill the analytic account and then creating the invoice with that relevant stack trace:
  File "/home/rvalyi/DEV/openerp/addons/account/account_analytic_line.py", line 45, in <lambda>
    'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', c),
  File "/home/rvalyi/DEV/openerp/server/bin/addons/base/res/res_company.py", line 115, in _company_default_get
    ids = proxy.search(cr, uid, args, context=context)
  File "/home/rvalyi/DEV/openerp/server/bin/osv/orm.py", line 3675, in search
    cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2)
  File "/home/rvalyi/DEV/openerp/server/bin/sql_db.py", line 74, in wrapper

With no time to investigate that new bug, I hardcorded company_id': 1 in account_analytic_line.py and tested further and see no analytic move created at all.

Now, once I applied the patch I had that error preventing me to create any invoice (with or without analytic accounts):
"warning -- Integrity Error !\n\nYou can not validate a non-balanced entry !\nMake sure you have configured Payment Term properly !\nIt should contain atleast one Payment Term Line with type "Balance""

So I have some doubts the proposed patch works.

That being said, I consider this is a very severe regression on the basic scope indeed, the kind of bug that kill you a demo of v6 no matter what...

Changed in openobject-addons:
importance: Undecided → High
status: New → Confirmed
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Hello guys,

I have gone through the issue and looking forward for that.
I have fixed the traceback that Raphael gets.

Thanks.

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Thanks for the review Raphaël & Jay.

Raphaël, does the proposed patch work for you now that Jay has fixed the other traceback?

I'll take a deeper look into it to make sure it is stable.

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Added a new patch based on the lastest revision of the trunk addons.

Raphaël, I tested it on a freshly created demo database over the lastest trunk revisions and it worked for me:
  - Created a new demo database with minimal profile.
  - Created an invoice for camp2camp, using an analytic account for the invoice line.
  - Confirmed the invoice => No analytic lines were created.

  - Applied the patch.
  - Created a new invoice for camp2campt, using the same analytic account for the invoice line.
  - Tried to confirm the invoice, one error was raised: I had to configure the analytic journal for that account journal.
  - Configured the journal.
  - Confirmed the invoice => Analytic lines were created.

I'm currently working on developing a small wizard (for the pxgo_account_admin_tools module) that finds confirmed account moves missing its analytic lines, and fixes them (validates the moves again).

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Ok, we tested it a bit more, and found that for some invoices the analytic lines creation failed because the currency was not correctly set.

I'm adding a new patch that will correct this problem too (res.currency calculate method may receive from_currency_id but not to_currency_id; it should use the same currency for both in that case).

Also, I just pushed a commit into extra-addons (http://bazaar.launchpad.net/~openerp-commiter/openobject-addons/trunk-extra-addons/revision/4552) that adds a wizard to let the user search for account moves with references to analytic accounts but without analytic lines (like the ones corrupted by this bug), and re-validate them (so their analytic lines are recomputed).

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Still no progress on this? We were a bit disappointed when the 5.0.11 version came without solving this (important) bug.

We've using the patches mentioned above on trunk without problems for 3 weeks.

Revision history for this message
Nhomar - Vauxoo (nhomar) wrote :

This Bug is so important is affecting a lot of people that probably is using Analityc functions with this issue.

Changed in openobject-addons:
milestone: none → 5.0.12
Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Yeah Nhomar, the problem is most people may not be noticing the problem (that the analytic lines aren't being created) until is too late* :(
* Well the "revalidate moves wizard" seems to be able to correct the problem afterwards :)

Changed in openobject-addons:
assignee: nobody → vra (openerp) (vra-openerp)
status: Confirmed → In Progress
Changed in openobject-addons:
milestone: 5.0.12 → 6.0
summary: - [6.0/5.0] Analytic account lines not being created for account moves
+ [6.0] Analytic account lines not being created for account moves
Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) wrote : Re: [6.0] Analytic account lines not being created for account moves

Hello Nhomar Hernández,

This bug happens only in trunk code. I have checked this with latest stable version but I am not able to reproduce this.
So, I escalate the milestone to 6.0.
If you face similar problem in current stable (In your end), Can you please provide me more specific steps with example to reproduce this at my end.

Thanks.

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

vra, just call the account.validate() method with a list of ids instead of a single id.
It will validate all the records, but create the analytic move only for the last record.

Thankfully it seems that the average user will not cause the validation of more than one entry at the time (that's why you can't reproduce it), but if any module or script tries to do a batch validation of several entries... :(

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

I've seen that just Mustufa Rangwala (mra) just reverted P. Christeas changes from revno 1628.3.67: http://bazaar.launchpad.net/~openerp/openobject-addons/trunk/revision/3164.77.24

So, now the situation on the 6.0 is the same as on the 5.0: The code 'seems' to work, but... the "if ok" conditional branch is invalid:
  1 - Is only executed once! not for each move.
  2 - The "move" variable, may be undefined (it's defined on the loop, the if branch is outside of the loop):
                 Only the last move (if "ids" contains more than one move) gets it's analytic lines created.
                 If ids is empty, it fails (undefined variable).

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Anyway, I would propose mra to take a look to the proposed patch: It solves the remaining problems, and the code is easier to read.

Revision history for this message
Mustufa Rangwala (Open ERP) (mra-tinyerp) wrote :

Hello Borja,

Yes you are correct If there is an multiple ids of account.move to be validated it will just create analytic lines for only last move ! Because of "move" variable in loop..

And debit, credit, balance fields on account.analytic.account are function field with store=True but it does not work properly. For e.g if you create invoice with analytic account X, Its debit,credit,balance entry does not updated (I mean its functions _balance_calc, _debit_calc, ... not called) ....So we can use store={} feature in analytic account fields...

vra can you check it ?

thanks,
mra

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

mra:
"And debit, credit, balance fields on account.analytic.account are function field with store=True but it does not work properly. For e.g if you create invoice with analytic account X, Its debit,credit,balance entry does not updated (I mean its functions _balance_calc, _debit_calc, ... not called) ....So we can use store={} feature in analytic account fields..."

That looks similar to bug 600547 ! (Invoice totals [store={}] not getting updated when the invoice lines are created/moved directly). I'm starting to think that we have a problem with stores and one2many/many2one relations: changes in the many2one side (analytic line / invoice line) might not trigger the 'store' on the one2many side (analytic account / invoice) :(

Somebody should review this behavior as it may cause big headaches.

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

It has been fixed by revision 2773 <email address hidden>.
Thank you for your work Borja,it has been committed authoured to you.

summary: - [6.0] Analytic account lines not being created for account moves
+ Analytic account lines not being created for account moves
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Fixed by revision 3923 <email address hidden>.
Thanks.

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) wrote :

Thanks Jay :D

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.