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=None):
50 model_obj = obj.pool.get('ir.model.data')
51 return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
53 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context=None):
55 This function create the mapping between an already existing data and the similar data of sugarcrm
56 @param res_model: model of the mapped object
57 @param sugar_id: external sugar id
58 @param id: id in the database
60 @return : the xml_id or sugar_id
64 ir_model_data_obj = obj.pool.get('ir.model.data')
65 id = ir_model_data_obj._update(cr, uid, res_model,
66 'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
67 noupdate=True, res_id=id, context=context)
70 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False, context=None):
72 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
73 use import_data that will take care to create/update or do nothing with the data
74 this method return the xml_id
75 @param fields: list of fields needed to create the object without id
76 @param data: the list of the data, in the same order as the field
77 ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
78 @param model: the openerp's model of the create/update object
79 @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
80 @param unique_name: the name of the object that we want to create/update get the id
81 @param domain_search : the domain that should find the unique existing record
83 @return: the xml_id of the ressources
87 domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
88 obj = sugar_obj.pool.get(model)
89 xml_id = generate_xml_id(name, table)
91 def mapped_id_if_exist(obj, domain, xml_id):
92 ids = obj.search(cr, uid, domain, context=context)
94 return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
98 mapped_id_if_exist(obj, domain_search, xml_id)
101 obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
104 def generate_xml_id(name, table):
106 @param name: name of the object, has to be unique in for a given table
107 @param table : table where the record we want generate come from
108 @return: a unique xml id for record, the xml_id will be the same given the same table and same name
109 To be used to avoid duplication of data that don't have ids
111 sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
112 name = name.replace('.', '_')
113 return sugar_instance + "_" + table + "_" + name
115 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
118 models = sugar_obj.pool.get(model)
119 model_code = sugar_val[0:2]
120 all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())])
121 output = sorted([(o.id, o.name)
122 for o in models.browse(cr, uid, all_model_ids, context=context)],
126 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
127 """Get states or create new state"""
131 res_country_state_obj = sugar_obj.pool.get('res.country.state')
133 state = get_all(sugar_obj,
134 cr, uid, 'res.country.state', sugar_val, context=context)
136 state_id = state and state[0][0]
138 state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
141 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
142 """Get Country or Create new country"""
145 res_country_obj = sugar_obj.pool.get('res.country')
147 country_code = sugar_country_val[0:2]
148 country = get_all(sugar_obj,
149 cr, uid, 'res.country', sugar_country_val, context=context)
151 country_id = country and country[0][0]
153 country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})
156 def import_partner_address(sugar_obj, cr, uid, context=None):
159 map_partner_address = {
161 'name': ['first_name', 'last_name'],
162 'partner_id/id': 'partner_id/id',
163 'phone': 'phone_work',
164 'mobile': 'phone_mobile',
167 'street': 'primary_address_street',
168 'zip': 'primary_address_postalcode',
169 'city': 'primary_address_city',
170 'country_id.id': 'country_id.id',
171 'state_id.id': 'state_id.id',
175 model_obj = sugar_obj.pool.get('ir.model.data')
176 address_obj = sugar_obj.pool.get('res.partner.address')
177 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
178 sugar_data = sugar.search(PortType, sessionid, 'Contacts')
179 for val in sugar_data:
180 account_xml_id = model_obj.search(cr, uid, [('model', '=', 'res.partner'), ('name', 'like', val.get('account_id'))])
181 if not account_xml_id:
182 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
184 val['partner_id/id'] = val.get('account_id')
185 val['type'] = 'contact'
186 val['email'] = val.get('email1') + ','+ val.get('email2')
187 if val.get('primary_address_country'):
188 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
189 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
190 val['country_id.id'] = country_id
191 val['state_id.id'] = state
192 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
193 address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
197 def import_users(sugar_obj, cr, uid, context=None):
200 'name': ['first_name', 'last_name'],
201 'login': 'user_name',
202 'context_lang' : 'context_lang',
203 'password' : 'password',
205 'context_department_id/id': 'context_department_id/id',
208 def get_users_department(sugar_obj, cr, uid, val, context=None):
213 return import_object(sugar_obj, cr, uid, fields, data, 'hr.department', 'hr_department_user', val, context=context)
217 department_id = False
219 user_obj = sugar_obj.pool.get('res.users')
220 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
221 sugar_data = sugar.search(PortType,sessionid, 'Users')
222 for val in sugar_data:
223 user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
225 val['.id'] = str(user_ids[0])
227 val['password'] = 'sugarcrm' #default password for all user
228 department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
229 val['context_department_id/id'] = department_id
230 val['context_lang'] = context.get('lang','en_US')
231 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user, context)
232 #All data has to be imported separatly because they don't have the same field
233 user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
236 def get_lead_status(sugar_obj, cr, uid, sugar_val,context=None):
239 fields = ['name', 'type']
240 name = 'lead_' + sugar_val['status']
241 data = [sugar_val['status'], 'lead']
242 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_lead_stage', name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val['status'])], context)
244 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
248 state_dict = {'status': #field in the sugarcrm database
249 { #Mapping of sugarcrm stage : openerp opportunity stage
252 'In Progress': 'open',
253 'Recycled': 'cancel',
256 state = state_dict['status'].get(sugar_val['status'], '')
259 def get_user_address(sugar_obj, cr, uid, val, context=None):
262 address_obj = sugar_obj.pool.get('res.partner.address')
264 'name': ['first_name', 'last_name'],
265 'city': 'address_city',
266 'country_id': 'country_id',
267 'state_id': 'state_id',
268 'street': 'address_street',
269 'zip': 'address_postalcode',
271 address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') or '' +' '+ val.get('last_name'))])
272 if val.get('address_country'):
273 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
274 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
275 val['country_id'] = country_id
276 val['state_id'] = state_id
277 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address, context)
278 dict_val = dict(zip(fields,datas))
280 address_obj.write(cr, uid, address_ids, dict_val)
282 new_address_id = address_obj.create(cr,uid, dict_val)
283 return new_address_id
286 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
289 address_obj = sugar_obj.pool.get('res.partner.address')
290 new_address_id = False
291 if type == 'invoice':
292 type_address = 'billing'
294 type_address = 'shipping'
296 map_partner_address.update({
297 'street': type_address + '_address_street',
298 'zip': type_address +'_address_postalcode',
299 'city': type_address +'_address_city',
300 'country_id': 'country_id',
304 if val.get(type_address +'_address_country'):
305 country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
306 state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
307 val['country_id'] = country_id
308 val['state_id'] = state
309 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
310 #Convert To list into Dictionary(Key, val). value pair.
311 dict_val = dict(zip(fields,datas))
312 new_address_id = address_obj.create(cr,uid, dict_val)
313 return new_address_id
315 def get_address(sugar_obj, cr, uid, val, context=None):
318 map_partner_address={}
320 address_obj = sugar_obj.pool.get('res.partner.address')
321 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
325 map_partner_address = {
328 'partner_id/id': 'account_id',
329 'phone': 'phone_office',
330 'mobile': 'phone_mobile',
334 if val.get('billing_address_street'):
335 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
337 if val.get('shipping_address_street'):
338 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
342 def import_partners(sugar_obj, cr, uid, context=None):
348 'website': 'website',
349 'user_id/id': 'assigned_user_id',
351 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
352 'customer': 'customer',
353 'supplier': 'supplier',
355 partner_obj = sugar_obj.pool.get('res.partner')
356 address_obj = sugar_obj.pool.get('res.partner.address')
357 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
358 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
359 for val in sugar_data:
360 add_id = get_address(sugar_obj, cr, uid, val, context)
361 val['customer'] = '1'
362 val['supplier'] = '0'
363 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner, context)
364 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
365 for address in address_obj.browse(cr,uid,add_id):
366 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
368 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
371 def get_category(sugar_obj, cr, uid, model, name, context=None):
374 fields = ['name', 'object_id']
376 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)], context)
378 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
381 alarm_dict = {'60': '1 minute before',
382 '300': '5 minutes before',
383 '600': '10 minutes before',
384 '900': '15 minutes before',
385 '1800':'30 minutes before',
386 '3600': '1 hour before',
389 alarm_obj = sugar_obj.pool.get('res.alarm')
390 if alarm_dict.get(val):
391 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
392 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
396 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
400 state_dict = {'status': #field in the sugarcrm database
401 { #Mapping of sugarcrm stage : openerp meeting stage
406 state = state_dict['status'].get(val, '')
409 def get_task_state(sugar_obj, cr, uid, val, context=None):
413 state_dict = {'status': #field in the sugarcrm database
414 { #Mapping of sugarcrm stage : openerp meeting stage
415 'Completed' : 'done',
416 'Not Started':'draft',
417 'In Progress': 'open',
418 'Pending Input': 'draft',
421 state = state_dict['status'].get(val, '')
424 def get_project_state(sugar_obj, cr, uid, val,context=None):
428 state_dict = {'status': #field in the sugarcrm database
429 { #Mapping of sugarcrm staus : openerp Projects state
432 'Published': 'close',
434 state = state_dict['status'].get(val, '')
437 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
441 state_dict = {'status': #field in the sugarcrm database
442 { #Mapping of sugarcrm status : openerp Porject Tasks state
443 'Not Started': 'draft',
444 'In Progress': 'open',
446 'Pending Input': 'pending',
447 'Deferred': 'cancelled',
449 state = state_dict['status'].get(val, '')
452 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
456 priority_dict = {'priority': #field in the sugarcrm database
457 { #Mapping of sugarcrm status : openerp Porject Tasks state
462 priority = priority_dict['priority'].get(val, '')
466 def get_account(sugar_obj, cr, uid, val, context=None):
470 partner_address_id = False
471 partner_phone = False
472 partner_mobile = False
473 model_obj = sugar_obj.pool.get('ir.model.data')
474 address_obj = sugar_obj.pool.get('res.partner.address')
475 crm_obj = sugar_obj.pool.get('crm.lead')
476 project_obj = sugar_obj.pool.get('project.project')
477 issue_obj = sugar_obj.pool.get('project.issue')
478 if val.get('parent_type') == 'Accounts':
479 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
481 model = model_obj.browse(cr, uid, model_ids)[0]
482 partner_id = model.res_id
483 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
485 address_id = address_obj.browse(cr, uid, address_ids[0])
486 partner_address_id = address_id.id
487 partner_phone = address_id.phone
488 partner_mobile = address_id.mobile
490 if val.get('parent_type') == 'Contacts':
491 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
493 model = model_obj.browse(cr, uid, model_ids)[0]
494 partner_address_id = model.res_id
495 address_id = address_obj.browse(cr, uid, partner_address_id)
496 partner_phone = address_id.phone
497 partner_mobile = address_id.mobile
498 partner_id = address_id and address_id.partner_id.id or False
500 if val.get('parent_type') == 'Opportunities':
501 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
503 model = model_obj.browse(cr, uid, model_ids)[0]
504 opportunity_id = model.res_id
505 opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
506 partner_id = opportunity_id.partner_id.id
507 partner_address_id = opportunity_id.partner_address_id.id
508 partner_phone = opportunity_id.partner_address_id.phone
509 partner_mobile = opportunity_id.partner_address_id.mobile
511 if val.get('parent_type') == 'Project':
512 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
514 model = model_obj.browse(cr, uid, model_ids)[0]
515 proj_ids = model.res_id
516 proj_id = project_obj.browse(cr, uid, proj_ids)
517 partner_id = proj_id.partner_id.id
518 partner_address_id = proj_id.contact_id.id
519 partner_phone = proj_id.contact_id.phone
520 partner_mobile = proj_id.contact_id.mobile
522 if val.get('parent_type') == 'Bugs':
523 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
525 model = model_obj.browse(cr, uid, model_ids)[0]
526 issue_ids = model.res_id
527 issue_id = issue_obj.browse(cr, uid, issue_ids)
528 partner_id = issue_id.partner_id.id
529 partner_address_id = issue_id.partner_address_id.id
530 partner_phone = issue_id.partner_address_id.phone
531 partner_mobile = issue_id.partner_address_id.mobile
532 return partner_id, partner_address_id, partner_phone,partner_mobile
535 def import_documents(sugar_obj, cr, uid, context=None):
538 map_document = {'id' : 'id',
540 'description': 'description',
542 'datas_fname': 'datas_fname',
544 attach_obj = sugar_obj.pool.get('ir.attachment')
545 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
546 sugar_data = sugar.search(PortType,sessionid, 'DocumentRevisions')
547 for val in sugar_data:
548 filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id')
549 f = open(filepath, "r")
552 val['datas'] = base64.encodestring(datas)
553 val['datas_fname'] = val.get('filename')
554 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
555 attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
558 def import_tasks(sugar_obj, cr, uid, context=None):
561 map_task = {'id' : 'id',
563 'date': ['__datetime__', 'date_start'],
564 'date_deadline' : ['__datetime__', 'date_due'],
565 'user_id/id': 'assigned_user_id',
566 'categ_id/id': 'categ_id/id',
567 'partner_id/.id': 'partner_id/.id',
568 'partner_address_id/.id': 'partner_address_id/.id',
571 meeting_obj = sugar_obj.pool.get('crm.meeting')
572 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
573 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
574 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
575 for val in sugar_data:
576 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
577 if not partner_xml_id:
578 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
579 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
580 val['partner_id/.id'] = partner_id
581 val['partner_address_id/.id'] = partner_address_id
582 val['categ_id/id'] = categ_id
583 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
584 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
585 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
588 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
591 model_obj = sugar_obj.pool.get('ir.model.data')
592 att_obj = sugar_obj.pool.get('calendar.attendee')
593 meeting_obj = sugar_obj.pool.get('crm.meeting')
594 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
595 for user in user_dict:
596 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
597 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
599 user_id = user_resource_id[0].res_id
600 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
602 attendees = attend_ids[0]
604 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
605 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
606 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
608 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
611 def import_meetings(sugar_obj, cr, uid, context=None):
614 map_meeting = {'id' : 'id',
616 'date': ['__datetime__', 'date_start'],
617 'duration': ['duration_hours', 'duration_minutes'],
618 'location': 'location',
619 'alarm_id/.id': 'alarm_id/.id',
620 'user_id/id': 'assigned_user_id',
621 'partner_id/.id':'partner_id/.id',
622 'partner_address_id/.id':'partner_address_id/.id',
625 meeting_obj = sugar_obj.pool.get('crm.meeting')
626 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
627 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
628 for val in sugar_data:
629 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
630 val['partner_id/.id'] = partner_id
631 val['partner_address_id/.id'] = partner_address_id
632 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
633 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
634 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
635 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
636 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
639 def get_calls_state(sugar_obj, cr, uid, val,context=None):
643 state_dict = {'status': #field in the sugarcrm database
644 { #Mapping of sugarcrm stage : openerp calls stage
647 'Not Held': 'pending',
649 state = state_dict['status'].get(val, '')
652 def import_calls(sugar_obj, cr, uid, context=None):
655 map_calls = {'id' : 'id',
657 'date': ['__datetime__', 'date_start'],
658 'duration': ['duration_hours', 'duration_minutes'],
659 'user_id/id': 'assigned_user_id',
660 'partner_id/.id': 'partner_id/.id',
661 'partner_address_id/.id': 'partner_address_id/.id',
662 'categ_id/id': 'categ_id/id',
664 'partner_phone': 'partner_phone',
665 'partner_mobile': 'partner_mobile',
666 'opportunity_id/id': 'opportunity_id/id',
669 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
670 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
671 sugar_data = sugar.search(PortType, sessionid, 'Calls')
672 for val in sugar_data:
673 sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
675 for call_opportunity in sugar_call_leads:
676 val['opportunity_id/id'] = call_opportunity
677 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
678 val['categ_id/id'] = categ_id
679 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
680 val['partner_id/.id'] = partner_id
681 val['partner_address_id/.id'] = partner_address_id
682 val['partner_phone'] = partner_phone
683 val['partner_mobile'] = partner_mobile
684 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
685 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
686 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
689 def import_resources(sugar_obj, cr, uid, context=None):
692 map_resource = {'id' : 'user_hash',
693 'name': ['first_name', 'last_name'],
695 resource_obj = sugar_obj.pool.get('resource.resource')
696 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
697 sugar_data = sugar.search(PortType, sessionid, 'Employees')
698 for val in sugar_data:
699 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
700 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
703 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
707 priority_dict = {'priority': #field in the sugarcrm database
708 { #Mapping of sugarcrm priority : openerp bugs priority
714 priority = priority_dict['priority'].get(val, '')
717 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
721 priority_dict = {'priority': #field in the sugarcrm database
722 { #Mapping of sugarcrm priority : openerp claims priority
727 priority = priority_dict['priority'].get(val, '')
730 def get_bug_state(sugar_obj, cr, uid, val,context=None):
734 state_dict = {'status': #field in the sugarcrm database
735 { #Mapping of sugarcrm status : openerp Bugs state
739 'Pending': 'pending',
740 'Rejected': 'cancel',
742 state = state_dict['status'].get(val, '')
745 def get_claim_state(sugar_obj, cr, uid, val,context=None):
749 state_dict = {'status': #field in the sugarcrm database
750 { #Mapping of sugarcrm status : openerp claim state
754 'Pending Input': 'pending',
755 'Rejected': 'cancel',
756 'Duplicate': 'draft',
758 state = state_dict['status'].get(val, '')
762 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
766 partner_address_id = False
767 partner_phone = False
768 partner_email = False
769 model_obj = sugar_obj.pool.get('ir.model.data')
770 address_obj = sugar_obj.pool.get('res.partner.address')
771 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
773 model = model_obj.browse(cr, uid, model_ids)[0]
774 partner_id = model.res_id
775 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
777 address_id = address_obj.browse(cr, uid, address_ids[0])
778 partner_address_id = address_id.id
779 partner_phone = address_id.phone
780 partner_mobile = address_id.email
781 return partner_id, partner_address_id, partner_phone,partner_email
783 def import_claims(sugar_obj, cr, uid, context=None):
786 map_claim = {'id' : 'id',
788 'date': ['__datetime__', 'date_entered'],
789 'user_id/id': 'assigned_user_id',
790 'priority':'priority',
791 'partner_id/.id': 'partner_id/.id',
792 'partner_address_id/.id': 'partner_address_id/.id',
793 'partner_phone': 'partner_phone',
794 'partner_mobile': 'partner_email',
795 'description': 'description',
798 claim_obj = sugar_obj.pool.get('crm.claim')
799 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
800 sugar_data = sugar.search(PortType, sessionid, 'Cases')
801 for val in sugar_data:
802 partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
803 val['partner_id/.id'] = partner_id
804 val['partner_address_id/.id'] = partner_address_id
805 val['partner_phone'] = partner_phone
806 val['email_from'] = partner_email
807 val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
808 val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
809 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
810 claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
813 def import_bug(sugar_obj, cr, uid, context=None):
816 map_resource = {'id' : 'id',
818 'project_id/.id':'project_id/.id',
819 'categ_id/id': 'categ_id/id',
820 'priority':'priority',
821 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
824 issue_obj = sugar_obj.pool.get('project.issue')
825 project_obj = sugar_obj.pool.get('project.project')
826 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
827 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
828 for val in sugar_data:
829 project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
831 project_id = project_ids[0]
833 project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})
834 val['project_id/.id'] = project_id
835 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
836 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
837 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
838 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
839 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
842 def get_job_id(sugar_obj, cr, uid, val, context=None):
847 return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, context=context)
849 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
854 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, context=context)
856 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
860 attachment_obj = sugar_obj.pool.get('ir.attachment')
861 partner_obj = sugar_obj.pool.get('res.partner')
862 model_obj = sugar_obj.pool.get('ir.model.data')
863 mailgate_obj = sugar_obj.pool.get('mailgate.message')
864 attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
865 if not attach_ids and Filename:
866 if parent_type == 'Accounts':
867 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')})
869 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)})
870 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
871 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
873 if parent_type == 'Accounts':
874 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
876 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
879 def import_history(sugar_obj, cr, uid, context=None):
882 map_attachment = {'id' : 'id',
884 'date': ['__datetime__', 'date_entered'],
885 'user_id/id': 'assigned_user_id',
886 'description': ['__prettyprint__','description', 'description_html'],
889 'partner_id/.id' : 'partner_id/.id',
891 mailgate_obj = sugar_obj.pool.get('mailgate.message')
892 model_obj = sugar_obj.pool.get('ir.model.data')
893 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
894 sugar_data = sugar.search(PortType, sessionid, 'Notes')
895 for val in sugar_data:
896 File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
897 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
899 model = model_obj.browse(cr, uid, model_ids)[0]
900 if model.model == 'res.partner':
901 val['partner_id/.id'] = model.res_id
903 val['res_id'] = model.res_id
904 val['model'] = model.model
905 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)
906 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
907 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
910 def import_employees(sugar_obj, cr, uid, context=None):
913 map_employee = {'id' : 'user_hash',
914 'resource_id/.id': 'resource_id/.id',
915 'name': ['first_name', 'last_name'],
916 'work_phone': 'phone_work',
917 'mobile_phone': 'phone_mobile',
918 'user_id/name': ['first_name', 'last_name'],
919 'address_home_id/.id': 'address_home_id/.id',
920 'notes': 'description',
921 #TODO: Creation of Employee create problem.
922 # 'coach_id/id': 'reports_to_id',
923 'job_id/id': 'job_id/id'
925 employee_obj = sugar_obj.pool.get('hr.employee')
926 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
927 sugar_data = sugar.search(PortType, sessionid, 'Employees')
928 for val in sugar_data:
929 address_id = get_user_address(sugar_obj, cr, uid, val, context)
930 val['address_home_id/.id'] = address_id
931 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
932 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
934 val['resource_id/.id'] = resource_id[0].res_id
935 val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
936 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
937 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
940 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
941 fields = ['shortcut', 'name', 'domain']
942 data = [salutation, salutation, domain]
943 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
945 def import_emails(sugar_obj, cr, uid, context=None):
948 map_emails = {'id': 'id',
950 'date':['__datetime__', 'date_sent'],
951 'email_from': 'from_addr_name',
952 'email_to': 'reply_to_addr',
953 'email_cc': 'cc_addrs_names',
954 'email_bcc': 'bcc_addrs_names',
955 'message_id': 'message_id',
956 'user_id/id': 'assigned_user_id',
957 'description': ['__prettyprint__', 'description', 'description_html'],
960 'partner_id/.id': 'partner_id/.id'
962 mailgate_obj = sugar_obj.pool.get('mailgate.message')
963 model_obj = sugar_obj.pool.get('ir.model.data')
964 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
965 sugar_data = sugar.search(PortType, sessionid, 'Emails')
966 for val in sugar_data:
967 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
968 for model in model_obj.browse(cr, uid, model_ids):
969 if model.model == 'res.partner':
970 val['partner_id/.id'] = model.res_id
972 val['res_id'] = model.res_id
973 val['model'] = model.model
974 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
975 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
978 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
982 partner_invoice_id = False
983 model_obj = sugar_obj.pool.get('ir.model.data')
984 partner_obj = sugar_obj.pool.get('res.partner')
985 partner_address_obj = sugar_obj.pool.get('res.partner.address')
986 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
987 for account_id in sugar_project_account:
988 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
990 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
991 partner_id = partner_obj.browse(cr, uid, model_id).id
992 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
993 partner_invoice_id = address_ids[0]
994 return partner_id, partner_invoice_id
996 def import_projects(sugar_obj, cr, uid, context=None):
999 map_project = {'id': 'id',
1001 'date_start': ['__datetime__', 'estimated_start_date'],
1002 'date': ['__datetime__', 'estimated_end_date'],
1003 'user_id/id': 'assigned_user_id',
1004 'partner_id/.id': 'partner_id/.id',
1005 'contact_id/.id': 'contact_id/.id',
1008 project_obj = sugar_obj.pool.get('project.project')
1009 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1010 sugar_data = sugar.search(PortType, sessionid, 'Project')
1011 for val in sugar_data:
1012 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
1013 val['partner_id/.id'] = partner_id
1014 val['contact_id/.id'] = partner_invoice_id
1015 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
1016 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
1017 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1021 def import_project_tasks(sugar_obj, cr, uid, context=None):
1024 map_project_task = {'id': 'id',
1026 'date_start': ['__datetime__', 'date_start'],
1027 'date_end': ['__datetime__', 'date_finish'],
1028 'progress': 'progress',
1029 'project_id/name': 'project_name',
1030 'planned_hours': 'planned_hours',
1031 'total_hours': 'total_hours',
1032 'priority': 'priority',
1033 'description': 'description',
1034 'user_id/id': 'assigned_user_id',
1037 task_obj = sugar_obj.pool.get('project.task')
1038 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1039 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1040 for val in sugar_data:
1041 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1042 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1043 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1044 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1047 def import_leads(sugar_obj, cr, uid, context=None):
1052 'name': ['first_name', 'last_name'],
1053 'contact_name': ['first_name', 'last_name'],
1054 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1055 'partner_name': 'account_name',
1056 'email_from': 'email1',
1057 'phone': 'phone_work',
1058 'mobile': 'phone_mobile',
1059 'title.id': 'title.id',
1061 'street': 'primary_address_street',
1062 'street2': 'alt_address_street',
1063 'zip': 'primary_address_postalcode',
1064 'city':'primary_address_city',
1065 'user_id/id' : 'assigned_user_id',
1066 'stage_id/id' : 'stage_id/id',
1070 'referred': 'refered_by',
1072 'type_id/id': 'type_id/id',
1073 'country_id.id': 'country_id.id',
1074 'state_id.id': 'state_id.id'
1076 lead_obj = sugar_obj.pool.get('crm.lead')
1077 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1078 sugar_data = sugar.search(PortType, sessionid, 'Leads')
1079 for val in sugar_data:
1080 if val.get('do_not_call') == '0':
1082 if val.get('opportunity_id'):
1084 if val.get('salutation'):
1085 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1086 val['title/id'] = title_id
1087 val['type'] = 'lead'
1088 val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1089 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1090 val['stage_id/id'] = stage_id
1091 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1092 if val.get('primary_address_country'):
1093 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1094 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1095 val['country_id.id'] = country_id
1096 val['state_id.id'] = state
1097 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1098 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1101 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1104 partner_contact_name = False
1105 partner_contact_email = False
1106 model_obj = sugar_obj.pool.get('ir.model.data')
1107 partner_address_obj = sugar_obj.pool.get('res.partner.address')
1108 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1109 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
1110 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1111 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1112 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1114 for contact in sugar_contact:
1115 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1117 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1118 address_id = partner_address_obj.browse(cr, uid, model_id)
1119 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1120 partner_contact_name = address_id.name
1121 partner_contact_email = address_id.email
1123 partner_contact_name = val.get('account_name')
1124 return partner_contact_name, partner_contact_email
1126 def import_opportunities(sugar_obj, cr, uid, context=None):
1129 map_opportunity = {'id' : 'id',
1131 'probability': 'probability',
1132 'partner_id/name': 'account_name',
1133 'title_action': 'next_step',
1134 'partner_address_id/name': 'partner_address_id/name',
1135 'planned_revenue': 'amount',
1136 'date_deadline': ['__datetime__', 'date_closed'],
1137 'user_id/id' : 'assigned_user_id',
1138 'stage_id/id' : 'stage_id/id',
1140 'categ_id/id': 'categ_id/id',
1141 'email_from': 'email_from'
1143 lead_obj = sugar_obj.pool.get('crm.lead')
1144 partner_obj = sugar_obj.pool.get('res.partner')
1145 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1146 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1147 for val in sugar_data:
1148 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1149 if not partner_xml_id:
1150 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1151 partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1152 val['partner_address_id/name'] = partner_contact_name
1153 val['email_from'] = partner_contact_email
1154 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
1155 val['type'] = 'opportunity'
1156 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1157 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1158 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1161 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1164 fields = ['name', 'type']
1165 name = 'Opportunity_' + sugar_val['sales_stage']
1166 data = [sugar_val['sales_stage'], 'Opportunity']
1167 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1169 MAP_FIELDS = {'Opportunities': #Object Mapping name
1170 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
1171 'process' : import_opportunities,
1174 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
1175 'process' : import_leads,
1178 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
1179 'process' : import_partner_address,
1182 {'dependencies' : ['Users'], #Object to import before this table
1183 'process' : import_partners,
1186 {'dependencies' : [],
1187 'process' : import_users,
1190 {'dependencies' : ['Users'],
1191 'process' : import_documents,
1194 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Projects', 'Opportunities', 'Tasks'],
1195 'process' : import_meetings,
1198 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1199 'process' : import_tasks,
1202 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1203 'process' : import_calls,
1206 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1207 'process' : import_projects,
1210 {'dependencies' : ['Users', 'Projects'],
1211 'process' : import_project_tasks,
1214 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1215 'process' : import_bug,
1218 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
1219 'process' : import_claims,
1222 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1223 'process' : import_emails,
1227 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1228 'process' : import_history,
1231 {'dependencies' : ['Resources', 'Users'],
1232 'process' : import_employees,
1235 {'dependencies' : ['Users'],
1236 'process' : import_resources,
1240 class import_sugarcrm(osv.osv):
1241 """Import SugarCRM DATA"""
1243 _name = "import.sugarcrm"
1244 _description = __doc__
1246 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1247 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1248 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1249 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1250 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1251 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1252 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1253 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1254 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1255 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1256 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1257 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1258 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1259 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1260 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1261 'username': fields.char('User Name', size=64),
1262 'password': fields.char('Password', size=24),
1264 _defaults = {#to be set to true, but easier for debugging
1265 'opportunity': True,
1276 'project_task': True,
1281 def get_key(self, cr, uid, ids, context=None):
1282 """Select Key as For which Module data we want import data."""
1286 for current in self.browse(cr, uid, ids, context):
1287 if current.opportunity:
1288 key_list.append('Opportunities')
1290 key_list.append('Users')
1292 key_list.append('Contacts')
1294 key_list.append('Accounts')
1295 if current.employee:
1296 key_list.append('Employees')
1298 key_list.append('Meetings')
1300 key_list.append('Tasks')
1302 key_list.append('Calls')
1304 key_list.append('Claims')
1306 key_list.append('Emails')
1308 key_list.append('Projects')
1309 if current.project_task:
1310 key_list.append('Project Tasks')
1312 key_list.append('Bugs')
1313 if current.attachment:
1314 key_list.append('Notes')
1315 if current.document:
1316 key_list.append('Documents')
1319 def import_all(self, cr, uid, ids, context=None):
1320 """Import all sugarcrm data into openerp module"""
1323 keys = self.get_key(cr, uid, ids, context)
1324 imported = set() #to invoid importing 2 times the sames modules
1326 if not key in imported:
1327 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1328 MAP_FIELDS[key]['process'](self, cr, uid, context)
1331 obj_model = self.pool.get('ir.model.data')
1332 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1333 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1335 'view_type': 'form',
1336 'view_mode': 'form',
1337 'res_model': 'import.message',
1338 'views': [(resource_id,'form')],
1339 'type': 'ir.actions.act_window',
1343 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1344 for dependency in dep:
1345 if not dependency in imported:
1346 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1347 dict[dependency]['process'](self, cr, uid, context)
1348 imported.add(dependency)