[MERGE] forward port of branch 7.0 up to de07c64
[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 openerp.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 _get_fiscalyear(self, cr, uid, context=None):
42         """Return default Fiscalyear value"""
43         return self.pool.get('account.fiscalyear').find(cr, uid, context=context)
44
45     def onchange_fiscalyear(self, cr, uid, ids, fiscalyear_id=False, context=None):
46         res = {}
47         if fiscalyear_id:
48             start_period = end_period = False
49             cr.execute('''
50                 SELECT * FROM (SELECT p.id
51                                FROM account_period p
52                                LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
53                                WHERE f.id = %s
54                                ORDER BY p.date_start ASC
55                                LIMIT 1) AS period_start
56                 UNION ALL
57                 SELECT * FROM (SELECT p.id
58                                FROM account_period p
59                                LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
60                                WHERE f.id = %s
61                                AND p.date_start < NOW()
62                                ORDER BY p.date_stop DESC
63                                LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
64             periods =  [i[0] for i in cr.fetchall()]
65             if periods and len(periods) > 1:
66                 start_period = periods[0]
67                 end_period = periods[1]
68             res['value'] = {'period_from': start_period, 'period_to': end_period}
69         else:
70             res['value'] = {'period_from': False, 'period_to': False}
71         return res
72
73     def account_chart_open_window(self, cr, uid, ids, context=None):
74         """
75         Opens chart of Accounts
76         @param cr: the current row, from the database cursor,
77         @param uid: the current user’s ID for security checks,
78         @param ids: List of account chart’s IDs
79         @return: dictionary of Open account chart window on given fiscalyear and all Entries or posted entries
80         """
81         mod_obj = self.pool.get('ir.model.data')
82         act_obj = self.pool.get('ir.actions.act_window')
83         period_obj = self.pool.get('account.period')
84         fy_obj = self.pool.get('account.fiscalyear')
85         if context is None:
86             context = {}
87         data = self.read(cr, uid, ids, [], context=context)[0]
88         result = mod_obj.get_object_reference(cr, uid, 'account', 'action_account_tree')
89         id = result and result[1] or False
90         result = act_obj.read(cr, uid, [id], context=context)[0]
91         fiscalyear_id = data.get('fiscalyear', False) and data['fiscalyear'][0] or False
92         result['periods'] = []
93         if data['period_from'] and data['period_to']:
94             period_from = data.get('period_from', False) and data['period_from'][0] or False
95             period_to = data.get('period_to', False) and data['period_to'][0] or False
96             result['periods'] = period_obj.build_ctx_periods(cr, uid, period_from, period_to)
97         result['context'] = str({'fiscalyear': fiscalyear_id, 'periods': result['periods'], \
98                                     'state': data['target_move']})
99         if fiscalyear_id:
100             result['name'] += ':' + fy_obj.read(cr, uid, [fiscalyear_id], context=context)[0]['code']
101         return result
102
103     _defaults = {
104         'target_move': 'posted',
105         'fiscalyear': _get_fiscalyear,
106     }
107
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: