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 _
28 pp = pprint.PrettyPrinter(indent=4)
30 OPENERP_FIEDS_MAPS = {'Leads': 'crm.lead',
31 'Opportunities': 'crm.lead',
32 'Contacts': 'res.partner.address',
33 'Accounts': 'res.partner',
34 'Resources': 'resource.resource',
36 'Meetings': 'crm.meeting',
37 'Calls': 'crm.phonecall',
38 'Claims': 'crm.claim',
39 'Employee': 'hr.employee',
40 'Project': 'project.project',
41 'ProjectTask': 'project.task',
42 'Bugs': 'project.issue',
43 'Documents': 'ir.attachment',
47 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
48 model_obj = obj.pool.get('ir.model.data')
49 return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
51 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context):
53 This function create the mapping between an already existing data and the similar data of sugarcrm
54 @param res_model: model of the mapped object
55 @param sugar_id: external sugar id
56 @param id: id in the database
58 @return : the xml_id or sugar_id
60 ir_model_data_obj = obj.pool.get('ir.model.data')
61 id = ir_model_data_obj._update(cr, uid, res_model,
62 'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
63 noupdate=True, res_id=id, context=context)
66 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False, context=None):
68 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
69 use import_data that will take care to create/update or do nothing with the data
70 this method return the xml_id
71 @param fields: list of fields needed to create the object without id
72 @param data: the list of the data, in the same order as the field
73 ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
74 @param model: the openerp's model of the create/update object
75 @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
76 @param unique_name: the name of the object that we want to create/update get the id
77 @param domain_search : the domain that should find the unique existing record
79 @return: the xml_id of the ressources
81 domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
82 obj = sugar_obj.pool.get(model)
83 xml_id = generate_xml_id(name, table)
85 def mapped_id_if_exist(obj, domain, xml_id):
86 ids = obj.search(cr, uid, domain, context=context)
88 return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
92 mapped_id_if_exist(obj, domain_search, xml_id)
95 obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
98 def generate_xml_id(name, table):
100 @param name: name of the object, has to be unique in for a given table
101 @param table : table where the record we want generate come from
102 @return: a unique xml id for record, the xml_id will be the same given the same table and same name
103 To be used to avoid duplication of data that don't have ids
105 sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
106 name = name.replace('.', '_')
107 return sugar_instance + "_" + table + "_" + name
109 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
110 models = sugar_obj.pool.get(model)
111 model_code = sugar_val[0:2]
112 all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())])
113 output = sorted([(o.id, o.name)
114 for o in models.browse(cr, uid, all_model_ids, context=context)],
118 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
119 """Get states or create new state"""
121 res_country_state_obj = sugar_obj.pool.get('res.country.state')
123 state = get_all(sugar_obj,
124 cr, uid, 'res.country.state', sugar_val, context=context)
126 state_id = state and state[0][0]
128 state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
131 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
132 """Get Country or Create new country"""
133 res_country_obj = sugar_obj.pool.get('res.country')
135 country_code = sugar_country_val[0:2]
136 country = get_all(sugar_obj,
137 cr, uid, 'res.country', sugar_country_val, context=context)
139 country_id = country and country[0][0]
141 country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})
144 def import_partner_address(sugar_obj, cr, uid, context=None):
147 map_partner_address = {
149 'name': ['first_name', 'last_name'],
150 'partner_id/id': 'account_id',
151 'phone': 'phone_work',
152 'mobile': 'phone_mobile',
155 'street': 'primary_address_street',
156 'zip': 'primary_address_postalcode',
157 'city': 'primary_address_city',
158 'country_id.id': 'country_id.id',
159 'state_id.id': 'state_id.id',
163 address_obj = sugar_obj.pool.get('res.partner.address')
164 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
165 sugar_data = sugar.search(PortType, sessionid, 'Contacts')
166 for val in sugar_data:
167 val['type'] = 'contact'
168 contact_emails = sugar.contact_emails_search(PortType, context.get('username', ''), context.get('password', ''), email_address=val.get('email1'))
169 val['email'] = (','.join(map(lambda x : x, contact_emails)))
170 if val.get('primary_address_country'):
171 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
172 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
173 val['country_id.id'] = country_id
174 val['state_id.id'] = state
175 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
176 address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
180 def import_users(sugar_obj, cr, uid, context=None):
183 'name': ['first_name', 'last_name'],
184 'login': 'user_name',
185 'context_lang' : 'context_lang',
186 'password' : 'password',
188 'context_department_id.id': 'context_department_id.id',
191 def get_users_department(sugar_obj, cr, uid, val, context=None):
192 department_id = False
193 department_obj = sugar_obj.pool.get('hr.department')
194 department_ids = department_obj.search(cr, uid, [('name', '=', val)])
196 department_id = department_ids[0]
198 department_id = department_obj.create(cr, uid, {'name': val})
203 department_id = False
205 user_obj = sugar_obj.pool.get('res.users')
206 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
207 sugar_data = sugar.search(PortType,sessionid, 'Users')
208 for val in sugar_data:
209 user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
211 val['.id'] = str(user_ids[0])
213 val['password'] = 'sugarcrm' #default password for all user
214 department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
215 val['context_department_id.id'] = department_id
216 val['context_lang'] = context.get('lang','en_US')
217 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user, context)
218 #All data has to be imported separatly because they don't have the same field
219 user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
222 def get_lead_status(sugar_obj, cr, uid, sugar_val,context=None):
225 fields = ['name', 'type']
226 name = 'lead_' + sugar_val['status']
227 data = [sugar_val['status'], 'lead']
228 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_lead_stage', name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val['status'])], context)
230 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
234 state_dict = {'status': #field in the sugarcrm database
235 { #Mapping of sugarcrm stage : openerp opportunity stage
238 'In Progress': 'open',
239 'Recycled': 'cancel',
242 state = state_dict['status'].get(sugar_val['status'], '')
245 def get_user_address(sugar_obj, cr, uid, val, context=None):
246 address_obj = sugar_obj.pool.get('res.partner.address')
248 'name': ['first_name', 'last_name'],
249 'city': 'address_city',
250 'country_id': 'country_id',
251 'state_id': 'state_id',
252 'street': 'address_street',
253 'zip': 'address_postalcode',
255 address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') or '' +' '+ val.get('last_name'))])
256 if val.get('address_country'):
257 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
258 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
259 val['country_id'] = country_id
260 val['state_id'] = state_id
261 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address, context)
262 dict_val = dict(zip(fields,datas))
264 address_obj.write(cr, uid, address_ids, dict_val)
266 new_address_id = address_obj.create(cr,uid, dict_val)
267 return new_address_id
270 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
271 address_obj = sugar_obj.pool.get('res.partner.address')
272 new_address_id = False
273 if type == 'invoice':
274 type_address = 'billing'
276 type_address = 'shipping'
278 map_partner_address.update({
279 'street': type_address + '_address_street',
280 'zip': type_address +'_address_postalcode',
281 'city': type_address +'_address_city',
282 'country_id': 'country_id',
286 if val.get(type_address +'_address_country'):
287 country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
288 state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
289 val['country_id'] = country_id
290 val['state_id'] = state
291 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
292 #Convert To list into Dictionary(Key, val). value pair.
293 dict_val = dict(zip(fields,datas))
294 new_address_id = address_obj.create(cr,uid, dict_val)
295 return new_address_id
297 def get_address(sugar_obj, cr, uid, val, context=None):
298 map_partner_address={}
300 address_obj = sugar_obj.pool.get('res.partner.address')
301 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
305 map_partner_address = {
308 'partner_id/id': 'account_id',
309 'phone': 'phone_office',
310 'mobile': 'phone_mobile',
314 if val.get('billing_address_street'):
315 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
317 if val.get('shipping_address_street'):
318 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
322 def import_partners(sugar_obj, cr, uid, context=None):
328 'website': 'website',
329 'user_id/id': 'assigned_user_id',
331 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
332 'customer': 'customer',
333 'supplier': 'supplier',
335 partner_obj = sugar_obj.pool.get('res.partner')
336 address_obj = sugar_obj.pool.get('res.partner.address')
337 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
338 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
339 for val in sugar_data:
340 add_id = get_address(sugar_obj, cr, uid, val, context)
341 val['customer'] = '1'
342 val['supplier'] = '0'
343 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner, context)
344 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
345 for address in address_obj.browse(cr,uid,add_id):
346 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
348 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
351 def get_category(sugar_obj, cr, uid, model, name, context=None):
354 fields = ['name', 'object_id']
356 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)], context)
358 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
360 alarm_dict = {'60': '1 minute before',
361 '300': '5 minutes before',
362 '600': '10 minutes before',
363 '900': '15 minutes before',
364 '1800':'30 minutes before',
365 '3600': '1 hour before',
368 alarm_obj = sugar_obj.pool.get('res.alarm')
369 if alarm_dict.get(val):
370 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
371 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
375 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
379 state_dict = {'status': #field in the sugarcrm database
380 { #Mapping of sugarcrm stage : openerp meeting stage
385 state = state_dict['status'].get(val, '')
388 def get_task_state(sugar_obj, cr, uid, val, context=None):
392 state_dict = {'status': #field in the sugarcrm database
393 { #Mapping of sugarcrm stage : openerp meeting stage
394 'Completed' : 'done',
395 'Not Started':'draft',
396 'In Progress': 'open',
397 'Pending Input': 'draft',
400 state = state_dict['status'].get(val, '')
403 def get_project_state(sugar_obj, cr, uid, val,context=None):
407 state_dict = {'status': #field in the sugarcrm database
408 { #Mapping of sugarcrm staus : openerp Projects state
411 'Published': 'close',
413 state = state_dict['status'].get(val, '')
416 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
420 state_dict = {'status': #field in the sugarcrm database
421 { #Mapping of sugarcrm status : openerp Porject Tasks state
422 'Not Started': 'draft',
423 'In Progress': 'open',
425 'Pending Input': 'pending',
426 'Deferred': 'cancelled',
428 state = state_dict['status'].get(val, '')
431 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
435 priority_dict = {'priority': #field in the sugarcrm database
436 { #Mapping of sugarcrm status : openerp Porject Tasks state
441 priority = priority_dict['priority'].get(val, '')
445 def get_account(sugar_obj, cr, uid, val, context=None):
449 partner_address_id = False
450 partner_phone = False
451 partner_mobile = False
452 model_obj = sugar_obj.pool.get('ir.model.data')
453 address_obj = sugar_obj.pool.get('res.partner.address')
454 crm_obj = sugar_obj.pool.get('crm.lead')
455 project_obj = sugar_obj.pool.get('project.project')
456 issue_obj = sugar_obj.pool.get('project.issue')
457 if val.get('parent_type') == 'Accounts':
458 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
460 model = model_obj.browse(cr, uid, model_ids)[0]
461 partner_id = model.res_id
462 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
464 address_id = address_obj.browse(cr, uid, address_ids[0])
465 partner_address_id = address_id.id
466 partner_phone = address_id.phone
467 partner_mobile = address_id.mobile
469 if val.get('parent_type') == 'Contacts':
470 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
471 for model in model_obj.browse(cr, uid, model_ids):
472 partner_address_id = model.res_id
473 address_id = address_obj.browse(cr, uid, partner_address_id)
474 partner_phone = address_id.phone
475 partner_mobile = address_id.mobile
476 partner_id = address_id and address_id.partner_id or False
478 if val.get('parent_type') == 'Opportunities':
479 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
480 for model in model_obj.browse(cr, uid, model_ids):
481 opportunity_id = model.res_id
482 opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
483 partner_id = opportunity_id.partner_id.id
484 partner_address_id = opportunity_id.partner_address_id.id
485 partner_phone = opportunity_id.partner_address_id.phone
486 partner_mobile = opportunity_id.partner_address_id.mobile
488 if val.get('parent_type') == 'Project':
489 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
490 for model in model_obj.browse(cr, uid, model_ids):
491 proj_ids = model.res_id
492 proj_id = project_obj.browse(cr, uid, proj_ids)
493 partner_id = proj_id.partner_id.id
494 partner_address_id = proj_id.contact_id.id
495 partner_phone = proj_id.contact_id.phone
496 partner_mobile = proj_id.contact_id.mobile
498 if val.get('parent_type') == 'Bugs':
499 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
500 for model in model_obj.browse(cr, uid, model_ids):
501 issue_ids = model.res_id
502 issue_id = issue_obj.browse(cr, uid, issue_ids)
503 partner_id = issue_id.partner_id.id
504 partner_address_id = issue_id.partner_address_id.id
505 partner_phone = issue_id.partner_address_id.phone
506 partner_mobile = issue_id.partner_address_id.mobile
508 return partner_id, partner_address_id, partner_phone,partner_mobile
511 def import_documents(sugar_obj, cr, uid, context=None):
514 map_document = {'id' : 'id',
516 'description': 'description',
518 'datas_fname': 'datas_fname',
520 attach_obj = sugar_obj.pool.get('ir.attachment')
521 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
522 sugar_data = sugar.search(PortType,sessionid, 'DocumentRevisions')
523 for val in sugar_data:
524 filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id')
525 f = open(filepath, "r")
528 val['datas'] = base64.encodestring(datas)
529 val['datas_fname'] = val.get('filename')
530 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
531 attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
534 def import_tasks(sugar_obj, cr, uid, context=None):
537 map_task = {'id' : 'id',
539 'date': ['__datetime__', 'date_start'],
540 'date_deadline' : ['__datetime__', 'date_due'],
541 'user_id/id': 'assigned_user_id',
542 'categ_id/id': 'categ_id/id',
543 'partner_id/.id': 'partner_id/.id',
544 'partner_address_id/.id': 'partner_address_id/.id',
547 meeting_obj = sugar_obj.pool.get('crm.meeting')
548 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
549 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
550 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
551 for val in sugar_data:
552 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
553 if not partner_xml_id:
554 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
555 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
556 val['partner_id/.id'] = partner_id
557 val['partner_address_id/.id'] = partner_address_id
558 val['categ_id/id'] = categ_id
559 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
560 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
561 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
564 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
567 model_obj = sugar_obj.pool.get('ir.model.data')
568 att_obj = sugar_obj.pool.get('calendar.attendee')
569 meeting_obj = sugar_obj.pool.get('crm.meeting')
570 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
571 for user in user_dict:
572 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
573 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
575 user_id = user_resource_id[0].res_id
576 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
578 attendees = attend_ids[0]
580 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
581 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
582 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
584 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
587 def import_meetings(sugar_obj, cr, uid, context=None):
590 map_meeting = {'id' : 'id',
592 'date': ['__datetime__', 'date_start'],
593 'duration': ['duration_hours', 'duration_minutes'],
594 'location': 'location',
595 'alarm_id/.id': 'alarm_id/.id',
596 'user_id/id': 'assigned_user_id',
597 'partner_id/.id':'partner_id/.id',
598 'partner_address_id/.id':'partner_address_id/.id',
601 meeting_obj = sugar_obj.pool.get('crm.meeting')
602 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
603 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
604 for val in sugar_data:
605 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
606 val['partner_id/.id'] = partner_id
607 val['partner_address_id/.id'] = partner_address_id
608 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
609 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
610 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
611 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
612 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
615 def get_calls_state(sugar_obj, cr, uid, val,context=None):
619 state_dict = {'status': #field in the sugarcrm database
620 { #Mapping of sugarcrm stage : openerp calls stage
623 'Not Held': 'pending',
625 state = state_dict['status'].get(val, '')
628 def import_calls(sugar_obj, cr, uid, context=None):
631 map_calls = {'id' : 'id',
633 'date': ['__datetime__', 'date_start'],
634 'duration': ['duration_hours', 'duration_minutes'],
635 'user_id/id': 'assigned_user_id',
636 'partner_id/.id': 'partner_id/.id',
637 'partner_address_id/.id': 'partner_address_id/.id',
638 'categ_id/id': 'categ_id/id',
640 'partner_phone': 'partner_phone',
641 'partner_mobile': 'partner_mobile',
642 'opportunity_id/id': 'opportunity_id/id',
645 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
646 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
647 sugar_data = sugar.search(PortType, sessionid, 'Calls')
648 for val in sugar_data:
649 sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
651 for call_opportunity in sugar_call_leads:
652 val['opportunity_id/id'] = call_opportunity
653 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
654 val['categ_id/id'] = categ_id
655 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
656 val['partner_id/.id'] = partner_id
657 val['partner_address_id/.id'] = partner_address_id
658 val['partner_phone'] = partner_phone
659 val['partner_mobile'] = partner_mobile
660 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
661 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
662 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
665 def import_resources(sugar_obj, cr, uid, context=None):
668 map_resource = {'id' : 'user_hash',
669 'name': ['first_name', 'last_name'],
671 resource_obj = sugar_obj.pool.get('resource.resource')
672 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
673 sugar_data = sugar.search(PortType, sessionid, 'Employees')
674 for val in sugar_data:
675 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
676 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
679 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
683 priority_dict = {'priority': #field in the sugarcrm database
684 { #Mapping of sugarcrm priority : openerp bugs priority
690 priority = priority_dict['priority'].get(val, '')
693 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
697 priority_dict = {'priority': #field in the sugarcrm database
698 { #Mapping of sugarcrm priority : openerp claims priority
703 priority = priority_dict['priority'].get(val, '')
706 def get_bug_state(sugar_obj, cr, uid, val,context=None):
710 state_dict = {'status': #field in the sugarcrm database
711 { #Mapping of sugarcrm status : openerp Bugs state
715 'Pending': 'pending',
716 'Rejected': 'cancel',
718 state = state_dict['status'].get(val, '')
721 def get_claim_state(sugar_obj, cr, uid, val,context=None):
725 state_dict = {'status': #field in the sugarcrm database
726 { #Mapping of sugarcrm status : openerp claim state
730 'Pending Input': 'pending',
731 'Rejected': 'cancel',
732 'Duplicate': 'draft',
734 state = state_dict['status'].get(val, '')
738 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
742 partner_address_id = False
743 partner_phone = False
744 partner_email = False
745 model_obj = sugar_obj.pool.get('ir.model.data')
746 address_obj = sugar_obj.pool.get('res.partner.address')
747 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
749 model = model_obj.browse(cr, uid, model_ids)[0]
750 partner_id = model.res_id
751 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
753 address_id = address_obj.browse(cr, uid, address_ids[0])
754 partner_address_id = address_id.id
755 partner_phone = address_id.phone
756 partner_mobile = address_id.email
757 return partner_id, partner_address_id, partner_phone,partner_email
759 def import_claims(sugar_obj, cr, uid, context=None):
762 map_claim = {'id' : 'id',
764 'date': ['__datetime__', 'date_entered'],
765 'user_id/id': 'assigned_user_id',
766 'priority':'priority',
767 'partner_id/.id': 'partner_id/.id',
768 'partner_address_id/.id': 'partner_address_id/.id',
769 'partner_phone': 'partner_phone',
770 'partner_mobile': 'partner_email',
771 'description': 'description',
774 claim_obj = sugar_obj.pool.get('crm.claim')
775 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
776 sugar_data = sugar.search(PortType, sessionid, 'Cases')
777 for val in sugar_data:
778 partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
779 val['partner_id/.id'] = partner_id
780 val['partner_address_id/.id'] = partner_address_id
781 val['partner_phone'] = partner_phone
782 val['email_from'] = partner_email
783 val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
784 val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
785 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
786 claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
789 def import_bug(sugar_obj, cr, uid, context=None):
792 map_resource = {'id' : 'id',
794 'project_id/.id':'project_id/.id',
795 'categ_id/id': 'categ_id/id',
796 'priority':'priority',
797 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
800 issue_obj = sugar_obj.pool.get('project.issue')
801 project_obj = sugar_obj.pool.get('project.project')
802 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
803 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
804 for val in sugar_data:
805 project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
807 project_id = project_ids[0]
809 project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})
810 val['project_id/.id'] = project_id
811 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
812 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
813 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
814 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
815 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
818 def get_job_id(sugar_obj, cr, uid, val, context=None):
823 return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, [('name', 'ilike', val)], context)
825 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
830 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, [('name', 'ilike', val)], context)
832 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
836 attachment_obj = sugar_obj.pool.get('ir.attachment')
837 partner_obj = sugar_obj.pool.get('res.partner')
838 model_obj = sugar_obj.pool.get('ir.model.data')
839 mailgate_obj = sugar_obj.pool.get('mailgate.message')
840 attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
841 if not attach_ids and Filename:
842 if parent_type == 'Accounts':
843 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')})
845 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)})
846 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
847 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
849 if parent_type == 'Accounts':
850 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
852 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
855 def import_history(sugar_obj, cr, uid, context=None):
858 map_attachment = {'id' : 'id',
860 'date': ['__datetime__', 'date_entered'],
861 'user_id/id': 'assigned_user_id',
862 'description': ['__prettyprint__','description', 'description_html'],
865 'partner_id/.id' : 'partner_id/.id',
867 mailgate_obj = sugar_obj.pool.get('mailgate.message')
868 model_obj = sugar_obj.pool.get('ir.model.data')
869 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
870 sugar_data = sugar.search(PortType, sessionid, 'Notes')
871 for val in sugar_data:
872 File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
873 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
875 model = model_obj.browse(cr, uid, model_ids)[0]
876 if model.model == 'res.partner':
877 val['partner_id/.id'] = model.res_id
879 val['res_id'] = model.res_id
880 val['model'] = model.model
881 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)
882 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
883 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
886 def import_employees(sugar_obj, cr, uid, context=None):
889 map_employee = {'id' : 'user_hash',
890 'resource_id/.id': 'resource_id/.id',
891 'name': ['first_name', 'last_name'],
892 'work_phone': 'phone_work',
893 'mobile_phone': 'phone_mobile',
894 'user_id/name': ['first_name', 'last_name'],
895 'address_home_id/.id': 'address_home_id/.id',
896 'notes': 'description',
897 #TODO: Creation of Employee create problem.
898 # 'coach_id/id': 'reports_to_id',
899 'job_id/id': 'job_id/id'
901 employee_obj = sugar_obj.pool.get('hr.employee')
902 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
903 sugar_data = sugar.search(PortType, sessionid, 'Employees')
904 for val in sugar_data:
905 address_id = get_user_address(sugar_obj, cr, uid, val, context)
906 val['address_home_id/.id'] = address_id
907 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
908 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
910 val['resource_id/.id'] = resource_id[0].res_id
911 val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
912 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
913 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
916 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
917 fields = ['shortcut', 'name', 'domain']
918 data = [salutation, salutation, domain]
919 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
921 def import_emails(sugar_obj, cr, uid, context=None):
924 map_emails = {'id': 'id',
926 'date':['__datetime__', 'date_sent'],
927 'email_from': 'from_addr_name',
928 'email_to': 'reply_to_addr',
929 'email_cc': 'cc_addrs_names',
930 'email_bcc': 'bcc_addrs_names',
931 'message_id': 'message_id',
932 'user_id/id': 'assigned_user_id',
933 'description': ['__prettyprint__', 'description', 'description_html'],
936 'partner_id/.id': 'partner_id/.id'
938 mailgate_obj = sugar_obj.pool.get('mailgate.message')
939 model_obj = sugar_obj.pool.get('ir.model.data')
940 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
941 sugar_data = sugar.search(PortType, sessionid, 'Emails')
942 for val in sugar_data:
943 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
944 for model in model_obj.browse(cr, uid, model_ids):
945 if model.model == 'res.partner':
946 val['partner_id/.id'] = model.res_id
948 val['res_id'] = model.res_id
949 val['model'] = model.model
950 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
951 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
954 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
958 partner_invoice_id = False
959 model_obj = sugar_obj.pool.get('ir.model.data')
960 partner_obj = sugar_obj.pool.get('res.partner')
961 partner_address_obj = sugar_obj.pool.get('res.partner.address')
962 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
963 for account_id in sugar_project_account:
964 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
966 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
967 partner_id = partner_obj.browse(cr, uid, model_id).id
968 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
969 partner_invoice_id = address_ids[0]
970 return partner_id, partner_invoice_id
972 def import_projects(sugar_obj, cr, uid, context=None):
975 map_project = {'id': 'id',
977 'date_start': ['__datetime__', 'estimated_start_date'],
978 'date': ['__datetime__', 'estimated_end_date'],
979 'user_id/id': 'assigned_user_id',
980 'partner_id/.id': 'partner_id/.id',
981 'contact_id/.id': 'contact_id/.id',
984 project_obj = sugar_obj.pool.get('project.project')
985 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
986 sugar_data = sugar.search(PortType, sessionid, 'Project')
987 for val in sugar_data:
988 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
989 val['partner_id/.id'] = partner_id
990 val['contact_id/.id'] = partner_invoice_id
991 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
992 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
993 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
997 def import_project_tasks(sugar_obj, cr, uid, context=None):
1000 map_project_task = {'id': 'id',
1002 'date_start': ['__datetime__', 'date_start'],
1003 'date_end': ['__datetime__', 'date_finish'],
1004 'progress': 'progress',
1005 'project_id/name': 'project_name',
1006 'planned_hours': 'planned_hours',
1007 'total_hours': 'total_hours',
1008 'priority': 'priority',
1009 'description': 'description',
1010 'user_id/id': 'assigned_user_id',
1013 task_obj = sugar_obj.pool.get('project.task')
1014 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1015 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1016 for val in sugar_data:
1017 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1018 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1019 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1020 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1023 def import_leads(sugar_obj, cr, uid, context=None):
1028 'name': ['first_name', 'last_name'],
1029 'contact_name': ['first_name', 'last_name'],
1030 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1031 'partner_name': 'account_name',
1032 'email_from': 'email1',
1033 'phone': 'phone_work',
1034 'mobile': 'phone_mobile',
1035 'title.id': 'title.id',
1037 'street': 'primary_address_street',
1038 'street2': 'alt_address_street',
1039 'zip': 'primary_address_postalcode',
1040 'city':'primary_address_city',
1041 'user_id/id' : 'assigned_user_id',
1042 'stage_id/id' : 'stage_id/id',
1046 'referred': 'refered_by',
1048 'type_id/id': 'type_id/id',
1049 'country_id.id': 'country_id.id',
1050 'state_id.id': 'state_id.id'
1052 lead_obj = sugar_obj.pool.get('crm.lead')
1053 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1054 sugar_data = sugar.search(PortType, sessionid, 'Leads')
1055 for val in sugar_data:
1056 if val.get('do_not_call') == '0':
1058 if val.get('opportunity_id'):
1060 if val.get('salutation'):
1061 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1062 val['title/id'] = title_id
1063 val['type'] = 'lead'
1064 val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1065 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1066 val['stage_id/id'] = stage_id
1067 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1068 if val.get('primary_address_country'):
1069 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1070 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1071 val['country_id.id'] = country_id
1072 val['state_id.id'] = state
1073 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1074 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1077 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1080 partner_contact_name = False
1081 partner_contact_email = False
1082 model_obj = sugar_obj.pool.get('ir.model.data')
1083 partner_address_obj = sugar_obj.pool.get('res.partner.address')
1084 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1085 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
1086 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1087 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1088 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1090 for contact in sugar_contact:
1091 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1093 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1094 address_id = partner_address_obj.browse(cr, uid, model_id)
1095 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1096 partner_contact_name = address_id.name
1097 partner_contact_email = address_id.email
1099 partner_contact_name = val.get('account_name')
1100 return partner_contact_name, partner_contact_email
1102 def import_opportunities(sugar_obj, cr, uid, context=None):
1105 map_opportunity = {'id' : 'id',
1107 'probability': 'probability',
1108 'partner_id/name': 'account_name',
1109 'title_action': 'next_step',
1110 'partner_address_id/name': 'partner_address_id/name',
1111 'planned_revenue': 'amount',
1112 'date_deadline': ['__datetime__', 'date_closed'],
1113 'user_id/id' : 'assigned_user_id',
1114 'stage_id/id' : 'stage_id/id',
1116 'categ_id/id': 'categ_id/id',
1117 'email_from': 'email_from'
1119 lead_obj = sugar_obj.pool.get('crm.lead')
1120 partner_obj = sugar_obj.pool.get('res.partner')
1121 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1122 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1123 for val in sugar_data:
1124 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1125 if not partner_xml_id:
1126 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1127 partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1128 val['partner_address_id/name'] = partner_contact_name
1129 val['email_from'] = partner_contact_email
1130 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
1131 val['type'] = 'opportunity'
1132 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1133 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1134 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1137 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1140 fields = ['name', 'type']
1141 name = 'Opportunity_' + sugar_val['sales_stage']
1142 data = [sugar_val['sales_stage'], 'Opportunity']
1143 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1145 MAP_FIELDS = {'Opportunities': #Object Mapping name
1146 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
1147 'process' : import_opportunities,
1150 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
1151 'process' : import_leads,
1154 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
1155 'process' : import_partner_address,
1158 {'dependencies' : ['Users'], #Object to import before this table
1159 'process' : import_partners,
1162 {'dependencies' : [],
1163 'process' : import_users,
1166 {'dependencies' : ['Users'],
1167 'process' : import_documents,
1170 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Projects', 'Opportunities'],
1171 'process' : import_meetings,
1174 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1175 'process' : import_tasks,
1178 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1179 'process' : import_calls,
1182 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1183 'process' : import_projects,
1186 {'dependencies' : ['Users', 'Projects'],
1187 'process' : import_project_tasks,
1190 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1191 'process' : import_bug,
1194 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
1195 'process' : import_claims,
1198 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1199 'process' : import_emails,
1203 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1204 'process' : import_history,
1207 {'dependencies' : ['Resources', 'Users'],
1208 'process' : import_employees,
1211 {'dependencies' : ['Users'],
1212 'process' : import_resources,
1216 class import_sugarcrm(osv.osv):
1217 """Import SugarCRM DATA"""
1219 _name = "import.sugarcrm"
1220 _description = __doc__
1222 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1223 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1224 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1225 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1226 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1227 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1228 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1229 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1230 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1231 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1232 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1233 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1234 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1235 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1236 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1237 'username': fields.char('User Name', size=64),
1238 'password': fields.char('Password', size=24),
1240 _defaults = {#to be set to true, but easier for debugging
1241 'opportunity': True,
1252 'project_task': True,
1257 def get_key(self, cr, uid, ids, context=None):
1258 """Select Key as For which Module data we want import data."""
1262 for current in self.browse(cr, uid, ids, context):
1263 if current.opportunity:
1264 key_list.append('Opportunities')
1266 key_list.append('Users')
1268 key_list.append('Contacts')
1270 key_list.append('Accounts')
1271 if current.employee:
1272 key_list.append('Employees')
1274 key_list.append('Meetings')
1276 key_list.append('Tasks')
1278 key_list.append('Calls')
1280 key_list.append('Claims')
1282 key_list.append('Emails')
1284 key_list.append('Projects')
1285 if current.project_task:
1286 key_list.append('Project Tasks')
1288 key_list.append('Bugs')
1289 if current.attachment:
1290 key_list.append('Notes')
1291 if current.document:
1292 key_list.append('Documents')
1295 def import_all(self, cr, uid, ids, context=None):
1296 """Import all sugarcrm data into openerp module"""
1299 keys = self.get_key(cr, uid, ids, context)
1300 imported = set() #to invoid importing 2 times the sames modules
1302 if not key in imported:
1303 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1304 MAP_FIELDS[key]['process'](self, cr, uid, context)
1307 obj_model = self.pool.get('ir.model.data')
1308 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1309 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1311 'view_type': 'form',
1312 'view_mode': 'form',
1313 'res_model': 'import.message',
1314 'views': [(resource_id,'form')],
1315 'type': 'ir.actions.act_window',
1319 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1320 for dependency in dep:
1321 if not dependency in imported:
1322 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1323 dict[dependency]['process'](self, cr, uid, context)
1324 imported.add(dependency)