Launchpad automatic translations update.
[odoo/odoo.git] / addons / import_sugarcrm / import_sugarcrm.py
index f189a68..3ff14bc 100644 (file)
@@ -27,8 +27,16 @@ from datetime import datetime
 import base64
 import pprint
 pp = pprint.PrettyPrinter(indent=4)
-
 #copy old import here
+
+import htmllib
+
+def unescape_htmlentities(s):
+    p = htmllib.HTMLParser(None)
+    p.save_bgn()
+    p.feed(s)
+    return p.save_end()
+
 class related_ref(dbmapper):
     def __init__(self, type):
         self.type = type
@@ -39,6 +47,7 @@ class related_ref(dbmapper):
         return ''
 
 class sugar_import(import_framework):
+    URL = False   
     TABLE_CONTACT = 'Contacts'
     TABLE_ACCOUNT = 'Accounts'
     TABLE_USER = 'Users'
@@ -57,25 +66,37 @@ class sugar_import(import_framework):
     TABLE_CASE = 'Cases'
     TABLE_NOTE = 'Notes'
     TABLE_EMAIL = 'Emails'
-    TABLE_DOCUMENT = 'DocumentRevisions'
     TABLE_COMPAIGN = 'Campaigns'
+    TABLE_DOCUMENT = 'Documents'
     TABLE_HISTORY_ATTACHMNET = 'history_attachment'
     
+    MAX_RESULT_PER_PAGE = 200
+    
     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 !'))
+            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):
-        
-        return sugar.search(self.context.get('port'), self.context.get('session_id'), 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)
+
     """
     Common import method
     """
-
     def get_category(self, val, model, name):
         fields = ['name', 'object_id']
         data = [name, model]
@@ -88,9 +109,11 @@ class sugar_import(import_framework):
             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, 'res.partner.canal', 'crm_channel', 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"""
@@ -109,30 +132,44 @@ class sugar_import(import_framework):
         min = int(min) * 100 / 60
         return "%s.%i" % (hour, min)
     
-    def get_attachment(self, val):
-        File, Filename = sugar.attachment_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_NOTE, val.get('id')) 
-        attach_xml_id = False
-        attachment_obj = self.obj.pool.get('ir.attachment')
-        model_obj = self.obj.pool.get('ir.model.data')
-        mailgate_obj = self.obj.pool.get('mailgate.message')
-        if File:
-            fields = ['name', 'datas', 'datas_fname','res_id', 'res_model']
-            name = 'attachment_'+ (Filename or val.get('name'))
-            datas = [Filename or val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)]
-            attach_xml_id = self.import_object(fields, datas, 'ir.attachment', self.TABLE_HISTORY_ATTACHMNET, name, [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))])
-        return attach_xml_id    
-    
     """
     import Documents
     """
-    #THIS IS A JOKE ? NOT FUNNY
+    
+    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):
-        filepath = '/home/openerp/Public/sugarcrm/cache/upload/'+ val.get('id')
-        f = open(filepath, "r")
-        datas = f.read()
-        f.close()
-        val['datas'] = base64.encodestring(datas)
-        val['datas_fname'] = val.get('filename')
+        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): 
@@ -140,84 +177,96 @@ class sugar_import(import_framework):
                 'model' : 'ir.attachment',
                 'dependencies' : [self.TABLE_USER],
                 'hook' : self.import_document,
-                'map' : {'name':'filename',
+                'map' : {
+                         'name':'document_name',
                          'description': ppconcat('description'),
                          'datas': 'datas',
                          'datas_fname': 'datas_fname',
+                         'res_model': 'res_model',
+                         'res_id': 'res_id',
                 }
             }     
+        
     
     """
     import Emails
     """
-    
+
+      
     def import_email(self, val):
-        print 'import email'
+        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')
-        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:
-            print 'model_id', xml_id, 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
+        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' : 'mailgate.message',
-                'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD, self.TABLE_OPPORTUNITY, self.TABLE_MEETING, self.TABLE_CALL],
+                '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' : {'name':'name',
+                'map' : {
+                        'subject':'name',
+                        'state' : const('received'),
                         'date':'date_sent',
                         'email_from': 'from_addr_name',
-                        'email_to': 'reply_to_addr',
+                        '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',                         
-                        'attachment_ids/id': self.get_attachment,
                         'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
-                        'description': ppconcat('description', 'description_html'),
+                        'body_text': 'description',
+                        'body_html' : 'description_html',
+                        
                 }
             } 
     
     """
     import History(Notes)
     """
+    
 
     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
-              else:    
-                    val['res_id'] = model.res_id
-                    val['model'] = model.model
+            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' : 'mailgate.message',
-                'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD, self.TABLE_OPPORTUNITY, self.TABLE_MEETING, self.TABLE_CALL],
+                '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',
-                      'date': 'date_entered',
-                      'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
+                      'user_id/id': ref(self.TABLE_USER, 'created_by'),
                       'description': ppconcat('description', 'description_html'),
                       'res_id': 'res_id',
-                      'model': 'model',
-                      'attachment_ids/id': self.get_attachment,
+                      'res_model': 'model',
                       'partner_id/.id' : 'partner_id/.id',
+                      'datas' : 'datas',
+                      'datas_fname' : 'datas_fname'
                 }
             }     
     
@@ -226,9 +275,9 @@ class sugar_import(import_framework):
     """
     def get_claim_priority(self, val):
         priority_dict = {            
-                'High': '2',
-                'Medium': '3',
-                'Low': '4'
+                'P1': '2',
+                'P2': '3',
+                'P3': '4'
         }
         return priority_dict.get(val.get('priority'), '')
         
@@ -248,7 +297,6 @@ class sugar_import(import_framework):
     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_address_id/.id'] = partner_address_id
         val['partner_phone'] = partner_phone
         val['email_from'] = partner_email
         return val
@@ -259,12 +307,13 @@ class sugar_import(import_framework):
                 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT, self.TABLE_LEAD],
                 'hook' : self.import_crm_claim,
                 'map' : {
-                    'name': 'name',
+                    'name': concat('case_number','name', delimiter='-'),
                     'date': 'date_entered',
                     'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'),
-                    'description': ppconcat('description'),
+                    '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,
@@ -299,14 +348,15 @@ class sugar_import(import_framework):
     def get_project_issue_mapping(self):
         return { 
                 'model' : 'project.issue',
-                'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK],
+                'dependencies' : [self.TABLE_USER],
                 'map' : {
-                    'name': 'name',
+                    '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', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'),
+                    '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)
+                    'state': map_val('status', self.project_issue_state),
+                    'assigned_to/id' : ref(self.TABLE_USER, 'assigned_user_id'),
                 }
             }
     
@@ -322,12 +372,12 @@ class sugar_import(import_framework):
      }
     
     def get_project_task_priority(self, val):
-      priority_dict = {
+        priority_dict = {
             'High': '0',
             'Medium': '2',
             'Low': '3'
         }
-      return priority_dict.get(val.get('priority'), '')
+        return priority_dict.get(val.get('priority'), '')
     
     def get_project_task_mapping(self):
         return { 
@@ -337,12 +387,10 @@ class sugar_import(import_framework):
                     'name': 'name',
                     'date_start': 'date_start',
                     'date_end': 'date_finish',
-                    'progress': 'progress',
                     'project_id/id': ref(self.TABLE_PROJECT, 'project_id'),
-                    'planned_hours': 'planned_hours',
-                    'total_hours': 'total_hours',        
+                    'planned_hours': 'estimated_effort',
                     'priority': self.get_project_task_priority,
-                    'description': ppconcat('description','milestone_flag', 'project_task_id', 'task_number'),
+                    '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',
@@ -358,6 +406,7 @@ class sugar_import(import_framework):
             'In Review': 'open',
             'Published': 'close'
      }
+    
     def import_project_account(self, val):
         partner_id = False
         partner_invoice_id = False        
@@ -403,8 +452,10 @@ class sugar_import(import_framework):
         }
 
     def import_task(self, val):
-        val['date'] = val.get('date_start') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
-        val['date_deadline'] = val.get('date_due') or datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
+        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_task_mapping(self):
@@ -426,7 +477,8 @@ class sugar_import(import_framework):
        
     """
     import Calls
-    """     
+    """  
+    #TODO adapt with project trunk-crm-imp   
     call_state = {   
             'Planned' : 'open',
             'Held':'done',
@@ -449,8 +501,8 @@ class sugar_import(import_framework):
                     'description': ppconcat('description'),   
                     'state': map_val('status', self.call_state)                      
                 }
-            }        
-        
+            }       
+         
     """
         import meeting
     """
@@ -467,12 +519,11 @@ class sugar_import(import_framework):
         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))
-            if user_id:
-                contact_id = False
-            else:    
+            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 or False, attendee.get('email1'), contact_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) 
@@ -489,6 +540,7 @@ class sugar_import(import_framework):
         return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val)
     
     #TODO attendees
+
     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
@@ -497,7 +549,7 @@ class sugar_import(import_framework):
     def get_meeting_mapping(self):
         return { 
                 'model' : 'crm.meeting',
-                'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD],
+                'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD, self.TABLE_TASK],
                 'hook': self.import_meeting,
                 'map' : {
                     'name': 'name',
@@ -525,10 +577,10 @@ class sugar_import(import_framework):
         }
         
     def get_opportunity_status(self, sugar_val):
-        fields = ['name', 'type']
+        fields = ['name', 'case_default']
         name = 'Opportunity_' + sugar_val['sales_stage']
-        data = [sugar_val['sales_stage'], 'Opportunity']
-        return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'opportunity'), ('name', 'ilike', sugar_val['sales_stage'])])
+        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))
@@ -553,8 +605,8 @@ class sugar_import(import_framework):
         return partner_contact_id, partner_contact_email
 
     def import_opp(self, val):    
-        partner_contact_name, partner_contact_email = self.import_opportunity_contact(val)
-        val['partner_address_id/id'] = partner_contact_name
+        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
     
@@ -576,7 +628,8 @@ class sugar_import(import_framework):
                 '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)  , #TODO
+                'state': map_val('status', self.opp_state),
+                'description' : 'description',
             }
         }
         
@@ -596,11 +649,11 @@ class sugar_import(import_framework):
         import lead
     """
     def get_lead_status(self, sugar_val):
-        fields = ['name', 'type']
+        fields = ['name', 'case_default']
         name = 'lead_' + sugar_val.get('status', '')
-        data = [sugar_val.get('status', ''), 'lead']
-        return self.import_object(fields, data, 'crm.case.stage', self.TABLE_STAGE, name, [('type', '=', 'lead'), ('name', 'ilike', sugar_val.get('status', ''))])
-
+        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', ''))])
+    
     lead_state = {
         'New' : 'draft',
         'Assigned':'open',
@@ -609,7 +662,6 @@ class sugar_import(import_framework):
         'Dead': 'done',
         'Converted': 'done',
     }
-
     
     def import_lead(self, val):
         if val.get('opportunity_id'): #if lead is converted into opp, don't import as lead
@@ -623,7 +675,7 @@ class sugar_import(import_framework):
     def get_lead_mapping(self):
         return {
             'model' : 'crm.lead',
-            'dependencies' : [self.TABLE_COMPAIGN],
+            'dependencies' : [self.TABLE_COMPAIGN, self.TABLE_USER],
             'hook' : self.import_lead,
             'map' : {
                 'name': concat('first_name', 'last_name'),
@@ -645,21 +697,22 @@ class sugar_import(import_framework):
                 'state': map_val('status', self.lead_state) ,
                 'fax': 'phone_fax',
                 'referred': 'refered_by',
-                'optout': 'do_not_call',
+                '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'
+                'state_id/id': 'state_id/id',
                 } 
         }
     
     """
         import contact
     """
+    
     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) 
+            return ','.join(email_address)     
     
     def import_contact(self, val):
         if val.get('primary_address_country'):
@@ -694,37 +747,46 @@ class sugar_import(import_framework):
     """ 
         import Account
     """
+    
     def get_address_type(self, val, type):
         if type == 'invoice':
             type_address = 'billing'
         else:
-            type_address = 'shipping'     
-    
-        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',
+            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_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)
@@ -772,7 +834,7 @@ class sugar_import(import_framework):
             'state_id/id': 'state_id/id',
             'street': 'address_street',
             'zip': 'address_postalcode',
-            'fax': 'fax',
+            'fax': 'phone_fax',
             'phone': 'phone_work',
             'mobile':'phone_mobile',
             'email': 'email1'
@@ -799,6 +861,8 @@ class sugar_import(import_framework):
                 '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',
             }
      }
     
@@ -806,7 +870,6 @@ class sugar_import(import_framework):
         import user
     """  
     def import_user(self, val):
-        print 'user_val', 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: 
@@ -831,7 +894,7 @@ class sugar_import(import_framework):
             'hook' : self.import_user,
             'map' : { 
                 'name': concat('first_name', 'last_name'),
-                'login': 'user_name',
+                'login': value('user_name', fallback='last_name'),
                 'context_lang' : 'context_lang',
                 'password' : 'password',
                 '.id' : '.id',
@@ -840,6 +903,7 @@ class sugar_import(import_framework):
             }
         }
 
+
     def get_mapping(self):
         return {
             self.TABLE_USER : self.get_user_mapping(),
@@ -859,9 +923,14 @@ class sugar_import(import_framework):
             self.TABLE_EMAIL: self.get_email_mapping(),
             self.TABLE_DOCUMENT: self.get_document_mapping(),
             self.TABLE_COMPAIGN: self.get_compaign_mapping()
-            
         }
-    def get_email_subject(self, result):
+        
+    """
+        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):
@@ -874,111 +943,169 @@ class import_sugarcrm(osv.osv):
     _name = "import.sugarcrm"
     _description = __doc__
     _columns = {
-               
         'username': fields.char('User Name', size=64, required=True),
         'password': fields.char('Password', size=24,required=True),
-         'url' : fields.char('Service', size=264, required=True, help="Connection with Sugarcrm Using Soap Protocol Services and For that Path should be 'http://localhost/sugarcrm/soap.php' Format."),
-                
-        '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"),
+        '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': False,
-       'user' : False,
-       'contact' : False,
-       'account' : False,
-        'employee' : False,
-        'meeting' : False,
-        'task' : False,
-        'call' : False,
-        'claim' : False,    
-        'email' : False, 
-        'project' : False,   
-        'project_task': False,     
-        'bug': False,
-        'document': False,
+       'user' : True,
+       'opportunity': True,
+       'contact' : True,
+       'account' : True,
+        'employee' : _hr_installed,
+        'meeting' : True,
+        'call' : 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': 'tfr@openerp.com',
-        'username' : 'tfr',
-        'password' : 'a',
-        'url':  "http://localhost/sugarcrm/soap.php"        
+        '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):
             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.opportunity:
-                key_list.append('Leads')
-                key_list.append('Opportunities')
             if current.user:
                 key_list.append('Users')
             if current.contact:
                 key_list.append('Contacts')
             if current.account:
-                key_list.append('Accounts') 
+                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('Cases')                
-            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('Project')
+                module.update({'Project':'project'})
             if current.project_task:
                 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('DocumentRevisions')                                                  
-        return key_list
+                key_list.append('Documents')
+        return key_list,module
 
 
     def do_import_all(self, cr, uid, *args):
         """
         scheduler Method
         """
-        print 'args of schedule method', args
         context = {'username': args[4], 'password': args[5], 'url': args[3], 'instance_name': args[3]}
-        print context
-        imp = sugar_import(self, cr, uid, args[2], "import_sugarcrm", [args[1]], context)
+        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 = self.get_key(cr, uid, ids, context)
+        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')
-        args = (keys,context.get('email_user'), context.get('instance_name'), context.get('url'), context.get('username'), context.get('password') )
+        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',
@@ -988,15 +1115,25 @@ class import_sugarcrm(osv.osv):
             '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 = self.get_key(cr, uid, ids, context)
-        imp = sugar_import(self, cr, uid, context.get('instance_name'), "import_sugarcrm", [context.get('email_user')], context)
+        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'])
@@ -1010,3 +1147,5 @@ class import_sugarcrm(osv.osv):
             }
         
 import_sugarcrm()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: