bug-fix for the account voucher
[odoo/odoo.git] / addons / account_voucher / voucher_account.py
1 import time
2 import netsvc
3 from osv import fields, osv
4 import ir
5 import pooler
6 import mx.DateTime
7 from mx.DateTime import RelativeDateTime
8
9 from tools import config
10
11 class Account(osv.osv):
12         _inherit = "account.account"
13         
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
16             mapping = {
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 "
20             }
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
25             accounts = {}
26             if ids2:
27                 query = self.pool.get('account.move.line')._query_get(cr, uid,
28                         context=context)
29                 cr.execute(("SELECT l.account_id as id, " +\
30                         ' , '.join(map(lambda x: mapping[x], field_names)) +
31                         "FROM " \
32                             "account_move_line l " \
33                         "WHERE " \
34                             "l.account_id IN (%s) " \
35                             "AND " + query + " " \
36                         "GROUP BY l.account_id") % (acc_set, ))
37     
38                 for res in cr.dictfetchall():
39                     accounts[res['id']] = res
40     
41             #for the asked accounts, get from the dictionnary 'accounts' the value of it
42             res = {}
43             for id in ids:
44                 res[id] = self._get_account_values(cr, uid, id, accounts, field_names, context)
45             for id in ids:    
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
55                 else:
56                     res[id]=res[id]
57             return res
58         
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:
66                         ids2.append(t)
67                 for i in ids2:
68                     tmp = self._get_account_values(cr, uid, i, accounts, field_names, context)
69                     for a in field_names:
70                         res[a] += tmp[a]
71             else:
72                 ids2 = self.search(cr, uid, [('parent_id', 'child_of', [browse_rec.id])])
73                 for i in ids2:
74                     for a in field_names:
75                         res[a] += accounts.get(i, {}).get(a, 0.0)
76             return res
77            
78         def _diff(self, cr, uid, ids, field_name, arg, context={}):
79
80             res={}
81             dr_total=0.0
82             cr_total=0.0
83             difference=0.0
84             for id in ids:
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
90                 else:
91                     difference=0.0
92             difference=dr_total-cr_total
93             for id in ids:
94                 res[id]=difference
95             return res
96
97         _columns = {
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'),
104
105
106     }
107         
108     
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
112             if not account_type:
113                 return {'value' : {}}
114             if account_type in ('cash','asset','expense'):
115                 type1 = 'dr'
116             elif account_type in ('equity','income','liability') : 
117                 type1 = 'cr'
118             else:
119                 type1 = 'none'
120           
121             return {
122                 'value' : {'type1' : type1}
123         }
124
125 Account()