e8f0e432d69db5b49fe5d74bfc159a24065b078b
[odoo/odoo.git] / addons / account / wizard / account_chart.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 from osv import fields, osv
23
24 class account_chart(osv.osv_memory):
25     """
26     For Chart of Accounts
27     """
28     _name = "account.chart"
29     _description = "Account chart"
30     _columns = {
31         'fiscalyear': fields.many2one('account.fiscalyear', \
32                                     'Fiscal year',  \
33                                     help = 'Keep empty for all open fiscal years'),
34         'period_from': fields.many2one('account.period', 'Start period'),
35         'period_to': fields.many2one('account.period', 'End period'),
36         'target_move': fields.selection([('posted', 'All Posted Entries'),
37                                          ('all', 'All Entries'),
38                                         ], 'Target Moves', required = True),
39     }
40
41     def onchange_fiscalyear(self, cr, uid, ids, fiscalyear_id=False, context=None):
42         res = {}
43         res['value'] = {}
44         if fiscalyear_id:
45             start_period = end_period = False
46             cr.execute('''
47                 SELECT * FROM (SELECT p.id
48                                FROM account_period p
49                                LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
50                                WHERE f.id = %s
51                                ORDER BY p.date_start ASC
52                                LIMIT 1) AS period_start
53                 UNION
54                 SELECT * FROM (SELECT p.id
55                                FROM account_period p
56                                LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
57                                WHERE f.id = %s
58                                AND p.date_start < NOW()
59                                ORDER BY p.date_stop DESC
60                                LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
61             periods =  [i[0] for i in cr.fetchall()]
62             if periods and len(periods) > 1:
63                 start_period = periods[0]
64                 end_period = periods[1]
65             res['value'] = {'period_from': start_period, 'period_to': end_period}
66         return res
67
68     def account_chart_open_window(self, cr, uid, ids, context=None):
69         """
70         Opens chart of Accounts
71         @param cr: the current row, from the database cursor,
72         @param uid: the current user’s ID for security checks,
73         @param ids: List of account chart’s IDs
74         @return: dictionary of Open account chart window on given fiscalyear and all Entries or posted entries
75         """
76         mod_obj = self.pool.get('ir.model.data')
77         act_obj = self.pool.get('ir.actions.act_window')
78         period_obj = self.pool.get('account.period')
79         fy_obj = self.pool.get('account.fiscalyear')
80         if context is None:
81             context = {}
82         data = self.read(cr, uid, ids, [], context=context)[0]
83         result = mod_obj.get_object_reference(cr, uid, 'account', 'action_account_tree')
84         id = result and result[1] or False
85         result = act_obj.read(cr, uid, [id], context=context)[0]
86         result['periods'] = []
87         if data['period_from'] and data['period_to']:
88             result['periods'] = period_obj.build_ctx_periods(cr, uid, data['period_from'], data['period_to'])
89         result['context'] = str({'fiscalyear': data['fiscalyear'], 'periods': result['periods'], \
90                                     'state': data['target_move']})
91         if data['fiscalyear']:
92             result['name'] += ':' + fy_obj.read(cr, uid, [data['fiscalyear']], context=context)[0]['code']
93         return result
94
95     _defaults = {
96         'target_move': 'posted'
97     }
98
99 account_chart()
100
101 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: