1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
24
from report import report_sxw
26
class account_inverted_analytic_balance(report_sxw.rml_parse):
27
def __init__(self, cr, uid, name, context):
28
super(account_inverted_analytic_balance, self).__init__(cr, uid, name, context=context)
29
self.localcontext.update( {
31
'lines_g': self._lines_g,
32
'lines_a': self._lines_a,
33
'sum_debit': self._sum_debit,
34
'sum_credit': self._sum_credit,
35
'sum_balance': self._sum_balance,
36
'sum_quantity': self._sum_quantity,
39
def _lines_g(self, accounts, date1, date2):
40
ids = map(lambda x: x.id, accounts)
41
self.cr.execute("SELECT aa.name AS name, aa.code AS code, sum(aal.amount) AS balance, sum(aal.unit_amount) AS quantity, aa.id AS id \
42
FROM account_analytic_line AS aal, account_account AS aa \
43
WHERE (aal.general_account_id=aa.id) AND (aal.account_id =ANY(%s)) AND (date>=%s) AND (date<=%s) AND aa.active \
44
GROUP BY aal.general_account_id, aa.name, aa.code, aal.code, aa.id ORDER BY aal.code", (ids,date1,date2,))
45
res = self.cr.dictfetchall()
49
r['debit'] = r['balance']
51
elif r['balance'] < 0:
53
r['credit'] = -r['balance']
59
def _lines_a(self, accounts, general_account_id, date1, date2):
60
ids = map(lambda x: x.id, accounts)
61
self.cr.execute("SELECT sum(aal.amount) AS balance, sum(aal.unit_amount) AS quantity, aaa.code AS code, aaa.name AS name, account_id \
62
FROM account_analytic_line AS aal, account_analytic_account AS aaa \
63
WHERE aal.account_id=aaa.id AND aal.account_id =ANY(%s) AND aal.general_account_id=%s AND aal.date>=%s AND aal.date<=%s \
64
GROUP BY aal.account_id, general_account_id, aaa.code, aaa.name ORDER BY aal.account_id", (ids,general_account_id, date1, date2,))
65
res = self.cr.dictfetchall()
67
aaa_obj = self.pool.get('account.analytic.account')
68
res2 = aaa_obj.read(self.cr, self.uid, ids, ['complete_name'])
71
complete_name[r['id']] = r['complete_name']
73
r['complete_name'] = complete_name[r['account_id']]
75
r['debit'] = r['balance']
77
elif r['balance'] < 0:
79
r['credit'] = -r['balance']
85
def _sum_debit(self, accounts, date1, date2):
86
ids = map(lambda x: x.id, accounts)
87
self.cr.execute("SELECT sum(amount) \
88
FROM account_analytic_line \
89
WHERE account_id =ANY(%s) AND date>=%s AND date<=%s AND amount>0", (ids,date1, date2,))
90
return self.cr.fetchone()[0] or 0.0
92
def _sum_credit(self, accounts, date1, date2):
93
ids = map(lambda x: x.id, accounts)
94
self.cr.execute("SELECT -sum(amount) \
95
FROM account_analytic_line \
96
WHERE account_id =ANY(%s) AND date>=%s AND date<=%s AND amount<0", (ids,date1, date2,))
97
return self.cr.fetchone()[0] or 0.0
99
def _sum_balance(self, accounts, date1, date2):
100
debit = self._sum_debit(accounts, date1, date2)
101
credit = self._sum_credit(accounts, date1, date2)
102
return (debit-credit)
104
def _sum_quantity(self, accounts, date1, date2):
105
ids = map(lambda x: x.id, accounts)
106
self.cr.execute("SELECT sum(unit_amount) \
107
FROM account_analytic_line \
108
WHERE account_id =ANY(%s) AND date>=%s AND date<=%s", (ids,date1, date2,))
109
return self.cr.fetchone()[0] or 0.0
111
report_sxw.report_sxw('report.account.analytic.account.inverted.balance', 'account.analytic.account', 'addons/account/project/report/inverted_analytic_balance.rml',parser=account_inverted_analytic_balance, header=False)
114
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: