33
33
class account_analytic_account(osv.osv):
34
34
_name = 'account.analytic.account'
35
35
_description = 'Analytic Accounts'
36
logger = netsvc.Logger()
37
38
def _credit_calc(self, cr, uid, ids, name, arg, context={}):
38
acc_set = ",".join(map(str, ids))
39
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
40
'Entering _credit_calc; ids:%s'%ids)
41
if context.get('from_date',False):
42
where_date += " AND l.date >= '" + context['from_date'] + "'"
43
if context.get('to_date',False):
44
where_date += " AND l.date <= '" + context['to_date'] + "'"
46
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE l.amount<0 and a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
44
if context.get('from_date'):
45
where_date += " AND l.date >= %(from_date)s"
46
if context.get('to_date'):
47
where_date += " AND l.date <= %(to_date)s"
49
cr.execute("SELECT a.id, COALESCE(SUM(l.amount), 0) "
50
"FROM account_analytic_account a "
51
"LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s)"
52
" WHERE l.amount < 0 AND a.id IN %%(ids)s "
53
"GROUP BY a.id" % (where_date),
54
dict(context, ids=tuple(ids)))
47
55
r = dict(cr.fetchall())
56
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
57
'_credit_calc results: %s'%r)
49
59
r.setdefault(i,0.0)
52
62
def _debit_calc(self, cr, uid, ids, name, arg, context={}):
63
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
64
'Entering _debit_calc; ids:%s'%ids)
54
acc_set = ",".join(map(str, ids))
57
if context.get('from_date',False):
58
where_date += " AND l.date >= '" + context['from_date'] + "'"
59
if context.get('to_date',False):
60
where_date += " AND l.date <= '" + context['to_date'] + "'"
62
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE l.amount>0 and a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
63
r= dict(cr.fetchall())
68
if context.get('from_date'):
69
where_date += " AND l.date >= %(from_date)s"
70
if context.get('to_date'):
71
where_date += " AND l.date <= %(to_date)s"
73
cr.execute("SELECT a.id, COALESCE(SUM(l.amount), 0) "
74
"FROM account_analytic_account a "
75
"LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s)"
76
" WHERE l.amount > 0 AND a.id IN %%(ids)s "
77
"GROUP BY a.id" % (where_date),
78
dict(context, ids=tuple(ids)))
79
r = dict(cr.fetchall())
80
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
81
'_debut_calc results: %s'%r)
65
83
r.setdefault(i,0.0)
68
86
def _balance_calc(self, cr, uid, ids, name, arg, context={}):
69
88
ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
70
acc_set = ",".join(map(str, ids2))
89
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
90
'Entering _balance_calc; ids:%s; ids2:%s'%(
73
if context.get('from_date',False):
74
where_date += " AND l.date >= '" + context['from_date'] + "'"
75
if context.get('to_date',False):
76
where_date += " AND l.date <= '" + context['to_date'] + "'"
78
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) FROM account_analytic_account a LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s) WHERE a.id IN (%s) GROUP BY a.id" % (where_date,acc_set))
100
if context.get('from_date'):
101
where_date += " AND l.date >= %(from_date)s"
102
if context.get('to_date'):
103
where_date += " AND l.date <= %(to_date)s"
105
cr.execute("SELECT a.id, COALESCE(SUM(l.amount),0) "
106
"FROM account_analytic_account a "
107
"LEFT JOIN account_analytic_line l ON (a.id=l.account_id %s)"
108
" WHERE a.id IN %%(ids)s "
109
"GROUP BY a.id" % (where_date),
110
dict(context, ids=tuple(ids2)))
80
112
for account_id, sum in cr.fetchall():
81
113
res[account_id] = sum
83
cr.execute("SELECT a.id, r.currency_id FROM account_analytic_account a INNER JOIN res_company r ON (a.company_id = r.id) where a.id in (%s)" % acc_set)
85
currency= dict(cr.fetchall())
114
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
115
'_balance_calc, (id, sum): %s'%res)
117
cr.execute("SELECT a.id, r.currency_id "
118
"FROM account_analytic_account a "
119
"INNER JOIN res_company r ON (a.company_id = r.id) "
120
"WHERE a.id in %s", (tuple(ids2),))
122
currency = dict(cr.fetchall())
123
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
124
'_balance_calc currency: %s'%currency)
87
126
res_currency= self.pool.get('res.currency')
89
130
for child in self.search(cr, uid, [('parent_id', 'child_of', [id])]):
91
132
res.setdefault(id, 0.0)
92
if currency[child]<>currency[id] :
133
if currency[child]<>currency[id]:
93
134
res[id] += res_currency.compute(cr, uid, currency[child], currency[id], res.get(child, 0.0), context=context)
95
136
res[id] += res.get(child, 0.0)
97
138
cur_obj = res_currency.browse(cr,uid,currency.values(),context)
98
139
cur_obj = dict([(o.id, o) for o in cur_obj])
100
res[id] = res_currency.round(cr,uid,cur_obj[currency[id]],res.get(id,0.0))
142
res[id] = res_currency.round(cr,uid,cur_obj[currency[id]],res.get(id,0.0))
102
144
return dict([(i, res[i]) for i in ids ])
104
146
def _quantity_calc(self, cr, uid, ids, name, arg, context={}):
147
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
148
'_quantity_calc ids:%s'%ids)
105
149
#XXX must convert into one uom
106
151
ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
107
acc_set = ",".join(map(str, ids2))
154
res.setdefault(i,0.0)
110
if context.get('from_date',False):
111
where_date += " AND l.date >= '" + context['from_date'] + "'"
112
if context.get('to_date',False):
113
where_date += " AND l.date <= '" + context['to_date'] + "'"
160
if context.get('from_date'):
161
where_date += " AND l.date >= %(from_date)s"
162
if context.get('to_date'):
163
where_date += " AND l.date <= %(to_date)s"
115
165
cr.execute('SELECT a.id, COALESCE(SUM(l.unit_amount), 0) \
116
166
FROM account_analytic_account a \
117
LEFT JOIN account_analytic_line l ON (a.id = l.account_id ' + where_date + ') \
118
WHERE a.id IN ('+acc_set+') GROUP BY a.id')
167
LEFT JOIN account_analytic_line l ON (a.id = l.account_id %s) \
168
WHERE a.id IN %%(ids)s GROUP BY a.id'%(where_date),
169
dict(context, ids=tuple(ids2)))
120
171
for account_id, sum in cr.fetchall():
121
172
res[account_id] = sum
173
self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
174
'_quantity_calc, (id, sum): %s'%res)
124
179
for child in self.search(cr, uid, [('parent_id', 'child_of', [id])]):
126
181
res.setdefault(id, 0.0)
127
182
res[id] += res.get(child, 0.0)
128
183
return dict([(i, res[i]) for i in ids])