[REF] account
[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
24 from report import report_sxw
25 from common_report_header import common_report_header
26 #
27 # Use period and Journal for selection or resources
28 #
29 class journal_print(report_sxw.rml_parse, common_report_header):
30
31     def __init__(self, cr, uid, name, context=None):
32         if context is None:
33             context = {}
34         super(journal_print, self).__init__(cr, uid, name, context=context)
35         self.period_ids = []
36         self.journal_ids = []
37         self.localcontext.update({
38             'time': time,
39             'lines': self.lines,
40             'sum_debit': self._sum_debit,
41             'sum_credit': self._sum_credit,
42             'get_filter': self._get_filter,
43             'get_fiscalyear': self._get_fiscalyear,
44             'get_account': self._get_account,
45             'get_start_period': self.get_start_period,
46             'get_end_period': self.get_end_period,
47             'get_sortby': self._get_sortby,
48             'get_start_date':self._get_start_date,
49             'get_end_date':self._get_end_date,
50             'display_currency':self._display_currency,
51             'get_target_move': self._get_target_move,
52         })
53
54     def set_context(self, objects, data, ids, report_type=None):
55         obj_move = self.pool.get('account.move.line')
56         new_ids = ids
57         self.query_get_clause = ''
58         self.target_move = data['form'].get('target_move', 'all')
59         if (data['model'] == 'ir.ui.menu'):
60             new_ids = 'active_ids' in data['form'] and data['form']['active_ids'] or []
61             self.query_get_clause = 'AND '
62             self.query_get_clause += obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
63             objects = self.pool.get('account.journal.period').browse(self.cr, self.uid, new_ids)
64         if new_ids:
65             self.cr.execute('SELECT period_id, journal_id FROM account_journal_period WHERE id IN %s', (tuple(new_ids),))
66             res = self.cr.fetchall()
67             self.period_ids, self.journal_ids = zip(*res)
68         return super(journal_print, self).set_context(objects, data, ids, report_type=report_type)
69
70     def lines(self, period_id, journal_id):
71         move_state = ['draft','posted']
72         if self.target_move == 'posted':
73             move_state = ['posted']
74         self.cr.execute('SELECT a.currency_id ,a.code, a.name, c.code 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.code , 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.code 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: