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')])
472 model = model_obj.browse(cr, uid, model_ids)[0]
473 partner_address_id = model.res_id
474 address_id = address_obj.browse(cr, uid, partner_address_id)
475 partner_phone = address_id.phone
476 partner_mobile = address_id.mobile
477 partner_id = address_id and address_id.partner_id.id or False
479 if val.get('parent_type') == 'Opportunities':
480 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
482 model = model_obj.browse(cr, uid, model_ids)[0]
483 opportunity_id = model.res_id
484 opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
485 partner_id = opportunity_id.partner_id.id
486 partner_address_id = opportunity_id.partner_address_id.id
487 partner_phone = opportunity_id.partner_address_id.phone
488 partner_mobile = opportunity_id.partner_address_id.mobile
490 if val.get('parent_type') == 'Project':
491 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
493 model = model_obj.browse(cr, uid, model_ids)[0]
494 proj_ids = model.res_id
495 proj_id = project_obj.browse(cr, uid, proj_ids)
496 partner_id = proj_id.partner_id.id
497 partner_address_id = proj_id.contact_id.id
498 partner_phone = proj_id.contact_id.phone
499 partner_mobile = proj_id.contact_id.mobile
501 if val.get('parent_type') == 'Bugs':
502 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
504 model = model_obj.browse(cr, uid, model_ids)[0]
505 issue_ids = model.res_id
506 issue_id = issue_obj.browse(cr, uid, issue_ids)
507 partner_id = issue_id.partner_id.id
508 partner_address_id = issue_id.partner_address_id.id
509 partner_phone = issue_id.partner_address_id.phone
510 partner_mobile = issue_id.partner_address_id.mobile
511 return partner_id, partner_address_id, partner_phone,partner_mobile
514 def import_documents(sugar_obj, cr, uid, context=None):
517 map_document = {'id' : 'id',
519 'description': 'description',
521 'datas_fname': 'datas_fname',
523 attach_obj = sugar_obj.pool.get('ir.attachment')
524 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
525 sugar_data = sugar.search(PortType,sessionid, 'DocumentRevisions')
526 for val in sugar_data:
527 filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id')
528 f = open(filepath, "r")
531 val['datas'] = base64.encodestring(datas)
532 val['datas_fname'] = val.get('filename')
533 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
534 attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
537 def import_tasks(sugar_obj, cr, uid, context=None):
540 map_task = {'id' : 'id',
542 'date': ['__datetime__', 'date_start'],
543 'date_deadline' : ['__datetime__', 'date_due'],
544 'user_id/id': 'assigned_user_id',
545 'categ_id/id': 'categ_id/id',
546 'partner_id/.id': 'partner_id/.id',
547 'partner_address_id/.id': 'partner_address_id/.id',
550 meeting_obj = sugar_obj.pool.get('crm.meeting')
551 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
552 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
553 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
554 for val in sugar_data:
555 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
556 if not partner_xml_id:
557 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
558 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
559 val['partner_id/.id'] = partner_id
560 val['partner_address_id/.id'] = partner_address_id
561 val['categ_id/id'] = categ_id
562 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
563 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
564 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
567 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
570 model_obj = sugar_obj.pool.get('ir.model.data')
571 att_obj = sugar_obj.pool.get('calendar.attendee')
572 meeting_obj = sugar_obj.pool.get('crm.meeting')
573 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
574 for user in user_dict:
575 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
576 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
578 user_id = user_resource_id[0].res_id
579 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
581 attendees = attend_ids[0]
583 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
584 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
585 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
587 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
590 def import_meetings(sugar_obj, cr, uid, context=None):
593 map_meeting = {'id' : 'id',
595 'date': ['__datetime__', 'date_start'],
596 'duration': ['duration_hours', 'duration_minutes'],
597 'location': 'location',
598 'alarm_id/.id': 'alarm_id/.id',
599 'user_id/id': 'assigned_user_id',
600 'partner_id/.id':'partner_id/.id',
601 'partner_address_id/.id':'partner_address_id/.id',
604 meeting_obj = sugar_obj.pool.get('crm.meeting')
605 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
606 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
607 for val in sugar_data:
608 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
609 val['partner_id/.id'] = partner_id
610 val['partner_address_id/.id'] = partner_address_id
611 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
612 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
613 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
614 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
615 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
618 def get_calls_state(sugar_obj, cr, uid, val,context=None):
622 state_dict = {'status': #field in the sugarcrm database
623 { #Mapping of sugarcrm stage : openerp calls stage
626 'Not Held': 'pending',
628 state = state_dict['status'].get(val, '')
631 def import_calls(sugar_obj, cr, uid, context=None):
634 map_calls = {'id' : 'id',
636 'date': ['__datetime__', 'date_start'],
637 'duration': ['duration_hours', 'duration_minutes'],
638 'user_id/id': 'assigned_user_id',
639 'partner_id/.id': 'partner_id/.id',
640 'partner_address_id/.id': 'partner_address_id/.id',
641 'categ_id/id': 'categ_id/id',
643 'partner_phone': 'partner_phone',
644 'partner_mobile': 'partner_mobile',
645 'opportunity_id/id': 'opportunity_id/id',
648 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
649 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
650 sugar_data = sugar.search(PortType, sessionid, 'Calls')
651 for val in sugar_data:
652 sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
654 for call_opportunity in sugar_call_leads:
655 val['opportunity_id/id'] = call_opportunity
656 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
657 val['categ_id/id'] = categ_id
658 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
659 val['partner_id/.id'] = partner_id
660 val['partner_address_id/.id'] = partner_address_id
661 val['partner_phone'] = partner_phone
662 val['partner_mobile'] = partner_mobile
663 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
664 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
665 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
668 def import_resources(sugar_obj, cr, uid, context=None):
671 map_resource = {'id' : 'user_hash',
672 'name': ['first_name', 'last_name'],
674 resource_obj = sugar_obj.pool.get('resource.resource')
675 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
676 sugar_data = sugar.search(PortType, sessionid, 'Employees')
677 for val in sugar_data:
678 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
679 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
682 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
686 priority_dict = {'priority': #field in the sugarcrm database
687 { #Mapping of sugarcrm priority : openerp bugs priority
693 priority = priority_dict['priority'].get(val, '')
696 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
700 priority_dict = {'priority': #field in the sugarcrm database
701 { #Mapping of sugarcrm priority : openerp claims priority
706 priority = priority_dict['priority'].get(val, '')
709 def get_bug_state(sugar_obj, cr, uid, val,context=None):
713 state_dict = {'status': #field in the sugarcrm database
714 { #Mapping of sugarcrm status : openerp Bugs state
718 'Pending': 'pending',
719 'Rejected': 'cancel',
721 state = state_dict['status'].get(val, '')
724 def get_claim_state(sugar_obj, cr, uid, val,context=None):
728 state_dict = {'status': #field in the sugarcrm database
729 { #Mapping of sugarcrm status : openerp claim state
733 'Pending Input': 'pending',
734 'Rejected': 'cancel',
735 'Duplicate': 'draft',
737 state = state_dict['status'].get(val, '')
741 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
745 partner_address_id = False
746 partner_phone = False
747 partner_email = False
748 model_obj = sugar_obj.pool.get('ir.model.data')
749 address_obj = sugar_obj.pool.get('res.partner.address')
750 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
752 model = model_obj.browse(cr, uid, model_ids)[0]
753 partner_id = model.res_id
754 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
756 address_id = address_obj.browse(cr, uid, address_ids[0])
757 partner_address_id = address_id.id
758 partner_phone = address_id.phone
759 partner_mobile = address_id.email
760 return partner_id, partner_address_id, partner_phone,partner_email
762 def import_claims(sugar_obj, cr, uid, context=None):
765 map_claim = {'id' : 'id',
767 'date': ['__datetime__', 'date_entered'],
768 'user_id/id': 'assigned_user_id',
769 'priority':'priority',
770 'partner_id/.id': 'partner_id/.id',
771 'partner_address_id/.id': 'partner_address_id/.id',
772 'partner_phone': 'partner_phone',
773 'partner_mobile': 'partner_email',
774 'description': 'description',
777 claim_obj = sugar_obj.pool.get('crm.claim')
778 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
779 sugar_data = sugar.search(PortType, sessionid, 'Cases')
780 for val in sugar_data:
781 partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
782 val['partner_id/.id'] = partner_id
783 val['partner_address_id/.id'] = partner_address_id
784 val['partner_phone'] = partner_phone
785 val['email_from'] = partner_email
786 val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
787 val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
788 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
789 claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
792 def import_bug(sugar_obj, cr, uid, context=None):
795 map_resource = {'id' : 'id',
797 'project_id/.id':'project_id/.id',
798 'categ_id/id': 'categ_id/id',
799 'priority':'priority',
800 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
803 issue_obj = sugar_obj.pool.get('project.issue')
804 project_obj = sugar_obj.pool.get('project.project')
805 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
806 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
807 for val in sugar_data:
808 project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
810 project_id = project_ids[0]
812 project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})
813 val['project_id/.id'] = project_id
814 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
815 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
816 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
817 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
818 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
821 def get_job_id(sugar_obj, cr, uid, val, context=None):
826 return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, [('name', 'ilike', val)], context)
828 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
833 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, [('name', 'ilike', val)], context)
835 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
839 attachment_obj = sugar_obj.pool.get('ir.attachment')
840 partner_obj = sugar_obj.pool.get('res.partner')
841 model_obj = sugar_obj.pool.get('ir.model.data')
842 mailgate_obj = sugar_obj.pool.get('mailgate.message')
843 attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
844 if not attach_ids and Filename:
845 if parent_type == 'Accounts':
846 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')})
848 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)})
849 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
850 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
852 if parent_type == 'Accounts':
853 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
855 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
858 def import_history(sugar_obj, cr, uid, context=None):
861 map_attachment = {'id' : 'id',
863 'date': ['__datetime__', 'date_entered'],
864 'user_id/id': 'assigned_user_id',
865 'description': ['__prettyprint__','description', 'description_html'],
868 'partner_id/.id' : 'partner_id/.id',
870 mailgate_obj = sugar_obj.pool.get('mailgate.message')
871 model_obj = sugar_obj.pool.get('ir.model.data')
872 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
873 sugar_data = sugar.search(PortType, sessionid, 'Notes')
874 for val in sugar_data:
875 File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
876 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
878 model = model_obj.browse(cr, uid, model_ids)[0]
879 if model.model == 'res.partner':
880 val['partner_id/.id'] = model.res_id
882 val['res_id'] = model.res_id
883 val['model'] = model.model
884 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)
885 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
886 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
889 def import_employees(sugar_obj, cr, uid, context=None):
892 map_employee = {'id' : 'user_hash',
893 'resource_id/.id': 'resource_id/.id',
894 'name': ['first_name', 'last_name'],
895 'work_phone': 'phone_work',
896 'mobile_phone': 'phone_mobile',
897 'user_id/name': ['first_name', 'last_name'],
898 'address_home_id/.id': 'address_home_id/.id',
899 'notes': 'description',
900 #TODO: Creation of Employee create problem.
901 # 'coach_id/id': 'reports_to_id',
902 'job_id/id': 'job_id/id'
904 employee_obj = sugar_obj.pool.get('hr.employee')
905 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
906 sugar_data = sugar.search(PortType, sessionid, 'Employees')
907 for val in sugar_data:
908 address_id = get_user_address(sugar_obj, cr, uid, val, context)
909 val['address_home_id/.id'] = address_id
910 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
911 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
913 val['resource_id/.id'] = resource_id[0].res_id
914 val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
915 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
916 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
919 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
920 fields = ['shortcut', 'name', 'domain']
921 data = [salutation, salutation, domain]
922 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
924 def import_emails(sugar_obj, cr, uid, context=None):
927 map_emails = {'id': 'id',
929 'date':['__datetime__', 'date_sent'],
930 'email_from': 'from_addr_name',
931 'email_to': 'reply_to_addr',
932 'email_cc': 'cc_addrs_names',
933 'email_bcc': 'bcc_addrs_names',
934 'message_id': 'message_id',
935 'user_id/id': 'assigned_user_id',
936 'description': ['__prettyprint__', 'description', 'description_html'],
939 'partner_id/.id': 'partner_id/.id'
941 mailgate_obj = sugar_obj.pool.get('mailgate.message')
942 model_obj = sugar_obj.pool.get('ir.model.data')
943 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
944 sugar_data = sugar.search(PortType, sessionid, 'Emails')
945 for val in sugar_data:
946 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
947 for model in model_obj.browse(cr, uid, model_ids):
948 if model.model == 'res.partner':
949 val['partner_id/.id'] = model.res_id
951 val['res_id'] = model.res_id
952 val['model'] = model.model
953 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
954 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
957 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
961 partner_invoice_id = False
962 model_obj = sugar_obj.pool.get('ir.model.data')
963 partner_obj = sugar_obj.pool.get('res.partner')
964 partner_address_obj = sugar_obj.pool.get('res.partner.address')
965 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
966 for account_id in sugar_project_account:
967 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
969 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
970 partner_id = partner_obj.browse(cr, uid, model_id).id
971 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
972 partner_invoice_id = address_ids[0]
973 return partner_id, partner_invoice_id
975 def import_projects(sugar_obj, cr, uid, context=None):
978 map_project = {'id': 'id',
980 'date_start': ['__datetime__', 'estimated_start_date'],
981 'date': ['__datetime__', 'estimated_end_date'],
982 'user_id/id': 'assigned_user_id',
983 'partner_id/.id': 'partner_id/.id',
984 'contact_id/.id': 'contact_id/.id',
987 project_obj = sugar_obj.pool.get('project.project')
988 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
989 sugar_data = sugar.search(PortType, sessionid, 'Project')
990 for val in sugar_data:
991 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
992 val['partner_id/.id'] = partner_id
993 val['contact_id/.id'] = partner_invoice_id
994 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
995 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
996 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1000 def import_project_tasks(sugar_obj, cr, uid, context=None):
1003 map_project_task = {'id': 'id',
1005 'date_start': ['__datetime__', 'date_start'],
1006 'date_end': ['__datetime__', 'date_finish'],
1007 'progress': 'progress',
1008 'project_id/name': 'project_name',
1009 'planned_hours': 'planned_hours',
1010 'total_hours': 'total_hours',
1011 'priority': 'priority',
1012 'description': 'description',
1013 'user_id/id': 'assigned_user_id',
1016 task_obj = sugar_obj.pool.get('project.task')
1017 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1018 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1019 for val in sugar_data:
1020 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1021 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1022 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1023 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1026 def import_leads(sugar_obj, cr, uid, context=None):
1031 'name': ['first_name', 'last_name'],
1032 'contact_name': ['first_name', 'last_name'],
1033 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1034 'partner_name': 'account_name',
1035 'email_from': 'email1',
1036 'phone': 'phone_work',
1037 'mobile': 'phone_mobile',
1038 'title.id': 'title.id',
1040 'street': 'primary_address_street',
1041 'street2': 'alt_address_street',
1042 'zip': 'primary_address_postalcode',
1043 'city':'primary_address_city',
1044 'user_id/id' : 'assigned_user_id',
1045 'stage_id/id' : 'stage_id/id',
1049 'referred': 'refered_by',
1051 'type_id/id': 'type_id/id',
1052 'country_id.id': 'country_id.id',
1053 'state_id.id': 'state_id.id'
1055 lead_obj = sugar_obj.pool.get('crm.lead')
1056 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1057 sugar_data = sugar.search(PortType, sessionid, 'Leads')
1058 for val in sugar_data:
1059 if val.get('do_not_call') == '0':
1061 if val.get('opportunity_id'):
1063 if val.get('salutation'):
1064 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1065 val['title/id'] = title_id
1066 val['type'] = 'lead'
1067 val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1068 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1069 val['stage_id/id'] = stage_id
1070 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1071 if val.get('primary_address_country'):
1072 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1073 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1074 val['country_id.id'] = country_id
1075 val['state_id.id'] = state
1076 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1077 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1080 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1083 partner_contact_name = False
1084 partner_contact_email = False
1085 model_obj = sugar_obj.pool.get('ir.model.data')
1086 partner_address_obj = sugar_obj.pool.get('res.partner.address')
1087 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1088 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
1089 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1090 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1091 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1093 for contact in sugar_contact:
1094 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1096 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1097 address_id = partner_address_obj.browse(cr, uid, model_id)
1098 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1099 partner_contact_name = address_id.name
1100 partner_contact_email = address_id.email
1102 partner_contact_name = val.get('account_name')
1103 return partner_contact_name, partner_contact_email
1105 def import_opportunities(sugar_obj, cr, uid, context=None):
1108 map_opportunity = {'id' : 'id',
1110 'probability': 'probability',
1111 'partner_id/name': 'account_name',
1112 'title_action': 'next_step',
1113 'partner_address_id/name': 'partner_address_id/name',
1114 'planned_revenue': 'amount',
1115 'date_deadline': ['__datetime__', 'date_closed'],
1116 'user_id/id' : 'assigned_user_id',
1117 'stage_id/id' : 'stage_id/id',
1119 'categ_id/id': 'categ_id/id',
1120 'email_from': 'email_from'
1122 lead_obj = sugar_obj.pool.get('crm.lead')
1123 partner_obj = sugar_obj.pool.get('res.partner')
1124 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1125 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1126 for val in sugar_data:
1127 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1128 if not partner_xml_id:
1129 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1130 partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1131 val['partner_address_id/name'] = partner_contact_name
1132 val['email_from'] = partner_contact_email
1133 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
1134 val['type'] = 'opportunity'
1135 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1136 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1137 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1140 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1143 fields = ['name', 'type']
1144 name = 'Opportunity_' + sugar_val['sales_stage']
1145 data = [sugar_val['sales_stage'], 'Opportunity']
1146 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1148 MAP_FIELDS = {'Opportunities': #Object Mapping name
1149 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
1150 'process' : import_opportunities,
1153 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
1154 'process' : import_leads,
1157 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
1158 'process' : import_partner_address,
1161 {'dependencies' : ['Users'], #Object to import before this table
1162 'process' : import_partners,
1165 {'dependencies' : [],
1166 'process' : import_users,
1169 {'dependencies' : ['Users'],
1170 'process' : import_documents,
1173 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Projects', 'Opportunities', 'Tasks'],
1174 'process' : import_meetings,
1177 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1178 'process' : import_tasks,
1181 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1182 'process' : import_calls,
1185 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1186 'process' : import_projects,
1189 {'dependencies' : ['Users', 'Projects'],
1190 'process' : import_project_tasks,
1193 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1194 'process' : import_bug,
1197 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
1198 'process' : import_claims,
1201 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1202 'process' : import_emails,
1206 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1207 'process' : import_history,
1210 {'dependencies' : ['Resources', 'Users'],
1211 'process' : import_employees,
1214 {'dependencies' : ['Users'],
1215 'process' : import_resources,
1219 class import_sugarcrm(osv.osv):
1220 """Import SugarCRM DATA"""
1222 _name = "import.sugarcrm"
1223 _description = __doc__
1225 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1226 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1227 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1228 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1229 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1230 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1231 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1232 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1233 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1234 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1235 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1236 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1237 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1238 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1239 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1240 'username': fields.char('User Name', size=64),
1241 'password': fields.char('Password', size=24),
1243 _defaults = {#to be set to true, but easier for debugging
1244 'opportunity': True,
1255 'project_task': True,
1260 def get_key(self, cr, uid, ids, context=None):
1261 """Select Key as For which Module data we want import data."""
1265 for current in self.browse(cr, uid, ids, context):
1266 if current.opportunity:
1267 key_list.append('Opportunities')
1269 key_list.append('Users')
1271 key_list.append('Contacts')
1273 key_list.append('Accounts')
1274 if current.employee:
1275 key_list.append('Employees')
1277 key_list.append('Meetings')
1279 key_list.append('Tasks')
1281 key_list.append('Calls')
1283 key_list.append('Claims')
1285 key_list.append('Emails')
1287 key_list.append('Projects')
1288 if current.project_task:
1289 key_list.append('Project Tasks')
1291 key_list.append('Bugs')
1292 if current.attachment:
1293 key_list.append('Notes')
1294 if current.document:
1295 key_list.append('Documents')
1298 def import_all(self, cr, uid, ids, context=None):
1299 """Import all sugarcrm data into openerp module"""
1302 keys = self.get_key(cr, uid, ids, context)
1303 imported = set() #to invoid importing 2 times the sames modules
1305 if not key in imported:
1306 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1307 MAP_FIELDS[key]['process'](self, cr, uid, context)
1310 obj_model = self.pool.get('ir.model.data')
1311 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1312 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1314 'view_type': 'form',
1315 'view_mode': 'form',
1316 'res_model': 'import.message',
1317 'views': [(resource_id,'form')],
1318 'type': 'ir.actions.act_window',
1322 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1323 for dependency in dep:
1324 if not dependency in imported:
1325 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1326 dict[dependency]['process'](self, cr, uid, context)
1327 imported.add(dependency)