2011-03-24 04:29:43 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields , osv
import sugar
2011-03-29 04:34:57 +00:00
from tools . translate import _
2011-05-12 08:56:24 +00:00
from import_base . import_framework import *
from import_base . mapper import *
2011-05-16 02:23:46 +00:00
from datetime import datetime
2011-04-28 12:42:57 +00:00
import base64
2011-05-12 08:56:24 +00:00
import pprint
2011-05-30 16:59:08 +00:00
from papyon . service . SOAPService import url_split
2011-03-24 04:29:43 +00:00
pp = pprint . PrettyPrinter ( indent = 4 )
2011-05-12 08:56:24 +00:00
#copy old import here
2011-05-27 13:04:28 +00:00
2011-05-12 08:56:24 +00:00
class related_ref ( dbmapper ) :
def __init__ ( self , type ) :
self . type = type
def __call__ ( self , external_val ) :
if external_val . get ( ' parent_type ' ) in self . type and external_val . get ( ' parent_id ' ) :
return self . parent . xml_id_exist ( external_val [ ' parent_type ' ] , external_val [ ' parent_id ' ] )
return ' '
class sugar_import ( import_framework ) :
2011-05-27 13:04:28 +00:00
URL = False
2011-05-12 08:56:24 +00:00
TABLE_CONTACT = ' Contacts '
TABLE_ACCOUNT = ' Accounts '
TABLE_USER = ' Users '
TABLE_EMPLOYEE = ' Employees '
TABLE_RESSOURCE = " resource "
TABLE_OPPORTUNITY = ' Opportunities '
TABLE_LEAD = ' Leads '
TABLE_STAGE = ' crm_stage '
2011-05-18 03:24:52 +00:00
TABLE_ATTENDEE = ' calendar_attendee '
2011-05-12 08:56:24 +00:00
TABLE_CALL = ' Calls '
TABLE_MEETING = ' Meetings '
TABLE_TASK = ' Tasks '
TABLE_PROJECT = ' Project '
TABLE_PROJECT_TASK = ' ProjectTask '
2011-05-16 12:53:27 +00:00
TABLE_BUG = ' Bugs '
2011-05-12 08:56:24 +00:00
TABLE_CASE = ' Cases '
2011-05-17 03:54:12 +00:00
TABLE_NOTE = ' Notes '
2011-05-17 13:42:22 +00:00
TABLE_EMAIL = ' Emails '
2011-05-18 06:44:47 +00:00
TABLE_COMPAIGN = ' Campaigns '
2011-06-28 05:10:01 +00:00
TABLE_DOCUMENT = ' Documents '
2011-05-19 03:12:37 +00:00
TABLE_HISTORY_ATTACHMNET = ' history_attachment '
2011-05-12 08:56:24 +00:00
2011-06-24 12:18:29 +00:00
MAX_RESULT_PER_PAGE = 200
2011-05-12 08:56:24 +00:00
def initialize ( self ) :
#login
PortType , sessionid = sugar . login ( self . context . get ( ' username ' , ' ' ) , self . context . get ( ' password ' , ' ' ) , self . context . get ( ' url ' , ' ' ) )
2011-05-19 03:12:37 +00:00
if sessionid == ' -1 ' :
2011-05-30 16:59:08 +00:00
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' Authentication error ! \n Bad Username or Password or bad SugarSoap Api url ! ' ) )
2011-05-12 08:56:24 +00:00
self . context [ ' port ' ] = PortType
self . context [ ' session_id ' ] = sessionid
def get_data ( self , table ) :
2011-06-24 12:18:29 +00:00
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)
2011-05-12 08:56:24 +00:00
"""
2011-05-19 03:12:37 +00:00
Common import method
2011-05-12 08:56:24 +00:00
"""
def get_category ( self , val , model , name ) :
fields = [ ' name ' , ' object_id ' ]
data = [ name , model ]
return self . import_object ( fields , data , ' crm.case.categ ' , ' crm_categ ' , name , [ ( ' object_id.model ' , ' = ' , model ) , ( ' name ' , ' ilike ' , name ) ] )
def get_job_title ( self , dict , salutation ) :
fields = [ ' shortcut ' , ' name ' , ' domain ' ]
2011-05-13 13:46:48 +00:00
if salutation :
data = [ salutation , salutation , ' Contact ' ]
return self . import_object ( fields , data , ' res.partner.title ' , ' contact_title ' , salutation , [ ( ' shortcut ' , ' = ' , salutation ) ] )
2011-05-05 17:10:37 +00:00
2011-05-18 06:44:47 +00:00
def get_channel_id ( self , dict , val ) :
2011-05-30 16:59:08 +00:00
if not val :
return False
2011-05-12 08:56:24 +00:00
fields = [ ' name ' ]
data = [ val ]
2011-05-18 06:44:47 +00:00
return self . import_object ( fields , data , ' res.partner.canal ' , ' crm_channel ' , val )
2011-05-12 08:56:24 +00:00
def get_all_states ( self , external_val , country_id ) :
""" Get states or create new state unless country_id is False """
state_code = external_val [ 0 : 3 ] #take the tree first char
fields = [ ' country_id/id ' , ' name ' , ' code ' ]
data = [ country_id , external_val , state_code ]
if country_id :
return self . import_object ( fields , data , ' res.country.state ' , ' country_state ' , external_val )
return False
def get_all_countries ( self , val ) :
""" Get Country, if no country match do not create anything, to avoid duplicate country code """
return self . mapped_id_if_exist ( ' res.country ' , [ ( ' name ' , ' ilike ' , val ) ] , ' country ' , val )
def get_float_time ( self , dict , hour , min ) :
min = int ( min ) * 100 / 60
return " %s . %i " % ( hour , min )
2011-05-17 13:42:22 +00:00
2011-06-03 14:19:11 +00:00
"""
import Documents
"""
2011-06-28 05:10:01 +00:00
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 )
2011-06-28 09:32:01 +00:00
if sugar_document_account :
res_id = self . get_mapped_id ( self . TABLE_ACCOUNT , sugar_document_account [ 0 ] )
2011-06-28 08:55:06 +00:00
res_model = ' res.partner '
2011-06-28 09:32:01 +00:00
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 '
2011-06-28 05:10:01 +00:00
return res_id , res_model
2011-06-03 14:19:11 +00:00
def import_document ( self , val ) :
2011-06-28 05:10:01 +00:00
File , Filename = sugar . get_document_revision_search ( self . context . get ( ' port ' ) , self . context . get ( ' session_id ' ) , val . get ( ' document_revision_id ' ) )
2011-07-06 12:52:33 +00:00
File = base64 . encodestring ( File )
2011-06-28 07:34:35 +00:00
res_id , res_model = self . import_related_document ( val )
2011-06-28 08:55:06 +00:00
val [ ' res_id ' ] = res_id
val [ ' res_model ' ] = res_model
2011-06-28 05:10:01 +00:00
if File :
val [ ' datas ' ] = File
val [ ' datas_fname ' ] = Filename
2011-06-03 14:19:11 +00:00
return val
def get_document_mapping ( self ) :
return {
' model ' : ' ir.attachment ' ,
2011-07-08 07:16:11 +00:00
' dependencies ' : [ self . TABLE_USER ] ,
2011-06-03 14:19:11 +00:00
' hook ' : self . import_document ,
2011-07-06 12:52:33 +00:00
' map ' : {
' name ' : ' document_name ' ,
2011-06-03 14:19:11 +00:00
' description ' : ppconcat ( ' description ' ) ,
' datas ' : ' datas ' ,
' datas_fname ' : ' datas_fname ' ,
2011-06-28 05:10:01 +00:00
' res_model ' : ' res_model ' ,
2011-06-28 07:34:35 +00:00
' res_id ' : ' res_id ' ,
2011-06-03 14:19:11 +00:00
}
}
2011-05-30 11:09:01 +00:00
"""
import Emails
"""
2011-06-24 12:18:29 +00:00
2011-05-17 13:42:22 +00:00
def import_email ( self , val ) :
2011-06-06 05:21:05 +00:00
vals = sugar . email_search ( self . context . get ( ' port ' ) , self . context . get ( ' session_id ' ) , self . TABLE_EMAIL , val . get ( ' id ' ) )
2011-05-17 13:42:22 +00:00
model_obj = self . obj . pool . get ( ' ir.model.data ' )
2011-06-06 05:21:05 +00:00
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
2011-05-17 13:42:22 +00:00
return val
def get_email_mapping ( self ) :
return {
' model ' : ' mailgate.message ' ,
2011-07-08 07:16:11 +00:00
' dependencies ' : [ self . TABLE_USER , self . TABLE_ACCOUNT , self . TABLE_CONTACT , self . TABLE_LEAD , self . TABLE_OPPORTUNITY , self . TABLE_MEETING , self . TABLE_CALL ] ,
2011-05-17 13:42:22 +00:00
' hook ' : self . import_email ,
' map ' : { ' name ' : ' name ' ,
2011-05-26 16:25:39 +00:00
' history ' : const ( " 1 " ) ,
2011-05-17 13:42:22 +00:00
' date ' : ' date_sent ' ,
' email_from ' : ' from_addr_name ' ,
2011-06-06 05:21:05 +00:00
' email_to ' : ' to_addrs_names ' ,
2011-05-17 13:42:22 +00:00
' 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 ' ) ,
2011-05-19 12:19:39 +00:00
' description ' : ppconcat ( ' description ' , ' description_html ' ) ,
2011-05-17 13:42:22 +00:00
}
}
2011-05-17 03:54:12 +00:00
"""
import History ( Notes )
"""
2011-06-24 12:18:29 +00:00
2011-05-17 03:54:12 +00:00
def import_history ( self , val ) :
model_obj = self . obj . pool . get ( ' ir.model.data ' )
2011-05-17 13:42:22 +00:00
xml_id = self . xml_id_exist ( val . get ( ' parent_type ' ) , val . get ( ' parent_id ' ) )
2011-05-17 03:54:12 +00:00
model_ids = model_obj . search ( self . cr , self . uid , [ ( ' name ' , ' like ' , xml_id ) ] )
if model_ids :
2011-05-26 16:25:39 +00:00
model = model_obj . browse ( self . cr , self . uid , model_ids ) [ 0 ]
if model . model == ' res.partner ' :
val [ ' partner_id/.id ' ] = model . res_id
2011-06-24 12:18:29 +00:00
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
2011-05-17 03:54:12 +00:00
return val
def get_history_mapping ( self ) :
return {
2011-06-24 12:18:29 +00:00
' model ' : ' ir.attachment ' ,
' 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 , self . TABLE_EMAIL ] ,
2011-05-17 03:54:12 +00:00
' hook ' : self . import_history ,
' map ' : {
' name ' : ' name ' ,
2011-06-24 12:18:29 +00:00
' user_id/id ' : ref ( self . TABLE_USER , ' created_by ' ) ,
2011-05-19 12:19:39 +00:00
' description ' : ppconcat ( ' description ' , ' description_html ' ) ,
2011-05-17 03:54:12 +00:00
' res_id ' : ' res_id ' ,
2011-06-24 12:18:29 +00:00
' res_model ' : ' model ' ,
2011-05-17 03:54:12 +00:00
' partner_id/.id ' : ' partner_id/.id ' ,
2011-06-24 12:18:29 +00:00
' datas ' : ' datas ' ,
' datas_fname ' : ' datas_fname '
2011-05-17 03:54:12 +00:00
}
}
2011-05-16 12:53:27 +00:00
"""
import Claims ( Cases )
"""
def get_claim_priority ( self , val ) :
priority_dict = {
2011-05-26 16:25:39 +00:00
' P1 ' : ' 2 ' ,
' P2 ' : ' 3 ' ,
' P3 ' : ' 4 '
2011-05-16 12:53:27 +00:00
}
return priority_dict . get ( val . get ( ' priority ' ) , ' ' )
def get_contact_info_from_account ( self , val ) :
partner_id = self . get_mapped_id ( self . TABLE_ACCOUNT , val . get ( ' account_id ' ) )
partner_address_id = False
partner_phone = False
partner_email = False
partner = self . obj . pool . get ( ' res.partner ' ) . browse ( self . cr , self . uid , [ partner_id ] ) [ 0 ]
if partner . address and partner . address [ 0 ] :
address = partner . address [ 0 ]
partner_address_id = address . id
partner_phone = address . phone
partner_email = address . email
return partner_address_id , partner_phone , partner_email
def import_crm_claim ( self , val ) :
partner_address_id , partner_phone , partner_email = self . get_contact_info_from_account ( val )
val [ ' partner_address_id/.id ' ] = partner_address_id
val [ ' partner_phone ' ] = partner_phone
val [ ' email_from ' ] = partner_email
return val
def get_crm_claim_mapping ( self ) :
return {
' model ' : ' crm.claim ' ,
' dependencies ' : [ self . TABLE_USER , self . TABLE_ACCOUNT , self . TABLE_CONTACT , self . TABLE_LEAD ] ,
' hook ' : self . import_crm_claim ,
' map ' : {
2011-05-26 16:25:39 +00:00
' name ' : concat ( ' case_number ' , ' name ' , delimiter = ' - ' ) ,
2011-05-16 12:53:27 +00:00
' date ' : ' date_entered ' ,
' user_id/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
2011-05-26 16:25:39 +00:00
' description ' : ppconcat ( ' description ' , ' resolution ' , ' work_log ' ) ,
2011-05-16 12:53:27 +00:00
' partner_id/id ' : ref ( self . TABLE_ACCOUNT , ' account_id ' ) ,
' partner_address_id/.id ' : ' partner_address_id/.id ' ,
2011-06-06 05:21:05 +00:00
' categ_id/id ' : call ( self . get_category , ' crm.claim ' , value ( ' type ' ) ) ,
2011-05-16 12:53:27 +00:00
' 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 ' ,
2011-05-26 16:25:39 +00:00
' dependencies ' : [ self . TABLE_USER ] ,
2011-05-16 12:53:27 +00:00
' map ' : {
2011-05-26 16:25:39 +00:00
' name ' : concat ( ' bug_number ' , ' name ' , delimiter = ' - ' ) ,
2011-05-16 12:53:27 +00:00
' project_id/id ' : call ( self . get_bug_project_id , ' sugarcrm_bugs ' ) ,
' categ_id/id ' : call ( self . get_category , ' project.issue ' , value ( ' type ' ) ) ,
2011-05-26 16:25:39 +00:00
' description ' : ppconcat ( ' description ' , ' source ' , ' resolution ' , ' work_log ' , ' found_in_release ' , ' release_name ' , ' fixed_in_release_name ' , ' fixed_in_release ' ) ,
2011-05-16 12:53:27 +00:00
' priority ' : self . get_project_issue_priority ,
2011-05-26 16:25:39 +00:00
' state ' : map_val ( ' status ' , self . project_issue_state ) ,
' assigned_to/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
2011-05-16 12:53:27 +00:00
}
}
2011-05-16 02:23:46 +00:00
"""
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 ) :
2011-05-26 16:25:39 +00:00
priority_dict = {
2011-05-16 02:23:46 +00:00
' High ' : ' 0 ' ,
' Medium ' : ' 2 ' ,
' Low ' : ' 3 '
}
2011-05-26 16:25:39 +00:00
return priority_dict . get ( val . get ( ' priority ' ) , ' ' )
2011-05-18 06:44:47 +00:00
2011-05-16 02:23:46 +00:00
def get_project_task_mapping ( self ) :
return {
' model ' : ' project.task ' ,
' dependencies ' : [ self . TABLE_USER , self . TABLE_PROJECT ] ,
' map ' : {
' name ' : ' name ' ,
' date_start ' : ' date_start ' ,
' date_end ' : ' date_finish ' ,
' project_id/id ' : ref ( self . TABLE_PROJECT , ' project_id ' ) ,
2011-05-26 16:25:39 +00:00
' planned_hours ' : ' estimated_effort ' ,
2011-05-16 02:23:46 +00:00
' priority ' : self . get_project_task_priority ,
2011-05-26 16:25:39 +00:00
' description ' : ppconcat ( ' description ' , ' milestone_flag ' , ' project_task_id ' , ' task_number ' , ' percent_complete ' ) ,
2011-05-16 02:23:46 +00:00
' 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 )
}
}
2011-05-13 13:46:48 +00:00
"""
import Projects
"""
project_state = {
' Draft ' : ' draft ' ,
' In Review ' : ' open ' ,
' Published ' : ' close '
}
2011-05-26 16:25:39 +00:00
2011-05-13 13:46:48 +00:00
def import_project_account ( self , val ) :
partner_id = False
partner_invoice_id = False
2011-05-16 02:23:46 +00:00
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 )
2011-05-13 13:46:48 +00:00
for account_id in sugar_project_account :
partner_id = self . get_mapped_id ( self . TABLE_ACCOUNT , account_id )
2011-05-16 02:23:46 +00:00
return partner_id , partner_invoice_id
2011-05-13 13:46:48 +00:00
def import_project ( self , val ) :
2011-05-16 02:23:46 +00:00
partner_id , partner_invoice_id = self . import_project_account ( val )
2011-05-13 13:46:48 +00:00
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 '
}
2011-05-16 02:23:46 +00:00
def import_task ( self , val ) :
2011-05-16 12:53:27 +00:00
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
2011-05-16 02:23:46 +00:00
2011-05-13 13:46:48 +00:00
def get_task_mapping ( self ) :
return {
' model ' : ' crm.meeting ' ,
' dependencies ' : [ self . TABLE_CONTACT , self . TABLE_ACCOUNT , self . TABLE_USER ] ,
2011-05-16 12:53:27 +00:00
' hook ' : self . import_task ,
2011-05-13 13:46:48 +00:00
' map ' : {
' name ' : ' name ' ,
2011-05-16 02:23:46 +00:00
' date ' : ' date ' ,
' date_deadline ' : ' date_deadline ' ,
2011-05-13 13:46:48 +00:00
' user_id/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
2011-05-16 02:23:46 +00:00
' categ_id/id ' : call ( self . get_category , ' crm.meeting ' , const ( ' Tasks ' ) ) ,
2011-05-13 13:46:48 +00:00
' partner_id/id ' : related_ref ( self . TABLE_ACCOUNT ) ,
2011-05-16 02:23:46 +00:00
' partner_address_id/id ' : ref ( self . TABLE_CONTACT , ' contact_id ' ) ,
2011-05-13 13:46:48 +00:00
' state ' : map_val ( ' status ' , self . task_state )
}
}
"""
import Calls
2011-05-26 16:25:39 +00:00
"""
#TODO adapt with project trunk-crm-imp
2011-05-13 13:46:48 +00:00
call_state = {
' Planned ' : ' open ' ,
' Held ' : ' done ' ,
' Not Held ' : ' pending ' ,
}
2011-05-16 02:23:46 +00:00
2011-05-13 13:46:48 +00:00
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 ) ,
2011-05-19 12:19:39 +00:00
' description ' : ppconcat ( ' description ' ) ,
2011-05-13 13:46:48 +00:00
' state ' : map_val ( ' status ' , self . call_state )
}
2011-05-26 07:32:28 +00:00
}
2011-05-12 08:56:24 +00:00
"""
import meeting
"""
meeting_state = {
2011-05-05 17:10:37 +00:00
' Planned ' : ' draft ' ,
' Held ' : ' open ' ,
' Not Held ' : ' draft ' ,
}
2011-05-18 03:24:52 +00:00
#TODO
def get_attendee_id ( self , cr , uid , module_name , module_id ) :
2011-05-19 12:19:39 +00:00
contact_id = False
user_id = False
attendee_id = [ ]
2011-05-19 03:12:37 +00:00
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 :
2011-05-19 12:19:39 +00:00
user_id = self . xml_id_exist ( self . TABLE_USER , attendee . get ( ' id ' , False ) )
2011-05-30 16:59:08 +00:00
contact_id = False
if not user_id :
2011-05-19 12:19:39 +00:00
contact_id = self . xml_id_exist ( self . TABLE_CONTACT , attendee . get ( ' id ' , False ) )
fields = [ ' user_id/id ' , ' email ' , ' partner_address_id/id ' ]
2011-05-30 16:59:08 +00:00
data = [ user_id , attendee . get ( ' email1 ' ) , contact_id ]
2011-05-19 12:19:39 +00:00
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 )
2011-05-12 08:56:24 +00:00
def get_alarm_id ( self , dict_val , val ) :
alarm_dict = {
' 60 ' : ' 1 minute before ' ,
' 300 ' : ' 5 minutes before ' ,
' 600 ' : ' 10 minutes before ' ,
' 900 ' : ' 15 minutes before ' ,
' 1800 ' : ' 30 minutes before ' ,
' 3600 ' : ' 1 hour before ' ,
}
return self . mapped_id_if_exist ( ' res.alarm ' , [ ( ' name ' , ' like ' , alarm_dict . get ( val ) ) ] , ' alarm ' , val )
#TODO attendees
2011-05-26 07:32:28 +00:00
2011-05-18 03:24:52 +00:00
def import_meeting ( self , val ) :
2011-05-19 03:12:37 +00:00
attendee_id = self . get_attendee_id ( self . cr , self . uid , ' Meetings ' , val . get ( ' id ' ) ) #TODO
2011-05-18 03:24:52 +00:00
val [ ' attendee_ids/id ' ] = attendee_id
return val
2011-05-12 08:56:24 +00:00
def get_meeting_mapping ( self ) :
return {
' model ' : ' crm.meeting ' ,
2011-05-26 07:32:28 +00:00
' dependencies ' : [ self . TABLE_CONTACT , self . TABLE_OPPORTUNITY , self . TABLE_LEAD , self . TABLE_TASK ] ,
2011-05-18 03:24:52 +00:00
' hook ' : self . import_meeting ,
2011-05-12 08:56:24 +00:00
' map ' : {
' name ' : ' name ' ,
' date ' : ' date_start ' ,
' duration ' : call ( self . get_float_time , value ( ' duration_hours ' ) , value ( ' duration_minutes ' ) ) ,
' location ' : ' location ' ,
2011-05-18 03:24:52 +00:00
' attendee_ids/id ' : ' attendee_ids/id ' ,
2011-05-12 08:56:24 +00:00
' alarm_id/id ' : call ( self . get_alarm_id , value ( ' reminder_time ' ) ) ,
' user_id/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
' partner_id/id ' : related_ref ( self . TABLE_ACCOUNT ) ,
' partner_address_id/id ' : related_ref ( self . TABLE_CONTACT ) ,
' state ' : map_val ( ' status ' , self . meeting_state )
}
2011-03-24 04:29:43 +00:00
}
2011-05-04 10:03:29 +00:00
2011-05-12 08:56:24 +00:00
"""
import Opportunity
"""
2011-05-18 06:44:47 +00:00
opp_state = {
' Need Analysis ' : ' New ' ,
' Closed Lost ' : ' Lost ' ,
' Closed Won ' : ' Won ' ,
' Value Proposition ' : ' Proposition ' ,
' Negotiation/Review ' : ' Negotiation '
}
2011-05-12 08:56:24 +00:00
def get_opportunity_status ( self , sugar_val ) :
fields = [ ' name ' , ' type ' ]
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 ' ] ) ] )
2011-05-04 10:03:29 +00:00
2011-05-12 08:56:24 +00:00
def import_opportunity_contact ( self , val ) :
sugar_opportunities_contact = set ( sugar . relation_search ( self . context . get ( ' port ' ) , self . context . get ( ' session_id ' ) , ' Opportunities ' , module_id = val . get ( ' id ' ) , related_module = ' Contacts ' , query = None , deleted = None ) )
partner_contact_id = False
partner_contact_email = False
partner_address_obj = self . obj . pool . get ( ' res.partner.address ' )
partner_xml_id = self . name_exist ( self . TABLE_ACCOUNT , val [ ' account_name ' ] , ' res.partner ' )
for contact in sugar_opportunities_contact :
address_id = self . get_mapped_id ( self . TABLE_CONTACT , contact )
if address_id :
address = partner_address_obj . browse ( self . cr , self . uid , address_id )
partner_name = address . partner_id and address . partner_id . name or False
if not partner_name : #link with partner id
fields = [ ' partner_id/id ' ]
data = [ partner_xml_id ]
self . import_object ( fields , data , ' res.partner.address ' , self . TABLE_CONTACT , contact , self . DO_NOT_FIND_DOMAIN )
if not partner_name or partner_name == val . get ( ' account_name ' ) :
partner_contact_id = self . xml_id_exist ( self . TABLE_CONTACT , contact )
partner_contact_email = address . email
return partner_contact_id , partner_contact_email
2011-05-05 17:10:37 +00:00
2011-05-12 08:56:24 +00:00
def import_opp ( self , val ) :
2011-05-26 07:32:28 +00:00
partner_contact_id , partner_contact_email = self . import_opportunity_contact ( val )
val [ ' partner_address_id/id ' ] = partner_contact_id
2011-05-12 08:56:24 +00:00
val [ ' email_from ' ] = partner_contact_email
return val
2011-04-04 08:34:41 +00:00
2011-05-12 08:56:24 +00:00
def get_opp_mapping ( self ) :
return {
' model ' : ' crm.lead ' ,
2011-05-19 03:12:37 +00:00
' dependencies ' : [ self . TABLE_USER , self . TABLE_ACCOUNT , self . TABLE_CONTACT , self . TABLE_COMPAIGN ] ,
2011-05-12 08:56:24 +00:00
' hook ' : self . import_opp ,
' map ' : {
' name ' : ' name ' ,
' probability ' : ' probability ' ,
' partner_id/id ' : refbyname ( self . TABLE_ACCOUNT , ' account_name ' , ' res.partner ' ) ,
' title_action ' : ' next_step ' ,
' partner_address_id/id ' : ' partner_address_id/id ' ,
' planned_revenue ' : ' amount ' ,
' date_deadline ' : ' date_closed ' ,
' user_id/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
' stage_id/id ' : self . get_opportunity_status ,
' type ' : const ( ' opportunity ' ) ,
' categ_id/id ' : call ( self . get_category , ' crm.lead ' , value ( ' opportunity_type ' ) ) ,
' email_from ' : ' email_from ' ,
2011-05-18 06:44:47 +00:00
' state ' : map_val ( ' status ' , self . opp_state ) , #TODO
2011-05-12 08:56:24 +00:00
}
}
2011-05-18 06:44:47 +00:00
"""
import campaign
"""
2011-04-21 08:35:01 +00:00
2011-05-18 06:44:47 +00:00
def get_compaign_mapping ( self ) :
return {
' model ' : ' crm.case.resource.type ' ,
' map ' : {
' name ' : ' name ' ,
}
}
2011-05-12 08:56:24 +00:00
"""
import lead
"""
def get_lead_status ( self , sugar_val ) :
fields = [ ' name ' , ' type ' ]
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 ' , ' ' ) ) ] )
lead_state = {
' New ' : ' draft ' ,
' Assigned ' : ' open ' ,
' In Progress ' : ' open ' ,
' Recycled ' : ' cancel ' ,
' 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
2011-05-04 10:03:29 +00:00
return False
2011-05-12 08:56:24 +00:00
if val . get ( ' primary_address_country ' ) :
country_id = self . get_all_countries ( val . get ( ' primary_address_country ' ) )
val [ ' country_id/id ' ] = country_id
val [ ' state_id/id ' ] = self . get_all_states ( val . get ( ' primary_address_state ' ) , country_id )
return val
2011-04-21 08:35:01 +00:00
2011-05-12 08:56:24 +00:00
def get_lead_mapping ( self ) :
return {
' model ' : ' crm.lead ' ,
2011-05-25 15:55:42 +00:00
' dependencies ' : [ self . TABLE_COMPAIGN , self . TABLE_USER ] ,
2011-05-12 08:56:24 +00:00
' hook ' : self . import_lead ,
' map ' : {
' name ' : concat ( ' first_name ' , ' last_name ' ) ,
' contact_name ' : concat ( ' first_name ' , ' last_name ' ) ,
' description ' : ppconcat ( ' description ' , ' refered_by ' , ' lead_source ' , ' lead_source_description ' , ' website ' , ' email2 ' , ' status_description ' , ' lead_source_description ' , ' do_not_call ' ) ,
' partner_name ' : ' account_name ' ,
' email_from ' : ' email1 ' ,
' phone ' : ' phone_work ' ,
' mobile ' : ' phone_mobile ' ,
' title/id ' : call ( self . get_job_title , value ( ' salutation ' ) ) ,
' function ' : ' title ' ,
' street ' : ' primary_address_street ' ,
' street2 ' : ' alt_address_street ' ,
' zip ' : ' primary_address_postalcode ' ,
' city ' : ' primary_address_city ' ,
' user_id/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
' stage_id/id ' : self . get_lead_status ,
' type ' : const ( ' lead ' ) ,
' state ' : map_val ( ' status ' , self . lead_state ) ,
' fax ' : ' phone_fax ' ,
' referred ' : ' refered_by ' ,
' optout ' : ' do_not_call ' ,
2011-05-18 06:44:47 +00:00
' channel_id/id ' : call ( self . get_channel_id , value ( ' lead_source ' ) ) ,
' type_id/id ' : ref ( self . TABLE_COMPAIGN , ' campaign_id ' ) ,
2011-05-12 08:56:24 +00:00
' country_id/id ' : ' country_id/id ' ,
' state_id/id ' : ' state_id/id '
}
}
2011-04-21 08:35:01 +00:00
2011-05-12 08:56:24 +00:00
"""
import contact
"""
2011-06-03 14:19:11 +00:00
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 )
2011-05-12 08:56:24 +00:00
def import_contact ( self , val ) :
if val . get ( ' primary_address_country ' ) :
country_id = self . get_all_countries ( val . get ( ' primary_address_country ' ) )
state = self . get_all_states ( val . get ( ' primary_address_state ' ) , country_id )
val [ ' country_id/id ' ] = country_id
2011-05-20 07:30:59 +00:00
val [ ' state_id/id ' ] = state
2011-05-12 08:56:24 +00:00
return val
def get_contact_mapping ( self ) :
return {
' model ' : ' res.partner.address ' ,
' dependencies ' : [ self . TABLE_ACCOUNT ] ,
' hook ' : self . import_contact ,
' map ' : {
' name ' : concat ( ' first_name ' , ' last_name ' ) ,
' partner_id/id ' : ref ( self . TABLE_ACCOUNT , ' account_id ' ) ,
' phone ' : ' phone_work ' ,
' mobile ' : ' phone_mobile ' ,
' fax ' : ' phone_fax ' ,
' function ' : ' title ' ,
' street ' : ' primary_address_street ' ,
' zip ' : ' primary_address_postalcode ' ,
' city ' : ' primary_address_city ' ,
' country_id/id ' : ' country_id/id ' ,
' state_id/id ' : ' state_id/id ' ,
2011-06-03 14:19:11 +00:00
' email ' : self . get_email ,
2011-05-12 08:56:24 +00:00
' type ' : const ( ' contact ' )
}
}
2011-05-04 10:03:29 +00:00
2011-05-12 08:56:24 +00:00
"""
import Account
"""
2011-06-03 14:19:11 +00:00
2011-05-12 08:56:24 +00:00
def get_address_type ( self , val , type ) :
2011-03-29 04:34:57 +00:00
if type == ' invoice ' :
type_address = ' billing '
else :
2011-06-03 14:19:11 +00:00
type_address = ' shipping '
if type == ' default ' :
2011-06-24 12:18:29 +00:00
map_partner_address = {
' name ' : ' name ' ,
' type ' : const ( ' default ' ) ,
' email ' : ' email1 '
2011-03-24 04:29:43 +00:00
}
2011-06-03 14:19:11 +00:00
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 ' ,
}
2011-04-14 10:39:21 +00:00
if val . get ( type_address + ' _address_country ' ) :
2011-05-12 08:56:24 +00:00
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 )
2011-05-02 14:51:13 +00:00
val [ ' country_id/id ' ] = country_id
val [ ' state_id/id ' ] = state
2011-06-03 14:19:11 +00:00
2011-05-04 10:03:29 +00:00
val [ ' type ' ] = type
val [ ' id_new ' ] = val [ ' id ' ] + ' _address_ ' + type
2011-05-12 08:56:24 +00:00
return self . import_object_mapping ( map_partner_address , val , ' res.partner.address ' , self . TABLE_CONTACT , val [ ' id_new ' ] , self . DO_NOT_FIND_DOMAIN )
2011-06-03 14:19:11 +00:00
2011-05-12 08:56:24 +00:00
def get_partner_address ( self , val ) :
2011-05-04 10:03:29 +00:00
address_id = [ ]
2011-06-03 14:19:11 +00:00
type_dict = { ' billing_address_street ' : ' invoice ' , ' shipping_address_street ' : ' delivery ' , ' type ' : ' default ' }
2011-05-04 10:03:29 +00:00
for key , type_value in type_dict . items ( ) :
if val . get ( key ) :
2011-05-12 08:56:24 +00:00
id = self . get_address_type ( val , type_value )
2011-05-04 10:03:29 +00:00
address_id . append ( id )
2011-05-12 08:56:24 +00:00
return ' , ' . join ( address_id )
2011-03-29 04:34:57 +00:00
2011-05-12 08:56:24 +00:00
def get_partner_mapping ( self ) :
return {
' model ' : ' res.partner ' ,
' dependencies ' : [ self . TABLE_USER ] ,
' map ' : {
2011-04-06 07:32:08 +00:00
' name ' : ' name ' ,
2011-05-12 08:56:24 +00:00
' website ' : ' website ' ,
' user_id/id ' : ref ( self . TABLE_USER , ' assigned_user_id ' ) ,
' ref ' : ' sic_code ' ,
' comment ' : ppconcat ( ' description ' , ' employees ' , ' ownership ' , ' annual_revenue ' , ' rating ' , ' industry ' , ' ticker_symbol ' ) ,
' customer ' : const ( ' 1 ' ) ,
' supplier ' : const ( ' 0 ' ) ,
' address/id ' : ' address/id ' ,
' parent_id/id_parent ' : ' parent_id ' ,
' address/id ' : self . get_partner_address ,
}
2011-05-05 17:10:37 +00:00
}
2011-04-25 05:48:20 +00:00
2011-05-12 08:56:24 +00:00
"""
import Employee
"""
def get_ressource ( self , val ) :
2011-05-04 10:03:29 +00:00
map_resource = {
2011-05-12 08:56:24 +00:00
' name ' : concat ( ' first_name ' , ' last_name ' ) ,
2011-05-04 13:34:16 +00:00
}
2011-05-12 08:56:24 +00:00
return self . import_object_mapping ( map_resource , val , ' resource.resource ' , self . TABLE_RESSOURCE , val [ ' id ' ] , self . DO_NOT_FIND_DOMAIN )
def get_job_id ( self , val ) :
fields = [ ' name ' ]
data = [ val . get ( ' title ' ) ]
return self . import_object ( fields , data , ' hr.job ' , ' hr_job ' , val . get ( ' title ' ) )
def get_user_address ( self , val ) :
2011-05-04 10:03:29 +00:00
map_user_address = {
2011-05-12 08:56:24 +00:00
' name ' : concat ( ' first_name ' , ' last_name ' ) ,
2011-05-04 10:03:29 +00:00
' city ' : ' address_city ' ,
' country_id/id ' : ' country_id/id ' ,
' state_id/id ' : ' state_id/id ' ,
' street ' : ' address_street ' ,
' zip ' : ' address_postalcode ' ,
2011-05-25 15:55:42 +00:00
' fax ' : ' phone_fax ' ,
2011-05-18 06:44:47 +00:00
' phone ' : ' phone_work ' ,
' mobile ' : ' phone_mobile ' ,
' email ' : ' email1 '
2011-05-04 10:03:29 +00:00
}
if val . get ( ' address_country ' ) :
2011-05-12 08:56:24 +00:00
country_id = self . get_all_countries ( val . get ( ' address_country ' ) )
state_id = self . get_all_states ( val . get ( ' address_state ' ) , country_id )
2011-05-04 10:03:29 +00:00
val [ ' country_id/id ' ] = country_id
val [ ' state_id/id ' ] = state_id
2011-05-04 13:34:16 +00:00
2011-05-12 08:56:24 +00:00
return self . import_object_mapping ( map_user_address , val , ' res.partner.address ' , self . TABLE_CONTACT , val [ ' id ' ] , self . DO_NOT_FIND_DOMAIN )
2011-05-18 06:44:47 +00:00
2011-05-12 08:56:24 +00:00
def get_employee_mapping ( self ) :
return {
' model ' : ' hr.employee ' ,
' dependencies ' : [ self . TABLE_USER ] ,
' map ' : {
' resource_id/id ' : self . get_ressource ,
' name ' : concat ( ' first_name ' , ' last_name ' ) ,
' work_phone ' : ' phone_work ' ,
' mobile_phone ' : ' phone_mobile ' ,
' user_id/id ' : ref ( self . TABLE_USER , ' id ' ) ,
' address_home_id/id ' : self . get_user_address ,
2011-05-18 06:44:47 +00:00
' notes ' : ppconcat ( ' messenger_type ' , ' messenger_id ' , ' description ' ) ,
2011-05-12 08:56:24 +00:00
' job_id/id ' : self . get_job_id ,
2011-05-25 15:55:42 +00:00
' work_email ' : ' email1 ' ,
' coach_id/id_parent ' : ' reports_to_id ' ,
2011-05-12 08:56:24 +00:00
}
}
"""
import user
"""
def import_user ( self , val ) :
user_obj = self . obj . pool . get ( ' res.users ' )
user_ids = user_obj . search ( self . cr , self . uid , [ ( ' login ' , ' = ' , val . get ( ' user_name ' ) ) ] )
if user_ids :
val [ ' .id ' ] = str ( user_ids [ 0 ] )
else :
val [ ' password ' ] = ' sugarcrm ' #default password for all user #TODO needed in documentation
val [ ' context_lang ' ] = self . context . get ( ' lang ' , ' en_US ' )
return val
2011-05-05 17:10:37 +00:00
2011-05-12 08:56:24 +00:00
def get_users_department ( self , val ) :
dep = val . get ( ' department ' )
fields = [ ' name ' ]
data = [ dep ]
if not dep :
return False
return self . import_object ( fields , data , ' hr.department ' , ' hr_department_user ' , dep )
2011-05-04 10:03:29 +00:00
2011-05-12 08:56:24 +00:00
def get_user_mapping ( self ) :
return {
' model ' : ' res.users ' ,
' hook ' : self . import_user ,
' map ' : {
' name ' : concat ( ' first_name ' , ' last_name ' ) ,
' login ' : ' user_name ' ,
' context_lang ' : ' context_lang ' ,
' password ' : ' password ' ,
' .id ' : ' .id ' ,
' context_department_id/id ' : self . get_users_department ,
2011-05-25 14:01:06 +00:00
' user_email ' : ' email1 ' ,
2011-05-12 08:56:24 +00:00
}
}
2011-03-31 14:02:39 +00:00
2011-05-25 15:55:42 +00:00
2011-05-12 08:56:24 +00:00
def get_mapping ( self ) :
return {
self . TABLE_USER : self . get_user_mapping ( ) ,
self . TABLE_EMPLOYEE : self . get_employee_mapping ( ) ,
self . TABLE_ACCOUNT : self . get_partner_mapping ( ) ,
self . TABLE_CONTACT : self . get_contact_mapping ( ) ,
self . TABLE_LEAD : self . get_lead_mapping ( ) ,
self . TABLE_OPPORTUNITY : self . get_opp_mapping ( ) ,
self . TABLE_MEETING : self . get_meeting_mapping ( ) ,
2011-05-13 13:46:48 +00:00
self . TABLE_CALL : self . get_calls_mapping ( ) ,
self . TABLE_TASK : self . get_task_mapping ( ) ,
self . TABLE_PROJECT : self . get_project_mapping ( ) ,
2011-05-16 12:53:27 +00:00
self . TABLE_PROJECT_TASK : self . get_project_task_mapping ( ) ,
self . TABLE_BUG : self . get_project_issue_mapping ( ) ,
2011-05-17 03:54:12 +00:00
self . TABLE_CASE : self . get_crm_claim_mapping ( ) ,
2011-05-17 13:42:22 +00:00
self . TABLE_NOTE : self . get_history_mapping ( ) ,
self . TABLE_EMAIL : self . get_email_mapping ( ) ,
2011-06-03 14:19:11 +00:00
self . TABLE_DOCUMENT : self . get_document_mapping ( ) ,
2011-05-18 06:44:47 +00:00
self . TABLE_COMPAIGN : self . get_compaign_mapping ( )
2011-05-12 08:56:24 +00:00
}
2011-05-25 15:55:42 +00:00
"""
Email notification
"""
2011-05-25 13:29:14 +00:00
def get_email_subject ( self , result ) :
return " your sugarcrm data were successfully imported at %s " % self . date_ended
def get_body_header ( self , result ) :
return " Sugarcrm import : report of last import "
2011-04-27 16:13:55 +00:00
2011-03-24 04:29:43 +00:00
class import_sugarcrm ( osv . osv ) :
""" Import SugarCRM DATA """
2011-03-29 04:34:57 +00:00
2011-03-24 04:29:43 +00:00
_name = " import.sugarcrm "
_description = __doc__
_columns = {
2011-05-19 03:12:37 +00:00
' username ' : fields . char ( ' User Name ' , size = 64 , required = True ) ,
' password ' : fields . char ( ' Password ' , size = 24 , required = True ) ,
2011-05-27 13:04:28 +00:00
' url ' : fields . char ( ' SugarSoap Api url: ' , size = 264 , required = True , help = " Webservice ' s url where to get the data. \
2011-05-30 16:59:08 +00:00
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 " ),
2011-07-04 09:54:11 +00:00
' opportunity ' : fields . boolean ( ' Leads & Opp ' , help = " IfLeads & Opp are checked, SugarCRM leads and opportunities data are imported in OpenERP crm-Opportunity form " ) ,
2011-04-11 09:56:03 +00:00
' 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 " ) ,
2011-05-26 07:32:28 +00:00
' meeting ' : fields . boolean ( ' Meetings ' , help = " If Meetings is checked, SugarCRM Meetings and Meeting Tasks data imported in OpenERP meetings form " ) ,
2011-04-11 09:56:03 +00:00
' call ' : fields . boolean ( ' Calls ' , help = " If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form " ) ,
2011-05-26 07:32:28 +00:00
' claim ' : fields . boolean ( ' Cases ' , help = " If Cases is checked, SugarCRM Cases data imported in OpenERP Claims form " ) ,
2011-06-30 11:36:47 +00:00
' email_history ' : fields . boolean ( ' Email and Note ' , help = " If Email and History is checked, SugarCRM Notes, Attachment and Emails data imported in OpenERP ' s Related module ' s History with attachment " ) ,
2011-04-11 09:56:03 +00:00
' 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 " ) ,
2011-04-13 12:25:11 +00:00
' bug ' : fields . boolean ( ' Bugs ' , help = " If Bugs is checked, SugarCRM Bugs data imported in OpenERP Project Issues form " ) ,
2011-06-03 14:19:11 +00:00
' document ' : fields . boolean ( ' Documents ' , help = " If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form " ) ,
2011-05-19 03:12:37 +00:00
' 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. " ) ,
2011-03-24 04:29:43 +00:00
}
2011-06-28 05:10:01 +00:00
def _get_email_id ( self , cr , uid , context = None ) :
2011-07-06 12:52:33 +00:00
return self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . user_email
2011-07-08 07:16:11 +00:00
def get_all ( self , cr , uid , model , context = None ) :
obj_module = self . pool . get ( ' ir.module.module ' )
module_id = obj_module . search ( cr , uid , [ ( ' name ' , ' = ' , model ) ] )
2011-07-12 07:10:15 +00:00
flag = False
2011-07-08 07:16:11 +00:00
if module_id :
for id in module_id :
module_state = obj_module . browse ( cr , uid , id ) . state
if module_state == ' installed ' :
2011-07-12 07:10:15 +00:00
flag = True
2011-07-08 07:16:11 +00:00
else :
2011-07-12 07:10:15 +00:00
flag = False
return flag
2011-07-08 07:16:11 +00:00
def _get_project ( self , cr , uid , context = None ) :
return self . get_all ( cr , uid , ' project ' , context = context )
def _get_crm_claim ( self , cr , uid , context = None ) :
return self . get_all ( cr , uid , ' crm_claim ' , context = context )
def _get_project_issue ( self , cr , uid , context = None ) :
return self . get_all ( cr , uid , ' project_issue ' , context = context )
2011-06-28 05:10:01 +00:00
2011-04-25 05:48:20 +00:00
_defaults = { #to be set to true, but easier for debugging
2011-07-08 07:16:11 +00:00
' opportunity ' : True ,
2011-06-30 11:36:47 +00:00
' contact ' : True ,
' account ' : True ,
2011-07-08 07:16:11 +00:00
' employee ' : True ,
' meeting ' : True ,
' call ' : True ,
' claim ' : _get_crm_claim ,
' email_history ' : True ,
' project ' : _get_project ,
' project_task ' : _get_project ,
' bug ' : _get_project_issue ,
' document ' : True ,
2011-05-19 12:19:39 +00:00
' instance_name ' : ' sugarcrm ' ,
2011-06-28 05:10:01 +00:00
' email_from ' : _get_email_id ,
2011-07-06 12:52:33 +00:00
' username ' : ' admin ' ,
' password ' : ' ' ,
' url ' : " http://sugarcrm.example.com/soap.php "
2011-03-24 04:29:43 +00:00
}
2011-05-30 16:59:08 +00:00
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 ) :
2011-05-30 09:37:35 +00:00
if not context :
context = { }
url = context . get ( ' url ' )
2011-05-30 16:59:08 +00:00
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
2011-05-30 09:37:35 +00:00
2011-03-24 04:29:43 +00:00
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 = [ ]
2011-07-06 12:52:33 +00:00
list = [ ]
2011-03-24 04:29:43 +00:00
for current in self . browse ( cr , uid , ids , context ) :
2011-05-19 03:12:37 +00:00
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 } )
2011-03-24 04:29:43 +00:00
if current . contact :
key_list . append ( ' Contacts ' )
if current . account :
2011-07-06 12:52:33 +00:00
key_list . append ( ' Accounts ' )
if current . opportunity :
key_list . append ( ' Leads ' )
key_list . append ( ' Opportunities ' )
2011-03-24 13:46:24 +00:00
if current . employee :
2011-07-06 12:52:33 +00:00
key_list . append ( ' Employees ' )
2011-04-06 07:32:08 +00:00
if current . meeting :
key_list . append ( ' Meetings ' )
if current . call :
2011-04-06 13:22:20 +00:00
key_list . append ( ' Calls ' )
2011-04-25 05:48:20 +00:00
if current . claim :
2011-07-06 12:52:33 +00:00
key_list . append ( ' Cases ' )
list . append ( ' crm_claim ' )
2011-05-26 07:32:28 +00:00
if current . email_history :
2011-04-07 08:57:52 +00:00
key_list . append ( ' Emails ' )
2011-05-26 07:32:28 +00:00
key_list . append ( ' Notes ' )
2011-07-06 12:52:33 +00:00
list . append ( ' project ' )
2011-04-07 08:57:52 +00:00
if current . project :
2011-05-13 13:46:48 +00:00
key_list . append ( ' Project ' )
2011-07-06 12:52:33 +00:00
list . append ( ' project ' )
2011-04-07 08:57:52 +00:00
if current . project_task :
2011-05-16 02:23:46 +00:00
key_list . append ( ' ProjectTask ' )
2011-07-06 12:52:33 +00:00
list . append ( ' project ' )
2011-04-13 12:25:11 +00:00
if current . bug :
2011-04-14 12:51:54 +00:00
key_list . append ( ' Bugs ' )
2011-07-06 12:52:33 +00:00
list . append ( ' project_issue ' )
2011-06-03 14:19:11 +00:00
if current . document :
2011-06-28 05:10:01 +00:00
key_list . append ( ' Documents ' )
2011-07-06 12:52:33 +00:00
return key_list , list
2011-03-24 04:29:43 +00:00
2011-05-19 12:19:39 +00:00
2011-05-19 03:12:37 +00:00
def do_import_all ( self , cr , uid , * args ) :
"""
scheduler Method
"""
2011-05-25 13:29:14 +00:00
context = { ' username ' : args [ 4 ] , ' password ' : args [ 5 ] , ' url ' : args [ 3 ] , ' instance_name ' : args [ 3 ] }
imp = sugar_import ( self , cr , uid , args [ 2 ] , " import_sugarcrm " , [ args [ 1 ] ] , context )
2011-05-20 10:27:30 +00:00
imp . set_table_list ( args [ 0 ] )
imp . start ( )
2011-05-19 03:12:37 +00:00
return True
def import_from_scheduler_all ( self , cr , uid , ids , context = None ) :
2011-07-08 09:38:48 +00:00
keys , module_list = self . get_key ( cr , uid , ids , context )
set_list = set ( module_list )
model = list ( set_list )
2011-05-19 03:12:37 +00:00
if not keys :
2011-05-25 13:29:14 +00:00
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Select Module to Import. ' ) )
2011-07-12 07:10:15 +00:00
for module in model :
state = self . get_all ( cr , uid , module , context = context )
if state == False :
keys = ' , ' . join ( keys )
raise osv . except_osv ( _ ( ' Error !! ' ) , _ ( " %s data required %s Module to be installed, Please install %s module " ) % ( keys , module , module ) )
2011-05-19 03:12:37 +00:00
cron_obj = self . pool . get ( ' ir.cron ' )
2011-05-30 16:59:08 +00:00
url = self . parse_valid_url ( context )
2011-05-30 09:37:35 +00:00
args = ( keys , context . get ( ' email_user ' ) , context . get ( ' instance_name ' ) , url , context . get ( ' username ' ) , context . get ( ' password ' ) )
2011-05-25 13:29:14 +00:00
new_create_id = cron_obj . create ( cr , uid , { ' name ' : ' Import SugarCRM datas ' , ' interval_type ' : ' hours ' , ' interval_number ' : 1 , ' numbercall ' : - 1 , ' model ' : ' import.sugarcrm ' , ' function ' : ' do_import_all ' , ' args ' : args , ' active ' : False } )
return {
' name ' : ' SugarCRM Scheduler ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form,tree ' ,
' res_model ' : ' ir.cron ' ,
' res_id ' : new_create_id ,
' type ' : ' ir.actions.act_window ' ,
}
2011-07-08 07:16:11 +00:00
2011-03-24 04:29:43 +00:00
def import_all ( self , cr , uid , ids , context = None ) :
2011-05-12 08:56:24 +00:00
# """Import all sugarcrm data into openerp module"""
2011-07-06 12:52:33 +00:00
keys , module_list = self . get_key ( cr , uid , ids , context )
set_list = set ( module_list )
model = list ( set_list )
if not keys :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Select Module to Import. ' ) )
2011-07-12 07:10:15 +00:00
for module in model :
state = self . get_all ( cr , uid , module , context = context )
if state == False :
keys = ' , ' . join ( keys )
raise osv . except_osv ( _ ( ' Error !! ' ) , _ ( " %s data required %s Module to be installed, Please install %s module " ) % ( keys , module , module ) )
2011-05-30 16:59:08 +00:00
url = self . parse_valid_url ( context )
2011-05-30 09:37:35 +00:00
context . update ( { ' url ' : url } )
2011-05-25 13:29:14 +00:00
imp = sugar_import ( self , cr , uid , context . get ( ' instance_name ' ) , " import_sugarcrm " , [ context . get ( ' email_user ' ) ] , context )
2011-05-16 09:35:47 +00:00
imp . set_table_list ( keys )
imp . start ( )
2011-03-24 04:29:43 +00:00
obj_model = self . pool . get ( ' ir.model.data ' )
model_data_ids = obj_model . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' import.message.form ' ) ] )
resource_id = obj_model . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] )
return {
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' import.message ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2011-05-12 08:56:24 +00:00
2011-03-24 04:29:43 +00:00
import_sugarcrm ( )