← Back to team overview

openerp-dev-web team mailing list archive

lp:~openerp-dev/openobject-addons/trunk-import_sugarcrm-import_opportunities-atp into lp:~openerp-dev/openobject-addons/trunk-import_sugarcrm

 

Atul Patel(OpenERP) has proposed merging lp:~openerp-dev/openobject-addons/trunk-import_sugarcrm-import_opportunities-atp into lp:~openerp-dev/openobject-addons/trunk-import_sugarcrm.

Requested reviews:
  Bhumika (OpenERP) (sbh-openerp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-import_sugarcrm-import_opportunities-atp/+merge/52805

Hello,
1) Backlogs -2 Import CRM: 
   -----------------------
          i) Import sugarcrm Leads into lead
          ii) Import sugarcrm opportunity into lead



Thanks

-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-import_sugarcrm-import_opportunities-atp/+merge/52805
Your team OpenERP R&D Team is subscribed to branch lp:~openerp-dev/openobject-addons/trunk-import_sugarcrm.
=== modified file 'sugarcrm_syncro/__init__.py'
--- sugarcrm_syncro/__init__.py	2011-02-17 11:07:49 +0000
+++ sugarcrm_syncro/__init__.py	2011-03-10 07:05:04 +0000
@@ -22,3 +22,4 @@
 import import_sugarcrm
 import sugar
 import wizard
+import sugarcrm_fields_mapping

=== modified file 'sugarcrm_syncro/import_sugarcrm.py'
--- sugarcrm_syncro/import_sugarcrm.py	2011-03-03 13:52:35 +0000
+++ sugarcrm_syncro/import_sugarcrm.py	2011-03-10 07:05:04 +0000
@@ -18,10 +18,151 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-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 get_lead_status(self, 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'], '')
+    lead_pool = self.pool.get("crm.lead")
+    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, context):
+        stage_id = stage.id         
+    return stage_id
+
+def get_opportunity_status(self, cr, uid, sugar_val,context=None):
+    if not context:
+       context = {} 
+    stage_id = ''           
+    stage_dict = {'sales_stage': #field in the sugarcrm database
+             { #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'], '')
+    lead_pool = self.pool.get("crm.lead")
+    stage_pool = self.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(self, cr, uid, context=None):
+   if not context:
+       context = {} 
+   user_id = import_users(self, cr, uid, context) 
+   map_lead = {               
+            'Leads':{'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',
+                   'write_date':'date_modified',
+                   'function':'title',
+                   'street': 'primary_address_street',
+                   'zip': 'primary_address_postalcode',
+                   'city':'primary_address_city',
+                  },
+               }
+      
+   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, 'Leads')
+   for val in sugar_data:
+       fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, 'Leads', map_lead)
+       stage_id = get_lead_status(self, cr, uid, val, context)
+       openerp_val = dict(zip(fields,datas))
+       openerp_val['type'] = 'lead'
+       openerp_val['user_id'] = user_id
+       openerp_val['stage_id'] = stage_id
+       new_lead_id = lead_pool.create(cr, uid, openerp_val, context)
+   return new_lead_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)
+       stage_id = get_opportunity_status(self, cr, uid, val, context)
+       openerp_val = dict(zip(fields,datas))
+       openerp_val['type'] = 'opportunity'
+       openerp_val['user_id'] = user_id
+       openerp_val['stage_id'] = stage_id
+       new_opportunity_id = lead_pool.create(cr, uid, openerp_val, context)
+   return new_opportunity_id           
+        
+def resolve_dependencies(self, cr, uid, dict, dep, context=None):
+     if not context:
+         context = {}
+     for dependency in dep:
+       resolve_dependencies(self, cr, uid, dict, dict[dependency]['dependencies'], context)
+       dict[dependency]['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,
+                         },
+              'Leads':
+              { 'dependencies' : ['Users'],  #Object to import before this table
+                       'process' : import_leads,
+                     },  
+          } 
 
 class import_sugarcrm(osv.osv):
      """Import SugarCRM DATA"""
@@ -38,203 +179,39 @@
         'lead': lambda *a: True,
         'opportunity': lambda *a: True,
      }        
-
-     def _get_all(self, cr, uid, model, sugar_val, 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')
+     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')
                 
-        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 = []
+         return key_list     
               
-       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',
-        }                 
+     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)
+           for key in keys:
+               resolve_dependencies(self, cr, uid, MAP_FIELDS, MAP_FIELDS[key]['dependencies'], context=context)
+               if MAP_FIELDS[key]['dependencies']:
+                       MAP_FIELDS[key]['process'](self, cr, uid, 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()
-
-
-
-
-

=== modified file 'sugarcrm_syncro/import_sugarcrm_view.xml'
--- sugarcrm_syncro/import_sugarcrm_view.xml	2011-03-01 05:47:06 +0000
+++ sugarcrm_syncro/import_sugarcrm_view.xml	2011-03-10 07:05:04 +0000
@@ -20,7 +20,7 @@
                     <group colspan="4" >
                         <label string="" colspan="2"/>
                         <button  icon="gtk-cancel" special="cancel" string="_Cancel"/>
-                        <button name="import_data" string="Import"
+                        <button name="import_all" string="Import"
                                 type="object" icon="gtk-ok"/>
                    </group>
                 </form>

=== added file 'sugarcrm_syncro/sugarcrm_fields_mapping.py'
--- sugarcrm_syncro/sugarcrm_fields_mapping.py	1970-01-01 00:00:00 +0000
+++ sugarcrm_syncro/sugarcrm_fields_mapping.py	2011-03-10 07:05:04 +0000
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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 <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+def sugarcrm_fields_mapp(dict_sugar,openerp_obj,openerp_dict):
+    fields=[]
+    data_lst = []
+    for key,val in openerp_dict.get(openerp_obj).items():
+        if key not in fields:
+            fields.append(key)
+            if isinstance(val, list):
+                data_lst.append(dict_sugar.get(val[0],['']) + ' ' + dict_sugar.get(val[1],['']))
+            else:
+                data_lst.append(dict_sugar.get(val,['']))
+    return fields,data_lst


Follow ups