[IMP] kept periods on single line
[odoo/odoo.git] / addons / account / wizard / account_move_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 from lxml import etree
23
24 from osv import osv, fields
25 from tools.translate import _
26 import tools
27
28 class account_move_journal(osv.osv_memory):
29     _name = "account.move.journal"
30     _description = "Move journal"
31
32     _columns = {
33        'target_move': fields.selection([('posted', 'All Posted Entries'),
34                                         ('all', 'All Entries'),
35                                         ], 'Target Moves', required=True),
36     }
37
38     _defaults = {
39         'target_move': 'all'
40     }
41     def _get_period(self, cr, uid, context={}):
42         """
43         Return  default account period value
44         """
45         account_period_obj = self.pool.get('account.period')
46         ids = account_period_obj.find(cr, uid, context=context)
47         period_id = False
48         if ids:
49             period_id = ids[0]
50         return period_id
51
52     def _get_journal(self, cr, uid, context=None):
53         """
54         Return journal based on the journal type
55         """
56
57         journal_id = False
58
59         journal_pool = self.pool.get('account.journal')
60         if context.get('journal_type', False):
61             jids = journal_pool.search(cr, uid, [('type','=', context.get('journal_type'))])
62             if not jids:
63                 raise osv.except_osv(_('Configuration Error !'), _('Can\'t find any account journal of %s type for this company.\n\nYou can create one in the menu: \nConfiguration/Financial Accounting/Accounts/Journals.') % context.get('journal_type'))
64             journal_id = jids[0]
65
66         return journal_id
67
68     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
69         """
70         Returns views and fields for current model where view will depend on {view_type}.
71         @param cr: A database cursor
72         @param user: ID of the user currently logged in
73         @param view_id: list of fields, which required to read signatures
74         @param view_type: defines a view type. it can be one of (form, tree, graph, calender, gantt, search, mdx)
75         @param context: context arguments, like lang, time zone
76         @param toolbar: contains a list of reports, wizards, and links related to current model
77
78         @return: Returns a dict that contains definition for fields, views, and toolbars
79         """
80         if context is None:context = {}
81         res = super(account_move_journal, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
82
83         if context:
84             if not view_id:
85                 return res
86     
87             period_pool = self.pool.get('account.period')
88             journal_pool = self.pool.get('account.journal')
89     
90             journal_id = self._get_journal(cr, uid, context)
91             period_id = self._get_period(cr, uid, context)
92     
93             journal = False
94             if journal_id:
95                 journal = journal_pool.read(cr, uid, [journal_id], ['name'])[0]['name']
96                 journal_string = _("Journal: %s") % tools.ustr(journal)
97             else:
98                 journal_string = _("Journal: All")
99     
100             period = False
101             if period_id:
102                 period = period_pool.browse(cr, uid, [period_id], ['name'])[0]['name']
103                 period_string = _("Period: %s") % tools.ustr(period)
104     
105             separator_string = _("Open Journal Items !")
106             cancel_string = _("Cancel")
107             open_string = _("Open")
108             view = """<?xml version="1.0" encoding="utf-8"?>
109             <form string="Standard entries">
110                 <header>
111                     <button icon="terp-gtk-go-back-rtl" string="%s" name="action_open_window" default_focus="1" type="object"/>
112                     <button icon="gtk-cancel" special="cancel" string="%s"/>
113                 </header>
114                 <group string="%s" colspan="4">
115                     <field name="target_move" />
116                 </group>
117                 <group colspan="4" >
118                     <label width="300" string="%s"/>
119                     <newline/>
120                     <label width="300" string="%s"/>
121                 </group>
122             </form>""" % (open_string, cancel_string, separator_string, journal_string, period_string)
123     
124             view = etree.fromstring(view.encode('utf8'))
125             xarch, xfields = self._view_look_dom_arch(cr, uid, view, view_id, context=context)
126             view = xarch
127             res.update({
128                 'arch': view
129             })
130         return res
131
132     def action_open_window(self, cr, uid, ids, context=None):
133         """
134         This function Open action move line window on given period and  Journal/Payment Mode
135         @param cr: the current row, from the database cursor,
136         @param uid: the current user’s ID for security checks,
137         @param ids: account move journal’s ID or list of IDs
138         @return: dictionary of Open action move line window on given period and  Journal/Payment Mode
139         """
140
141         period_pool = self.pool.get('account.journal.period')
142         data_pool = self.pool.get('ir.model.data')
143         journal_pool = self.pool.get('account.journal')
144         account_period_obj = self.pool.get('account.period')
145
146         if context is None:
147             context = {}
148
149         journal_id = self._get_journal(cr, uid, context)
150         period_id = self._get_period(cr, uid, context)
151         target_move = self.read(cr, uid, ids, ['target_move'], context=context)[0]['target_move']
152
153         name = _("Journal Items")
154         if journal_id:
155             ids = period_pool.search(cr, uid, [('journal_id', '=', journal_id), ('period_id', '=', period_id)], context=context)
156
157             if not ids:
158                 journal = journal_pool.browse(cr, uid, journal_id, context=context)
159                 period = account_period_obj.browse(cr, uid, period_id, context=context)
160
161                 name = journal.name
162                 state = period.state
163
164                 if state == 'done':
165                     raise osv.except_osv(_('UserError'), _('This period is already closed !'))
166
167                 company = period.company_id.id
168                 res = {
169                     'name': name,
170                     'period_id': period_id,
171                     'journal_id': journal_id,
172                     'company_id': company
173                 }
174                 period_pool.create(cr, uid, res,context=context)
175
176             ids = period_pool.search(cr, uid, [('journal_id', '=', journal_id), ('period_id', '=', period_id)], context=context)
177             period = period_pool.browse(cr, uid, ids[0], context=context)
178             name = (period.journal_id.code or '') + ':' + (period.period_id.code or '')
179
180         result = data_pool.get_object_reference(cr, uid, 'account', 'view_account_move_line_filter')
181         res_id = result and result[1] or False
182         move = 0
183         if target_move == 'posted':
184             move = 1
185         return {
186             'name': name,
187             'view_type': 'form',
188             'view_mode': 'tree,graph,form',
189             'res_model': 'account.move.line',
190             'view_id': False,
191             'context': "{'search_default_posted': %d, 'search_default_journal_id':%d, 'search_default_period_id':%d}" % (move, journal_id, period_id),
192             'type': 'ir.actions.act_window',
193             'search_view_id': res_id
194         }
195
196 account_move_journal()
197
198 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: