[FIX]: fix a name_search for the account move
[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 osv import fields, osv
23 from tools.translate import _
24 import tools
25
26 class account_move_journal(osv.osv_memory):
27     _name = "account.move.journal"
28     _description = "Move journal"
29
30     def _get_period(self, cr, uid, context={}):
31         """
32         Return  default account period value
33         """
34         ids = self.pool.get('account.period').find(cr, uid, context=context)
35         period_id = False
36         if len(ids):
37             period_id = ids[0]
38         return period_id
39
40     def _get_journal(self, cr, uid, context={}):
41         """
42         Return journal based on the journal type
43         """
44
45         journal_id = False
46         
47         journal_pool = self.pool.get('account.journal')
48         if context.get('journal_type', False):
49             jids = journal_pool.search(cr, uid, [('type','=', context.get('journal_type'))])
50             journal_id = jids[0]
51
52         return journal_id
53         
54     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
55         """
56         Returns views and fields for current model where view will depend on {view_type}.
57         @param cr: A database cursor
58         @param user: ID of the user currently logged in
59         @param view_id: list of fields, which required to read signatures
60         @param view_type: defines a view type. it can be one of (form, tree, graph, calender, gantt, search, mdx)
61         @param context: context arguments, like lang, time zone
62         @param toolbar: contains a list of reports, wizards, and links related to current model
63         
64         @return: Returns a dict that contains definition for fields, views, and toolbars
65         """
66
67         res = super(account_move_journal, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
68
69         if not view_id:
70             return res
71
72         period_pool = self.pool.get('account.period')
73         journal_pool = self.pool.get('account.journal')
74         
75         journal_id = self._get_journal(cr, uid, context)
76         period_id = self._get_period(cr, uid, context)
77
78         journal = False
79         if journal_id:
80             journal = journal_pool.read(cr, uid, [journal_id], ['name'])[0]['name']
81         else:
82             journal = "All"
83
84         period = False
85         if period_id:
86             period = period_pool.browse(cr, uid, [period_id], ['name'])[0]['name']
87
88         view = """<?xml version="1.0" encoding="utf-8"?>
89         <form string="Standard entries">
90             <separator string="Open a Journal Items !" colspan="4"/>
91             <group colspan="4" >
92                 <label width="300" string="Journal : %s"/>
93                 <newline/>
94                 <label width="300" string="Period :  %s"/>
95             </group>
96             <group colspan="4" col="4">
97                 <label string ="" colspan="2"/>
98                 <button icon="terp-gtk-go-back-rtl" string="Ok" name="action_open_window" type="object"/>
99             </group>
100         </form>""" % (str(journal), str(period))
101         
102         res.update({
103             'arch':view
104         })
105         return res
106         
107     def action_open_window(self, cr, uid, ids, context=None):
108         """
109         This function Open action move line window on given period and  Journal/Payment Mode
110         @param cr: the current row, from the database cursor,
111         @param uid: the current user’s ID for security checks,
112         @param ids: account move journal’s ID or list of IDs
113         @return: dictionary of Open action move line window on given period and  Journal/Payment Mode
114         """
115         
116         period_pool = self.pool.get('account.journal.period')
117         data_pool = self.pool.get('ir.model.data')
118         journal_pool = self.pool.get('account.journal')
119         
120         if context is None:
121             context = {}
122         
123         journal_id = self._get_journal(cr, uid, context)
124         period_id = self._get_period(cr, uid, context)
125
126         name = _("Journal Items")
127         if journal_id:
128             ids = period_pool.search(cr, uid, [('journal_id', '=', journal_id), ('period_id', '=', period_id)], context=context)
129             
130             if not len(ids):
131                 journal = journal_pool.browse(cr, uid, journal_id)
132                 period = self.pool.get('account.period').browse(cr, uid, period_id)
133                 
134                 name = journal.name
135                 state = period.state
136                 
137                 if state == 'done':
138                     raise osv.except_osv(_('UserError'), _('This period is already closed !'))
139                 
140                 company = period.company_id.id
141                 res = {
142                     'name': name, 
143                     'period_id': period_id, 
144                     'journal_id': journal_id, 
145                     'company_id': company
146                 }
147                 period_pool.create(cr, uid, res,context=context)
148             
149             ids = period_pool.search(cr, uid, [('journal_id', '=', journal_id), ('period_id', '=', period_id)],context=context)
150             period = period_pool.browse(cr, uid, ids[0], context=context)
151             name = (period.journal_id.code or '') + ':' + (period.period_id.code or '')
152         
153         result = data_pool._get_id(cr, uid, 'account', 'view_account_move_line_filter')
154         res_id = data_pool.browse(cr, uid, result, context=context).res_id
155         
156         return {
157 #            'domain': str([('journal_id', '=', journal_id), ('period_id', '=', period_id)]),
158             'name': name,
159             'view_type': 'form',
160             'view_mode': 'tree,graph,form',
161             'res_model': 'account.move.line',
162             'view_id': False,
163             'context': "{'visible_id':%s, 'journal_id': %d, 'search_default_journal_id':%d, 'search_default_period_id':%d}" % (journal_id, journal_id, journal_id, period_id),
164             'type': 'ir.actions.act_window',
165             'search_view_id': res_id
166         }
167         
168 account_move_journal()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: