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 OPENERP_FIEDS_MAPS = {'Leads': 'crm.lead',
30 'Opportunities': 'crm.lead',
31 'Contacts': 'res.partner.address',
32 'Accounts': 'res.partner',
33 'Resources': 'resource.resource',
35 'Meetings': 'crm.meeting',
36 'Calls': 'crm.phonecall',
37 'Claims': 'crm.claim',
38 'Employee': 'hr.employee',
39 'Project': 'project.project',
40 'ProjectTask': 'project.task',
41 'Bugs': 'project.issue',
42 'Documents': 'ir.attachment',
46 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
47 model_obj = obj.pool.get('ir.model.data')
48 return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
50 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context):
52 This function create the mapping between an already existing data and the similar data of sugarcrm
53 @param res_model: model of the mapped object
54 @param sugar_id: external sugar id
55 @param id: id in the database
57 @return : the xml_id or sugar_id
59 ir_model_data_obj = obj.pool.get('ir.model.data')
60 id = ir_model_data_obj._update(cr, uid, res_model,
61 'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
62 noupdate=True, res_id=id, context=context)
65 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False, context=None):
67 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
68 use import_data that will take care to create/update or do nothing with the data
69 this method return the xml_id
70 @param fields: list of fields needed to create the object without id
71 @param data: the list of the data, in the same order as the field
72 ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
73 @param model: the openerp's model of the create/update object
74 @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
75 @param unique_name: the name of the object that we want to create/update get the id
76 @param domain_search : the domain that should find the unique existing record
78 @return: the xml_id of the ressources
80 domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
81 obj = sugar_obj.pool.get(model)
82 xml_id = generate_xml_id(name, table)
84 def mapped_id_if_exist(obj, domain, xml_id):
85 ids = obj.search(cr, uid, domain, context=context)
87 return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
91 mapped_id_if_exist(obj, domain_search, xml_id)
94 obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
97 def generate_xml_id(name, table):
99 @param name: name of the object, has to be unique in for a given table
100 @param table : table where the record we want generate come from
101 @return: a unique xml id for record, the xml_id will be the same given the same table and same name
102 To be used to avoid duplication of data that don't have ids
104 sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
105 name = name.replace('.', '_')
106 return sugar_instance + "_" + table + "_" + name
108 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
109 models = sugar_obj.pool.get(model)
110 model_code = sugar_val[0:2]
111 all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())])
112 output = sorted([(o.id, o.name)
113 for o in models.browse(cr, uid, all_model_ids, context=context)],
117 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
118 """Get states or create new state"""
120 res_country_state_obj = sugar_obj.pool.get('res.country.state')
122 state = get_all(sugar_obj,
123 cr, uid, 'res.country.state', sugar_val, context=context)
125 state_id = state and state[0][0]
127 state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
130 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
131 """Get Country or Create new country"""
132 res_country_obj = sugar_obj.pool.get('res.country')
134 country_code = sugar_country_val[0:2]
135 country = get_all(sugar_obj,
136 cr, uid, 'res.country', sugar_country_val, context=context)
138 country_id = country and country[0][0]
140 country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})
143 def import_partner_address(sugar_obj, cr, uid, context=None):
146 map_partner_address = {
148 'name': ['first_name', 'last_name'],
149 'partner_id/id': 'account_id',
150 'phone': 'phone_work',
151 'mobile': 'phone_mobile',
154 'street': 'primary_address_street',
155 'zip': 'primary_address_postalcode',
156 'city': 'primary_address_city',
157 'country_id.id': 'country_id.id',
158 'state_id.id': 'state_id.id',
162 address_obj = sugar_obj.pool.get('res.partner.address')
163 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
164 sugar_data = sugar.search(PortType, sessionid, 'Contacts')
165 for val in sugar_data:
166 val['type'] = 'contact'
167 contact_emails = sugar.contact_emails_search(PortType, context.get('username', ''), context.get('password', ''), email_address=val.get('email1'))
168 val['email'] = (','.join(map(lambda x : x, contact_emails)))
169 if val.get('primary_address_country'):
170 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
171 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
172 val['country_id.id'] = country_id
173 val['state_id.id'] = state
174 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
175 address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
179 def import_users(sugar_obj, cr, uid, context=None):
182 'name': ['first_name', 'last_name'],
183 'login': 'user_name',
184 'context_lang' : 'context_lang',
185 'password' : 'password',
187 'context_department_id.id': 'context_department_id.id',
190 def get_users_department(sugar_obj, cr, uid, val, context=None):
191 department_id = False
192 department_obj = sugar_obj.pool.get('hr.department')
193 department_ids = department_obj.search(cr, uid, [('name', '=', val)])
195 department_id = department_ids[0]
197 department_id = department_obj.create(cr, uid, {'name': val})
202 department_id = False
204 user_obj = sugar_obj.pool.get('res.users')
205 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
206 sugar_data = sugar.search(PortType,sessionid, 'Users')
207 for val in sugar_data:
208 user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
210 val['.id'] = str(user_ids[0])
212 val['password'] = 'sugarcrm' #default password for all user
213 department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
214 val['context_department_id.id'] = department_id
215 val['context_lang'] = context.get('lang','en_US')
216 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user, context)
217 #All data has to be imported separatly because they don't have the same field
218 user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
221 def get_lead_status(sugar_obj, cr, uid, sugar_val,context=None):
224 fields = ['name', 'type']
225 name = 'lead_' + sugar_val['status']
226 data = [sugar_val['status'], 'lead']
227 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_lead_stage', name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val['status'])], context)
229 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
233 state_dict = {'status': #field in the sugarcrm database
234 { #Mapping of sugarcrm stage : openerp opportunity stage
237 'In Progress': 'open',
238 'Recycled': 'cancel',
241 state = state_dict['status'].get(sugar_val['status'], '')
244 def get_user_address(sugar_obj, cr, uid, val, context=None):
245 address_obj = sugar_obj.pool.get('res.partner.address')
247 'name': ['first_name', 'last_name'],
248 'city': 'address_city',
249 'country_id': 'country_id',
250 'state_id': 'state_id',
251 'street': 'address_street',
252 'zip': 'address_postalcode',
254 address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') or '' +' '+ val.get('last_name'))])
255 if val.get('address_country'):
256 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
257 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
258 val['country_id'] = country_id
259 val['state_id'] = state_id
260 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address, context)
261 dict_val = dict(zip(fields,datas))
263 address_obj.write(cr, uid, address_ids, dict_val)
265 new_address_id = address_obj.create(cr,uid, dict_val)
266 return new_address_id
269 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
270 address_obj = sugar_obj.pool.get('res.partner.address')
271 new_address_id = False
272 if type == 'invoice':
273 type_address = 'billing'
275 type_address = 'shipping'
277 map_partner_address.update({
278 'street': type_address + '_address_street',
279 'zip': type_address +'_address_postalcode',
280 'city': type_address +'_address_city',
281 'country_id': 'country_id',
285 if val.get(type_address +'_address_country'):
286 country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
287 state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
288 val['country_id'] = country_id
289 val['state_id'] = state
290 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
291 #Convert To list into Dictionary(Key, val). value pair.
292 dict_val = dict(zip(fields,datas))
293 new_address_id = address_obj.create(cr,uid, dict_val)
294 return new_address_id
296 def get_address(sugar_obj, cr, uid, val, context=None):
297 map_partner_address={}
299 address_obj = sugar_obj.pool.get('res.partner.address')
300 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
304 map_partner_address = {
307 'partner_id/id': 'account_id',
308 'phone': 'phone_office',
309 'mobile': 'phone_mobile',
313 if val.get('billing_address_street'):
314 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
316 if val.get('shipping_address_street'):
317 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
321 def import_partners(sugar_obj, cr, uid, context=None):
327 'website': 'website',
328 'user_id/id': 'assigned_user_id',
330 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
331 'customer': 'customer',
332 'supplier': 'supplier',
334 partner_obj = sugar_obj.pool.get('res.partner')
335 address_obj = sugar_obj.pool.get('res.partner.address')
336 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
337 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
338 for val in sugar_data:
339 add_id = get_address(sugar_obj, cr, uid, val, context)
340 val['customer'] = '1'
341 val['supplier'] = '0'
342 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner, context)
343 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
344 for address in address_obj.browse(cr,uid,add_id):
345 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
347 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
350 def get_category(sugar_obj, cr, uid, model, name, context=None):
353 fields = ['name', 'object_id']
355 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)], context)
357 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
359 alarm_dict = {'60': '1 minute before',
360 '300': '5 minutes before',
361 '600': '10 minutes before',
362 '900': '15 minutes before',
363 '1800':'30 minutes before',
364 '3600': '1 hour before',
367 alarm_obj = sugar_obj.pool.get('res.alarm')
368 if alarm_dict.get(val):
369 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
370 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
374 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
378 state_dict = {'status': #field in the sugarcrm database
379 { #Mapping of sugarcrm stage : openerp meeting stage
384 state = state_dict['status'].get(val, '')
387 def get_task_state(sugar_obj, cr, uid, val, context=None):
391 state_dict = {'status': #field in the sugarcrm database
392 { #Mapping of sugarcrm stage : openerp meeting stage
393 'Completed' : 'done',
394 'Not Started':'draft',
395 'In Progress': 'open',
396 'Pending Input': 'draft',
399 state = state_dict['status'].get(val, '')
402 def get_project_state(sugar_obj, cr, uid, val,context=None):
406 state_dict = {'status': #field in the sugarcrm database
407 { #Mapping of sugarcrm staus : openerp Projects state
410 'Published': 'close',
412 state = state_dict['status'].get(val, '')
415 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
419 state_dict = {'status': #field in the sugarcrm database
420 { #Mapping of sugarcrm status : openerp Porject Tasks state
421 'Not Started': 'draft',
422 'In Progress': 'open',
424 'Pending Input': 'pending',
425 'Deferred': 'cancelled',
427 state = state_dict['status'].get(val, '')
430 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
434 priority_dict = {'priority': #field in the sugarcrm database
435 { #Mapping of sugarcrm status : openerp Porject Tasks state
440 priority = priority_dict['priority'].get(val, '')
444 def get_account(sugar_obj, cr, uid, val, context=None):
448 partner_address_id = False
449 partner_phone = False
450 partner_mobile = False
451 model_obj = sugar_obj.pool.get('ir.model.data')
452 address_obj = sugar_obj.pool.get('res.partner.address')
453 crm_obj = sugar_obj.pool.get('crm.lead')
454 project_obj = sugar_obj.pool.get('project.project')
455 issue_obj = sugar_obj.pool.get('project.issue')
456 if val.get('parent_type') == 'Accounts':
457 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
459 model = model_obj.browse(cr, uid, model_ids)[0]
460 partner_id = model.res_id
461 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
463 address_id = address_obj.browse(cr, uid, address_ids[0])
464 partner_address_id = address_id.id
465 partner_phone = address_id.phone
466 partner_mobile = address_id.mobile
468 if val.get('parent_type') == 'Contacts':
469 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
470 for model in model_obj.browse(cr, uid, model_ids):
471 partner_address_id = model.res_id
472 address_id = address_obj.browse(cr, uid, partner_address_id)
473 partner_phone = address_id.phone
474 partner_mobile = address_id.mobile
475 partner_id = address_id and address_id.partner_id or False
477 if val.get('parent_type') == 'Opportunities':
478 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
479 for model in model_obj.browse(cr, uid, model_ids):
480 opportunity_id = model.res_id
481 opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
482 partner_id = opportunity_id.partner_id.id
483 partner_address_id = opportunity_id.partner_address_id.id
484 partner_phone = opportunity_id.partner_address_id.phone
485 partner_mobile = opportunity_id.partner_address_id.mobile
487 if val.get('parent_type') == 'Project':
488 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
489 for model in model_obj.browse(cr, uid, model_ids):
490 proj_ids = model.res_id
491 proj_id = project_obj.browse(cr, uid, proj_ids)
492 partner_id = proj_id.partner_id.id
493 partner_address_id = proj_id.contact_id.id
494 partner_phone = proj_id.contact_id.phone
495 partner_mobile = proj_id.contact_id.mobile
497 if val.get('parent_type') == 'Bugs':
498 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
499 for model in model_obj.browse(cr, uid, model_ids):
500 issue_ids = model.res_id
501 issue_id = issue_obj.browse(cr, uid, issue_ids)
502 partner_id = issue_id.partner_id.id
503 partner_address_id = issue_id.partner_address_id.id
504 partner_phone = issue_id.partner_address_id.phone
505 partner_mobile = issue_id.partner_address_id.mobile
507 return partner_id, partner_address_id, partner_phone,partner_mobile
509 def import_documents(sugar_obj, cr, uid, context=None):
512 map_document = {'id' : 'id',
513 'name': 'document_name',
514 'description': 'description',
516 'datas_fname': 'datas_fname',
518 attach_obj = sugar_obj.pool.get('ir.attachment')
519 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
520 sugar_data = sugar.search(PortType,sessionid, 'Documents')
521 for val in sugar_data:
522 file, filename = sugar.attachment_search(PortType, sessionid, 'DocumentRevisions', val.get('document_revision_id'))
524 val['datas_fname'] = filename
525 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
526 attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
529 def import_tasks(sugar_obj, cr, uid, context=None):
532 map_task = {'id' : 'id',
534 'date': ['__datetime__', 'date_start'],
535 'date_deadline' : ['__datetime__', 'date_due'],
536 'user_id/id': 'assigned_user_id',
537 'categ_id/id': 'categ_id/id',
538 'partner_id/.id': 'partner_id/.id',
539 'partner_address_id/.id': 'partner_address_id/.id',
542 meeting_obj = sugar_obj.pool.get('crm.meeting')
543 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
544 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
545 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
546 for val in sugar_data:
547 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
548 if not partner_xml_id:
549 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
550 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
551 val['partner_id/.id'] = partner_id
552 val['partner_address_id/.id'] = partner_address_id
553 val['categ_id/id'] = categ_id
554 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
555 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
556 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
559 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
562 model_obj = sugar_obj.pool.get('ir.model.data')
563 att_obj = sugar_obj.pool.get('calendar.attendee')
564 meeting_obj = sugar_obj.pool.get('crm.meeting')
565 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
566 for user in user_dict:
567 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
568 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
570 user_id = user_resource_id[0].res_id
571 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
573 attendees = attend_ids[0]
575 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
576 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
577 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
579 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
582 def import_meetings(sugar_obj, cr, uid, context=None):
585 map_meeting = {'id' : 'id',
587 'date': ['__datetime__', 'date_start'],
588 'duration': ['duration_hours', 'duration_minutes'],
589 'location': 'location',
590 'alarm_id/.id': 'alarm_id/.id',
591 'user_id/id': 'assigned_user_id',
592 'partner_id/.id':'partner_id/.id',
593 'partner_address_id/.id':'partner_address_id/.id',
596 meeting_obj = sugar_obj.pool.get('crm.meeting')
597 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
598 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
599 for val in sugar_data:
600 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
601 val['partner_id/.id'] = partner_id
602 val['partner_address_id/.id'] = partner_address_id
603 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
604 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
605 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
606 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
607 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
610 def get_calls_state(sugar_obj, cr, uid, val,context=None):
614 state_dict = {'status': #field in the sugarcrm database
615 { #Mapping of sugarcrm stage : openerp calls stage
618 'Not Held': 'pending',
620 state = state_dict['status'].get(val, '')
623 def import_calls(sugar_obj, cr, uid, context=None):
626 map_calls = {'id' : 'id',
628 'date': ['__datetime__', 'date_start'],
629 'duration': ['duration_hours', 'duration_minutes'],
630 'user_id/id': 'assigned_user_id',
631 'partner_id/.id': 'partner_id/.id',
632 'partner_address_id/.id': 'partner_address_id/.id',
633 'categ_id/id': 'categ_id/id',
635 'partner_phone': 'partner_phone',
636 'partner_mobile': 'partner_mobile',
637 'opportunity_id/id': 'opportunity_id/id',
640 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
641 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
642 sugar_data = sugar.search(PortType, sessionid, 'Calls')
643 for val in sugar_data:
644 sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
646 for call_opportunity in sugar_call_leads:
647 val['opportunity_id/id'] = call_opportunity
648 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
649 val['categ_id/id'] = categ_id
650 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
652 val['partner_id/.id'] = partner_id
653 val['partner_address_id/.id'] = partner_address_id
654 val['partner_phone'] = partner_phone
655 val['partner_mobile'] = partner_mobile
656 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
657 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
658 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
661 def import_resources(sugar_obj, cr, uid, context=None):
664 map_resource = {'id' : 'user_hash',
665 'name': ['first_name', 'last_name'],
667 resource_obj = sugar_obj.pool.get('resource.resource')
668 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
669 sugar_data = sugar.search(PortType, sessionid, 'Employees')
670 for val in sugar_data:
671 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
672 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
675 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
679 priority_dict = {'priority': #field in the sugarcrm database
680 { #Mapping of sugarcrm priority : openerp bugs priority
686 priority = priority_dict['priority'].get(val, '')
689 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
693 priority_dict = {'priority': #field in the sugarcrm database
694 { #Mapping of sugarcrm priority : openerp claims priority
699 priority = priority_dict['priority'].get(val, '')
702 def get_bug_state(sugar_obj, cr, uid, val,context=None):
706 state_dict = {'status': #field in the sugarcrm database
707 { #Mapping of sugarcrm status : openerp Bugs state
711 'Pending': 'pending',
712 'Rejected': 'cancel',
714 state = state_dict['status'].get(val, '')
717 def get_claim_state(sugar_obj, cr, uid, val,context=None):
721 state_dict = {'status': #field in the sugarcrm database
722 { #Mapping of sugarcrm status : openerp claim state
726 'Pending Input': 'pending',
727 'Rejected': 'cancel',
728 'Duplicate': 'draft',
730 state = state_dict['status'].get(val, '')
734 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
738 partner_address_id = False
739 partner_phone = False
740 partner_email = False
741 model_obj = sugar_obj.pool.get('ir.model.data')
742 address_obj = sugar_obj.pool.get('res.partner.address')
743 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
745 model = model_obj.browse(cr, uid, model_ids)[0]
746 partner_id = model.res_id
747 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
749 address_id = address_obj.browse(cr, uid, address_ids[0])
750 partner_address_id = address_id.id
751 partner_phone = address_id.phone
752 partner_mobile = address_id.email
753 return partner_id, partner_address_id, partner_phone,partner_email
755 def import_claims(sugar_obj, cr, uid, context=None):
758 map_claim = {'id' : 'id',
760 'date': ['__datetime__', 'date_entered'],
761 'user_id/id': 'assigned_user_id',
762 'priority':'priority',
763 'partner_id/.id': 'partner_id/.id',
764 'partner_address_id/.id': 'partner_address_id/.id',
765 'partner_phone': 'partner_phone',
766 'partner_mobile': 'partner_email',
767 'description': 'description',
770 claim_obj = sugar_obj.pool.get('crm.claim')
771 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
772 sugar_data = sugar.search(PortType, sessionid, 'Cases')
773 for val in sugar_data:
774 partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
775 val['partner_id/.id'] = partner_id
776 val['partner_address_id/.id'] = partner_address_id
777 val['partner_phone'] = partner_phone
778 val['email_from'] = partner_email
779 val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
780 val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
781 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
782 claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
785 def import_bug(sugar_obj, cr, uid, context=None):
788 map_resource = {'id' : 'id',
790 'project_id/.id':'project_id/.id',
791 'categ_id/id': 'categ_id/id',
792 'priority':'priority',
793 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
796 issue_obj = sugar_obj.pool.get('project.issue')
797 project_obj = sugar_obj.pool.get('project.project')
798 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
799 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
800 for val in sugar_data:
801 project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
803 project_id = project_ids[0]
805 project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})
806 val['project_id/.id'] = project_id
807 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
808 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
809 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
810 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
811 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
814 def get_job_id(sugar_obj, cr, uid, val, context=None):
819 return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, [('name', 'ilike', val)], context)
821 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
826 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, [('name', 'ilike', val)], context)
828 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
832 attachment_obj = sugar_obj.pool.get('ir.attachment')
833 partner_obj = sugar_obj.pool.get('res.partner')
834 model_obj = sugar_obj.pool.get('ir.model.data')
835 mailgate_obj = sugar_obj.pool.get('mailgate.message')
836 attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
837 if not attach_ids and Filename:
838 if parent_type == 'Accounts':
839 new_attachment_id = attachment_obj.create(cr, uid, {'name': Filename, 'datas_fname': Filename, 'datas': File, 'res_id': val.get('res_id', False),'res_model': val.get('model',False), 'partner_id': val.get('partner_id/.id')})
841 new_attachment_id = attachment_obj.create(cr, uid, {'name': Filename, 'datas_fname': Filename, 'datas': File, 'res_id': val.get('res_id', False),'res_model': val.get('model',False)})
842 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
843 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
845 if parent_type == 'Accounts':
846 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
848 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
851 def import_history(sugar_obj, cr, uid, context=None):
854 map_attachment = {'id' : 'id',
856 'date': ['__datetime__', 'date_entered'],
857 'user_id/id': 'assigned_user_id',
858 'description': ['__prettyprint__','description', 'description_html'],
861 'partner_id/.id' : 'partner_id/.id',
863 mailgate_obj = sugar_obj.pool.get('mailgate.message')
864 model_obj = sugar_obj.pool.get('ir.model.data')
865 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
866 sugar_data = sugar.search(PortType, sessionid, 'Notes')
867 for val in sugar_data:
868 File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
869 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
871 model = model_obj.browse(cr, uid, model_ids)[0]
872 if model.model == 'res.partner':
873 val['partner_id/.id'] = model.res_id
875 val['res_id'] = model.res_id
876 val['model'] = model.model
877 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)
878 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
879 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
882 def import_employees(sugar_obj, cr, uid, context=None):
885 map_employee = {'id' : 'id',
886 'resource_id/.id': 'resource_id/.id',
887 'name': ['first_name', 'last_name'],
888 'work_phone': 'phone_work',
889 'mobile_phone': 'phone_mobile',
890 'user_id/name': ['first_name', 'last_name'],
891 'address_home_id/.id': 'address_home_id/.id',
892 'notes': 'description',
893 #TODO: Creation of Employee create problem.
894 # 'coach_id/id': 'reports_to_id',
895 'job_id/id': 'job_id/id'
897 employee_obj = sugar_obj.pool.get('hr.employee')
898 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
899 sugar_data = sugar.search(PortType, sessionid, 'Employees')
900 for val in sugar_data:
901 address_id = get_user_address(sugar_obj, cr, uid, val, context)
902 val['address_home_id/.id'] = address_id
903 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
904 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
906 val['resource_id/.id'] = resource_id[0].res_id
907 val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
908 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
909 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
912 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
913 fields = ['shortcut', 'name', 'domain']
914 data = [salutation, salutation, domain]
915 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
917 def import_emails(sugar_obj, cr, uid, context=None):
920 map_emails = {'id': 'id',
922 'date':['__datetime__', 'date_sent'],
923 'email_from': 'from_addr_name',
924 'email_to': 'reply_to_addr',
925 'email_cc': 'cc_addrs_names',
926 'email_bcc': 'bcc_addrs_names',
927 'message_id': 'message_id',
928 'user_id/id': 'assigned_user_id',
929 'description': ['__prettyprint__', 'description', 'description_html'],
932 'partner_id/.id': 'partner_id/.id'
934 mailgate_obj = sugar_obj.pool.get('mailgate.message')
935 model_obj = sugar_obj.pool.get('ir.model.data')
936 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
937 sugar_data = sugar.search(PortType, sessionid, 'Emails')
938 for val in sugar_data:
939 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
940 for model in model_obj.browse(cr, uid, model_ids):
941 if model.model == 'res.partner':
942 val['partner_id/.id'] = model.res_id
944 val['res_id'] = model.res_id
945 val['model'] = model.model
946 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
947 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
950 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
954 partner_invoice_id = False
955 model_obj = sugar_obj.pool.get('ir.model.data')
956 partner_obj = sugar_obj.pool.get('res.partner')
957 partner_address_obj = sugar_obj.pool.get('res.partner.address')
958 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
959 for account_id in sugar_project_account:
960 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
962 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
963 partner_id = partner_obj.browse(cr, uid, model_id).id
964 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
965 partner_invoice_id = address_ids[0]
966 return partner_id, partner_invoice_id
968 def import_projects(sugar_obj, cr, uid, context=None):
971 map_project = {'id': 'id',
973 'date_start': ['__datetime__', 'estimated_start_date'],
974 'date': ['__datetime__', 'estimated_end_date'],
975 'user_id/id': 'assigned_user_id',
976 'partner_id/.id': 'partner_id/.id',
977 'contact_id/.id': 'contact_id/.id',
980 project_obj = sugar_obj.pool.get('project.project')
981 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
982 sugar_data = sugar.search(PortType, sessionid, 'Project')
983 for val in sugar_data:
984 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
985 val['partner_id/.id'] = partner_id
986 val['contact_id/.id'] = partner_invoice_id
987 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
988 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
989 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
993 def import_project_tasks(sugar_obj, cr, uid, context=None):
996 map_project_task = {'id': 'id',
998 'date_start': ['__datetime__', 'date_start'],
999 'date_end': ['__datetime__', 'date_finish'],
1000 'progress': 'progress',
1001 'project_id/name': 'project_name',
1002 'planned_hours': 'planned_hours',
1003 'total_hours': 'total_hours',
1004 'priority': 'priority',
1005 'description': 'description',
1006 'user_id/id': 'assigned_user_id',
1009 task_obj = sugar_obj.pool.get('project.task')
1010 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1011 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1012 for val in sugar_data:
1013 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1014 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1015 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1016 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1019 def import_leads(sugar_obj, cr, uid, context=None):
1024 'name': ['first_name', 'last_name'],
1025 'contact_name': ['first_name', 'last_name'],
1026 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1027 'partner_name': 'account_name',
1028 'email_from': 'email1',
1029 'phone': 'phone_work',
1030 'mobile': 'phone_mobile',
1031 'title.id': 'title.id',
1033 'street': 'primary_address_street',
1034 'street2': 'alt_address_street',
1035 'zip': 'primary_address_postalcode',
1036 'city':'primary_address_city',
1037 'user_id/id' : 'assigned_user_id',
1038 'stage_id/id' : 'stage_id/id',
1042 'referred': 'refered_by',
1044 'type_id/id': 'type_id/id',
1045 'country_id.id': 'country_id.id',
1046 'state_id.id': 'state_id.id'
1048 lead_obj = sugar_obj.pool.get('crm.lead')
1049 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1050 sugar_data = sugar.search(PortType, sessionid, 'Leads')
1051 for val in sugar_data:
1052 if val.get('do_not_call') == '0':
1054 if val.get('opportunity_id'):
1056 if val.get('salutation'):
1057 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1058 val['title/id'] = title_id
1059 val['type'] = 'lead'
1060 val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1061 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1062 val['stage_id/id'] = stage_id
1063 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1064 if val.get('primary_address_country'):
1065 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1066 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1067 val['country_id.id'] = country_id
1068 val['state_id.id'] = state
1069 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1070 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1073 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1076 partner_contact_name = False
1077 partner_contact_email = False
1078 model_obj = sugar_obj.pool.get('ir.model.data')
1079 partner_address_obj = sugar_obj.pool.get('res.partner.address')
1080 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1081 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
1082 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1083 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1084 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1086 for contact in sugar_contact:
1087 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1089 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1090 address_id = partner_address_obj.browse(cr, uid, model_id)
1091 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1092 partner_contact_name = address_id.name
1093 partner_contact_email = address_id.email
1095 partner_contact_name = val.get('account_name')
1096 return partner_contact_name, partner_contact_email
1098 def import_opportunities(sugar_obj, cr, uid, context=None):
1101 map_opportunity = {'id' : 'id',
1103 'probability': 'probability',
1104 'partner_id/name': 'account_name',
1105 'title_action': 'next_step',
1106 'partner_address_id/name': 'partner_address_id/name',
1107 'planned_revenue': 'amount',
1108 'date_deadline': ['__datetime__', 'date_closed'],
1109 'user_id/id' : 'assigned_user_id',
1110 'stage_id/id' : 'stage_id/id',
1112 'categ_id/id': 'categ_id/id',
1113 'email_from': 'email_from'
1115 lead_obj = sugar_obj.pool.get('crm.lead')
1116 partner_obj = sugar_obj.pool.get('res.partner')
1117 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1118 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1119 for val in sugar_data:
1120 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1121 if not partner_xml_id:
1122 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1123 partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1124 val['partner_address_id/name'] = partner_contact_name
1125 val['email_from'] = partner_contact_email
1126 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
1127 val['type'] = 'opportunity'
1128 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1129 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1130 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1133 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1136 fields = ['name', 'type']
1137 name = 'Opportunity_' + sugar_val['sales_stage']
1138 data = [sugar_val['sales_stage'], 'Opportunity']
1139 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1141 MAP_FIELDS = {'Opportunities': #Object Mapping name
1142 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
1143 'process' : import_opportunities,
1146 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
1147 'process' : import_leads,
1150 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
1151 'process' : import_partner_address,
1154 {'dependencies' : ['Users'], #Object to import before this table
1155 'process' : import_partners,
1158 {'dependencies' : [],
1159 'process' : import_users,
1162 {'dependencies' : ['Users'],
1163 'process' : import_documents,
1166 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1167 'process' : import_meetings,
1170 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1171 'process' : import_tasks,
1174 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1175 'process' : import_calls,
1178 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1179 'process' : import_projects,
1182 {'dependencies' : ['Users', 'Projects'],
1183 'process' : import_project_tasks,
1186 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1187 'process' : import_bug,
1190 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
1191 'process' : import_claims,
1194 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1195 'process' : import_emails,
1199 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1200 'process' : import_history,
1203 {'dependencies' : ['Resources', 'Users'],
1204 'process' : import_employees,
1207 {'dependencies' : ['Users'],
1208 'process' : import_resources,
1212 class import_sugarcrm(osv.osv):
1213 """Import SugarCRM DATA"""
1215 _name = "import.sugarcrm"
1216 _description = __doc__
1218 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1219 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1220 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1221 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1222 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1223 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1224 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1225 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1226 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1227 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1228 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1229 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1230 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1231 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1232 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1233 'username': fields.char('User Name', size=64),
1234 'password': fields.char('Password', size=24),
1236 _defaults = {#to be set to true, but easier for debugging
1237 'opportunity': True,
1248 'project_task': True,
1253 def get_key(self, cr, uid, ids, context=None):
1254 """Select Key as For which Module data we want import data."""
1258 for current in self.browse(cr, uid, ids, context):
1259 if current.opportunity:
1260 key_list.append('Opportunities')
1262 key_list.append('Users')
1264 key_list.append('Contacts')
1266 key_list.append('Accounts')
1267 if current.employee:
1268 key_list.append('Employees')
1270 key_list.append('Meetings')
1272 key_list.append('Tasks')
1274 key_list.append('Calls')
1276 key_list.append('Claims')
1278 key_list.append('Emails')
1280 key_list.append('Projects')
1281 if current.project_task:
1282 key_list.append('Project Tasks')
1284 key_list.append('Bugs')
1285 if current.attachment:
1286 key_list.append('Notes')
1287 if current.document:
1288 key_list.append('Documents')
1291 def import_all(self, cr, uid, ids, context=None):
1292 """Import all sugarcrm data into openerp module"""
1295 keys = self.get_key(cr, uid, ids, context)
1296 imported = set() #to invoid importing 2 times the sames modules
1298 if not key in imported:
1299 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1300 MAP_FIELDS[key]['process'](self, cr, uid, context)
1303 obj_model = self.pool.get('ir.model.data')
1304 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1305 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1307 'view_type': 'form',
1308 'view_mode': 'form',
1309 'res_model': 'import.message',
1310 'views': [(resource_id,'form')],
1311 'type': 'ir.actions.act_window',
1315 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1316 for dependency in dep:
1317 if not dependency in imported:
1318 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1319 dict[dependency]['process'](self, cr, uid, context)
1320 imported.add(dependency)