From 45c7fab903bb7b42f261a26403420ef365d53ff5 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Wed, 9 Mar 2011 19:38:32 +0530 Subject: [PATCH 01/11] [ADD]: ADD Dependency table and import dependency before Import table for sugarcrm. bzr revid: atp@tinyerp.com-20110309140832-kadne2p106oiox8f --- addons/sugarcrm_syncro/__init__.py | 1 + addons/sugarcrm_syncro/import_sugarcrm.py | 273 +++++------------- .../sugarcrm_syncro/import_sugarcrm_view.xml | 2 +- .../sugarcrm_fields_mapping.py | 32 ++ 4 files changed, 112 insertions(+), 196 deletions(-) create mode 100644 addons/sugarcrm_syncro/sugarcrm_fields_mapping.py diff --git a/addons/sugarcrm_syncro/__init__.py b/addons/sugarcrm_syncro/__init__.py index 94721485e61..c683d3f86c7 100644 --- a/addons/sugarcrm_syncro/__init__.py +++ b/addons/sugarcrm_syncro/__init__.py @@ -22,3 +22,4 @@ import import_sugarcrm import sugar import wizard +import sugarcrm_fields_mapping diff --git a/addons/sugarcrm_syncro/import_sugarcrm.py b/addons/sugarcrm_syncro/import_sugarcrm.py index 5eca4174d20..68583e8b5f3 100644 --- a/addons/sugarcrm_syncro/import_sugarcrm.py +++ b/addons/sugarcrm_syncro/import_sugarcrm.py @@ -22,6 +22,70 @@ from operator import itemgetter from osv import fields, osv import sugar from tools.translate import _ +import sugarcrm_fields_mapping + +def import_users(self, cr, uid, context=None): + if not context: + context = {} + map_user = {'Users': + {'name': ['first_name', 'last_name'], + 'login': 'user_name', + 'new_password' : 'pwd_last_changed', + } , + } + user_obj = self.pool.get('res.users') + ids = self.search(cr, uid, []) + 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: + return user_ids[0] + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, 'Users', map_user) + openerp_val = dict(zip(fields,datas)) + openerp_val['context_lang'] = context.get('lang','en_US') + new_user_id = user_obj.create(cr, uid, openerp_val, context) + return new_user_id + +def import_opportunities(self, cr, uid, context=None): + if not context: + context = {} + user_id = import_users(self, cr, uid, context) + map_opportunity = {'Opportunities': {'name': 'name', + 'probability': 'probability', + 'planned_revenue': 'amount_usdollar', + 'date_deadline':'date_closed' + }, + } + ids = self.search(cr, uid, []) + lead_pool = self.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: + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, 'Opportunities', map_opportunity) + openerp_val = dict(zip(fields,datas)) + openerp_val['type'] = 'opportunity' + openerp_val['user_id'] = user_id + new_lead_id = lead_pool.create(cr, uid, openerp_val, context) + return new_lead_id + +def resolve_dependencies(self, cr, uid, dict, dep, key, context=None): + if not context: + context = {} + for dependency in dep: + resolve_dependencies(self, cr, uid, dict, dict[dependency]['dependencies'], key, context) + dict[dependency]['process'](self, cr, uid, context) + dict[key]['process'](self, cr, uid, context) + return True + +MAP_FIELDS = {'Opportunities': #Object Mapping name + { 'dependencies' : ['Users'], #Object to import before this table + 'process' : import_opportunities, + }, + 'Users' : {'dependencies' : [], + 'process' : import_users, + } + } class import_sugarcrm(osv.osv): """Import SugarCRM DATA""" @@ -39,202 +103,21 @@ class import_sugarcrm(osv.osv): 'opportunity': lambda *a: True, } - def _get_all(self, cr, uid, model, sugar_val, context=None): + def import_all(self, cr, uid, ids, context=None): if not context: context = {} - models = self.pool.get(model) - all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) - 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(self, cr, uid, sugar_val, context=None): - if not context: - context = {} - return self._get_all( - cr, uid, 'res.country.state', sugar_val, context=context) - - def _get_all_countries(self, cr, uid, sugar_val, context=None): - if not context: - context = {} - return self._get_all( - cr, uid, 'res.country', sugar_val, context=context) - - def _get_lead_status(self, cr, uid, sugar_val, context=None): - if not context: - context = {} - sugar_stage = '' - if sugar_val.get('status','') == 'New': - sugar_stage = 'New' - elif sugar_val.get('status','') == 'Assigned': - sugar_stage = 'Qualification' - elif sugar_val.get('status','') == 'In Progress': - sugar_stage = 'Proposition' - elif sugar_val.get('status','') == 'Recycled': - sugar_stage = 'Negotiation' - elif sugar_val.get('status','') == 'Dead': - sugar_stage = 'Lost' - else: - sugar_stage = '' - return sugar_stage - - def _get_opportunity_status(self, cr, uid, sugar_val, context=None): - if not context: - context = {} - sugar_stage = '' - if sugar_val.get('sales_stage','') == 'Need Analysis': - sugar_stage = 'New' - elif sugar_val.get('sales_stage','') == 'Closed Lost': - sugar_stage = 'Lost' - elif sugar_val.get('sales_stage','') == 'Closed Won': - sugar_stage = 'Won' - elif sugar_val.get('sales_stage','') == 'Value Proposition': - sugar_stage = 'Proposition' - elif sugar_val.get('sales_stage','') == 'Negotiation/Review': - sugar_stage = 'Negotiation' - else: - sugar_stage = '' - return sugar_stage - - def create_lead(self, cr, uid, sugar_val, country, state, context=None): - if not context: - context = {} - - lead_pool = self.pool.get("crm.lead") - stage_id = '' - stage = self._get_lead_status(cr, uid, sugar_val, context=None) - stage_pool = self.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): - stage_id = stage.id - vals = {'name': sugar_val.get('first_name','')+' '+ sugar_val.get('last_name',''), - 'contact_name': sugar_val.get('first_name','')+' '+ sugar_val.get('last_name',''), - 'user_id':sugar_val.get('created_by',''), - 'description': sugar_val.get('description',''), - 'partner_name': sugar_val.get('first_name','')+' '+ sugar_val.get('last_name',''), - 'email_from': sugar_val.get('email1',''), - 'stage_id': stage_id or '', - 'phone': sugar_val.get('phone_work',''), - 'mobile': sugar_val.get('phone_mobile',''), - 'write_date':sugar_val.get('date_modified',''), - 'function':sugar_val.get('title',''), - 'street': sugar_val.get('primary_address_street',''), - 'zip': sugar_val.get('primary_address_postalcode',''), - 'city':sugar_val.get('primary_address_city',''), - 'country_id': country and country[0][0] or False, - 'state_id': state and state[0][0] or False - } - new_lead_id = lead_pool.create(cr, uid, vals) - return new_lead_id - - def create_opportunity(self, cr, uid, sugar_val, country, state, context=None): - if not context: - context = {} - lead_pool = self.pool.get("crm.lead") - stage_id = '' - stage_pool = self.pool.get('crm.case.stage') - stage = self._get_opportunity_status(cr, uid, sugar_val, context) - - stage_ids = stage_pool.search(cr, uid, [("type", '=', 'opportunity'), ('name', '=', stage)]) - for stage in stage_pool.browse(cr, uid, stage_ids): - stage_id = stage.id - vals = {'name': sugar_val.get('name',''), - 'probability': sugar_val.get('probability',''), - 'user_id': sugar_val.get('created_by', ''), - 'stage_id': stage_id or '', - 'type': 'opportunity', - 'user_id': sugar_val.get('created_by',''), - 'planned_revenue': sugar_val.get('amount_usdollar'), - 'write_date':sugar_val.get('date_modified',''), - } - new_opportunity_id = lead_pool.create(cr, uid, vals) - return new_opportunity_id - - def _get_sugar_module_name(self, cr, uid, ids, context=None): - - if not context: - context = {} - - sugar_name = [] - - for current in self.read(cr, uid, ids): - if current.get('lead'): - sugar_name.append('Leads') - if current.get('opportunity'): - sugar_name.append('Opportunities') - - return sugar_name - - - def _get_module_name(self, cr, uid, ids, context=None): - - if not context: - context = {} - module_name = [] - - for current in self.read(cr, uid, ids, ['lead', 'opportunity']): - if not current.get('lead') and not current.get('opportunity'): - raise osv.except_osv(_('Error !'), _('Please Select Module')) - - if current.get('lead'): - module_name.append('crm') - if current.get('opportunity'): - module_name.append('crm') - - ids = self.pool.get("ir.module.module").search(cr, uid, [('name', 'in', module_name),('state', '=', 'installed')]) - if not ids: - for module in module_name: - raise osv.except_osv(_('Error !'), _('Please Install %s Module') % ((module))) - - def get_create_method(self, cr, uid, sugar_name, sugar_val, country, state, context=None): - if not context: - context = {} - - if sugar_name == "Leads": - self.create_lead(cr, uid, sugar_val, country, state, context) - - elif sugar_name == "Opportunities": - self.create_opportunity(cr, uid, sugar_val, country, state,context) - return {} - - def import_data(self, cr, uid, ids,context=None): - if not context: - context={} - sugar_val = [] - - self._get_module_name(cr, uid, ids, context) - sugar_module = self._get_sugar_module_name(cr, uid, ids, context=None) - - PortType,sessionid = sugar.login(context.get('username',''), context.get('password','')) - for sugar_name in sugar_module: - sugar_data = sugar.search(PortType,sessionid,sugar_name) - - for val in sugar_data: - country = self._get_all_countries(cr, uid, val.get('primary_address_country'), context) - state = self._get_all_states(cr, uid, val.get('primary_address_state'), context) - self.get_create_method(cr, uid, sugar_name, val, country, state, context) - - - 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', - } + for key in MAP_FIELDS.keys(): + resolve_dependencies(self, cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], key, context=context) + 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', + } import_sugarcrm() - - - - - diff --git a/addons/sugarcrm_syncro/import_sugarcrm_view.xml b/addons/sugarcrm_syncro/import_sugarcrm_view.xml index 96c2043a9cf..a847c0ebe77 100644 --- a/addons/sugarcrm_syncro/import_sugarcrm_view.xml +++ b/addons/sugarcrm_syncro/import_sugarcrm_view.xml @@ -20,7 +20,7 @@