1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 from osv import fields, osv
28 from mx.DateTime import RelativeDateTime
30 from tools import config
32 class Account(osv.osv):
33 _inherit = "account.account"
35 def __compute(self, cr, uid, ids, field_names, arg, context={}, query=''):
36 #compute the balance/debit/credit accordingly to the value of field_name for the given account ids
38 'balance': "COALESCE(SUM(l.debit) - SUM(l.credit) , 0) as balance ",
39 'debit': "COALESCE(SUM(l.debit), 0) as debit ",
40 'credit': "COALESCE(SUM(l.credit), 0) as credit "
42 #get all the necessary accounts
43 ids2 = self._get_children_and_consol(cr, uid, ids, context)
44 acc_set = ",".join(map(str, ids2))
45 #compute for each account the balance/debit/credit from the move lines
48 query = self.pool.get('account.move.line')._query_get(cr, uid,
50 cr.execute(("SELECT l.account_id as id, " +\
51 ' , '.join(map(lambda x: mapping[x], field_names)) +
53 "account_move_line l " \
55 "l.account_id IN (%s) " \
56 "AND " + query + " " \
57 "GROUP BY l.account_id") % (acc_set, ))
59 for res in cr.dictfetchall():
60 accounts[res['id']] = res
62 #for the asked accounts, get from the dictionnary 'accounts' the value of it
65 res[id] = self._get_account_values(cr, uid, id, accounts, field_names, context)
67 open=self.browse(cr, uid, id, context)
68 type_id=open.user_type
69 obj=self.pool.get('account.account.type').browse(cr,uid,type_id.id)
70 open_balance=open.open_bal
71 if obj.code in ('cash','asset','expense'):
72 res[id]['balance']+=open_balance
73 elif obj.code in ('equity','income','liability'):
74 total=open_balance*(-1)
75 res[id]['balance']+=total
80 def _get_account_values(self, cr, uid, id, accounts, field_names, context={}):
81 res = {}.fromkeys(field_names, 0.0)
82 browse_rec = self.browse(cr, uid, id)
83 if browse_rec.type == 'consolidation':
84 ids2 = self.read(cr, uid, [browse_rec.id], ['child_consol_ids'], context)[0]['child_consol_ids']
85 for t in self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])]):
86 if t not in ids2 and t != browse_rec.id:
89 tmp = self._get_account_values(cr, uid, i, accounts, field_names, context)
93 ids2 = self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])])
96 res[a] += accounts.get(i, {}).get(a, 0.0)
99 def _diff(self, cr, uid, ids, field_name, arg, context={}):
106 open=self.browse(cr, uid, id, context)
107 if open.type1 == 'dr':
108 dr_total+=open.open_bal
109 elif open.type1 == 'cr':
110 cr_total+=open.open_bal
113 difference=dr_total-cr_total
119 'open_bal' : fields.float('Opening Balance',digits=(16,2)),
120 'diff' : fields.function(_diff, digits=(16,2),method=True,string='Difference of Opening Bal.'),
121 'type1':fields.selection([('dr','Debit'),('cr','Credit'),('none','None')], 'Dr/Cr',store=True),
122 'balance': fields.function(__compute, digits=(16,2), method=True, string='Closing Balance', multi='balance'),
123 'credit': fields.function(__compute, digits=(16,2), method=True, string='Credit', multi='balance'),
124 'debit': fields.function(__compute, digits=(16,2), method=True, string='Debit', multi='balance'),
130 def onchange_type(self, cr, uid, ids,user_type,type1):
131 obj=self.pool.get('account.account.type').browse(cr,uid,user_type)
132 account_type=obj.code
134 return {'value' : {}}
135 if account_type in ('cash','asset','expense'):
137 elif account_type in ('equity','income','liability') :
143 'value' : {'type1' : type1}