[IMP] Upgrade the translation files
[odoo/odoo.git] / addons / account_voucher / voucher_account.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22 import time
23 import netsvc
24 from osv import fields, osv
25 import ir
26 import pooler
27 import mx.DateTime
28 from mx.DateTime import RelativeDateTime
29
30 from tools import config
31
32 class Account(osv.osv):
33         _inherit = "account.account"
34         
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
37             mapping = {
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 "
41             }
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
46             accounts = {}
47             if ids2:
48                 query = self.pool.get('account.move.line')._query_get(cr, uid,
49                         context=context)
50                 cr.execute(("SELECT l.account_id as id, " +\
51                         ' , '.join(map(lambda x: mapping[x], field_names)) +
52                         "FROM " \
53                             "account_move_line l " \
54                         "WHERE " \
55                             "l.account_id IN (%s) " \
56                             "AND " + query + " " \
57                         "GROUP BY l.account_id") % (acc_set, ))
58     
59                 for res in cr.dictfetchall():
60                     accounts[res['id']] = res
61     
62             #for the asked accounts, get from the dictionnary 'accounts' the value of it
63             res = {}
64             for id in ids:
65                 res[id] = self._get_account_values(cr, uid, id, accounts, field_names, context)
66             for id in ids:    
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
76                 else:
77                     res[id]=res[id]
78             return res
79         
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:
87                         ids2.append(t)
88                 for i in ids2:
89                     tmp = self._get_account_values(cr, uid, i, accounts, field_names, context)
90                     for a in field_names:
91                         res[a] += tmp[a]
92             else:
93                 ids2 = self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])])
94                 for i in ids2:
95                     for a in field_names:
96                         res[a] += accounts.get(i, {}).get(a, 0.0)
97             return res
98            
99         def _diff(self, cr, uid, ids, field_name, arg, context={}):
100
101             res={}
102             dr_total=0.0
103             cr_total=0.0
104             difference=0.0
105             for id in ids:
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
111                 else:
112                     difference=0.0
113             difference=dr_total-cr_total
114             for id in ids:
115                 res[id]=difference
116             return res
117
118         _columns = {
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'),
125
126
127     }
128         
129     
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
133             if not account_type:
134                 return {'value' : {}}
135             if account_type in ('cash','asset','expense'):
136                 type1 = 'dr'
137             elif account_type in ('equity','income','liability') : 
138                 type1 = 'cr'
139             else:
140                 type1 = 'none'
141           
142             return {
143                 'value' : {'type1' : type1}
144         }
145
146 Account()