From 703a2410d9398fb5e0c471e2d058dd45664fc1cb Mon Sep 17 00:00:00 2001 From: qdp Date: Thu, 25 Sep 2008 16:17:35 +0200 Subject: [PATCH] *added computation of debit/credit/balance for consolidation accounts *small improvements bzr revid: qdp@tinyerp.com-20080925141735-7loyyuoe6cq9pfgr --- addons/account/account.py | 45 +++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/addons/account/account.py b/addons/account/account.py index 61c0a77be3b..3dbb1877679 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -160,14 +160,29 @@ class account_account(osv.osv): return super(account_account,self).search(cr, uid, args, offset, limit, order, context=context, count=count) + def _get_children_and_consol(self, cr, uid, ids, context={}): + #this function search for all the children and all consolidated children (recursively) of the given account ids + res = self.search(cr, uid, [('parent_id', 'child_of', ids)]) + for id in res: + this = self.browse(cr, uid, id, context) + for child in this.child_consol_ids: + if child.id not in res: + res.append(child.id) + if len(res) != len(ids): + return self._get_children_and_consol(cr, uid, res, context) + return res + def __compute(self, cr, uid, ids, field_names, arg, context={}, query=''): + #compute the balance/debit/credit accordingly to the value of field_name for the given account ids mapping = { 'balance': "COALESCE(SUM(l.debit) - SUM(l.credit), 0) as balance ", 'debit': "COALESCE(SUM(l.debit), 0) as debit ", 'credit': "COALESCE(SUM(l.credit), 0) as credit " } - ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)]) + #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 accounts = {} if ids2: query = self.pool.get('account.move.line')._query_get(cr, uid, @@ -184,13 +199,29 @@ class account_account(osv.osv): 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] = {}.fromkeys(field_names, 0.0) - ids2 = self.search(cr, uid, [('parent_id', 'child_of', [id])]) + 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[id][a] += accounts.get(i, {}).get(a, 0.0) + res[a] += accounts.get(i, {}).get(a, 0.0) return res def _get_company_currency(self, cr, uid, ids, field_name, arg, context={}): @@ -216,7 +247,7 @@ class account_account(osv.osv): _columns = { 'name': fields.char('Name', size=128, required=True, select=True), 'currency_id': fields.many2one('res.currency', 'Secondary Currency', help="Force all moves for this account to have this secondary currency."), - 'code': fields.char('Code', size=64), + 'code': fields.char('Code', size=64, required=True), 'type': fields.selection([ ('receivable','Receivable'), ('payable','Payable'), @@ -229,7 +260,7 @@ class account_account(osv.osv): 'user_type': fields.many2one('account.account.type', 'Account Type', required=True), 'parent_id': fields.many2one('account.account','Parent', ondelete='cascade'), 'child_parent_ids':fields.one2many('account.account','parent_id','Children'), - 'child_consol_ids':fields.many2many('account.account', 'account_account_consol_rel', 'child_id', 'parent_id', 'Consolidated Children',domain=[('type', '=', 'consolidation')]), + 'child_consol_ids':fields.many2many('account.account', 'account_account_consol_rel', 'child_id', 'parent_id', 'Consolidated Children'), 'child_id': fields.function(_get_child_ids, method=True, type='many2many',relation="account.account",string="Children Accounts"), 'balance': fields.function(__compute, digits=(16,2), method=True, string='Balance', multi='balance'), 'credit': fields.function(__compute, digits=(16,2), method=True, string='Credit', multi='balance'), @@ -245,7 +276,7 @@ class account_account(osv.osv): 'parent_left': fields.integer('Parent Left', select=1), 'parent_right': fields.integer('Parent Right', select=1), - 'check_history': fields.boolean('Display History', help="Check this box if you want to print all entries otherwise it will print balance of this account") + 'check_history': fields.boolean('Display History', help="Check this box if you want to print all entries when printing the General Ledger, otherwise it will only print its balance.") } def _default_company(self, cr, uid, context={}):