1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
21 from osv import fields, osv
22 from operator import itemgetter
24 import sugarcrm_fields_mapping
25 from tools.translate import _
27 pp = pprint.PrettyPrinter(indent=4)
29 OPENERP_FIEDS_MAPS = {'Leads': 'crm.lead',
30 'Opportunities': 'crm.lead',
31 'Contacts': 'res.partner.address',
32 'Accounts': 'res.partner',
33 'Resources': 'resource.resource',
35 'Meetings': 'crm.meeting',
36 'Calls': 'crm.phonecall',
37 'Claims': 'crm.claim',
38 'Employee': 'hr.employee',
39 'Project': 'project.project',
40 'ProjectTask': 'project.task',
41 'Bugs': 'project.issue',
42 'Documents': 'ir.attachment',
46 def find_mapped_id(obj, cr, uid, res_model, sugar_id, context):
47 model_obj = obj.pool.get('ir.model.data')
48 return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context)
50 def mapped_id(obj, cr, uid, res_model, sugar_id, id, context):
52 This function create the mapping between an already existing data and the similar data of sugarcrm
53 @param res_model: model of the mapped object
54 @param sugar_id: external sugar id
55 @param id: id in the database
57 @return : the xml_id or sugar_id
59 ir_model_data_obj = obj.pool.get('ir.model.data')
60 id = ir_model_data_obj._update(cr, uid, res_model,
61 'sugarcrm_import', {}, mode='update', xml_id=sugar_id,
62 noupdate=True, res_id=id, context=context)
65 def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False, context=None):
67 This method will import an object in the openerp, usefull for field that is only a char in sugar and is an object in openerp
68 use import_data that will take care to create/update or do nothing with the data
69 this method return the xml_id
70 @param fields: list of fields needed to create the object without id
71 @param data: the list of the data, in the same order as the field
72 ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds']
73 @param model: the openerp's model of the create/update object
74 @param table: the table where data come from in sugarcrm, no need to fit the real name of openerp name, just need to be unique
75 @param unique_name: the name of the object that we want to create/update get the id
76 @param domain_search : the domain that should find the unique existing record
78 @return: the xml_id of the ressources
80 domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
81 obj = sugar_obj.pool.get(model)
82 xml_id = generate_xml_id(name, table)
84 def mapped_id_if_exist(obj, domain, xml_id):
85 ids = obj.search(cr, uid, domain, context=context)
87 return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context)
91 mapped_id_if_exist(obj, domain_search, xml_id)
94 obj.import_data(cr, uid, fields, [data], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
97 def generate_xml_id(name, table):
99 @param name: name of the object, has to be unique in for a given table
100 @param table : table where the record we want generate come from
101 @return: a unique xml id for record, the xml_id will be the same given the same table and same name
102 To be used to avoid duplication of data that don't have ids
104 sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard
105 name = name.replace('.', '_')
106 return sugar_instance + "_" + table + "_" + name
108 def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
109 models = sugar_obj.pool.get(model)
110 model_code = sugar_val[0:2]
111 all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())])
112 output = sorted([(o.id, o.name)
113 for o in models.browse(cr, uid, all_model_ids, context=context)],
117 def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
118 """Get states or create new state"""
120 res_country_state_obj = sugar_obj.pool.get('res.country.state')
122 state = get_all(sugar_obj,
123 cr, uid, 'res.country.state', sugar_val, context=context)
125 state_id = state and state[0][0]
127 state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
130 def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
131 """Get Country or Create new country"""
132 res_country_obj = sugar_obj.pool.get('res.country')
134 country_code = sugar_country_val[0:2]
135 country = get_all(sugar_obj,
136 cr, uid, 'res.country', sugar_country_val, context=context)
138 country_id = country and country[0][0]
140 country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})
143 def import_partner_address(sugar_obj, cr, uid, context=None):
146 map_partner_address = {
148 'name': ['first_name', 'last_name'],
149 'partner_id/id': 'account_id',
150 'phone': 'phone_work',
151 'mobile': 'phone_mobile',
154 'street': 'primary_address_street',
155 'zip': 'primary_address_postalcode',
156 'city': 'primary_address_city',
157 'country_id.id': 'country_id.id',
158 'state_id.id': 'state_id.id',
162 address_obj = sugar_obj.pool.get('res.partner.address')
163 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
164 sugar_data = sugar.search(PortType, sessionid, 'Contacts')
165 for val in sugar_data:
166 val['type'] = 'contact'
167 contact_emails = sugar.contact_emails_search(PortType, context.get('username', ''), context.get('password', ''), email_address=val.get('email1'))
168 val['email'] = (','.join(map(lambda x : x, contact_emails)))
169 if val.get('primary_address_country'):
170 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
171 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
172 val['country_id.id'] = country_id
173 val['state_id.id'] = state
174 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
175 address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
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(surgar_obj, cr, uid, sugar_val,context=None):
226 stage_dict = {'status': #field in the sugarcrm database
227 { #Mapping of sugarcrm stage : openerp opportunity stage
229 'Assigned':'Qualification',
230 'In Progress': 'Proposition',
231 'Recycled': 'Negotiation',
234 stage = stage_dict['status'].get(sugar_val['status'], '')
235 stage_pool = surgar_obj.pool.get('crm.case.stage')
236 stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)])
237 for stage in stage_pool.browse(cr, uid, stage_ids, context):
241 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
245 state_dict = {'status': #field in the sugarcrm database
246 { #Mapping of sugarcrm stage : openerp opportunity stage
249 'In Progress': 'open',
250 'Recycled': 'cancel',
253 state = state_dict['status'].get(sugar_val['status'], '')
258 def get_user_address(sugar_obj, cr, uid, val, context=None):
259 address_obj = sugar_obj.pool.get('res.partner.address')
261 'name': ['first_name', 'last_name'],
262 'city': 'address_city',
263 'country_id': 'country_id',
264 'state_id': 'state_id',
265 'street': 'address_street',
266 'zip': 'address_postalcode',
268 address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') +' '+ val.get('last_name'))])
269 if val.get('address_country'):
270 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
271 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
272 val['country_id'] = country_id
273 val['state_id'] = state_id
274 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address, context)
275 dict_val = dict(zip(fields,datas))
277 address_obj.write(cr, uid, address_ids, dict_val)
279 new_address_id = address_obj.create(cr,uid, dict_val)
280 return new_address_id
283 def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
284 address_obj = sugar_obj.pool.get('res.partner.address')
285 new_address_id = False
286 if type == 'invoice':
287 type_address = 'billing'
289 type_address = 'shipping'
291 map_partner_address.update({
292 'street': type_address + '_address_street',
293 'zip': type_address +'_address_postalcode',
294 'city': type_address +'_address_city',
295 'country_id': 'country_id',
299 if val.get(type_address +'_address_country'):
300 country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context)
301 state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context)
302 val['country_id'] = country_id
303 val['state_id'] = state
304 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
305 #Convert To list into Dictionary(Key, val). value pair.
306 dict_val = dict(zip(fields,datas))
307 new_address_id = address_obj.create(cr,uid, dict_val)
308 return new_address_id
310 def get_address(sugar_obj, cr, uid, val, context=None):
311 map_partner_address={}
313 address_obj = sugar_obj.pool.get('res.partner.address')
314 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
318 map_partner_address = {
321 'partner_id/id': 'account_id',
322 'phone': 'phone_office',
323 'mobile': 'phone_mobile',
327 if val.get('billing_address_street'):
328 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
330 if val.get('shipping_address_street'):
331 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
335 def import_partners(sugar_obj, cr, uid, context=None):
341 'website': 'website',
342 'user_id/id': 'assigned_user_id',
344 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
345 'customer': 'customer',
346 'supplier': 'supplier',
348 partner_obj = sugar_obj.pool.get('res.partner')
349 address_obj = sugar_obj.pool.get('res.partner.address')
350 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
351 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
352 for val in sugar_data:
353 add_id = get_address(sugar_obj, cr, uid, val, context)
354 val['customer'] = '1'
355 val['supplier'] = '0'
356 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner, context)
357 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
358 for address in address_obj.browse(cr,uid,add_id):
359 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
361 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
364 def get_category(sugar_obj, cr, uid, model, name, context=None):
366 categ_obj = sugar_obj.pool.get('crm.case.categ')
367 categ_ids = categ_obj.search(cr, uid, [('object_id.model','=',model), ('name', 'like', name)] )
369 categ_id = categ_ids[0]
371 model_ids = sugar_obj.pool.get('ir.model').search(cr, uid, [('model', '=', model)], context=context)
372 model = model_ids and model_ids[0] or False
373 categ_id = categ_obj.create(cr, uid, {'name': name, 'object_id': model})
376 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
378 alarm_dict = {'60': '1 minute before',
379 '300': '5 minutes before',
380 '600': '10 minutes before',
381 '900': '15 minutes before',
382 '1800':'30 minutes before',
383 '3600': '1 hour before',
386 alarm_obj = sugar_obj.pool.get('res.alarm')
387 if alarm_dict.get(val):
388 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
389 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
393 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
397 state_dict = {'status': #field in the sugarcrm database
398 { #Mapping of sugarcrm stage : openerp meeting stage
403 state = state_dict['status'].get(val, '')
406 def get_task_state(sugar_obj, cr, uid, val, context=None):
410 state_dict = {'status': #field in the sugarcrm database
411 { #Mapping of sugarcrm stage : openerp meeting stage
412 'Completed' : 'done',
413 'Not Started':'draft',
414 'In Progress': 'open',
415 'Pending Input': 'draft',
418 state = state_dict['status'].get(val, '')
421 def get_project_state(sugar_obj, cr, uid, val,context=None):
425 state_dict = {'status': #field in the sugarcrm database
426 { #Mapping of sugarcrm staus : openerp Projects state
429 'Published': 'close',
431 state = state_dict['status'].get(val, '')
434 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
438 state_dict = {'status': #field in the sugarcrm database
439 { #Mapping of sugarcrm status : openerp Porject Tasks state
440 'Not Started': 'draft',
441 'In Progress': 'open',
443 'Pending Input': 'pending',
444 'Deferred': 'cancelled',
446 state = state_dict['status'].get(val, '')
449 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
453 priority_dict = {'priority': #field in the sugarcrm database
454 { #Mapping of sugarcrm status : openerp Porject Tasks state
459 priority = priority_dict['priority'].get(val, '')
463 def get_account(sugar_obj, cr, uid, val, context=None):
467 partner_address_id = False
468 partner_phone = False
469 partner_mobile = False
470 model_obj = sugar_obj.pool.get('ir.model.data')
471 address_obj = sugar_obj.pool.get('res.partner.address')
472 crm_obj = sugar_obj.pool.get('crm.lead')
473 project_obj = sugar_obj.pool.get('project.project')
474 issue_obj = sugar_obj.pool.get('project.issue')
475 if val.get('parent_type') == 'Accounts':
476 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
478 model = model_obj.browse(cr, uid, model_ids)[0]
479 partner_id = model.res_id
480 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
482 address_id = address_obj.browse(cr, uid, address_ids[0])
483 partner_address_id = address_id.id
484 partner_phone = address_id.phone
485 partner_mobile = address_id.mobile
487 if val.get('parent_type') == 'Contacts':
488 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
489 for model in model_obj.browse(cr, uid, model_ids):
490 partner_address_id = model.res_id
491 address_id = address_obj.browse(cr, uid, partner_address_id)
492 partner_phone = address_id.phone
493 partner_mobile = address_id.mobile
494 partner_id = address_id and address_id.partner_id or False
496 if val.get('parent_type') == 'Opportunities':
497 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
498 for model in model_obj.browse(cr, uid, model_ids):
499 opportunity_id = model.res_id
500 opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
501 partner_id = opportunity_id.partner_id.id
502 partner_address_id = opportunity_id.partner_address_id.id
503 partner_phone = opportunity_id.partner_address_id.phone
504 partner_mobile = opportunity_id.partner_address_id.mobile
506 if val.get('parent_type') == 'Project':
507 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
508 for model in model_obj.browse(cr, uid, model_ids):
509 proj_ids = model.res_id
510 proj_id = project_obj.browse(cr, uid, proj_ids)
511 partner_id = proj_id.partner_id.id
512 partner_address_id = proj_id.contact_id.id
513 partner_phone = proj_id.contact_id.phone
514 partner_mobile = proj_id.contact_id.mobile
516 if val.get('parent_type') == 'Bugs':
517 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
518 for model in model_obj.browse(cr, uid, model_ids):
519 issue_ids = model.res_id
520 issue_id = issue_obj.browse(cr, uid, issue_ids)
521 partner_id = issue_id.partner_id.id
522 partner_address_id = issue_id.partner_address_id.id
523 partner_phone = issue_id.partner_address_id.phone
524 partner_mobile = issue_id.partner_address_id.mobile
526 return partner_id, partner_address_id, partner_phone,partner_mobile
528 def import_documents(sugar_obj, cr, uid, context=None):
531 map_document = {'id' : 'id',
532 'name': 'document_name',
533 'description': 'description',
535 'datas_fname': 'datas_fname',
537 attach_obj = sugar_obj.pool.get('ir.attachment')
538 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
539 sugar_data = sugar.search(PortType,sessionid, 'Documents')
540 for val in sugar_data:
541 file, filename = sugar.attachment_search(PortType, sessionid, 'DocumentRevisions', val.get('document_revision_id'))
543 val['datas_fname'] = filename
544 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
545 attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
548 def import_tasks(sugar_obj, cr, uid, context=None):
551 map_task = {'id' : 'id',
553 'date': ['__datetime__', 'date_start'],
554 'date_deadline' : ['__datetime__', 'date_due'],
555 'user_id/id': 'assigned_user_id',
556 'categ_id/.id': 'categ_id/.id',
557 'partner_id/.id': 'partner_id/.id',
558 'partner_address_id/.id': 'partner_address_id/.id',
561 meeting_obj = sugar_obj.pool.get('crm.meeting')
562 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
563 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
564 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
565 for val in sugar_data:
566 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
567 if not partner_xml_id:
568 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
569 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
570 val['partner_id/.id'] = partner_id
571 val['partner_address_id/.id'] = partner_address_id
572 val['categ_id/.id'] = categ_id
573 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
574 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
575 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
578 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
581 model_obj = sugar_obj.pool.get('ir.model.data')
582 att_obj = sugar_obj.pool.get('calendar.attendee')
583 meeting_obj = sugar_obj.pool.get('crm.meeting')
584 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
585 for user in user_dict:
586 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
587 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
589 user_id = user_resource_id[0].res_id
590 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
592 attendees = attend_ids[0]
594 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
595 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
596 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
598 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
601 def import_meetings(sugar_obj, cr, uid, context=None):
604 map_meeting = {'id' : 'id',
606 'date': ['__datetime__', 'date_start'],
607 'duration': ['duration_hours', 'duration_minutes'],
608 'location': 'location',
609 'alarm_id/.id': 'alarm_id/.id',
610 'user_id/id': 'assigned_user_id',
611 'partner_id/.id':'partner_id/.id',
612 'partner_address_id/.id':'partner_address_id/.id',
615 meeting_obj = sugar_obj.pool.get('crm.meeting')
616 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
617 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
618 for val in sugar_data:
619 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
620 val['partner_id/.id'] = partner_id
621 val['partner_address_id/.id'] = partner_address_id
622 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
623 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
624 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
625 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
626 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
629 def get_calls_state(sugar_obj, cr, uid, val,context=None):
633 state_dict = {'status': #field in the sugarcrm database
634 { #Mapping of sugarcrm stage : openerp calls stage
637 'Not Held': 'pending',
639 state = state_dict['status'].get(val, '')
642 def import_calls(sugar_obj, cr, uid, context=None):
645 map_calls = {'id' : 'id',
647 'date': ['__datetime__', 'date_start'],
648 'duration': ['duration_hours', 'duration_minutes'],
649 'user_id/id': 'assigned_user_id',
650 'partner_id/.id': 'partner_id/.id',
651 'partner_address_id/.id': 'partner_address_id/.id',
652 'categ_id/.id': 'categ_id/.id',
654 'partner_phone': 'partner_phone',
655 'partner_mobile': 'partner_mobile',
656 'opportunity_id/id': 'opportunity_id/id',
659 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
660 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
661 sugar_data = sugar.search(PortType, sessionid, 'Calls')
662 for val in sugar_data:
663 sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
665 for call_opportunity in sugar_call_leads:
666 val['opportunity_id/id'] = call_opportunity
667 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
668 val['categ_id/.id'] = categ_id
669 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
671 val['partner_id/.id'] = partner_id
672 val['partner_address_id/.id'] = partner_address_id
673 val['partner_phone'] = partner_phone
674 val['partner_mobile'] = partner_mobile
675 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
676 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
677 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
680 def import_resources(sugar_obj, cr, uid, context=None):
683 map_resource = {'id' : 'user_hash',
684 'name': ['first_name', 'last_name'],
686 resource_obj = sugar_obj.pool.get('resource.resource')
687 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
688 sugar_data = sugar.search(PortType, sessionid, 'Employees')
689 for val in sugar_data:
690 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
691 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
694 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
698 priority_dict = {'priority': #field in the sugarcrm database
699 { #Mapping of sugarcrm priority : openerp bugs priority
705 priority = priority_dict['priority'].get(val, '')
708 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
712 priority_dict = {'priority': #field in the sugarcrm database
713 { #Mapping of sugarcrm priority : openerp claims priority
718 priority = priority_dict['priority'].get(val, '')
721 def get_bug_state(sugar_obj, cr, uid, val,context=None):
725 state_dict = {'status': #field in the sugarcrm database
726 { #Mapping of sugarcrm status : openerp Bugs state
730 'Pending': 'pending',
731 'Rejected': 'cancel',
733 state = state_dict['status'].get(val, '')
736 def get_claim_state(sugar_obj, cr, uid, val,context=None):
740 state_dict = {'status': #field in the sugarcrm database
741 { #Mapping of sugarcrm status : openerp claim state
745 'Pending Input': 'pending',
746 'Rejected': 'cancel',
747 'Duplicate': 'draft',
749 state = state_dict['status'].get(val, '')
753 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
757 partner_address_id = False
758 partner_phone = False
759 partner_email = False
760 model_obj = sugar_obj.pool.get('ir.model.data')
761 address_obj = sugar_obj.pool.get('res.partner.address')
762 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
764 model = model_obj.browse(cr, uid, model_ids)[0]
765 partner_id = model.res_id
766 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
768 address_id = address_obj.browse(cr, uid, address_ids[0])
769 partner_address_id = address_id.id
770 partner_phone = address_id.phone
771 partner_mobile = address_id.email
772 return partner_id, partner_address_id, partner_phone,partner_email
774 def import_claims(sugar_obj, cr, uid, context=None):
777 map_claim = {'id' : 'id',
779 'date': ['__datetime__', 'date_entered'],
780 'user_id/id': 'assigned_user_id',
781 'priority':'priority',
782 'partner_id/.id': 'partner_id/.id',
783 'partner_address_id/.id': 'partner_address_id/.id',
784 'partner_phone': 'partner_phone',
785 'partner_mobile': 'partner_email',
786 'description': 'description',
789 claim_obj = sugar_obj.pool.get('crm.claim')
790 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
791 sugar_data = sugar.search(PortType, sessionid, 'Cases')
792 for val in sugar_data:
793 partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
794 val['partner_id/.id'] = partner_id
795 val['partner_address_id/.id'] = partner_address_id
796 val['partner_phone'] = partner_phone
797 val['email_from'] = partner_email
798 val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
799 val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
800 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
801 claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
804 def import_bug(sugar_obj, cr, uid, context=None):
807 map_resource = {'id' : 'id',
809 'project_id/.id':'project_id/.id',
810 'categ_id.id': 'categ_id.id',
811 'priority':'priority',
812 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
815 issue_obj = sugar_obj.pool.get('project.issue')
816 project_obj = sugar_obj.pool.get('project.project')
817 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
818 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
819 for val in sugar_data:
820 project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
822 project_id = project_ids[0]
824 project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})
825 val['project_id/.id'] = project_id
826 val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
827 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
828 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
829 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
830 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
833 def get_job_id(sugar_obj, cr, uid, val, context=None):
837 job_obj = sugar_obj.pool.get('hr.job')
838 job_ids = job_obj.search(cr, uid, [('name', '=', val)])
842 job_id = job_obj.create(cr, uid, {'name': val})
845 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
849 cam_obj = sugar_obj.pool.get('crm.case.resource.type')
850 cam_ids = cam_obj.search(cr, uid, [('name', '=', val)])
854 cam_id = cam_obj.create(cr, uid, {'name': val})
857 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
861 attachment_obj = sugar_obj.pool.get('ir.attachment')
862 partner_obj = sugar_obj.pool.get('res.partner')
863 model_obj = sugar_obj.pool.get('ir.model.data')
864 mailgate_obj = sugar_obj.pool.get('mailgate.message')
865 attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
866 if not attach_ids and Filename:
867 if parent_type == 'Accounts':
868 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')})
870 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)})
871 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
872 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
874 if parent_type == 'Accounts':
875 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
877 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
880 def import_history(sugar_obj, cr, uid, context=None):
883 map_attachment = {'id' : 'id',
885 'date': ['__datetime__', 'date_entered'],
886 'user_id/id': 'assigned_user_id',
887 'description': ['__prettyprint__','description', 'description_html'],
890 'partner_id/.id' : 'partner_id/.id',
892 mailgate_obj = sugar_obj.pool.get('mailgate.message')
893 model_obj = sugar_obj.pool.get('ir.model.data')
894 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
895 sugar_data = sugar.search(PortType, sessionid, 'Notes')
896 for val in sugar_data:
897 File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
898 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
900 model = model_obj.browse(cr, uid, model_ids)[0]
901 if model.model == 'res.partner':
902 val['partner_id/.id'] = model.res_id
904 val['res_id'] = model.res_id
905 val['model'] = model.model
906 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)
907 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
908 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
911 def import_employees(sugar_obj, cr, uid, context=None):
914 map_employee = {'id' : 'user_hash',
915 'resource_id/.id': 'resource_id/.id',
916 'name': ['first_name', 'last_name'],
917 'work_phone': 'phone_work',
918 'mobile_phone': 'phone_mobile',
919 'user_id/name': ['first_name', 'last_name'],
920 'address_home_id/.id': 'address_home_id/.id',
921 'notes': 'description',
922 #TODO: Creation of Employee create problem.
923 # 'coach_id/id': 'reports_to_id',
924 'job_id/.id': 'job_id/.id'
926 employee_obj = sugar_obj.pool.get('hr.employee')
927 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
928 sugar_data = sugar.search(PortType, sessionid, 'Employees')
929 for val in sugar_data:
930 address_id = get_user_address(sugar_obj, cr, uid, val, context)
931 val['address_home_id/.id'] = address_id
932 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
933 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
935 val['resource_id/.id'] = resource_id[0].res_id
936 val['job_id/.id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
937 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
938 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
941 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
942 fields = ['shortcut', 'name', 'domain']
943 data = [salutation, salutation, domain]
944 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
951 def import_emails(sugar_obj, cr, uid, context=None):
954 map_emails = {'id': 'id',
956 'date':['__datetime__', 'date_sent'],
957 'email_from': 'from_addr_name',
958 'email_to': 'reply_to_addr',
959 'email_cc': 'cc_addrs_names',
960 'email_bcc': 'bcc_addrs_names',
961 'message_id': 'message_id',
962 'user_id/id': 'assigned_user_id',
963 'description': ['__prettyprint__', 'description', 'description_html'],
966 'partner_id/.id': 'partner_id/.id'
968 mailgate_obj = sugar_obj.pool.get('mailgate.message')
969 model_obj = sugar_obj.pool.get('ir.model.data')
970 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
971 sugar_data = sugar.search(PortType, sessionid, 'Emails')
972 for val in sugar_data:
973 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
974 for model in model_obj.browse(cr, uid, model_ids):
975 if model.model == 'res.partner':
976 val['partner_id/.id'] = model.res_id
978 val['res_id'] = model.res_id
979 val['model'] = model.model
980 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
981 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
984 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
988 partner_invoice_id = False
989 model_obj = sugar_obj.pool.get('ir.model.data')
990 partner_obj = sugar_obj.pool.get('res.partner')
991 partner_address_obj = sugar_obj.pool.get('res.partner.address')
992 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
993 for account_id in sugar_project_account:
994 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
996 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
997 partner_id = partner_obj.browse(cr, uid, model_id).id
998 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
999 partner_invoice_id = address_ids[0]
1000 return partner_id, partner_invoice_id
1002 def import_projects(sugar_obj, cr, uid, context=None):
1005 map_project = {'id': 'id',
1007 'date_start': ['__datetime__', 'estimated_start_date'],
1008 'date': ['__datetime__', 'estimated_end_date'],
1009 'user_id/id': 'assigned_user_id',
1010 'partner_id/.id': 'partner_id/.id',
1011 'contact_id/.id': 'contact_id/.id',
1014 project_obj = sugar_obj.pool.get('project.project')
1015 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1016 sugar_data = sugar.search(PortType, sessionid, 'Project')
1017 for val in sugar_data:
1018 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
1019 val['partner_id/.id'] = partner_id
1020 val['contact_id/.id'] = partner_invoice_id
1021 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
1022 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
1023 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1027 def import_project_tasks(sugar_obj, cr, uid, context=None):
1030 map_project_task = {'id': 'id',
1032 'date_start': ['__datetime__', 'date_start'],
1033 'date_end': ['__datetime__', 'date_finish'],
1034 'progress': 'progress',
1035 'project_id/name': 'project_name',
1036 'planned_hours': 'planned_hours',
1037 'total_hours': 'total_hours',
1038 'priority': 'priority',
1039 'description': 'description',
1040 'user_id/id': 'assigned_user_id',
1043 task_obj = sugar_obj.pool.get('project.task')
1044 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1045 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1046 for val in sugar_data:
1047 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1048 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1049 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1050 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1053 def import_leads(sugar_obj, cr, uid, context=None):
1058 'name': ['first_name', 'last_name'],
1059 'contact_name': ['first_name', 'last_name'],
1060 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1061 'partner_name': 'account_name',
1062 'email_from': 'email1',
1063 'phone': 'phone_work',
1064 'mobile': 'phone_mobile',
1065 'title.id': 'title.id',
1067 'street': 'primary_address_street',
1068 'street2': 'alt_address_street',
1069 'zip': 'primary_address_postalcode',
1070 'city':'primary_address_city',
1071 'user_id/id' : 'assigned_user_id',
1072 'stage_id.id' : 'stage_id.id',
1076 'referred': 'refered_by',
1078 'type_id/.id': 'type_id/.id',
1079 'country_id.id': 'country_id.id',
1080 'state_id.id': 'state_id.id'
1084 lead_obj = sugar_obj.pool.get('crm.lead')
1085 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1086 sugar_data = sugar.search(PortType, sessionid, 'Leads')
1087 for val in sugar_data:
1088 if val.get('do_not_call') == '0':
1090 if val.get('opportunity_id'):
1092 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1093 val['title/id'] = title_id
1094 val['type'] = 'lead'
1095 val['type_id/.id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1096 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1097 val['stage_id.id'] = stage_id
1098 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1099 if val.get('primary_address_country'):
1100 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1101 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1102 val['country_id.id'] = country_id
1103 val['state_id.id'] = state
1104 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1105 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1108 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1111 partner_contact_name = False
1112 partner_contact_email = False
1113 model_obj = sugar_obj.pool.get('ir.model.data')
1114 partner_address_obj = sugar_obj.pool.get('res.partner.address')
1115 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1116 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
1117 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1118 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1119 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1121 for contact in sugar_contact:
1122 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1124 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1125 address_id = partner_address_obj.browse(cr, uid, model_id)
1126 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1127 partner_contact_name = address_id.name
1128 partner_contact_email = address_id.email
1130 partner_contact_name = val.get('account_name')
1131 return partner_contact_name, partner_contact_email
1133 def import_opportunities(sugar_obj, cr, uid, context=None):
1136 map_opportunity = {'id' : 'id',
1138 'probability': 'probability',
1139 'partner_id/name': 'account_name',
1140 'title_action': 'next_step',
1141 'partner_address_id/name': 'partner_address_id/name',
1142 'planned_revenue': 'amount',
1143 'date_deadline': ['__datetime__', 'date_closed'],
1144 'user_id/id' : 'assigned_user_id',
1145 'stage_id/id' : 'stage_id/id',
1147 'categ_id.id': 'categ_id.id',
1148 'email_from': 'email_from'
1150 lead_obj = sugar_obj.pool.get('crm.lead')
1151 partner_obj = sugar_obj.pool.get('res.partner')
1152 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1153 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1154 for val in sugar_data:
1155 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1156 if not partner_xml_id:
1157 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1158 partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1159 val['partner_address_id/name'] = partner_contact_name
1160 val['email_from'] = partner_contact_email
1161 val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
1162 val['type'] = 'opportunity'
1163 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1164 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1165 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1168 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1171 fields = ['name', 'type']
1172 name = 'Opportunity_' + sugar_val['sales_stage']
1173 data = [sugar_val['sales_stage'], 'Opportunity']
1174 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1176 MAP_FIELDS = {'Opportunities': #Object Mapping name
1177 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
1178 'process' : import_opportunities,
1181 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
1182 'process' : import_leads,
1185 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
1186 'process' : import_partner_address,
1189 {'dependencies' : ['Users'], #Object to import before this table
1190 'process' : import_partners,
1193 {'dependencies' : [],
1194 'process' : import_users,
1197 {'dependencies' : ['Users'],
1198 'process' : import_documents,
1201 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1202 'process' : import_meetings,
1205 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1206 'process' : import_tasks,
1209 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1210 'process' : import_calls,
1213 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1214 'process' : import_projects,
1217 {'dependencies' : ['Users', 'Projects'],
1218 'process' : import_project_tasks,
1221 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1222 'process' : import_bug,
1226 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1227 'process' : import_emails,
1231 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1232 'process' : import_history,
1235 {'dependencies' : ['Resources', 'Users'],
1236 'process' : import_employees,
1239 {'dependencies' : ['Users'],
1240 'process' : import_resources,
1244 class import_sugarcrm(osv.osv):
1245 """Import SugarCRM DATA"""
1247 _name = "import.sugarcrm"
1248 _description = __doc__
1250 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1251 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1252 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1253 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1254 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1255 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1256 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1257 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1258 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1259 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1260 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1261 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1262 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1263 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1264 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1265 'username': fields.char('User Name', size=64),
1266 'password': fields.char('Password', size=24),
1268 _defaults = {#to be set to true, but easier for debugging
1269 'opportunity': True,
1280 'project_task': True,
1285 def get_key(self, cr, uid, ids, context=None):
1286 """Select Key as For which Module data we want import data."""
1290 for current in self.browse(cr, uid, ids, context):
1291 if current.opportunity:
1292 key_list.append('Opportunities')
1294 key_list.append('Users')
1296 key_list.append('Contacts')
1298 key_list.append('Accounts')
1299 if current.employee:
1300 key_list.append('Employees')
1302 key_list.append('Meetings')
1304 key_list.append('Tasks')
1306 key_list.append('Calls')
1308 key_list.append('Claims')
1310 key_list.append('Emails')
1312 key_list.append('Projects')
1313 if current.project_task:
1314 key_list.append('Project Tasks')
1316 key_list.append('Bugs')
1317 if current.attachment:
1318 key_list.append('Notes')
1319 if current.document:
1320 key_list.append('Documents')
1323 def import_all(self, cr, uid, ids, context=None):
1324 """Import all sugarcrm data into openerp module"""
1327 keys = self.get_key(cr, uid, ids, context)
1328 imported = set() #to invoid importing 2 times the sames modules
1330 if not key in imported:
1331 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1332 MAP_FIELDS[key]['process'](self, cr, uid, context)
1335 obj_model = self.pool.get('ir.model.data')
1336 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1337 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1339 'view_type': 'form',
1340 'view_mode': 'form',
1341 'res_model': 'import.message',
1342 'views': [(resource_id,'form')],
1343 'type': 'ir.actions.act_window',
1347 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1348 for dependency in dep:
1349 if not dependency in imported:
1350 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1351 dict[dependency]['process'](self, cr, uid, context)
1352 imported.add(dependency)