[ADD] generic function to import data that doesn't have external_id
[odoo/odoo.git] / addons / import_sugarcrm / import_sugarcrm.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 from osv import fields, osv
22 from operator import itemgetter
23 import sugar
24 import sugarcrm_fields_mapping
25 from tools.translate import _
26 import pprint
27 pp = pprint.PrettyPrinter(indent=4)
28
29
30 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
31     model_obj = obj.pool.get('ir.model.data')
32     return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
33
34 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context):
35     """
36         This function create the mapping between an already existing data and the similar data of sugarcrm
37         @param res_model: model of the mapped object
38         @param sugar_id: external sugar id
39         @param id: id in the database
40         
41         @return : the xml_id or sugar_id 
42     """
43     ir_model_data_obj = obj.pool.get('ir.model.data')
44     id = ir_model_data_obj._update(cr, uid, res_model,
45                      'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
46                      noupdate=True, res_id=id, context=context)
47     return sugar_id
48
49 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False,  context=None):
50     """
51         This method will import an object in the openerp, usefull for field that is only a char in sugar and is an object in openerp
52         use import_data that will take care to create/update or do nothing with the data
53         this method return the xml_id
54         @param fields: list of fields needed to create the object without id
55         @param data: the list of the data, in the same order as the field
56             ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
57         @param model: the openerp's model of the create/update object
58         @param table: the table where data come from in sugarcrm, no need to fit the real name of openerp name, just need to be unique
59         @param unique_name: the name of the object that we want to create/update get the id
60         @param domain_search : the domain that should find the unique existing record
61         
62         @return: the xml_id of the ressources
63     """
64     domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
65     obj = sugar_obj.pool.get(model)
66     xml_id = generate_xml_id(name, table)
67
68     def mapped_id_if_exist(obj, domain, xml_id):
69         ids = obj.search(cr, uid, domain, context=context)
70         if ids:
71             return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
72         return False
73     
74     
75     mapped_id_if_exist(obj, domain_search, xml_id)
76     fields.append('id')
77     data.append(xml_id)
78     obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
79     return xml_id
80
81 def generate_xml_id(name, table):
82     """
83         @param name: name of the object, has to be unique in for a given table
84         @param table : table where the record we want generate come from
85         @return: a unique xml id for record, the xml_id will be the same given the same table and same name
86                  To be used to avoid duplication of data that don't have ids
87     """
88     sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
89     name = name.replace('.', '_')
90     return sugar_instance + "_" + table + "_" + name
91
92 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
93     models = sugar_obj.pool.get(model)
94     model_code = sugar_val[0:2]
95     all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())]) 
96     output = sorted([(o.id, o.name)
97                      for o in models.browse(cr, uid, all_model_ids, context=context)],
98                     key=itemgetter(1))
99     return output
100
101 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
102     """Get states or create new state"""
103     state_id = False
104     res_country_state_obj = sugar_obj.pool.get('res.country.state')
105     
106     state = get_all(sugar_obj,
107         cr, uid, 'res.country.state', sugar_val, context=context)
108     if state:
109         state_id = state and state[0][0]
110     else:
111         state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
112     return state_id   
113
114 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
115     """Get Country or Create new country"""
116     res_country_obj = sugar_obj.pool.get('res.country')
117     country_id = False
118     country_code = sugar_country_val[0:2]
119     country = get_all(sugar_obj,
120         cr, uid, 'res.country', sugar_country_val, context=context)
121     if country:
122         country_id = country and country[0][0] 
123     else:
124         country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})  
125     return country_id
126
127 def import_partner_address(sugar_obj, cr, uid, context=None):
128     if not context:
129         context = {}
130     map_partner_address = {
131              'id': 'id',              
132              'name': ['first_name', 'last_name'],
133             'phone': 'phone_work',
134             'mobile': 'phone_mobile',
135             'fax': 'phone_fax',
136             'function': 'title',
137             'street': 'primary_address_street',
138             'zip': 'primary_address_postalcode',
139             'city': 'primary_address_city',
140             'country_id.id': 'country_id.id',
141             'state_id.id': 'state_id.id'
142             }
143     address_obj = sugar_obj.pool.get('res.partner.address')
144     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
145     sugar_data = sugar.search(PortType, sessionid, 'Contacts')
146     for val in sugar_data:
147         if val.get('primary_address_country'):
148             country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
149             state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
150             val['country_id.id'] =  country_id
151             val['state_id.id'] =  state        
152         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
153         address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
154     return True
155     
156
157
158 def import_users(sugar_obj, cr, uid, context=None):
159     map_user = {
160         'id' : 'id', 
161         'name': ['first_name', 'last_name'],
162         'login': 'user_name',
163         'context_lang' : 'context_lang',
164         'password' : 'password',
165         '.id' : '.id',
166         'context_department_id.id': 'context_department_id.id',
167     } 
168     
169     def get_users_department(sugar_obj, cr, uid, val, context=None):
170         department_id = False       
171         department_obj = sugar_obj.pool.get('hr.department')
172         department_ids = department_obj.search(cr, uid, [('name', '=', val)])
173         if department_ids:
174             department_id = department_ids[0]
175         elif val:
176             department_id = department_obj.create(cr, uid, {'name': val})
177         return department_id 
178     
179     if not context:
180         context = {}
181     department_id = False        
182     
183     user_obj = sugar_obj.pool.get('res.users')
184     PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
185     sugar_data = sugar.search(PortType,sessionid, 'Users')
186     for val in sugar_data:
187         user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
188         if user_ids: 
189             val['.id'] = str(user_ids[0])
190         else:
191             val['password'] = 'sugarcrm' #default password for all user
192         department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
193         val['context_department_id.id'] = department_id     
194         val['context_lang'] = context.get('lang','en_US')
195         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user)
196         #All data has to be imported separatly because they don't have the same field
197         user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
198     return True
199
200 def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None):
201     if not context:
202         context = {}
203     stage_id = False
204     stage_dict = {'status': #field in the sugarcrm database
205         { #Mapping of sugarcrm stage : openerp opportunity stage
206             'New' : 'New',
207             'Assigned':'Qualification',
208             'In Progress': 'Proposition',
209             'Recycled': 'Negotiation',
210             'Dead': 'Lost'
211         },}
212     stage = stage_dict['status'].get(sugar_val['status'], '')
213     stage_pool = surgar_obj.pool.get('crm.case.stage')
214     stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)])
215     for stage in stage_pool.browse(cr, uid, stage_ids, context):
216         stage_id = stage.id
217     return stage_id
218
219 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
220     if not context:
221         context = {}
222     state = False
223     state_dict = {'status': #field in the sugarcrm database
224         { #Mapping of sugarcrm stage : openerp opportunity stage
225             'New' : 'draft',
226             'Assigned':'open',
227             'In Progress': 'open',
228             'Recycled': 'cancel',
229             'Dead': 'done'
230         },}
231     state = state_dict['status'].get(sugar_val['status'], '')
232     return state
233
234
235
236 def get_user_address(sugar_obj, cr, uid, val, context=None):
237     address_obj = sugar_obj.pool.get('res.partner.address')
238     map_user_address = {
239     'name': ['first_name', 'last_name'],
240     'city': 'address_city',
241     'country_id': 'country_id',
242     'state_id': 'state_id',
243     'street': 'address_street',
244     'zip': 'address_postalcode',
245     }
246     address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') +' '+ val.get('last_name'))])
247     if val.get('address_country'):
248         country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
249         state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
250         val['country_id'] =  country_id
251         val['state_id'] =  state_id
252     fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address)
253     dict_val = dict(zip(fields,datas))
254     if address_ids:
255         address_obj.write(cr, uid, address_ids, dict_val)
256     else:        
257         new_address_id = address_obj.create(cr,uid, dict_val)
258         return new_address_id
259     return True
260
261 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
262         address_obj = sugar_obj.pool.get('res.partner.address')
263         new_address_id = False
264         if type == 'invoice':
265             type_address = 'billing'
266         else:
267             type_address = 'shipping'     
268     
269         map_partner_address.update({
270             'street': type_address + '_address_street',
271             'zip': type_address +'_address_postalcode',
272             'city': type_address +'_address_city',
273              'country_id': 'country_id',
274              'type': 'type',
275             })
276         val['type'] = type
277         if val.get(type_address +'_address_country'):
278             country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
279             state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
280             val['country_id'] =  country_id
281             val['state_id'] =  state
282         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
283         #Convert To list into Dictionary(Key, val). value pair.
284         dict_val = dict(zip(fields,datas))
285         new_address_id = address_obj.create(cr,uid, dict_val)
286         return new_address_id
287     
288 def get_address(sugar_obj, cr, uid, val, context=None):
289     map_partner_address={}
290     address_id=[]
291     address_obj = sugar_obj.pool.get('res.partner.address')
292     address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
293     if address_ids:
294         return address_ids 
295     else:
296         map_partner_address = {
297             'id': 'id',                    
298             'name': 'name',
299             'partner_id/id': 'account_id',
300             'phone': 'phone_office',
301             'mobile': 'phone_mobile',
302             'fax': 'phone_fax',
303             'type': 'type',
304             }
305         if val.get('billing_address_street'):
306             address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
307             
308         if val.get('shipping_address_street'):
309             address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
310         return address_id
311     return True
312
313 def import_partners(sugar_obj, cr, uid, context=None):
314     if not context:
315         context = {}
316     map_partner = {
317                 'id': 'id',
318                 'name': 'name',
319                 'website': 'website',
320                 'user_id/id': 'assigned_user_id',
321                 'ref': 'sic_code',
322                 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
323                 'customer': 'customer',
324                 'supplier': 'supplier', 
325                 }
326     partner_obj = sugar_obj.pool.get('res.partner')
327     address_obj = sugar_obj.pool.get('res.partner.address')
328     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
329     sugar_data = sugar.search(PortType, sessionid, 'Accounts')
330     for val in sugar_data:
331         add_id = get_address(sugar_obj, cr, uid, val, context)
332         val['customer'] = '1'
333         val['supplier'] = '0'
334         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner)
335         partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
336         for address in  address_obj.browse(cr,uid,add_id):
337             data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
338             if data_id:
339                 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})                
340     return True
341
342 def get_category(sugar_obj, cr, uid, model, name, context=None):
343     categ_id = False
344     categ_obj = sugar_obj.pool.get('crm.case.categ')
345     categ_ids = categ_obj.search(cr, uid, [('object_id.model','=',model), ('name', 'like', name)] )
346     if categ_ids:
347         categ_id = categ_ids[0]
348     else:
349         model_ids = sugar_obj.pool.get('ir.model').search(cr, uid, [('model', '=', model)], context=context)
350         model = model_ids and model_ids[0] or False
351         categ_id = categ_obj.create(cr, uid, {'name': name, 'object_id': model})
352     return categ_id     
353
354 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
355     
356     alarm_dict = {'60': '1 minute before',
357                   '300': '5 minutes before',
358                   '600': '10 minutes before',
359                   '900': '15 minutes before',
360                   '1800':'30 minutes before',
361                   '3600': '1 hour before',
362      }
363     alarm_id = False
364     alarm_obj = sugar_obj.pool.get('res.alarm')
365     if alarm_dict.get(val):
366         alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
367         for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
368             alarm_id = alarm.id
369     return alarm_id 
370     
371 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
372     if not context:
373         context = {}
374     state = False
375     state_dict = {'status': #field in the sugarcrm database
376         { #Mapping of sugarcrm stage : openerp meeting stage
377             'Planned' : 'draft',
378             'Held':'open',
379             'Not Held': 'draft',
380         },}
381     state = state_dict['status'].get(val, '')
382     return state    
383
384 def get_task_state(sugar_obj, cr, uid, val, context=None):
385     if not context:
386         context = {}
387     state = False
388     state_dict = {'status': #field in the sugarcrm database
389         { #Mapping of sugarcrm stage : openerp meeting stage
390             'Completed' : 'done',
391             'Not Started':'draft',
392             'In Progress': 'open',
393             'Pending Input': 'draft',
394             'deferred': 'cancel'
395         },}
396     state = state_dict['status'].get(val, '')
397     return state    
398
399 def get_project_state(sugar_obj, cr, uid, val,context=None):
400     if not context:
401         context = {}
402     state = False
403     state_dict = {'status': #field in the sugarcrm database
404         { #Mapping of sugarcrm staus : openerp Projects state
405             'Draft' : 'draft',
406             'In Review': 'open',
407             'Published': 'close',
408         },}
409     state = state_dict['status'].get(val, '')
410     return state    
411
412 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
413     if not context:
414         context = {}
415     state = False
416     state_dict = {'status': #field in the sugarcrm database
417         { #Mapping of sugarcrm status : openerp Porject Tasks state
418              'Not Started': 'draft',
419              'In Progress': 'open',
420              'Completed': 'done',
421             'Pending Input': 'pending',
422             'Deferred': 'cancelled',
423         },}
424     state = state_dict['status'].get(val, '')
425     return state    
426
427 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
428     if not context:
429         context = {}
430     priority = False
431     priority_dict = {'priority': #field in the sugarcrm database
432         { #Mapping of sugarcrm status : openerp Porject Tasks state
433             'High': '0',
434             'Medium': '2',
435             'Low': '3'
436         },}
437     priority = priority_dict['priority'].get(val, '')
438     return priority    
439
440
441 def get_account(sugar_obj, cr, uid, val, context=None):
442     if not context:
443         context = {}
444     partner_id = False    
445     partner_address_id = False
446     model_obj = sugar_obj.pool.get('ir.model.data')
447     address_obj = sugar_obj.pool.get('res.partner.address')
448     if val.get('parent_type') == 'Accounts':
449         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
450         if model_ids:
451             model = model_obj.browse(cr, uid, model_ids)[0]
452             partner_id = model.res_id
453             address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
454             partner_address_id = address_ids and address_ids[0]
455             
456     if val.get('parent_type') == 'Contacts':
457         model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
458         for model in model_obj.browse(cr, uid, model_ids):
459             partner_address_id = model.res_id
460             address_id = address_obj.browse(cr, uid, partner_address_id)
461             partner_id = address_id and address_id.partner_id or False
462     return partner_id, partner_address_id                             
463
464 def import_tasks(sugar_obj, cr, uid, context=None):
465     if not context:
466         context = {}
467     map_task = {'id' : 'id',
468                 'name': 'name',
469                 'date': 'date_start',
470                 'date_deadline' : 'date_due',
471                 'user_id/id': 'assigned_user_id',
472                 'categ_id/.id': 'categ_id/.id',
473                 'partner_id/.id': 'partner_id/.id',
474                 'partner_address_id/.id': 'partner_address_id/.id',
475                 'state': 'state'
476     }
477     meeting_obj = sugar_obj.pool.get('crm.meeting')
478     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
479     categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
480     sugar_data = sugar.search(PortType, sessionid, 'Tasks')
481     for val in sugar_data:
482         partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
483         if not partner_xml_id:
484             raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
485         partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
486         val['partner_id/.id'] = partner_id
487         val['partner_address_id/.id'] = partner_address_id
488         val['categ_id/.id'] = categ_id
489         val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context=None)
490         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task)
491         meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
492     return True    
493     
494 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
495     if not context:
496         context = {}
497     model_obj = sugar_obj.pool.get('ir.model.data')
498     att_obj = sugar_obj.pool.get('calendar.attendee')
499     meeting_obj = sugar_obj.pool.get('crm.meeting')
500     user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
501     for user in user_dict: 
502         user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
503         user_resource_id = model_obj.browse(cr, uid, user_model_ids)        
504         if user_resource_id:
505             user_id = user_resource_id[0].res_id 
506             attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
507             if attend_ids:
508                 attendees = attend_ids[0]
509             else:      
510                 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
511             meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
512             meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
513             if meeting_xml_id:
514                 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})       
515     return True   
516     
517 def import_meetings(sugar_obj, cr, uid, context=None):
518     if not context:
519         context = {}
520     map_meeting = {'id' : 'id',
521                     'name': 'name',
522                     'date': 'date_start',
523                     'duration': ['duration_hours', 'duration_minutes'],
524                     'location': 'location',
525                     'alarm_id/.id': 'alarm_id/.id',
526                     'user_id/id': 'assigned_user_id',
527                     'partner_id/.id':'partner_id/.id',
528                     'partner_address_id/.id':'partner_address_id/.id',
529                     'state': 'state'
530     }
531     meeting_obj = sugar_obj.pool.get('crm.meeting')
532     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
533     sugar_data = sugar.search(PortType, sessionid, 'Meetings')
534     for val in sugar_data:
535         partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
536         val['partner_id/.id'] = partner_id
537         val['partner_address_id/.id'] = partner_address_id
538         val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
539         val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
540         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting)
541         meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
542         get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
543     return True    
544
545 def get_calls_state(sugar_obj, cr, uid, val,context=None):
546     if not context:
547         context = {}
548     state = False
549     state_dict = {'status': #field in the sugarcrm database
550         { #Mapping of sugarcrm stage : openerp calls stage
551             'Planned' : 'open',
552             'Held':'done',
553             'Not Held': 'pending',
554         },}
555     state = state_dict['status'].get(val, '')
556     return state   
557
558 def import_calls(sugar_obj, cr, uid, context=None):
559     if not context:
560         context = {}
561     map_calls = {'id' : 'id',
562                     'name': 'name',
563                     'date': 'date_start',
564                     'duration': ['duration_hours', 'duration_minutes'],
565                     'user_id/id': 'assigned_user_id',
566                     'partner_id/.id': 'partner_id/.id',
567                     'partner_address_id/.id': 'partner_address_id/.id',
568                     'categ_id/.id': 'categ_id/.id',
569                     'state': 'state',
570     }
571     phonecall_obj = sugar_obj.pool.get('crm.phonecall')
572     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
573     sugar_data = sugar.search(PortType, sessionid, 'Calls')
574     for val in sugar_data:
575         categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))         
576         val['categ_id/.id'] = categ_id
577         partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
578         val['partner_id/.id'] = partner_id
579         val['partner_address_id/.id'] = partner_address_id
580         val['state'] =  get_calls_state(sugar_obj, cr, uid, val.get('status'), context)  
581         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls)
582         phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
583     return True
584     
585 def import_resources(sugar_obj, cr, uid, context=None):
586     if not context:
587         context = {}
588     map_resource = {'id' : 'user_hash',
589                     'name': ['first_name', 'last_name'],
590     }
591     resource_obj = sugar_obj.pool.get('resource.resource')
592     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
593     sugar_data = sugar.search(PortType, sessionid, 'Employees')
594     for val in sugar_data:
595         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
596         resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
597     return True    
598
599 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
600     if not context:
601         context = {}
602     priority = False
603     priority_dict = {'priority': #field in the sugarcrm database
604         { #Mapping of sugarcrm priority : openerp bugs priority
605             'Urgent': '1',
606             'High': '2',
607             'Medium': '3',
608             'Low': '4'
609         },}
610     priority = priority_dict['priority'].get(val, '')
611     return priority    
612
613 def get_bug_state(sugar_obj, cr, uid, val,context=None):
614     if not context:
615         context = {}
616     state = False
617     state_dict = {'status': #field in the sugarcrm database
618         { #Mapping of sugarcrm status : openerp Bugs state
619             'New' : 'draft',
620             'Assigned':'open',
621             'Closed': 'done',
622             'Pending': 'pending',
623             'Rejected': 'cancel',
624         },}
625     state = state_dict['status'].get(val, '')
626     return state
627     
628 def get_issue_related_project(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
629     if not context:
630         context={}
631     project_id = False        
632     model_obj = sugar_obj.pool.get('ir.model.data')
633     project_obj = sugar_obj.pool.get('project.project')
634     sugar_bug_project = sugar.relation_search(PortType, sessionid, 'Bugs', module_id=val.get('id'), related_module='Project', query=None, deleted=None)
635     for project_id in sugar_bug_project:
636         model_ids = find_mapped_id(sugar_obj, cr, uid, 'project.project', project_id, context)
637         if model_ids:
638             model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
639             project_id = project_obj.browse(cr, uid, model_id).id
640     return project_id     
641
642 def import_bug(sugar_obj, cr, uid, context=None):
643     if not context:
644         context = {}
645     map_resource = {'id' : 'id',
646                     'name': 'name',
647                     'project_id/.id':'project_id/.id',
648                     'categ_id.id': 'categ_id.id',
649                     'priority':'priority',
650                     'description': 'description',
651                     'state': 'state',
652     }
653     issue_obj = sugar_obj.pool.get('project.issue')
654     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
655     sugar_data = sugar.search(PortType, sessionid, 'Bugs')
656     for val in sugar_data:
657         val['project_id/.id'] = get_issue_related_project(sugar_obj,cr,uid, PortType, sessionid, val, context)
658         val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
659         val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
660         val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
661         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
662         issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
663     return True    
664
665 def get_job_id(sugar_obj, cr, uid, val, context=None):
666     if not context:
667         context={}
668     job_id = False    
669     job_obj = sugar_obj.pool.get('hr.job')        
670     job_ids = job_obj.search(cr, uid, [('name', '=', val)])
671     if job_ids:
672         job_id = job_ids[0]
673     else:
674         job_id = job_obj.create(cr, uid, {'name': val})
675     return job_id
676     
677 def get_attachment(sugar_obj, cr, uid, val, model, File, context=None):
678     if not context:
679         context = {}
680     attachment_obj = sugar_obj.pool.get('ir.attachment')
681     model_obj = sugar_obj.pool.get('ir.model.data')
682     mailgate_obj = sugar_obj.pool.get('mailgate.message')
683     new_attachment_id = attachment_obj.create(cr, uid, {'name': val.get('name'), 'datas': File, 'res_id': val['res_id'],'res_model': val['model']})
684     message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
685     message_xml_id = model_obj.browse(cr, uid, message_model_ids)
686     if message_xml_id:
687         mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})             
688     return True    
689     
690 def import_history(sugar_obj, cr, uid, context=None):
691     if not context:
692         context = {}
693     map_attachment = {'id' : 'id',
694                       'name':'name',
695                       'date':'date_entered',
696                       'user_id/id': 'assigned_user_id',
697                       'description': ['description', 'description_html'],
698                       'res_id': 'res_id',
699                       'model': 'model',
700                       'partner_id.id' : 'partner_id.id',
701     }
702     mailgate_obj = sugar_obj.pool.get('mailgate.message')
703     model_obj =  sugar_obj.pool.get('ir.model.data')
704     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
705     sugar_data = sugar.search(PortType, sessionid, 'Notes')
706     for val in sugar_data:
707         File = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
708         model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
709         for model in model_obj.browse(cr, uid, model_ids):
710             val['res_id'] = model.res_id
711             val['model'] = model.model
712         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment)   
713         mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
714         get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, context)
715     return True       
716     
717 def import_employees(sugar_obj, cr, uid, context=None):
718     if not context:
719         context = {}
720     map_employee = {'id' : 'user_hash',
721                     'resource_id/.id': 'resource_id/.id',
722                     'name': ['first_name', 'last_name'],
723                     'work_phone': 'phone_work',
724                     'mobile_phone':  'phone_mobile',
725                     'user_id/name': ['first_name', 'last_name'], 
726                     'address_home_id/.id': 'address_home_id/.id',
727                     'notes': 'description',
728                     #TODO: Creation of Employee create problem.
729                  #   'coach_id/id': 'reports_to_id',
730                     'job_id/.id': 'job_id/.id'
731     }
732     employee_obj = sugar_obj.pool.get('hr.employee')
733     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
734     sugar_data = sugar.search(PortType, sessionid, 'Employees')
735     for val in sugar_data:
736         address_id = get_user_address(sugar_obj, cr, uid, val, context)
737         val['address_home_id/.id'] = address_id
738         model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
739         resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
740         if resource_id:
741             val['resource_id/.id'] = resource_id[0].res_id
742         val['job_id/.id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
743         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee)
744         employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
745     return True
746
747 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
748     fields = ['shortcut', 'name', 'domain']
749     data = [salutation, salutation, domain]
750     return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
751
752
753     
754         
755     
756     
757 def import_emails(sugar_obj, cr, uid, context=None):
758     if not context:
759         context= {}
760     map_emails = {'id': 'id',
761     'name':'name',
762     'date':'date_sent',
763     'email_from': 'from_addr_name',
764     'email_to': 'reply_to_addr',
765     'email_cc': 'cc_addrs_names',
766     'email_bcc': 'bcc_addrs_names',
767     'message_id': 'message_id',
768     'user_id/id': 'assigned_user_id',
769     'description': ['description', 'description_html'],
770     'res_id': 'res_id',
771     'model': 'model',
772     }
773     mailgate_obj = sugar_obj.pool.get('mailgate.message')
774     model_obj = sugar_obj.pool.get('ir.model.data')
775     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
776     sugar_data = sugar.search(PortType, sessionid, 'Emails')
777     for val in sugar_data:
778         model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
779         for model in model_obj.browse(cr, uid, model_ids):
780             val['res_id'] = model.res_id
781             val['model'] = model.model
782         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails)
783         mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
784     return True    
785     
786 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
787     if not context:
788         context={}
789     partner_id = False
790     partner_invoice_id = False        
791     model_obj = sugar_obj.pool.get('ir.model.data')
792     partner_obj = sugar_obj.pool.get('res.partner')
793     partner_address_obj = sugar_obj.pool.get('res.partner.address')
794     sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
795     for account_id in sugar_project_account:
796         model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
797         if model_ids:
798             model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
799             partner_id = partner_obj.browse(cr, uid, model_id).id
800             address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
801             partner_invoice_id = address_ids[0] 
802     return partner_id, partner_invoice_id      
803     
804 def import_projects(sugar_obj, cr, uid, context=None):
805     if not context:
806         context = {}
807     map_project = {'id': 'id',
808         'name': 'name',
809         'date_start': 'estimated_start_date',
810         'date': 'estimated_end_date',
811         'user_id/id': 'assigned_user_id',
812         'partner_id/.id': 'partner_id/.id',
813         'contact_id/.id': 'contact_id/.id', 
814          'state': 'state'   
815     }
816     project_obj = sugar_obj.pool.get('project.project')
817     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
818     sugar_data = sugar.search(PortType, sessionid, 'Project')
819     for val in sugar_data:
820         partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context) 
821         val['partner_id/.id'] = partner_id
822         val['contact_id/.id'] = partner_invoice_id 
823         val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
824         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project)
825         project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
826     return True 
827
828
829 def import_project_tasks(sugar_obj, cr, uid, context=None):
830     if not context:
831         context = {}
832     map_project_task = {'id': 'id',
833         'name': 'name',
834         'date_start': 'date_start',
835         'date_end': 'date_finish',
836         'progress': 'progress',
837         'project_id/name': 'project_name',
838         'planned_hours': 'planned_hours',
839         'total_hours': 'total_hours',        
840         'priority': 'priority',
841         'description': 'description',
842         'user_id/id': 'assigned_user_id',
843          'state': 'state'   
844     }
845     task_obj = sugar_obj.pool.get('project.task')
846     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
847     sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
848     for val in sugar_data:
849         val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
850         val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
851         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task)
852         task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
853     return True 
854     
855 def import_leads(sugar_obj, cr, uid, context=None):
856     if not context:
857         context = {}
858     map_lead = {
859             'id' : 'id',
860             'name': ['first_name', 'last_name'],
861             'contact_name': ['first_name', 'last_name'],
862             'description': ['description', 'refered_by', 'lead_source', 'lead_source_description', 'website'],
863             'partner_name': 'account_name',
864             'email_from': 'email1',
865             'phone': 'phone_work',
866             'mobile': 'phone_mobile',
867             'title.id': 'title.id',
868             'function':'title',
869             'street': 'primary_address_street',
870             'street2': 'alt_address_street',
871             'zip': 'primary_address_postalcode',
872             'city':'primary_address_city',
873             'user_id/id' : 'assigned_user_id',
874             'stage_id.id' : 'stage_id.id',
875             'type' : 'type',
876             'state': 'state',
877             }
878         
879     lead_obj = sugar_obj.pool.get('crm.lead')
880     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
881     sugar_data = sugar.search(PortType, sessionid, 'Leads')
882     for val in sugar_data:
883         if val.get('opportunity_id'):
884             continue
885         title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
886         val['title/id'] = title_id
887         val['type'] = 'lead'
888         stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
889         val['stage_id.id'] = stage_id
890         val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
891         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead)
892         lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
893     return True
894
895 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
896     if not context:
897         context={}
898     partner_contact_name = False        
899     model_obj = sugar_obj.pool.get('ir.model.data')
900     partner_address_obj = sugar_obj.pool.get('res.partner.address')
901     model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
902     model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name 
903     sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
904     sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
905     sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
906     if sugar_contact: 
907         for contact in sugar_contact:
908             model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
909             if model_ids:
910                 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
911                 address_id = partner_address_obj.browse(cr, uid, model_id)
912                 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
913                 partner_contact_name = address_id.name
914             else:
915                 partner_contact_name = val.get('account_name')    
916     return partner_contact_name 
917
918 def import_opportunities(sugar_obj, cr, uid, context=None):
919     if not context:
920         context = {}
921     map_opportunity = {'id' : 'id',
922         'name': 'name',
923         'probability': 'probability',
924         'partner_id/name': 'account_name',
925         'title_action': 'next_step',
926         'partner_address_id/name': 'partner_address_id/name',
927         'planned_revenue': 'amount',
928         'date_deadline':'date_closed',
929         'user_id/id' : 'assigned_user_id',
930         'stage_id/id' : 'stage_id/id',
931         'type' : 'type',
932         'categ_id.id': 'categ_id.id'
933     }
934     lead_obj = sugar_obj.pool.get('crm.lead')
935     partner_obj = sugar_obj.pool.get('res.partner')
936     PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
937     sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
938     for val in sugar_data:
939         partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
940         if not partner_xml_id:
941             raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
942         partner_contact_name = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
943         val['partner_address_id/name'] = partner_contact_name
944         val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))                    
945         val['type'] = 'opportunity'
946         val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
947         fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
948         lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
949     return True
950
951 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
952     if not context:
953         context = {}
954     fields = ['name', 'type']
955     name = 'Opportunity_' + sugar_val['sales_stage']
956     data = [sugar_val['sales_stage'], 'Opportunity']
957     return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
958
959 MAP_FIELDS = {'Opportunities':  #Object Mapping name
960                     {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],  #Object to import before this table
961                      'process' : import_opportunities,
962                      },
963               'Leads':
964                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],  #Object to import before this table
965                      'process' : import_leads,
966                     },
967               'Contacts':
968                     {'dependencies' : [],  #Object to import before this table
969                      'process' : import_partner_address,
970                     },
971               'Accounts':
972                     {'dependencies' : ['Users', 'Contacts'],  #Object to import before this table
973                      'process' : import_partners,
974                     },
975               'Users': 
976                     {'dependencies' : [],
977                      'process' : import_users,
978                     },
979               'Meetings': 
980                     {'dependencies' : ['Accounts', 'Contacts', 'Users'],
981                      'process' : import_meetings,
982                     },        
983               'Tasks': 
984                     {'dependencies' : ['Accounts', 'Contacts', 'Users'],
985                      'process' : import_tasks,
986                     },  
987               'Calls': 
988                     {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
989                      'process' : import_calls,
990                     },  
991               'Projects': 
992                     {'dependencies' : ['Users', 'Accounts', 'Contacts'],
993                      'process' : import_projects,
994                     },                        
995               'Project Tasks': 
996                     {'dependencies' : ['Users', 'Projects'],
997                      'process' : import_project_tasks,
998                     },
999               'Bugs': 
1000                     {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1001                      'process' : import_bug,
1002                     },                         
1003               
1004               'Emails': 
1005                     {'dependencies' : ['Users'],
1006                      'process' : import_emails,
1007                     },    
1008               
1009               'Notes': 
1010                     {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1011                      'process' : import_history,
1012                     },  
1013               'Employees': 
1014                     {'dependencies' : ['Resources', 'Users'],
1015                      'process' : import_employees,
1016                     },                  
1017               'Resources': 
1018                     {'dependencies' : ['Users'],
1019                      'process' : import_resources,
1020                     },                                      
1021           }
1022
1023 class import_sugarcrm(osv.osv):
1024     """Import SugarCRM DATA"""
1025     
1026     _name = "import.sugarcrm"
1027     _description = __doc__
1028     _columns = {
1029         'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1030         'user': fields.boolean('Users', help="If Users  are checked, SugarCRM Users data imported in OpenERP Users form"),
1031         'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1032         'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM  Accounts data imported in OpenERP partners form"),
1033         'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1034         'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1035         'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1036         'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1037         'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1038         'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1039         'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1040         'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1041         'username': fields.char('User Name', size=64),
1042         'password': fields.char('Password', size=24),
1043     }
1044     _defaults = { #to be set to true, but easier for debugging
1045        'opportunity': False,
1046        'user' : False,
1047        'contact' : False,
1048        'account' : False,
1049         'employee' : False,
1050         'meeting' : False,
1051         'call' : False,    
1052         'email' : False, 
1053         'project' : False,   
1054         'project_task': False,     
1055         'bug': False,
1056     }
1057     
1058     def get_key(self, cr, uid, ids, context=None):
1059         """Select Key as For which Module data we want import data."""
1060         if not context:
1061             context = {}
1062         key_list = []
1063         for current in self.browse(cr, uid, ids, context):
1064             if current.opportunity:
1065                 key_list.append('Opportunities')
1066             if current.user:
1067                 key_list.append('Users')
1068             if current.contact:
1069                 key_list.append('Contacts')
1070             if current.account:
1071                 key_list.append('Accounts') 
1072             if current.employee:
1073                 key_list.append('Employees')  
1074             if current.meeting:
1075                 key_list.append('Meetings')
1076             if current.call:
1077                 key_list.append('Calls')
1078             if current.email:
1079                 key_list.append('Emails') 
1080             if current.project:
1081                 key_list.append('Projects')
1082             if current.project_task:
1083                 key_list.append('Project Tasks')
1084             if current.bug:
1085                 key_list.append('Bugs')
1086             if current.attachment:
1087                 key_list.append('Notes')                                    
1088         return key_list
1089
1090     def import_all(self, cr, uid, ids, context=None):
1091         """Import all sugarcrm data into openerp module"""
1092         if not context:
1093             context = {}
1094         keys = self.get_key(cr, uid, ids, context)
1095         imported = set() #to invoid importing 2 times the sames modules
1096         for key in keys:
1097             if not key in imported:
1098                 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1099                 MAP_FIELDS[key]['process'](self, cr, uid, context)
1100                 imported.add(key)
1101
1102         obj_model = self.pool.get('ir.model.data')
1103         model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1104         resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1105         return {
1106                 'view_type': 'form',
1107                 'view_mode': 'form',
1108                 'res_model': 'import.message',
1109                 'views': [(resource_id,'form')],
1110                 'type': 'ir.actions.act_window',
1111                 'target': 'new',
1112             }
1113
1114     def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1115         for dependency in dep:
1116             if not dependency in imported:
1117                 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1118                 dict[dependency]['process'](self, cr, uid, context)
1119                 imported.add(dependency)
1120         return True        
1121
1122 import_sugarcrm()