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