1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting FROM its eventual inadequacies AND bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees AND support are strongly adviced to contract a Free Software
15 # This program is Free Software; you can redistribute it AND/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
33 from report import report_sxw
36 class account_analytic_balance(rml_parse.rml_parse):
37 def __init__(self, cr, uid, name, context):
38 super(account_analytic_balance, self).__init__(cr, uid, name, context)
39 self.localcontext.update( {
41 'lines_g': self._lines_g,
42 'move_sum_debit': self._move_sum_debit,
43 'move_sum_credit': self._move_sum_credit,
44 'sum_debit': self._sum_debit,
45 'sum_credit': self._sum_credit,
46 'sum_balance': self._sum_balance,
47 'sum_quantity': self._sum_quantity,
48 'move_sum_balance': self._move_sum_balance,
49 'move_sum_quantity': self._move_sum_quantity,
52 def _lines_g(self, account_id, date1, date2):
53 account_analytic_obj = self.pool.get('account.analytic.account')
54 ids = account_analytic_obj.search(self.cr, self.uid,
55 [('parent_id', 'child_of', [account_id])])
56 self.cr.execute("SELECT aa.name AS name, aa.code AS code, \
57 sum(aal.amount) AS balance, sum(aal.unit_amount) AS quantity \
58 FROM account_analytic_line AS aal, account_account AS aa \
59 WHERE (aal.general_account_id=aa.id) \
60 AND (aal.account_id in (" + ','.join(map(str, ids)) + "))\
61 AND (date>=%s) AND (date<=%s) AND aa.active \
62 GROUP BY aal.general_account_id, aa.name, aa.code, aal.code \
63 ORDER BY aal.code", (date1, date2))
64 res = self.cr.dictfetchall()
68 r['debit'] = r['balance']
70 elif r['balance'] < 0:
72 r['credit'] = -r['balance']
80 def _move_sum_debit(self, account_id, date1, date2):
81 account_analytic_obj = self.pool.get('account.analytic.account')
82 ids = account_analytic_obj.search(self.cr, self.uid,
83 [('parent_id', 'child_of', [account_id])])
84 self.cr.execute("SELECT sum(amount) \
85 FROM account_analytic_line \
86 WHERE account_id in ("+ ','.join(map(str, ids)) +") \
87 AND date>=%s AND date<=%s AND amount>0",
89 return self.cr.fetchone()[0] or 0.0
91 def _move_sum_credit(self, account_id, date1, date2):
92 account_analytic_obj = self.pool.get('account.analytic.account')
93 ids = account_analytic_obj.search(self.cr, self.uid,
94 [('parent_id', 'child_of', [account_id])])
95 self.cr.execute("SELECT -sum(amount) \
96 FROM account_analytic_line \
97 WHERE account_id in ("+ ','.join(map(str, ids)) +") \
98 AND date>=%s AND date<=%s AND amount<0",
100 return self.cr.fetchone()[0] or 0.0
102 def _move_sum_balance(self, account_id, date1, date2):
103 debit = self._move_sum_debit(account_id, date1, date2)
104 credit = self._move_sum_credit(account_id, date1, date2)
105 return (debit-credit)
107 def _move_sum_quantity(self, account_id, date1, date2):
108 account_analytic_obj = self.pool.get('account.analytic.account')
109 ids = account_analytic_obj.search(self.cr, self.uid,
110 [('parent_id', 'child_of', [account_id])])
111 self.cr.execute("SELECT sum(unit_amount) \
112 FROM account_analytic_line \
113 WHERE account_id in ("+ ','.join(map(str, ids)) +") \
114 AND date>=%s AND date<=%s",
116 return self.cr.fetchone()[0] or 0.0
119 def _sum_debit(self, accounts, date1, date2):
120 ids = map(lambda x: x.id, accounts)
123 account_analytic_obj = self.pool.get('account.analytic.account')
124 ids2 = account_analytic_obj.search(self.cr, self.uid,
125 [('parent_id', 'child_of', ids)])
126 self.cr.execute("SELECT sum(amount) \
127 FROM account_analytic_line \
128 WHERE account_id IN ("+','.join(map(str, ids2))+") \
129 AND date>=%s AND date<=%s AND amount>0",
131 return self.cr.fetchone()[0] or 0.0
133 def _sum_credit(self, accounts, date1, date2):
134 ids = map(lambda x: x.id, accounts)
137 ids = map(lambda x: x.id, accounts)
138 account_analytic_obj = self.pool.get('account.analytic.account')
139 ids2 = account_analytic_obj.search(self.cr, self.uid,
140 [('parent_id', 'child_of', ids)])
141 self.cr.execute("SELECT -sum(amount) \
142 FROM account_analytic_line \
143 WHERE account_id IN ("+','.join(map(str, ids2))+") \
144 AND date>=%s AND date<=%s AND amount<0",
146 return self.cr.fetchone()[0] or 0.0
148 def _sum_balance(self, accounts, date1, date2):
149 debit = self._sum_debit(accounts, date1, date2) or 0.0
150 credit = self._sum_credit(accounts, date1, date2) or 0.0
151 return (debit-credit)
153 def _sum_quantity(self, accounts, date1, date2):
154 ids = map(lambda x: x.id, accounts)
157 account_analytic_obj = self.pool.get('account.analytic.account')
158 ids2 = account_analytic_obj.search(self.cr, self.uid,
159 [('parent_id', 'child_of', ids)])
160 self.cr.execute("SELECT sum(unit_amount) \
161 FROM account_analytic_line \
162 WHERE account_id IN ("+','.join(map(str, ids2))+") \
163 AND date>=%s AND date<=%s",
165 return self.cr.fetchone()[0] or 0.0
167 report_sxw.report_sxw('report.account.analytic.account.balance',
168 'account.analytic.account', 'addons/account/project/report/analytic_balance.rml',
169 parser=account_analytic_balance, header=False)
172 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: