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