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)
29 def create_mapping(obj, cr, uid, res_model, open_id, sugar_id, context):
33 'module': 'sugarcrm_import',
36 model_obj = obj.pool.get('ir.model.data')
37 model_obj.create(cr, uid, model_data, context=context)
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)
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,
54 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
55 """Get states or create new state"""
57 res_country_state_obj = sugar_obj.pool.get('res.country.state')
59 state = get_all(sugar_obj,
60 cr, uid, 'res.country.state', sugar_val, context=context)
62 state_id = state and state[0][0]
64 state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
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')
71 country_code = sugar_country_val[0:2]
72 country = get_all(sugar_obj,
73 cr, uid, 'res.country', sugar_country_val, context=context)
75 country_id = country and country[0][0]
77 country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})
80 def import_partner_address(sugar_obj, cr, uid, context=None):
83 map_partner_address = {
85 'name': ['first_name', 'last_name'],
86 'phone': 'phone_work',
87 'mobile': 'phone_mobile',
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'
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)
108 def get_users_department(sugar_obj, cr, uid, val, context=None):
111 department_id = False
112 department_obj = sugar_obj.pool.get('hr.department')
113 department_ids = department_obj.search(cr, uid, [('name', '=', val)])
115 department_id = department_ids[0]
117 department_id = department_obj.create(cr, uid, {'name': val})
120 def import_users(sugar_obj, cr, uid, context=None):
123 department_id = False
124 map_user = {'id' : 'id',
125 'name': ['first_name', 'last_name'],
126 'login': 'user_name',
128 'context_lang' : 'context_lang',
129 'password' : 'password',
131 'context_department_id.id': 'context_department_id.id',
133 user_obj = sugar_obj.pool.get('res.users')
134 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
135 sugar_data = sugar.search(PortType,sessionid, 'Users')
136 for val in sugar_data:
137 user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
139 val['.id'] = str(user_ids[0])
141 val['password'] = 'sugarcrm' #default password for all user
142 department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
143 val['context_department_id.id'] = department_id
144 val['context_lang'] = context.get('lang','en_US')
145 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user)
146 #All data has to be imported separatly because they don't have the same field
147 user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
150 def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None):
154 stage_dict = {'status': #field in the sugarcrm database
155 { #Mapping of sugarcrm stage : openerp opportunity stage
157 'Assigned':'Qualification',
158 'In Progress': 'Proposition',
159 'Recycled': 'Negotiation',
162 stage = stage_dict['status'].get(sugar_val['status'], '')
163 stage_pool = surgar_obj.pool.get('crm.case.stage')
164 stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)])
165 for stage in stage_pool.browse(cr, uid, stage_ids, context):
169 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
173 state_dict = {'status': #field in the sugarcrm database
174 { #Mapping of sugarcrm stage : openerp opportunity stage
177 'In Progress': 'open',
178 'Recycled': 'cancel',
181 state = state_dict['status'].get(sugar_val['status'], '')
184 def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None):
188 stage_dict = { 'sales_stage':
189 {#Mapping of sugarcrm stage : openerp opportunity stage Mapping
190 'Need Analysis': 'New',
191 'Closed Lost': 'Lost',
193 'Value Proposition': 'Proposition',
194 'Negotiation/Review': 'Negotiation'
197 stage = stage_dict['sales_stage'].get(sugar_val['sales_stage'], '')
198 stage_pool = surgar_obj.pool.get('crm.case.stage')
199 stage_ids = stage_pool.search(cr, uid, [('type', '=', 'opportunity'), ('name', '=', stage)])
200 for stage in stage_pool.browse(cr, uid, stage_ids, context):
204 def get_user_address(sugar_obj, cr, uid, val, context=None):
205 address_obj = sugar_obj.pool.get('res.partner.address')
207 'name': ['first_name', 'last_name'],
208 'city': 'address_city',
209 'country_id': 'country_id',
210 'state_id': 'state_id',
211 'street': 'address_street',
212 'zip': 'address_postalcode',
214 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('first_name') +''+ val.get('last_name'))])
215 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
216 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
217 val['country_id'] = country_id
218 val['state_id'] = state_id
219 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address)
220 dict_val = dict(zip(fields,datas))
222 address_obj.write(cr, uid, address_ids, dict_val)
224 new_address_id = address_obj.create(cr,uid, dict_val)
225 return new_address_id
228 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
229 address_obj = sugar_obj.pool.get('res.partner.address')
230 new_address_id = False
231 if type == 'invoice':
232 type_address = 'billing'
234 type_address = 'shipping'
236 map_partner_address.update({
237 'street': type_address + '_address_street',
238 'zip': type_address +'_address_postalcode',
239 'city': type_address +'_address_city',
240 'country_id': 'country_id',
244 country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
245 state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
246 val['country_id'] = country_id
247 val['state_id'] = state
248 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
249 #Convert To list into Dictionary(Key, val). value pair.
250 dict_val = dict(zip(fields,datas))
251 new_address_id = address_obj.create(cr,uid, dict_val)
252 return new_address_id
254 def get_address(sugar_obj, cr, uid, val, context=None):
255 map_partner_address={}
257 address_obj = sugar_obj.pool.get('res.partner.address')
258 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
262 map_partner_address = {
265 'partner_id/id': 'account_id',
266 'phone': 'phone_office',
267 'mobile': 'phone_mobile',
271 if val.get('billing_address_street'):
272 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
274 if val.get('shipping_address_street'):
275 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
279 def import_partners(sugar_obj, cr, uid, context=None):
285 'website': 'website',
286 'user_id/id': 'assigned_user_id',
288 'comment': ['description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
289 'customer': 'customer',
290 'supplier': 'supplier',
292 partner_obj = sugar_obj.pool.get('res.partner')
293 address_obj = sugar_obj.pool.get('res.partner.address')
294 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
295 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
296 for val in sugar_data:
297 add_id = get_address(sugar_obj, cr, uid, val, context)
298 if val.get('account_type') in ('Customer', 'Prospect', 'Other'):
299 val['customer'] = '1'
301 val['supplier'] = '1'
302 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner)
303 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
304 for address in address_obj.browse(cr,uid,add_id):
305 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
307 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
310 def get_category(sugar_obj, cr, uid, model, name, context=None):
312 categ_obj = sugar_obj.pool.get('crm.case.categ')
313 categ_ids = categ_obj.search(cr, uid, [('object_id.model','=',model), ('name', 'like', name)] )
315 categ_id = categ_ids[0]
317 categ_id = categ_obj.create(cr, uid, {'name': name, 'object_id.model': model})
320 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
322 alarm_dict = {'60': '1 minute before',
323 '300': '5 minutes before',
324 '600': '10 minutes before',
325 '900': '15 minutes before',
326 '1800':'30 minutes before',
327 '3600': '1 hour before',
330 alarm_obj = sugar_obj.pool.get('res.alarm')
331 if alarm_dict.get(val):
332 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
333 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
337 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
341 state_dict = {'status': #field in the sugarcrm database
342 { #Mapping of sugarcrm stage : openerp meeting stage
347 state = state_dict['status'].get(val, '')
350 def get_task_state(sugar_obj, cr, uid, val, context=None):
354 state_dict = {'status': #field in the sugarcrm database
355 { #Mapping of sugarcrm stage : openerp meeting stage
356 'Completed' : 'done',
357 'Not Started':'draft',
358 'In Progress': 'open',
359 'Pending Input': 'draft',
362 state = state_dict['status'].get(val, '')
365 def get_project_state(sugar_obj, cr, uid, val,context=None):
369 state_dict = {'status': #field in the sugarcrm database
370 { #Mapping of sugarcrm staus : openerp Projects state
373 'Published': 'close',
375 state = state_dict['status'].get(val, '')
378 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
382 state_dict = {'status': #field in the sugarcrm database
383 { #Mapping of sugarcrm status : openerp Porject Tasks state
384 'Not Started': 'draft',
385 'In Progress': 'open',
387 'Pending Input': 'pending',
388 'Deferred': 'cancelled',
390 state = state_dict['status'].get(val, '')
393 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
397 priority_dict = {'priority': #field in the sugarcrm database
398 { #Mapping of sugarcrm status : openerp Porject Tasks state
403 priority = priority_dict['priority'].get(val, '')
407 def get_account(sugar_obj, cr, uid, val, context=None):
411 partner_address_id = False
412 model_obj = sugar_obj.pool.get('ir.model.data')
413 address_obj = sugar_obj.pool.get('res.partner.address')
414 if val.get('parent_type') == 'Accounts':
415 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
417 model = model_obj.browse(cr, uid, model_ids)[0]
418 partner_id = model.res_id
419 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
420 partner_address_id = address_ids and address_ids[0]
422 if val.get('parent_type') == 'Contacts':
423 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
424 for model in model_obj.browse(cr, uid, model_ids):
425 partner_address_id = model.res_id
426 address_id = address_obj.browse(cr, uid, partner_address_id)
427 partner_id = address_id and address_id.partner_id or False
428 return partner_id, partner_address_id
430 def import_tasks(sugar_obj, cr, uid, context=None):
434 partner_address_id = False
435 map_task = {'id' : 'id',
437 'date': 'date_entered',
438 'user_id/id': 'assigned_user_id',
439 'categ_id/.id': 'categ_id/.id',
440 'partner_id/.id': 'partner_id/.id',
441 'partner_address_id/.id': 'partner_address_id/.id',
444 meeting_obj = sugar_obj.pool.get('crm.meeting')
445 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
446 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
447 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
448 for val in sugar_data:
449 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
450 if not partner_xml_id:
451 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
452 partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
453 val['partner_id/.id'] = partner_id
454 val['partner_address_id/.id'] = partner_address_id
455 val['categ_id/.id'] = categ_id
456 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context=None)
457 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task)
458 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
461 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
464 model_obj = sugar_obj.pool.get('ir.model.data')
465 att_obj = sugar_obj.pool.get('calendar.attendee')
466 meeting_obj = sugar_obj.pool.get('crm.meeting')
467 user_xml_id, user_email_id = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
468 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user_xml_id, context)
469 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
471 user_id = user_resource_id[0].res_id
472 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user_email_id})
473 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
474 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
476 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
479 def import_meetings(sugar_obj, cr, uid, context=None):
483 partner_address_id = False
484 map_meeting = {'id' : 'id',
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',
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)
509 def get_calls_state(sugar_obj, cr, uid, val,context=None):
513 state_dict = {'status': #field in the sugarcrm database
514 { #Mapping of sugarcrm stage : openerp calls stage
517 'Not Held': 'pending',
519 state = state_dict['status'].get(val, '')
522 def import_calls(sugar_obj, cr, uid, context=None):
526 partner_address_id = False
527 map_calls = {'id' : 'id',
529 'date': 'date_start',
530 'duration': ['duration_hours', 'duration_minutes'],
531 'user_id/id': 'assigned_user_id',
532 'partner_id/.id': 'partner_id/.id',
533 'partner_address_id/.id': 'partner_address_id/.id',
534 'categ_id/.id': 'categ_id/.id',
537 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
538 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
539 sugar_data = sugar.search(PortType, sessionid, 'Calls')
540 for val in sugar_data:
541 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
542 val['categ_id/.id'] = categ_id
543 partner_id, partner_address_id = get_account(sugar_obj, cr, uid, val, context)
544 val['partner_id/.id'] = partner_id
545 val['partner_address_id/.id'] = partner_address_id
546 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
547 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls)
548 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
551 def import_resources(sugar_obj, cr, uid, context=None):
554 map_resource = {'id' : 'user_hash',
555 'name': ['first_name', 'last_name'],
557 resource_obj = sugar_obj.pool.get('resource.resource')
558 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
559 sugar_data = sugar.search(PortType, sessionid, 'Employees')
560 for val in sugar_data:
561 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource)
562 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
565 def get_job_id(sugar_obj, cr, uid, val, context=None):
569 job_obj = sugar_obj.pool.get('hr.job')
570 job_ids = job_obj.search(cr, uid, [('name', '=', val)])
574 job_id = job_obj.create(cr, uid, {'name': val})
577 def import_employees(sugar_obj, cr, uid, context=None):
580 map_employee = {'id' : 'user_hash',
581 'resource_id/.id': 'resource_id/.id',
582 'name': ['first_name', 'last_name'],
583 'work_phone': 'phone_work',
584 'mobile_phone': 'phone_mobile',
585 'user_id/name': ['first_name', 'last_name'],
586 'address_home_id/.id': 'address_home_id/.id',
587 'notes': 'description',
588 #TODO: Creation of Employee create problem.
589 # 'coach_id/id': 'reports_to_id',
590 'job_id/.id': 'job_id/.id'
592 employee_obj = sugar_obj.pool.get('hr.employee')
593 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
594 sugar_data = sugar.search(PortType, sessionid, 'Employees')
595 for val in sugar_data:
596 address_id = get_user_address(sugar_obj, cr, uid, val, context)
597 val['address_home_id/.id'] = address_id
598 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
599 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
601 val['resource_id/.id'] = resource_id[0].res_id
602 val['job_id/.id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
603 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee)
604 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
607 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
610 contact_title_obj = sugar_obj.pool.get('res.partner.title')
612 title_ids = contact_title_obj.search(cr, uid, [('shortcut', '=', salutation), ('domain', '=', domain)])
614 title_id = title_ids[0]
616 title_id = contact_title_obj.create(cr, uid, {'name': salutation, 'shortcut': salutation, 'domain': domain})
619 def import_emails(sugar_obj, cr, uid, context=None):
623 map_emails = {'id': 'id',
626 'email_from': 'from_addr_name',
627 'email_to': 'reply_to_addr',
628 'email_cc': 'cc_addrs_names',
629 'email_bcc': 'bcc_addrs_names',
630 'message_id': 'message_id',
631 'user_id/id': 'assigned_user_id',
632 'description': 'description_html',
636 mailgate_obj = sugar_obj.pool.get('mailgate.message')
637 model_obj = sugar_obj.pool.get('ir.model.data')
638 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
639 sugar_data = sugar.search(PortType, sessionid, 'Emails')
640 for val in sugar_data:
641 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
642 for model in model_obj.browse(cr, uid, model_ids):
643 val['res_id'] = model.res_id
644 val['model'] = model.model
645 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails)
646 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
649 def import_projects(sugar_obj, cr, uid, context=None):
652 map_project = {'id': 'id',
654 'date_start': 'estimated_start_date',
655 'date': 'estimated_end_date',
656 'user_id/id': 'assigned_user_id',
659 project_obj = sugar_obj.pool.get('project.project')
660 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
661 sugar_data = sugar.search(PortType, sessionid, 'Project')
662 for val in sugar_data:
663 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
664 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project)
665 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
669 def import_project_tasks(sugar_obj, cr, uid, context=None):
672 map_project_task = {'id': 'id',
674 'date_start': 'date_start',
675 'date_end': 'date_finish',
676 'progress': 'progress',
677 'project_id/name': 'project_name',
678 'planned_hours': 'planned_hours',
679 'total_hours': 'total_hours',
680 'priority': 'priority',
681 'description': 'description',
682 'user_id/id': 'assigned_user_id',
685 task_obj = sugar_obj.pool.get('project.task')
686 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
687 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
688 for val in sugar_data:
689 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
690 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
691 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task)
692 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
695 def import_leads(sugar_obj, cr, uid, context=None):
701 'name': ['first_name', 'last_name'],
702 'contact_name': ['first_name', 'last_name'],
703 'description': ['description', 'refered_by', 'lead_source', 'lead_source_description', 'website'],
704 'partner_name': 'account_name',
705 'email_from': 'email1',
706 'phone': 'phone_work',
707 'mobile': 'phone_mobile',
708 'title.id': 'title.id',
710 'street': 'primary_address_street',
711 'street2': 'alt_address_street',
712 'zip': 'primary_address_postalcode',
713 'city':'primary_address_city',
714 'user_id/id' : 'assigned_user_id',
715 'stage_id.id' : 'stage_id.id',
720 lead_obj = sugar_obj.pool.get('crm.lead')
721 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
722 sugar_data = sugar.search(PortType, sessionid, 'Leads')
723 for val in sugar_data:
724 if val.get('opportunity_id'):
726 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'contact', context)
727 val['title.id'] = title_id
729 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
730 val['stage_id.id'] = stage_id
731 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
732 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead)
733 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
736 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
739 partner_contact_name = False
740 model_obj = sugar_obj.pool.get('ir.model.data')
741 partner_address_obj = sugar_obj.pool.get('res.partner.address')
742 sugar_opportunity_contact = sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None)
743 for contact in sugar_opportunity_contact:
744 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
746 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
747 address_id = partner_address_obj.browse(cr, uid, model_id)
748 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
749 partner_contact_name = address_id.name
751 partner_contact_name = val.get('account_name')
752 return partner_contact_name
754 def import_opportunities(sugar_obj, cr, uid, context=None):
757 partner_contact_name = False
758 map_opportunity = {'id' : 'id',
760 'probability': 'probability',
761 'partner_id/name': 'account_name',
762 'title_action': 'next_step',
763 'partner_address_id/name': 'partner_address_id/name',
764 'planned_revenue': 'amount',
765 'date_deadline':'date_closed',
766 'user_id/id' : 'assigned_user_id',
767 'stage_id.id' : 'stage_id.id',
769 'categ_id.id': 'categ_id.id'
771 lead_obj = sugar_obj.pool.get('crm.lead')
772 partner_obj = sugar_obj.pool.get('res.partner')
773 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
774 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
775 for val in sugar_data:
776 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
777 if not partner_xml_id:
778 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
779 partner_contact_name = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
780 val['partner_address_id/name'] = partner_contact_name
781 val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
782 val['type'] = 'opportunity'
783 stage_id = get_opportunity_status(sugar_obj, cr, uid, val, context)
784 val['stage_id.id'] = stage_id
785 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
786 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
789 MAP_FIELDS = {'Opportunities': #Object Mapping name
790 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
791 'process' : import_opportunities,
794 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
795 'process' : import_leads,
798 {'dependencies' : ['Users'], #Object to import before this table
799 'process' : import_partner_address,
802 {'dependencies' : ['Users'], #Object to import before this table
803 'process' : import_partners,
806 {'dependencies' : [],
807 'process' : import_users,
810 {'dependencies' : ['Users', 'Tasks'],
811 'process' : import_meetings,
814 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
815 'process' : import_tasks,
818 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
819 'process' : import_calls,
822 {'dependencies' : ['Resources'],
823 'process' : import_employees,
826 {'dependencies' : ['Users'],
827 'process' : import_emails,
830 {'dependencies' : ['Users'],
831 'process' : import_projects,
834 {'dependencies' : ['Users', 'Projects'],
835 'process' : import_project_tasks,
838 {'dependencies' : ['Users'],
839 'process' : import_resources,
843 class import_sugarcrm(osv.osv):
844 """Import SugarCRM DATA"""
846 _name = "import.sugarcrm"
847 _description = __doc__
849 'lead': fields.boolean('Leads', help="If Leads are checked, SugarCRM Leads data imported in openERP crm-Lead form"),
850 'opportunity': fields.boolean('Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in openERP crm-Opportunity form"),
851 'user': fields.boolean('User', help="If Users are checked, SugarCRM Users data imported in openERP Users form"),
852 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in openERP partner address form"),
853 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in openERP partners form"),
854 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in openERP employees form"),
855 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in openERP meetings form"),
856 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in openERP phonecalls form"),
857 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in openERP Emails form"),
858 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in openERP Projects form"),
859 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in openERP Project Tasks form"),
860 'username': fields.char('User Name', size=64),
861 'password': fields.char('Password', size=24),
877 def get_key(self, cr, uid, ids, context=None):
878 """Select Key as For which Module data we want import data."""
882 for current in self.browse(cr, uid, ids, context):
884 key_list.append('Leads')
885 if current.opportunity:
886 key_list.append('Opportunities')
888 key_list.append('Users')
890 key_list.append('Contacts')
892 key_list.append('Accounts')
894 key_list.append('Employees')
896 key_list.append('Meetings')
898 key_list.append('Calls')
900 key_list.append('Emails')
902 key_list.append('Projects')
903 if current.project_task:
904 key_list.append('Project Tasks')
907 def import_all(self, cr, uid, ids, context=None):
908 """Import all sugarcrm data into openerp module"""
911 keys = self.get_key(cr, uid, ids, context)
912 imported = set() #to invoid importing 2 times the sames modules
914 if not key in imported:
915 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
916 MAP_FIELDS[key]['process'](self, cr, uid, context)
919 obj_model = self.pool.get('ir.model.data')
920 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
921 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
925 'res_model': 'import.message',
926 'views': [(resource_id,'form')],
927 'type': 'ir.actions.act_window',
931 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
932 for dependency in dep:
933 if not dependency in imported:
934 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
935 dict[dependency]['process'](self, cr, uid, context)
936 imported.add(dependency)