[FIX] account, print journal reports: fixed active_ids and blank lines at the beginni...
[odoo/odoo.git] / addons / account / report / account_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 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.last_move_id = False
34         self.journal_ids = []
35         self.sort_selection = 'am.name'
36         self.localcontext.update({
37             'time': time,
38             'lines': self.lines,
39             'sum_debit': self._sum_debit,
40             'sum_credit': self._sum_credit,
41             'get_start_period': self.get_start_period,
42             'get_end_period': self.get_end_period,
43             'get_account': self._get_account,
44             'get_filter': self._get_filter,
45             'get_start_date': self._get_start_date,
46             'get_end_date': self._get_end_date,
47             'get_fiscalyear': self._get_fiscalyear,
48             'display_currency':self._display_currency,
49             'get_sortby': self._get_sortby,
50             'get_target_move': self._get_target_move,
51             'check_last_move_id': self.check_last_move_id,
52             'set_last_move_id': self.set_last_move_id,
53             'tax_codes': self.tax_codes,
54             'sum_vat': self._sum_vat,
55     })
56
57     def set_context(self, objects, data, ids, report_type=None):
58         obj_move = self.pool.get('account.move.line')
59         new_ids = ids
60         self.query_get_clause = ''
61         self.target_move = data['form'].get('target_move', 'all')
62         if (data['model'] == 'ir.ui.menu'):
63             self.period_ids = tuple(data['form']['periods'])
64             self.journal_ids = tuple(data['form']['journal_ids'])
65             new_ids = data['form'].get('active_ids', [])
66             self.query_get_clause = 'AND '
67             self.query_get_clause += obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
68             self.sort_selection = data['form'].get('sort_selection', 'date')
69             objects = self.pool.get('account.journal.period').browse(self.cr, self.uid, new_ids)
70         elif new_ids:
71             #in case of direct access from account.journal.period object, we need to set the journal_ids and periods_ids
72             self.cr.execute('SELECT period_id, journal_id FROM account_journal_period WHERE id IN %s', (tuple(new_ids),))
73             res = self.cr.fetchall()
74             self.period_ids, self.journal_ids = zip(*res)
75         return super(journal_print, self).set_context(objects, data, ids, report_type=report_type)
76
77     def set_last_move_id(self, move_id):
78         self.last_move_id = move_id
79
80     def check_last_move_id(self, move_id):
81         '''
82         return True if we need to draw a gray line above this line, used to separate moves
83         '''
84         if self.last_move_id:
85             return not(self.last_move_id == move_id)
86         return False
87
88     def tax_codes(self, period_id, journal_id):
89         ids_journal_period = self.pool.get('account.journal.period').search(self.cr, self.uid, 
90             [('journal_id', '=', journal_id), ('period_id', '=', period_id)])
91         self.cr.execute(
92             'select distinct tax_code_id from account_move_line ' \
93             'where period_id=%s and journal_id=%s and tax_code_id is not null and state<>\'draft\'',
94             (period_id, journal_id)
95         )
96         ids = map(lambda x: x[0], self.cr.fetchall())
97         tax_code_ids = []
98         if ids:
99             self.cr.execute('select id from account_tax_code where id in %s order by code', (tuple(ids),))
100             tax_code_ids = map(lambda x: x[0], self.cr.fetchall())
101         tax_codes = self.pool.get('account.tax.code').browse(self.cr, self.uid, tax_code_ids)
102         return tax_codes
103
104     def _sum_vat(self, period_id, journal_id, tax_code_id):
105         self.cr.execute('select sum(tax_amount) from account_move_line where ' \
106                         'period_id=%s and journal_id=%s and tax_code_id=%s and state<>\'draft\'',
107                         (period_id, journal_id, tax_code_id))
108         return self.cr.fetchone()[0] or 0.0
109
110     def _sum_debit(self, period_id=False, journal_id=False):
111         if journal_id and isinstance(journal_id, int):
112             journal_id = [journal_id]
113         if period_id and isinstance(period_id, int):
114             period_id = [period_id]
115         if not journal_id:
116             journal_id = self.journal_ids
117         if not period_id:
118             period_id = self.period_ids
119         if not (period_id and journal_id):
120             return 0.0
121         move_state = ['draft','posted']
122         if self.target_move == 'posted':
123             move_state = ['posted']
124
125         self.cr.execute('SELECT SUM(debit) FROM account_move_line l, account_move am '
126                         'WHERE l.move_id=am.id AND am.state IN %s AND l.period_id IN %s AND l.journal_id IN %s ' + self.query_get_clause + ' ',
127                         (tuple(move_state), tuple(period_id), tuple(journal_id)))
128         return self.cr.fetchone()[0] or 0.0
129
130     def _sum_credit(self, period_id=False, journal_id=False):
131         if journal_id and isinstance(journal_id, int):
132             journal_id = [journal_id]
133         if period_id and isinstance(period_id, int):
134             period_id = [period_id]
135         if not journal_id:
136             journal_id = self.journal_ids
137         if not period_id:
138             period_id = self.period_ids
139         if not (period_id and journal_id):
140             return 0.0
141         move_state = ['draft','posted']
142         if self.target_move == 'posted':
143             move_state = ['posted']
144
145         self.cr.execute('SELECT SUM(l.credit) FROM account_move_line l, account_move am '
146                         'WHERE l.move_id=am.id AND am.state IN %s AND l.period_id IN %s AND l.journal_id IN %s '+ self.query_get_clause+'',
147                         (tuple(move_state), tuple(period_id), tuple(journal_id)))
148         return self.cr.fetchone()[0] or 0.0
149
150     def lines(self, period_id, journal_id=False):
151         if not journal_id:
152             journal_id = self.journal_ids
153         else:
154             journal_id = [journal_id]
155         obj_mline = self.pool.get('account.move.line')
156         self.cr.execute('update account_journal_period set state=%s where journal_id IN %s and period_id=%s and state=%s', ('printed', self.journal_ids, period_id, 'draft'))
157
158         move_state = ['draft','posted']
159         if self.target_move == 'posted':
160             move_state = ['posted']
161
162         self.cr.execute('SELECT l.id FROM account_move_line l, account_move am WHERE l.move_id=am.id AND am.state IN %s AND l.period_id=%s AND l.journal_id IN %s ' + self.query_get_clause + ' ORDER BY '+ self.sort_selection + ', l.move_id',(tuple(move_state), period_id, tuple(journal_id) ))
163         ids = map(lambda x: x[0], self.cr.fetchall())
164         return obj_mline.browse(self.cr, self.uid, ids)
165
166     def _set_get_account_currency_code(self, account_id):
167         self.cr.execute("SELECT c.symbol AS code "\
168                 "FROM res_currency c,account_account AS ac "\
169                 "WHERE ac.id = %s AND ac.currency_id = c.id" % (account_id))
170         result = self.cr.fetchone()
171         if result:
172             self.account_currency = result[0]
173         else:
174             self.account_currency = False
175
176     def _get_fiscalyear(self, data):
177         if data['model'] == 'account.journal.period':
178             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).fiscalyear_id.name
179         return super(journal_print, self)._get_fiscalyear(data)
180
181     def _get_account(self, data):
182         if data['model'] == 'account.journal.period':
183             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).company_id.name
184         return super(journal_print, self)._get_account(data)
185
186     def _display_currency(self, data):
187         if data['model'] == 'account.journal.period':
188             return True
189         return data['form']['amount_currency']
190
191     def _get_sortby(self, data):
192         if self.sort_selection == 'date':
193             return 'Date'
194         elif self.sort_selection == 'ref':
195             return 'Reference Number'
196         return 'Date'
197
198 report_sxw.report_sxw('report.account.journal.period.print', 'account.journal.period', 'addons/account/report/account_journal.rml', parser=journal_print, header='internal')
199 report_sxw.report_sxw('report.account.journal.period.print.sale.purchase', 'account.journal.period', 'addons/account/report/account_journal_sale_purchase.rml', parser=journal_print, header='internal')
200
201 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: