Merge branch 'master' of ssh://ced@dev.tinyerp.com//home/ced/terp
[odoo/odoo.git] / addons / account / project / report / analytic_balance.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting FROM its eventual inadequacies AND bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees AND support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it AND/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import pooler
30 import time
31 from report import report_sxw
32
33 class account_analytic_balance(report_sxw.rml_parse):
34         def __init__(self, cr, uid, name, context):
35                 super(account_analytic_balance, self).__init__(cr, uid, name, context)
36                 self.localcontext.update( {
37                         'time': time,
38                         'lines_g': self._lines_g,
39                         'move_sum_debit': self._move_sum_debit,
40                         'move_sum_credit': self._move_sum_credit,
41                         'sum_debit': self._sum_debit,
42                         'sum_credit': self._sum_credit,
43                         'sum_balance': self._sum_balance,
44                         'sum_quantity': self._sum_quantity,
45                         'move_sum_balance': self._move_sum_balance,
46                         'move_sum_quantity': self._move_sum_quantity,
47                 })
48                 
49         def _lines_g(self, account_id, date1, date2):
50                 self.cr.execute("SELECT aa.name AS name, aa.code AS code, sum(aal.amount) AS balance, sum(aal.unit_amount) AS quantity \
51                                 FROM account_analytic_line AS aal, account_account AS aa \
52                                 WHERE (aal.general_account_id=aa.id) AND (aal.account_id=%d) AND (date>=%s) AND (date<=%s) AND aa.active \
53                                 GROUP BY aal.general_account_id, aa.name, aa.code, aal.code ORDER BY aal.code", (account_id, date1, date2))
54                 res = self.cr.dictfetchall()
55                 
56                 for r in res:
57                         if r['balance'] > 0:
58                                 r['debit'] = r['balance']
59                                 r['credit'] = 0.0
60                         elif r['balance'] < 0:
61                                 r['debit'] = 0.0
62                                 r['credit'] = -r['balance']
63                         else:
64                                 r['balance'] == 0
65                                 r['debit'] = 0.0
66                                 r['credit'] = 0.0
67                 return res
68         
69
70         def _move_sum_debit(self, account_id, date1, date2):
71                 self.cr.execute("SELECT sum(amount) \
72                                 FROM account_analytic_line \
73                                 WHERE account_id=%d AND date>=%s AND date<=%s AND amount>0", (account_id, date1, date2))
74                 return self.cr.fetchone()[0] or 0.0
75
76         def _move_sum_credit(self, account_id, date1, date2):
77                 self.cr.execute("SELECT -sum(amount) \
78                                 FROM account_analytic_line \
79                                 WHERE account_id=%d AND date>=%s AND date<=%s AND amount<0", (account_id, date1, date2))
80                 return self.cr.fetchone()[0] or 0.0
81         
82         def _move_sum_balance(self, account_id, date1, date2):
83                 debit = self._move_sum_debit(account_id, date1, date2) 
84                 credit = self._move_sum_credit(account_id, date1, date2)
85                 return (debit-credit)
86         
87         def _move_sum_quantity(self, account_id, date1, date2):
88                 self.cr.execute("SELECT sum(unit_amount) \
89                                 FROM account_analytic_line \
90                                 WHERE account_id=%d AND date>=%s AND date<=%s", (account_id, date1, date2))
91                 return self.cr.fetchone()[0] or 0.0
92
93         
94         def _sum_debit(self, accounts, date1, date2):
95                 ids = map(lambda x: x.id, accounts)
96                 if not len(ids):
97                         return 0.0
98                 self.cr.execute("SELECT sum(amount) \
99                                 FROM account_analytic_line \
100                                 WHERE account_id IN ("+','.join(map(str, ids))+") AND date>=%s AND date<=%s AND amount>0", (date1, date2))
101                 return self.cr.fetchone()[0] or 0.0
102                 
103         def _sum_credit(self, accounts, date1, date2):
104                 ids = map(lambda x: x.id, accounts)
105                 if not len(ids):
106                         return 0.0
107                 ids = map(lambda x: x.id, accounts)
108                 self.cr.execute("SELECT -sum(amount) \
109                                 FROM account_analytic_line \
110                                 WHERE account_id IN ("+','.join(map(str, ids))+") AND date>=%s AND date<=%s AND amount<0", (date1, date2))
111                 return self.cr.fetchone()[0] or 0.0
112
113         def _sum_balance(self, accounts, date1, date2):
114                 debit = self._sum_debit(accounts, date1, date2) or 0.0
115                 credit = self._sum_credit(accounts, date1, date2) or 0.0
116                 return (debit-credit)
117
118         def _sum_quantity(self, accounts, date1, date2):
119                 ids = map(lambda x: x.id, accounts)
120                 if not len(ids):
121                         return 0.0
122                 self.cr.execute("SELECT sum(unit_amount) \
123                                 FROM account_analytic_line \
124                                 WHERE account_id IN ("+','.join(map(str, ids))+") AND date>=%s AND date<=%s", (date1, date2))
125                 return self.cr.fetchone()[0] or 0.0
126
127 report_sxw.report_sxw('report.account.analytic.account.balance', 'account.analytic.account', 'addons/account/project/report/analytic_balance.rml',parser=account_analytic_balance, header=False)
128