[IMP] crm_parner_assign, crm: Corrected history[whole, laetst, case info] for lead...
[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.journal_ids = []
34         self.sort_selection = 'date'
35         self.localcontext.update({
36             'time': time,
37             'lines': self.lines,
38             'sum_debit': self._sum_debit,
39             'sum_credit': self._sum_credit,
40             'get_start_period': self.get_start_period,
41             'get_end_period': self.get_end_period,
42             'get_account': self._get_account,
43             'get_filter': self._get_filter,
44             'get_start_date': self._get_start_date,
45             'get_end_date': self._get_end_date,
46             'get_fiscalyear': self._get_fiscalyear,
47             'display_currency':self._display_currency,
48             'get_sortby': self._get_sortby,
49             'get_target_move': self._get_target_move,
50     })
51
52     def set_context(self, objects, data, ids, report_type=None):
53         obj_move = self.pool.get('account.move.line')
54         new_ids = ids
55         self.query_get_clause = ''
56         self.target_move = data['form'].get('target_move', 'all')
57         if (data['model'] == 'ir.ui.menu'):
58             new_ids = data['form'].get('active_ids', [])
59             self.query_get_clause = 'AND '
60             self.query_get_clause += obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
61             self.sort_selection = data['form'].get('sort_selection', 'date')
62             objects = self.pool.get('account.journal.period').browse(self.cr, self.uid, new_ids)
63         if new_ids:
64             self.cr.execute('SELECT period_id, journal_id FROM account_journal_period WHERE id IN %s', (tuple(new_ids),))
65             res = self.cr.fetchall()
66             self.period_ids, self.journal_ids = zip(*res)
67         return super(journal_print, self).set_context(objects, data, ids, report_type=report_type)
68
69     def _sum_debit(self, period_id=False, journal_id=False):
70         if journal_id and isinstance(journal_id, int):
71             journal_id = [journal_id]
72         if period_id and isinstance(period_id, int):
73             period_id = [period_id]
74         if not journal_id:
75             journal_id = self.journal_ids
76         if not period_id:
77             period_id = self.period_ids
78         if not (period_id and journal_id):
79             return 0.0
80         move_state = ['draft','posted']
81         if self.target_move == 'posted':
82             move_state = ['posted']
83
84         self.cr.execute('SELECT SUM(debit) FROM account_move_line l, account_move am '
85                         '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 + ' ',
86                         (tuple(move_state), tuple(period_id), tuple(journal_id)))
87         return self.cr.fetchone()[0] or 0.0
88
89     def _sum_credit(self, period_id=False, journal_id=False):
90         if journal_id and isinstance(journal_id, int):
91             journal_id = [journal_id]
92         if period_id and isinstance(period_id, int):
93             period_id = [period_id]
94         if not journal_id:
95             journal_id = self.journal_ids
96         if not period_id:
97             period_id = self.period_ids
98         if not (period_id and journal_id):
99             return 0.0
100         move_state = ['draft','posted']
101         if self.target_move == 'posted':
102             move_state = ['posted']
103
104         self.cr.execute('SELECT SUM(l.credit) FROM account_move_line l, account_move am '
105                         '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+'',
106                         (tuple(move_state), tuple(period_id), tuple(journal_id)))
107         return self.cr.fetchone()[0] or 0.0
108
109     def lines(self, period_id, journal_id=False):
110         if not journal_id:
111             journal_id = self.journal_ids
112         else:
113             journal_id = [journal_id]
114         obj_mline = self.pool.get('account.move.line')
115         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'))
116
117         move_state = ['draft','posted']
118         if self.target_move == 'posted':
119             move_state = ['posted']
120
121         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 l.'+ self.sort_selection + ', l.move_id',(tuple(move_state), period_id, tuple(journal_id) ))
122         ids = map(lambda x: x[0], self.cr.fetchall())
123         return obj_mline.browse(self.cr, self.uid, ids)
124
125     def _set_get_account_currency_code(self, account_id):
126         self.cr.execute("SELECT c.symbol AS code "\
127                 "FROM res_currency c,account_account AS ac "\
128                 "WHERE ac.id = %s AND ac.currency_id = c.id" % (account_id))
129         result = self.cr.fetchone()
130         if result:
131             self.account_currency = result[0]
132         else:
133             self.account_currency = False
134
135     def _get_fiscalyear(self, data):
136         if data['model'] == 'account.journal.period':
137             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).fiscalyear_id.name
138         return super(journal_print, self)._get_fiscalyear(data)
139
140     def _get_account(self, data):
141         if data['model'] == 'account.journal.period':
142             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).company_id.name
143         return super(journal_print, self)._get_account(data)
144
145     def _display_currency(self, data):
146         if data['model'] == 'account.journal.period':
147             return True
148         return data['form']['amount_currency']
149
150     def _get_sortby(self, data):
151         if self.sort_selection == 'date':
152             return 'Date'
153         elif self.sort_selection == 'ref':
154             return 'Reference Number'
155         return 'Date'
156
157 report_sxw.report_sxw('report.account.journal.period.print', 'account.journal.period', 'addons/account/report/account_journal.rml', parser=journal_print, header='internal')
158
159 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: