From 491ec719d7801d3103e57105dc6c7135d9f7d23f Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Wed, 20 Jan 2010 18:19:31 +0100 Subject: [PATCH] [FIX] account: credit/debit/balance of consolidated accounts lp bug: https://launchpad.net/bugs/504824 fixed bzr revid: chs@tinyerp.com-20100120171931-8nta52yk7j7mlu3p --- addons/account/account.py | 65 ++++++++++++++++++------------- addons/account_voucher/account.py | 34 ++-------------- 2 files changed, 42 insertions(+), 57 deletions(-) diff --git a/addons/account/account.py b/addons/account/account.py index b049ab5d087..4e29bb1901f 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -77,7 +77,10 @@ class account_payment_term_line(osv.osv): _columns = { 'name': fields.char('Line Name', size=32, required=True), 'sequence': fields.integer('Sequence', required=True, help="The sequence field is used to order the payment term lines from the lowest sequences to the higher ones"), - 'value': fields.selection([('procent', 'Percent'), ('balance', 'Balance'), ('fixed', 'Fixed Amount')], 'Value', required=True, help="""Example: 14 days 2%, 30 days net + 'value': fields.selection([('procent', 'Percent'), + ('balance', 'Balance'), + ('fixed', 'Fixed Amount')], 'Value', + required=True, help="""Example: 14 days 2%, 30 days net 1. Line 1: percent 0.02 14 days 2. Line 2: balance 30 days"""), @@ -205,43 +208,50 @@ class account_account(osv.osv): #compute for each account the balance/debit/credit from the move lines accounts = {} if ids2: - query = self.pool.get('account.move.line')._query_get(cr, uid, - context=context) + aml_query = self.pool.get('account.move.line')._query_get(cr, uid, context=context) + + wheres = [""] + if query: + wheres.append(query.strip()) + if aml_query: + wheres.append(aml_query.strip()) + query = " AND ".join(wheres) + cr.execute(("SELECT l.account_id as id, " +\ ' , '.join(map(lambda x: mapping[x], field_names)) + "FROM " \ "account_move_line l " \ "WHERE " \ "l.account_id IN (%s) " \ - "AND " + query + " " \ - "GROUP BY l.account_id") % (acc_set, )) + + query + + " GROUP BY l.account_id") % (acc_set, )) for res in cr.dictfetchall(): accounts[res['id']] = res - #for the asked accounts, get from the dictionnary 'accounts' the value of it + + # consolidate accounts with direct children + brs = list(self.browse(cr, uid, ids2, context=context)) + sums = {} + while brs: + current = brs[0] + can_compute = True + for child in current.child_id: + if child.id not in sums: + can_compute = False + try: + brs.insert(0, brs.pop(brs.index(child))) + except ValueError: + brs.insert(0, child) + if can_compute: + brs.pop(0) + for fn in field_names: + sums.setdefault(current.id, {})[fn] = accounts.get(current.id, {}).get(fn, 0.0) + if current.child_id: + sums[current.id][fn] += sum(sums[child.id][fn] for child in current.child_id) res = {} for id in ids: - res[id] = self._get_account_values(cr, uid, id, accounts, field_names, context) - return res - - def _get_account_values(self, cr, uid, id, accounts, field_names, context={}): - res = {}.fromkeys(field_names, 0.0) - browse_rec = self.browse(cr, uid, id) - if browse_rec.type == 'consolidation': - ids2 = self.read(cr, uid, [browse_rec.id], ['child_consol_ids'], context)[0]['child_consol_ids'] - for t in self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])]): - if t not in ids2 and t != browse_rec.id: - ids2.append(t) - for i in ids2: - tmp = self._get_account_values(cr, uid, i, accounts, field_names, context) - for a in field_names: - res[a] += tmp[a] - else: - ids2 = self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])]) - for i in ids2: - for a in field_names: - res[a] += accounts.get(i, {}).get(a, 0.0) + res[id] = sums[id] return res def _get_company_currency(self, cr, uid, ids, field_name, arg, context={}): @@ -260,7 +270,8 @@ class account_account(osv.osv): if record.child_consol_ids: for acc in record.child_consol_ids: - result[record.id].append(acc.id) + if acc.id not in result[record.id]: + result[record.id].append(acc.id) return result diff --git a/addons/account_voucher/account.py b/addons/account_voucher/account.py index e0f0b5de62b..9c153a185b2 100644 --- a/addons/account_voucher/account.py +++ b/addons/account_voucher/account.py @@ -64,38 +64,12 @@ class account_account(osv.osv): 'type1':fields.selection([('dr','Debit'),('cr','Credit'),('none','None')], 'Dr/Cr',store=True), } - def compute_total(self, cr, uid, ids, yr_st_date, yr_end_date, st_date, end_date, field_names, context={}, query=''): - #compute the balance/debit/credit accordingly to the value of field_name for the given account ids - mapping = { - 'credit': "COALESCE(SUM(l.credit), 0) as credit ", - 'balance': "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as balance ", - 'debit': "COALESCE(SUM(l.debit), 0) as debit ", - } - #get all the necessary accounts - ids2 = self._get_children_and_consol(cr, uid, ids, context) - acc_set = ",".join(map(str, ids2)) - #compute for each account the balance/debit/credit from the move lines + def compute_total(self, cr, uid, ids, yr_st_date, yr_end_date, st_date, end_date, field_names, context={}): if not (st_date >= yr_st_date and end_date <= yr_end_date): return {} - accounts = {} - if ids2: - query = self.pool.get('account.move.line')._query_get(cr, uid, - context=context) - cr.execute("SELECT l.account_id as id, " \ - + ' , '.join(map(lambda x: mapping[x], field_names.keys() )) + \ - "FROM account_move_line l " \ - "WHERE l.account_id IN ("+ acc_set +") " \ - "AND " + query + " " \ - " AND l.date >= "+"'"+ st_date +"'"+" AND l.date <= "+"'"+ end_date +""+"'"" " \ - "GROUP BY l.account_id ") - for res in cr.dictfetchall(): - accounts[res['id']] = res - #for the asked accounts, get from the dictionnary 'accounts' the value of it - res = {} - for id in ids: - res[id] = self._get_account_values(cr, uid, id, accounts, field_names, context) - return res - + query = "l.date >= '%s' AND l.date <= '%s'" (st_date, end_date) + return self.__compute(cr, uid, ids, field_names, context=context, query=query) + def create(self, cr, uid, vals, context={}): name=self.search(cr,uid,[('name','ilike',vals['name']),('company_id','=',vals['name'])]) if name: