Imrpovement in wizards
[odoo/odoo.git] / addons / base_setup / wizard / wizard_base_setup.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 import wizard
31 import pooler
32 import time
33 import tools
34 import os
35
36 view_form_profit = """<?xml version="1.0"?>
37 <form string="Setup">
38         <image name="gtk-dialog-info"/>
39         <group>
40                 <separator string="Select a profile" colspan="2"/>
41                 <newline/>
42                 <field align="0.0" name="profile"/>
43                 <newline/>
44                 <label string="A profile sets a pre-selection of modules for enterprise needs." colspan="2" align="0.0"/>
45                 <newline/>
46                 <label string="You'll be able to install others modules later through the Administration menu." colspan="2" align="0.0"/>
47         </group>
48 </form>"""
49
50 view_form_charts = """<?xml version="1.0"?>
51 <form string="Setup">
52         <image name="gtk-dialog-info" colspan="2"/>
53         <group>
54                 <separator string="Select a chart of accounts" colspan="2"/>
55                 <newline/>
56                 <field name="charts" align="0.0"/>
57                 <newline/>
58                 <label string="There are many other chart of accounts available on the OpenERP website." colspan="2" align="0.0"/>
59                 <newline/>
60                 <label string="If you don't select one now, you'll be able to install an other one through the Administration menu." colspan="2" align="0.0"/>
61         </group>
62 </form>"""
63
64 view_form_company = """<?xml version="1.0"?>
65 <form string="Setup">
66         <image name="gtk-dialog-info" colspan="2"/>
67         <group>
68                 <separator string="Define main company" colspan="4"/>
69                 <newline/>
70                 <field name="name" align="0.0" colspan="4" required="True"/>
71                 <newline/>
72                 <field name="street" align="0.0"/>
73                 <field name="street2" align="0.0"/>
74                 <field name="zip" align="0.0"/>
75                 <field name="city" align="0.0"/>
76                 <field name="country_id" align="0.0"/>
77                 <field name="state_id" align="0.0"/>
78                 <field name="email" align="0.0"/>
79                 <field name="phone" align="0.0"/>
80                 <field name="currency" align="0.0"/>
81                 <separator string="Report header" colspan="4"/>
82                 <newline/>
83                 <field name="rml_header1" align="0.0" colspan="4"/>
84                 <field name="rml_footer1" align="0.0" colspan="4"/>
85                 <field name="rml_footer2" align="0.0" colspan="4"/>
86         </group>
87 </form>"""
88
89 view_form_update = """<?xml version="1.0"?>
90 <form string="Setup">
91         <image name="gtk-dialog-info" colspan="2"/>
92         <group>
93                 <separator string="Summary" colspan="2"/>
94                 <newline/>
95                 <field name="profile" align="0.0" readonly="1"/>
96                 <newline/>
97                 <field name="charts" align="0.0" readonly="1"/>
98                 <newline/>
99                 <field name="name" align="0.0" readonly="1"/>
100         </group>
101 </form>
102 """
103
104 view_form_finish = """<?xml version="1.0"?>
105 <form string="Setup">
106         <image name="gtk-dialog-info" colspan="2"/>
107         <group colspan="2" col="4">
108                 <separator colspan="4" string="Installation Done"/>
109                 <label align="0.0" colspan="4" string="Your new database is now fully installed."/>
110                 <label align="0.0" colspan="4" string="You can start configuring the system or connect directly to the database using the default setup."/>
111         </group>
112 </form>
113 """
114
115 class wizard_base_setup(wizard.interface):
116
117         def _get_profiles(self, cr, uid, context):
118                 module_obj=pooler.get_pool(cr.dbname).get('ir.module.module')
119                 ids=module_obj.search(cr, uid, [('category_id', '=', 'Profile'),
120                         ('state', '<>', 'uninstallable')])
121                 res=[(m.id, m.shortdesc) for m in module_obj.browse(cr, uid, ids)]
122                 res.append((-1, 'Minimal Profile'))
123                 res.sort()
124                 return res
125
126         def _get_charts(self, cr, uid, context):
127                 module_obj=pooler.get_pool(cr.dbname).get('ir.module.module')
128                 ids=module_obj.search(cr, uid, [('category_id', '=', 'Account charts'),
129                         ('state', '<>', 'uninstallable')])
130                 res=[(m.id, m.shortdesc) for m in module_obj.browse(cr, uid, ids)]
131                 res.append((-1, 'None'))
132                 res.sort(lambda x,y: cmp(x[1],y[1]))
133                 return res
134
135         def _get_company(self, cr, uid, data, context):
136                 pool=pooler.get_pool(cr.dbname)
137                 company_obj=pool.get('res.company')
138                 ids=company_obj.search(cr, uid, [])
139                 if not ids:
140                         return {}
141                 company=company_obj.browse(cr, uid, ids)[0]
142                 self.fields['name']['default']=company.name
143                 self.fields['currency']['default']=company.currency_id.id
144                 return {}
145                 #self.fields['rml_header1']['default']=company.rml_header1
146                 #self.fields['rml_footer1']['default']=company.rml_footer1
147                 #self.fields['rml_footer2']['default']=company.rml_footer2
148                 #if not company.partner_id.address:
149                 #       return {}
150                 #address=company.partner_id.address[0]
151                 #self.fields['street']['default']=address.street
152                 #self.fields['street2']['default']=address.street2
153                 #self.fields['zip']['default']=address.zip
154                 #self.fields['city']['default']=address.city
155                 #self.fields['email']['default']=address.email
156                 #self.fields['phone']['default']=address.phone
157                 #if address.state_id:
158                 #       self.fields['state_id']['default']=address.state_id.id
159                 #else:
160                 #       self.fields['state_id']['default']=-1
161                 #if address.country_id:
162                 #       self.fields['country_id']['default']=address.country_id.id
163                 #else:
164                 #       self.fields['country_id']['default']=-1
165                 #return {}
166
167         def _get_states(self, cr, uid, context):
168                 pool=pooler.get_pool(cr.dbname)
169                 state_obj=pool.get('res.country.state')
170                 ids=state_obj.search(cr, uid, [])
171                 res=[(state.id, state.name) for state in state_obj.browse(cr, uid, ids)]
172                 res.append((-1, ''))
173                 res.sort(lambda x,y: cmp(x[1],y[1]))
174                 return res
175
176         def _get_countries(self, cr, uid, context):
177                 pool=pooler.get_pool(cr.dbname)
178                 country_obj=pool.get('res.country')
179                 ids=country_obj.search(cr, uid, [])
180                 res=[(country.id, country.name) for country in country_obj.browse(cr, uid, ids)]
181                 res.sort(lambda x,y: cmp(x[1],y[1]))
182                 return res
183
184         def _update(self, cr, uid, data, context):
185                 pool=pooler.get_pool(cr.dbname)
186                 form=data['form']
187                 if 'profile' in data['form'] and data['form']['profile'] > 0:
188                         module_obj=pool.get('ir.module.module')
189                         module_obj.state_change(cr, uid, [data['form']['profile']], 'to install', context)
190                 if 'charts' in data['form'] and data['form']['charts'] > 0:
191                         module_obj=pool.get('ir.module.module')
192                         module_obj.state_change(cr, uid, [data['form']['charts']], 'to install', context)
193
194                 company_obj=pool.get('res.company')
195                 partner_obj=pool.get('res.partner')
196                 address_obj=pool.get('res.partner.address')
197                 ids=company_obj.search(cr, uid, [])
198                 company=company_obj.browse(cr, uid, ids)[0]
199                 company_obj.write(cr, uid, [company.id], {
200                                 'name': form['name'],
201                                 'rml_header1': form['rml_header1'],
202                                 'rml_footer1': form['rml_footer1'],
203                                 'rml_footer2': form['rml_footer2'],
204                                 'currency_id': form['currency'],
205                         })
206                 partner_obj.write(cr, uid, [company.partner_id.id], {
207                                 'name': form['name'],
208                         })
209                 values={
210                                         'name': form['name'],
211                                         'street': form['street'],
212                                         'street2': form['street2'],
213                                         'zip': form['zip'],
214                                         'city': form['city'],
215                                         'email': form['email'],
216                                         'phone': form['phone'],
217                                         'country_id': form['country_id'],
218                                 }
219                 if form['state_id'] > 0:
220                         values['state_id']=form['state_id']
221                 if company.partner_id.address:
222                         address=company.partner_id.address[0]
223                         address_obj.write(cr, uid, [address.id], values)
224                 else:
225                         values['partner_id']=company.partner_id.id
226                         add_id=address_obj.create(cr, uid, values)
227
228                 cr.commit()
229                 (db, pool)=pooler.restart_pool(cr.dbname, update_module=True)
230
231                 lang_obj=pool.get('res.lang')
232                 lang_ids=lang_obj.search(cr, uid, [])
233                 langs=lang_obj.browse(cr, uid, lang_ids)
234                 for lang in langs:
235                         if lang.code and lang.code != 'en_US':
236                                 filename=os.path.join(tools.config["root_path"], "i18n", lang.code + ".csv")
237                                 tools.trans_load(cr.dbname, filename, lang.code)
238                 return {}
239
240         def _menu(self, cr, uid, data, context):
241                 users_obj=pooler.get_pool(cr.dbname).get('res.users')
242                 action_obj=pooler.get_pool(cr.dbname).get('ir.actions.act_window')
243
244                 ids=action_obj.search(cr, uid, [('name', '=', 'Menu')])
245                 menu=action_obj.browse(cr, uid, ids)[0]
246
247                 ids=users_obj.search(cr, uid, [('action_id', '=', 'Setup')])
248                 users_obj.write(cr, uid, ids, {'action_id': menu.id})
249                 ids=users_obj.search(cr, uid, [('menu_id', '=', 'Setup')])
250                 users_obj.write(cr, uid, ids, {'menu_id': menu.id})
251
252                 return {
253                         'name': menu.name,
254                         'type': menu.type,
255                         'view_id': (menu.view_id and\
256                                         (menu.view_id.id, menu.view_id.name)) or False,
257                         'domain': menu.domain,
258                         'res_model': menu.res_model,
259                         'src_model': menu.src_model,
260                         'view_type': menu.view_type,
261                         'view_mode': menu.view_mode,
262                         'views': menu.views,
263                 }
264
265         def _next(self, cr, uid, data, context):
266                 if not data['form']['profile'] or data['form']['profile'] <= 0:
267                         return 'company'
268                 return 'charts'
269
270         def _previous(self, cr, uid, data, context):
271                 if 'profile' not in data['form'] or data['form']['profile'] <= 0:
272                         return 'init'
273                 return 'charts'
274
275         def _config(self, cr, uid, data, context=None):
276                 users_obj=pooler.get_pool(cr.dbname).get('res.users')
277                 action_obj=pooler.get_pool(cr.dbname).get('ir.actions.act_window')
278
279                 ids=action_obj.search(cr, uid, [('name', '=', 'Menu')])
280                 menu=action_obj.browse(cr, uid, ids)[0]
281
282                 ids=users_obj.search(cr, uid, [('action_id', '=', 'Setup')])
283                 users_obj.write(cr, uid, ids, {'action_id': menu.id})
284                 ids=users_obj.search(cr, uid, [('menu_id', '=', 'Setup')])
285                 users_obj.write(cr, uid, ids, {'menu_id': menu.id})
286                 return {
287                 'view_type': 'form',
288                 "view_mode": 'form',
289                                 'res_model': 'ir.module.module.configuration.wizard',
290                                 'type': 'ir.actions.act_window',
291                                 'target':'new',
292          }
293
294         fields={
295                 'profile':{
296                         'string':'Profile',
297                         'type':'selection',
298                         'selection':_get_profiles,
299                         'default': -1,
300                         'required': True,
301                 },
302                 'charts':{
303                         'string':'Chart of accounts',
304                         'type':'selection',
305                         'selection':_get_charts,
306                         'default': -1,
307                         'required': True,
308                 },
309                 'name':{
310                         'string': 'Company Name',
311                         'type': 'char',
312                         'size': 64,
313                 },
314                 'street':{
315                         'string': 'Street',
316                         'type': 'char',
317                         'size': 128,
318                 },
319                 'street2':{
320                         'string': 'Street2',
321                         'type': 'char',
322                         'size': 128,
323                 },
324                 'zip':{
325                         'string': 'Zip code',
326                         'type': 'char',
327                         'size': 24,
328                 },
329                 'city':{
330                         'string': 'City',
331                         'type': 'char',
332                         'size': 128,
333                 },
334                 'state_id':{
335                         'string': 'State',
336                         'type': 'selection',
337                         'selection':_get_states,
338                 },
339                 'country_id':{
340                         'string': 'Country',
341                         'type': 'selection',
342                         'selection':_get_countries,
343                 },
344                 'email':{
345                         'string': 'E-mail',
346                         'type': 'char',
347                         'size': 64,
348                 },
349                 'phone':{
350                         'string': 'Phone',
351                         'type': 'char',
352                         'size': 64,
353                 },
354                 'currency': {
355                         'string': 'Currency',
356                         'type': 'many2one',
357                         'relation': 'res.currency',
358                         'required': True,
359                 },
360                 'rml_header1':{
361                         'string': 'Report Header',
362                         'type': 'char',
363                         'help': """This sentence will appear at the top right corner of your reports.
364 We suggest you to put a slogan here:
365 "Open Source Business Solutions".""",
366                         'size': 200,
367                 },
368                 'rml_footer1':{
369                         'string': 'Report Footer 1',
370                         'type': 'char',
371                         'help': """This sentence will appear at the bottom of your reports.
372 We suggest you to write legal sentences here:
373 Web: http://openerp.com - Fax: +32.81.73.35.01 - Fortis Bank: 126-2013269-07""",
374                         'size': 200,
375                 },
376                 'rml_footer2':{
377                         'string': 'Report Footer 2',
378                         'help': """This sentence will appear at the bottom of your reports.
379 We suggest you to put bank information here:
380 IBAN: BE74 1262 0121 6907 - SWIFT: CPDF BE71 - VAT: BE0477.472.701""",
381                         'type': 'char',
382                         'size': 200,
383                 },
384         }
385         states={
386                 'init':{
387                         'actions': [_get_company],
388                         'result': {'type': 'form', 'arch': view_form_profit, 'fields': fields,
389                                 'state': [
390                                         ('menu', 'Cancel', 'gtk-cancel'),
391                                         ('next', 'Next', 'gtk-go-forward', True)
392                                 ]
393                         }
394                 },
395                 'next': {
396                         'actions': [],
397                         'result': {'type': 'choice', 'next_state': _next}
398                 },
399                 'charts':{
400                         'actions': [],
401                         'result': {'type': 'form', 'arch': view_form_charts, 'fields': fields,
402                                 'state':[
403                                         ('init', 'Previous', 'gtk-go-back'),
404                                         ('company', 'Next', 'gtk-go-forward', True)
405                                 ]
406                         }
407                 },
408                 'company':{
409                         'actions': [],
410                         'result': {'type': 'form', 'arch': view_form_company, 'fields': fields,
411                                 'state': [
412                                         ('previous', 'Previous', 'gtk-go-back'),
413                                         ('update', 'Next', 'gtk-go-forward', True)
414                                 ]
415                         }
416                 },
417                 'previous':{
418                         'actions': [],
419                         'result': {'type': 'choice', 'next_state': _previous}
420                 },
421                 'update':{
422                         'actions': [],
423                         'result': {'type': 'form', 'arch': view_form_update, 'fields': fields,
424                                 'state': [
425                                         ('company', 'Previous', 'gtk-go-back'),
426                                         ('finish', 'Install', 'gtk-ok', True)
427                                 ]
428                         }
429                 },
430                 'finish':{
431                         'actions': [_update],
432                         'result': {'type': 'form', 'arch': view_form_finish, 'fields': {},
433                                 'state': [
434                                         ('menu', 'Use Directly'),
435                                         ('config', 'Start Configuration', 'gtk-ok', True)
436                                 ]
437                         }
438                 },
439                 'config': {
440             'result': {
441                 'type': 'action',
442                 'action': _config,
443                 'state': 'end',
444             },
445         },
446                 'menu': {
447                         'actions': [],
448                         'result': {'type': 'action', 'action': _menu, 'state': 'end'}
449                 },
450         }
451
452 wizard_base_setup('base_setup.base_setup')