Launchpad automatic translations update.
[odoo/odoo.git] / addons / import_sugarcrm / import_sugarcrm.py
index 38c6a25..3ff14bc 100644 (file)
 #
 ##############################################################################
 from osv import fields, osv
-from operator import itemgetter
 import sugar
-import sugarcrm_fields_mapping
 from tools.translate import _
+from import_base.import_framework import *
+from import_base.mapper import *
+from datetime import datetime
+import base64
 import pprint
 pp = pprint.PrettyPrinter(indent=4)
+#copy old import here
 
+import htmllib
 
-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 unescape_htmlentities(s):
+    p = htmllib.HTMLParser(None)
+    p.save_bgn()
+    p.feed(s)
+    return p.save_end()
 
-def get_all(sugar_obj, cr, uid, model, sugar_val, context=None):
-    models = sugar_obj.pool.get(model)
-    model_code = sugar_val[0:2]
-    all_model_ids = models.search(cr, uid, [('name', '=', sugar_val)]) or models.search(cr, uid, [('code', '=', model_code.upper())]) 
-    output = sorted([(o.id, o.name)
-                     for o in models.browse(cr, uid, all_model_ids, context=context)],
-                    key=itemgetter(1))
-    return output
+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 ''
 
-def get_all_states(sugar_obj, cr, uid, sugar_val, country_id, context=None):
-    """Get states or create new state"""
-    state_id = False
-    res_country_state_obj = sugar_obj.pool.get('res.country.state')
+class sugar_import(import_framework):
+    URL = False   
+    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_ATTENDEE = 'calendar_attendee'
+    TABLE_CALL = 'Calls'
+    TABLE_MEETING = 'Meetings'
+    TABLE_TASK = 'Tasks'
+    TABLE_PROJECT = 'Project'
+    TABLE_PROJECT_TASK = 'ProjectTask'
+    TABLE_BUG = 'Bugs'
+    TABLE_CASE = 'Cases'
+    TABLE_NOTE = 'Notes'
+    TABLE_EMAIL = 'Emails'
+    TABLE_COMPAIGN = 'Campaigns'
+    TABLE_DOCUMENT = 'Documents'
+    TABLE_HISTORY_ATTACHMNET = 'history_attachment'
+    
+    MAX_RESULT_PER_PAGE = 200
     
-    state = get_all(sugar_obj,
-        cr, uid, 'res.country.state', sugar_val, context=context)
-    if state:
-        state_id = state and state[0][0]
-    else:
-        state_id = res_country_state_obj.create(cr, uid, {'name': sugar_val, 'code': sugar_val, 'country_id': country_id})
-    return state_id   
+    def initialize(self):
+        #login
+        PortType,sessionid = sugar.login(self.context.get('username',''), self.context.get('password',''), self.context.get('url',''))
+        if sessionid == '-1':
+            raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password or bad SugarSoap Api url !'))
+        self.context['port'] = PortType
+        self.context['session_id'] = sessionid
+        
+    def get_data(self, table):
+        offset = 0
+        res = []
+        while True:
+            r = sugar.search(self.context.get('port'), self.context.get('session_id'), table, offset, self.MAX_RESULT_PER_PAGE)
+            res.extend(r)
+            if len(r) < self.MAX_RESULT_PER_PAGE:
+                break;
+            offset += self.MAX_RESULT_PER_PAGE
+        return res
+    
+    #def get_link(self, from_table, ids, to_table):
+        #return sugar.relation_search(self.context.get('port'), self.context.get('session_id'), from_table, module_id=ids, related_module=to_table)
 
-def get_all_countries(sugar_obj, cr, uid, sugar_country_val, context=None):
-    """Get Country or Create new country"""
-    res_country_obj = sugar_obj.pool.get('res.country')
-    country_id = False
-    country_code = sugar_country_val[0:2]
-    country = get_all(sugar_obj,
-        cr, uid, 'res.country', sugar_country_val, context=context)
-    if country:
-        country_id = country and country[0][0] 
-    else:
-        country_id = res_country_obj.create(cr, uid, {'name': sugar_country_val, 'code': country_code})  
-    return country_id
+    """
+    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)])
 
-def import_partner_address(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    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:
-        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        
-        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', noupdate=True, context=context)
-    return True
-    
+    def get_job_title(self, dict, salutation):
+        fields = ['shortcut', 'name', 'domain']
+        if salutation:
+            data = [salutation, salutation, 'Contact']
+            return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)])
 
+    def get_channel_id(self, dict, val):
+        if not val:
+            return False
+        fields = ['name']
+        data = [val]
+        return self.import_object(fields, data, 'crm.case.channel', 'crm_channel', 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 import_users(sugar_obj, cr, uid, context=None):
-    map_user = {
-        'id' : 'id', 
-        '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_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_users_department(sugar_obj, cr, uid, val, context=None):
-        department_id = False       
-        department_obj = sugar_obj.pool.get('hr.department')
-        department_ids = department_obj.search(cr, uid, [('name', '=', val)])
-        if department_ids:
-            department_id = department_ids[0]
-        elif val:
-            department_id = department_obj.create(cr, uid, {'name': val})
-        return department_id 
+    def get_float_time(self, dict, hour, min):
+        min = int(min) * 100 / 60
+        return "%s.%i" % (hour, min)
     
-    if not context:
-        context = {}
-    department_id = False        
+    """
+    import Documents
+    """
     
-    user_obj = sugar_obj.pool.get('res.users')
-    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
-        department_id = get_users_department(sugar_obj, cr, uid, val.get('department'), context=context)
-        val['context_department_id.id'] = 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', noupdate=True, context=context)
-    return True
+    def import_related_document(self, val):
+        res_model = False
+        res_id = False
+        sugar_document_account = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Documents', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None)
+        sugar_document_contact = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Documents', module_id=val.get('id'), related_module=self.TABLE_CONTACT, query=None, deleted=None)
+        sugar_document_opportunity = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Documents', module_id=val.get('id'), related_module=self.TABLE_OPPORTUNITY, query=None, deleted=None)
+        sugar_document_case = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Documents', module_id=val.get('id'), related_module=self.TABLE_CASE, query=None, deleted=None)
+        sugar_document_bug = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Documents', module_id=val.get('id'), related_module=self.TABLE_BUG, query=None, deleted=None)
+        if sugar_document_account:
+            res_id = self.get_mapped_id(self.TABLE_ACCOUNT,sugar_document_account[0])
+            res_model = 'res.partner'
+        elif sugar_document_contact:
+            res_id = self.get_mapped_id(self.TABLE_CONTACT, sugar_document_contact[0])
+            res_model = 'res.partner.address'
+        elif sugar_document_opportunity:
+            res_id = self.get_mapped_id(self.TABLE_OPPORTUNITY, sugar_document_opportunity[0])
+            res_model = 'crm.lead'
+        elif sugar_document_case:
+            res_id = self.get_mapped_id(self.TABLE_CASE, sugar_document_case[0])
+            res_model = 'crm.claim'
+        elif sugar_document_bug:
+            res_id = self.get_mapped_id(self.TABLE_BUG, sugar_document_bug[0])
+            res_model = 'project.issue'
+        return res_id,res_model
+    
+    def import_document(self, val):
+        File,Filename = sugar.get_document_revision_search(self.context.get('port'), self.context.get('session_id'), val.get('document_revision_id'))
+        #File = base64.encodestring(File)
+        res_id, res_model  = self.import_related_document(val)
+        val['res_id'] = res_id
+        val['res_model'] = res_model
+        if File:
+            val['datas'] = File
+            val['datas_fname'] = Filename
+        return val   
+        
+    def get_document_mapping(self): 
+        return { 
+                'model' : 'ir.attachment',
+                'dependencies' : [self.TABLE_USER],
+                'hook' : self.import_document,
+                'map' : {
+                         'name':'document_name',
+                         'description': ppconcat('description'),
+                         'datas': 'datas',
+                         'datas_fname': 'datas_fname',
+                         'res_model': 'res_model',
+                         'res_id': 'res_id',
+                }
+            }     
+        
+    
+    """
+    import Emails
+    """
 
-def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None):
-    if not context:
-        context = {}
-    stage_id = False
-    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 import_email(self, val):
+        vals = sugar.email_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_EMAIL, val.get('id'))
+        model_obj =  self.obj.pool.get('ir.model.data')
+        for val in vals:
+            xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_id'))
+            model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', xml_id)])
+            if model_ids:
+                model = model_obj.browse(self.cr, self.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
+        return val   
+        
+    def get_email_mapping(self): 
+        return { 
+                'model' : 'mail.message',
+                'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD, self.TABLE_OPPORTUNITY, self.TABLE_MEETING, self.TABLE_CALL],
+                'hook' : self.import_email,
+                'map' : {
+                        'subject':'name',
+                        'state' : const('received'),
+                        'date':'date_sent',
+                        'email_from': 'from_addr_name',
+                        'email_to': 'to_addrs_names',
+                        'email_cc': 'cc_addrs_names',
+                        'email_bcc': 'bcc_addrs_names',
+                        'message_id': 'message_id',
+                        'res_id': 'res_id',
+                        'model': 'model',
+                        'partner_id/.id': 'partner_id/.id',                         
+                        'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
+                        'body_text': 'description',
+                        'body_html' : 'description_html',
+                        
+                }
+            } 
+    
+    """
+    import History(Notes)
+    """
+    
 
-def get_lead_state(surgar_obj, cr, uid, sugar_val,context=None):
-    if not context:
-        context = {}
-    state = False
-    state_dict = {'status': #field in the sugarcrm database
-        { #Mapping of sugarcrm stage : openerp opportunity stage
+    def import_history(self, val):
+        model_obj =  self.obj.pool.get('ir.model.data')
+        xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_id'))
+        model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', xml_id)])
+        if model_ids:
+            model = model_obj.browse(self.cr, self.uid, model_ids)[0]
+            if model.model == 'res.partner':
+                val['partner_id/.id'] = model.res_id
+            val['res_id'] = model.res_id
+            val['model'] = model.model
+        File, Filename = sugar.attachment_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_NOTE, val.get('id')) 
+        if File:
+            val['datas'] = File
+            val['datas_fname'] = Filename
+        return val    
+    
+    def get_history_mapping(self): 
+        return { 
+                'model' : 'ir.attachment',
+                'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD, self.TABLE_OPPORTUNITY, self.TABLE_MEETING, self.TABLE_CALL, self.TABLE_EMAIL],
+                'hook' : self.import_history,
+                'map' : {
+                      'name':'name',
+                      'user_id/id': ref(self.TABLE_USER, 'created_by'),
+                      'description': ppconcat('description', 'description_html'),
+                      'res_id': 'res_id',
+                      'res_model': 'model',
+                      'partner_id/.id' : 'partner_id/.id',
+                      'datas' : 'datas',
+                      'datas_fname' : 'datas_fname'
+                }
+            }     
+    
+    """
+    import Claims(Cases)
+    """
+    def get_claim_priority(self, val):
+        priority_dict = {            
+                'P1': '2',
+                'P2': '3',
+                'P3': '4'
+        }
+        return priority_dict.get(val.get('priority'), '')
+        
+    def get_contact_info_from_account(self, val):
+        partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, val.get('account_id'))
+        partner_address_id = False
+        partner_phone = False
+        partner_email = False
+        partner = self.obj.pool.get('res.partner').browse(self.cr, self.uid, [partner_id])[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 import_crm_claim(self, val):
+        partner_address_id, partner_phone,partner_email =  self.get_contact_info_from_account(val)
+        val['partner_address_id/.id'] = partner_address_id
+        val['partner_phone'] = partner_phone
+        val['email_from'] = partner_email
+        return val
+    
+    def get_crm_claim_mapping(self): 
+        return { 
+                'model' : 'crm.claim',
+                'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD],
+                'hook' : self.import_crm_claim,
+                'map' : {
+                    'name': concat('case_number','name', delimiter='-'),
+                    'date': 'date_entered',
+                    'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
+                    'description': ppconcat('description', 'resolution', 'work_log'),
+                    'partner_id/id': ref(self.TABLE_ACCOUNT, 'account_id'),
+                    'partner_address_id/.id': 'partner_address_id/.id',
+                    'categ_id/id': call(self.get_category, 'crm.claim', value('type')),
+                    'partner_phone': 'partner_phone',
+                    'email_from': 'email_from',                                        
+                    'priority': self.get_claim_priority,
+                    'state': map_val('status', self.project_issue_state)
+                }
+            }    
+    """
+    Import Project Issue(Bugs)
+    """
+    project_issue_state = {
             '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 = {}
-    stage_id = False
-    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_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': 'country_id',
-    'state_id': 'state_id',
-    'street': 'address_street',
-    'zip': 'address_postalcode',
+            'Closed': 'done',
+            'Pending': 'pending',
+            'Rejected': 'cancel',
     }
-    address_ids = address_obj.search(cr, uid, [('name', 'like', val.get('first_name') +' '+ val.get('last_name'))])
-    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)
-        val['country_id'] =  country_id
-        val['state_id'] =  state_id
-    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
-    return True
-
-def get_address_type(sugar_obj, cr, uid, val, map_partner_address, type, context=None):
-        address_obj = sugar_obj.pool.get('res.partner.address')
-        new_address_id = False
-        if type == 'invoice':
-            type_address = 'billing'
-        else:
-            type_address = 'shipping'     
+     
+    def get_project_issue_priority(self, val):
+        priority_dict = {
+                'Urgent': '1',
+                'High': '2',
+                'Medium': '3',
+                'Low': '4'
+         }
+        return priority_dict.get(val.get('priority'), '')     
+      
+    def get_bug_project_id(self, dict, val):
+        fields = ['name']
+        data = [val]
+        return self.import_object(fields, data, 'project.project', 'project_issue', val)    
     
-        map_partner_address.update({
-            'street': type_address + '_address_street',
-            'zip': type_address +'_address_postalcode',
-            'city': type_address +'_address_city',
-             'country_id': 'country_id',
-             'type': 'type',
-            })
-        val['type'] = type
-        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)
-            val['country_id'] =  country_id
-            val['state_id'] =  state
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_partner_address)
-        #Convert To list into Dictionary(Key, val). value pair.
-        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=[]
-    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',
-            }
-        if val.get('billing_address_street'):
-            address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'invoice', context))
-            
-        if val.get('shipping_address_street'):
-            address_id.append(get_address_type(sugar_obj, cr, uid, val, map_partner_address, 'delivery', context))
-        return address_id
-    return True
-
-def import_partners(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_partner = {
-                'id': 'id',
-                'name': 'name',
-                'website': 'website',
-                'user_id/id': 'assigned_user_id',
-                'ref': 'sic_code',
-                'comment': ['__prettyprint__', 'description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'],
-                'customer': 'customer',
-                'supplier': 'supplier', 
+    def get_project_issue_mapping(self):
+        return { 
+                'model' : 'project.issue',
+                'dependencies' : [self.TABLE_USER],
+                'map' : {
+                    'name': concat('bug_number', 'name', delimiter='-'),
+                    'project_id/id': call(self.get_bug_project_id, 'sugarcrm_bugs'),
+                    'categ_id/id': call(self.get_category, 'project.issue', value('type')),
+                    'description': ppconcat('description', 'source', 'resolution', 'work_log', 'found_in_release', 'release_name', 'fixed_in_release_name', 'fixed_in_release'),
+                    'priority': self.get_project_issue_priority,
+                    'state': map_val('status', self.project_issue_state),
+                    'assigned_to/id' : ref(self.TABLE_USER, 'assigned_user_id'),
                 }
-    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)
-        val['customer'] = '1'
-        val['supplier'] = '0'
-        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', noupdate=True, context=context)
-        for address in  address_obj.browse(cr,uid,add_id):
-            data_id = partner_obj.search(cr,uid,[('name','like',address.name),('website','like',val.get('website'))])
-            if data_id:
-                address_obj.write(cr,uid,address.id,{'partner_id':data_id[0]})                
-    return True
-
-def get_category(sugar_obj, cr, uid, model, name, context=None):
-    categ_id = False
-    categ_obj = sugar_obj.pool.get('crm.case.categ')
-    categ_ids = categ_obj.search(cr, uid, [('object_id.model','=',model), ('name', 'like', name)] )
-    if categ_ids:
-        categ_id = categ_ids[0]
-    else:
-        model_ids = sugar_obj.pool.get('ir.model').search(cr, uid, [('model', '=', model)], context=context)
-        model = model_ids and model_ids[0] or False
-        categ_id = categ_obj.create(cr, uid, {'name': name, 'object_id': model})
-    return categ_id     
-
-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',
-     }
-    alarm_id = False
-    alarm_obj = sugar_obj.pool.get('res.alarm')
-    if alarm_dict.get(val):
-        alarm_ids = alarm_obj.search(cr, uid, [('name', 'like', alarm_dict.get(val))])
-        for alarm in alarm_obj.browse(cr, uid, alarm_ids, context):
-            alarm_id = alarm.id
-    return alarm_id 
+            }
     
-def get_meeting_state(sugar_obj, cr, uid, val,context=None):
-    if not context:
-        context = {}
-    state = False
-    state_dict = {'status': #field in the sugarcrm database
-        { #Mapping of sugarcrm stage : openerp meeting stage
-            'Planned' : 'draft',
-            'Held':'open',
-            'Not Held': 'draft',
-        },}
-    state = state_dict['status'].get(val, '')
-    return state    
-
-def get_task_state(sugar_obj, cr, uid, val, context=None):
-    if not context:
-        context = {}
-    state = False
-    state_dict = {'status': #field in the sugarcrm database
-        { #Mapping of sugarcrm stage : openerp meeting stage
-            'Completed' : 'done',
-            'Not Started':'draft',
+    """
+    import Project Tasks
+    """
+    project_task_state = {
+            'Not Started': 'draft',
             'In Progress': 'open',
-            'Pending Input': 'draft',
-            'deferred': 'cancel'
-        },}
-    state = state_dict['status'].get(val, '')
-    return state    
-
-def get_project_state(sugar_obj, cr, uid, val,context=None):
-    if not context:
-        context = {}
-    state = False
-    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 = False
-    state_dict = {'status': #field in the sugarcrm database
-        { #Mapping of sugarcrm status : openerp Porject Tasks state
-             'Not Started': 'draft',
-             'In Progress': 'open',
-             'Completed': 'done',
+            '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 = False
-    priority_dict = {'priority': #field in the sugarcrm database
-        { #Mapping of sugarcrm status : openerp Porject Tasks state
+     }
+    
+    def get_project_task_priority(self, val):
+        priority_dict = {
             'High': '0',
             'Medium': '2',
             'Low': '3'
-        },}
-    priority = priority_dict['priority'].get(val, '')
-    return priority    
-
-
-def get_account(sugar_obj, cr, uid, val, context=None):
-    if not context:
-        context = {}
-    partner_id = False    
-    partner_address_id = False
-    partner_phone = False
-    partner_mobile = False
-    model_obj = sugar_obj.pool.get('ir.model.data')
-    address_obj = sugar_obj.pool.get('res.partner.address')
-    crm_obj = sugar_obj.pool.get('crm.lead')
-    project_obj = sugar_obj.pool.get('project.project')
-    issue_obj = sugar_obj.pool.get('project.issue')
-    if val.get('parent_type') == 'Accounts':
-        model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_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_mobile = address_id.mobile
-            
-    if val.get('parent_type') == 'Contacts':
-        model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'res.partner.address')])
-        for model in model_obj.browse(cr, uid, model_ids):
-            partner_address_id = model.res_id
-            address_id = address_obj.browse(cr, uid, partner_address_id)
-            partner_phone = address_id.phone
-            partner_mobile = address_id.mobile
-            partner_id = address_id and address_id.partner_id or False
-            
-    if val.get('parent_type') == 'Opportunities':
-        model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'crm.lead')])
-        for model in model_obj.browse(cr, uid, model_ids):
-            opportunity_id = model.res_id
-            opportunity_id = crm_obj.browse(cr, uid, opportunity_id)
-            partner_id = opportunity_id.partner_id.id
-            partner_address_id =  opportunity_id.partner_address_id.id
-            partner_phone = opportunity_id.partner_address_id.phone
-            partner_mobile = opportunity_id.partner_address_id.mobile
-            
-    if val.get('parent_type') == 'Project':
-        model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.project')])
-        for model in model_obj.browse(cr, uid, model_ids):
-            proj_ids = model.res_id
-            proj_id = project_obj.browse(cr, uid, proj_ids)
-            partner_id = proj_id.partner_id.id
-            partner_address_id =  proj_id.partner_address_id.id
-            partner_phone = proj_id.partner_address_id.phone
-            partner_mobile = proj_id.partner_address_id.mobile
-
-    if val.get('parent_type') == 'Bugs':
-        model_ids = model_obj.search(cr, uid, [('name', '=', val.get('parent_id')), ('model', '=', 'project.issue')])
-        for model in model_obj.browse(cr, uid, model_ids):
-            issue_ids = model.res_id
-            issue_id = issue_obj.browse(cr, uid, issue_ids)
-            partner_id = issue_id.partner_id.id
-            partner_address_id =  issue_id.partner_address_id.id
-            partner_phone = issue_id.partner_address_id.phone
-            partner_mobile = issue_id.partner_address_id.mobile                        
-                        
-    return partner_id, partner_address_id, partner_phone,partner_mobile                          
-
-def import_documents(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_document = {'id' : 'id', 
-             'name': 'document_name',
-            'active_date': 'create_date',
-           'description': 'description',
-           'datas': 'datas',
-           'datas_fname': 'document_name',
-            } 
-    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, 'Documents')
-    for val in sugar_data:
-        File = sugar.attachment_search(PortType, sessionid, 'DocumentRevisions', val.get('document_revision_id'))
-        val['datas'] = File
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_document)
-        attach_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True
+        }
+        return priority_dict.get(val.get('priority'), '')
+    
+    def get_project_task_mapping(self):
+        return { 
+                'model' : 'project.task',
+                'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT],
+                'map' : {
+                    'name': 'name',
+                    'date_start': 'date_start',
+                    'date_end': 'date_finish',
+                    'project_id/id': ref(self.TABLE_PROJECT, 'project_id'),
+                    'planned_hours': 'estimated_effort',
+                    'priority': self.get_project_task_priority,
+                    'description': ppconcat('description','milestone_flag', 'project_task_id', 'task_number', 'percent_complete'),
+                    'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
+                    'partner_id/id': 'partner_id/id',
+                    'contact_id/id': 'contact_id/id',
+                    'state': map_val('status', self.project_task_state)
+                }
+            }
 
-def import_tasks(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_task = {'id' : 'id',
-                'name': 'name',
-                'date': 'date_start',
-                'date_deadline' : 'date_due',
-                'user_id/id': 'assigned_user_id',
-                'categ_id/.id': 'categ_id/.id',
-                'partner_id/.id': 'partner_id/.id',
-                'partner_address_id/.id': 'partner_address_id/.id',
-                'state': 'state'
-    }
-    meeting_obj = sugar_obj.pool.get('crm.meeting')
-    PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
-    categ_id = get_category(sugar_obj, cr, uid, 'crm.meeting', 'Tasks')
-    sugar_data = sugar.search(PortType, sessionid, 'Tasks')
-    for val in sugar_data:
-        partner_xml_id = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', val.get('contact_id'), context)
-        if not partner_xml_id:
-            raise osv.except_osv(_('Warning !'), _('Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('contact_name'))
-        partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
-        val['partner_id/.id'] = partner_id
-        val['partner_address_id/.id'] = partner_address_id
-        val['categ_id/.id'] = categ_id
-        val['state'] = get_task_state(sugar_obj, cr, uid, val.get('status'), context=None)
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_task)
-        meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True    
+    """
+    import Projects
+    """
+    project_state = {
+            'Draft' : 'draft',
+            'In Review': 'open',
+            'Published': 'close'
+     }
     
-def get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, module_name, module_id, context=None):
-    if not context:
-        context = {}
-    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)
-    for user in user_dict: 
-        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)]})       
-    return True   
+    def import_project_account(self, val):
+        partner_id = False
+        partner_invoice_id = False        
+        sugar_project_account = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Project', module_id=val.get('id'), related_module=self.TABLE_ACCOUNT, query=None, deleted=None)
+        sugar_project_contact = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Project', module_id=val.get('id'), related_module=self.TABLE_CONTACT, query=None, deleted=None)
+        for contact_id in sugar_project_contact:
+            partner_invoice_id = self.get_mapped_id(self.TABLE_CONTACT, contact_id)
+        for account_id in sugar_project_account:
+            partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, account_id)
+        return partner_id, partner_invoice_id      
+           
+    def import_project(self, val):
+        partner_id, partner_invoice_id  = self.import_project_account(val)    
+        val['partner_id/.id'] = partner_id
+        val['contact_id/.id'] = partner_invoice_id
+        return val
     
-def import_meetings(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_meeting = {'id' : 'id',
+    def get_project_mapping(self):
+        return { 
+                'model' : 'project.project',
+                'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
+                'hook' : self.import_project,
+                'map' : {
                     'name': 'name',
-                    'date': 'date_start',
-                    'duration': ['duration_hours', 'duration_minutes'],
-                    'location': 'location',
-                    'alarm_id/.id': 'alarm_id/.id',
-                    'user_id/id': 'assigned_user_id',
-                    'partner_id/.id':'partner_id/.id',
-                    'partner_address_id/.id':'partner_address_id/.id',
-                    'state': 'state'
-    }
-    meeting_obj = sugar_obj.pool.get('crm.meeting')
-    PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
-    sugar_data = sugar.search(PortType, sessionid, 'Meetings')
-    for val in sugar_data:
-        partner_id, partner_address_id, partner_phone, partner_mobile = get_account(sugar_obj, cr, uid, val, context)
-        val['partner_id/.id'] = partner_id
-        val['partner_address_id/.id'] = partner_address_id
-        val['state'] = get_meeting_state(sugar_obj, cr, uid, val.get('status'),context)
-        val['alarm_id/.id'] = get_alarm_id(sugar_obj, cr, uid, val.get('reminder_time'), context)
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_meeting)
-        meeting_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-        get_attendee_id(sugar_obj, cr, uid, PortType, sessionid, 'Meetings', val.get('id'), context)
-    return True    
+                    'date_start': 'estimated_start_date',
+                    'date': 'estimated_end_date',
+                    'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
+                    'partner_id/.id': 'partner_id/.id',
+                    'contact_id/.id': 'contact_id/.id',
+                    'state': map_val('status', self.project_state)
+                }
+            }
+    
+    """
+    import Tasks
+    """
+    task_state = {
+            'Completed' : 'done',
+            'Not Started':'draft',
+            'In Progress': 'open',
+            'Pending Input': 'draft',
+            'deferred': 'cancel'
+        }
+
+    def import_task(self, val):
+        date =  val.get('date_start') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
+        val['date'] = ''.join(date)
+        date_deadline = val.get('date_due') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
+        val['date_deadline'] = ''.join(date_deadline)
+        return val
 
-def get_calls_state(sugar_obj, cr, uid, val,context=None):
-    if not context:
-        context = {}
-    state = False
-    state_dict = {'status': #field in the sugarcrm database
-        { #Mapping of sugarcrm stage : openerp calls stage
+    def get_task_mapping(self):
+        return { 
+                'model' : 'crm.meeting',
+                'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER],
+                'hook' : self.import_task,
+                'map' : {
+                    'name': 'name',
+                    'date': 'date',
+                    'date_deadline': 'date_deadline',
+                    'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
+                    'categ_id/id': call(self.get_category, 'crm.meeting', const('Tasks')),
+                    'partner_id/id': related_ref(self.TABLE_ACCOUNT),
+                    'partner_address_id/id': ref(self.TABLE_CONTACT,'contact_id'),
+                    'state': map_val('status', self.task_state)
+                }
+            }
+       
+    """
+    import Calls
+    """  
+    #TODO adapt with project trunk-crm-imp   
+    call_state = {   
             'Planned' : 'open',
             'Held':'done',
             'Not Held': 'pending',
-        },}
-    state = state_dict['status'].get(val, '')
-    return state   
+        }
 
-def import_calls(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_calls = {'id' : 'id',
+    def get_calls_mapping(self):
+        return { 
+                'model' : 'crm.phonecall',
+                'dependencies' : [self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
+                'map' : {
                     'name': 'name',
                     'date': 'date_start',
-                    'duration': ['duration_hours', 'duration_minutes'],
-                    'user_id/id': 'assigned_user_id',
-                    'partner_id/.id': 'partner_id/.id',
-                    'partner_address_id/.id': 'partner_address_id/.id',
-                    'categ_id/.id': 'categ_id/.id',
-                   'state': 'state',
-                   'partner_phone': 'partner_phone',
-                   'partner_mobile': 'partner_mobile',
-                   'opportunity_id/id': 'opportunity_id/id',
-
-    }
-    phonecall_obj = sugar_obj.pool.get('crm.phonecall')
-    PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', ''), context.get('url',''))
-    sugar_data = sugar.search(PortType, sessionid, 'Calls')
-    for val in sugar_data:
-        sugar_call_leads = sugar.relation_search(PortType, sessionid, 'Calls', module_id=val.get('id'), related_module='Leads', query=None, deleted=None)
-        if sugar_call_leads:
-            for call_opportunity in sugar_call_leads: 
-                val['opportunity_id/id'] = call_opportunity 
-        categ_id = get_category(sugar_obj, cr, uid, 'crm.phonecall', val.get('direction'))         
-        val['categ_id/.id'] = categ_id
-        partner_id, partner_address_id, partner_phone, partner_mobile = get_account(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['partner_mobile'] = partner_mobile
-        val['state'] =  get_calls_state(sugar_obj, cr, uid, val.get('status'), context)  
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_calls)
-        phonecall_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True
+                    'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
+                    '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),
+                    'categ_id/id': call(self.get_category, 'crm.phonecall', value('direction')),
+                    'opportunity_id/id': related_ref(self.TABLE_OPPORTUNITY),
+                    'description': ppconcat('description'),   
+                    'state': map_val('status', self.call_state)                      
+                }
+            }       
+         
+    """
+        import meeting
+    """
+    meeting_state = {
+            'Planned' : 'draft',
+            'Held': 'open',
+            'Not Held': 'draft', 
+        }
+#TODO    
+    def get_attendee_id(self, cr, uid, module_name, module_id):
+        contact_id = False
+        user_id = False
+        attendee_id= []
+        attendee_dict = sugar.user_get_attendee_list(self.context.get('port'), self.context.get('session_id'), module_name, module_id)
+        for attendee in attendee_dict:
+            user_id = self.xml_id_exist(self.TABLE_USER, attendee.get('id', False))
+            contact_id = False
+            if not user_id:
+                contact_id = self.xml_id_exist(self.TABLE_CONTACT, attendee.get('id', False))
+            fields = ['user_id/id', 'email', 'partner_address_id/id']
+            data = [user_id, attendee.get('email1'), contact_id]
+            attendee_xml_id = self.import_object(fields, data, 'calendar.attendee', self.TABLE_ATTENDEE, user_id or contact_id or attendee.get('email1'), ['|',('user_id', '=', attendee.get('id')),('partner_address_id','=',attendee.get('id')),('email', '=', attendee.get('email1'))])
+            attendee_id.append(attendee_xml_id)
+        return ','.join(attendee_id) 
     
-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', noupdate=True, context=context)
-    return True    
-
-def get_bug_priority(sugar_obj, cr, uid, val,context=None):
-    if not context:
-        context = {}
-    priority = False
-    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 = False
-    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 = False
-    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 = False
-    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_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)
     
-def get_issue_related_project(sugar_obj,cr,uid, PortType, sessionid, val, context=None):
-    if not context:
-        context={}
-    project_id = False        
-    model_obj = sugar_obj.pool.get('ir.model.data')
-    project_obj = sugar_obj.pool.get('project.project')
-    sugar_bug_project = sugar.relation_search(PortType, sessionid, 'Bugs', module_id=val.get('id'), related_module='Project', query=None, deleted=None)
-    for project_id in sugar_bug_project:
-        model_ids = find_mapped_id(sugar_obj, cr, uid, 'project.project', project_id, context)
-        if model_ids:
-            model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
-            project_id = project_obj.browse(cr, uid, model_id).id
-    return project_id     
-
-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_mobile = address_id.email
-    return partner_id, partner_address_id, partner_phone,partner_email
+    #TODO attendees
 
-def import_claims(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_claim = {'id' : 'id',
-                    'name': 'name',
-                    'date': '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_mapping.sugarcrm_fields_mapp(val, map_claim)
-        claim_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True    
+    def import_meeting(self, val):
+        attendee_id = self.get_attendee_id(self.cr, self.uid, 'Meetings', val.get('id')) #TODO
+        val['attendee_ids/id'] = attendee_id
+        return val
 
-def import_bug(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_resource = {'id' : 'id',
+    def get_meeting_mapping(self):
+        return { 
+                'model' : 'crm.meeting',
+                'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD, self.TABLE_TASK],
+                'hook': self.import_meeting,
+                'map' : {
                     'name': 'name',
-                    'project_id/.id':'project_id/.id',
-                    'categ_id.id': 'categ_id.id',
-                    'priority':'priority',
-                    'description': 'description',
-                    'state': 'state',
-    }
-    issue_obj = sugar_obj.pool.get('project.issue')
-    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:
-        val['project_id/.id'] = get_issue_related_project(sugar_obj,cr,uid, PortType, sessionid, val, context)
-        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_mapping.sugarcrm_fields_mapp(val, map_resource)
-        issue_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True    
-
-def get_job_id(sugar_obj, cr, uid, val, context=None):
-    if not context:
-        context={}
-    job_id = False    
-    job_obj = sugar_obj.pool.get('hr.job')        
-    job_ids = job_obj.search(cr, uid, [('name', '=', val)])
-    if job_ids:
-        job_id = job_ids[0]
-    else:
-        job_id = job_obj.create(cr, uid, {'name': val})
-    return job_id
+                    'date': 'date_start',
+                    'duration': call(self.get_float_time, value('duration_hours'), value('duration_minutes')),
+                    'location': 'location',
+                    'attendee_ids/id':'attendee_ids/id',
+                    '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)
+                }
+            }
+    
+    """
+        import Opportunity
+    """
+    opp_state = {
+            'Need Analysis' : 'New',
+            'Closed Lost': 'Lost',
+            'Closed Won': 'Won', 
+            'Value Proposition': 'Proposition',
+            'Negotiation/Review': 'Negotiation'
+        }
+        
+    def get_opportunity_status(self, sugar_val):
+        fields = ['name', 'case_default']
+        name = 'Opportunity_' + sugar_val['sales_stage']
+        data = [sugar_val['sales_stage'], '1']
+        return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('name', 'ilike', sugar_val['sales_stage'])])
+    
+    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))
+            
+        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
 
-def get_campaign_id(sugar_obj, cr, uid, val, context=None):
-    if not context:
-        context={}
-    cam_id = False    
-    cam_obj = sugar_obj.pool.get('crm.case.resource.type')        
-    cam_ids = cam_obj.search(cr, uid, [('name', '=', val)])
-    if cam_ids:
-        cam_id = cam_ids[0]
-    else:
-        cam_id = cam_obj.create(cr, uid, {'name': val})
-    return cam_id
+    def import_opp(self, val):    
+        partner_contact_id, partner_contact_email = self.import_opportunity_contact(val)
+        val['partner_address_id/id'] = partner_contact_id
+        val['email_from'] = partner_contact_email
+        return val
+    
+    def get_opp_mapping(self):
+        return {
+            'model' : 'crm.lead',
+            'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT,self.TABLE_COMPAIGN],
+            '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': map_val('status', self.opp_state),
+                'description' : 'description',
+            }
+        }
+        
+    """
+    import campaign
+    """
     
-def get_attachment(sugar_obj, cr, uid, val, model, File, 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')
-    new_attachment_id = attachment_obj.create(cr, uid, {'name': val.get('name'), 'datas': File, 'res_id': val['res_id'],'res_model': val['model']})
-    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:
-        mailgate_obj.write(cr, uid, [message_xml_id[0].res_id], {'attachment_ids': [(4, new_attachment_id)]})             
-    return True    
+    def get_compaign_mapping(self):
+        return {
+            'model' : 'crm.case.resource.type',
+            'map' : {
+                'name': 'name',
+                } 
+        }    
+        
+    """
+        import lead
+    """
+    def get_lead_status(self, sugar_val):
+        fields = ['name', 'case_default']
+        name = 'lead_' + sugar_val.get('status', '')
+        data = [sugar_val.get('status', ''), '1']
+        return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('name', 'ilike', sugar_val.get('status', ''))])
     
-def import_history(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_attachment = {'id' : 'id',
-                      'name':'name',
-                      'date':'date_entered',
-                      'user_id/id': 'assigned_user_id',
-                      'description': ['description', 'description_html'],
-                      'res_id': 'res_id',
-                      'model': 'model',
-                      'partner_id.id' : 'partner_id.id',
+    lead_state = {
+        'New' : 'draft',
+        'Assigned':'open',
+        'In Progress': 'open',
+        'Recycled': 'cancel',
+        'Dead': 'done',
+        'Converted': 'done',
     }
-    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 = sugar.attachment_search(PortType, sessionid, 'Notes', val.get('id'))
-        model_ids = model_obj.search(cr, uid, [('name', 'like', val.get('parent_id'))])
-        for model in model_obj.browse(cr, uid, model_ids):
-            val['res_id'] = model.res_id
-            val['model'] = model.model
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_attachment)   
-        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, context)
-    return True       
     
-def import_employees(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_employee = {'id' : 'user_hash',
-                    'resource_id/.id': 'resource_id/.id',
-                    'name': ['first_name', 'last_name'],
-                    'work_phone': 'phone_work',
-                    'mobile_phone':  'phone_mobile',
-                    'user_id/name': ['first_name', 'last_name'], 
-                    'address_home_id/.id': 'address_home_id/.id',
-                    'notes': 'description',
-                    #TODO: Creation of Employee create problem.
-                 #   'coach_id/id': 'reports_to_id',
-                    'job_id/.id': 'job_id/.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
-        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
-        val['job_id/.id'] = get_job_id(sugar_obj, cr, uid, val.get('title'), context)
-        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', noupdate=True, context=context)
-    return True
-
-def get_contact_title(sugar_obj, cr, uid, salutation, domain, context=None):
-    if not context:
-        context = {}
-    contact_title_obj = sugar_obj.pool.get('res.partner.title')
-    title_id = False            
-    title_ids = contact_title_obj.search(cr, uid, [('shortcut', '=', salutation), ('domain', '=', domain)])
-    if title_ids:
-         title_id = title_ids[0]
-    elif salutation:
-         title_id = contact_title_obj.create(cr, uid, {'name': salutation, 'shortcut': salutation, 'domain': domain})
-    return title_id
+    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 import_emails(sugar_obj, cr, uid, context=None):
-    if not context:
-        context= {}
-    map_emails = {'id': 'id',
-    'name':'name',
-    'date':'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': ['description', 'description_html'],
-    'res_id': 'res_id',
-    'model': 'model',
-    }
-    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'))])
-        for model in model_obj.browse(cr, uid, model_ids):
-            val['res_id'] = model.res_id
-            val['model'] = model.model
-        fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_emails)
-        mailgate_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True    
+    def get_lead_mapping(self):
+        return {
+            'model' : 'crm.lead',
+            'dependencies' : [self.TABLE_COMPAIGN, self.TABLE_USER],
+            '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',
+                'opt_out': 'do_not_call',
+                'channel_id/id': call(self.get_channel_id, value('lead_source')),
+                'type_id/id': ref(self.TABLE_COMPAIGN, 'campaign_id'),
+                'country_id/id': 'country_id/id',
+                'state_id/id': 'state_id/id',
+                } 
+        }
     
-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      
+    """
+        import contact
+    """
     
-def import_projects(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_project = {'id': 'id',
-        'name': 'name',
-        'date_start': 'estimated_start_date',
-        'date': 'estimated_end_date',
-        'user_id/id': 'assigned_user_id',
-        'partner_id/.id': 'partner_id/.id',
-        'contact_id/.id': 'contact_id/.id', 
-         'state': 'state'   
-    }
-    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_mapping.sugarcrm_fields_mapp(val, map_project)
-        project_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True 
-
+    def get_email(self, val):
+        email_address = sugar.get_contact_by_email(self.context.get('port'), self.context.get('username'), self.context.get('password'), val.get('email1'))
+        if email_address:
+            return ','.join(email_address)     
+    
+    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:
+            type_address = 'shipping' 
+            
+        if type == 'default':
+            map_partner_address = {
+                'name': 'name',
+                'type': const('default'),
+                'email': 'email1' 
+            }
+        else:        
+            map_partner_address = {
+                'name': 'name',
+                'phone': 'phone_office',
+                'mobile': 'phone_mobile',
+                'fax': 'phone_fax',
+                'type': 'type',
+                'street': type_address + '_address_street',
+                'zip': type_address +'_address_postalcode',
+                'city': type_address +'_address_city',
+                 'country_id/id': 'country_id/id',
+                 'type': 'type',
+                }
+            
+        if val.get(type_address +'_address_country'):
+            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 self.import_object_mapping(map_partner_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id_new'], self.DO_NOT_FIND_DOMAIN) 
+        
+    def get_partner_address(self, val):
+        address_id=[]
+        type_dict = {'billing_address_street' : 'invoice', 'shipping_address_street' : 'delivery', 'type': 'default'}
+        for key, type_value in type_dict.items():
+            if val.get(key):
+                id = self.get_address_type(val, type_value)
+                address_id.append(id)
+          
+        return ','.join(address_id)
+    
+    def get_partner_mapping(self):
+        return {
+                'model' : 'res.partner',
+                'dependencies' : [self.TABLE_USER],
+                'map' : {
+                    'name': 'name',
+                    '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,
+                }
+        }
 
-def import_project_tasks(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_project_task = {'id': 'id',
-        'name': 'name',
-        'date_start': 'date_start',
-        'date_end': '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_mapping.sugarcrm_fields_mapp(val, map_project_task)
-        task_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', noupdate=True, context=context)
-    return True 
+    """
+        import Employee
+    """
+    def get_ressource(self, val):
+        map_resource = { 
+            'name': concat('first_name', 'last_name'),
+        }        
+        return self.import_object_mapping(map_resource, val, 'resource.resource', self.TABLE_RESSOURCE, val['id'], self.DO_NOT_FIND_DOMAIN)
     
-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', 'refered_by', 'lead_source', 'lead_source_description', 'website', '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' : 'assigned_user_id',
-            'stage_id.id' : 'stage_id.id',
-            'type' : 'type',
-            'state': 'state',
+    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': concat('first_name', 'last_name'),
+            'city': 'address_city',
+            'country_id/id': 'country_id/id',
+            'state_id/id': 'state_id/id',
+            'street': 'address_street',
+            'zip': 'address_postalcode',
             'fax': 'phone_fax',
-            'referred': 'refered_by',
-            'optout': 'optout',
-            'type_id/.id': 'type_id/.id'
-            }
+            'phone': 'phone_work',
+            'mobile':'phone_mobile',
+            'email': 'email1'
+        }
         
-    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:
-        if val.get('do_not_call') == '0':
-            val['optout'] = '1'
-        if val.get('opportunity_id'):
-            continue
-        title_id = get_contact_title(sugar_obj, cr, uid, val.get('salutation'), 'contact', context)
-        val['title.id'] = title_id
-        val['type'] = 'lead'
-        val['type_id/.id'] = get_campaign_id(sugar_obj, cr, uid, val.get('lead_source'), context)
-        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', noupdate=True, context=context)
-    return True
+        if val.get('address_country'):
+            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 self.import_object_mapping(map_user_address, val, 'res.partner.address', self.TABLE_CONTACT, val['id'], self.DO_NOT_FIND_DOMAIN)
 
-def get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context=None):
-    if not context:
-        context={}
-    partner_contact_name = False        
-    model_obj = sugar_obj.pool.get('ir.model.data')
-    partner_address_obj = sugar_obj.pool.get('res.partner.address')
-    model_account_ids = model_obj.search(cr, uid, [('res_id', '=', partner_xml_id[0]), ('model', '=', 'res.partner'), ('module', '=', 'sugarcrm_import')])
-    model_xml_id = model_obj.browse(cr, uid, model_account_ids)[0].name 
-    sugar_account_contact = set(sugar.relation_search(PortType, sessionid, 'Accounts', module_id=model_xml_id, related_module='Contacts', query=None, deleted=None))
-    sugar_opportunities_contact = set(sugar.relation_search(PortType, sessionid, 'Opportunities', module_id=val.get('id'), related_module='Contacts', query=None, deleted=None))
-    sugar_contact = list(sugar_account_contact.intersection(sugar_opportunities_contact))
-    if sugar_contact: 
-        for contact in sugar_contact:
-            model_ids = find_mapped_id(sugar_obj, cr, uid, 'res.partner.address', contact, context)
-            if model_ids:
-                model_id = model_obj.browse(cr, uid, model_ids)[0].res_id
-                address_id = partner_address_obj.browse(cr, uid, model_id)
-                partner_address_obj.write(cr, uid, [address_id.id], {'partner_id': partner_xml_id[0]})
-                partner_contact_name = address_id.name
-            else:
-                partner_contact_name = val.get('account_name')    
-    return partner_contact_name 
+    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': ppconcat('messenger_type', 'messenger_id', 'description'),
+                'job_id/id': self.get_job_id,
+                'work_email' : 'email1',
+                'coach_id/id_parent' : 'reports_to_id',
+            }
+     }
+    
+    """
+        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
+            
+        val['context_lang'] = self.context.get('lang','en_US')
+        return val
+    
+    def get_users_department(self, val):
+        dep = val.get('department')
+        fields = ['name']
+        data = [dep]
+        if not dep:
+            return False
+        return self.import_object(fields, data, 'hr.department', 'hr_department_user', dep)
 
-def import_opportunities(sugar_obj, cr, uid, context=None):
-    if not context:
-        context = {}
-    map_opportunity = {'id' : 'id',
-        'name': 'name',
-        'probability': 'probability',
-        'partner_id/name': 'account_name',
-        'title_action': 'next_step',
-        'partner_address_id/name': 'partner_address_id/name',
-        'planned_revenue': 'amount',
-        'date_deadline':'date_closed',
-        'user_id/id' : 'assigned_user_id',
-        'stage_id.id' : 'stage_id.id',
-        'type' : 'type',
-        'categ_id.id': 'categ_id.id'
-    }
-    lead_obj = sugar_obj.pool.get('crm.lead')
-    partner_obj = sugar_obj.pool.get('res.partner')
-    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:
-        partner_xml_id = partner_obj.search(cr, uid, [('name', 'like', val.get('account_name'))])
-        if not partner_xml_id:
-            raise osv.except_osv(_('Warning !'), _('Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration.') % val.get('account_name'))
-        partner_contact_name = get_opportunity_contact(sugar_obj,cr,uid, PortType, sessionid, val, partner_xml_id, context)
-        val['partner_address_id/name'] = partner_contact_name
-        val['categ_id.id'] = get_category(sugar_obj, cr, uid, 'crm.lead', val.get('opportunity_type'))                    
-        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', noupdate=True, context=context)
-    return True
+    def get_user_mapping(self):
+        return {
+            'model' : 'res.users',
+            'hook' : self.import_user,
+            'map' : { 
+                'name': concat('first_name', 'last_name'),
+                'login': value('user_name', fallback='last_name'),
+                'context_lang' : 'context_lang',
+                'password' : 'password',
+                '.id' : '.id',
+                'context_department_id/id': self.get_users_department,
+                'user_email' : 'email1',
+            }
+        }
+
+
+    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(),
+            self.TABLE_CALL : self.get_calls_mapping(),
+            self.TABLE_TASK : self.get_task_mapping(),
+            self.TABLE_PROJECT : self.get_project_mapping(),
+            self.TABLE_PROJECT_TASK: self.get_project_task_mapping(),
+            self.TABLE_BUG: self.get_project_issue_mapping(),
+            self.TABLE_CASE: self.get_crm_claim_mapping(),
+            self.TABLE_NOTE: self.get_history_mapping(),
+            self.TABLE_EMAIL: self.get_email_mapping(),
+            self.TABLE_DOCUMENT: self.get_document_mapping(),
+            self.TABLE_COMPAIGN: self.get_compaign_mapping()
+        }
+        
+    """
+        Email notification
+    """   
+    def get_email_subject(self, result, error=False):
+        if error:
+            return "Sugarcrm data import failed at %s due to an unexpected error" % self.date_ended
+        return "your sugarcrm data were successfully imported at %s" % self.date_ended 
+    
+    def get_body_header(self, result):
+        return "Sugarcrm import : report of last import" 
 
-MAP_FIELDS = {'Opportunities':  #Object Mapping name
-                    {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],  #Object to import before this table
-                     'process' : import_opportunities,
-                     },
-              'Leads':
-                    {'dependencies' : ['Users', 'Accounts', 'Contacts'],  #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', 'Contacts'],  #Object to import before this table
-                     'process' : import_partners,
-                    },
-              'Users': 
-                    {'dependencies' : [],
-                     'process' : import_users,
-                    },
-              'Documents': 
-                    {'dependencies' : ['Users'],
-                     'process' : import_documents,
-                    },                      
-              'Meetings': 
-                    {'dependencies' : ['Users', 'Tasks'],
-                     'process' : import_meetings,
-                    },        
-              'Tasks': 
-                    {'dependencies' : ['Users', 'Accounts', 'Contacts'],
-                     'process' : import_tasks,
-                    },  
-              'Calls': 
-                    {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
-                     'process' : import_calls,
-                    }, 
-              'Claims': 
-                    {'dependencies' : ['Users', 'Accounts', 'Contacts', 'Leads'],
-                     'process' : import_claims,
-                    },
-              'Employees': 
-                    {'dependencies' : ['Resources'],
-                     'process' : import_employees,
-                    },
-              'Emails': 
-                    {'dependencies' : ['Users'],
-                     'process' : import_emails,
-                    },    
-              '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,
-                    },   
-              'Notes': 
-                    {'dependencies' : ['Users', 'Projects', 'Project Tasks', 'Accounts', 'Contacts', 'Leads', 'Opportunities', 'Meetings', 'Calls'],
-                     'process' : import_history,
-                    },                    
-              'Resources': 
-                    {'dependencies' : ['Users'],
-                     'process' : import_resources,
-                    },                                      
-          }
 
 class import_sugarcrm(osv.osv):
     """Import SugarCRM DATA"""
@@ -1159,91 +943,197 @@ class import_sugarcrm(osv.osv):
     _name = "import.sugarcrm"
     _description = __doc__
     _columns = {
-        'opportunity': fields.boolean('Leads and Opportunities', help="If Opportunities are checked, SugarCRM opportunities data imported in OpenERP crm-Opportunity form"),
-        'user': fields.boolean('Users', help="If Users  are checked, SugarCRM Users data imported in OpenERP Users form"),
-        'contact': fields.boolean('Contacts', help="If Contacts are checked, SugarCRM Contacts data imported in OpenERP partner address form"),
-        'account': fields.boolean('Accounts', help="If Accounts are checked, SugarCRM  Accounts data imported in OpenERP partners form"),
-        'employee': fields.boolean('Employee', help="If Employees is checked, SugarCRM Employees data imported in OpenERP employees form"),
-        'meeting': fields.boolean('Meetings', help="If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form"),
-        'call': fields.boolean('Calls', help="If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form"),
-        'claim': fields.boolean('Claims', help="If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form"),
-        'email': fields.boolean('Emails', help="If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form"),
-        'project': fields.boolean('Projects', help="If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form"),
-        'project_task': fields.boolean('Project Tasks', help="If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form"),
-        'task': fields.boolean('Tasks', help="If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings form"),
-        'bug': fields.boolean('Bugs', help="If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form"),
-        'attachment': fields.boolean('Attachments', help="If Attachments is checked, SugarCRM Notes data imported in OpenERP's Related module's History with attachment"),
-         'document': fields.boolean('Documents', help="If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form"),
-        'username': fields.char('User Name', size=64),
-        'password': fields.char('Password', size=24),
+        'username': fields.char('User Name', size=64, required=True),
+        'password': fields.char('Password', size=24,required=True),
+        'url' : fields.char('SugarSoap Api url:', size=264, required=True, help="Webservice's url where to get the data.\
+                      example : 'http://example.com/sugarcrm/soap.php', or copy the address of your sugarcrm application http://trial.sugarcrm.com/qbquyj4802/index.php?module=Home&action=index"),
+        'user' : fields.boolean('User', help="Check this box to import sugarCRM Users into OpenERP users, warning if a user with the same login exist in OpenERP, user information will be erase by sugarCRM user information", readonly=True),
+        'opportunity': fields.boolean('Leads & Opp', help="Check this box to import sugarCRM Leads and Opportunities into OpenERP Leads and Opportunities"),
+        'contact': fields.boolean('Contacts', help="Check this box to import sugarCRM Contacts into OpenERP addresses"),
+        'account': fields.boolean('Accounts', help="Check this box to import sugarCRM Accounts into OpenERP partners"),
+        'employee': fields.boolean('Employee', help="Check this box to import sugarCRM Employees into OpenERP employees"),
+        'meeting': fields.boolean('Meetings', help="Check this box to import sugarCRM Meetings and Tasks into OpenERP meetings"),
+        'call': fields.boolean('Calls', help="Check this box to import sugarCRM Calls into OpenERP calls"),
+        'claim': fields.boolean('Cases', help="Check this box to import sugarCRM Cases into OpenERP claims"),
+        'email_history': fields.boolean('Email and Note',help="Check this box to import sugarCRM Emails, Notes and Attachments into OpenERP Messages and Attachments"),
+        'project': fields.boolean('Projects', help="Check this box to import sugarCRM Projects into OpenERP projects"),
+        'project_task': fields.boolean('Project Tasks', help="Check this box to import sugarCRM Project Tasks into OpenERP tasks"),
+        'bug': fields.boolean('Bugs', help="Check this box to import sugarCRM Bugs into OpenERP project issues"),
+        'document': fields.boolean('Documents', help="Check this box to import sugarCRM Documents into OpenERP documents"),
+        'email_from': fields.char('Notify End Of Import To:', size=128),
+        'instance_name': fields.char("Instance's Name", size=64, help="Prefix of SugarCRM id to differentiate xml_id of SugarCRM models datas come from different server."),
     }
+    
+    def _get_email_id(self, cr, uid, context=None):
+        return self.pool.get('res.users').browse(cr, uid, uid, context=context).user_email
+    
+    def _module_installed(self, cr, uid, model, context=None):
+        module_id = self.pool.get('ir.module.module').search(cr, uid, [('name', '=', model), ('state', "=", "installed")], context=context)
+        return bool(module_id)
+                
+    def _project_installed(self, cr, uid, context=None):
+        return self._module_installed(cr,uid,'project',context=context)
+                        
+    def _crm_claim_installed(self, cr, uid, context=None):
+        return self._module_installed(cr,uid,'crm_claim',context=context)
+                    
+    def _project_issue_installed(self, cr, uid, context=None):
+        return self._module_installed(cr,uid,'project_issue',context=context)
+    
+    def _hr_installed(self, cr, uid, context=None):
+        return self._module_installed(cr,uid,'hr',context=context)
+    
     _defaults = {#to be set to true, but easier for debugging
-       'opportunity': True,
        'user' : True,
+       'opportunity': True,
        'contact' : True,
        'account' : True,
-        'employee' : True,
+        'employee' : _hr_installed,
         'meeting' : True,
-        'task' : True,
         'call' : True,
-        'claim' : True,    
-        'email' : True, 
-        'project' : True,   
-        'project_task': True,     
-        'bug': True,
-        'document': True
+        'claim' : _crm_claim_installed,    
+        'email_history' : True, 
+        'project' : _project_installed,   
+        'project_task': _project_installed,     
+        'bug': _project_issue_installed,
+        'document': True,
+        'instance_name': 'sugarcrm',
+        'email_from': _get_email_id,
+        'username' : 'admin',
+        'password' : '',
+        'url':  "http://sugarcrm.example.com/soap.php"
     }
     
+    def check_url(self, url, context):
+        if not context:
+            context = {}
+        user = context.get('username')
+        password = context.get('password')
+        
+        try :
+            sugar.login(user, password, url)
+            return True
+        except Exception:
+            return False
+                
+        
+    def parse_valid_url(self, context=None):
+        if not context:
+            context = {}
+        url = context.get('url')
+        url_split = str(url).split('/')
+        while len(url_split) >= 3:
+            #3 case, soap.php is already at the end of url should be valid
+            #url end by / or not
+            if url_split[-1] == 'soap.php':
+                url = "/".join(url_split)
+            elif url_split[-1] == '':
+                url = "/".join(url_split) + "soap.php"
+            else:
+                url = "/".join(url_split) + "/soap.php"
+            
+            if self.check_url(url, context):
+                return url
+            else: 
+                url_split = url_split[:-1] #take parent folder
+        return url
+            
     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 = []
+        module = {}
         for current in self.browse(cr, uid, ids, context):
-            if current.opportunity:
-                key_list.append('Opportunities')
+            context.update({'username': current.username, 'password': current.password, 'url': current.url, 'email_user': current.email_from or False, 'instance_name': current.instance_name or False})
             if current.user:
                 key_list.append('Users')
             if current.contact:
                 key_list.append('Contacts')
             if current.account:
-                key_list.append('Accounts') 
+                key_list.append('Accounts')  
+            if current.opportunity:
+                key_list.append('Leads')
+                key_list.append('Opportunities')
             if current.employee:
-                key_list.append('Employees')  
+                key_list.append('Employees')
+                module.update({'Employees':'hr'}) 
             if current.meeting:
                 key_list.append('Meetings')
-            if current.task:
-                key_list.append('Tasks')
             if current.call:
                 key_list.append('Calls')
             if current.claim:
-                key_list.append('Claims')                
-            if current.email:
+                key_list.append('Cases')  
+                module.update({'Cases':'crm_claim'})
+            if current.email_history:
                 key_list.append('Emails') 
+                key_list.append('Notes') 
             if current.project:
-                key_list.append('Projects')
+                key_list.append('Project')
+                module.update({'Project':'project'})
             if current.project_task:
-                key_list.append('Project Tasks')
+                key_list.append('ProjectTask')
+                module.update({'ProjectTask':'project'})
             if current.bug:
                 key_list.append('Bugs')
-            if current.attachment:
-                key_list.append('Notes')     
+                module.update({'Bugs':'project_issue'})
             if current.document:
-                key_list.append('Documents')                                                  
-        return key_list
+                key_list.append('Documents')
+        return key_list,module
 
-    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)
 
+    def do_import_all(self, cr, uid, *args):
+        """
+        scheduler Method
+        """
+        context = {'username': args[4], 'password': args[5], 'url': args[3], 'instance_name': args[3]}
+        imp = sugar_import(self, cr, uid, args[2], "import_sugarcrm", args[1], context)
+        imp.set_table_list(args[0])
+        imp.start()
+        return True 
+
+    def import_from_scheduler_all(self, cr, uid, ids, context=None):
+        keys, module_list = self.get_key(cr, uid, ids, context)
+        if not keys:
+            raise osv.except_osv(_('Warning !'), _('Select Module to Import.'))
+        key_list = module_list.keys()
+        for module in key_list :
+            module = module_list[module]
+            state = self.get_all(cr,uid,module,context=context)
+            if state == False:
+                keys =  ', '.join(key_list)
+                raise osv.except_osv(_('Error !!'), _("%s data required %s Module to be installed, Please install %s module") %(keys,module,module))
+        cron_obj = self.pool.get('ir.cron')
+        url = self.parse_valid_url(context)
+        args = (keys,context.get('email_user'), context.get('instance_name'), url, context.get('username'), context.get('password') )
+        new_create_id = cron_obj.create(cr, uid, {'name': 'Import SugarCRM datas','interval_type': 'hours','interval_number': 1, 'numbercall': -1,'model': 'import.sugarcrm','function': 'do_import_all', 'args': args, 'active': False})
+        return {
+            'name': 'SugarCRM Scheduler',
+            'view_type': 'form',
+            'view_mode': 'form,tree',
+            'res_model': 'ir.cron',
+            'res_id': new_create_id,
+            'type': 'ir.actions.act_window',
+        }
+        
+    def import_all(self, cr, uid, ids, context=None):
+        
+#        """Import all sugarcrm data into openerp module"""
+        keys, module_list = self.get_key(cr, uid, ids, context)
+        if not keys:
+            raise osv.except_osv(_('Warning !'), _('Select Module to Import.'))
+        key_list = module_list.keys()
+        for module in key_list :
+            module = module_list[module]
+            state = self._module_installed(cr,uid,module,context=context)
+            if state == False:
+                keys =  ', '.join(key_list)
+                raise osv.except_osv(_('Error !!'), _("%s data required %s Module to be installed, Please install %s module") %(keys,module,module))
+        url = self.parse_valid_url(context)
+        context.update({'url': url})
+        imp = sugar_import(self, cr, uid, context.get('instance_name'), "import_sugarcrm", context.get('email_user'), context)
+        imp.set_table_list(keys)
+        imp.start()
         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'])
@@ -1255,13 +1145,7 @@ 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)
-        return True        
-
+        
 import_sugarcrm()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: