From ff4802a4aad452dab3a33891928fe4b50744fc89 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Fri, 13 May 2011 19:16:48 +0530 Subject: [PATCH 01/13] [IMP]: Integrate with import_base phonecall, task, projects bzr revid: atp@tinyerp.com-20110513134648-76yiswmp0qmrbqa5 --- addons/import_sugarcrm/__openerp__.py | 2 +- addons/import_sugarcrm/import_sugarcrm.py | 113 ++++++++++++++++++++-- 2 files changed, 105 insertions(+), 10 deletions(-) diff --git a/addons/import_sugarcrm/__openerp__.py b/addons/import_sugarcrm/__openerp__.py index 51659670bb3..85d04da2f01 100644 --- a/addons/import_sugarcrm/__openerp__.py +++ b/addons/import_sugarcrm/__openerp__.py @@ -27,7 +27,7 @@ "Contacts", "Employees", Meetings, Phonecalls, Emails, and Project, Project Tasks Data into OpenERP Module.""", 'author': 'OpenERP SA', 'website': 'http://www.openerp.com', - 'depends': ['import_base', 'crm_claim', 'project', 'project_issue', 'hr', 'document'], + 'depends': ['import_base', 'crm_claim', 'project', 'project_issue', 'project_timesheet', 'hr', 'document'], 'init_xml': [], 'update_xml': ["wizard/sugarcrm_login_view.xml", "wizard/import_message_view.xml", diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 3a6592318ac..2a7a5ed31c3 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -63,7 +63,6 @@ class sugar_import(import_framework): self.context['session_id'] = sessionid def get_data(self, table): - return sugar.search(self.context.get('port'), self.context.get('session_id'), table) """ Common import method @@ -75,8 +74,9 @@ class sugar_import(import_framework): def get_job_title(self, dict, salutation): fields = ['shortcut', 'name', 'domain'] - data = [salutation, salutation, 'Contact'] - return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)]) + if salutation: + data = [salutation, salutation, 'Contact'] + return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)]) def get_campaign_id(self, dict, val): fields = ['name'] @@ -90,7 +90,6 @@ class sugar_import(import_framework): 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 get_all_countries(self, val): @@ -100,6 +99,103 @@ class sugar_import(import_framework): def get_float_time(self, dict, hour, min): min = int(min) * 100 / 60 return "%s.%i" % (hour, min) + """ + import Projects + """ + project_state = { + 'Draft' : 'draft', + 'In Review': 'open', + 'Published': 'close' + } + def import_project_account(self, val): + partner_id = False + partner_invoice_id = False + model_obj = self.obj.pool.get('ir.model.data') + partner_obj = self.obj.pool.get('res.partner') + partner_address_obj = self.obj.pool.get('res.partner.address') + sugar_project_account = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None) + for account_id in sugar_project_account: + partner_id = self.get_mapped_id(self.TABLE_ACCOUNT, account_id) + partner_invoice_ids= partner_address_obj.search(self.cr, self.uid, [('partner_id', '=', partner_id), ('type', '=', 'invoice')]) + if partner_invoice_ids: + partner_invoice_id = partner_invoice_ids[0] + 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 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_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 get_task_mapping(self): + return { + 'model' : 'crm.meeting', + 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER], + 'map' : { + 'name': 'name', + 'date': 'date_start' or datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + 'date_deadline': 'date_due' or datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), + 'categ_id/id': call(self.get_category, 'crm.meeting', 'Tasks'), + 'partner_id/id': related_ref(self.TABLE_ACCOUNT), + 'partner_address_id/id': related_ref(self.TABLE_CONTACT), + 'state': map_val('status', self.task_state) + } + } + + """ + import Calls + """ + call_state = { + 'Planned' : 'open', + 'Held':'done', + 'Not Held': 'pending', + } + + 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': 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': 'description', + 'state': map_val('status', self.call_state) + } + } """ import meeting @@ -122,7 +218,6 @@ class sugar_import(import_framework): return self.mapped_id_if_exist('res.alarm', [('name', 'like', alarm_dict.get(val))], 'alarm', val) #TODO attendees - def get_meeting_mapping(self): return { 'model' : 'crm.meeting', @@ -221,7 +316,6 @@ class sugar_import(import_framework): 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 @@ -317,7 +411,6 @@ class sugar_import(import_framework): '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) @@ -449,6 +542,9 @@ class sugar_import(import_framework): 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(), } @@ -524,7 +620,7 @@ class import_sugarcrm(osv.osv): if current.email: key_list.append('Emails') if current.project: - key_list.append('Projects') + key_list.append('Project') if current.project_task: key_list.append('Project Tasks') if current.bug: @@ -539,7 +635,6 @@ class import_sugarcrm(osv.osv): # """Import all sugarcrm data into openerp module""" keys = self.get_key(cr, uid, ids, context) - imp = sugar_import(self, cr, uid, "sugarcrm", "import_sugarcrm", context) imp.import_all(keys) From 84a5f647fedbb60bc39422fe682f42caf33f4605 Mon Sep 17 00:00:00 2001 From: Atul Patel Date: Mon, 16 May 2011 07:53:46 +0530 Subject: [PATCH 02/13] [Fix]: Correction in project and Integrate project task import_framework. bzr revid: atp@tinyerp.com-20110516022346-rnh6sciedtr0dgij --- addons/import_sugarcrm/import_sugarcrm.py | 77 ++++++++++++++++++----- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 2a7a5ed31c3..f7d1f6e0b57 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -23,7 +23,7 @@ import sugar 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) @@ -99,6 +99,48 @@ class sugar_import(import_framework): def get_float_time(self, dict, hour, min): min = int(min) * 100 / 60 return "%s.%i" % (hour, min) + + + """ + import Project Tasks + """ + project_task_state = { + 'Not Started': 'draft', + 'In Progress': 'open', + 'Completed': 'done', + 'Pending Input': 'pending', + 'Deferred': 'cancelled', + } + + def get_project_task_priority(self, val): + priority_dict = { + 'High': '0', + 'Medium': '2', + 'Low': '3' + } + 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', + 'progress': 'progress', + 'project_id/id': ref(self.TABLE_PROJECT, 'project_id'), + 'planned_hours': 'planned_hours', + 'total_hours': 'total_hours', + 'priority': self.get_project_task_priority, + 'description': 'description', + '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) + } + } + """ import Projects """ @@ -113,16 +155,16 @@ class sugar_import(import_framework): model_obj = self.obj.pool.get('ir.model.data') partner_obj = self.obj.pool.get('res.partner') partner_address_obj = self.obj.pool.get('res.partner.address') - sugar_project_account = sugar.relation_search(self.context.get('port'), self.context.get('session_id'), 'Project', module_id=val.get('id'), related_module='Accounts', query=None, deleted=None) + 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) - partner_invoice_ids= partner_address_obj.search(self.cr, self.uid, [('partner_id', '=', partner_id), ('type', '=', 'invoice')]) - if partner_invoice_ids: - partner_invoice_id = partner_invoice_ids[0] - return partner_id, partner_invoice_id + return partner_id, partner_invoice_id def import_project(self, val): - partner_id, partner_invoice_id = self.import_project_account(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 @@ -153,19 +195,25 @@ class sugar_import(import_framework): 'Pending Input': 'draft', 'deferred': 'cancel' } - + + 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'), + return val + 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_start' or datetime.now().strftime('%Y-%m-%d %H:%M:%S'), - 'date_deadline': 'date_due' or datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + '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', 'Tasks'), + 'categ_id/id': call(self.get_category, 'crm.meeting', const('Tasks')), 'partner_id/id': related_ref(self.TABLE_ACCOUNT), - 'partner_address_id/id': related_ref(self.TABLE_CONTACT), + 'partner_address_id/id': ref(self.TABLE_CONTACT,'contact_id'), 'state': map_val('status', self.task_state) } } @@ -178,7 +226,7 @@ class sugar_import(import_framework): 'Held':'done', 'Not Held': 'pending', } - + def get_calls_mapping(self): return { 'model' : 'crm.phonecall', @@ -545,6 +593,7 @@ class sugar_import(import_framework): 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() } @@ -622,7 +671,7 @@ class import_sugarcrm(osv.osv): if current.project: key_list.append('Project') if current.project_task: - key_list.append('Project Tasks') + key_list.append('ProjectTask') if current.bug: key_list.append('Bugs') if current.attachment: From 797b722de0d9c8c556a67f2776b90893a1ae6c8e Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Mon, 16 May 2011 18:23:27 +0530 Subject: [PATCH 03/13] [IMP]: Intregate Project task , bug and claims bzr revid: atp@tinyerp.com-20110516125327-qcynjls983iubi3v --- addons/import_sugarcrm/import_sugarcrm.py | 106 ++++++++++++++++++++-- 1 file changed, 98 insertions(+), 8 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index f7d1f6e0b57..0eaddc5d7ab 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -53,6 +53,7 @@ class sugar_import(import_framework): TABLE_TASK = 'Tasks' TABLE_PROJECT = 'Project' TABLE_PROJECT_TASK = 'ProjectTask' + TABLE_BUG = 'Bugs' TABLE_CASE = 'Cases' @@ -99,8 +100,95 @@ class sugar_import(import_framework): def get_float_time(self, dict, hour, min): min = int(min) * 100 / 60 return "%s.%i" % (hour, min) - - + """ + import Claims(Cases) + """ + def get_claim_priority(self, val): + priority_dict = { + 'High': '2', + 'Medium': '3', + 'Low': '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_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': 'name', + 'date': 'date_entered', + 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), + 'description': 'description', + 'partner_id/id': ref(self.TABLE_ACCOUNT, 'account_id'), + 'partner_address_id/.id': 'partner_address_id/.id', + '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', + 'Closed': 'done', + 'Pending': 'pending', + 'Rejected': 'cancel', + } + + 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) + + def get_project_issue_mapping(self): + return { + 'model' : 'project.issue', + 'dependencies' : [self.TABLE_USER, self.TABLE_PROJECT, self.TABLE_PROJECT_TASK], + 'map' : { + 'name': 'name', + 'project_id/id': call(self.get_bug_project_id, 'sugarcrm_bugs'), + 'categ_id/id': call(self.get_category, 'project.issue', value('type')), + 'description': ppconcat('__prettyprint__','description', 'bug_number', 'fixed_in_release_name', 'source', 'fixed_in_release', 'work_log', 'found_in_release', 'release_name', 'resolution'), + 'priority': self.get_project_issue_priority, + 'state': map_val('status', self.project_issue_state) + } + } + """ import Project Tasks """ @@ -197,15 +285,15 @@ 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'), - return 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'), + return val def get_task_mapping(self): return { 'model' : 'crm.meeting', 'dependencies' : [self.TABLE_CONTACT, self.TABLE_ACCOUNT, self.TABLE_USER], - 'hook' : self.import_task, + 'hook' : self.import_task, 'map' : { 'name': 'name', 'date': 'date', @@ -593,7 +681,9 @@ class sugar_import(import_framework): 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_PROJECT_TASK: self.get_project_task_mapping(), + self.TABLE_BUG: self.get_project_issue_mapping(), + self.TABLE_CASE: self.get_crm_claim_mapping() } @@ -665,7 +755,7 @@ class import_sugarcrm(osv.osv): if current.call: key_list.append('Calls') if current.claim: - key_list.append('Claims') + key_list.append('Cases') if current.email: key_list.append('Emails') if current.project: From 3c1712a9c1a26c4bb09bbeeddee0c78ec2811449 Mon Sep 17 00:00:00 2001 From: Atul Patel Date: Tue, 17 May 2011 09:24:12 +0530 Subject: [PATCH 04/13] [IMP]: Intregate history to import_base module bzr revid: atp@tinyerp.com-20110517035412-5mf8krzgacrbkesq --- addons/import_sugarcrm/import_sugarcrm.py | 51 ++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 0eaddc5d7ab..056486b07a1 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -55,6 +55,7 @@ class sugar_import(import_framework): TABLE_PROJECT_TASK = 'ProjectTask' TABLE_BUG = 'Bugs' TABLE_CASE = 'Cases' + TABLE_NOTE = 'Notes' def initialize(self): @@ -100,6 +101,53 @@ class sugar_import(import_framework): def get_float_time(self, dict, hour, min): min = int(min) * 100 / 60 return "%s.%i" % (hour, min) + + """ + import History(Notes) + """ +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.get('res_id',False),'res_model': val.get('model',False)}) + message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, context.get('instance_name')+'_note_'+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 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_type')) + 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_history_mapping(self): + return { + 'model' : 'mailgate.message', + 'dependencies' : [], + 'hook' : self.import_history, + 'map' : { + 'name':'name', + 'date': 'date_entered', + 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), + 'description': ppconcat('__prettyprint__','description', 'description_html'), + 'res_id': 'res_id', + 'model': 'model', + 'partner_id/.id' : 'partner_id/.id', + } + } + """ import Claims(Cases) """ @@ -683,7 +731,8 @@ class sugar_import(import_framework): 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_CASE: self.get_crm_claim_mapping(), + self.TABLE_NOTE: self.get_history_mapping() } From 4b3649d9dd8187061a8cfd13b3f01aa0f72323a9 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Tue, 17 May 2011 19:12:22 +0530 Subject: [PATCH 05/13] [IMP]: Intregate history and email and bug to import_base module. bzr revid: atp@tinyerp.com-20110517134222-xszd6ot5pvs1c8ur --- addons/import_sugarcrm/import_sugarcrm.py | 107 ++++++++++++++++++---- 1 file changed, 87 insertions(+), 20 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index a65825b28e0..e953324a4af 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -56,6 +56,8 @@ class sugar_import(import_framework): TABLE_BUG = 'Bugs' TABLE_CASE = 'Cases' TABLE_NOTE = 'Notes' + TABLE_EMAIL = 'Emails' + TABLE_DOCUMENT = 'DocumentRevisions' def initialize(self): @@ -101,27 +103,41 @@ class sugar_import(import_framework): def get_float_time(self, dict, hour, min): min = int(min) * 100 / 60 return "%s.%i" % (hour, min) + + """ + import Documents + """ + + def import_document(self, val): + + filepath = '/var/www/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') + return val + + def get_document_mapping(self): + return { + 'model' : 'ir.attachment', + 'dependencies' : [self.TABLE_USER], + 'hook' : self.import_document, + 'map' : {'name':'filename', + 'description': 'description', + 'datas': 'datas', + 'datas_fname': 'datas_fname', + } + } """ - import History(Notes) + import Emails """ -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.get('res_id',False),'res_model': val.get('model',False)}) - message_model_ids = find_mapped_id(sugar_obj, cr, uid, model, context.get('instance_name')+'_note_'+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 import_history(self, val): + + def import_email(self, val): model_obj = self.obj.pool.get('ir.model.data') xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_type')) + xml_id = self.xml_id_exist(history_attachment, val.get('parent_type')) 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] @@ -130,12 +146,60 @@ def get_attachment(sugar_obj, cr, uid, val, model, File, context=None): 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], + 'hook' : self.import_email, + 'map' : {'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', + 'res_id': 'res_id', + 'model': 'model', + 'partner_id/.id': 'partner_id/.id', + 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), + 'description': ppconcat('__prettyprint__','description', 'description_html'), + } + } + + + """ + import History(Notes) + """ + 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')) + 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') + fields = ['name', 'datas', 'res_id', 'res_model'] + datas = [val.get('name'), File, val.get('res_id'),val.get('model',False)] + self.import_object(fields, datas, 'ir.attachment', 'history_attachment', val.get('name'), [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))]) + return True + + 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 + get_attachment(val) return val def get_history_mapping(self): return { 'model' : 'mailgate.message', - 'dependencies' : [], + '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], 'hook' : self.import_history, 'map' : { 'name':'name', @@ -732,7 +796,10 @@ def get_attachment(sugar_obj, cr, uid, val, model, File, context=None): 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_NOTE: self.get_history_mapping(), + self.TABLE_EMAIL: self.get_email_mapping(), + self.TABLE_DOCUMENT: self.get_document_mapping() + } @@ -745,6 +812,7 @@ 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"), @@ -816,14 +884,13 @@ class import_sugarcrm(osv.osv): if current.attachment: key_list.append('Notes') if current.document: - key_list.append('Documents') + key_list.append('DocumentRevisions') return key_list 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, "sugarcrm", "import_sugarcrm", ["tfr@openerp.com"], context) imp.set_table_list(keys) imp.start() From 5ac960861cbc2a97973987208cbf0447ab785734 Mon Sep 17 00:00:00 2001 From: Atul Patel Date: Tue, 17 May 2011 23:04:22 +0530 Subject: [PATCH 06/13] [ADD]: Intregate attachment into import_base bzr revid: atp@tinyerp.com-20110517173422-0xf84vh0zd7314xz --- addons/import_sugarcrm/import_sugarcrm.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index e953324a4af..0712514dae9 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -109,7 +109,6 @@ class sugar_import(import_framework): """ def import_document(self, val): - filepath = '/var/www/sugarcrm/cache/upload/'+ val.get('id') f = open(filepath, "r") datas = f.read() @@ -137,7 +136,6 @@ class sugar_import(import_framework): def import_email(self, val): model_obj = self.obj.pool.get('ir.model.data') xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_type')) - xml_id = self.xml_id_exist(history_attachment, val.get('parent_type')) 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] @@ -146,6 +144,8 @@ class sugar_import(import_framework): else: val['res_id'] = model.res_id val['model'] = model.model + attach_id = self.get_attachment(val) + val['attachment_ids/id'] = attach_id return val def get_email_mapping(self): @@ -163,6 +163,7 @@ class sugar_import(import_framework): 'res_id': 'res_id', 'model': 'model', 'partner_id/.id': 'partner_id/.id', + 'attachment_ids/id': 'attachment_ids/id', 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), 'description': ppconcat('__prettyprint__','description', 'description_html'), } @@ -177,10 +178,11 @@ class sugar_import(import_framework): 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') - fields = ['name', 'datas', 'res_id', 'res_model'] - datas = [val.get('name'), File, val.get('res_id'),val.get('model',False)] + fields = ['name', 'datas', 'datas_fname','res_id', 'res_model'] + datas = [val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)] self.import_object(fields, datas, 'ir.attachment', 'history_attachment', val.get('name'), [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))]) - return True + attach_id = self.xml_id_exist('history_attachment', val.get('name')) + return attach_id def import_history(self, val): model_obj = self.obj.pool.get('ir.model.data') @@ -193,7 +195,8 @@ class sugar_import(import_framework): else: val['res_id'] = model.res_id val['model'] = model.model - get_attachment(val) + attach_id = self.get_attachment(val) + val['attachment_ids/id'] = attach_id return val def get_history_mapping(self): @@ -208,6 +211,7 @@ class sugar_import(import_framework): 'description': ppconcat('__prettyprint__','description', 'description_html'), 'res_id': 'res_id', 'model': 'model', + 'attachment_ids/id': 'attachment_ids/id', 'partner_id/.id' : 'partner_id/.id', } } From 2a6abd22fa2deaf2e75df674937025879a4d49f2 Mon Sep 17 00:00:00 2001 From: Atul Patel Date: Wed, 18 May 2011 08:54:52 +0530 Subject: [PATCH 07/13] [IMP]: Add attendee into Meeting for user. bzr revid: atp@tinyerp.com-20110518032452-xszk5u9r5besryxg --- addons/import_sugarcrm/import_sugarcrm.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 0712514dae9..70251ecca3e 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -48,6 +48,7 @@ class sugar_import(import_framework): TABLE_OPPORTUNITY = 'Opportunities' TABLE_LEAD = 'Leads' TABLE_STAGE = 'crm_stage' + TABLE_ATTENDEE = 'calendar_attendee' TABLE_CALL = 'Calls' TABLE_MEETING = 'Meetings' TABLE_TASK = 'Tasks' @@ -457,6 +458,22 @@ class sugar_import(import_framework): 'Held': 'open', 'Not Held': 'draft', } +#TODO + def get_attendee_id(self, cr, uid, module_name, module_id): + model_obj = self.obj.pool.get('ir.model.data') + att_obj = self.obj.pool.get('calendar.attendee') + meeting_obj = self.obj.pool.get('crm.meeting') + user_dict = sugar.user_get_attendee_list(self.context.get('port'), self.context.get('session_id'), module_name, module_id) + #TODO there is more then just user in attendee list + #there is contact, partner, lead (but we don't import the link) and user + for user in user_dict: + continue + user_id = self.get_mapped_id(self.TABLE_USER, user.get('id')) + fields = ['user_id', 'email1'] + data = [user_id, user.get('email1')] + self.import_object(fields, data, 'calendar.attendee', self.TABLE_ATTENDEE, user_id, [('user_id', '=', user_id)]) + attendee_id = self.xml_id_exist(self.TABLE_ATTENDEE, user_id) + return attendee_id def get_alarm_id(self, dict_val, val): alarm_dict = { @@ -470,15 +487,22 @@ 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 + return val + def get_meeting_mapping(self): return { 'model' : 'crm.meeting', 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD], + 'hook': self.import_meeting, 'map' : { 'name': 'name', '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), From 90e0fd284d2158a41503630c14815a0ccb158223 Mon Sep 17 00:00:00 2001 From: "Atul Patel (OpenERP)" Date: Wed, 18 May 2011 12:14:47 +0530 Subject: [PATCH 08/13] [IMP]: Add some missing field in intregated module. bzr revid: atp@tinyerp.com-20110518064447-jah9d1aqqs3dr5rk --- addons/import_sugarcrm/import_sugarcrm.py | 66 ++++++++++++++++------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index 70251ecca3e..bdfde8e4acd 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -59,6 +59,7 @@ class sugar_import(import_framework): TABLE_NOTE = 'Notes' TABLE_EMAIL = 'Emails' TABLE_DOCUMENT = 'DocumentRevisions' + TABLE_COMPAIGN = 'Campaigns' def initialize(self): @@ -83,10 +84,10 @@ class sugar_import(import_framework): data = [salutation, salutation, 'Contact'] return self.import_object(fields, data, 'res.partner.title', 'contact_title', salutation, [('shortcut', '=', salutation)]) - def get_campaign_id(self, dict, val): + def get_channel_id(self, dict, val): fields = ['name'] data = [val] - return self.import_object(fields, data, 'crm.case.resource.type', 'crm_campaign', val) + return self.import_object(fields, data, 'res.partner.canal', 'crm_channel', val) def get_all_states(self, external_val, country_id): """Get states or create new state unless country_id is False""" @@ -124,7 +125,7 @@ class sugar_import(import_framework): 'dependencies' : [self.TABLE_USER], 'hook' : self.import_document, 'map' : {'name':'filename', - 'description': 'description', + 'description': ppconcat('__prettyprint__','description'), 'datas': 'datas', 'datas_fname': 'datas_fname', } @@ -175,14 +176,15 @@ class sugar_import(import_framework): import History(Notes) """ 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')) + File, Filename = sugar.attachment_search(self.context.get('port'), self.context.get('session_id'), self.TABLE_NOTE, val.get('id')) 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') fields = ['name', 'datas', 'datas_fname','res_id', 'res_model'] - datas = [val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)] - self.import_object(fields, datas, 'ir.attachment', 'history_attachment', val.get('name'), [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))]) - attach_id = self.xml_id_exist('history_attachment', val.get('name')) + name = 'attachment_'+ (Filename or val.get('name')) + datas = [Filename or val.get('name'), File, Filename, val.get('res_id'),val.get('model',False)] + self.import_object(fields, datas, 'ir.attachment', 'history_attachment', name, [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))]) + attach_id = self.xml_id_exist('history_attachment', name) return attach_id def import_history(self, val): @@ -258,7 +260,7 @@ class sugar_import(import_framework): 'name': 'name', 'date': 'date_entered', 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), - 'description': 'description', + 'description': ppconcat('__prettyprint__','description'), 'partner_id/id': ref(self.TABLE_ACCOUNT, 'account_id'), 'partner_address_id/.id': 'partner_address_id/.id', 'partner_phone': 'partner_phone', @@ -324,7 +326,7 @@ class sugar_import(import_framework): 'Low': '3' } return priority_dict.get(val.get('priority'), '') - + def get_project_task_mapping(self): return { 'model' : 'project.task', @@ -338,7 +340,7 @@ class sugar_import(import_framework): 'planned_hours': 'planned_hours', 'total_hours': 'total_hours', 'priority': self.get_project_task_priority, - 'description': 'description', + 'description': ppconcat('__prettyprint__','description','milestone_flag', 'project_task_id', 'task_number'), 'user_id/id': ref(self.TABLE_USER, 'assigned_user_id'), 'partner_id/id': 'partner_id/id', 'contact_id/id': 'contact_id/id', @@ -445,7 +447,7 @@ class sugar_import(import_framework): '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': 'description', + 'description': ppconcat('__prettyprint__','description'), 'state': map_val('status', self.call_state) } } @@ -495,7 +497,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_COMPAIGN], 'hook': self.import_meeting, 'map' : { 'name': 'name', @@ -514,6 +516,14 @@ class sugar_import(import_framework): """ 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', 'type'] name = 'Opportunity_' + sugar_val['sales_stage'] @@ -566,10 +576,22 @@ 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' : const('open'), #TODO + 'state': map_val('status', self.opp_state) , #TODO } } + + """ + import campaign + """ + def get_compaign_mapping(self): + return { + 'model' : 'crm.case.resource.type', + 'map' : { + 'name': 'name', + } + } + """ import lead """ @@ -601,11 +623,12 @@ class sugar_import(import_framework): def get_lead_mapping(self): return { 'model' : 'crm.lead', + 'dependencies' : [self.TABLE_COMPAIGN], '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'), + 'description': ppconcat('__prettyprint__','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', @@ -623,7 +646,8 @@ class sugar_import(import_framework): 'fax': 'phone_fax', 'referred': 'refered_by', 'optout': 'do_not_call', - 'type_id/id': call(self.get_campaign_id, value('lead_source')), + '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' } @@ -715,7 +739,7 @@ class sugar_import(import_framework): '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'), + 'comment': ppconcat('__prettyprint__','description', 'employees', 'ownership', 'annual_revenue', 'rating', 'industry', 'ticker_symbol'), 'customer': const('1'), 'supplier': const('0'), 'address/id':'address/id', @@ -747,6 +771,9 @@ class sugar_import(import_framework): 'street': 'address_street', 'zip': 'address_postalcode', 'fax': 'fax', + 'phone': 'phone_work', + 'mobile':'phone_mobile', + 'email': 'email1' } if val.get('address_country'): @@ -756,7 +783,7 @@ class sugar_import(import_framework): 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_employee_mapping(self): return { 'model' : 'hr.employee', @@ -768,7 +795,7 @@ class sugar_import(import_framework): 'mobile_phone': 'phone_mobile', 'user_id/id': ref(self.TABLE_USER, 'id'), 'address_home_id/id': self.get_user_address, - 'notes': 'description', + 'notes': ppconcat('messenger_type', 'messenger_id', 'description'), 'job_id/id': self.get_job_id, } } @@ -826,7 +853,8 @@ class sugar_import(import_framework): 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_DOCUMENT: self.get_document_mapping(), + self.TABLE_COMPAIGN: self.get_compaign_mapping() } From beff44398891bd098301a18ae1551f121868fa6d Mon Sep 17 00:00:00 2001 From: Atul Patel Date: Thu, 19 May 2011 08:42:37 +0530 Subject: [PATCH 09/13] [IMP]: create wizard and changed in view. intregate attendee in import_base. create scheduler based on sugarcrm. bzr revid: atp@tinyerp.com-20110519031237-kvwo64jcy9pncm78 --- addons/import_sugarcrm/__openerp__.py | 2 +- addons/import_sugarcrm/import_sugarcrm.py | 114 +++++++++++++----- .../import_sugarcrm/import_sugarcrm_view.xml | 76 +++++++++--- .../import_sugarcrm/wizard/sugarcrm_login.py | 21 ---- .../wizard/sugarcrm_login_view.xml | 14 --- 5 files changed, 144 insertions(+), 83 deletions(-) diff --git a/addons/import_sugarcrm/__openerp__.py b/addons/import_sugarcrm/__openerp__.py index 85d04da2f01..28de3e63b30 100644 --- a/addons/import_sugarcrm/__openerp__.py +++ b/addons/import_sugarcrm/__openerp__.py @@ -29,7 +29,7 @@ 'website': 'http://www.openerp.com', 'depends': ['import_base', 'crm_claim', 'project', 'project_issue', 'project_timesheet', 'hr', 'document'], 'init_xml': [], - 'update_xml': ["wizard/sugarcrm_login_view.xml", + 'update_xml': [ "wizard/import_message_view.xml", "import_sugarcrm_view.xml"], 'demo_xml': [], diff --git a/addons/import_sugarcrm/import_sugarcrm.py b/addons/import_sugarcrm/import_sugarcrm.py index bdfde8e4acd..0604b9b5d05 100644 --- a/addons/import_sugarcrm/import_sugarcrm.py +++ b/addons/import_sugarcrm/import_sugarcrm.py @@ -60,19 +60,25 @@ class sugar_import(import_framework): TABLE_EMAIL = 'Emails' TABLE_DOCUMENT = 'DocumentRevisions' TABLE_COMPAIGN = 'Campaigns' + TABLE_HISTORY_ATTACHMNET = 'history_attachment' + 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 !')) 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) """ - Common import method + Common import method """ + def get_category(self, val, model, name): fields = ['name', 'object_id'] data = [name, model] @@ -130,14 +136,14 @@ class sugar_import(import_framework): 'datas_fname': 'datas_fname', } } - + """ import Emails """ def import_email(self, val): model_obj = self.obj.pool.get('ir.model.data') - xml_id = self.xml_id_exist(val.get('parent_type'), val.get('parent_type')) + 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] @@ -183,9 +189,9 @@ class sugar_import(import_framework): 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)] - self.import_object(fields, datas, 'ir.attachment', 'history_attachment', name, [('res_id', '=', val.get('res_id'), ('model', '=', val.get('model')))]) - attach_id = self.xml_id_exist('history_attachment', name) - return attach_id + 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')))]) + #attach_id = self.xml_id_exist(self.TABLE_HISTORY_ATTACHMNET, name) + return attach_xml_id def import_history(self, val): model_obj = self.obj.pool.get('ir.model.data') @@ -463,19 +469,27 @@ class sugar_import(import_framework): #TODO def get_attendee_id(self, cr, uid, module_name, module_id): model_obj = self.obj.pool.get('ir.model.data') + user_obj = self.obj.pool.get('res.users') + address_obj = self.obj.pool.get('res.partner.address') att_obj = self.obj.pool.get('calendar.attendee') meeting_obj = self.obj.pool.get('crm.meeting') - user_dict = sugar.user_get_attendee_list(self.context.get('port'), self.context.get('session_id'), module_name, module_id) - #TODO there is more then just user in attendee list - #there is contact, partner, lead (but we don't import the link) and user - for user in user_dict: - continue - user_id = self.get_mapped_id(self.TABLE_USER, user.get('id')) - fields = ['user_id', 'email1'] - data = [user_id, user.get('email1')] - self.import_object(fields, data, 'calendar.attendee', self.TABLE_ATTENDEE, user_id, [('user_id', '=', user_id)]) - attendee_id = self.xml_id_exist(self.TABLE_ATTENDEE, user_id) - return attendee_id + attendee_dict = sugar.user_get_attendee_list(self.context.get('port'), self.context.get('session_id'), module_name, module_id) + contact_id = False + user_id = False + for attendee in attendee_dict: + model_ids = model_obj.search(self.cr, self.uid, [('name', 'like', attendee.get('id'))]) + if model_ids: + model = model_obj.browse(self.cr, self.uid, model_ids)[0] + if model.model == 'res.users': + user_id = self.get_mapped_id(self.TABLE_USER, attendee.get('id', False)) + elif model.model == 'res.partner.address': + contact_id = self.get_mapped_id(self.TABLE_CONTACT, attendee.get('id', False)) + fields = ['user_id', 'email1', 'contact_id'] + name = 'attendee_'+ attendee.get('id') + data = [user_id or False, attendee.get('email1'), contact_id] + attendee_xml_id = self.import_object(fields, data, 'calendar.attendee', self.TABLE_ATTENDEE, name, [('user_id', '=', user_id)]) + #attendee_id = self.xml_id_exist(self.TABLE_ATTENDEE, name) + return attendee_xml_id def get_alarm_id(self, dict_val, val): alarm_dict = { @@ -490,14 +504,14 @@ class sugar_import(import_framework): #TODO attendees def import_meeting(self, val): - attendee_id = self.get_attendee_id(self.cr, self.uid, 'Meetings', val.get('id')) #TODO + attendee_id = self.get_attendee_id(self.cr, self.uid, 'Meetings', val.get('id')) #TODO val['attendee_ids/id'] = attendee_id return val def get_meeting_mapping(self): return { 'model' : 'crm.meeting', - 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD, self.TABLE_COMPAIGN], + 'dependencies' : [self.TABLE_CONTACT, self.TABLE_OPPORTUNITY, self.TABLE_LEAD], 'hook': self.import_meeting, 'map' : { 'name': 'name', @@ -561,7 +575,7 @@ class sugar_import(import_framework): def get_opp_mapping(self): return { 'model' : 'crm.lead', - 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT], + 'dependencies' : [self.TABLE_USER, self.TABLE_ACCOUNT, self.TABLE_CONTACT,self.TABLE_COMPAIGN], 'hook' : self.import_opp, 'map' : { 'name': 'name', @@ -859,15 +873,16 @@ class sugar_import(import_framework): } - - - class import_sugarcrm(osv.osv): """Import SugarCRM DATA""" _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"), @@ -884,8 +899,9 @@ class import_sugarcrm(osv.osv): '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), + '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."), + } _defaults = {#to be set to true, but easier for debugging 'opportunity': False, @@ -901,7 +917,10 @@ class import_sugarcrm(osv.osv): 'project' : False, 'project_task': False, 'bug': False, - 'document': False + 'document': False, + 'username' : 'tfr', + 'password' : 'a', + 'url': "http://localhost/sugarcrm/soap.php" } def get_key(self, cr, uid, ids, context=None): @@ -910,6 +929,7 @@ class import_sugarcrm(osv.osv): context = {} key_list = [] 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') @@ -943,11 +963,51 @@ class import_sugarcrm(osv.osv): key_list.append('DocumentRevisions') return key_list + def do_import_all(self, cr, uid, *args): + """ + scheduler Method + """ + imported = set() + context = {} + login_obj = self.pool.get('sugarcrm.login') + if args[1]: + login_id = login_obj.browse(cr, uid, args[1]) + context.update({'username': login_id.username, 'password': login_id.password, 'url': login_id.url, 'instance_name': args[3]}) + for key in args[0]: + imp = sugar_import(self, cr, uid, context.get('instance_name'), "import_sugarcrm", [context.get('email_from', 'tfr@openerp.com')], context) + imp.set_table_list(keys) + imp.start() + return True + + def import_from_scheduler_all(self, cr, uid, ids, context=None): + keys = self.get_key(cr, uid, ids, context) + if not keys: + raise osv.except_osv(_('Warning !'), _('Select Module to Import.')) + cron_obj = self.pool.get('ir.cron') + mod_obj = self.pool.get('ir.model.data') + login_obj = self.pool.get('sugarcrm.login') + field = ['username', 'password', 'url'] + datas = [context.get('username'), context.get('password'), context.get('url')] + name = 'login_user_'+ context.get('username') + self.import_object(fields, datas, 'sugarcrm.login', 'sugarcrm_login', name, [('username', '=', context.get('username'))]) + login_id = self.xml_id_exist('sugarcrm_login', name) + args = (keys, login_id, context.get('email_user'), context.get('instance_name')) + for current in self.browse(cr, uid, ids): + 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_sugarcrm_data', '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 = self.get_key(cr, uid, ids, context) - imp = sugar_import(self, cr, uid, "sugarcrm", "import_sugarcrm", ["tfr@openerp.com"], context) + imp = sugar_import(self, cr, uid, context.get('instance_name'), "import_sugarcrm", [context.get('email_from', 'tfr@openerp.com')], context) imp.set_table_list(keys) imp.start() diff --git a/addons/import_sugarcrm/import_sugarcrm_view.xml b/addons/import_sugarcrm/import_sugarcrm_view.xml index 715c02e0a4b..229e32ab7f3 100644 --- a/addons/import_sugarcrm/import_sugarcrm_view.xml +++ b/addons/import_sugarcrm/import_sugarcrm_view.xml @@ -8,28 +8,62 @@ import.sugarcrm form -
+ - + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +