[IMP]account:string chaged from credit note to refund
[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=None):
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!'), _('Cannot find any account journal of %s type for this company.\n\nYou can create one in the menu: \nConfiguration/Journals/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'], context=context).get('name',False)
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, context=context).name
103                 period_string = _("Period: %s") % tools.ustr(period)
104
105             open_string = _("Open")
106             view = """<?xml version="1.0" encoding="utf-8"?>
107             <form string="Standard entries" version="7.0">
108                 <group>
109                     <field name="target_move"/>
110                 </group>
111                 %s: <label string="%s"/>
112                 %s: <label string="%s"/>
113                 <footer>
114                     <button string="%s" name="action_open_window" default_focus="1" type="object" class="oe_highlight"/>
115                     or
116                     <button string="Cancel" class="oe_link" special="cancel"/>
117                 </footer>
118             </form>""" % (_('Journal'), journal_string, _('Period'), period_string, open_string)
119
120             view = etree.fromstring(view.encode('utf8'))
121             xarch, xfields = self._view_look_dom_arch(cr, uid, view, view_id, context=context)
122             view = xarch
123             res.update({
124                 'arch': view
125             })
126         return res
127
128     def action_open_window(self, cr, uid, ids, context=None):
129         """
130         This function Open action move line window on given period and  Journal/Payment Mode
131         @param cr: the current row, from the database cursor,
132         @param uid: the current user’s ID for security checks,
133         @param ids: account move journal’s ID or list of IDs
134         @return: dictionary of Open action move line window on given period and  Journal/Payment Mode
135         """
136
137         period_pool = self.pool.get('account.journal.period')
138         data_pool = self.pool.get('ir.model.data')
139         journal_pool = self.pool.get('account.journal')
140         account_period_obj = self.pool.get('account.period')
141
142         if context is None:
143             context = {}
144
145         journal_id = self._get_journal(cr, uid, context)
146         period_id = self._get_period(cr, uid, context)
147         target_move = self.read(cr, uid, ids, ['target_move'], context=context)[0]['target_move']
148
149         name = _("Journal Items")
150         if journal_id:
151             ids = period_pool.search(cr, uid, [('journal_id', '=', journal_id), ('period_id', '=', period_id)], context=context)
152
153             if not ids:
154                 journal = journal_pool.browse(cr, uid, journal_id, context=context)
155                 period = account_period_obj.browse(cr, uid, period_id, context=context)
156
157                 name = journal.name
158                 state = period.state
159
160                 if state == 'done':
161                     raise osv.except_osv(_('User Error!'), _('This period is already closed.'))
162
163                 company = period.company_id.id
164                 res = {
165                     'name': name,
166                     'period_id': period_id,
167                     'journal_id': journal_id,
168                     'company_id': company
169                 }
170                 period_pool.create(cr, uid, res,context=context)
171
172             ids = period_pool.search(cr, uid, [('journal_id', '=', journal_id), ('period_id', '=', period_id)], context=context)
173             period = period_pool.browse(cr, uid, ids[0], context=context)
174             name = (period.journal_id.code or '') + ':' + (period.period_id.code or '')
175
176         result = data_pool.get_object_reference(cr, uid, 'account', 'view_account_move_line_filter')
177         res_id = result and result[1] or False
178         move = 0
179         if target_move == 'posted':
180             move = 1
181         return {
182             'name': name,
183             'view_type': 'form',
184             'view_mode': 'tree,graph,form',
185             'res_model': 'account.move.line',
186             'view_id': False,
187             'context': "{'search_default_posted': %d, 'search_default_journal_id':%d, 'search_default_period_id':%d}" % (move, journal_id, period_id),
188             'type': 'ir.actions.act_window',
189             'search_view_id': res_id
190         }
191
192 account_move_journal()
193
194 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: