Launchpad automatic translations update.
[odoo/odoo.git] / addons / account / report / account_general_journal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23 from common_report_header import common_report_header
24 from openerp.report import report_sxw
25
26 class journal_print(report_sxw.rml_parse, common_report_header):
27
28     def __init__(self, cr, uid, name, context=None):
29         if context is None:
30             context = {}
31         super(journal_print, self).__init__(cr, uid, name, context=context)
32         self.period_ids = []
33         self.journal_ids = []
34         self.localcontext.update( {
35             'time': time,
36             'lines': self.lines,
37             'periods': self.periods,
38             'sum_debit_period': self._sum_debit_period,
39             'sum_credit_period': self._sum_credit_period,
40             'sum_debit': self._sum_debit,
41             'sum_credit': self._sum_credit,
42             'get_fiscalyear': self._get_fiscalyear,
43             'get_account': self._get_account,
44             'get_start_period': self.get_start_period,
45             'get_end_period': self.get_end_period,
46             'get_sortby': self._get_sortby,
47             'get_filter': self._get_filter,
48             'get_journal': self._get_journal,
49             'get_start_date':self._get_start_date,
50             'get_end_date':self._get_end_date,
51             'display_currency':self._display_currency,
52             'get_target_move': self._get_target_move,
53         })
54
55     def set_context(self, objects, data, ids, report_type=None):
56         obj_move = self.pool.get('account.move.line')
57         new_ids = ids
58         self.query_get_clause = ''
59         self.target_move = data['form'].get('target_move', 'all')
60         if (data['model'] == 'ir.ui.menu'):
61             new_ids = 'active_ids' in data['form'] and data['form']['active_ids'] or []
62             self.query_get_clause = 'AND '
63             self.query_get_clause += obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
64             objects = self.pool.get('account.journal.period').browse(self.cr, self.uid, new_ids)
65         if new_ids:
66             self.cr.execute('SELECT period_id, journal_id FROM account_journal_period WHERE id IN %s', (tuple(new_ids),))
67             res = self.cr.fetchall()
68             self.period_ids, self.journal_ids = zip(*res)
69         return super(journal_print, self).set_context(objects, data, ids, report_type=report_type)
70
71     # returns a list of period objs
72     def periods(self, journal_period_objs):
73         dic = {}
74         def filter_unique(o):
75             key = o.period_id.id
76             res = key in dic
77             if not res:
78                 dic[key] = True
79             return not res
80         filtered_objs = filter(filter_unique, journal_period_objs)
81         return map(lambda x: x.period_id, filtered_objs)
82
83     def lines(self, period_id):
84         if not self.journal_ids:
85             return []
86         move_state = ['draft','posted']
87         if self.target_move == 'posted':
88             move_state = ['posted']
89         self.cr.execute('SELECT j.code, j.name, l.amount_currency,c.symbol AS currency_code,l.currency_id, '
90                         'SUM(l.debit) AS debit, SUM(l.credit) AS credit '
91                         'FROM account_move_line l '
92                         'LEFT JOIN account_move am ON (l.move_id=am.id) '
93                         'LEFT JOIN account_journal j ON (l.journal_id=j.id) '
94                         'LEFT JOIN res_currency c on (l.currency_id=c.id)'
95                         'WHERE am.state IN %s AND l.period_id=%s AND l.journal_id IN %s ' + self.query_get_clause + ' '
96                         'GROUP BY j.id, j.code, j.name, l.amount_currency, c.symbol, l.currency_id ',
97                         (tuple(move_state), period_id, tuple(self.journal_ids)))
98         return self.cr.dictfetchall()
99
100     def _set_get_account_currency_code(self, account_id):
101         self.cr.execute("SELECT c.symbol AS code "\
102                         "FROM res_currency c, account_account AS ac "\
103                         "WHERE ac.id = %s AND ac.currency_id = c.id" % (account_id))
104         result = self.cr.fetchone()
105         if result:
106             self.account_currency = result[0]
107         else:
108             self.account_currency = False
109
110     def _get_account(self, data):
111         if data['model'] == 'account.journal.period':
112             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).company_id.name
113         return super(journal_print, self)._get_account(data)
114
115     def _get_fiscalyear(self, data):
116         if data['model'] == 'account.journal.period':
117             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).fiscalyear_id.name
118         return super(journal_print, self)._get_fiscalyear(data)
119
120     def _display_currency(self, data):
121         if data['model'] == 'account.journal.period':
122             return True
123         return data['form']['amount_currency']
124
125     def _sum_debit_period(self, period_id, journal_id=False):
126         if journal_id:
127             journals = [journal_id]
128         else:
129             journals = self.journal_ids
130         if not journals:
131             return 0.0
132         move_state = ['draft','posted']
133         if self.target_move == 'posted':
134             move_state = ['posted']
135         self.cr.execute('SELECT SUM(l.debit) FROM account_move_line l '
136                         'LEFT JOIN account_move am ON (l.move_id=am.id) '
137                         'WHERE am.state IN %s AND l.period_id=%s AND l.journal_id IN %s ' + self.query_get_clause + ' ' \
138                         'AND l.state<>\'draft\'',
139                         (tuple(move_state), period_id, tuple(journals)))
140         return self.cr.fetchone()[0] or 0.0
141
142     def _sum_credit_period(self, period_id, journal_id=None):
143         if journal_id:
144             journals = [journal_id]
145         else:
146             journals = self.journal_ids
147         move_state = ['draft','posted']
148         if self.target_move == 'posted':
149             move_state = ['posted']
150         if not journals:
151             return 0.0
152         self.cr.execute('SELECT SUM(l.credit) FROM account_move_line l '
153                         'LEFT JOIN account_move am ON (l.move_id=am.id) '
154                         'WHERE am.state IN %s AND l.period_id=%s AND l.journal_id IN %s '+ self.query_get_clause + ' ' \
155                         'AND l.state<>\'draft\'',
156                         (tuple(move_state), period_id, tuple(journals)))
157         return self.cr.fetchone()[0] or 0.0
158
159 report_sxw.report_sxw('report.account.general.journal', 'account.journal.period', 'addons/account/report/general_journal.rml', parser=journal_print, header='internal')
160
161 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: