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': 'account_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 address_obj = sugar_obj.pool.get('res.partner.address')
176 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
177 sugar_data = sugar.search(PortType, sessionid, 'Contacts')
178 for val in sugar_data:
179 val['type'] = 'contact'
180 contact_emails = sugar.contact_emails_search(PortType, context.get('username', ''), context.get('password', ''), email_address=val.get('email1'))
181 val['email'] = (','.join(map(lambda x : x, contact_emails)))
182 if val.get('primary_address_country'):
183 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
184 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
185 val['country_id.id'] = country_id
186 val['state_id.id'] = state
187 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address, context)
188 address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
192 def import_users(sugar_obj, cr, uid, context=None):
195 'name': ['first_name', 'last_name'],
196 'login': 'user_name',
197 'context_lang' : 'context_lang',
198 'password' : 'password',
200 'context_department_id.id': 'context_department_id.id',
203 def get_users_department(sugar_obj, cr, uid, val, context=None):
208 return import_object(sugar_obj, cr, uid, fields, data, 'hr.department', 'hr_department_user', val, context=context)
212 department_id = False
214 user_obj = sugar_obj.pool.get('res.users')
215 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
216 sugar_data = sugar.search(PortType,sessionid, 'Users')
217 for val in sugar_data:
218 user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))])
220 val['.id'] = str(user_ids[0])
222 val['password'] = 'sugarcrm' #default password for all user
223 department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
224 val['context_department_id.id'] = department_id
225 val['context_lang'] = context.get('lang','en_US')
226 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user, context)
227 #All data has to be imported separatly because they don't have the same field
228 user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
231 def get_lead_status(sugar_obj, cr, uid, sugar_val,context=None):
234 fields = ['name', 'type']
235 name = 'lead_' + sugar_val['status']
236 data = [sugar_val['status'], 'lead']
237 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_lead_stage', name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val['status'])], context)
239 def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
243 state_dict = {'status': #field in the sugarcrm database
244 { #Mapping of sugarcrm stage : openerp opportunity stage
247 'In Progress': 'open',
248 'Recycled': 'cancel',
251 state = state_dict['status'].get(sugar_val['status'], '')
254 def get_user_address(sugar_obj, cr, uid, val, context=None):
257 address_obj = sugar_obj.pool.get('res.partner.address')
259 'name': ['first_name', 'last_name'],
260 'city': 'address_city',
261 'country_id': 'country_id',
262 'state_id': 'state_id',
263 'street': 'address_street',
264 'zip': 'address_postalcode',
266 address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') or '' +' '+ val.get('last_name'))])
267 if val.get('address_country'):
268 country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context)
269 state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context)
270 val['country_id'] = country_id
271 val['state_id'] = state_id
272 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address, context)
273 dict_val = dict(zip(fields,datas))
275 address_obj.write(cr, uid, address_ids, dict_val)
277 new_address_id = address_obj.create(cr,uid, dict_val)
278 return new_address_id
281 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):
313 map_partner_address={}
315 address_obj = sugar_obj.pool.get('res.partner.address')
316 address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))])
320 map_partner_address = {
323 'partner_id/id': 'account_id',
324 'phone': 'phone_office',
325 'mobile': 'phone_mobile',
329 if val.get('billing_address_street'):
330 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
332 if val.get('shipping_address_street'):
333 address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
337 def import_partners(sugar_obj, cr, uid, context=None):
343 'website': 'website',
344 'user_id/id': 'assigned_user_id',
346 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
347 'customer': 'customer',
348 'supplier': 'supplier',
350 partner_obj = sugar_obj.pool.get('res.partner')
351 address_obj = sugar_obj.pool.get('res.partner.address')
352 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
353 sugar_data = sugar.search(PortType, sessionid, 'Accounts')
354 for val in sugar_data:
355 add_id = get_address(sugar_obj, cr, uid, val, context)
356 val['customer'] = '1'
357 val['supplier'] = '0'
358 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner, context)
359 partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
360 for address in address_obj.browse(cr,uid,add_id):
361 data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
363 address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})
366 def get_category(sugar_obj, cr, uid, model, name, context=None):
369 fields = ['name', 'object_id']
371 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)], context)
373 def get_alarm_id(sugar_obj, cr, uid, val, context=None):
376 alarm_dict = {'60': '1 minute before',
377 '300': '5 minutes before',
378 '600': '10 minutes before',
379 '900': '15 minutes before',
380 '1800':'30 minutes before',
381 '3600': '1 hour before',
384 alarm_obj = sugar_obj.pool.get('res.alarm')
385 if alarm_dict.get(val):
386 alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
387 for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
391 def get_meeting_state(sugar_obj, cr, uid, val,context=None):
395 state_dict = {'status': #field in the sugarcrm database
396 { #Mapping of sugarcrm stage : openerp meeting stage
401 state = state_dict['status'].get(val, '')
404 def get_task_state(sugar_obj, cr, uid, val, context=None):
408 state_dict = {'status': #field in the sugarcrm database
409 { #Mapping of sugarcrm stage : openerp meeting stage
410 'Completed' : 'done',
411 'Not Started':'draft',
412 'In Progress': 'open',
413 'Pending Input': 'draft',
416 state = state_dict['status'].get(val, '')
419 def get_project_state(sugar_obj, cr, uid, val,context=None):
423 state_dict = {'status': #field in the sugarcrm database
424 { #Mapping of sugarcrm staus : openerp Projects state
427 'Published': 'close',
429 state = state_dict['status'].get(val, '')
432 def get_project_task_state(sugar_obj, cr, uid, val,context=None):
436 state_dict = {'status': #field in the sugarcrm database
437 { #Mapping of sugarcrm status : openerp Porject Tasks state
438 'Not Started': 'draft',
439 'In Progress': 'open',
441 'Pending Input': 'pending',
442 'Deferred': 'cancelled',
444 state = state_dict['status'].get(val, '')
447 def get_project_task_priority(sugar_obj, cr, uid, val,context=None):
451 priority_dict = {'priority': #field in the sugarcrm database
452 { #Mapping of sugarcrm status : openerp Porject Tasks state
457 priority = priority_dict['priority'].get(val, '')
461 def get_account(sugar_obj, cr, uid, val, context=None):
465 partner_address_id = False
466 partner_phone = False
467 partner_mobile = False
468 model_obj = sugar_obj.pool.get('ir.model.data')
469 address_obj = sugar_obj.pool.get('res.partner.address')
470 crm_obj = sugar_obj.pool.get('crm.lead')
471 project_obj = sugar_obj.pool.get('project.project')
472 issue_obj = sugar_obj.pool.get('project.issue')
473 if val.get('parent_type') == 'Accounts':
474 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner')])
476 model = model_obj.browse(cr, uid, model_ids)[0]
477 partner_id = model.res_id
478 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
480 address_id = address_obj.browse(cr, uid, address_ids[0])
481 partner_address_id = address_id.id
482 partner_phone = address_id.phone
483 partner_mobile = address_id.mobile
485 if val.get('parent_type') == 'Contacts':
486 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
488 model = model_obj.browse(cr, uid, model_ids)[0]
489 partner_address_id = model.res_id
490 address_id = address_obj.browse(cr, uid, partner_address_id)
491 partner_phone = address_id.phone
492 partner_mobile = address_id.mobile
493 partner_id = address_id and address_id.partner_id.id or False
495 if val.get('parent_type') == 'Opportunities':
496 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
498 model = model_obj.browse(cr, uid, model_ids)[0]
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')])
509 model = model_obj.browse(cr, uid, model_ids)[0]
510 proj_ids = model.res_id
511 proj_id = project_obj.browse(cr, uid, proj_ids)
512 partner_id = proj_id.partner_id.id
513 partner_address_id = proj_id.contact_id.id
514 partner_phone = proj_id.contact_id.phone
515 partner_mobile = proj_id.contact_id.mobile
517 if val.get('parent_type') == 'Bugs':
518 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
520 model = model_obj.browse(cr, uid, model_ids)[0]
521 issue_ids = model.res_id
522 issue_id = issue_obj.browse(cr, uid, issue_ids)
523 partner_id = issue_id.partner_id.id
524 partner_address_id = issue_id.partner_address_id.id
525 partner_phone = issue_id.partner_address_id.phone
526 partner_mobile = issue_id.partner_address_id.mobile
527 return partner_id, partner_address_id, partner_phone,partner_mobile
530 def import_documents(sugar_obj, cr, uid, context=None):
533 map_document = {'id' : 'id',
535 'description': 'description',
537 'datas_fname': 'datas_fname',
539 attach_obj = sugar_obj.pool.get('ir.attachment')
540 PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url',''))
541 sugar_data = sugar.search(PortType,sessionid, 'DocumentRevisions')
542 for val in sugar_data:
543 filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id')
544 f = open(filepath, "r")
547 val['datas'] = base64.encodestring(datas)
548 val['datas_fname'] = val.get('filename')
549 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document, context)
550 attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
553 def import_tasks(sugar_obj, cr, uid, context=None):
556 map_task = {'id' : 'id',
558 'date': ['__datetime__', 'date_start'],
559 'date_deadline' : ['__datetime__', 'date_due'],
560 'user_id/id': 'assigned_user_id',
561 'categ_id/id': 'categ_id/id',
562 'partner_id/.id': 'partner_id/.id',
563 'partner_address_id/.id': 'partner_address_id/.id',
566 meeting_obj = sugar_obj.pool.get('crm.meeting')
567 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
568 categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
569 sugar_data = sugar.search(PortType, sessionid, 'Tasks')
570 for val in sugar_data:
571 partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
572 if not partner_xml_id:
573 raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
574 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
575 val['partner_id/.id'] = partner_id
576 val['partner_address_id/.id'] = partner_address_id
577 val['categ_id/id'] = categ_id
578 val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context)
579 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task, context)
580 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
583 def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
586 model_obj = sugar_obj.pool.get('ir.model.data')
587 att_obj = sugar_obj.pool.get('calendar.attendee')
588 meeting_obj = sugar_obj.pool.get('crm.meeting')
589 user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id)
590 for user in user_dict:
591 user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context)
592 user_resource_id = model_obj.browse(cr, uid, user_model_ids)
594 user_id = user_resource_id[0].res_id
595 attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)])
597 attendees = attend_ids[0]
599 attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')})
600 meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context)
601 meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids)
603 meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]})
606 def import_meetings(sugar_obj, cr, uid, context=None):
609 map_meeting = {'id' : 'id',
611 'date': ['__datetime__', 'date_start'],
612 'duration': ['duration_hours', 'duration_minutes'],
613 'location': 'location',
614 'alarm_id/.id': 'alarm_id/.id',
615 'user_id/id': 'assigned_user_id',
616 'partner_id/.id':'partner_id/.id',
617 'partner_address_id/.id':'partner_address_id/.id',
620 meeting_obj = sugar_obj.pool.get('crm.meeting')
621 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
622 sugar_data = sugar.search(PortType, sessionid, 'Meetings')
623 for val in sugar_data:
624 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
625 val['partner_id/.id'] = partner_id
626 val['partner_address_id/.id'] = partner_address_id
627 val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
628 val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
629 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting, context)
630 meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
631 get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
634 def get_calls_state(sugar_obj, cr, uid, val,context=None):
638 state_dict = {'status': #field in the sugarcrm database
639 { #Mapping of sugarcrm stage : openerp calls stage
642 'Not Held': 'pending',
644 state = state_dict['status'].get(val, '')
647 def import_calls(sugar_obj, cr, uid, context=None):
650 map_calls = {'id' : 'id',
652 'date': ['__datetime__', 'date_start'],
653 'duration': ['duration_hours', 'duration_minutes'],
654 'user_id/id': 'assigned_user_id',
655 'partner_id/.id': 'partner_id/.id',
656 'partner_address_id/.id': 'partner_address_id/.id',
657 'categ_id/id': 'categ_id/id',
659 'partner_phone': 'partner_phone',
660 'partner_mobile': 'partner_mobile',
661 'opportunity_id/id': 'opportunity_id/id',
664 phonecall_obj = sugar_obj.pool.get('crm.phonecall')
665 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
666 sugar_data = sugar.search(PortType, sessionid, 'Calls')
667 for val in sugar_data:
668 sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
670 for call_opportunity in sugar_call_leads:
671 val['opportunity_id/id'] = call_opportunity
672 categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))
673 val['categ_id/id'] = categ_id
674 partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
675 val['partner_id/.id'] = partner_id
676 val['partner_address_id/.id'] = partner_address_id
677 val['partner_phone'] = partner_phone
678 val['partner_mobile'] = partner_mobile
679 val['state'] = get_calls_state(sugar_obj, cr, uid, val.get('status'), context)
680 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls, context)
681 phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
684 def import_resources(sugar_obj, cr, uid, context=None):
687 map_resource = {'id' : 'user_hash',
688 'name': ['first_name', 'last_name'],
690 resource_obj = sugar_obj.pool.get('resource.resource')
691 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
692 sugar_data = sugar.search(PortType, sessionid, 'Employees')
693 for val in sugar_data:
694 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
695 resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
698 def get_bug_priority(sugar_obj, cr, uid, val,context=None):
702 priority_dict = {'priority': #field in the sugarcrm database
703 { #Mapping of sugarcrm priority : openerp bugs priority
709 priority = priority_dict['priority'].get(val, '')
712 def get_claim_priority(sugar_obj, cr, uid, val,context=None):
716 priority_dict = {'priority': #field in the sugarcrm database
717 { #Mapping of sugarcrm priority : openerp claims priority
722 priority = priority_dict['priority'].get(val, '')
725 def get_bug_state(sugar_obj, cr, uid, val,context=None):
729 state_dict = {'status': #field in the sugarcrm database
730 { #Mapping of sugarcrm status : openerp Bugs state
734 'Pending': 'pending',
735 'Rejected': 'cancel',
737 state = state_dict['status'].get(val, '')
740 def get_claim_state(sugar_obj, cr, uid, val,context=None):
744 state_dict = {'status': #field in the sugarcrm database
745 { #Mapping of sugarcrm status : openerp claim state
749 'Pending Input': 'pending',
750 'Rejected': 'cancel',
751 'Duplicate': 'draft',
753 state = state_dict['status'].get(val, '')
757 def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None):
761 partner_address_id = False
762 partner_phone = False
763 partner_email = False
764 model_obj = sugar_obj.pool.get('ir.model.data')
765 address_obj = sugar_obj.pool.get('res.partner.address')
766 model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')])
768 model = model_obj.browse(cr, uid, model_ids)[0]
769 partner_id = model.res_id
770 address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)])
772 address_id = address_obj.browse(cr, uid, address_ids[0])
773 partner_address_id = address_id.id
774 partner_phone = address_id.phone
775 partner_mobile = address_id.email
776 return partner_id, partner_address_id, partner_phone,partner_email
778 def import_claims(sugar_obj, cr, uid, context=None):
781 map_claim = {'id' : 'id',
783 'date': ['__datetime__', 'date_entered'],
784 'user_id/id': 'assigned_user_id',
785 'priority':'priority',
786 'partner_id/.id': 'partner_id/.id',
787 'partner_address_id/.id': 'partner_address_id/.id',
788 'partner_phone': 'partner_phone',
789 'partner_mobile': 'partner_email',
790 'description': 'description',
793 claim_obj = sugar_obj.pool.get('crm.claim')
794 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
795 sugar_data = sugar.search(PortType, sessionid, 'Cases')
796 for val in sugar_data:
797 partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context)
798 val['partner_id/.id'] = partner_id
799 val['partner_address_id/.id'] = partner_address_id
800 val['partner_phone'] = partner_phone
801 val['email_from'] = partner_email
802 val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context)
803 val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context)
804 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_claim, context)
805 claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
808 def import_bug(sugar_obj, cr, uid, context=None):
811 map_resource = {'id' : 'id',
813 'project_id/.id':'project_id/.id',
814 'categ_id/id': 'categ_id/id',
815 'priority':'priority',
816 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'],
819 issue_obj = sugar_obj.pool.get('project.issue')
820 project_obj = sugar_obj.pool.get('project.project')
821 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
822 sugar_data = sugar.search(PortType, sessionid, 'Bugs')
823 for val in sugar_data:
824 project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')])
826 project_id = project_ids[0]
828 project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'})
829 val['project_id/.id'] = project_id
830 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type'))
831 val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context)
832 val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context)
833 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource, context)
834 issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
837 def get_job_id(sugar_obj, cr, uid, val, context=None):
842 return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, context=context)
844 def get_campaign_id(sugar_obj, cr, uid, val, context=None):
849 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, context=context)
851 def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None):
855 attachment_obj = sugar_obj.pool.get('ir.attachment')
856 partner_obj = sugar_obj.pool.get('res.partner')
857 model_obj = sugar_obj.pool.get('ir.model.data')
858 mailgate_obj = sugar_obj.pool.get('mailgate.message')
859 attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))])
860 if not attach_ids and Filename:
861 if parent_type == 'Accounts':
862 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')})
864 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)})
865 message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context)
866 message_xml_id = model_obj.browse(cr, uid, message_model_ids)
868 if parent_type == 'Accounts':
869 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')})
871 mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})
874 def import_history(sugar_obj, cr, uid, context=None):
877 map_attachment = {'id' : 'id',
879 'date': ['__datetime__', 'date_entered'],
880 'user_id/id': 'assigned_user_id',
881 'description': ['__prettyprint__','description', 'description_html'],
884 'partner_id/.id' : 'partner_id/.id',
886 mailgate_obj = sugar_obj.pool.get('mailgate.message')
887 model_obj = sugar_obj.pool.get('ir.model.data')
888 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
889 sugar_data = sugar.search(PortType, sessionid, 'Notes')
890 for val in sugar_data:
891 File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
892 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
894 model = model_obj.browse(cr, uid, model_ids)[0]
895 if model.model == 'res.partner':
896 val['partner_id/.id'] = model.res_id
898 val['res_id'] = model.res_id
899 val['model'] = model.model
900 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment, context)
901 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
902 get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context)
905 def import_employees(sugar_obj, cr, uid, context=None):
908 map_employee = {'id' : 'user_hash',
909 'resource_id/.id': 'resource_id/.id',
910 'name': ['first_name', 'last_name'],
911 'work_phone': 'phone_work',
912 'mobile_phone': 'phone_mobile',
913 'user_id/name': ['first_name', 'last_name'],
914 'address_home_id/.id': 'address_home_id/.id',
915 'notes': 'description',
916 #TODO: Creation of Employee create problem.
917 # 'coach_id/id': 'reports_to_id',
918 'job_id/id': 'job_id/id'
920 employee_obj = sugar_obj.pool.get('hr.employee')
921 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
922 sugar_data = sugar.search(PortType, sessionid, 'Employees')
923 for val in sugar_data:
924 address_id = get_user_address(sugar_obj, cr, uid, val, context)
925 val['address_home_id/.id'] = address_id
926 model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context)
927 resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids)
929 val['resource_id/.id'] = resource_id[0].res_id
930 val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
931 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee, context)
932 employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
935 def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
936 fields = ['shortcut', 'name', 'domain']
937 data = [salutation, salutation, domain]
938 return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context)
940 def import_emails(sugar_obj, cr, uid, context=None):
943 map_emails = {'id': 'id',
945 'date':['__datetime__', 'date_sent'],
946 'email_from': 'from_addr_name',
947 'email_to': 'reply_to_addr',
948 'email_cc': 'cc_addrs_names',
949 'email_bcc': 'bcc_addrs_names',
950 'message_id': 'message_id',
951 'user_id/id': 'assigned_user_id',
952 'description': ['__prettyprint__', 'description', 'description_html'],
955 'partner_id/.id': 'partner_id/.id'
957 mailgate_obj = sugar_obj.pool.get('mailgate.message')
958 model_obj = sugar_obj.pool.get('ir.model.data')
959 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
960 sugar_data = sugar.search(PortType, sessionid, 'Emails')
961 for val in sugar_data:
962 model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])])
963 for model in model_obj.browse(cr, uid, model_ids):
964 if model.model == 'res.partner':
965 val['partner_id/.id'] = model.res_id
967 val['res_id'] = model.res_id
968 val['model'] = model.model
969 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails, context)
970 mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
973 def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
977 partner_invoice_id = False
978 model_obj = sugar_obj.pool.get('ir.model.data')
979 partner_obj = sugar_obj.pool.get('res.partner')
980 partner_address_obj = sugar_obj.pool.get('res.partner.address')
981 sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
982 for account_id in sugar_project_account:
983 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context)
985 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
986 partner_id = partner_obj.browse(cr, uid, model_id).id
987 address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')])
988 partner_invoice_id = address_ids[0]
989 return partner_id, partner_invoice_id
991 def import_projects(sugar_obj, cr, uid, context=None):
994 map_project = {'id': 'id',
996 'date_start': ['__datetime__', 'estimated_start_date'],
997 'date': ['__datetime__', 'estimated_end_date'],
998 'user_id/id': 'assigned_user_id',
999 'partner_id/.id': 'partner_id/.id',
1000 'contact_id/.id': 'contact_id/.id',
1003 project_obj = sugar_obj.pool.get('project.project')
1004 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1005 sugar_data = sugar.search(PortType, sessionid, 'Project')
1006 for val in sugar_data:
1007 partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context)
1008 val['partner_id/.id'] = partner_id
1009 val['contact_id/.id'] = partner_invoice_id
1010 val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context)
1011 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project, context)
1012 project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1016 def import_project_tasks(sugar_obj, cr, uid, context=None):
1019 map_project_task = {'id': 'id',
1021 'date_start': ['__datetime__', 'date_start'],
1022 'date_end': ['__datetime__', 'date_finish'],
1023 'progress': 'progress',
1024 'project_id/name': 'project_name',
1025 'planned_hours': 'planned_hours',
1026 'total_hours': 'total_hours',
1027 'priority': 'priority',
1028 'description': 'description',
1029 'user_id/id': 'assigned_user_id',
1032 task_obj = sugar_obj.pool.get('project.task')
1033 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1034 sugar_data = sugar.search(PortType, sessionid, 'ProjectTask')
1035 for val in sugar_data:
1036 val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context)
1037 val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context)
1038 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_project_task, context)
1039 task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1042 def import_leads(sugar_obj, cr, uid, context=None):
1047 'name': ['first_name', 'last_name'],
1048 'contact_name': ['first_name', 'last_name'],
1049 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'],
1050 'partner_name': 'account_name',
1051 'email_from': 'email1',
1052 'phone': 'phone_work',
1053 'mobile': 'phone_mobile',
1054 'title.id': 'title.id',
1056 'street': 'primary_address_street',
1057 'street2': 'alt_address_street',
1058 'zip': 'primary_address_postalcode',
1059 'city':'primary_address_city',
1060 'user_id/id' : 'assigned_user_id',
1061 'stage_id/id' : 'stage_id/id',
1065 'referred': 'refered_by',
1067 'type_id/id': 'type_id/id',
1068 'country_id.id': 'country_id.id',
1069 'state_id.id': 'state_id.id'
1071 lead_obj = sugar_obj.pool.get('crm.lead')
1072 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1073 sugar_data = sugar.search(PortType, sessionid, 'Leads')
1074 for val in sugar_data:
1075 if val.get('do_not_call') == '0':
1077 if val.get('opportunity_id'):
1079 if val.get('salutation'):
1080 title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context)
1081 val['title/id'] = title_id
1082 val['type'] = 'lead'
1083 val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
1084 stage_id = get_lead_status(sugar_obj, cr, uid, val, context)
1085 val['stage_id/id'] = stage_id
1086 val['state'] = get_lead_state(sugar_obj, cr, uid, val,context)
1087 if val.get('primary_address_country'):
1088 country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context)
1089 state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context)
1090 val['country_id.id'] = country_id
1091 val['state_id.id'] = state
1092 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead, context)
1093 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1096 def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
1099 partner_contact_name = False
1100 partner_contact_email = False
1101 model_obj = sugar_obj.pool.get('ir.model.data')
1102 partner_address_obj = sugar_obj.pool.get('res.partner.address')
1103 model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
1104 model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name
1105 sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
1106 sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
1107 sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
1109 for contact in sugar_contact:
1110 model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
1112 model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
1113 address_id = partner_address_obj.browse(cr, uid, model_id)
1114 partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
1115 partner_contact_name = address_id.name
1116 partner_contact_email = address_id.email
1118 partner_contact_name = val.get('account_name')
1119 return partner_contact_name, partner_contact_email
1121 def import_opportunities(sugar_obj, cr, uid, context=None):
1124 map_opportunity = {'id' : 'id',
1126 'probability': 'probability',
1127 'partner_id/name': 'account_name',
1128 'title_action': 'next_step',
1129 'partner_address_id/name': 'partner_address_id/name',
1130 'planned_revenue': 'amount',
1131 'date_deadline': ['__datetime__', 'date_closed'],
1132 'user_id/id' : 'assigned_user_id',
1133 'stage_id/id' : 'stage_id/id',
1135 'categ_id/id': 'categ_id/id',
1136 'email_from': 'email_from'
1138 lead_obj = sugar_obj.pool.get('crm.lead')
1139 partner_obj = sugar_obj.pool.get('res.partner')
1140 PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
1141 sugar_data = sugar.search(PortType, sessionid, 'Opportunities')
1142 for val in sugar_data:
1143 partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
1144 if not partner_xml_id:
1145 raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
1146 partner_contact_name, partner_contact_email = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
1147 val['partner_address_id/name'] = partner_contact_name
1148 val['email_from'] = partner_contact_email
1149 val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))
1150 val['type'] = 'opportunity'
1151 val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context)
1152 fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity)
1153 lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
1156 def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None):
1159 fields = ['name', 'type']
1160 name = 'Opportunity_' + sugar_val['sales_stage']
1161 data = [sugar_val['sales_stage'], 'Opportunity']
1162 return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', 'crm_stage', name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context)
1164 MAP_FIELDS = {'Opportunities': #Object Mapping name
1165 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table
1166 'process' : import_opportunities,
1169 {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table
1170 'process' : import_leads,
1173 {'dependencies' : ['Users', 'Accounts'], #Object to import before this table
1174 'process' : import_partner_address,
1177 {'dependencies' : ['Users'], #Object to import before this table
1178 'process' : import_partners,
1181 {'dependencies' : [],
1182 'process' : import_users,
1185 {'dependencies' : ['Users'],
1186 'process' : import_documents,
1189 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Projects', 'Opportunities', 'Tasks'],
1190 'process' : import_meetings,
1193 {'dependencies' : ['Accounts', 'Contacts', 'Users'],
1194 'process' : import_tasks,
1197 {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities'],
1198 'process' : import_calls,
1201 {'dependencies' : ['Users', 'Accounts', 'Contacts'],
1202 'process' : import_projects,
1205 {'dependencies' : ['Users', 'Projects'],
1206 'process' : import_project_tasks,
1209 {'dependencies' : ['Users', 'Projects', 'Project Tasks'],
1210 'process' : import_bug,
1213 {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
1214 'process' : import_claims,
1217 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1218 'process' : import_emails,
1222 {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
1223 'process' : import_history,
1226 {'dependencies' : ['Resources', 'Users'],
1227 'process' : import_employees,
1230 {'dependencies' : ['Users'],
1231 'process' : import_resources,
1235 class import_sugarcrm(osv.osv):
1236 """Import SugarCRM DATA"""
1238 _name = "import.sugarcrm"
1239 _description = __doc__
1241 'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
1242 'user': fields.boolean('Users', help="If Users are checked, SugarCRM Users data imported in OpenERP Users form"),
1243 'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
1244 'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM Accounts data imported in OpenERP partners form"),
1245 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
1246 'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
1247 'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
1248 'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
1249 'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
1250 'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
1251 'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
1252 'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
1253 'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
1254 'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
1255 'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
1256 'username': fields.char('User Name', size=64),
1257 'password': fields.char('Password', size=24),
1259 _defaults = {#to be set to true, but easier for debugging
1260 'opportunity': True,
1271 'project_task': True,
1276 def get_key(self, cr, uid, ids, context=None):
1277 """Select Key as For which Module data we want import data."""
1281 for current in self.browse(cr, uid, ids, context):
1282 if current.opportunity:
1283 key_list.append('Opportunities')
1285 key_list.append('Users')
1287 key_list.append('Contacts')
1289 key_list.append('Accounts')
1290 if current.employee:
1291 key_list.append('Employees')
1293 key_list.append('Meetings')
1295 key_list.append('Tasks')
1297 key_list.append('Calls')
1299 key_list.append('Claims')
1301 key_list.append('Emails')
1303 key_list.append('Projects')
1304 if current.project_task:
1305 key_list.append('Project Tasks')
1307 key_list.append('Bugs')
1308 if current.attachment:
1309 key_list.append('Notes')
1310 if current.document:
1311 key_list.append('Documents')
1314 def import_all(self, cr, uid, ids, context=None):
1315 """Import all sugarcrm data into openerp module"""
1318 keys = self.get_key(cr, uid, ids, context)
1319 imported = set() #to invoid importing 2 times the sames modules
1321 if not key in imported:
1322 self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context)
1323 MAP_FIELDS[key]['process'](self, cr, uid, context)
1326 obj_model = self.pool.get('ir.model.data')
1327 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')])
1328 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])
1330 'view_type': 'form',
1331 'view_mode': 'form',
1332 'res_model': 'import.message',
1333 'views': [(resource_id,'form')],
1334 'type': 'ir.actions.act_window',
1338 def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None):
1339 for dependency in dep:
1340 if not dependency in imported:
1341 self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context)
1342 dict[dependency]['process'](self, cr, uid, context)
1343 imported.add(dependency)