From 113a42339413bc4fe45170f081feb7cef1963f46 Mon Sep 17 00:00:00 2001 From: "tfr@openerp.com" <> Date: Thu, 12 May 2011 10:56:24 +0200 Subject: [PATCH] [IMP] use import_base framework bzr revid: tfr@openerp.com-20110512085624-x16f1b3dt94flddy --- addons/import_sugarcrm/__init__.py | 2 +- addons/import_sugarcrm/import_framework.py | 205 --- addons/import_sugarcrm/import_sugarcrm.py | 1269 +++++------------ addons/import_sugarcrm/old_import.py | 514 +++++++ .../sugarcrm_fields_mapping.py | 1 - 5 files changed, 881 insertions(+), 1110 deletions(-) delete mode 100644 addons/import_sugarcrm/import_framework.py create mode 100644 addons/import_sugarcrm/old_import.py delete mode 100644 addons/import_sugarcrm/sugarcrm_fields_mapping.py diff --git a/addons/import_sugarcrm/__init__.py b/addons/import_sugarcrm/__init__.py index 0525b0a4aec..1d8b36260d2 100644 --- a/addons/import_sugarcrm/__init__.py +++ b/addons/import_sugarcrm/__init__.py @@ -23,4 +23,4 @@ import import_sugarcrm import sugar import wizard import sugarcrm_fields_mapping -import import_framework + diff --git a/addons/import_sugarcrm/import_framework.py b/addons/import_sugarcrm/import_framework.py deleted file mode 100644 index b8318fca277..00000000000 --- a/addons/import_sugarcrm/import_framework.py +++ /dev/null @@ -1,205 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -import tools -import pytz -import time -from datetime import datetime, timedelta, date -from dateutil import parser -import dateutil - - -MODULE_NAME = 'sugarcrm_import' - - -DO_NOT_FIND_DOMAIN = [('id', '=', 0)] - -def xml_id_exist(obj, cr, uid, table, sugar_id, context=None): - if not sugar_id: - return False - - xml_id = generate_xml_id(sugar_id, table) - id = obj.pool.get('ir.model.data').search(cr, uid, [('name', '=', xml_id), ('module', '=', MODULE_NAME)]) - return id and xml_id or False - -def get_mapped_id(obj, cr, uid, table, sugar_id, context=None): - if not sugar_id: - return False - - xml_id = generate_xml_id(sugar_id, table) - return obj.pool.get('ir.model.data').get_object_reference(cr, uid, MODULE_NAME, xml_id)[1] - - -def mapped_id(obj, cr, uid, res_model, sugar_id, id, context=None): - """ - This function create the mapping between an already existing data and the similar data of sugarcrm - @param res_model: model of the mapped object - @param sugar_id: external sugar id - @param id: id in the database - - @return : the xml_id or sugar_id - """ - if not context: - context = {} - ir_model_data_obj = obj.pool.get('ir.model.data') - id = ir_model_data_obj._update(cr, uid, res_model, - MODULE_NAME, {}, mode='update', xml_id=sugar_id, - noupdate=True, res_id=id, context=context) - return sugar_id - -def mapped_id_if_exist(sugar_obj, cr, uid, model, domain, xml_id, context=None): - """ - @param domain : search domain to find existing record, should return a unique record - @param xml_id: xml_id give to the mapping - - @return : the xml_id if the record exist in the db, False otherwise - """ - obj = sugar_obj.pool.get(model) - ids = obj.search(cr, uid, domain, context=context) - #print "ids", ids, "domain", domain - if ids: - return mapped_id(obj, cr, uid, model, xml_id, ids[0], context=context) - return False - -def import_module(obj, cr, uid, model, mapping, datas, table, context=None): - """ - @param mapping : definition of the mapping - @see: sugarcrm_fields_mapp - @param datas : list of dictionnaries - datas = [data_1, data_2, ..] - data_i is a map external field_name => value - and each data_i have a external id => in data_id['id'] - """ - mapping['id'] = 'id_new' - res = [] - for data in datas: - self_dependencies = [] - for k in data.keys(): - if '_new' in k: - self_dependencies.append(k[:-4]) - - data['id_new'] = generate_xml_id(data['id'], table) - fields, values = _sugarcrm_fields_mapp(data, mapping, context) - res.append(values) - - model_obj = obj.pool.get(model) - model_obj.import_data(cr, uid, fields, res, mode='update', current_module=MODULE_NAME, noupdate=True, context=context) - for field in self_dependencies: - _import_self_dependencies(model_obj, cr, uid, field, datas, context) - -def import_object_mapping(sugar_obj, cr, uid, mapping, data, model, table, name, domain_search=False, context=None): - fields, datas = _sugarcrm_fields_mapp(data, mapping, context) - return import_object(sugar_obj, cr, uid, fields, datas, model, table, name, domain_search, context=context) - -def import_object(sugar_obj, cr, uid, fields, data, model, table, name, domain_search=False, context=None): - """ - 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 - use import_data that will take care to create/update or do nothing with the data - this method return the xml_id - @param fields: list of fields needed to create the object without id - @param data: the list of the data, in the same order as the field - ex : fields = ['firstname', 'lastname'] ; data = ['John', 'Mc donalds'] - @param model: the openerp's model of the create/update object - @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 - @param unique_name: the name of the object that we want to create/update get the id - @param domain_search : the domain that should find the unique existing record - - @return: the xml_id of the ressources - """ - domain_search = not domain_search and [('name', 'ilike', name)] or domain_search - obj = sugar_obj.pool.get(model) - xml_id = generate_xml_id(name, table) - - xml_ref = mapped_id_if_exist(obj, cr, uid, model, domain_search, xml_id, context=context) - fields.append('id') - data.append(xml_id) - obj.import_data(cr, uid, fields, [data], mode='update', current_module=MODULE_NAME, noupdate=True, context=context) - return xml_ref or xml_id - - -#TODO for more then one field that need parent -def _import_self_dependencies(obj, cr, uid, parent_field, datas, context): - """ - @param parent_field: the name of the field that generate a self_dependencies, we call the object referenced in this - field the parent of the object - @param datas: a list of dictionnaries - Dictionnaries need to contains - id_new : the xml_id of the object - field_new : the xml_id of the parent - """ - fields = ['id', parent_field+'/id'] - for data in datas: - if data.get(parent_field + '_new'): - values = [data['id_new'], data[parent_field + '_new']] - obj.import_data(cr, uid, fields, [values], mode='update', current_module=MODULE_NAME, noupdate=True, context=context) - - -def generate_xml_id(name, table): - """ - @param name: name of the object, has to be unique in for a given table - @param table : table where the record we want generate come from - @return: a unique xml id for record, the xml_id will be the same given the same table and same name - To be used to avoid duplication of data that don't have ids - """ - sugar_instance = "sugarcrm" #TODO need to be changed we information is known in the wizard - name = name.replace('.', '_').replace(',', '_') - #print "generate xml_id", table, name - return sugar_instance + "_" + table + "_" + name - - -def _sugarcrm_fields_mapp(dict_sugar, openerp_dict, context=None): - #TODO write documentation - if not context: - context = {} - if 'tz' in context and context['tz']: - time_zone = context['tz'] - else: - time_zone = tools.get_server_timezone() - au_tz = pytz.timezone(time_zone) - fields=[] - data_lst = [] - for key,val in openerp_dict.items(): - if key not in fields and dict_sugar: - fields.append(key) - if isinstance(val, list) and val: - #Allow to print a bit more pretty way long list of data in the same field - if len(val) >= 1 and val[0] == "__prettyprint__": - val = val[1:] - data_lst.append('\n\n'.join(map(lambda x : x + ": " + dict_sugar.get(x,''), val))) - elif val[0] == '__datetime__': - val = val[1] - if dict_sugar.get(val) and len(dict_sugar.get(val))<=10: - updated_dt = date.fromtimestamp(time.mktime(time.strptime(dict_sugar.get(val), '%Y-%m-%d'))) or False - elif dict_sugar.get(val): - convert_date = datetime.strptime(dict_sugar.get(val), '%Y-%m-%d %H:%M:%S') - edate = convert_date.replace(tzinfo=au_tz) - au_dt = au_tz.normalize(edate.astimezone(au_tz)) - updated_dt = datetime(*au_dt.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S') - else: - updated_dt = False - data_lst.append(updated_dt) - else: - if key == 'duration': - data_lst.append('.'.join(map(lambda x : dict_sugar.get(x,''), val))) - else: - data_lst.append(' '.join(map(lambda x : dict_sugar.get(x,''), val))) - else: - data_lst.append(dict_sugar.get(val,'')) - return fields,data_lst \ No newline at end of file diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 8d3814dbab2..3a6592318ac 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -19,181 +19,286 @@ # ############################################################################## from osv import fields, osv -from operator import itemgetter import sugar -import sugarcrm_fields_mapping from tools.translate import _ -from import_framework import * -import pprint +from import_base.import_framework import * +from import_base.mapper import * + import base64 +import pprint pp = pprint.PrettyPrinter(indent=4) -#print 'xml ids exist', sugar_obj.pool.get('ir.model.data').search(cr, uid, [('name', 'in', address_id )]) +#copy old import here +class related_ref(dbmapper): + def __init__(self, type): + self.type = type + + def __call__(self, external_val): + if external_val.get('parent_type') in self.type and external_val.get('parent_id'): + return self.parent.xml_id_exist(external_val['parent_type'], external_val['parent_id']) + return '' -OPENERP_FIEDS_MAPS = {'Leads': 'crm.lead', - 'Opportunities': 'crm.lead', - 'Contacts': 'res.partner.address', - 'Accounts': 'res.partner', - 'Resources': 'resource.resource', - 'Users': 'res.users', - 'Meetings': 'crm.meeting', - 'Calls': 'crm.phonecall', - 'Claims': 'crm.claim', - 'Employee': 'hr.employee', - 'Project': 'project.project', - 'ProjectTask': 'project.task', - 'Bugs': 'project.issue', - 'Documents': 'ir.attachment', - -} -TABLE_CONTACT = 'Contacts' -TABLE_ACCOUNT = 'Accounts' -TABLE_USER = 'Users' -TABLE_EMPLOYEE = 'Employees' -TABLE_RESSOURCE = "resource" -TABLE_OPPORTUNITY = 'Opportunities' -TABLE_LEAD = 'Leads' -TABLE_STAGE = 'crm_stage' -TABLE_CALL = 'Calls' -TABLE_MEETING = 'Meetings' -TABLE_TASK = 'Tasks' +class sugar_import(import_framework): + TABLE_CONTACT = 'Contacts' + TABLE_ACCOUNT = 'Accounts' + TABLE_USER = 'Users' + TABLE_EMPLOYEE = 'Employees' + TABLE_RESSOURCE = "resource" + TABLE_OPPORTUNITY = 'Opportunities' + TABLE_LEAD = 'Leads' + TABLE_STAGE = 'crm_stage' + TABLE_CALL = 'Calls' + TABLE_MEETING = 'Meetings' + TABLE_TASK = 'Tasks' + TABLE_PROJECT = 'Project' + TABLE_PROJECT_TASK = 'ProjectTask' + TABLE_CASE = 'Cases' + + + def initialize(self): + #login + PortType,sessionid = sugar.login(self.context.get('username',''), self.context.get('password',''), self.context.get('url','')) + self.context['port'] = PortType + self.context['session_id'] = sessionid + + def get_data(self, table): + + return sugar.search(self.context.get('port'), self.context.get('session_id'), table) + """ + Common import method + """ + def get_category(self, val, model, name): + fields = ['name', 'object_id'] + data = [name, model] + return self.import_object(fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)]) -STATE_MAP = { - TABLE_LEAD: { - 'New' : 'draft', - 'Assigned':'open', - 'In Progress': 'open', - 'Recycled': 'cancel', - 'Dead': 'done', - }, - TABLE_CALL : { - 'Planned' : 'open', - 'Held':'done', - 'Not Held': 'pending', - }, - TABLE_MEETING : { + def get_job_title(self, dict, salutation): + fields = ['shortcut', 'name', 'domain'] + data = [salutation, salutation, 'Contact'] + return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)]) + + def get_campaign_id(self, dict, val): + fields = ['name'] + data = [val] + return self.import_object(fields, data, 'crm.case.resource.type', 'crm_campaign', val) + + def get_all_states(self, external_val, country_id): + """Get states or create new state unless country_id is False""" + state_code = external_val[0:3] #take the tree first char + fields = ['country_id/id', 'name', 'code'] + data = [country_id, external_val, state_code] + if country_id: + return self.import_object(fields, data, 'res.country.state', 'country_state', external_val) + + return False + + def get_all_countries(self, val): + """Get Country, if no country match do not create anything, to avoid duplicate country code""" + return self.mapped_id_if_exist('res.country', [('name', 'ilike', val)], 'country', val) + + def get_float_time(self, dict, hour, min): + min = int(min) * 100 / 60 + return "%s.%i" % (hour, min) + + """ + import meeting + """ + meeting_state = { 'Planned' : 'draft', 'Held': 'open', 'Not Held': 'draft', - }, - TABLE_TASK : { - 'Completed' : 'done', - 'Not Started':'draft', - 'In Progress': 'open', - 'Pending Input': 'draft', - 'deferred': 'cancel' } -} - -def get_sugar_data(module_name, context=None): - if not context: - context = {} - PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) - return sugar.search(PortType,sessionid, module_name) - -def get_all_states(sugar_obj, cr, uid, external_val, country_id, context=None): - """Get states or create new state unless country_id is False""" - state_code = external_val[0:3] #take the tree first char - fields = ['country_id/id', 'name', 'code'] - data = [country_id, external_val, state_code] - if country_id: - return import_object(sugar_obj, cr, uid, fields, data, 'res.country.state', 'country_state', external_val, context=context) - - return False - -def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None): - """Get Country, if no country match do not create anything, to avoid duplicate country code""" - xml_id = generate_xml_id(sugar_country_val, 'country') - return mapped_id_if_exist(sugar_obj, cr, uid, 'res.country', [('name', 'ilike', sugar_country_val)], xml_id, context=context) - -def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None): - fields = ['shortcut', 'name', 'domain'] - data = [salutation, salutation, domain] - return import_object(sugar_obj, cr, uid, fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)], context=context) - -def import_partner_address(sugar_obj, cr, uid, context=None): - map_partner_address = { - 'name': ['first_name', 'last_name'], - 'partner_id/id': 'partner_id/id', - 'phone': 'phone_work', - 'mobile': 'phone_mobile', - 'fax': 'phone_fax', - 'function': 'title', - 'street': 'primary_address_street', - 'zip': 'primary_address_postalcode', - 'city': 'primary_address_city', - 'country_id/id': 'country_id/id', - 'state_id/id': 'state_id/id', - 'email': 'email', - 'type': 'type' + + def get_alarm_id(self, dict_val, val): + alarm_dict = { + '60': '1 minute before', + '300': '5 minutes before', + '600': '10 minutes before', + '900': '15 minutes before', + '1800':'30 minutes before', + '3600': '1 hour before', + } + return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val) + + #TODO attendees + + def get_meeting_mapping(self): + return { + 'model' : 'crm.meeting', + 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD], + 'map' : { + 'name': 'name', + 'date': 'date_start', + 'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')), + 'location': 'location', + 'alarm_id/id': call(self.get_alarm_id, value('reminder_time')), + 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), + 'partner_id/id': related_ref(self.TABLE_ACCOUNT), + 'partner_address_id/id': related_ref(self.TABLE_CONTACT), + 'state': map_val('status', self.meeting_state) + } } - sugar_data = get_sugar_data(TABLE_CONTACT, context) + """ + import Opportunity + """ + def get_opportunity_status(self, sugar_val): + fields = ['name', 'type'] + name = 'Opportunity_' + sugar_val['sales_stage'] + data = [sugar_val['sales_stage'], 'Opportunity'] + return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])]) - for val in sugar_data: - val['id_new'] = generate_xml_id(val['id'], TABLE_CONTACT) - val['partner_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_ACCOUNT, val.get('account_id')) - val['type'] = 'contact' - val['email'] = val.get('email1') + ','+ val.get('email2') - if val.get('primary_address_country'): - country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context) - state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context) - val['country_id/id'] = country_id - val['state_id/id'] = state - - import_module(sugar_obj, cr, uid, 'res.partner.address', map_partner_address, sugar_data, TABLE_CONTACT, context) - -#Validated -def import_users(sugar_obj, cr, uid, context=None): - map_user = { - 'name': ['first_name', 'last_name'], - 'login': 'user_name', - 'context_lang' : 'context_lang', - 'password' : 'password', - '.id' : '.id', - 'context_department_id/id': 'context_department_id/id', - } - - def get_users_department(sugar_obj, cr, uid, val, context=None): - fields = ['name'] - data = [val] - if not val: - return False - return import_object(sugar_obj, cr, uid, fields, data, 'hr.department', 'hr_department_user', val, context=context) - - if not context: - context = {} - - user_obj = sugar_obj.pool.get('res.users') - - datas = get_sugar_data(TABLE_USER, context) - for val in datas: - user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))]) - if user_ids: - val['.id'] = str(user_ids[0]) - else: - val['password'] = 'sugarcrm' #default password for all user #TODO needed in documentation + def import_opportunity_contact(self, val): + sugar_opportunities_contact = set(sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None)) - val['context_department_id/id'] = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context) - val['context_lang'] = context.get('lang','en_US') - import_module(sugar_obj, cr, uid, 'res.users', map_user, datas, TABLE_USER, context) + partner_contact_id = False + partner_contact_email = False + partner_address_obj = self.obj.pool.get('res.partner.address') + partner_xml_id = self.name_exist(self.TABLE_ACCOUNT, val['account_name'], 'res.partner') + + for contact in sugar_opportunities_contact: + address_id = self.get_mapped_id(self.TABLE_CONTACT, contact) + if address_id: + address = partner_address_obj.browse(self.cr, self.uid, address_id) + partner_name = address.partner_id and address.partner_id.name or False + if not partner_name: #link with partner id + fields = ['partner_id/id'] + data = [partner_xml_id] + self.import_object(fields, data, 'res.partner.address', self.TABLE_CONTACT, contact, self.DO_NOT_FIND_DOMAIN) + if not partner_name or partner_name == val.get('account_name'): + partner_contact_id = self.xml_id_exist(self.TABLE_CONTACT, contact) + partner_contact_email = address.email + return partner_contact_id, partner_contact_email - -#Validated -def import_partners(sugar_obj, cr, uid, context=None): - map_partner = { - 'name': 'name', - 'website': 'website', - 'user_id/id': 'user_id/id', - 'ref': 'sic_code', - 'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'], - 'customer': 'customer', - 'supplier': 'supplier', - 'address/id':'address/id', - } + def import_opp(self, val): + partner_contact_name, partner_contact_email = self.import_opportunity_contact(val) + val['partner_address_id/id'] = partner_contact_name + val['email_from'] = partner_contact_email + return val - def get_address_type(sugar_obj, cr, uid, val, type, context=None): + def get_opp_mapping(self): + return { + 'model' : 'crm.lead', + 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT], + 'hook' : self.import_opp, + 'map' : { + 'name': 'name', + 'probability': 'probability', + 'partner_id/id': refbyname(self.TABLE_ACCOUNT, 'account_name', 'res.partner'), + 'title_action': 'next_step', + 'partner_address_id/id': 'partner_address_id/id', + 'planned_revenue': 'amount', + 'date_deadline': 'date_closed', + 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'), + 'stage_id/id' : self.get_opportunity_status, + 'type' : const('opportunity'), + 'categ_id/id': call(self.get_category, 'crm.lead', value('opportunity_type')), + 'email_from': 'email_from', + 'state' : const('open'), #TODO + } + } + + """ + import lead + """ + def get_lead_status(self, sugar_val): + fields = ['name', 'type'] + name = 'lead_' + sugar_val.get('status', '') + data = [sugar_val.get('status', ''), 'lead'] + return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val.get('status', ''))]) + + lead_state = { + 'New' : 'draft', + 'Assigned':'open', + 'In Progress': 'open', + 'Recycled': 'cancel', + 'Dead': 'done', + 'Converted': 'done', + } + + + def import_lead(self, val): + if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead + return False + + if val.get('primary_address_country'): + country_id = self.get_all_countries(val.get('primary_address_country')) + val['country_id/id'] = country_id + val['state_id/id'] = self.get_all_states(val.get('primary_address_state'), country_id) + return val + + def get_lead_mapping(self): + return { + 'model' : 'crm.lead', + 'hook' : self.import_lead, + 'map' : { + 'name': concat('first_name', 'last_name'), + 'contact_name': concat('first_name', 'last_name'), + 'description': ppconcat('description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'), + 'partner_name': 'account_name', + 'email_from': 'email1', + 'phone': 'phone_work', + 'mobile': 'phone_mobile', + 'title/id': call(self.get_job_title, value('salutation')), + 'function':'title', + 'street': 'primary_address_street', + 'street2': 'alt_address_street', + 'zip': 'primary_address_postalcode', + 'city':'primary_address_city', + 'user_id/id' : ref(self.TABLE_USER, 'assigned_user_id'), + 'stage_id/id' : self.get_lead_status, + 'type' : const('lead'), + 'state': map_val('status', self.lead_state) , + 'fax': 'phone_fax', + 'referred': 'refered_by', + 'optout': 'do_not_call', + 'type_id/id': call(self.get_campaign_id, value('lead_source')), + 'country_id/id': 'country_id/id', + 'state_id/id': 'state_id/id' + } + } + + """ + import contact + """ + def get_email(self, val): + return val.get('email1') + ','+ val.get('email2') + + def import_contact(self, val): + if val.get('primary_address_country'): + country_id = self.get_all_countries(val.get('primary_address_country')) + state = self.get_all_states(val.get('primary_address_state'), country_id) + val['country_id/id'] = country_id + val['state_id/id'] = state + return val + + def get_contact_mapping(self): + return { + 'model' : 'res.partner.address', + 'dependencies' : [self.TABLE_ACCOUNT], + 'hook' : self.import_contact, + 'map' : { + 'name': concat('first_name', 'last_name'), + 'partner_id/id': ref(self.TABLE_ACCOUNT,'account_id'), + 'phone': 'phone_work', + 'mobile': 'phone_mobile', + 'fax': 'phone_fax', + 'function': 'title', + 'street': 'primary_address_street', + 'zip': 'primary_address_postalcode', + 'city': 'primary_address_city', + 'country_id/id': 'country_id/id', + 'state_id/id': 'state_id/id', + 'email': self.get_email, + 'type': const('contact') + } + } + + """ + import Account + """ + def get_address_type(self, val, type): if type == 'invoice': type_address = 'billing' else: @@ -201,7 +306,6 @@ def import_partners(sugar_obj, cr, uid, context=None): map_partner_address = { 'name': 'name', - 'partner_id/id': 'account_id', 'phone': 'phone_office', 'mobile': 'phone_mobile', 'fax': 'phone_fax', @@ -215,443 +319,59 @@ def import_partners(sugar_obj, cr, uid, context=None): if val.get(type_address +'_address_country'): - country_id = get_all_countries(sugar_obj, cr, uid, val.get(type_address +'_address_country'), context) - state = get_all_states(sugar_obj, cr, uid, val.get(type_address +'_address_state'), country_id, context) + country_id = self.get_all_countries(val.get(type_address +'_address_country')) + state = self.get_all_states(val.get(type_address +'_address_state'), country_id) val['country_id/id'] = country_id val['state_id/id'] = state val['type'] = type val['id_new'] = val['id'] + '_address_' + type - return import_object_mapping(sugar_obj, cr, uid, map_partner_address, val, 'res.partner.address', TABLE_CONTACT, val['id_new'], DO_NOT_FIND_DOMAIN, context=context) + return self.import_object_mapping(map_partner_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id_new'], self.DO_NOT_FIND_DOMAIN) - def get_address(sugar_obj, cr, uid, val, context=None): + def get_partner_address(self, val): address_id=[] type_dict = {'billing_address_street' : 'invoice', 'shipping_address_street' : 'delivery'} for key, type_value in type_dict.items(): if val.get(key): - id = get_address_type(sugar_obj, cr, uid, val, type_value, context) + id = self.get_address_type(val, type_value) address_id.append(id) - return address_id + return ','.join(address_id) - sugar_data = get_sugar_data(TABLE_ACCOUNT, context) - for val in sugar_data: - add_id = get_address(sugar_obj, cr, uid, val, context) - val['customer'] = '1' - val['supplier'] = '0' - val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['assigned_user_id']) - val['parent_id_new'] = val['parent_id'] and generate_xml_id(val['parent_id'], TABLE_ACCOUNT) or '' - val['address/id'] = ','.join(add_id) - - import_module(sugar_obj, cr, uid, 'res.partner', map_partner, sugar_data, TABLE_ACCOUNT, context) - -def get_category(sugar_obj, cr, uid, model, name, context=None): - fields = ['name', 'object_id'] - data = [name, model] - return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.categ', 'crm_categ', name, [('object_id.model','=',model), ('name', 'ilike', name)], context) - - - -def get_project_state(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - state_dict = {'status': #field in the sugarcrm database - { #Mapping of sugarcrm staus : openerp Projects state - 'Draft' : 'draft', - 'In Review': 'open', - 'Published': 'close', - },} - state = state_dict['status'].get(val, '') - return state - -def get_project_task_state(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - state_dict = {'status': #field in the sugarcrm database - { #Mapping of sugarcrm status : openerp Porject Tasks state - 'Not Started': 'draft', - 'In Progress': 'open', - 'Completed': 'done', - 'Pending Input': 'pending', - 'Deferred': 'cancelled', - },} - state = state_dict['status'].get(val, '') - return state - -def get_project_task_priority(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - priority_dict = {'priority': #field in the sugarcrm database - { #Mapping of sugarcrm status : openerp Porject Tasks state - 'High': '0', - 'Medium': '2', - 'Low': '3' - },} - priority = priority_dict['priority'].get(val, '') - return priority - -def import_documents(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_document = {'id' : 'id', - 'name': 'filename', - 'description': 'description', - 'datas': 'datas', - 'datas_fname': 'datas_fname', - } - attach_obj = sugar_obj.pool.get('ir.attachment') - PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) - sugar_data = sugar.search(PortType,sessionid, 'DocumentRevisions') - for val in sugar_data: - filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id') - f = open(filepath, "r") - datas = f.read() - f.close() - val['datas'] = base64.encodestring(datas) - val['datas_fname'] = val.get('filename') - fields, datas = sugarcrm_fields_mapp(val, map_document, context) - attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) - -def import_tasks(sugar_obj, cr, uid, context=None): - map_task = { - 'name': 'name', - 'date': ['__datetime__', 'date_start'], - 'date_deadline' : ['__datetime__', 'date_due'], - 'user_id/id': 'user_id/id', - 'categ_id/id': 'categ_id/id', - 'partner_id/id': 'partner_id/id', - 'partner_address_id/id': 'partner_address_id/id', - 'state': 'state' - } - - sugar_data = get_sugar_data(TABLE_TASK, context) - for val in sugar_data: - print "import task" - - val['user_id/id'] = generate_xml_id(val['assigned_user_id'], TABLE_USER) - val['partner_id/id'] = get_related_id(sugar_obj, cr, uid, val, TABLE_ACCOUNT, context) - val['partner_address_id/id'] = get_related_id(sugar_obj, cr, uid, val, TABLE_CONTACT, context) or xml_id_exist(sugar_obj, cr, uid, TABLE_CONTACT, val['contact_id'], context) - val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks') - val['state'] = STATE_MAP[TABLE_TASK].get(val['status']) - val['date_start'] = val['date_start'] or datetime.now().strftime('%Y-%m-%d %H:%M:%S') - val['date_due'] = val['date_due'] or datetime.now().strftime('%Y-%m-%d %H:%M:%S') - pp.pprint(val) - import_module(sugar_obj, cr, uid, 'crm.meeting', map_task, sugar_data, TABLE_TASK, context=context) - -#TODO -def get_attendee_id(sugar_obj, cr, uid, module_name, module_id, context=None): - if not context: - context = {} - PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) - model_obj = sugar_obj.pool.get('ir.model.data') - att_obj = sugar_obj.pool.get('calendar.attendee') - meeting_obj = sugar_obj.pool.get('crm.meeting') - user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id) - #TODO there is more then just user in attendee list - #there is contact, partner, lead (but we don't import the link) and user - for user in user_dict: - continue - user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context) - user_resource_id = model_obj.browse(cr, uid, user_model_ids) - if user_resource_id: - user_id = user_resource_id[0].res_id - attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)]) - if attend_ids: - attendees = attend_ids[0] - else: - attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')}) - meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context) - meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids) - if meeting_xml_id: - meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]}) - -def import_meetings(sugar_obj, cr, uid, context=None): - map_meeting = { + def get_partner_mapping(self): + return { + 'model' : 'res.partner', + 'dependencies' : [self.TABLE_USER], + 'map' : { 'name': 'name', - 'date': ['__datetime__', 'date_start'], - 'duration': ['duration_hours', 'duration_minutes'], - 'location': 'location', - 'alarm_id/id': 'alarm_id/id', - 'user_id/id': 'user_id/id', - 'partner_id/id':'partner_id/id', - 'partner_address_id/id':'partner_address_id/id', - 'state': 'state' - } - - def get_alarm_id(sugar_obj, cr, uid, val, context=None): - alarm_dict = { - '60': '1 minute before', - '300': '5 minutes before', - '600': '10 minutes before', - '900': '15 minutes before', - '1800':'30 minutes before', - '3600': '1 hour before', + 'website': 'website', + 'user_id/id': ref(self.TABLE_USER,'assigned_user_id'), + 'ref': 'sic_code', + 'comment': ppconcat('description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'), + 'customer': const('1'), + 'supplier': const('0'), + 'address/id':'address/id', + 'parent_id/id_parent' : 'parent_id', + 'address/id' : self.get_partner_address, + } } - xml_id = generate_xml_id(val, 'alarm') - return mapped_id_if_exist(sugar_obj, cr, uid, 'res.alarm', [('name', 'like', alarm_dict.get(val))], xml_id, context=context) - - sugar_data = get_sugar_data(TABLE_MEETING, context) - for val in sugar_data: - val['user_id/id'] = generate_xml_id(val['assigned_user_id'], TABLE_USER) - val['partner_id/id'] = get_related_id(sugar_obj, cr, uid, val, TABLE_ACCOUNT, context) - val['partner_address_id/id'] = get_related_id(sugar_obj, cr, uid, val, TABLE_CONTACT, context) - val['state'] = STATE_MAP[TABLE_MEETING].get(val['status']) - val['alarm_id/id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context) - get_attendee_id(sugar_obj, cr, uid, 'Meetings', val.get('id'), context) #TODO - import_module(sugar_obj, cr, uid, 'crm.meeting', map_meeting, sugar_data, TABLE_MEETING, context=context) - - -def get_related_id(obj, cr, uid, val, type, context=None): - if val['parent_type'] in type and val['parent_id']: - return xml_id_exist(obj, cr, uid, val['parent_type'], val['parent_id'], context) - return '' - - -def import_calls(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_calls = { - 'name': 'name', - 'date': ['__datetime__', 'date_start'], - 'duration': ['duration_hours', 'duration_minutes'], - 'user_id/id': 'user_id/id', - 'partner_id/id': 'partner_id/id', - 'partner_address_id/id': 'partner_address_id/id', - 'categ_id/id': 'categ_id/id', - 'state': 'state', - 'opportunity_id/id': 'opportunity_id/id', - 'description': 'description', - } - sugar_data = get_sugar_data('Calls', context) - for val in sugar_data: - val['user_id/id'] = generate_xml_id(val['assigned_user_id'], TABLE_USER) - val['opportunity_id/id'] = get_related_id(sugar_obj, cr, uid, val, [TABLE_OPPORTUNITY, TABLE_LEAD], context) - val['partner_id/id'] = get_related_id(sugar_obj, cr, uid, val, [TABLE_ACCOUNT], context) - val['partner_address_id/id'] = get_related_id(sugar_obj, cr, uid, val, [TABLE_CONTACT], context) - val['state'] = STATE_MAP[TABLE_CALL].get(val['status']) - val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction')) - import_module(sugar_obj, cr, uid, 'crm.phonecall', map_calls, sugar_data, TABLE_CALL, context=context) - - -def get_bug_priority(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - priority_dict = {'priority': #field in the sugarcrm database - { #Mapping of sugarcrm priority : openerp bugs priority - 'Urgent': '1', - 'High': '2', - 'Medium': '3', - 'Low': '4' - },} - priority = priority_dict['priority'].get(val, '') - return priority - -def get_claim_priority(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - priority_dict = {'priority': #field in the sugarcrm database - { #Mapping of sugarcrm priority : openerp claims priority - 'High': '2', - 'Medium': '3', - 'Low': '4' - },} - priority = priority_dict['priority'].get(val, '') - return priority - -def get_bug_state(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - state_dict = {'status': #field in the sugarcrm database - { #Mapping of sugarcrm status : openerp Bugs state - 'New' : 'draft', - 'Assigned':'open', - 'Closed': 'done', - 'Pending': 'pending', - 'Rejected': 'cancel', - },} - state = state_dict['status'].get(val, '') - return state - -def get_claim_state(sugar_obj, cr, uid, val,context=None): - if not context: - context = {} - state_dict = {'status': #field in the sugarcrm database - { #Mapping of sugarcrm status : openerp claim state - 'New' : 'draft', - 'Assigned':'open', - 'Closed': 'done', - 'Pending Input': 'pending', - 'Rejected': 'cancel', - 'Duplicate': 'draft', - },} - state = state_dict['status'].get(val, '') - return state - - -def get_acc_contact_claim(sugar_obj, cr, uid, val, context=None): - if not context: - context = {} - partner_id = False - partner_address_id = False - partner_phone = False - partner_email = False - model_obj = sugar_obj.pool.get('ir.model.data') - address_obj = sugar_obj.pool.get('res.partner.address') - model_ids = model_obj.search(cr, uid, [('name', '=', val.get('account_id')), ('model', '=', 'res.partner')]) - if model_ids: - model = model_obj.browse(cr, uid, model_ids)[0] - partner_id = model.res_id - address_ids = address_obj.search(cr, uid, [('partner_id', '=', partner_id)]) - if address_ids: - address_id = address_obj.browse(cr, uid, address_ids[0]) - partner_address_id = address_id.id - partner_phone = address_id.phone - partner_email = address_id.email - return partner_id, partner_address_id, partner_phone,partner_email - -def import_claims(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_claim = {'id' : 'id', - 'name': 'name', - 'date': ['__datetime__', 'date_entered'], - 'user_id/id': 'assigned_user_id', - 'priority':'priority', - 'partner_id/.id': 'partner_id/.id', - 'partner_address_id/.id': 'partner_address_id/.id', - 'partner_phone': 'partner_phone', - 'partner_mobile': 'partner_email', - 'description': 'description', - 'state': 'state', - } - claim_obj = sugar_obj.pool.get('crm.claim') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) - sugar_data = sugar.search(PortType, sessionid, 'Cases') - for val in sugar_data: - partner_id, partner_address_id, partner_phone,partner_email = get_acc_contact_claim(sugar_obj, cr, uid, val, context) - val['partner_id/.id'] = partner_id - val['partner_address_id/.id'] = partner_address_id - val['partner_phone'] = partner_phone - val['email_from'] = partner_email - val['priority'] = get_claim_priority(sugar_obj, cr, uid, val.get('priority'),context) - val['state'] = get_claim_state(sugar_obj, cr, uid, val.get('status'),context) - fields, datas = sugarcrm_fields_mapp(val, map_claim, context) - claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) - -def import_bug(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_resource = {'id' : 'id', - 'name': 'name', - 'project_id/.id':'project_id/.id', - 'categ_id/id': 'categ_id/id', - 'priority':'priority', - 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'], - 'state': 'state', - } - issue_obj = sugar_obj.pool.get('project.issue') - project_obj = sugar_obj.pool.get('project.project') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) - sugar_data = sugar.search(PortType, sessionid, 'Bugs') - for val in sugar_data: - project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')]) - if project_ids: - project_id = project_ids[0] - else: - project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'}) - val['project_id/.id'] = project_id - val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type')) - val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context) - val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context) - fields, datas = sugarcrm_fields_mapp(val, map_resource, context) - issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module=MODULE_NAME, noupdate=True, context=context) - -def get_job_id(sugar_obj, cr, uid, val, context=None): - if not context: - context={} - fields = ['name'] - data = [val] - return import_object(sugar_obj, cr, uid, fields, data, 'hr.job', 'hr_job', val, context=context) - -def get_campaign_id(sugar_obj, cr, uid, val, context=None): - if not context: - context={} - fields = ['name'] - data = [val] - return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.resource.type', 'crm_campaign', val, context=context) - -def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None): - if not context: - context = {} - attachment_obj = sugar_obj.pool.get('ir.attachment') - model_obj = sugar_obj.pool.get('ir.model.data') - mailgate_obj = sugar_obj.pool.get('mailgate.message') - attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))]) - if not attach_ids and Filename: - if parent_type == 'Accounts': - 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')}) - else: - 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)}) - message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context) - message_xml_id = model_obj.browse(cr, uid, message_model_ids) - if message_xml_id: - if parent_type == 'Accounts': - mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')}) - else: - mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]}) - -def import_history(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_attachment = {'id' : 'id', - 'name':'name', - 'date': ['__datetime__', 'date_entered'], - 'user_id/id': 'assigned_user_id', - 'description': ['__prettyprint__','description', 'description_html'], - 'res_id': 'res_id', - 'model': 'model', - 'partner_id/.id' : 'partner_id/.id', - } - mailgate_obj = sugar_obj.pool.get('mailgate.message') - model_obj = sugar_obj.pool.get('ir.model.data') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) - sugar_data = sugar.search(PortType, sessionid, 'Notes') - for val in sugar_data: - File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id')) - model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])]) - if model_ids: - model = model_obj.browse(cr, uid, model_ids)[0] - if model.model == 'res.partner': - val['partner_id/.id'] = model.res_id - else: - val['res_id'] = model.res_id - val['model'] = model.model - fields, datas = sugarcrm_fields_mapp(val, map_attachment, context) - mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) - get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context) -#Validated -def import_employees(sugar_obj, cr, uid, context=None): - - map_employee = { - 'resource_id/id': 'resource_id/id', - 'name': ['first_name', 'last_name'], - 'work_phone': 'phone_work', - 'mobile_phone': 'phone_mobile', - 'user_id/id': 'user_id/id', - 'address_home_id/id': 'address_home_id/id', - 'notes': 'description', - 'job_id/id': 'job_id/id' - } - - def get_ressource(sugar_obj, cr, uid, val, context=None): + """ + import Employee + """ + def get_ressource(self, val): map_resource = { - 'name': ['first_name', 'last_name'], + 'name': concat('first_name', 'last_name'), } - return import_object_mapping(sugar_obj, cr, uid, map_resource, val, 'resource.resource', TABLE_RESSOURCE, val['id'], DO_NOT_FIND_DOMAIN, context) - - def get_user_address(sugar_obj, cr, uid, val, context=None): + return self.import_object_mapping(map_resource, val, 'resource.resource', self.TABLE_RESSOURCE, val['id'], self.DO_NOT_FIND_DOMAIN) + + def get_job_id(self, val): + fields = ['name'] + data = [val.get('title')] + return self.import_object(fields, data, 'hr.job', 'hr_job', val.get('title')) + + def get_user_address(self, val): map_user_address = { - 'name': ['first_name', 'last_name'], + 'name': concat('first_name', 'last_name'), 'city': 'address_city', 'country_id/id': 'country_id/id', 'state_id/id': 'state_id/id', @@ -661,326 +381,79 @@ def import_employees(sugar_obj, cr, uid, context=None): } if val.get('address_country'): - country_id = get_all_countries(sugar_obj, cr, uid, val.get('address_country'), context) - state_id = get_all_states(sugar_obj, cr, uid, val.get('address_state'), country_id, context) + country_id = self.get_all_countries(val.get('address_country')) + state_id = self.get_all_states(val.get('address_state'), country_id) val['country_id/id'] = country_id val['state_id/id'] = state_id - return import_object_mapping(sugar_obj, cr, uid, map_user_address, val, 'res.partner.address', TABLE_CONTACT, val['id'], DO_NOT_FIND_DOMAIN, context=context) + return self.import_object_mapping(map_user_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id'], self.DO_NOT_FIND_DOMAIN) - sugar_data = get_sugar_data(TABLE_EMPLOYEE, context) - for val in sugar_data: - val['address_home_id/id'] = get_user_address(sugar_obj, cr, uid, val, context) - val['job_id/id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context) - val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['id']) - val['resource_id/id'] = get_ressource(sugar_obj, cr, uid, val, context) - #for cycle dependencies - val['parent_id_new'] = val['reports_to_id'] and generate_xml_id(val['reports_to_id'], TABLE_EMPLOYEE) or '' - - import_module(sugar_obj, cr, uid, 'hr.employee', map_employee, sugar_data, TABLE_EMPLOYEE, context=context) - -def import_emails(sugar_obj, cr, uid, context=None): - if not context: - context= {} - map_emails = { - 'id': 'id', - 'name':'name', - 'date':['__datetime__', 'date_sent'], - 'email_from': 'from_addr_name', - 'email_to': 'reply_to_addr', - 'email_cc': 'cc_addrs_names', - 'email_bcc': 'bcc_addrs_names', - 'message_id': 'message_id', - 'user_id/id': 'assigned_user_id', - 'description': ['__prettyprint__', 'description', 'description_html'], - 'res_id': 'res_id', - 'model': 'model', - 'partner_id/.id': 'partner_id/.id' - } - mailgate_obj = sugar_obj.pool.get('mailgate.message') - model_obj = sugar_obj.pool.get('ir.model.data') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) - sugar_data = sugar.search(PortType, sessionid, 'Emails') - for val in sugar_data: - model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])]) - for model in model_obj.browse(cr, uid, model_ids): - if model.model == 'res.partner': - val['partner_id/.id'] = model.res_id - else: - val['res_id'] = model.res_id - val['model'] = model.model - fields, datas = sugarcrm_fields_mapp(val, map_emails, context) - mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) - return True - - - + def get_employee_mapping(self): + return { + 'model' : 'hr.employee', + 'dependencies' : [self.TABLE_USER], + 'map' : { + 'resource_id/id': self.get_ressource, + 'name': concat('first_name', 'last_name'), + 'work_phone': 'phone_work', + 'mobile_phone': 'phone_mobile', + 'user_id/id': ref(self.TABLE_USER, 'id'), + 'address_home_id/id': self.get_user_address, + 'notes': 'description', + 'job_id/id': self.get_job_id, + } + } -def import_projects(sugar_obj, cr, uid, context=None): - - map_project = {'id': 'id', - 'name': 'name', - 'date_start': ['__datetime__', 'estimated_start_date'], - 'date': ['__datetime__', 'estimated_end_date'], - 'user_id/id': 'assigned_user_id', - 'partner_id/.id': 'partner_id/.id', - 'contact_id/.id': 'contact_id/.id', - 'state': 'state' - } - - #TODO more simplier use xml id please !!!! - def get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context=None): - if not context: - context={} - partner_id = False - partner_invoice_id = False - model_obj = sugar_obj.pool.get('ir.model.data') - partner_obj = sugar_obj.pool.get('res.partner') - partner_address_obj = sugar_obj.pool.get('res.partner.address') - sugar_project_account = sugar.relation_search(PortType, sessionid, 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None) - for account_id in sugar_project_account: - model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner', account_id, context) - if model_ids: - model_id = model_obj.browse(cr, uid, model_ids)[0].res_id - partner_id = partner_obj.browse(cr, uid, model_id).id - address_ids = partner_address_obj.search(cr, uid, [('partner_id', '=', partner_id),('type', '=', 'invoice')]) - partner_invoice_id = address_ids[0] - return partner_id, partner_invoice_id - - if not context: - context = {} - - - project_obj = sugar_obj.pool.get('project.project') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) - sugar_data = sugar.search(PortType, sessionid, 'Project') - for val in sugar_data: - partner_id, partner_invoice_id = get_project_account(sugar_obj,cr,uid, PortType, sessionid, val, context) - val['partner_id/.id'] = partner_id - val['contact_id/.id'] = partner_invoice_id - val['state'] = get_project_state(sugar_obj, cr, uid, val.get('status'),context) - fields, datas = sugarcrm_fields_mapp(val, map_project, context) - project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module=MODULE_NAME, noupdate=True, context=context) - return True - - -def import_project_tasks(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_project_task = {'id': 'id', - 'name': 'name', - 'date_start': ['__datetime__', 'date_start'], - 'date_end': ['__datetime__', 'date_finish'], - 'progress': 'progress', - 'project_id/name': 'project_name', - 'planned_hours': 'planned_hours', - 'total_hours': 'total_hours', - 'priority': 'priority', - 'description': 'description', - 'user_id/id': 'assigned_user_id', - 'state': 'state' - } - task_obj = sugar_obj.pool.get('project.task') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) - sugar_data = sugar.search(PortType, sessionid, 'ProjectTask') - for val in sugar_data: - val['state'] = get_project_task_state(sugar_obj, cr, uid, val.get('status'),context) - val['priority'] = get_project_task_priority(sugar_obj, cr, uid, val.get('priority'),context) - fields, datas = sugarcrm_fields_mapp(val, map_project_task, context) - task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module=MODULE_NAME, noupdate=True, context=context) - return True - -def import_leads(sugar_obj, cr, uid, context=None): - map_lead = { - 'name': ['first_name', 'last_name'], - 'contact_name': ['first_name', 'last_name'], - 'description': ['__prettyprint__', 'description', 'refered_by', 'lead_source', 'lead_source_description', 'website', 'email2', 'status_description', 'lead_source_description', 'do_not_call'], - 'partner_name': 'account_name', - 'email_from': 'email1', - 'phone': 'phone_work', - 'mobile': 'phone_mobile', - 'title/id': 'title/id', - 'function':'title', - 'street': 'primary_address_street', - 'street2': 'alt_address_street', - 'zip': 'primary_address_postalcode', - 'city':'primary_address_city', - 'user_id/id' : 'user_id/id', - 'stage_id/id' : 'stage_id/id', - 'type' : 'type', - 'state': 'state', - 'fax': 'phone_fax', - 'referred': 'refered_by', - 'optout': 'do_not_call', - 'type_id/id': 'type_id/id', - 'country_id/id': 'country_id/id', - 'state_id/id': 'state_id/id' - } - - def get_lead_status(sugar_obj, cr, uid, sugar_val,context=None): - fields = ['name', 'type'] - name = 'lead_' + sugar_val['status'] - data = [sugar_val['status'], 'lead'] - return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', TABLE_STAGE, name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val['status'])], context) - - sugar_data = get_sugar_data(TABLE_LEAD, context) - end_data = [] - for val in sugar_data: - if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead - continue - - end_data.append(val) - - if val.get('salutation'): - val['title/id'] = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'Contact', context) - val['user_id/id'] = generate_xml_id(val['assigned_user_id'], TABLE_USER) - val['type'] = 'lead' - val['type_id/id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context) - val['stage_id/id'] = get_lead_status(sugar_obj, cr, uid, val, context) - val['state'] = STATE_MAP[TABLE_LEAD].get(val['status']) - if val.get('primary_address_country'): - country_id = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context) - val['country_id/id'] = country_id - val['state_id/id'] = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), country_id, context) - - import_module(sugar_obj, cr, uid, 'crm.lead', map_lead, end_data, TABLE_LEAD, context) - - -def import_opportunities(sugar_obj, cr, uid, context=None): - map_opportunity = { - 'name': 'name', - 'probability': 'probability', - 'partner_id/id': 'partner_id/id', - 'title_action': 'next_step', - 'partner_address_id/id': 'partner_address_id/id', - 'planned_revenue': 'amount', - 'date_deadline': ['__datetime__', 'date_closed'], - 'user_id/id' : 'user_id/id', - 'stage_id/id' : 'stage_id/id', - 'type' : 'type', - 'categ_id/id': 'categ_id/id', - 'email_from': 'email_from' - } - - def import_opportunity_contact(sugar_obj,cr,uid, val, partner_xml_id, context=None): - if not context: - context = {} + """ + import user + """ + def import_user(self, val): + user_obj = self.obj.pool.get('res.users') + user_ids = user_obj.search(self.cr, self.uid, [('login', '=', val.get('user_name'))]) + if user_ids: + val['.id'] = str(user_ids[0]) + else: + val['password'] = 'sugarcrm' #default password for all user #TODO needed in documentation - PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) - sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None)) - - - partner_contact_id = False - partner_contact_email = False - partner_address_obj = sugar_obj.pool.get('res.partner.address') - - if sugar_opportunities_contact: - for contact in sugar_opportunities_contact: - address_id = get_mapped_id(sugar_obj, cr, uid, TABLE_CONTACT, contact, context) - if address_id: - address = partner_address_obj.browse(cr, uid, address_id) - partner_name = address.partner_id and address.partner_id.name or False - if not partner_name: #link with partner id - fields = ['partner_id/id'] - data = [partner_xml_id] - import_object(sugar_obj, cr, uid, fields, data, 'res.partner.address', TABLE_CONTACT, contact, DO_NOT_FIND_DOMAIN, context) - if not partner_name or partner_name == val.get('account_name'): - partner_contact_id = generate_xml_id(contact, TABLE_CONTACT) - partner_contact_email = address.email - return partner_contact_id, partner_contact_email + val['context_lang'] = self.context.get('lang','en_US') + return val - def get_opportunity_status(sugar_obj, cr, uid, sugar_val,context=None): - fields = ['name', 'type'] - name = 'Opportunity_' + sugar_val['sales_stage'] - data = [sugar_val['sales_stage'], 'Opportunity'] - return import_object(sugar_obj, cr, uid, fields, data, 'crm.case.stage', TABLE_STAGE, name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])], context) - - def get_oppportunity_partner(sugar_obj, cr, uid, name, context=None): + def get_users_department(self, val): + dep = val.get('department') fields = ['name'] - data = [name] - return import_object(sugar_obj, cr, uid, fields, data, 'res.partner', TABLE_ACCOUNT, name, [('name', '=', name)], context) - - sugar_data = get_sugar_data(TABLE_OPPORTUNITY, context) - for val in sugar_data: - val['partner_id/id'] = get_oppportunity_partner(sugar_obj, cr, uid, val['account_name'], context) + data = [dep] + if not dep: + return False + return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep) + + def get_user_mapping(self): + return { + 'model' : 'res.users', + 'hook' : self.import_user, + 'map' : { + 'name': concat('first_name', 'last_name'), + 'login': 'user_name', + 'context_lang' : 'context_lang', + 'password' : 'password', + '.id' : '.id', + 'context_department_id/id': self.get_users_department, + } + } + + def get_mapping(self): + return { + self.TABLE_USER : self.get_user_mapping(), + self.TABLE_EMPLOYEE : self.get_employee_mapping(), + self.TABLE_ACCOUNT : self.get_partner_mapping(), + self.TABLE_CONTACT : self.get_contact_mapping(), + self.TABLE_LEAD : self.get_lead_mapping(), + self.TABLE_OPPORTUNITY : self.get_opp_mapping(), + self.TABLE_MEETING : self.get_meeting_mapping(), + } + - partner_contact_name, partner_contact_email = import_opportunity_contact(sugar_obj,cr,uid, val, val['partner_id/id'], context) - val['partner_address_id/id'] = partner_contact_name - val['email_from'] = partner_contact_email - - val['user_id/id'] = generate_xml_id(val['assigned_user_id'], TABLE_USER) - val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type')) - val['type'] = 'opportunity' - val['stage_id/id'] = get_opportunity_status(sugar_obj, cr, uid, val, context) - import_module(sugar_obj, cr, uid, 'crm.lead', map_opportunity, sugar_data, TABLE_OPPORTUNITY, context) -MAP_FIELDS = {'Opportunities': #Object Mapping name - {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], #Object to import before this table - 'process' : import_opportunities, - }, - 'Leads': - {'dependencies' : ['Users'], #Object to import before this table - 'process' : import_leads, - }, - 'Contacts': - {'dependencies' : ['Users','Accounts'], #Object to import before this table - 'process' : import_partner_address, - }, - 'Accounts': - {'dependencies' : ['Users'], #Object to import before this table - 'process' : import_partners, - }, - 'Users': - {'dependencies' : [], - 'process' : import_users, - }, - 'Documents': - {'dependencies' : ['Users'], - 'process' : import_documents, - }, - 'Meetings': - {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities', 'Leads'], #PROJECT TASK - 'process' : import_meetings, - }, - 'Tasks': - {'dependencies' : ['Accounts', 'Contacts', 'Users'], - 'process' : import_tasks, - }, - 'Calls': - {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities', 'Leads'], - 'process' : import_calls, - }, - 'Projects': - {'dependencies' : ['Users', 'Accounts', 'Contacts'], - 'process' : import_projects, - }, - 'Project Tasks': - {'dependencies' : ['Users', 'Projects'], - 'process' : import_project_tasks, - }, - 'Bugs': - {'dependencies' : ['Users', 'Projects', 'Project Tasks'], - 'process' : import_bug, - }, - 'Claims': - {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], - 'process' : import_claims, - }, - 'Emails': - {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'], - 'process' : import_emails, - }, - - 'Notes': - {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'], - 'process' : import_history, - }, - 'Employees': - {'dependencies' : ['Users'], - 'process' : import_employees, - }, - } class import_sugarcrm(osv.osv): """Import SugarCRM DATA""" @@ -1030,6 +503,7 @@ class import_sugarcrm(osv.osv): key_list = [] for current in self.browse(cr, uid, ids, context): if current.opportunity: + key_list.append('Leads') key_list.append('Opportunities') if current.user: key_list.append('Users') @@ -1062,17 +536,13 @@ class import_sugarcrm(osv.osv): return key_list def import_all(self, cr, uid, ids, context=None): - """Import all sugarcrm data into openerp module""" - if not context: - context = {} + +# """Import all sugarcrm data into openerp module""" keys = self.get_key(cr, uid, ids, context) - imported = set() #to invoid importing 2 times the sames modules - for key in keys: - if not key in imported: - self.resolve_dependencies(cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], imported, context=context) - MAP_FIELDS[key]['process'](self, cr, uid, context) - imported.add(key) + imp = sugar_import(self, cr, uid, "sugarcrm", "import_sugarcrm", context) + imp.import_all(keys) + obj_model = self.pool.get('ir.model.data') model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.message.form')]) resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id']) @@ -1084,12 +554,5 @@ class import_sugarcrm(osv.osv): 'type': 'ir.actions.act_window', 'target': 'new', } - - def resolve_dependencies(self, cr, uid, dict, dep, imported, context=None): - for dependency in dep: - if not dependency in imported: - self.resolve_dependencies(cr, uid, dict, dict[dependency]['dependencies'], imported, context=context) - dict[dependency]['process'](self, cr, uid, context) - imported.add(dependency) - + import_sugarcrm() diff --git a/addons/import_sugarcrm/old_import.py b/addons/import_sugarcrm/old_import.py new file mode 100644 index 00000000000..569cfd8b848 --- /dev/null +++ b/addons/import_sugarcrm/old_import.py @@ -0,0 +1,514 @@ +''' +Created on 9 mai 2011 + +@author: openerp +''' + +#TODO old source code to be deleted once it's migrated + + +#print 'xml ids exist', sugar_obj.pool.get('ir.model.data').search(cr, uid, [('name', 'in', address_id )]) + +OPENERP_FIEDS_MAPS = {'Leads': 'crm.lead', + 'Opportunities': 'crm.lead', + 'Contacts': 'res.partner.address', + 'Accounts': 'res.partner', + 'Resources': 'resource.resource', + 'Users': 'res.users', + 'Meetings': 'crm.meeting', + 'Calls': 'crm.phonecall', + 'Claims': 'crm.claim', + 'Employee': 'hr.employee', + 'Project': 'project.project', + 'ProjectTask': 'project.task', + 'Bugs': 'project.issue', + 'Documents': 'ir.attachment', + +} + + + +STATE_MAP = { + + TABLE_CALL : { + 'Planned' : 'open', + 'Held':'done', + 'Not Held': 'pending', + }, + TABLE_TASK : { + 'Completed' : 'done', + 'Not Started':'draft', + 'In Progress': 'open', + 'Pending Input': 'draft', + 'deferred': 'cancel' + }, + TABLE_PROJECT: { + 'Draft' : 'draft', + 'In Review': 'open', + 'Published': 'close', + }, + TABLE_PROJECT_TASK: { + 'Not Started': 'draft', + 'In Progress': 'open', + 'Completed': 'done', + 'Pending Input': 'pending', + 'Deferred': 'cancelled', + }, + TABLE_CASE : { + 'New' : 'draft', + 'Assigned':'open', + 'Closed': 'done', + 'Pending Input': 'pending', + 'Rejected': 'cancel', + 'Duplicate': 'draft', + }, +} + + + + + + + +def get_related_objects(sugar_obj,cr,uid, id, TABLE_PARENT, TABLE_CHILD, context=None): + if not context: + context={} + PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) + sugar_links = sugar.relation_search(PortType, sessionid, TABLE_PARENT, module_id=id, related_module=TABLE_CHILD, query=None, deleted=None) + res = [] + for link in sugar_links: + xml_id = xml_id_exist(sugar_obj, cr, uid, TABLE_CHILD, link, context) + res.append(xml_id) + return res + +def get_related_object(sugar_obj,cr,uid, id, TABLE_PARENT, TABLE_CHILD, context=None): + res = get_related_objects(sugar_obj,cr,uid, id, TABLE_PARENT, TABLE_CHILD, context) + return res and res[0] or '' + +def get_contact_info_from_account(sugar_obj, cr, uid, account_id, context=None): + partner_id = get_mapped_id(sugar_obj, cr, uid, TABLE_ACCOUNT, account_id, context) + partner_address_id = False + partner_phone = False + partner_email = False + + if not partner_id: + return partner_address_id, partner_phone,partner_email + + partner = sugar_obj.pool.get('res.partner').browse(cr, uid, [partner_id], context=context)[0] + if partner.address and partner.address[0]: + address = partner.address[0] + partner_address_id = address.id + partner_phone = address.phone + partner_email = address.email + + return partner_address_id, partner_phone,partner_email + + + + + + + + + +def get_project_task_state(sugar_obj, cr, uid, val,context=None): + if not context: + context = {} + state_dict = {} + state = state_dict['status'].get(val, '') + return state + + + +def import_documents(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_document = {'id' : 'id', + 'name': 'filename', + 'description': 'description', + 'datas': 'datas', + 'datas_fname': 'datas_fname', + } + attach_obj = sugar_obj.pool.get('ir.attachment') + PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) + sugar_data = sugar.search(PortType,sessionid, 'DocumentRevisions') + for val in sugar_data: + filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id') + f = open(filepath, "r") + datas = f.read() + f.close() + val['datas'] = base64.encodestring(datas) + val['datas_fname'] = val.get('filename') + fields, datas = sugarcrm_fields_mapp(val, map_document, context) + attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) + +def import_tasks(sugar_obj, cr, uid, context=None): + map_task = { + 'name': 'name', + 'date': ['__datetime__', 'date_start'], + 'date_deadline' : ['__datetime__', 'date_due'], + 'user_id/id': 'user_id/id', + 'categ_id/id': 'categ_id/id', + 'partner_id/id': 'partner_id/id', + 'partner_address_id/id': 'partner_address_id/id', + 'state': 'state' + } + + + + sugar_data = get_sugar_data(TABLE_TASK, context) + for val in sugar_data: + val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['assigned_user_id'], context) + val['partner_id/id'] = get_related_id(sugar_obj, cr, uid, val, TABLE_ACCOUNT, context) + val['partner_address_id/id'] = get_related_id(sugar_obj, cr, uid, val, TABLE_CONTACT, context) or xml_id_exist(sugar_obj, cr, uid, TABLE_CONTACT, val['contact_id'], context) + val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks') + val['state'] = STATE_MAP[TABLE_TASK].get(val['status']) + val['date_start'] = val['date_start'] or datetime.now().strftime('%Y-%m-%d %H:%M:%S') + val['date_due'] = val['date_due'] or datetime.now().strftime('%Y-%m-%d %H:%M:%S') + import_module(sugar_obj, cr, uid, 'crm.meeting', map_task, sugar_data, TABLE_TASK, context=context) + +#TODO +def get_attendee_id(sugar_obj, cr, uid, module_name, module_id, context=None): + if not context: + context = {} + PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) + model_obj = sugar_obj.pool.get('ir.model.data') + att_obj = sugar_obj.pool.get('calendar.attendee') + meeting_obj = sugar_obj.pool.get('crm.meeting') + user_dict = sugar.user_get_attendee_list(PortType, sessionid, module_name, module_id) + #TODO there is more then just user in attendee list + #there is contact, partner, lead (but we don't import the link) and user + for user in user_dict: + continue + user_model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.users', user.get('id'), context) + user_resource_id = model_obj.browse(cr, uid, user_model_ids) + if user_resource_id: + user_id = user_resource_id[0].res_id + attend_ids = att_obj.search(cr, uid, [('user_id', '=', user_id)]) + if attend_ids: + attendees = attend_ids[0] + else: + attendees = att_obj.create(cr, uid, {'user_id': user_id, 'email': user.get('email1')}) + meeting_model_ids = find_mapped_id(sugar_obj, cr, uid, 'crm.meeting', module_id, context) + meeting_xml_id = model_obj.browse(cr, uid, meeting_model_ids) + if meeting_xml_id: + meeting_obj.write(cr, uid, [meeting_xml_id[0].res_id], {'attendee_ids': [(4, attendees)]}) + +def import_meetings(sugar_obj, cr, uid, context=None): + map_meeting = + + + sugar_data = get_sugar_data(TABLE_MEETING, context) + for val in sugar_data: + val['alarm_id/id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context) + get_attendee_id(sugar_obj, cr, uid, 'Meetings', val.get('id'), context) #TODO + import_module(sugar_obj, cr, uid, 'crm.meeting', map_meeting, sugar_data, TABLE_MEETING, context=context) + + + + + + +def import_calls(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_calls = { + 'name': 'name', + 'date': ['__datetime__', 'date_start'], + 'duration': ['duration_hours', 'duration_minutes'], + 'user_id/id': 'user_id/id', + 'partner_id/id': 'partner_id/id', + 'partner_address_id/id': 'partner_address_id/id', + 'categ_id/id': 'categ_id/id', + 'state': 'state', + 'opportunity_id/id': 'opportunity_id/id', + 'description': 'description', + } + sugar_data = get_sugar_data('Calls', context) + for val in sugar_data: + val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['assigned_user_id'], context) + val['opportunity_id/id'] = get_related_id(sugar_obj, cr, uid, val, [TABLE_OPPORTUNITY, TABLE_LEAD], context) + val['partner_id/id'] = get_related_id(sugar_obj, cr, uid, val, [TABLE_ACCOUNT], context) + val['partner_address_id/id'] = get_related_id(sugar_obj, cr, uid, val, [TABLE_CONTACT], context) + val['state'] = STATE_MAP[TABLE_CALL].get(val['status']) + val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction')) + import_module(sugar_obj, cr, uid, 'crm.phonecall', map_calls, sugar_data, TABLE_CALL, context=context) + + +def get_bug_priority(sugar_obj, cr, uid, val,context=None): + if not context: + context = {} + priority_dict = {'priority': #field in the sugarcrm database + { #Mapping of sugarcrm priority : openerp bugs priority + 'Urgent': '1', + 'High': '2', + 'Medium': '3', + 'Low': '4' + },} + priority = priority_dict['priority'].get(val, '') + return priority + + + +def get_bug_state(sugar_obj, cr, uid, val,context=None): + if not context: + context = {} + state_dict = {'status': #field in the sugarcrm database + { #Mapping of sugarcrm status : openerp Bugs state + 'New' : 'draft', + 'Assigned':'open', + 'Closed': 'done', + 'Pending': 'pending', + 'Rejected': 'cancel', + },} + state = state_dict['status'].get(val, '') + return state + + + + + +def import_claims(sugar_obj, cr, uid, context=None): + map_claim = { + 'name': 'name', + 'date': ['__datetime__', 'date_entered'], + 'user_id/id': 'user_id/id', + 'priority':'priority', + 'partner_id/id': 'partner_id/id', + 'partner_address_id/.id': 'partner_address_id/.id', + 'partner_phone': 'partner_phone', + 'email_from': 'email_from', + 'description': 'description', + 'state': 'state', + } + + def get_claim_priority(val): + priority_dict = { + 'High': '2', + 'Medium': '3', + 'Low': '4' + } + return priority_dict.get(val, '') + + sugar_data = get_sugar_data(TABLE_CASE, context) + print sugar_data + for val in sugar_data: + val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['assigned_user_id'], context) + val['partner_address_id/.id'], val['partner_phone'],val['email_from'] = get_contact_info_from_account(sugar_obj, cr, uid, val['account_id'], context) + val['partner_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_ACCOUNT, val['account_id'], context=context) + val['priority'] = get_claim_priority(val.get('priority')) + val['state'] = STATE_MAP[TABLE_CASE].get( val.get('status')) + print "cases" + pp.pprint(val) + import_module(sugar_obj, cr, uid, 'crm.claim', map_claim, sugar_data, TABLE_CASE, context=context) + +def import_bug(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_resource = {'id' : 'id', + 'name': 'name', + 'project_id/.id':'project_id/.id', + 'categ_id/id': 'categ_id/id', + 'priority':'priority', + 'description': ['__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'], + 'state': 'state', + } + issue_obj = sugar_obj.pool.get('project.issue') + project_obj = sugar_obj.pool.get('project.project') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Bugs') + for val in sugar_data: + project_ids = project_obj.search(cr, uid, [('name', 'like', 'sugarcrm_bugs')]) + if project_ids: + project_id = project_ids[0] + else: + project_id = project_obj.create(cr, uid, {'name':'sugarcrm_bugs'}) + val['project_id/.id'] = project_id + val['categ_id/id'] = get_category(sugar_obj, cr, uid, 'project.issue', val.get('type')) + val['priority'] = get_bug_priority(sugar_obj, cr, uid, val.get('priority'),context) + val['state'] = get_bug_state(sugar_obj, cr, uid, val.get('status'),context) + fields, datas = sugarcrm_fields_mapp(val, map_resource, context) + issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module=MODULE_NAME, noupdate=True, context=context) + + + + +def get_attachment(sugar_obj, cr, uid, val, model, File, Filename, parent_type, context=None): + if not context: + context = {} + attachment_obj = sugar_obj.pool.get('ir.attachment') + model_obj = sugar_obj.pool.get('ir.model.data') + mailgate_obj = sugar_obj.pool.get('mailgate.message') + attach_ids = attachment_obj.search(cr, uid, [('res_id','=', val.get('res_id'), ('res_model', '=', val.get('model')))]) + if not attach_ids and Filename: + if parent_type == 'Accounts': + 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')}) + else: + 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)}) + message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, val.get('id'), context) + message_xml_id = model_obj.browse(cr, uid, message_model_ids) + if message_xml_id: + if parent_type == 'Accounts': + mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)], 'partner_id': val.get('partner_id/.id')}) + else: + mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]}) + +def import_history(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_attachment = {'id' : 'id', + 'name':'name', + 'date': ['__datetime__', 'date_entered'], + 'user_id/id': 'assigned_user_id', + 'description': ['__prettyprint__','description', 'description_html'], + 'res_id': 'res_id', + 'model': 'model', + 'partner_id/.id' : 'partner_id/.id', + } + mailgate_obj = sugar_obj.pool.get('mailgate.message') + model_obj = sugar_obj.pool.get('ir.model.data') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Notes') + for val in sugar_data: + File, Filename = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id')) + model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])]) + if model_ids: + model = model_obj.browse(cr, uid, model_ids)[0] + if model.model == 'res.partner': + val['partner_id/.id'] = model.res_id + else: + val['res_id'] = model.res_id + val['model'] = model.model + fields, datas = sugarcrm_fields_mapp(val, map_attachment, context) + mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) + get_attachment(sugar_obj, cr, uid, val, 'mailgate.message', File, Filename, val.get('parent_type'), context) +#Validated + +def import_emails(sugar_obj, cr, uid, context=None): + if not context: + context= {} + map_emails = { + 'id': 'id', + 'name':'name', + 'date':['__datetime__', 'date_sent'], + 'email_from': 'from_addr_name', + 'email_to': 'reply_to_addr', + 'email_cc': 'cc_addrs_names', + 'email_bcc': 'bcc_addrs_names', + 'message_id': 'message_id', + 'user_id/id': 'assigned_user_id', + 'description': ['__prettyprint__', 'description', 'description_html'], + 'res_id': 'res_id', + 'model': 'model', + 'partner_id/.id': 'partner_id/.id' + } + mailgate_obj = sugar_obj.pool.get('mailgate.message') + model_obj = sugar_obj.pool.get('ir.model.data') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Emails') + for val in sugar_data: + model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id')),('model','=', OPENERP_FIEDS_MAPS[val.get('parent_type')])]) + for model in model_obj.browse(cr, uid, model_ids): + if model.model == 'res.partner': + val['partner_id/.id'] = model.res_id + else: + val['res_id'] = model.res_id + val['model'] = model.model + fields, datas = sugarcrm_fields_mapp(val, map_emails, context) + mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context) + return True + + + + + +def import_projects(sugar_obj, cr, uid, context=None): + + map_project = { + 'name': 'name', + 'date_start': ['__datetime__', 'estimated_start_date'], + 'date': ['__datetime__', 'estimated_end_date'], + 'user_id/id': 'user_id/id', + 'partner_id/id': 'partner_id/id', + 'contact_id/id' : 'contact_id/id', + 'state': 'state' + } + + sugar_data = get_sugar_data(TABLE_PROJECT, context) + for val in sugar_data: + val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['assigned_user_id'], context) + val['partner_id/id'] = get_related_object(sugar_obj,cr,uid, val['id'], TABLE_PROJECT, TABLE_ACCOUNT, context) + val['contact_id/id'] = get_related_object(sugar_obj,cr,uid, val['id'], TABLE_PROJECT, TABLE_CONTACT, context) + val['state'] = STATE_MAP[TABLE_PROJECT].get(val['status']) + import_module(sugar_obj, cr, uid, 'project.project', map_project, sugar_data, TABLE_PROJECT, context) + +def import_project_tasks(sugar_obj, cr, uid, context=None): + map_project_task = { + 'name': 'name', + 'date_start': ['__datetime__', 'date_start'], + 'date_end': ['__datetime__', 'date_finish'], + 'progress': 'progress', + 'project_id/id': 'project_id/id', + 'planned_hours': 'planned_hours', + 'total_hours': 'total_hours', + 'priority': 'priority', + 'description': 'description', + 'user_id/id': 'user_id/id', + 'state': 'state' + } + + def get_project_task_priority(val): + priority_dict = { + 'High': '0', + 'Medium': '2', + 'Low': '3' + } + return priority_dict.get(val, '') + + sugar_data = get_sugar_data(TABLE_PROJECT_TASK, context) + for val in sugar_data: + val['project_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_PROJECT, val['project_id'], context) + val['user_id/id'] = xml_id_exist(sugar_obj, cr, uid, TABLE_USER, val['assigned_user_id'], context) + val['state'] = STATE_MAP[TABLE_PROJECT_TASK].get( val.get('status')) + val['priority'] = get_project_task_priority(val.get('priority')) + import_module(sugar_obj, cr, uid, 'project.task', map_project_task, sugar_data, TABLE_PROJECT_TASK, context) + + +MAP_FIELDS = {'Documents': + {'dependencies' : ['Users'], + 'process' : import_documents, + }, + 'Tasks': + {'dependencies' : ['Accounts', 'Contacts', 'Users'], + 'process' : import_tasks, + }, + 'Calls': + {'dependencies' : ['Accounts', 'Contacts', 'Users', 'Opportunities', 'Leads'], + 'process' : import_calls, + }, + 'Projects': + {'dependencies' : ['Users', 'Accounts', 'Contacts'], + 'process' : import_projects, + }, + 'Project Tasks': + {'dependencies' : ['Users', 'Projects'], + 'process' : import_project_tasks, + }, + 'Bugs': + {'dependencies' : ['Users', 'Projects', 'Project Tasks'], + 'process' : import_bug, + }, + 'Claims': + {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'], + 'process' : import_claims, + }, + 'Emails': + {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'], + 'process' : import_emails, + }, + + 'Notes': + {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'], + 'process' : import_history, + }, + } diff --git a/addons/import_sugarcrm/sugarcrm_fields_mapping.py b/addons/import_sugarcrm/sugarcrm_fields_mapping.py deleted file mode 100644 index 8b137891791..00000000000 --- a/addons/import_sugarcrm/sugarcrm_fields_mapping.py +++ /dev/null @@ -1 +0,0 @@ -