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