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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ##############################################################################
31 from report import report_sxw
32 from common_report_header import common_report_header
34 class general_ledger(report_sxw.rml_parse, common_report_header):
35 _name = 'report.account.general.ledger'
37 def set_context(self, objects, data, ids, report_type=None):
39 obj_move = self.pool.get('account.move.line')
40 self.sortby = data['form'].get('sortby', 'sort_date')
41 self.query = obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context',{}))
42 ctx2 = data['form'].get('used_context',{}).copy()
43 ctx2.update({'initial_bal': True})
44 self.init_query = obj_move._query_get(self.cr, self.uid, obj='l', context=ctx2)
45 self.init_balance = data['form']['initial_balance']
46 self.display_account = data['form']['display_account']
47 self.target_move = data['form'].get('target_move', 'all')
48 ctx = self.context.copy()
49 ctx['fiscalyear'] = data['form']['fiscalyear_id']
50 if data['form']['filter'] == 'filter_period':
51 ctx['periods'] = data['form']['periods']
52 elif data['form']['filter'] == 'filter_date':
53 ctx['date_from'] = data['form']['date_from']
54 ctx['date_to'] = data['form']['date_to']
55 ctx['state'] = data['form']['target_move']
56 self.context.update(ctx)
57 if (data['model'] == 'ir.ui.menu'):
58 new_ids = [data['form']['chart_account_id']]
59 objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
60 return super(general_ledger, self).set_context(objects, data, new_ids, report_type=report_type)
62 def __init__(self, cr, uid, name, context=None):
65 super(general_ledger, self).__init__(cr, uid, name, context=context)
67 self.tot_currency = 0.0
69 self.sold_accounts = {}
70 self.sortby = 'sort_date'
71 self.localcontext.update( {
74 'sum_debit_account': self._sum_debit_account,
75 'sum_credit_account': self._sum_credit_account,
76 'sum_balance_account': self._sum_balance_account,
77 'sum_currency_amount_account': self._sum_currency_amount_account,
78 'get_children_accounts': self.get_children_accounts,
79 'get_fiscalyear': self._get_fiscalyear,
80 'get_journal': self._get_journal,
81 'get_account': self._get_account,
82 'get_start_period': self.get_start_period,
83 'get_end_period': self.get_end_period,
84 'get_filter': self._get_filter,
85 'get_sortby': self._get_sortby,
86 'get_start_date':self._get_start_date,
87 'get_end_date':self._get_end_date,
88 'get_target_move': self._get_target_move,
89 'strip_name': self._strip_name,
91 self.context = context
93 def _ellipsis(self, orig_str, maxlen=100, ellipsis='...'):
94 maxlen = maxlen - len(ellipsis)
97 new_str = orig_str[:maxlen]
100 def _strip_name(self, name, maxlen=50):
101 return self._ellipsis(name, maxlen, ' ...')
103 def _sum_currency_amount_account(self, account):
104 self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
105 FROM account_move_line l \
106 WHERE l.account_id = %s AND %s' %(account.id, self.query))
107 sum_currency = self.cr.fetchone()[0] or 0.0
108 if self.init_balance:
109 self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
110 FROM account_move_line l \
111 WHERE l.account_id = %s AND %s '%(account.id, self.init_query))
112 sum_currency += self.cr.fetchone()[0] or 0.0
115 def get_children_accounts(self, account):
117 ids_acc = self.pool.get('account.account')._get_children_and_consol(self.cr, self.uid, account.id)
118 for child_account in self.pool.get('account.account').browse(self.cr, self.uid, ids_acc, context=self.context):
121 FROM account_move_line AS l
122 WHERE %s AND l.account_id = %%s
124 self.cr.execute(sql, (child_account.id,))
125 num_entry = self.cr.fetchone()[0] or 0
126 sold_account = self._sum_balance_account(child_account)
127 self.sold_accounts[child_account.id] = sold_account
128 if self.display_account == 'bal_movement':
129 if child_account.type != 'view' and num_entry <> 0:
130 res.append(child_account)
131 elif self.display_account == 'bal_solde':
132 if child_account.type != 'view' and num_entry <> 0:
133 if ( sold_account <> 0.0):
134 res.append(child_account)
136 res.append(child_account)
141 def lines(self, account):
142 """ Return all the account_move_line of account with their account code counterparts """
143 move_state = ['draft','posted']
144 if self.target_move == 'posted':
145 move_state = ['posted', '']
146 # First compute all counterpart strings for every move_id where this account appear.
147 # Currently, the counterpart info is used only in landscape mode
150 array_to_string(ARRAY(SELECT DISTINCT a.code
151 FROM account_move_line m2
152 LEFT JOIN account_account a ON (m2.account_id=a.id)
153 WHERE m2.move_id = m1.move_id
154 AND m2.account_id<>%%s), ', ') AS counterpart
156 FROM account_move_line l
157 LEFT JOIN account_move am ON (am.id = l.move_id)
158 WHERE am.state IN %s and %s AND l.account_id = %%s GROUP BY move_id) m1
159 """% (tuple(move_state), self.query)
160 self.cr.execute(sql, (account.id, account.id))
161 counterpart_res = self.cr.dictfetchall()
162 counterpart_accounts = {}
163 for i in counterpart_res:
164 counterpart_accounts[i['move_id']] = i['counterpart']
167 # Then select all account_move_line of this account
168 if self.sortby == 'sort_journal_partner':
169 sql_sort='j.code, p.name'
173 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,
174 m.name AS move_name, m.id AS mmove_id,
175 c.symbol AS currency_code,
176 i.id AS invoice_id, i.type AS invoice_type, i.number AS invoice_number,
177 p.name AS partner_name
178 FROM account_move_line l
179 JOIN account_move m on (l.move_id=m.id)
180 LEFT JOIN res_currency c on (l.currency_id=c.id)
181 LEFT JOIN res_partner p on (l.partner_id=p.id)
182 LEFT JOIN account_invoice i on (m.id =i.move_id)
183 JOIN account_journal j on (l.journal_id=j.id)
184 WHERE %s AND m.state IN %s AND l.account_id = %%s ORDER by %s
185 """ %(self.query, tuple(move_state), sql_sort)
186 self.cr.execute(sql, (account.id,))
187 res_lines = self.cr.dictfetchall()
189 if res_lines and self.init_balance:
190 #FIXME: replace the label of lname with a string translatable
192 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,
193 '' AS move_name, '' AS mmove_id,
196 '' AS invoice_id, '' AS invoice_type, '' AS invoice_number,
198 FROM account_move_line l
199 LEFT JOIN account_move m on (l.move_id=m.id)
200 LEFT JOIN res_currency c on (l.currency_id=c.id)
201 LEFT JOIN res_partner p on (l.partner_id=p.id)
202 LEFT JOIN account_invoice i on (m.id =i.move_id)
203 JOIN account_journal j on (l.journal_id=j.id)
204 WHERE %s AND m.state IN %s AND l.account_id = %%s
205 """ %(self.init_query, tuple(move_state))
206 self.cr.execute(sql, (account.id,))
207 res_init = self.cr.dictfetchall()
208 res = res_init + res_lines
210 inv_types = { 'out_invoice': 'CI', 'in_invoice': 'SI', 'out_refund': 'OR', 'in_refund': 'SR', }
212 l['move'] = l['move_name']
214 l['lref'] = '%s: %s'%(inv_types[l['invoice_type']], l['invoice_number'])
215 l['partner'] = l['partner_name'] or ''
216 account_sum += l['debit'] - l['credit']
217 l['progress'] = account_sum
218 l['line_corresp'] = l['mmove_id'] == '' and ' ' or counterpart_accounts[l['mmove_id']]
219 # Modification of amount Currency
221 if l['amount_currency'] != None:
222 l['amount_currency'] = abs(l['amount_currency']) * -1
223 if l['amount_currency'] != None:
224 self.tot_currency = self.tot_currency + l['amount_currency']
227 def _sum_debit_account(self, account):
228 if account.type == 'view':
230 move_state = ['draft','posted']
231 if self.target_move == 'posted':
232 move_state = ['posted','']
233 self.cr.execute('SELECT sum(debit) \
234 FROM account_move_line l \
235 JOIN account_move am ON (am.id = l.move_id) \
236 WHERE (l.account_id = %s) \
237 AND (am.state IN %s) \
238 AND '+ self.query +' '
239 ,(account.id, tuple(move_state)))
240 sum_debit = self.cr.fetchone()[0] or 0.0
241 if self.init_balance:
242 self.cr.execute('SELECT sum(debit) \
243 FROM account_move_line l \
244 JOIN account_move am ON (am.id = l.move_id) \
245 WHERE (l.account_id = %s) \
246 AND (am.state IN %s) \
247 AND '+ self.init_query +' '
248 ,(account.id, tuple(move_state)))
249 # Add initial balance to the result
250 sum_debit += self.cr.fetchone()[0] or 0.0
253 def _sum_credit_account(self, account):
254 if account.type == 'view':
255 return account.credit
256 move_state = ['draft','posted']
257 if self.target_move == 'posted':
258 move_state = ['posted','']
259 self.cr.execute('SELECT sum(credit) \
260 FROM account_move_line l \
261 JOIN account_move am ON (am.id = l.move_id) \
262 WHERE (l.account_id = %s) \
263 AND (am.state IN %s) \
264 AND '+ self.query +' '
265 ,(account.id, tuple(move_state)))
266 sum_credit = self.cr.fetchone()[0] or 0.0
267 if self.init_balance:
268 self.cr.execute('SELECT sum(credit) \
269 FROM account_move_line l \
270 JOIN account_move am ON (am.id = l.move_id) \
271 WHERE (l.account_id = %s) \
272 AND (am.state IN %s) \
273 AND '+ self.init_query +' '
274 ,(account.id, tuple(move_state)))
275 # Add initial balance to the result
276 sum_credit += self.cr.fetchone()[0] or 0.0
279 def _sum_balance_account(self, account):
280 if account.type == 'view':
281 return account.balance
282 move_state = ['draft','posted']
283 if self.target_move == 'posted':
284 move_state = ['posted','']
285 self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
286 FROM account_move_line l \
287 JOIN account_move am ON (am.id = l.move_id) \
288 WHERE (l.account_id = %s) \
289 AND (am.state IN %s) \
290 AND '+ self.query +' '
291 ,(account.id, tuple(move_state)))
292 sum_balance = self.cr.fetchone()[0] or 0.0
293 if self.init_balance:
294 self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
295 FROM account_move_line l \
296 JOIN account_move am ON (am.id = l.move_id) \
297 WHERE (l.account_id = %s) \
298 AND (am.state IN %s) \
299 AND '+ self.init_query +' '
300 ,(account.id, tuple(move_state)))
301 # Add initial balance to the result
302 sum_balance += self.cr.fetchone()[0] or 0.0
305 def _get_account(self, data):
306 if data['model'] == 'account.account':
307 return self.pool.get('account.account').browse(self.cr, self.uid, data['form']['id']).company_id.name
308 return super(general_ledger ,self)._get_account(data)
310 def _get_sortby(self, data):
311 if self.sortby == 'sort_date':
313 elif self.sortby == 'sort_journal_partner':
314 return 'Journal & Partner'
317 report_sxw.report_sxw('report.account.general.ledger', 'account.account', 'addons/account/report/account_general_ledger.rml', parser=general_ledger, header='internal')
318 report_sxw.report_sxw('report.account.general.ledger_landscape', 'account.account', 'addons/account/report/account_general_ledger_landscape.rml', parser=general_ledger, header='internal landscape')
320 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: