[MERGE] merge with main branch
[odoo/odoo.git] / addons / account / report / account_central_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 report import report_sxw
24 from common_report_header import common_report_header
25 #
26 # Use period and Journal for selection or resources
27 #
28 class journal_print(report_sxw.rml_parse, common_report_header):
29
30     def __init__(self, cr, uid, name, context=None):
31         if context is None:
32             context = {}
33         super(journal_print, self).__init__(cr, uid, name, context=context)
34         self.period_ids = []
35         self.journal_ids = []
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_filter': self._get_filter,
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_start_date':self._get_start_date,
48             'get_end_date':self._get_end_date,
49             'display_currency':self._display_currency,
50             'get_target_move': self._get_target_move,
51         })
52
53     def set_context(self, objects, data, ids, report_type=None):
54         obj_move = self.pool.get('account.move.line')
55         new_ids = ids
56         self.query_get_clause = ''
57         self.target_move = data['form'].get('target_move', 'all')
58         if (data['model'] == 'ir.ui.menu'):
59             new_ids = 'active_ids' in data['form'] and data['form']['active_ids'] or []
60             self.query_get_clause = 'AND '
61             self.query_get_clause += obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
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 lines(self, period_id, journal_id):
70         move_state = ['draft','posted']
71         if self.target_move == 'posted':
72             move_state = ['posted']
73
74         self.cr.execute('SELECT a.currency_id, a.code, a.name, c.symbol AS currency_code, l.currency_id, l.amount_currency, SUM(debit) AS debit, SUM(credit) AS credit  \
75                         from account_move_line l  \
76                         LEFT JOIN account_move am ON (l.move_id=am.id) \
77                         LEFT JOIN account_account a ON (l.account_id=a.id) \
78                         LEFT JOIN res_currency c on (l.currency_id=c.id) WHERE am.state IN %s AND l.period_id=%s AND l.journal_id=%s '+self.query_get_clause+' GROUP BY a.id, a.code, a.name,l.amount_currency,c.symbol, a.currency_id,l.currency_id', (tuple(move_state), period_id, journal_id))
79         return self.cr.dictfetchall()
80
81     def _set_get_account_currency_code(self, account_id):
82         self.cr.execute("SELECT c.symbol as code "\
83                 "FROM res_currency c,account_account as ac "\
84                 "WHERE ac.id = %s AND ac.currency_id = c.id"%(account_id))
85         result = self.cr.fetchone()
86         if result:
87             self.account_currency = result[0]
88         else:
89             self.account_currency = False
90
91     def _get_account(self, data):
92         if data['model'] == 'account.journal.period':
93             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).company_id.name
94         return super(journal_print,self)._get_account(data)
95
96     def _get_fiscalyear(self, data):
97         if data['model'] == 'account.journal.period':
98             return self.pool.get('account.journal.period').browse(self.cr, self.uid, data['id']).fiscalyear_id.name
99         return super(journal_print,self)._get_fiscalyear(data)
100
101     def _display_currency(self, data):
102         if data['model'] == 'account.journal.period':
103             return True
104         return data['form']['amount_currency']
105
106 report_sxw.report_sxw('report.account.central.journal', 'account.journal.period', 'addons/account/report/account_central_journal.rml', parser=journal_print, header='internal')
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: