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