From da504f014e87bc4195700d9c1e638f3f88e7eb00 Mon Sep 17 00:00:00 2001 From: "olt@tinyerp.com" <> Date: Fri, 1 Oct 2010 14:50:26 +0200 Subject: [PATCH] [FIX] account: 'balance' field now uses '_query_get' and uses initial balance bzr revid: olt@tinyerp.com-20101001125026-m6yabgobzcslok80 --- addons/account/account_move_line.py | 32 +++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/addons/account/account_move_line.py b/addons/account/account_move_line.py index 42efd14aa97..1fda3fa8a6c 100644 --- a/addons/account/account_move_line.py +++ b/addons/account/account_move_line.py @@ -96,8 +96,6 @@ class account_move_line(osv.osv): else: fiscalperiod_ids = self.pool.get('account.period').search(cr, uid, [('fiscalyear_id','in',fiscalyear_ids)]) - - #remove from the old query the clause related to the period selection res = '' count = 1 @@ -328,15 +326,27 @@ class account_move_line(osv.osv): ml = self.browse(cr, uid, id, context) return map(lambda x: x.id, ml.move_id.line_id) - # TODO: this is false, it does not uses draft and closed periods - def _balance(self, cr, uid, ids, prop, unknow_none, unknow_dict): - res={} - # TODO group the foreach in sql - for id in ids: - cr.execute('SELECT date,account_id FROM account_move_line WHERE id=%s', (id,)) - dt, acc = cr.fetchone() - cr.execute('SELECT SUM(debit-credit) FROM account_move_line WHERE account_id=%s AND (date<%s OR (date=%s AND id<=%s))', (acc,dt,dt,id)) - res[id] = cr.fetchone()[0] + def _balance(self, cr, uid, ids, name, arg, context=None): + if context is None: + context = {} + + c = context.copy() + c['initital_bal'] = True + + sql = [ + """select l2.id, sum(l1.debit-l1.credit) from account_move_line l1, account_move_line l2""", + """where l2.account_id=l1.account_id""", + """and""", + """l1.id<=l2.id""", + """and""", + """l2.id in %s""", + """and""", + self._query_get(cr, uid, obj='l1', context=c), + """ group by l2.id""", + ] + + cr.execute('\n'.join(sql), [tuple(ids)]) + res = dict(cr.fetchall()) return res def _invoice(self, cursor, user, ids, name, arg, context=None):