3 from osv import fields, osv
7 from mx.DateTime import RelativeDateTime
9 from tools import config
11 class Account(osv.osv):
12 _inherit = "account.account"
14 def __compute(self, cr, uid, ids, field_names, arg, context={}, query=''):
15 #compute the balance/debit/credit accordingly to the value of field_name for the given account ids
17 'balance': "COALESCE(SUM(l.debit) - SUM(l.credit) , 0) as balance ",
18 'debit': "COALESCE(SUM(l.debit), 0) as debit ",
19 'credit': "COALESCE(SUM(l.credit), 0) as credit "
21 #get all the necessary accounts
22 ids2 = self._get_children_and_consol(cr, uid, ids, context)
23 acc_set = ",".join(map(str, ids2))
24 #compute for each account the balance/debit/credit from the move lines
27 query = self.pool.get('account.move.line')._query_get(cr, uid,
29 cr.execute(("SELECT l.account_id as id, " +\
30 ' , '.join(map(lambda x: mapping[x], field_names)) +
32 "account_move_line l " \
34 "l.account_id IN (%s) " \
35 "AND " + query + " " \
36 "GROUP BY l.account_id") % (acc_set, ))
38 for res in cr.dictfetchall():
39 accounts[res['id']] = res
41 #for the asked accounts, get from the dictionnary 'accounts' the value of it
44 res[id] = self._get_account_values(cr, uid, id, accounts, field_names, context)
46 open=self.browse(cr, uid, id, context)
47 type_id=open.user_type
48 obj=self.pool.get('account.account.type').browse(cr,uid,type_id.id)
49 open_balance=open.open_bal
50 if obj.code in ('cash','asset','expense'):
51 res[id]['balance']+=open_balance
52 elif obj.code in ('equity','income','liability'):
53 total=open_balance*(-1)
54 res[id]['balance']+=total
59 def _get_account_values(self, cr, uid, id, accounts, field_names, context={}):
60 res = {}.fromkeys(field_names, 0.0)
61 browse_rec = self.browse(cr, uid, id)
62 if browse_rec.type == 'consolidation':
63 ids2 = self.read(cr, uid, [browse_rec.id], ['child_consol_ids'], context)[0]['child_consol_ids']
64 for t in self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])]):
65 if t not in ids2 and t != browse_rec.id:
68 tmp = self._get_account_values(cr, uid, i, accounts, field_names, context)
72 ids2 = self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])])
75 res[a] += accounts.get(i, {}).get(a, 0.0)
78 def _diff(self, cr, uid, ids, field_name, arg, context={}):
85 open=self.browse(cr, uid, id, context)
86 if open.type1 == 'dr':
87 dr_total+=open.open_bal
88 elif open.type1 == 'cr':
89 cr_total+=open.open_bal
92 difference=dr_total-cr_total
98 'open_bal' : fields.float('Opening Balance',digits=(16,2)),
99 'diff' : fields.function(_diff, digits=(16,2),method=True,string='Difference of Opening Bal.'),
100 'type1':fields.selection([('dr','Debit'),('cr','Credit'),('none','None')], 'Dr/Cr',store=True),
101 'balance': fields.function(__compute, digits=(16,2), method=True, string='Closing Balance', multi='balance'),
102 'credit': fields.function(__compute, digits=(16,2), method=True, string='Credit', multi='balance'),
103 'debit': fields.function(__compute, digits=(16,2), method=True, string='Debit', multi='balance'),
109 def onchange_type(self, cr, uid, ids,user_type,type1):
110 obj=self.pool.get('account.account.type').browse(cr,uid,user_type)
111 account_type=obj.code
113 return {'value' : {}}
114 if account_type in ('cash','asset','expense'):
116 elif account_type in ('equity','income','liability') :
122 'value' : {'type1' : type1}