1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2005-2006 CamptoCamp
5 # Copyright (c) 2006-2010 OpenERP S.A
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsibility of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # guarantees and support are strongly advised to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 ##############################################################################
31 from openerp.osv import osv
32 from openerp.report import report_sxw
33 from common_report_header import common_report_header
36 class general_ledger(report_sxw.rml_parse, common_report_header):
37 _name = 'report.account.general.ledger'
39 def set_context(self, objects, data, ids, report_type=None):
41 obj_move = self.pool.get('account.move.line')
42 self.sortby = data['form'].get('sortby', 'sort_date')
43 self.query = obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context',{}))
44 ctx2 = data['form'].get('used_context',{}).copy()
45 self.init_balance = data['form'].get('initial_balance', True)
47 ctx2.update({'initial_bal': True})
48 self.init_query = obj_move._query_get(self.cr, self.uid, obj='l', context=ctx2)
49 self.display_account = data['form']['display_account']
50 self.target_move = data['form'].get('target_move', 'all')
51 ctx = self.context.copy()
52 ctx['fiscalyear'] = data['form']['fiscalyear_id']
53 if data['form']['filter'] == 'filter_period':
54 ctx['periods'] = data['form']['periods']
55 elif data['form']['filter'] == 'filter_date':
56 ctx['date_from'] = data['form']['date_from']
57 ctx['date_to'] = data['form']['date_to']
58 ctx['state'] = data['form']['target_move']
59 self.context.update(ctx)
60 if (data['model'] == 'ir.ui.menu'):
61 new_ids = [data['form']['chart_account_id']]
62 objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
63 return super(general_ledger, self).set_context(objects, data, new_ids, report_type=report_type)
65 def __init__(self, cr, uid, name, context=None):
68 super(general_ledger, self).__init__(cr, uid, name, context=context)
70 self.tot_currency = 0.0
72 self.sold_accounts = {}
73 self.sortby = 'sort_date'
74 self.localcontext.update( {
77 'sum_debit_account': self._sum_debit_account,
78 'sum_credit_account': self._sum_credit_account,
79 'sum_balance_account': self._sum_balance_account,
80 'sum_currency_amount_account': self._sum_currency_amount_account,
81 'get_children_accounts': self.get_children_accounts,
82 'get_fiscalyear': self._get_fiscalyear,
83 'get_journal': self._get_journal,
84 'get_account': self._get_account,
85 'get_start_period': self.get_start_period,
86 'get_end_period': self.get_end_period,
87 'get_filter': self._get_filter,
88 'get_sortby': self._get_sortby,
89 'get_start_date':self._get_start_date,
90 'get_end_date':self._get_end_date,
91 'get_target_move': self._get_target_move,
93 self.context = context
95 def _sum_currency_amount_account(self, account):
96 self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
97 FROM account_move_line l \
98 WHERE l.account_id = %s AND %s' %(account.id, self.query))
99 sum_currency = self.cr.fetchone()[0] or 0.0
100 if self.init_balance:
101 self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
102 FROM account_move_line l \
103 WHERE l.account_id = %s AND %s '%(account.id, self.init_query))
104 sum_currency += self.cr.fetchone()[0] or 0.0
107 def get_children_accounts(self, account):
109 currency_obj = self.pool.get('res.currency')
110 ids_acc = self.pool.get('account.account')._get_children_and_consol(self.cr, self.uid, account.id)
111 currency = account.currency_id and account.currency_id or account.company_id.currency_id
112 for child_account in self.pool.get('account.account').browse(self.cr, self.uid, ids_acc, context=self.context):
115 FROM account_move_line AS l
116 WHERE %s AND l.account_id = %%s
118 self.cr.execute(sql, (child_account.id,))
119 num_entry = self.cr.fetchone()[0] or 0
120 sold_account = self._sum_balance_account(child_account)
121 self.sold_accounts[child_account.id] = sold_account
122 if self.display_account == 'movement':
123 if child_account.type != 'view' and num_entry <> 0:
124 res.append(child_account)
125 elif self.display_account == 'not_zero':
126 if child_account.type != 'view' and num_entry <> 0:
127 if not currency_obj.is_zero(self.cr, self.uid, currency, sold_account):
128 res.append(child_account)
130 res.append(child_account)
135 def lines(self, account):
136 """ Return all the account_move_line of account with their account code counterparts """
137 move_state = ['draft','posted']
138 if self.target_move == 'posted':
139 move_state = ['posted', '']
140 # First compute all counterpart strings for every move_id where this account appear.
141 # Currently, the counterpart info is used only in landscape mode
144 array_to_string(ARRAY(SELECT DISTINCT a.code
145 FROM account_move_line m2
146 LEFT JOIN account_account a ON (m2.account_id=a.id)
147 WHERE m2.move_id = m1.move_id
148 AND m2.account_id<>%%s), ', ') AS counterpart
150 FROM account_move_line l
151 LEFT JOIN account_move am ON (am.id = l.move_id)
152 WHERE am.state IN %s and %s AND l.account_id = %%s GROUP BY move_id) m1
153 """% (tuple(move_state), self.query)
154 self.cr.execute(sql, (account.id, account.id))
155 counterpart_res = self.cr.dictfetchall()
156 counterpart_accounts = {}
157 for i in counterpart_res:
158 counterpart_accounts[i['move_id']] = i['counterpart']
161 # Then select all account_move_line of this account
162 if self.sortby == 'sort_journal_partner':
163 sql_sort='j.code, p.name, l.move_id'
165 sql_sort='l.date, l.move_id'
167 SELECT l.id AS lid, l.date AS ldate, j.code AS lcode, l.currency_id,l.amount_currency,l.ref AS lref, l.name AS lname, COALESCE(l.debit,0) AS debit, COALESCE(l.credit,0) AS credit, l.period_id AS lperiod_id, l.partner_id AS lpartner_id,
168 m.name AS move_name, m.id AS mmove_id,per.code as period_code,
169 c.symbol AS currency_code,
170 i.id AS invoice_id, i.type AS invoice_type, i.number AS invoice_number,
171 p.name AS partner_name
172 FROM account_move_line l
173 JOIN account_move m on (l.move_id=m.id)
174 LEFT JOIN res_currency c on (l.currency_id=c.id)
175 LEFT JOIN res_partner p on (l.partner_id=p.id)
176 LEFT JOIN account_invoice i on (m.id =i.move_id)
177 LEFT JOIN account_period per on (per.id=l.period_id)
178 JOIN account_journal j on (l.journal_id=j.id)
179 WHERE %s AND m.state IN %s AND l.account_id = %%s ORDER by %s
180 """ %(self.query, tuple(move_state), sql_sort)
181 self.cr.execute(sql, (account.id,))
182 res_lines = self.cr.dictfetchall()
184 if res_lines and self.init_balance:
185 #FIXME: replace the label of lname with a string translatable
187 SELECT 0 AS lid, '' AS ldate, '' AS lcode, COALESCE(SUM(l.amount_currency),0.0) AS amount_currency, '' AS lref, 'Initial Balance' AS lname, COALESCE(SUM(l.debit),0.0) AS debit, COALESCE(SUM(l.credit),0.0) AS credit, '' AS lperiod_id, '' AS lpartner_id,
188 '' AS move_name, '' AS mmove_id, '' AS period_code,
191 '' AS invoice_id, '' AS invoice_type, '' AS invoice_number,
193 FROM account_move_line l
194 LEFT JOIN account_move m on (l.move_id=m.id)
195 LEFT JOIN res_currency c on (l.currency_id=c.id)
196 LEFT JOIN res_partner p on (l.partner_id=p.id)
197 LEFT JOIN account_invoice i on (m.id =i.move_id)
198 JOIN account_journal j on (l.journal_id=j.id)
199 WHERE %s AND m.state IN %s AND l.account_id = %%s
200 """ %(self.init_query, tuple(move_state))
201 self.cr.execute(sql, (account.id,))
202 res_init = self.cr.dictfetchall()
203 res = res_init + res_lines
206 l['move'] = l['move_name'] != '/' and l['move_name'] or ('*'+str(l['mmove_id']))
207 l['partner'] = l['partner_name'] or ''
208 account_sum += l['debit'] - l['credit']
209 l['progress'] = account_sum
210 l['line_corresp'] = l['mmove_id'] == '' and ' ' or counterpart_accounts[l['mmove_id']].replace(', ',',')
211 # Modification of amount Currency
213 if l['amount_currency'] != None:
214 l['amount_currency'] = abs(l['amount_currency']) * -1
215 if l['amount_currency'] != None:
216 self.tot_currency = self.tot_currency + l['amount_currency']
219 def _sum_debit_account(self, account):
220 if account.type == 'view':
222 move_state = ['draft','posted']
223 if self.target_move == 'posted':
224 move_state = ['posted','']
225 self.cr.execute('SELECT sum(debit) \
226 FROM account_move_line l \
227 JOIN account_move am ON (am.id = l.move_id) \
228 WHERE (l.account_id = %s) \
229 AND (am.state IN %s) \
230 AND '+ self.query +' '
231 ,(account.id, tuple(move_state)))
232 sum_debit = self.cr.fetchone()[0] or 0.0
233 if self.init_balance:
234 self.cr.execute('SELECT sum(debit) \
235 FROM account_move_line l \
236 JOIN account_move am ON (am.id = l.move_id) \
237 WHERE (l.account_id = %s) \
238 AND (am.state IN %s) \
239 AND '+ self.init_query +' '
240 ,(account.id, tuple(move_state)))
241 # Add initial balance to the result
242 sum_debit += self.cr.fetchone()[0] or 0.0
245 def _sum_credit_account(self, account):
246 if account.type == 'view':
247 return account.credit
248 move_state = ['draft','posted']
249 if self.target_move == 'posted':
250 move_state = ['posted','']
251 self.cr.execute('SELECT sum(credit) \
252 FROM account_move_line l \
253 JOIN account_move am ON (am.id = l.move_id) \
254 WHERE (l.account_id = %s) \
255 AND (am.state IN %s) \
256 AND '+ self.query +' '
257 ,(account.id, tuple(move_state)))
258 sum_credit = self.cr.fetchone()[0] or 0.0
259 if self.init_balance:
260 self.cr.execute('SELECT sum(credit) \
261 FROM account_move_line l \
262 JOIN account_move am ON (am.id = l.move_id) \
263 WHERE (l.account_id = %s) \
264 AND (am.state IN %s) \
265 AND '+ self.init_query +' '
266 ,(account.id, tuple(move_state)))
267 # Add initial balance to the result
268 sum_credit += self.cr.fetchone()[0] or 0.0
271 def _sum_balance_account(self, account):
272 if account.type == 'view':
273 return account.balance
274 move_state = ['draft','posted']
275 if self.target_move == 'posted':
276 move_state = ['posted','']
277 self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
278 FROM account_move_line l \
279 JOIN account_move am ON (am.id = l.move_id) \
280 WHERE (l.account_id = %s) \
281 AND (am.state IN %s) \
282 AND '+ self.query +' '
283 ,(account.id, tuple(move_state)))
284 sum_balance = self.cr.fetchone()[0] or 0.0
285 if self.init_balance:
286 self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
287 FROM account_move_line l \
288 JOIN account_move am ON (am.id = l.move_id) \
289 WHERE (l.account_id = %s) \
290 AND (am.state IN %s) \
291 AND '+ self.init_query +' '
292 ,(account.id, tuple(move_state)))
293 # Add initial balance to the result
294 sum_balance += self.cr.fetchone()[0] or 0.0
297 def _get_account(self, data):
298 if data['model'] == 'account.account':
299 return self.pool.get('account.account').browse(self.cr, self.uid, data['form']['id']).company_id.name
300 return super(general_ledger ,self)._get_account(data)
302 def _get_sortby(self, data):
303 if self.sortby == 'sort_date':
304 return self._translate('Date')
305 elif self.sortby == 'sort_journal_partner':
306 return self._translate('Journal & Partner')
307 return self._translate('Date')
310 class report_generalledger(osv.AbstractModel):
311 _name = 'report.account.report_generalledger'
312 _inherit = 'report.abstract_report'
313 _template = 'account.report_generalledger'
314 _wrapped_report_class = general_ledger
316 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: