From 9ab39c25da2dff031aae8a8505be9ce3774c5932 Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Mon, 18 Aug 2008 23:08:08 +0200 Subject: [PATCH] Speed Improvement: accounting bzr revid: fp@tinyerp.com-20080818210808-01v66l4mnwef6umw --- addons/account/account.py | 1 - addons/account/partner.py | 43 +++++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/addons/account/account.py b/addons/account/account.py index d9fba92ef14..9da50f7564a 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -195,7 +195,6 @@ class account_account(osv.osv): for i in ids2: for a in range(len(field_names)): res[id][a] += accounts.get(i, (0.0,0.0,0.0))[a] - print res return res def _get_company_currency(self, cr, uid, ids, field_name, arg, context={}): diff --git a/addons/account/partner.py b/addons/account/partner.py index 06df0ed0c57..b6bcb2324b2 100644 --- a/addons/account/partner.py +++ b/addons/account/partner.py @@ -35,20 +35,33 @@ class res_partner(osv.osv): _name = 'res.partner' _inherit = 'res.partner' _description = 'Partner' - def _credit_get(self, cr, uid, ids, name, arg, context): - res={} + def _credit_debit_get(self, cr, uid, ids, field_names, arg, context): query = self.pool.get('account.move.line')._query_get(cr, uid, context=context) - for id in ids: - cr.execute("select sum(debit-credit) from account_move_line as l where account_id in (select id from account_account where type = %s and active) and partner_id=%d and reconcile_id is null and "+query, ('receivable', id)) - res[id]=cr.fetchone()[0] or 0.0 - return res - - def _debit_get(self, cr, uid, ids, name, arg, context): - res={} - query = self.pool.get('account.move.line')._query_get(cr, uid, context=context) - for id in ids: - cr.execute("select sum(debit-credit) from account_move_line as l where account_id in (select id from account_account where type = %s and active) and partner_id=%d and reconcile_id is null and "+query, ('payable', id)) - res[id]=cr.fetchone()[0] or 0.0 + cr.execute(("""select + l.partner_id, a.type, sum(l.debit-l.credit) + from + account_move_line l + left join + account_account a on (l.account_id=a.id) + where + a.type in ('receivable','payable') and + l.partner_id in (%s) and + l.reconcile_id is null and + """ % (','.join(map(str, ids)),))+query+""" + group by + l.partner_id, a.type + """) + tinvert = { + 'credit': 'receivable', + 'debit': 'payable' + } + maps = {} + for i in range(len(field_names)): + maps[{'credit': 'receivable', 'debit': 'payable' }[field_names[i]]] = i + res = {}.fromkeys(ids, map(lambda x: 0.0, field_names)) + for pid,type,val in cr.fetchall(): + if type in maps: + res[pid][maps[type]] = val return res def _credit_search(self, cr, uid, obj, name, args): @@ -74,8 +87,8 @@ class res_partner(osv.osv): return [('id','in',map(lambda x:x[0], res))] _columns = { - 'credit': fields.function(_credit_get, fnct_search=_credit_search, method=True, string='Total Receivable'), - 'debit': fields.function(_debit_get, fnct_search=_debit_search, method=True, string='Total Payable'), + 'credit': fields.function(_credit_debit_get, fnct_search=_credit_search, method=True, string='Total Receivable', multi='dc'), + 'debit': fields.function(_credit_debit_get, fnct_search=_debit_search, method=True, string='Total Payable', multi='dc'), 'debit_limit': fields.float('Payable Limit'), 'property_account_payable': fields.property( 'account.account',