From 3b82c73f4c55cf4cbc4ccdc6a66ba34db43fa3a1 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Thu, 24 Mar 2011 09:59:43 +0530 Subject: [PATCH 1/8] [ADD]: Import Accounts data into Partner form. bzr revid: atp@tinyerp.com-20110324042943-yzb7gy4xv4v3vqof --- .../__init__.py | 0 .../__openerp__.py | 0 addons/import_sugarcrm/import_sugarcrm.py | 427 ++++++++++++++++++ .../import_sugarcrm_view.xml | 4 +- .../sugar.py | 0 .../sugarcrm_fields_mapping.py | 0 .../sugarsoap_services.py | 0 .../sugarsoap_services_types.py | 0 .../wizard/__init__.py | 0 .../wizard/import_message.py | 0 .../wizard/import_message_view.xml | 0 .../wizard/sugarcrm_login.py | 9 +- .../wizard/sugarcrm_login_view.xml | 4 +- addons/sugarcrm_syncro/import_sugarcrm.py | 248 ---------- 14 files changed, 439 insertions(+), 253 deletions(-) rename addons/{sugarcrm_syncro => import_sugarcrm}/__init__.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/__openerp__.py (100%) create mode 100644 addons/import_sugarcrm/import_sugarcrm.py rename addons/{sugarcrm_syncro => import_sugarcrm}/import_sugarcrm_view.xml (91%) rename addons/{sugarcrm_syncro => import_sugarcrm}/sugar.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/sugarcrm_fields_mapping.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/sugarsoap_services.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/sugarsoap_services_types.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/wizard/__init__.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/wizard/import_message.py (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/wizard/import_message_view.xml (100%) rename addons/{sugarcrm_syncro => import_sugarcrm}/wizard/sugarcrm_login.py (87%) rename addons/{sugarcrm_syncro => import_sugarcrm}/wizard/sugarcrm_login_view.xml (89%) delete mode 100644 addons/sugarcrm_syncro/import_sugarcrm.py diff --git a/addons/sugarcrm_syncro/__init__.py b/addons/import_sugarcrm/__init__.py similarity index 100% rename from addons/sugarcrm_syncro/__init__.py rename to addons/import_sugarcrm/__init__.py diff --git a/addons/sugarcrm_syncro/__openerp__.py b/addons/import_sugarcrm/__openerp__.py similarity index 100% rename from addons/sugarcrm_syncro/__openerp__.py rename to addons/import_sugarcrm/__openerp__.py diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py new file mode 100644 index 00000000000..6eb8f994865 --- /dev/null +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -0,0 +1,427 @@ +# -*- 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 . +# +############################################################################## +from osv import fields, osv +from operator import itemgetter +import sugar +import sugarcrm_fields_mapping +import pprint +pp = pprint.PrettyPrinter(indent=4) + +def create_mapping(obj, cr, uid, res_model, open_id, sugar_id, context): + model_data = { + 'name': sugar_id, + 'model': res_model, + 'module': 'sugarcrm_import', + 'res_id': open_id + } + model_obj = obj.pool.get('ir.model.data') + model_obj.create(cr, uid, model_data, context=context) + +def find_mapped_id(obj, cr, uid, res_model, sugar_id, context): + model_obj = obj.pool.get('ir.model.data') + return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context) + +def get_all(sugar_obj, cr, uid, model, sugar_val, context=None): + models = sugar_obj.pool.get(model) + str = sugar_val[0:2] + all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', str.upper())]) + output = [(False, '')] + output = sorted([(o.id, o.name) + for o in models.browse(cr, uid, all_model_ids, + context=context)], + key=itemgetter(1)) + return output + +def get_all_states(sugar_obj, cr, uid, sugar_val, context=None): + return get_all(sugar_obj, + cr, uid, 'res.country.state', sugar_val, context=context) + +def get_all_countries(sugar_obj, cr, uid, sugar_val, context=None): + return get_all(sugar_obj, + cr, uid, 'res.country', sugar_val, context=context) + +def import_partner_address(sugar_obj, cr, uid, context=None): + if not context: + context = {} + res_country_obj = sugar_obj.pool.get('res.country') + str = val.get('primary_address_country')[0:2] + map_partner_address = { + 'id': 'id', + 'name': ['first_name', 'last_name'], + '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' + } + address_obj = sugar_obj.pool.get('res.partner.address') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Contacts') + for val in sugar_data: + country = 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'), context) + val['country_id/id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('primary_address_country'), 'code': str}), + val['state_id/id'] = state and state[0][0] or False + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) + address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + + + +def import_users(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_user = {'id' : 'id', + 'name': ['first_name', 'last_name'], + 'login': 'user_name', + + 'context_lang' : 'context_lang', + 'password' : 'password', + '.id' : '.id', + 'context_department_id': 'department' + } + user_obj = sugar_obj.pool.get('res.users') + department_obj = sugar_obj.pool.get('hr.department') + PortType,sessionid = sugar.login(context.get('username',''), context.get('password',''), context.get('url','')) + sugar_data = sugar.search(PortType,sessionid, 'Users') + for val in sugar_data: + 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 +# cr.execute('SELECT * FROM users_signatures u LIMIT 0,1000') + new_department_id = department_obj.create(cr, uid, {'name': val.get('department')}) + val['context_department_id'] = new_department_id + val['context_lang'] = context.get('lang','en_US') + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user) + #All data has to be imported separatly because they don't have the same field + user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + +def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None): + if not context: + context = {} + stage_id = '' + stage_dict = {'status': #field in the sugarcrm database + { #Mapping of sugarcrm stage : openerp opportunity stage + 'New' : 'New', + 'Assigned':'Qualification', + 'In Progress': 'Proposition', + 'Recycled': 'Negotiation', + 'Dead': 'Lost' + },} + stage = stage_dict['status'].get(sugar_val['status'], '') + stage_pool = surgar_obj.pool.get('crm.case.stage') + stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)]) + for stage in stage_pool.browse(cr, uid, stage_ids, context): + stage_id = stage.id + return stage_id + +def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None): + if not context: + context = {} + stage_id = '' + stage_dict = { 'sales_stage': + {#Mapping of sugarcrm stage : openerp opportunity stage Mapping + 'Need Analysis': 'New', + 'Closed Lost': 'Lost', + 'Closed Won': 'Won', + 'Value Proposition': 'Proposition', + 'Negotiation/Review': 'Negotiation' + }, + } + stage = stage_dict['sales_stage'].get(sugar_val['sales_stage'], '') + stage_pool = surgar_obj.pool.get('crm.case.stage') + stage_ids = stage_pool.search(cr, uid, [('type', '=', 'opportunity'), ('name', '=', stage)]) + for stage in stage_pool.browse(cr, uid, stage_ids, context): + stage_id = stage.id + return stage_id + +def get_billing_address(sugar_obj,cr, uid, val, map_partner_address, context=None): + fields=[] + datas=[] + + address_obj = sugar_obj.pool.get('res.partner.address') + res_country_obj = sugar_obj.pool.get('res.country') + str = val.get('billing_address_country')[0:2] + map_partner_address.update({ + 'street': 'billing_address_street', + 'zip': 'billing_address_postalcode', + 'city': 'billing_address_city', + 'country_id': 'country_id', + 'type': 'type' + }) + val['type'] = 'invoice' + country = get_all_countries(sugar_obj,cr, uid, val.get('billing_address_country'), context) + state = get_all_states(sugar_obj, cr, uid, val.get('billing_address_state'), context) + val['country_id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('billing_address_country'), 'code': str}), + val['state_id'] = state and state[0][0] or False, + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) + dict_val = dict(zip(fields,datas)) + new_address_id = address_obj.create(cr,uid, dict_val) + return new_address_id + +def get_shipping_address(sugar_obj,cr, uid, val, map_partner_address, context=None): + fields=[] + datas=[] + str = '' + address_obj = sugar_obj.pool.get('res.partner.address') + res_country_obj = sugar_obj.pool.get('res.country') + str = val.get('shipping_address_country')[0:2] + map_partner_address.update({ + 'street': 'shipping_address_street', + 'zip': 'shipping_address_postalcode', + 'city': 'shipping_address_city', + 'country_id': 'country_id', + 'type': 'type' + }) + val['type'] = 'delivery' + country = get_all_countries(sugar_obj, cr, uid, val.get('shipping_address_country'), context) + state = get_all_states(sugar_obj, cr, uid, val.get('shipping_address_state'), context) + val['country_id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('shipping_address_country'), 'code': str}), + val['state_id'] = state and state[0][0] or False, + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) + dict_val = dict(zip(fields,datas)) + new_address_id = address_obj.create(cr,uid, dict_val) + return new_address_id + + +def get_address(sugar_obj, cr, uid, val, context=None): + map_partner_address={} + address_id=[] + fields=[] + datas=[] + address_obj = sugar_obj.pool.get('res.partner.address') + address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))]) + if address_ids: + return address_ids + else: + map_partner_address = { + 'id': 'id', + 'name': 'name', + 'partner_id/id': 'account_id', + 'phone': 'phone_office', + 'mobile': 'phone_mobile', + 'fax': 'phone_fax', + 'type': 'type' + } + + address_id.append(get_billing_address(sugar_obj,cr, uid, val, map_partner_address, context)) + address_id.append(get_shipping_address(sugar_obj,cr, uid, val, map_partner_address, context)) + return address_id + +def import_partners(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_lead = { + 'id': 'id', + 'name': 'name', + 'website': 'website', + 'address/.id': 'address/.id', + 'user_id/id': 'assigned_user_id', + 'ref': 'sic_code', + 'comment': ['description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'], + 'customer': False, + 'supplier': False + } + + partner_obj = sugar_obj.pool.get('res.partner') + address_obj = sugar_obj.pool.get('res.partner.address') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Accounts') + + for val in sugar_data: + add_id = get_address(sugar_obj, cr, uid, val, context) + for res_address in address_obj.browse(cr, uid, add_id): + partner_ids = partner_obj.search(cr, uid, [('name', '=', res_address.name)]) + if partner_ids: + for partner in partner_obj.browse(cr, uid, partner_ids): + address_obj.write(cr,uid,res_address.id,{'partner_id':partner.id}) + else: + val['address/.id'] = res_address + if val.get('account_type') in ('customer', 'prospect', 'other'): + val['customer'] = True + else: + val['supplier'] = True + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) + partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + + +def import_leads(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_lead = { + 'id' : 'id', + 'name': ['first_name', 'last_name'], + 'contact_name': ['first_name', 'last_name'], + 'description': 'description', + 'partner_name': ['first_name', 'last_name'], + 'email_from': 'email1', + 'phone': 'phone_work', + 'mobile': 'phone_mobile', + 'function':'title', + 'street': 'primary_address_street', + 'zip': 'primary_address_postalcode', + 'city':'primary_address_city', + 'user_id/id' : 'assigned_user_id', + 'stage_id.id' : 'stage_id.id', + 'type' : 'type', + } + + lead_obj = sugar_obj.pool.get('crm.lead') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Leads') + for val in sugar_data: + val['type'] = 'lead' + stage_id = get_lead_status(sugar_obj, cr, uid, val, context) + val['stage_id.id'] = stage_id + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) + lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + +def import_opportunities(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_opportunity = {'id' : 'id', + 'name': 'name', + 'probability': 'probability', + 'planned_revenue': 'amount_usdollar', + 'date_deadline':'date_closed', + 'user_id/id' : 'assigned_user_id', + 'stage_id.id' : 'stage_id.id', + 'type' : 'type', + } + lead_obj = sugar_obj.pool.get('crm.lead') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Opportunities') + for val in sugar_data: + val['type'] = 'opportunity' + stage_id = get_opportunity_status(sugar_obj, cr, uid, val, context) + val['stage_id.id'] = stage_id + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity) + lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + +MAP_FIELDS = {'Opportunities': #Object Mapping name + {'dependencies' : ['Users'], #Object to import before this table + 'process' : import_opportunities, + }, + 'Leads': + {'dependencies' : ['Users'], #Object to import before this table + 'process' : import_leads, + }, + 'Contacts': + {'dependencies' : ['Users'], #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, + }, +# 'Users_Addresses': +# {'dependencies' : [], +# 'process' : import_users_addresses, +# }, + } + +class import_sugarcrm(osv.osv): + """Import SugarCRM DATA""" + + + _name = "import.sugarcrm" + _description = __doc__ + _columns = { + 'lead': fields.boolean('Leads', help="If Leads is checked, SugarCRM Leads data imported in openerp crm-Lead form"), + 'opportunity': fields.boolean('Opportunities', help="If Leads is checked, SugarCRM Leads data imported in openerp crm-Opportunity form"), + 'user': fields.boolean('User', help="If Users is checked, SugarCRM Users data imported in openerp crm-Opportunity form"), + 'contact': fields.boolean('Contacts', help="If Contacts is checked, SugarCRM Contacts data imported in openerp partner address form"), + 'account': fields.boolean('Accounts', help="If Accounts is checked, SugarCRM Accounts data imported in openerp partner form"), + 'username': fields.char('User Name', size=64), + 'password': fields.char('Password', size=24), + } + _defaults = { + 'lead': True, + 'opportunity': True, + 'user' : True, + 'contact' : True, + 'account' : True, + } + + def get_key(self, cr, uid, ids, context=None): + """Select Key as For which Module data we want import data.""" + if not context: + context = {} + key_list = [] + for current in self.browse(cr, uid, ids, context): + if current.lead: + key_list.append('Leads') + if current.opportunity: + key_list.append('Opportunities') + if current.user: + key_list.append('Users') + if current.contact: + key_list.append('Contacts') + if current.account: + key_list.append('Accounts') + + + return key_list + + def import_all(self, cr, uid, ids, context=None): + """Import all sugarcrm data into openerp module""" + if not context: + context = {} + 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) + + + 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']) + return { + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'import.message', + 'views': [(resource_id,'form')], + '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/sugarcrm_syncro/import_sugarcrm_view.xml b/addons/import_sugarcrm/import_sugarcrm_view.xml similarity index 91% rename from addons/sugarcrm_syncro/import_sugarcrm_view.xml rename to addons/import_sugarcrm/import_sugarcrm_view.xml index 26a3b259140..d46e22dce9b 100644 --- a/addons/sugarcrm_syncro/import_sugarcrm_view.xml +++ b/addons/import_sugarcrm/import_sugarcrm_view.xml @@ -9,11 +9,13 @@ form
- + + + diff --git a/addons/sugarcrm_syncro/sugar.py b/addons/import_sugarcrm/sugar.py similarity index 100% rename from addons/sugarcrm_syncro/sugar.py rename to addons/import_sugarcrm/sugar.py diff --git a/addons/sugarcrm_syncro/sugarcrm_fields_mapping.py b/addons/import_sugarcrm/sugarcrm_fields_mapping.py similarity index 100% rename from addons/sugarcrm_syncro/sugarcrm_fields_mapping.py rename to addons/import_sugarcrm/sugarcrm_fields_mapping.py diff --git a/addons/sugarcrm_syncro/sugarsoap_services.py b/addons/import_sugarcrm/sugarsoap_services.py similarity index 100% rename from addons/sugarcrm_syncro/sugarsoap_services.py rename to addons/import_sugarcrm/sugarsoap_services.py diff --git a/addons/sugarcrm_syncro/sugarsoap_services_types.py b/addons/import_sugarcrm/sugarsoap_services_types.py similarity index 100% rename from addons/sugarcrm_syncro/sugarsoap_services_types.py rename to addons/import_sugarcrm/sugarsoap_services_types.py diff --git a/addons/sugarcrm_syncro/wizard/__init__.py b/addons/import_sugarcrm/wizard/__init__.py similarity index 100% rename from addons/sugarcrm_syncro/wizard/__init__.py rename to addons/import_sugarcrm/wizard/__init__.py diff --git a/addons/sugarcrm_syncro/wizard/import_message.py b/addons/import_sugarcrm/wizard/import_message.py similarity index 100% rename from addons/sugarcrm_syncro/wizard/import_message.py rename to addons/import_sugarcrm/wizard/import_message.py diff --git a/addons/sugarcrm_syncro/wizard/import_message_view.xml b/addons/import_sugarcrm/wizard/import_message_view.xml similarity index 100% rename from addons/sugarcrm_syncro/wizard/import_message_view.xml rename to addons/import_sugarcrm/wizard/import_message_view.xml diff --git a/addons/sugarcrm_syncro/wizard/sugarcrm_login.py b/addons/import_sugarcrm/wizard/sugarcrm_login.py similarity index 87% rename from addons/sugarcrm_syncro/wizard/sugarcrm_login.py rename to addons/import_sugarcrm/wizard/sugarcrm_login.py index 0ebb68cb020..b1df71b27b0 100644 --- a/addons/sugarcrm_syncro/wizard/sugarcrm_login.py +++ b/addons/import_sugarcrm/wizard/sugarcrm_login.py @@ -20,7 +20,7 @@ ############################################################################## from osv import fields, osv from tools.translate import _ -from sugarcrm_syncro import sugar +from import_sugarcrm import sugar class sugarcrm_login(osv.osv): """SugarCRM Login""" @@ -30,23 +30,26 @@ class sugarcrm_login(osv.osv): _columns = { 'username': fields.char('User Name', size=64, required=True), 'password': fields.char('Password', size=24,required=True), + 'url' : fields.char('SugarCRM Path', size=264, required=True, help="Path for SugarCRM connection should be 'http://localhost/sugarcrm/soap.php' Format."), + } _defaults = { 'username' : 'admin', 'password' : 'admin', + 'url': "http://localhost/sugarcrm/soap.php" } def open_import(self, cr, uid, ids, context=None): for current in self.browse(cr, uid, ids, context): - PortType,sessionid = sugar.login(current.username, current.password) + PortType,sessionid = sugar.login(current.username, current.password, current.url) if sessionid == '-1': raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password !')) obj_model = self.pool.get('ir.model.data') model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','import.sugarcrm.form')]) resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id']) - context.update({'rec_id': ids, 'username': current.username, 'password': current.password}) + context.update({'rec_id': ids, 'username': current.username, 'password': current.password, 'url': current.url}) return { 'view_type': 'form', 'view_mode': 'form', diff --git a/addons/sugarcrm_syncro/wizard/sugarcrm_login_view.xml b/addons/import_sugarcrm/wizard/sugarcrm_login_view.xml similarity index 89% rename from addons/sugarcrm_syncro/wizard/sugarcrm_login_view.xml rename to addons/import_sugarcrm/wizard/sugarcrm_login_view.xml index 1e0cc0306c9..4cb8fe98ab6 100644 --- a/addons/sugarcrm_syncro/wizard/sugarcrm_login_view.xml +++ b/addons/import_sugarcrm/wizard/sugarcrm_login_view.xml @@ -9,7 +9,9 @@ form - + + + diff --git a/addons/sugarcrm_syncro/import_sugarcrm.py b/addons/sugarcrm_syncro/import_sugarcrm.py deleted file mode 100644 index 6c7b3fe3cc4..00000000000 --- a/addons/sugarcrm_syncro/import_sugarcrm.py +++ /dev/null @@ -1,248 +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 . -# -############################################################################## -from osv import fields, osv -import sugar -import sugarcrm_fields_mapping -import pprint -pp = pprint.PrettyPrinter(indent=4) - -def create_mapping(obj, cr, uid, res_model, open_id, sugar_id, context): - model_data = { - 'name': sugar_id, - 'model': res_model, - 'module': 'sugarcrm_import', - 'res_id': open_id - } - model_obj = obj.pool.get('ir.model.data') - model_obj.create(cr, uid, model_data, context=context) - -def find_mapped_id(obj, cr, uid, res_model, sugar_id, context): - model_obj = obj.pool.get('ir.model.data') - return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context) - - -def import_users(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_user = {'id' : 'id', - 'name': ['first_name', 'last_name'], - 'login': 'user_name', - - - 'context_lang' : 'context_lang', - 'password' : 'password', - '.id' : '.id', - } - user_obj = sugar_obj.pool.get('res.users') - PortType,sessionid = sugar.login(context.get('username',''), context.get('password','')) - sugar_data = sugar.search(PortType,sessionid, 'Users') - for val in sugar_data: - 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 - - val['context_lang'] = context.get('lang','en_US') - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user) - #All data has to be imported separatly because they don't have the same field - user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) - -def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None): - if not context: - context = {} - stage_id = '' - stage_dict = {'status': #field in the sugarcrm database - { #Mapping of sugarcrm stage : openerp opportunity stage - 'New' : 'New', - 'Assigned':'Qualification', - 'In Progress': 'Proposition', - 'Recycled': 'Negotiation', - 'Dead': 'Lost' - },} - stage = stage_dict['status'].get(sugar_val['status'], '') - stage_pool = surgar_obj.pool.get('crm.case.stage') - stage_ids = stage_pool.search(cr, uid, [('type', '=', 'lead'), ('name', '=', stage)]) - for stage in stage_pool.browse(cr, uid, stage_ids, context): - stage_id = stage.id - return stage_id - -def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None): - if not context: - context = {} - stage_id = '' - stage_dict = { 'sales_stage': - {#Mapping of sugarcrm stage : openerp opportunity stage Mapping - 'Need Analysis': 'New', - 'Closed Lost': 'Lost', - 'Closed Won': 'Won', - 'Value Proposition': 'Proposition', - 'Negotiation/Review': 'Negotiation' - }, - } - stage = stage_dict['sales_stage'].get(sugar_val['sales_stage'], '') - stage_pool = surgar_obj.pool.get('crm.case.stage') - stage_ids = stage_pool.search(cr, uid, [('type', '=', 'opportunity'), ('name', '=', stage)]) - for stage in stage_pool.browse(cr, uid, stage_ids, context): - stage_id = stage.id - return stage_id - -def import_leads(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_lead = { - 'id' : 'id', - 'name': ['first_name', 'last_name'], - 'contact_name': ['first_name', 'last_name'], - 'description': 'description', - 'partner_name': ['first_name', 'last_name'], - 'email_from': 'email1', - 'phone': 'phone_work', - 'mobile': 'phone_mobile', - 'function':'title', - 'street': 'primary_address_street', - 'zip': 'primary_address_postalcode', - 'city':'primary_address_city', - 'user_id/id' : 'assigned_user_id', - - - 'stage_id.id' : 'stage_id.id', - 'type' : 'type', - - } - - lead_obj = sugar_obj.pool.get('crm.lead') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', '')) - sugar_data = sugar.search(PortType, sessionid, 'Leads') - for val in sugar_data: - val['type'] = 'lead' - stage_id = get_lead_status(sugar_obj, cr, uid, val, context) - val['stage_id.id'] = stage_id - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) - lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) - -def import_opportunities(sugar_obj, cr, uid, context=None): - if not context: - context = {} - map_opportunity = {'id' : 'id', - 'name': 'name', - 'probability': 'probability', - 'planned_revenue': 'amount_usdollar', - 'date_deadline':'date_closed', - 'user_id/id' : 'assigned_user_id', - 'stage_id.id' : 'stage_id.id', - 'type' : 'type', - } - lead_obj = sugar_obj.pool.get('crm.lead') - PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', '')) - sugar_data = sugar.search(PortType, sessionid, 'Opportunities') - for val in sugar_data: - val['type'] = 'opportunity' - stage_id = get_opportunity_status(sugar_obj, cr, uid, val, context) - val['stage_id.id'] = stage_id - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_opportunity) - lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) - - - -MAP_FIELDS = {'Opportunities': #Object Mapping name - { 'dependencies' : ['Users'], #Object to import before this table - 'process' : import_opportunities, - }, - 'Users' : {'dependencies' : [], - 'process' : import_users, - }, - 'Leads': - { 'dependencies' : ['Users'], #Object to import before this table - 'process' : import_leads, - }, - } - -class import_sugarcrm(osv.osv): - """Import SugarCRM DATA""" - - - _name = "import.sugarcrm" - _description = __doc__ - _columns = { - 'lead': fields.boolean('Leads', help="If Leads is checked, SugarCRM Leads data imported in openerp crm-Lead form"), - 'opportunity': fields.boolean('Opportunities', help="If Leads is checked, SugarCRM Leads data imported in openerp crm-Opportunity form"), - 'user': fields.boolean('User', help="If Users is checked, SugarCRM Users data imported in openerp crm-Opportunity form"), - 'username': fields.char('User Name', size=64), - 'password': fields.char('Password', size=24), - } - _defaults = { - 'lead': True, - 'opportunity': True, - 'user' : True, - } - def get_key(self, cr, uid, ids, context=None): - """Select Key as For which Module data we want import data.""" - if not context: - context = {} - key_list = [] - for current in self.browse(cr, uid, ids, context): - if current.lead: - key_list.append('Leads') - if current.opportunity: - key_list.append('Opportunities') - if current.user: - key_list.append('Users') - - return key_list - - def import_all(self, cr, uid, ids, context=None): - """Import all sugarcrm data into openerp module""" - if not context: - context = {} - 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) - - - 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']) - return { - 'view_type': 'form', - 'view_mode': 'form', - 'res_model': 'import.message', - 'views': [(resource_id,'form')], - '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() From ce12313fbf33cb19b605295b8c9de514dff80128 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Thu, 24 Mar 2011 10:22:51 +0530 Subject: [PATCH 2/8] [FIX]: Fix Dependency Problem. bzr revid: atp@tinyerp.com-20110324045251-fab8nl4fg9hswbp2 --- addons/import_sugarcrm/__openerp__.py | 2 +- addons/import_sugarcrm/import_sugarcrm.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/addons/import_sugarcrm/__openerp__.py b/addons/import_sugarcrm/__openerp__.py index 55dad57d529..1848b53df37 100644 --- a/addons/import_sugarcrm/__openerp__.py +++ b/addons/import_sugarcrm/__openerp__.py @@ -26,7 +26,7 @@ 'description': """This Module Import SugarCRM "Leads", "Opportunities", and "Users" Data into OpenERP Module.""", 'author': 'OpenERP SA', 'website': 'http://www.openerp.com', - 'depends': ['crm'], + 'depends': ['crm', 'hr'], 'init_xml': [], 'update_xml': ["wizard/sugarcrm_login_view.xml", "wizard/import_message_view.xml", diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 6eb8f994865..b100fbcdd1e 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -62,7 +62,6 @@ def import_partner_address(sugar_obj, cr, uid, context=None): if not context: context = {} res_country_obj = sugar_obj.pool.get('res.country') - str = val.get('primary_address_country')[0:2] map_partner_address = { 'id': 'id', 'name': ['first_name', 'last_name'], @@ -73,17 +72,19 @@ def import_partner_address(sugar_obj, cr, uid, context=None): 'street': 'primary_address_street', 'zip': 'primary_address_postalcode', 'city': 'primary_address_city', - 'country_id/id': 'country_id/id', - 'state_id/id': 'state_id/id' + # 'country_id/id': 'country_id/id', + # 'state_id/id': 'state_id/id' } address_obj = sugar_obj.pool.get('res.partner.address') PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) sugar_data = sugar.search(PortType, sessionid, 'Contacts') for val in sugar_data: + str = val.get('primary_address_country')[0:2] country = 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'), context) - val['country_id/id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('primary_address_country'), 'code': str}), - val['state_id/id'] = state and state[0][0] or False + #Need To Fix +# val['country_id/id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('primary_address_country'), 'code': str}), +# # val['state_id/id'] = state and state[0][0] or False fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) From ed1066bd886a3082c226bfb88a42db796dcfbd0f Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Thu, 24 Mar 2011 14:07:47 +0530 Subject: [PATCH 3/8] [IMP]: Minor Fix bzr revid: atp@tinyerp.com-20110324083747-5hshzfmmtr00qpz3 --- addons/import_sugarcrm/import_sugarcrm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index b100fbcdd1e..00eaf31ec40 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -260,13 +260,13 @@ def import_partners(sugar_obj, cr, uid, context=None): for partner in partner_obj.browse(cr, uid, partner_ids): address_obj.write(cr,uid,res_address.id,{'partner_id':partner.id}) else: - val['address/.id'] = res_address + val['address/.id'] = res_address.id if val.get('account_type') in ('customer', 'prospect', 'other'): val['customer'] = True else: val['supplier'] = True - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) - partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) + partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) def import_leads(sugar_obj, cr, uid, context=None): From 592cb502554794bdae8b8ccc5cb89df33dab0ab1 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Thu, 24 Mar 2011 19:16:24 +0530 Subject: [PATCH 4/8] [ADD]: Add Employee data, improvement in opportunity, leads. bzr revid: atp@tinyerp.com-20110324134624-elr8vo7y2jukypr2 --- addons/import_sugarcrm/import_sugarcrm.py | 112 ++++++++++++++++-- .../import_sugarcrm/import_sugarcrm_view.xml | 1 + 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 00eaf31ec40..75d60245bc2 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -139,6 +139,21 @@ def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None): stage_id = stage.id return stage_id +def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None): + if not context: + context = {} + state = '' + state_dict = {'status': #field in the sugarcrm database + { #Mapping of sugarcrm stage : openerp opportunity stage + 'New' : 'draft', + 'Assigned':'open', + 'In Progress': 'open', + 'Recycled': 'cancel', + 'Dead': 'done' + },} + state = state_dict['status'].get(sugar_val['status'], '') + return state + def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None): if not context: context = {} @@ -207,7 +222,28 @@ def get_shipping_address(sugar_obj,cr, uid, val, map_partner_address, context=No new_address_id = address_obj.create(cr,uid, dict_val) return new_address_id - +def get_user_address(sugar_obj, cr, uid, val, context=None): + address_obj = sugar_obj.pool.get('res.partner.address') + + map_user_address = { + 'name': ['first_name', 'last_name'], + 'city': 'address_city', +# 'country_id.id': 'id(address_country)', +# 'state_id.id': 'id(address_state)', + 'street': 'address_street', + 'zip': 'address_postalcode', + } + address_ids = address_obj.search(cr, uid, [('name', '=',val.get('first_name') +''+ val.get('last_name'))]) + + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user_address) + dict_val = dict(zip(fields,datas)) + + if address_ids: + address_obj.write(cr, uid, address_ids, dict_val) + else: + new_address_id = address_obj.create(cr,uid, dict_val) + return new_address_id + def get_address(sugar_obj, cr, uid, val, context=None): map_partner_address={} address_id=[] @@ -235,7 +271,7 @@ def get_address(sugar_obj, cr, uid, val, context=None): def import_partners(sugar_obj, cr, uid, context=None): if not context: context = {} - map_lead = { + map_partner = { 'id': 'id', 'name': 'name', 'website': 'website', @@ -265,9 +301,48 @@ def import_partners(sugar_obj, cr, uid, context=None): val['customer'] = True else: val['supplier'] = True - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner) partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + +def import_resources(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_resource = {'id' : 'user_hash', + 'name': ['first_name', 'last_name'], + } + resource_obj = sugar_obj.pool.get('resource.resource') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Employees') + for val in sugar_data: + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_resource) + resource_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + + +def import_employees(sugar_obj, cr, uid, context=None): + if not context: + context = {} + map_employee = {'id' : 'user_hash', + 'name': ['first_name', 'last_name'], + 'work_phone': 'phone_work', + 'mobile_phone': 'phone_mobile', + 'user_id/name': ['first_name', 'last_name'], + #'resource_id/id': 'resource_id/id', + 'address_home_id/.id': 'address_home_id/.id', + 'notes': 'description', + #TODO: Creation of Employee create problem. + # 'coach_id/id': 'reports_to_id', +# 'job_id': 'job_id' + } + employee_obj = sugar_obj.pool.get('hr.employee') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) + sugar_data = sugar.search(PortType, sessionid, 'Employees') + for val in sugar_data: + address_id = get_user_address(sugar_obj, cr, uid, val, context) + val['address_home_id/.id'] = address_id + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee) + employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + def import_leads(sugar_obj, cr, uid, context=None): if not context: @@ -277,7 +352,7 @@ def import_leads(sugar_obj, cr, uid, context=None): 'name': ['first_name', 'last_name'], 'contact_name': ['first_name', 'last_name'], 'description': 'description', - 'partner_name': ['first_name', 'last_name'], + 'partner_name': 'account_name', 'email_from': 'email1', 'phone': 'phone_work', 'mobile': 'phone_mobile', @@ -288,6 +363,7 @@ def import_leads(sugar_obj, cr, uid, context=None): 'user_id/id' : 'assigned_user_id', 'stage_id.id' : 'stage_id.id', 'type' : 'type', + 'state': 'state', } lead_obj = sugar_obj.pool.get('crm.lead') @@ -297,6 +373,7 @@ def import_leads(sugar_obj, cr, uid, context=None): val['type'] = 'lead' stage_id = get_lead_status(sugar_obj, cr, uid, val, context) val['stage_id.id'] = stage_id + val['state'] = get_lead_state(sugar_obj, cr, uid, val,context) fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) @@ -306,6 +383,9 @@ def import_opportunities(sugar_obj, cr, uid, context=None): map_opportunity = {'id' : 'id', 'name': 'name', 'probability': 'probability', + 'partner_id/name': 'account_name', + 'title_action': 'next_step', + 'partner_address_id/name': 'account_name', 'planned_revenue': 'amount_usdollar', 'date_deadline':'date_closed', 'user_id/id' : 'assigned_user_id', @@ -323,11 +403,11 @@ def import_opportunities(sugar_obj, cr, uid, context=None): lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) MAP_FIELDS = {'Opportunities': #Object Mapping name - {'dependencies' : ['Users'], #Object to import before this table + {'dependencies' : ['Users', 'Accounts'], #Object to import before this table 'process' : import_opportunities, }, 'Leads': - {'dependencies' : ['Users'], #Object to import before this table + {'dependencies' : ['Users', 'Accounts', 'Contacts'], #Object to import before this table 'process' : import_leads, }, 'Contacts': @@ -343,10 +423,14 @@ MAP_FIELDS = {'Opportunities': #Object Mapping name {'dependencies' : [], 'process' : import_users, }, -# 'Users_Addresses': -# {'dependencies' : [], -# 'process' : import_users_addresses, -# }, + 'Employees': + {'dependencies' : ['Resources'], + 'process' : import_employees, + }, + 'Resources': + {'dependencies' : ['Users'], + 'process' : import_resources, + }, } class import_sugarcrm(osv.osv): @@ -361,6 +445,7 @@ class import_sugarcrm(osv.osv): 'user': fields.boolean('User', help="If Users is checked, SugarCRM Users data imported in openerp crm-Opportunity form"), 'contact': fields.boolean('Contacts', help="If Contacts is checked, SugarCRM Contacts data imported in openerp partner address form"), 'account': fields.boolean('Accounts', help="If Accounts is checked, SugarCRM Accounts data imported in openerp partner form"), + 'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in openerp partner employee form"), 'username': fields.char('User Name', size=64), 'password': fields.char('Password', size=24), } @@ -369,7 +454,8 @@ class import_sugarcrm(osv.osv): 'opportunity': True, 'user' : True, 'contact' : True, - 'account' : True, + 'account' : True, + 'employee' : True, } def get_key(self, cr, uid, ids, context=None): @@ -387,7 +473,9 @@ class import_sugarcrm(osv.osv): if current.contact: key_list.append('Contacts') if current.account: - key_list.append('Accounts') + key_list.append('Accounts') + if current.employee: + key_list.append('Employees') return key_list diff --git a/addons/import_sugarcrm/import_sugarcrm_view.xml b/addons/import_sugarcrm/import_sugarcrm_view.xml index d46e22dce9b..1ad78f7a410 100644 --- a/addons/import_sugarcrm/import_sugarcrm_view.xml +++ b/addons/import_sugarcrm/import_sugarcrm_view.xml @@ -16,6 +16,7 @@ + From 7c78896426df780bda9cd1f49a87f68c013d6207 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Thu, 24 Mar 2011 19:46:41 +0530 Subject: [PATCH 5/8] [Fix] bzr revid: atp@tinyerp.com-20110324141641-3oijofx26udbuun8 --- addons/import_sugarcrm/import_sugarcrm.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 75d60245bc2..09174f3faee 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -191,6 +191,7 @@ def get_billing_address(sugar_obj,cr, uid, val, map_partner_address, context=Non val['type'] = 'invoice' country = get_all_countries(sugar_obj,cr, uid, val.get('billing_address_country'), context) state = get_all_states(sugar_obj, cr, uid, val.get('billing_address_state'), context) + #Need to Fix bcz code create problem. val['country_id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('billing_address_country'), 'code': str}), val['state_id'] = state and state[0][0] or False, fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) @@ -228,8 +229,8 @@ def get_user_address(sugar_obj, cr, uid, val, context=None): map_user_address = { 'name': ['first_name', 'last_name'], 'city': 'address_city', -# 'country_id.id': 'id(address_country)', -# 'state_id.id': 'id(address_state)', + 'country_id.id': 'address_country', + 'state_id.id': 'address_state', 'street': 'address_street', 'zip': 'address_postalcode', } @@ -332,14 +333,17 @@ def import_employees(sugar_obj, cr, uid, context=None): 'notes': 'description', #TODO: Creation of Employee create problem. # 'coach_id/id': 'reports_to_id', -# 'job_id': 'job_id' + 'job_id/.id': 'job_id/.id' } employee_obj = sugar_obj.pool.get('hr.employee') + job_obj = sugar_obj.pool.get('hr.job') PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) sugar_data = sugar.search(PortType, sessionid, 'Employees') for val in sugar_data: address_id = get_user_address(sugar_obj, cr, uid, val, context) val['address_home_id/.id'] = address_id + new_job_id = job_obj.create(cr, uid, {'name': val.get('title')}) + val['job_id/.id'] = new_job_id fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee) employee_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) From 1b8c12e336aa4b7d2aa43ee8504899b65970ccb0 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Fri, 25 Mar 2011 12:45:57 +0530 Subject: [PATCH 6/8] [ADD]: Add resource id in employee module. bzr revid: atp@tinyerp.com-20110325071557-cprcgepl55zypeo0 --- addons/import_sugarcrm/import_sugarcrm.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 09174f3faee..de6f321e062 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -328,7 +328,7 @@ def import_employees(sugar_obj, cr, uid, context=None): 'work_phone': 'phone_work', 'mobile_phone': 'phone_mobile', 'user_id/name': ['first_name', 'last_name'], - #'resource_id/id': 'resource_id/id', + 'resource_id/.id': 'resource_id/.id', 'address_home_id/.id': 'address_home_id/.id', 'notes': 'description', #TODO: Creation of Employee create problem. @@ -342,6 +342,10 @@ def import_employees(sugar_obj, cr, uid, context=None): for val in sugar_data: address_id = get_user_address(sugar_obj, cr, uid, val, context) val['address_home_id/.id'] = address_id + model_ids = find_mapped_id(sugar_obj, cr, uid, 'resource.resource', val.get('user_hash')+ '_resource_resource', context) + resource_id = sugar_obj.pool.get('ir.model.data').browse(cr, uid, model_ids) + if resource_id: + val['resource_id/.id'] = resource_id[0].res_id new_job_id = job_obj.create(cr, uid, {'name': val.get('title')}) val['job_id/.id'] = new_job_id fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_employee) From 26d02e7f5123a1d0df4974446a4d8743b2c472db Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Fri, 25 Mar 2011 18:03:53 +0530 Subject: [PATCH 7/8] [FIX]: Fix Import partner address problem bzr revid: atp@tinyerp.com-20110325123353-r4r6bdsbp2rrgg0h --- addons/import_sugarcrm/import_sugarcrm.py | 30 +++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index de6f321e062..7209109c4a2 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -197,6 +197,7 @@ def get_billing_address(sugar_obj,cr, uid, val, map_partner_address, context=Non fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) dict_val = dict(zip(fields,datas)) new_address_id = address_obj.create(cr,uid, dict_val) + return new_address_id def get_shipping_address(sugar_obj,cr, uid, val, map_partner_address, context=None): @@ -251,6 +252,7 @@ def get_address(sugar_obj, cr, uid, val, context=None): fields=[] datas=[] address_obj = sugar_obj.pool.get('res.partner.address') + model_obj = sugar_obj.pool.get('ir.model.data') address_ids = address_obj.search(cr, uid, [('name', '=',val.get('name')), ('type', 'in', ('invoice', 'delivery')), ('street', '=', val.get('billing_address_street'))]) if address_ids: return address_ids @@ -262,9 +264,8 @@ def get_address(sugar_obj, cr, uid, val, context=None): 'phone': 'phone_office', 'mobile': 'phone_mobile', 'fax': 'phone_fax', - 'type': 'type' + 'type': 'type', } - address_id.append(get_billing_address(sugar_obj,cr, uid, val, map_partner_address, context)) address_id.append(get_shipping_address(sugar_obj,cr, uid, val, map_partner_address, context)) return address_id @@ -276,7 +277,6 @@ def import_partners(sugar_obj, cr, uid, context=None): 'id': 'id', 'name': 'name', 'website': 'website', - 'address/.id': 'address/.id', 'user_id/id': 'assigned_user_id', 'ref': 'sic_code', 'comment': ['description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'], @@ -291,19 +291,17 @@ def import_partners(sugar_obj, cr, uid, context=None): for val in sugar_data: add_id = get_address(sugar_obj, cr, uid, val, context) - for res_address in address_obj.browse(cr, uid, add_id): - partner_ids = partner_obj.search(cr, uid, [('name', '=', res_address.name)]) - if partner_ids: - for partner in partner_obj.browse(cr, uid, partner_ids): - address_obj.write(cr,uid,res_address.id,{'partner_id':partner.id}) - else: - val['address/.id'] = res_address.id - if val.get('account_type') in ('customer', 'prospect', 'other'): - val['customer'] = True - else: - val['supplier'] = True - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner) - partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + if val.get('account_type') in ('customer', 'prospect', 'other'): + val['customer'] = True + else: + val['supplier'] = True + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner) + partner_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + for a in address_obj.browse(cr,uid,add_id): + data_id=partner_obj.search(cr,uid,[('name','like',a.name),('website','like',val.get('website'))]) + if data_id: + address_obj.write(cr,uid,a.id,{'partner_id':data_id[0]}) + return True def import_resources(sugar_obj, cr, uid, context=None): From 35de82308730b85b8b8bd19c61f0ce255988d05f Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Fri, 25 Mar 2011 18:20:15 +0530 Subject: [PATCH 8/8] [ADD]: Add country into contacts data bzr revid: atp@tinyerp.com-20110325125015-2gb7hy8rwcn6c7jl --- addons/import_sugarcrm/import_sugarcrm.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 7209109c4a2..cb56f71bdaa 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -72,8 +72,8 @@ def import_partner_address(sugar_obj, cr, uid, context=None): 'street': 'primary_address_street', 'zip': 'primary_address_postalcode', 'city': 'primary_address_city', - # 'country_id/id': 'country_id/id', - # 'state_id/id': 'state_id/id' + 'country_id/.id': 'country_id/.id', + 'state_id/.id': 'state_id/id' } address_obj = sugar_obj.pool.get('res.partner.address') PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url','')) @@ -81,10 +81,13 @@ def import_partner_address(sugar_obj, cr, uid, context=None): for val in sugar_data: str = val.get('primary_address_country')[0:2] country = get_all_countries(sugar_obj, cr, uid, val.get('primary_address_country'), context) + if country: + country_id = country and country[0][0] + else: + country_id = res_country_obj.create(cr, uid, {'name': val.get('primary_address_country'), 'code': str}) state = get_all_states(sugar_obj,cr, uid, val.get('primary_address_state'), context) - #Need To Fix -# val['country_id/id'] = country and country[0][0] or res_country_obj.create(cr, uid, {'name': val.get('primary_address_country'), 'code': str}), -# # val['state_id/id'] = state and state[0][0] or False + val['country_id/.id'] = country_id + val['state_id/.id'] = state and state[0][0] or False fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address) address_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context)