bfe59251f45403d1f4363f117b2be82976e18915
[odoo/odoo.git] / addons / point_of_sale / wizard / wizard_default_journal.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import wizard
24 import pooler
25
26
27 def _get_default_journal_selection(self, cr, uid, context):
28     pool = pooler.get_pool(cr.dbname)
29     obj = pool.get('account.journal')
30     ids = obj.search(cr, uid, [('type', '=', 'cash')])
31     res = obj.read(cr, uid, ids, ['id', 'name'], context)
32     res = [(r['id'], r['name']) for r in res]
33     res.insert(0, ('', ''))
34     return res
35
36 default_journal_form = '''<?xml version="1.0"?>
37 <form string="Select default journals">
38     <field name="default_journal" />
39     <newline />
40     <field name="default_journal_rebate" />
41     <newline />
42     <field name="default_journal_gift" />
43     <newline />
44 </form>'''
45
46 default_journal_fields = {
47     'default_journal': {'string': 'Default journal', 'type': 'selection',
48         'selection': _get_default_journal_selection,
49     },
50     'default_journal_rebate': {'string': 'Default rebate journal', 'type': 'selection',
51         'selection': _get_default_journal_selection,
52     },
53     'default_journal_gift': {'string': 'Default gift journal', 'type': 'selection',
54         'selection': _get_default_journal_selection,
55     },
56 }
57
58
59 class wizard_default_journal(wizard.interface):
60
61     def _set_default_journal(self, cr, uid, data, context):
62
63         def _update_default_journal_config(journal_type, journal_code, journal_descr, journal_codes, data):
64             default_journal_id = data.get('form', {}).get(journal_type) or None
65             dico = dict(name=journal_descr, code=journal_code, journal_id=default_journal_id)
66             if default_journal_id:
67                 if journal_code in journal_codes:
68                     ids = [obj.id for obj in objs if obj.code == journal_code]
69                     pos_config_journal.write(cr, uid, ids, dico, context)
70                 else:
71                     pos_config_journal.create(cr, uid, dico, context)
72             else:
73                 ids = [obj.id for obj in objs if obj.code == journal_code]
74                 pos_config_journal.write(cr, uid, ids, dico, context)
75
76         pool = pooler.get_pool(cr.dbname)
77         pos_config_journal = pool.get('pos.config.journal')
78         ids = pos_config_journal.search(cr, uid, [])
79         objs = pos_config_journal.browse(cr, uid, ids)
80         journal_codes = [str(obj.code) for obj in objs]
81
82         _update_default_journal_config('default_journal', 'DEFAULT', 'Default journal', journal_codes, data)
83         _update_default_journal_config('default_journal_rebate', 'REBATE', 'Default rebate journal', journal_codes, data)
84         _update_default_journal_config('default_journal_gift', 'GIFT', 'Default gift journal', journal_codes, data)
85
86         return {}
87
88     def _get_defaults(self, cr, uid, data, context):
89         pool = pooler.get_pool(cr.dbname)
90         pos_config_journal = pool.get('pos.config.journal')
91         ids = pos_config_journal.search(cr, uid, [])
92         objs = pos_config_journal.browse(cr, uid, ids)
93         journal_codes = {}
94         for obj in objs:
95             journal_codes[obj.code] = int(obj.journal_id.id)
96
97         form = data['form']
98         form['default_journal'] = journal_codes.get('DEFAULT') or False
99         form['default_journal_rebate'] = journal_codes.get('REBATE') or False
100         form['default_journal_gift'] = journal_codes.get('GIFT') or False
101
102         return form
103
104     states = {
105         'init': {
106             'actions': [_get_defaults],
107             'result': {
108                 'type': 'form',
109                 'arch': default_journal_form,
110                 'fields': default_journal_fields,
111                 'state': [
112                     ('end', 'Cancel'),
113                     ('set_default_journal', 'Define default journals')
114                 ]
115             }
116         },
117         'set_default_journal': {
118             'actions': [_set_default_journal],
119             'result': {
120                 'type': 'state',
121                 'state': "end",
122             }
123         },
124     }
125
126 wizard_default_journal('pos.config.journal')
127
128 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: