1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
21 from osv import fields, osv
22 from operator import itemgetter
24 import sugarcrm_fields_mapping
25 from tools.translate import _
27 pp = pprint.PrettyPrinter(indent=4)
30 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
31 model_obj = obj.pool.get('ir.model.data')
32 return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
34 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context):
36 This function create the mapping between an already existing data and the similar data of sugarcrm
37 @param res_model: model of the mapped object
38 @param sugar_id: external sugar id
39 @param id: id in the database
41 @return : the xml_id or sugar_id
43 ir_model_data_obj = obj.pool.get('ir.model.data')
44 id = ir_model_data_obj._update(cr, uid, res_model,
45 'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
46 noupdate=True, res_id=id, context=context)
49 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False, context=None):
51 This method will import an object in the openerp, usefull for field that is only a char in sugar and is an object in openerp
52 use import_data that will take care to create/update or do nothing with the data
53 this method return the xml_id
54 @param fields: list of fields needed to create the object without id
55 @param data: the list of the data, in the same order as the field
56 ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
57 @param model: the openerp's model of the create/update object
58 @param table: the table where data come from in sugarcrm, no need to fit the real name of openerp name, just need to be unique
59 @param unique_name: the name of the object that we want to create/update get the id
60 @param domain_search : the domain that should find the unique existing record
62 @return: the xml_id of the ressources
64 domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
65 obj = sugar_obj.pool.get(model)
66 xml_id = generate_xml_id(name, table)
68 def mapped_id_if_exist(obj, domain, xml_id):
69 ids = obj.search(cr, uid, domain, context=context)
71 return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
75 mapped_id_if_exist(obj, domain_search, xml_id)
78 obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
81 def generate_xml_id(name, table):
83 @param name: name of the object, has to be unique in for a given table
84 @param table : table where the record we want generate come from
85 @return: a unique xml id for record, the xml_id will be the same given the same table and same name
86 To be used to avoid duplication of data that don't have ids
88 sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
89 name = name.replace('.', '_')
90 return sugar_instance + "_" + table + "_" + name
92 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
93 models = sugar_obj.pool.get(model)
94 model_code = sugar_val[0:2]
95 all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())])
96 output = sorted([(o.id, o.name)
97 for o in models.browse(cr, uid, all_model_ids, context=context)],
101 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
102 """Get states or create new state"""
104 res_country_state_obj = sugar_obj.pool.get('res.country.state')
106 state = get_all(sugar_obj,
107 cr, uid, 'res.country.state', sugar_val, context=context)
109 state_id = state and state[0][0]
111 state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
114 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
115 """Get Country or Create new country"""
116 res_country_obj = sugar_obj.pool.get('res.country')
118 country_code = sugar_country_val[0:2]
119 country = get_all(sugar_obj,
120 cr, uid, 'res.country', sugar_country_val, context=context)
122 country_id = country and country[0][0]
124 country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})
127 def import_partner_address(sugar_obj, cr, uid, context=None):
130 map_partner_address = {
132 'name': ['first_name', 'last_name'],
133 'phone': 'phone_work',
134 'mobile': 'phone_mobile',
137 'street': 'primary_address_street',
138 'zip': 'primary_address_postalcode',
139 'city': 'primary_address_city',
140 'country_id.id': 'country_id.id',
141 'state_id.id': 'state_id.id'
143 address_obj = sugar_obj.pool.get('res.partner.address')
144 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
145 sugar_data = sugar.search(PortType, sessionid, 'Contacts')
146 for val in sugar_data:
147 if val.get('primary_address_country'):
148 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
149 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
150 val['country_id.id'] = country_id
151 val['state_id.id'] = state
152 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
153 address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
158 def import_users(sugar_obj, cr, uid, context=None):
161 'name': ['first_name', 'last_name'],
162 'login': 'user_name',
163 'context_lang' : 'context_lang',
164 'password' : 'password',
166 'context_department_id.id': 'context_department_id.id',
169 def get_users_department(sugar_obj, cr, uid, val, context=None):
170 department_id = False
171 department_obj = sugar_obj.pool.get('hr.department')
172 department_ids = department_obj.search(cr, uid, [('name', '=', val)])
174 department_id = department_ids[0]
176 department_id = department_obj.create(cr, uid, {'name': val})
181 department_id = False
183 user_obj = sugar_obj.pool.get('res.users')
184 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
185 sugar_data = sugar.search(PortType,sessionid, 'Users')
186 for val in sugar_data:
187 user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
189 val['.id'] = str(user_ids[0])
191 val['password'] = 'sugarcrm' #default password for all user
192 department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
193 val['context_department_id.id'] = department_id
194 val['context_lang'] = context.get('lang','en_US')
195 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user)
196 #All data has to be imported separatly because they don't have the same field
197 user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
200 def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None):
204 stage_dict = {'status': #field in the sugarcrm database
205 { #Mapping of sugarcrm stage : openerp opportunity stage
207 'Assigned':'Qualification',
208 'In Progress': 'Proposition',
209 'Recycled': 'Negotiation',
212 stage = stage_dict['status'].get(sugar_val['status'], '')
213 stage_pool = surgar_obj.pool.get('crm.case.stage')
214 stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)])
215 for stage in stage_pool.browse(cr, uid, stage_ids, context):
219 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
223 state_dict = {'status': #field in the sugarcrm database
224 { #Mapping of sugarcrm stage : openerp opportunity stage
227 'In Progress': 'open',
228 'Recycled': 'cancel',
231 state = state_dict['status'].get(sugar_val['status'], '')
236 def get_user_address(sugar_obj, cr, uid, val, context=None):
237 address_obj = sugar_obj.pool.get('res.partner.address')
239 'name': ['first_name', 'last_name'],
240 'city': 'address_city',
241 'country_id': 'country_id',
242 'state_id': 'state_id',
243 'street': 'address_street',
244 'zip': 'address_postalcode',
246 address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') +' '+ val.get('last_name'))])
247 if val.get('address_country'):
248 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
249 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
250 val['country_id'] = country_id
251 val['state_id'] = state_id
252 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address)
253 dict_val = dict(zip(fields,datas))
255 address_obj.write(cr, uid, address_ids, dict_val)
257 new_address_id = address_obj.create(cr,uid, dict_val)
258 return new_address_id
261 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
262 address_obj = sugar_obj.pool.get('res.partner.address')
263 new_address_id = False
264 if type == 'invoice':
265 type_address = 'billing'
267 type_address = 'shipping'
269 map_partner_address.update({
270 'street': type_address + '_address_street',
271 'zip': type_address +'_address_postalcode',
272 'city': type_address +'_address_city',
273 'country_id': 'country_id',
277 if val.get(type_address +'_address_country'):
278 country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
279 state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
280 val['country_id'] = country_id
281 val['state_id'] = state
282 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
283 #Convert To list into Dictionary(Key, val). value pair.
284 dict_val = dict(zip(fields,datas))
285 new_address_id = address_obj.create(cr,uid, dict_val)
286 return new_address_id
288 def get_address(sugar_obj, cr, uid, val, context=None):
289 map_partner_address={}
291 address_obj = sugar_obj.pool.get('res.partner.address')
292 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
296 map_partner_address = {
299 'partner_id/id': 'account_id',
300 'phone': 'phone_office',
301 'mobile': 'phone_mobile',
305 if val.get('billing_address_street'):
306 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
308 if val.get('shipping_address_street'):
309 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
313 def import_partners(sugar_obj, cr, uid, context=None):
319 'website': 'website',
320 'user_id/id': 'assigned_user_id',
322 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
323 'customer': 'customer',
324 'supplier': 'supplier',
326 partner_obj = sugar_obj.pool.get('res.partner')
327 address_obj = sugar_obj.pool.get('res.partner.address')
328 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
329 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
330 for val in sugar_data:
331 add_id = get_address(sugar_obj, cr, uid, val, context)
332 val['customer'] = '1'
333 val['supplier'] = '0'
334 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner)
335 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
336 for address in address_obj.browse(cr,uid,add_id):
337 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
339 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
342 def get_category(sugar_obj, cr, uid, model, name, context=None):
344 categ_obj = sugar_obj.pool.get('crm.case.categ')
345 categ_ids = categ_obj.search(cr, uid, [('object_id.model','=',model), ('name', 'like', name)] )
347 categ_id = categ_ids[0]
349 model_ids = sugar_obj.pool.get('ir.model').search(cr, uid, [('model', '=', model)], context=context)
350 model = model_ids and model_ids[0] or False
351 categ_id = categ_obj.create(cr, uid, {'name': name, 'object_id': model})
354 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
356 alarm_dict = {'60': '1 minute before',
357 '300': '5 minutes before',
358 '600': '10 minutes before',
359 '900': '15 minutes before',
360 '1800':'30 minutes before',
361 '3600': '1 hour before',
364 alarm_obj = sugar_obj.pool.get('res.alarm')
365 if alarm_dict.get(val):
366 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
367 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
371 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
375 state_dict = {'status': #field in the sugarcrm database
376 { #Mapping of sugarcrm stage : openerp meeting stage
381 state = state_dict['status'].get(val, '')
384 def get_task_state(sugar_obj, cr, uid, val, context=None):
388 state_dict = {'status': #field in the sugarcrm database
389 { #Mapping of sugarcrm stage : openerp meeting stage
390 'Completed' : 'done',
391 'Not Started':'draft',
392 'In Progress': 'open',
393 'Pending Input': 'draft',
396 state = state_dict['status'].get(val, '')
399 def get_project_state(sugar_obj, cr, uid, val,context=None):
403 state_dict = {'status': #field in the sugarcrm database
404 { #Mapping of sugarcrm staus : openerp Projects state
407 'Published': 'close',
409 state = state_dict['status'].get(val, '')
412 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
416 state_dict = {'status': #field in the sugarcrm database
417 { #Mapping of sugarcrm status : openerp Porject Tasks state
418 'Not Started': 'draft',
419 'In Progress': 'open',
421 'Pending Input': 'pending',
422 'Deferred': 'cancelled',
424 state = state_dict['status'].get(val, '')
427 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
431 priority_dict = {'priority': #field in the sugarcrm database
432 { #Mapping of sugarcrm status : openerp Porject Tasks state
437 priority = priority_dict['priority'].get(val, '')
441 def get_account(sugar_obj, cr, uid, val, context=None):
445 partner_address_id = False
446 model_obj = sugar_obj.pool.get('ir.model.data')
447 address_obj = sugar_obj.pool.get('res.partner.address')
448 if val.get('parent_type') == 'Accounts':
449 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
451 model = model_obj.browse(cr, uid, model_ids)[0]
452 partner_id = model.res_id
453 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
454 partner_address_id = address_ids and address_ids[0]
456 if val.get('parent_type') == 'Contacts':
457 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
458 for model in model_obj.browse(cr, uid, model_ids):
459 partner_address_id = model.res_id
460 address_id = address_obj.browse(cr, uid, partner_address_id)
461 partner_id = address_id and address_id.partner_id or False
462 return partner_id, partner_address_id
464 def import_tasks(sugar_obj, cr, uid, context=None):
467 map_task = {'id' : 'id',
469 'date': 'date_start',
470 'date_deadline' : 'date_due',
471 'user_id/id': 'assigned_user_id',
472 'categ_id/.id': 'categ_id/.id',
473 'partner_id/.id': 'partner_id/.id',
474 'partner_address_id/.id': 'partner_address_id/.id',
477 meeting_obj = sugar_obj.pool.get('crm.meeting')
478 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
479 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
480 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
481 for val in sugar_data:
482 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
483 if not partner_xml_id:
484 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
485 partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
486 val['partner_id/.id'] = partner_id
487 val['partner_address_id/.id'] = partner_address_id
488 val['categ_id/.id'] = categ_id
489 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context=None)
490 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task)
491 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
494 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
497 model_obj = sugar_obj.pool.get('ir.model.data')
498 att_obj = sugar_obj.pool.get('calendar.attendee')
499 meeting_obj = sugar_obj.pool.get('crm.meeting')
500 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
501 for user in user_dict:
502 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
503 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
505 user_id = user_resource_id[0].res_id
506 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
508 attendees = attend_ids[0]
510 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
511 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
512 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
514 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
517 def import_meetings(sugar_obj, cr, uid, context=None):
520 map_meeting = {'id' : 'id',
522 'date': 'date_start',
523 'duration': ['duration_hours', 'duration_minutes'],
524 'location': 'location',
525 'alarm_id/.id': 'alarm_id/.id',
526 'user_id/id': 'assigned_user_id',
527 'partner_id/.id':'partner_id/.id',
528 'partner_address_id/.id':'partner_address_id/.id',
531 meeting_obj = sugar_obj.pool.get('crm.meeting')
532 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
533 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
534 for val in sugar_data:
535 partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
536 val['partner_id/.id'] = partner_id
537 val['partner_address_id/.id'] = partner_address_id
538 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
539 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
540 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting)
541 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
542 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
545 def get_calls_state(sugar_obj, cr, uid, val,context=None):
549 state_dict = {'status': #field in the sugarcrm database
550 { #Mapping of sugarcrm stage : openerp calls stage
553 'Not Held': 'pending',
555 state = state_dict['status'].get(val, '')
558 def import_calls(sugar_obj, cr, uid, context=None):
561 map_calls = {'id' : 'id',
563 'date': 'date_start',
564 'duration': ['duration_hours', 'duration_minutes'],
565 'user_id/id': 'assigned_user_id',
566 'partner_id/.id': 'partner_id/.id',
567 'partner_address_id/.id': 'partner_address_id/.id',
568 'categ_id/.id': 'categ_id/.id',
571 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
572 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
573 sugar_data = sugar.search(PortType, sessionid, 'Calls')
574 for val in sugar_data:
575 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
576 val['categ_id/.id'] = categ_id
577 partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
578 val['partner_id/.id'] = partner_id
579 val['partner_address_id/.id'] = partner_address_id
580 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
581 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls)
582 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
585 def import_resources(sugar_obj, cr, uid, context=None):
588 map_resource = {'id' : 'user_hash',
589 'name': ['first_name', 'last_name'],
591 resource_obj = sugar_obj.pool.get('resource.resource')
592 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
593 sugar_data = sugar.search(PortType, sessionid, 'Employees')
594 for val in sugar_data:
595 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
596 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
599 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
603 priority_dict = {'priority': #field in the sugarcrm database
604 { #Mapping of sugarcrm priority : openerp bugs priority
610 priority = priority_dict['priority'].get(val, '')
613 def get_bug_state(sugar_obj, cr, uid, val,context=None):
617 state_dict = {'status': #field in the sugarcrm database
618 { #Mapping of sugarcrm status : openerp Bugs state
622 'Pending': 'pending',
623 'Rejected': 'cancel',
625 state = state_dict['status'].get(val, '')
628 def get_issue_related_project(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
632 model_obj = sugar_obj.pool.get('ir.model.data')
633 project_obj = sugar_obj.pool.get('project.project')
634 sugar_bug_project = sugar.relation_search(PortType, sessionid, 'Bugs', module_id=val.get('id'), related_module='Project', query=None, deleted=None)
635 for project_id in sugar_bug_project:
636 model_ids = find_mapped_id(sugar_obj, cr, uid, 'project.project', project_id, context)
638 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
639 project_id = project_obj.browse(cr, uid, model_id).id
642 def import_bug(sugar_obj, cr, uid, context=None):
645 map_resource = {'id' : 'id',
647 'project_id/.id':'project_id/.id',
648 'categ_id.id': 'categ_id.id',
649 'priority':'priority',
650 'description': 'description',
653 issue_obj = sugar_obj.pool.get('project.issue')
654 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
655 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
656 for val in sugar_data:
657 val['project_id/.id'] = get_issue_related_project(sugar_obj,cr,uid, PortType, sessionid, val, context)
658 val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
659 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
660 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
661 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
662 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
665 def get_job_id(sugar_obj, cr, uid, val, context=None):
669 job_obj = sugar_obj.pool.get('hr.job')
670 job_ids = job_obj.search(cr, uid, [('name', '=', val)])
674 job_id = job_obj.create(cr, uid, {'name': val})
677 def get_attachment(sugar_obj, cr, uid, val, model, File, context=None):
680 attachment_obj = sugar_obj.pool.get('ir.attachment')
681 model_obj = sugar_obj.pool.get('ir.model.data')
682 mailgate_obj = sugar_obj.pool.get('mailgate.message')
683 new_attachment_id = attachment_obj.create(cr, uid, {'name': val.get('name'), 'datas': File, 'res_id': val['res_id'],'res_model': val['model']})
684 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
685 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
687 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
690 def import_history(sugar_obj, cr, uid, context=None):
693 map_attachment = {'id' : 'id',
695 'date':'date_entered',
696 'user_id/id': 'assigned_user_id',
697 'description': ['description', 'description_html'],
700 'partner_id.id' : 'partner_id.id',
702 mailgate_obj = sugar_obj.pool.get('mailgate.message')
703 model_obj = sugar_obj.pool.get('ir.model.data')
704 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
705 sugar_data = sugar.search(PortType, sessionid, 'Notes')
706 for val in sugar_data:
707 File = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
708 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
709 for model in model_obj.browse(cr, uid, model_ids):
710 val['res_id'] = model.res_id
711 val['model'] = model.model
712 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment)
713 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
714 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, context)
717 def import_employees(sugar_obj, cr, uid, context=None):
720 map_employee = {'id' : 'user_hash',
721 'resource_id/.id': 'resource_id/.id',
722 'name': ['first_name', 'last_name'],
723 'work_phone': 'phone_work',
724 'mobile_phone': 'phone_mobile',
725 'user_id/name': ['first_name', 'last_name'],
726 'address_home_id/.id': 'address_home_id/.id',
727 'notes': 'description',
728 #TODO: Creation of Employee create problem.
729 # 'coach_id/id': 'reports_to_id',
730 'job_id/.id': 'job_id/.id'
732 employee_obj = sugar_obj.pool.get('hr.employee')
733 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
734 sugar_data = sugar.search(PortType, sessionid, 'Employees')
735 for val in sugar_data:
736 address_id = get_user_address(sugar_obj, cr, uid, val, context)
737 val['address_home_id/.id'] = address_id
738 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
739 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
741 val['resource_id/.id'] = resource_id[0].res_id
742 val['job_id/.id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
743 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee)
744 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
747 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
748 fields = ['shortcut', 'name', 'domain']
749 data = [salutation, salutation, domain]
750 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
757 def import_emails(sugar_obj, cr, uid, context=None):
760 map_emails = {'id': 'id',
763 'email_from': 'from_addr_name',
764 'email_to': 'reply_to_addr',
765 'email_cc': 'cc_addrs_names',
766 'email_bcc': 'bcc_addrs_names',
767 'message_id': 'message_id',
768 'user_id/id': 'assigned_user_id',
769 'description': ['description', 'description_html'],
773 mailgate_obj = sugar_obj.pool.get('mailgate.message')
774 model_obj = sugar_obj.pool.get('ir.model.data')
775 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
776 sugar_data = sugar.search(PortType, sessionid, 'Emails')
777 for val in sugar_data:
778 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
779 for model in model_obj.browse(cr, uid, model_ids):
780 val['res_id'] = model.res_id
781 val['model'] = model.model
782 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails)
783 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
786 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
790 partner_invoice_id = False
791 model_obj = sugar_obj.pool.get('ir.model.data')
792 partner_obj = sugar_obj.pool.get('res.partner')
793 partner_address_obj = sugar_obj.pool.get('res.partner.address')
794 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
795 for account_id in sugar_project_account:
796 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
798 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
799 partner_id = partner_obj.browse(cr, uid, model_id).id
800 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
801 partner_invoice_id = address_ids[0]
802 return partner_id, partner_invoice_id
804 def import_projects(sugar_obj, cr, uid, context=None):
807 map_project = {'id': 'id',
809 'date_start': 'estimated_start_date',
810 'date': 'estimated_end_date',
811 'user_id/id': 'assigned_user_id',
812 'partner_id/.id': 'partner_id/.id',
813 'contact_id/.id': 'contact_id/.id',
816 project_obj = sugar_obj.pool.get('project.project')
817 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
818 sugar_data = sugar.search(PortType, sessionid, 'Project')
819 for val in sugar_data:
820 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
821 val['partner_id/.id'] = partner_id
822 val['contact_id/.id'] = partner_invoice_id
823 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
824 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project)
825 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
829 def import_project_tasks(sugar_obj, cr, uid, context=None):
832 map_project_task = {'id': 'id',
834 'date_start': 'date_start',
835 'date_end': 'date_finish',
836 'progress': 'progress',
837 'project_id/name': 'project_name',
838 'planned_hours': 'planned_hours',
839 'total_hours': 'total_hours',
840 'priority': 'priority',
841 'description': 'description',
842 'user_id/id': 'assigned_user_id',
845 task_obj = sugar_obj.pool.get('project.task')
846 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
847 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
848 for val in sugar_data:
849 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
850 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
851 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task)
852 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
855 def import_leads(sugar_obj, cr, uid, context=None):
860 'name': ['first_name', 'last_name'],
861 'contact_name': ['first_name', 'last_name'],
862 'description': ['description', 'refered_by', 'lead_source', 'lead_source_description', 'website'],
863 'partner_name': 'account_name',
864 'email_from': 'email1',
865 'phone': 'phone_work',
866 'mobile': 'phone_mobile',
867 'title.id': 'title.id',
869 'street': 'primary_address_street',
870 'street2': 'alt_address_street',
871 'zip': 'primary_address_postalcode',
872 'city':'primary_address_city',
873 'user_id/id' : 'assigned_user_id',
874 'stage_id.id' : 'stage_id.id',
879 lead_obj = sugar_obj.pool.get('crm.lead')
880 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
881 sugar_data = sugar.search(PortType, sessionid, 'Leads')
882 for val in sugar_data:
883 if val.get('opportunity_id'):
885 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
886 val['title/id'] = title_id
888 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
889 val['stage_id.id'] = stage_id
890 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
891 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead)
892 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
895 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
898 partner_contact_name = False
899 model_obj = sugar_obj.pool.get('ir.model.data')
900 partner_address_obj = sugar_obj.pool.get('res.partner.address')
901 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
902 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
903 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
904 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
905 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
907 for contact in sugar_contact:
908 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
910 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
911 address_id = partner_address_obj.browse(cr, uid, model_id)
912 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
913 partner_contact_name = address_id.name
915 partner_contact_name = val.get('account_name')
916 return partner_contact_name
918 def import_opportunities(sugar_obj, cr, uid, context=None):
921 map_opportunity = {'id' : 'id',
923 'probability': 'probability',
924 'partner_id/name': 'account_name',
925 'title_action': 'next_step',
926 'partner_address_id/name': 'partner_address_id/name',
927 'planned_revenue': 'amount',
928 'date_deadline':'date_closed',
929 'user_id/id' : 'assigned_user_id',
930 'stage_id/id' : 'stage_id/id',
932 'categ_id.id': 'categ_id.id'
934 lead_obj = sugar_obj.pool.get('crm.lead')
935 partner_obj = sugar_obj.pool.get('res.partner')
936 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
937 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
938 for val in sugar_data:
939 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
940 if not partner_xml_id:
941 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
942 partner_contact_name = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
943 val['partner_address_id/name'] = partner_contact_name
944 val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
945 val['type'] = 'opportunity'
946 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
947 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
948 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
951 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
954 fields = ['name', 'type']
955 name = 'Opportunity_' + sugar_val['sales_stage']
956 data = [sugar_val['sales_stage'], 'Opportunity']
957 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
959 MAP_FIELDS = {'Opportunities': #Object Mapping name
960 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
961 'process' : import_opportunities,
964 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
965 'process' : import_leads,
968 {'dependencies' : [], #Object to import before this table
969 'process' : import_partner_address,
972 {'dependencies' : ['Users', 'Contacts'], #Object to import before this table
973 'process' : import_partners,
976 {'dependencies' : [],
977 'process' : import_users,
980 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
981 'process' : import_meetings,
984 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
985 'process' : import_tasks,
988 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
989 'process' : import_calls,
992 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
993 'process' : import_projects,
996 {'dependencies' : ['Users', 'Projects'],
997 'process' : import_project_tasks,
1000 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1001 'process' : import_bug,
1005 {'dependencies' : ['Users'],
1006 'process' : import_emails,
1010 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1011 'process' : import_history,
1014 {'dependencies' : ['Resources', 'Users'],
1015 'process' : import_employees,
1018 {'dependencies' : ['Users'],
1019 'process' : import_resources,
1023 class import_sugarcrm(osv.osv):
1024 """Import SugarCRM DATA"""
1026 _name = "import.sugarcrm"
1027 _description = __doc__
1029 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1030 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1031 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1032 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1033 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1034 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1035 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1036 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1037 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1038 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1039 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1040 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1041 'username': fields.char('User Name', size=64),
1042 'password': fields.char('Password', size=24),
1044 _defaults = { #to be set to true, but easier for debugging
1045 'opportunity': False,
1054 'project_task': False,
1058 def get_key(self, cr, uid, ids, context=None):
1059 """Select Key as For which Module data we want import data."""
1063 for current in self.browse(cr, uid, ids, context):
1064 if current.opportunity:
1065 key_list.append('Opportunities')
1067 key_list.append('Users')
1069 key_list.append('Contacts')
1071 key_list.append('Accounts')
1072 if current.employee:
1073 key_list.append('Employees')
1075 key_list.append('Meetings')
1077 key_list.append('Calls')
1079 key_list.append('Emails')
1081 key_list.append('Projects')
1082 if current.project_task:
1083 key_list.append('Project Tasks')
1085 key_list.append('Bugs')
1086 if current.attachment:
1087 key_list.append('Notes')
1090 def import_all(self, cr, uid, ids, context=None):
1091 """Import all sugarcrm data into openerp module"""
1094 keys = self.get_key(cr, uid, ids, context)
1095 imported = set() #to invoid importing 2 times the sames modules
1097 if not key in imported:
1098 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1099 MAP_FIELDS[key]['process'](self, cr, uid, context)
1102 obj_model = self.pool.get('ir.model.data')
1103 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1104 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1106 'view_type': 'form',
1107 'view_mode': 'form',
1108 'res_model': 'import.message',
1109 'views': [(resource_id,'form')],
1110 'type': 'ir.actions.act_window',
1114 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1115 for dependency in dep:
1116 if not dependency in imported:
1117 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1118 dict[dependency]['process'](self, cr, uid, context)
1119 imported.add(dependency)