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
from operator import itemgetter
import sugar
import sugarcrm_fields_mapping
2011-03-29 04:34:57 +00:00
from tools . translate import _
2011-05-04 10:03:29 +00:00
from import_framework import *
2011-03-24 04:29:43 +00:00
import pprint
2011-04-28 12:42:57 +00:00
import base64
2011-03-24 04:29:43 +00:00
pp = pprint . PrettyPrinter ( indent = 4 )
2011-05-04 10:03:29 +00:00
#print 'xml ids exist', sugar_obj.pool.get('ir.model.data').search(cr, uid, [('name', 'in', address_id )])
2011-05-02 14:51:13 +00:00
2011-04-26 05:03:45 +00:00
OPENERP_FIEDS_MAPS = { ' Leads ' : ' crm.lead ' ,
' Opportunities ' : ' crm.lead ' ,
' Contacts ' : ' res.partner.address ' ,
' Accounts ' : ' res.partner ' ,
' Resources ' : ' resource.resource ' ,
' Users ' : ' res.users ' ,
' Meetings ' : ' crm.meeting ' ,
' Calls ' : ' crm.phonecall ' ,
' Claims ' : ' crm.claim ' ,
' Employee ' : ' hr.employee ' ,
' Project ' : ' project.project ' ,
' ProjectTask ' : ' project.task ' ,
' Bugs ' : ' project.issue ' ,
' Documents ' : ' ir.attachment ' ,
}
2011-03-24 04:29:43 +00:00
2011-05-04 10:03:29 +00:00
TABLE_CONTACT = " contact "
TABLE_ACCOUNT = " account "
TABLE_USER = " user "
TABLE_EMPLOYEE = " employee "
TABLE_RESSOURCE = " resource "
2011-05-02 14:51:13 +00:00
2011-04-27 16:13:55 +00:00
2011-03-24 04:29:43 +00:00
2011-03-31 14:02:39 +00:00
def get_all_states ( sugar_obj , cr , uid , sugar_val , country_id , context = None ) :
2011-05-02 14:51:13 +00:00
""" Get states or create new state unless country_id is False """
state_code = sugar_val [ 0 : 3 ] #take the tree first char
fields = [ ' country_id/id ' , ' name ' , ' code ' ]
data = [ country_id , sugar_val , state_code ]
if country_id :
return import_object ( sugar_obj , cr , uid , fields , data , ' res.country.state ' , ' country_state ' , sugar_val , context = context )
2011-05-04 10:03:29 +00:00
2011-05-02 14:51:13 +00:00
return False
2011-04-07 08:57:52 +00:00
2011-03-31 14:02:39 +00:00
def get_all_countries ( sugar_obj , cr , uid , sugar_country_val , context = None ) :
2011-05-02 14:51:13 +00:00
""" Get Country, if no country match do not create anything, to avoid duplicate country code """
xml_id = generate_xml_id ( sugar_country_val , ' country ' )
return mapped_id_if_exist ( sugar_obj , cr , uid , ' res.country ' , [ ( ' name ' , ' ilike ' , sugar_country_val ) ] , xml_id , context = context )
2011-03-24 04:29:43 +00:00
def import_partner_address ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
2011-05-04 10:03:29 +00:00
2011-03-24 04:29:43 +00:00
map_partner_address = {
2011-05-04 10:03:29 +00:00
' id ' : ' id_new ' ,
' name ' : [ ' first_name ' , ' last_name ' ] ,
' partner_id/id ' : ' partner_id/id ' ,
2011-03-24 04:29:43 +00:00
' phone ' : ' phone_work ' ,
' mobile ' : ' phone_mobile ' ,
' fax ' : ' phone_fax ' ,
' function ' : ' title ' ,
' street ' : ' primary_address_street ' ,
' zip ' : ' primary_address_postalcode ' ,
' city ' : ' primary_address_city ' ,
2011-05-02 14:51:13 +00:00
' country_id/id ' : ' country_id/id ' ,
2011-05-04 10:03:29 +00:00
' state_id/id ' : ' state_id/id ' ,
2011-04-26 13:10:33 +00:00
' email ' : ' email ' ,
2011-04-26 05:03:45 +00:00
' type ' : ' type '
2011-03-24 04:29:43 +00:00
}
2011-05-04 10:03:29 +00:00
2011-03-24 04:29:43 +00:00
address_obj = sugar_obj . pool . get ( ' res.partner.address ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Contacts ' )
2011-05-04 10:03:29 +00:00
2011-03-24 04:29:43 +00:00
for val in sugar_data :
2011-05-04 10:03:29 +00:00
val [ ' id_new ' ] = generate_xml_id ( val [ ' id ' ] , TABLE_CONTACT )
val [ ' partner_id/id ' ] = xml_id_exist ( sugar_obj , cr , uid , TABLE_ACCOUNT , val . get ( ' account_id ' ) )
2011-04-26 05:03:45 +00:00
val [ ' type ' ] = ' contact '
2011-05-02 12:13:59 +00:00
val [ ' email ' ] = val . get ( ' email1 ' ) + ' , ' + val . get ( ' email2 ' )
2011-04-14 10:39:21 +00:00
if val . get ( ' primary_address_country ' ) :
country_id = get_all_countries ( sugar_obj , cr , uid , val . get ( ' primary_address_country ' ) , context )
state = get_all_states ( sugar_obj , cr , uid , val . get ( ' primary_address_state ' ) , country_id , context )
2011-05-02 14:51:13 +00:00
val [ ' country_id/id ' ] = country_id
val [ ' state_id/id ' ] = state
2011-03-24 04:29:43 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_partner_address )
2011-05-04 10:03:29 +00:00
print ' address '
pp . pprint ( val )
2011-05-02 14:51:13 +00:00
address_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-04 08:34:41 +00:00
return True
2011-04-21 08:35:01 +00:00
2011-03-24 04:29:43 +00:00
def import_users ( sugar_obj , cr , uid , context = None ) :
2011-04-21 08:35:01 +00:00
map_user = {
2011-05-04 10:03:29 +00:00
' id ' : ' id_new ' ,
2011-04-21 08:35:01 +00:00
' name ' : [ ' first_name ' , ' last_name ' ] ,
' login ' : ' user_name ' ,
' context_lang ' : ' context_lang ' ,
' password ' : ' password ' ,
' .id ' : ' .id ' ,
2011-04-29 05:27:56 +00:00
' context_department_id/id ' : ' context_department_id/id ' ,
2011-04-21 08:35:01 +00:00
}
def get_users_department ( sugar_obj , cr , uid , val , context = None ) :
2011-04-29 05:00:46 +00:00
fields = [ ' name ' ]
data = [ val ]
2011-05-04 10:03:29 +00:00
if not val :
return False
2011-04-29 05:00:46 +00:00
return import_object ( sugar_obj , cr , uid , fields , data , ' hr.department ' , ' hr_department_user ' , val , context = context )
2011-04-21 08:35:01 +00:00
2011-03-24 04:29:43 +00:00
if not context :
context = { }
2011-04-21 08:35:01 +00:00
2011-03-24 04:29:43 +00:00
user_obj = sugar_obj . pool . get ( ' res.users ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Users ' )
for val in sugar_data :
user_ids = user_obj . search ( cr , uid , [ ( ' login ' , ' = ' , val . get ( ' user_name ' ) ) ] )
if user_ids :
2011-03-29 12:41:37 +00:00
val [ ' .id ' ] = str ( user_ids [ 0 ] )
2011-03-24 04:29:43 +00:00
else :
val [ ' password ' ] = ' sugarcrm ' #default password for all user
2011-05-04 10:03:29 +00:00
val [ ' id_new ' ] = generate_xml_id ( val [ ' id ' ] , TABLE_USER )
val [ ' context_department_id/id ' ] = get_users_department ( sugar_obj , cr , uid , val . get ( ' department ' ) , context = context )
2011-03-29 12:41:37 +00:00
val [ ' context_lang ' ] = context . get ( ' lang ' , ' en_US ' )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_user , context )
2011-05-02 14:51:13 +00:00
user_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-04 08:34:41 +00:00
return True
2011-03-24 04:29:43 +00:00
2011-04-28 06:31:55 +00:00
def get_lead_status ( sugar_obj , cr , uid , sugar_val , context = None ) :
2011-03-24 04:29:43 +00:00
if not context :
context = { }
2011-04-28 06:31:55 +00:00
fields = [ ' name ' , ' type ' ]
name = ' lead_ ' + sugar_val [ ' status ' ]
data = [ sugar_val [ ' status ' ] , ' lead ' ]
return import_object ( sugar_obj , cr , uid , fields , data , ' crm.case.stage ' , ' crm_lead_stage ' , name , [ ( ' type ' , ' = ' , ' lead ' ) , ( ' name ' , ' ilike ' , sugar_val [ ' status ' ] ) ] , context )
2011-03-24 04:29:43 +00:00
2011-03-24 13:46:24 +00:00
def get_lead_state ( surgar_obj , cr , uid , sugar_val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm stage : openerp opportunity stage
' New ' : ' draft ' ,
' Assigned ' : ' open ' ,
' In Progress ' : ' open ' ,
' Recycled ' : ' cancel ' ,
' Dead ' : ' done '
} , }
state = state_dict [ ' status ' ] . get ( sugar_val [ ' status ' ] , ' ' )
return state
2011-04-27 16:13:55 +00:00
2011-05-04 10:03:29 +00:00
def import_partners ( sugar_obj , cr , uid , context = None ) :
map_partner = {
' id ' : ' id_new ' ,
' name ' : ' name ' ,
' website ' : ' website ' ,
' user_id/id ' : ' user_id/id ' ,
' ref ' : ' sic_code ' ,
' comment ' : [ ' __prettyprint__ ' , ' description ' , ' employees ' , ' ownership ' , ' annual_revenue ' , ' rating ' , ' industry ' , ' ticker_symbol ' ] ,
' customer ' : ' customer ' ,
' supplier ' : ' supplier ' ,
2011-03-24 13:46:24 +00:00
}
2011-05-04 10:03:29 +00:00
def get_address_type ( sugar_obj , cr , uid , val , type , context = None ) :
2011-03-29 04:34:57 +00:00
if type == ' invoice ' :
type_address = ' billing '
else :
type_address = ' shipping '
2011-03-24 04:29:43 +00:00
map_partner_address = {
' name ' : ' name ' ,
' partner_id/id ' : ' account_id ' ,
' phone ' : ' phone_office ' ,
' mobile ' : ' phone_mobile ' ,
' fax ' : ' phone_fax ' ,
2011-03-25 12:33:53 +00:00
' type ' : ' type ' ,
2011-03-29 04:34:57 +00:00
' street ' : type_address + ' _address_street ' ,
' zip ' : type_address + ' _address_postalcode ' ,
' city ' : type_address + ' _address_city ' ,
2011-05-02 14:51:13 +00:00
' country_id/id ' : ' country_id/id ' ,
2011-03-29 04:34:57 +00:00
' type ' : ' type ' ,
2011-03-24 04:29:43 +00:00
}
2011-05-04 10:03:29 +00:00
2011-04-14 10:39:21 +00:00
if val . get ( type_address + ' _address_country ' ) :
country_id = get_all_countries ( sugar_obj , cr , uid , val . get ( type_address + ' _address_country ' ) , context )
state = get_all_states ( sugar_obj , cr , uid , val . get ( type_address + ' _address_state ' ) , country_id , context )
2011-05-02 14:51:13 +00:00
val [ ' country_id/id ' ] = country_id
val [ ' state_id/id ' ] = state
2011-05-04 10:03:29 +00:00
val [ ' type ' ] = type
val [ ' id_new ' ] = val [ ' id ' ] + ' _address_ ' + type
2011-03-29 04:34:57 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_partner_address )
2011-05-04 10:03:29 +00:00
return import_object ( sugar_obj , cr , uid , fields , datas , ' res.partner.address ' , TABLE_CONTACT , val [ ' id_new ' ] , DO_NOT_FIND_DOMAIN , context = context )
def get_address ( sugar_obj , cr , uid , val , context = None ) :
address_id = [ ]
type_dict = { ' billing_address_street ' : ' invoice ' , ' shipping_address_street ' : ' delivery ' }
for key , type_value in type_dict . items ( ) :
if val . get ( key ) :
id = get_address_type ( sugar_obj , cr , uid , val , type_value , context )
address_id . append ( id )
2011-03-24 04:29:43 +00:00
return address_id
2011-03-29 04:34:57 +00:00
2011-03-24 04:29:43 +00:00
if not context :
context = { }
partner_obj = sugar_obj . pool . get ( ' res.partner ' )
2011-05-04 10:03:29 +00:00
2011-03-24 04:29:43 +00:00
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Accounts ' )
2011-05-04 10:03:29 +00:00
2011-03-24 04:29:43 +00:00
for val in sugar_data :
add_id = get_address ( sugar_obj , cr , uid , val , context )
2011-04-21 08:35:01 +00:00
val [ ' customer ' ] = ' 1 '
val [ ' supplier ' ] = ' 0 '
2011-05-04 10:03:29 +00:00
val [ ' user_id/id ' ] = xml_id_exist ( sugar_obj , cr , uid , TABLE_USER , val [ ' assigned_user_id ' ] )
val [ ' id_new ' ] = generate_xml_id ( val [ ' id ' ] , TABLE_ACCOUNT )
print " values of partner "
pp . pprint ( val )
if val [ ' parent_id ' ] :
print ' parent_id ' , val [ ' parent_id ' ]
val [ ' parent_id_new ' ] = generate_xml_id ( val [ ' parent_id ' ] , TABLE_ACCOUNT )
2011-05-02 14:51:13 +00:00
fields , data = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_partner )
fields , data = add_m2o_data ( data , fields , ' address/id ' , add_id )
2011-05-04 10:03:29 +00:00
2011-05-02 14:51:13 +00:00
partner_obj . import_data ( cr , uid , fields , [ data ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-05-04 10:03:29 +00:00
import_self_dependencies ( partner_obj , cr , uid , ' parent_id ' , sugar_data , context )
2011-03-25 12:33:53 +00:00
return True
2011-03-24 04:29:43 +00:00
2011-04-06 13:22:20 +00:00
def get_category ( sugar_obj , cr , uid , model , name , context = None ) :
2011-04-28 06:31:55 +00:00
if not context :
context = { }
fields = [ ' name ' , ' object_id ' ]
data = [ name , model ]
return import_object ( sugar_obj , cr , uid , fields , data , ' crm.case.categ ' , ' crm_categ ' , name , [ ( ' object_id.model ' , ' = ' , model ) , ( ' name ' , ' ilike ' , name ) ] , context )
2011-04-06 13:22:20 +00:00
2011-04-06 07:32:08 +00:00
def get_alarm_id ( sugar_obj , cr , uid , val , context = None ) :
2011-04-29 05:00:46 +00:00
if not context :
context = { }
2011-04-06 07:32:08 +00:00
alarm_dict = { ' 60 ' : ' 1 minute before ' ,
' 300 ' : ' 5 minutes before ' ,
' 600 ' : ' 10 minutes before ' ,
' 900 ' : ' 15 minutes before ' ,
' 1800 ' : ' 30 minutes before ' ,
' 3600 ' : ' 1 hour before ' ,
}
alarm_id = False
alarm_obj = sugar_obj . pool . get ( ' res.alarm ' )
if alarm_dict . get ( val ) :
alarm_ids = alarm_obj . search ( cr , uid , [ ( ' name ' , ' like ' , alarm_dict . get ( val ) ) ] )
for alarm in alarm_obj . browse ( cr , uid , alarm_ids , context ) :
alarm_id = alarm . id
return alarm_id
def get_meeting_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm stage : openerp meeting stage
' Planned ' : ' draft ' ,
' Held ' : ' open ' ,
' Not Held ' : ' draft ' ,
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
def get_task_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm stage : openerp meeting stage
' Completed ' : ' done ' ,
' Not Started ' : ' draft ' ,
' In Progress ' : ' open ' ,
' Pending Input ' : ' draft ' ,
' deferred ' : ' cancel '
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
2011-04-06 13:22:20 +00:00
2011-04-07 08:57:52 +00:00
def get_project_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm staus : openerp Projects state
' Draft ' : ' draft ' ,
' In Review ' : ' open ' ,
' Published ' : ' close ' ,
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
def get_project_task_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm status : openerp Porject Tasks state
' Not Started ' : ' draft ' ,
' In Progress ' : ' open ' ,
' Completed ' : ' done ' ,
' Pending Input ' : ' pending ' ,
' Deferred ' : ' cancelled ' ,
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
def get_project_task_priority ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
priority_dict = { ' priority ' : #field in the sugarcrm database
{ #Mapping of sugarcrm status : openerp Porject Tasks state
' High ' : ' 0 ' ,
' Medium ' : ' 2 ' ,
' Low ' : ' 3 '
} , }
priority = priority_dict [ ' priority ' ] . get ( val , ' ' )
return priority
2011-04-06 13:22:20 +00:00
def get_account ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
partner_id = False
partner_address_id = False
2011-04-21 13:40:23 +00:00
partner_phone = False
partner_mobile = False
2011-04-06 13:22:20 +00:00
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
2011-04-08 06:52:51 +00:00
address_obj = sugar_obj . pool . get ( ' res.partner.address ' )
2011-04-21 13:40:23 +00:00
crm_obj = sugar_obj . pool . get ( ' crm.lead ' )
project_obj = sugar_obj . pool . get ( ' project.project ' )
issue_obj = sugar_obj . pool . get ( ' project.issue ' )
2011-04-06 13:22:20 +00:00
if val . get ( ' parent_type ' ) == ' Accounts ' :
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' = ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , ' res.partner ' ) ] )
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
partner_id = model . res_id
2011-04-08 06:52:51 +00:00
address_ids = address_obj . search ( cr , uid , [ ( ' partner_id ' , ' = ' , partner_id ) ] )
2011-04-21 13:40:23 +00:00
if address_ids :
address_id = address_obj . browse ( cr , uid , address_ids [ 0 ] )
partner_address_id = address_id . id
partner_phone = address_id . phone
partner_mobile = address_id . mobile
2011-04-06 13:22:20 +00:00
if val . get ( ' parent_type ' ) == ' Contacts ' :
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' = ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , ' res.partner.address ' ) ] )
2011-04-28 13:08:59 +00:00
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
2011-04-06 13:22:20 +00:00
partner_address_id = model . res_id
2011-04-08 06:52:51 +00:00
address_id = address_obj . browse ( cr , uid , partner_address_id )
2011-04-21 13:40:23 +00:00
partner_phone = address_id . phone
partner_mobile = address_id . mobile
2011-04-28 13:08:59 +00:00
partner_id = address_id and address_id . partner_id . id or False
2011-04-21 13:40:23 +00:00
if val . get ( ' parent_type ' ) == ' Opportunities ' :
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' = ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , ' crm.lead ' ) ] )
2011-04-28 13:08:59 +00:00
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
2011-04-21 13:40:23 +00:00
opportunity_id = model . res_id
opportunity_id = crm_obj . browse ( cr , uid , opportunity_id )
partner_id = opportunity_id . partner_id . id
partner_address_id = opportunity_id . partner_address_id . id
partner_phone = opportunity_id . partner_address_id . phone
partner_mobile = opportunity_id . partner_address_id . mobile
if val . get ( ' parent_type ' ) == ' Project ' :
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' = ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , ' project.project ' ) ] )
2011-04-28 13:08:59 +00:00
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
2011-04-21 13:40:23 +00:00
proj_ids = model . res_id
proj_id = project_obj . browse ( cr , uid , proj_ids )
partner_id = proj_id . partner_id . id
2011-04-26 05:03:45 +00:00
partner_address_id = proj_id . contact_id . id
partner_phone = proj_id . contact_id . phone
partner_mobile = proj_id . contact_id . mobile
2011-04-21 13:40:23 +00:00
if val . get ( ' parent_type ' ) == ' Bugs ' :
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' = ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , ' project.issue ' ) ] )
2011-04-28 13:08:59 +00:00
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
2011-04-21 13:40:23 +00:00
issue_ids = model . res_id
issue_id = issue_obj . browse ( cr , uid , issue_ids )
partner_id = issue_id . partner_id . id
partner_address_id = issue_id . partner_address_id . id
partner_phone = issue_id . partner_address_id . phone
partner_mobile = issue_id . partner_address_id . mobile
2011-04-28 12:42:57 +00:00
return partner_id , partner_address_id , partner_phone , partner_mobile
2011-04-21 13:40:23 +00:00
def import_documents ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_document = { ' id ' : ' id ' ,
2011-04-28 12:42:57 +00:00
' name ' : ' filename ' ,
2011-04-21 13:40:23 +00:00
' description ' : ' description ' ,
' datas ' : ' datas ' ,
2011-04-26 05:03:45 +00:00
' datas_fname ' : ' datas_fname ' ,
2011-04-21 13:40:23 +00:00
}
attach_obj = sugar_obj . pool . get ( ' ir.attachment ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
2011-04-28 12:42:57 +00:00
sugar_data = sugar . search ( PortType , sessionid , ' DocumentRevisions ' )
2011-04-21 13:40:23 +00:00
for val in sugar_data :
2011-04-28 12:42:57 +00:00
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 ' )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_document , context )
2011-04-21 13:40:23 +00:00
attach_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = ' sugarcrm_import ' , noupdate = True , context = context )
return True
2011-04-06 13:22:20 +00:00
2011-04-06 07:32:08 +00:00
def import_tasks ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_task = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date ' : [ ' __datetime__ ' , ' date_start ' ] ,
' date_deadline ' : [ ' __datetime__ ' , ' date_due ' ] ,
2011-04-06 07:32:08 +00:00
' user_id/id ' : ' assigned_user_id ' ,
2011-04-28 06:31:55 +00:00
' categ_id/id ' : ' categ_id/id ' ,
2011-04-06 13:22:20 +00:00
' partner_id/.id ' : ' partner_id/.id ' ,
' partner_address_id/.id ' : ' partner_address_id/.id ' ,
2011-04-06 07:32:08 +00:00
' state ' : ' state '
}
meeting_obj = sugar_obj . pool . get ( ' crm.meeting ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
2011-04-06 13:22:20 +00:00
categ_id = get_category ( sugar_obj , cr , uid , ' crm.meeting ' , ' Tasks ' )
2011-04-06 07:32:08 +00:00
sugar_data = sugar . search ( PortType , sessionid , ' Tasks ' )
for val in sugar_data :
partner_xml_id = find_mapped_id ( sugar_obj , cr , uid , ' res.partner.address ' , val . get ( ' contact_id ' ) , context )
if not partner_xml_id :
2011-04-06 13:22:20 +00:00
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Reference Contact %s cannot be created, due to Lower Record Limit in SugarCRM Configuration. ' ) % val . get ( ' contact_name ' ) )
2011-04-21 13:40:23 +00:00
partner_id , partner_address_id , partner_phone , partner_mobile = get_account ( sugar_obj , cr , uid , val , context )
2011-04-06 13:22:20 +00:00
val [ ' partner_id/.id ' ] = partner_id
val [ ' partner_address_id/.id ' ] = partner_address_id
2011-04-28 06:31:55 +00:00
val [ ' categ_id/id ' ] = categ_id
2011-04-26 06:43:27 +00:00
val [ ' state ' ] = get_task_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_task , context )
2011-04-06 07:32:08 +00:00
meeting_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = ' sugarcrm_import ' , noupdate = True , context = context )
return True
2011-04-11 06:16:18 +00:00
def get_attendee_id ( sugar_obj , cr , uid , PortType , sessionid , module_name , module_id , context = None ) :
2011-04-11 06:02:16 +00:00
if not context :
context = { }
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
att_obj = sugar_obj . pool . get ( ' calendar.attendee ' )
2011-04-11 06:16:18 +00:00
meeting_obj = sugar_obj . pool . get ( ' crm.meeting ' )
2011-04-11 07:38:23 +00:00
user_dict = sugar . user_get_attendee_list ( PortType , sessionid , module_name , module_id )
for user in user_dict :
user_model_ids = find_mapped_id ( sugar_obj , cr , uid , ' res.users ' , user . get ( ' id ' ) , context )
2011-04-11 06:48:50 +00:00
user_resource_id = model_obj . browse ( cr , uid , user_model_ids )
if user_resource_id :
user_id = user_resource_id [ 0 ] . res_id
attend_ids = att_obj . search ( cr , uid , [ ( ' user_id ' , ' = ' , user_id ) ] )
if attend_ids :
2011-04-27 16:13:55 +00:00
attendees = attend_ids [ 0 ]
2011-04-11 06:48:50 +00:00
else :
2011-04-11 07:38:23 +00:00
attendees = att_obj . create ( cr , uid , { ' user_id ' : user_id , ' email ' : user . get ( ' email1 ' ) } )
2011-04-11 06:48:50 +00:00
meeting_model_ids = find_mapped_id ( sugar_obj , cr , uid , ' crm.meeting ' , module_id , context )
meeting_xml_id = model_obj . browse ( cr , uid , meeting_model_ids )
if meeting_xml_id :
meeting_obj . write ( cr , uid , [ meeting_xml_id [ 0 ] . res_id ] , { ' attendee_ids ' : [ ( 4 , attendees ) ] } )
2011-04-11 06:02:16 +00:00
return True
2011-04-06 07:32:08 +00:00
def import_meetings ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_meeting = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date ' : [ ' __datetime__ ' , ' date_start ' ] ,
2011-04-06 07:32:08 +00:00
' duration ' : [ ' duration_hours ' , ' duration_minutes ' ] ,
' location ' : ' location ' ,
' alarm_id/.id ' : ' alarm_id/.id ' ,
' user_id/id ' : ' assigned_user_id ' ,
2011-04-06 13:22:20 +00:00
' partner_id/.id ' : ' partner_id/.id ' ,
' partner_address_id/.id ' : ' partner_address_id/.id ' ,
2011-04-06 07:32:08 +00:00
' state ' : ' state '
}
meeting_obj = sugar_obj . pool . get ( ' crm.meeting ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Meetings ' )
for val in sugar_data :
2011-04-21 13:40:23 +00:00
partner_id , partner_address_id , partner_phone , partner_mobile = get_account ( sugar_obj , cr , uid , val , context )
2011-04-06 13:22:20 +00:00
val [ ' partner_id/.id ' ] = partner_id
val [ ' partner_address_id/.id ' ] = partner_address_id
2011-04-06 07:32:08 +00:00
val [ ' state ' ] = get_meeting_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
val [ ' alarm_id/.id ' ] = get_alarm_id ( sugar_obj , cr , uid , val . get ( ' reminder_time ' ) , context )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_meeting , context )
2011-04-06 07:32:08 +00:00
meeting_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = ' sugarcrm_import ' , noupdate = True , context = context )
2011-04-11 06:16:18 +00:00
get_attendee_id ( sugar_obj , cr , uid , PortType , sessionid , ' Meetings ' , val . get ( ' id ' ) , context )
2011-04-06 07:32:08 +00:00
return True
2011-04-06 13:22:20 +00:00
def get_calls_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state = False
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm stage : openerp calls stage
' Planned ' : ' open ' ,
' Held ' : ' done ' ,
' Not Held ' : ' pending ' ,
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
2011-04-06 07:32:08 +00:00
def import_calls ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_calls = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date ' : [ ' __datetime__ ' , ' date_start ' ] ,
2011-04-06 07:32:08 +00:00
' duration ' : [ ' duration_hours ' , ' duration_minutes ' ] ,
' user_id/id ' : ' assigned_user_id ' ,
2011-04-06 13:22:20 +00:00
' partner_id/.id ' : ' partner_id/.id ' ,
' partner_address_id/.id ' : ' partner_address_id/.id ' ,
2011-04-28 06:31:55 +00:00
' categ_id/id ' : ' categ_id/id ' ,
2011-04-06 07:32:08 +00:00
' state ' : ' state ' ,
2011-04-21 13:40:23 +00:00
' partner_phone ' : ' partner_phone ' ,
' partner_mobile ' : ' partner_mobile ' ,
' opportunity_id/id ' : ' opportunity_id/id ' ,
2011-04-25 04:41:00 +00:00
2011-04-06 07:32:08 +00:00
}
phonecall_obj = sugar_obj . pool . get ( ' crm.phonecall ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Calls ' )
for val in sugar_data :
2011-04-21 13:40:23 +00:00
sugar_call_leads = sugar . relation_search ( PortType , sessionid , ' Calls ' , module_id = val . get ( ' id ' ) , related_module = ' Leads ' , query = None , deleted = None )
if sugar_call_leads :
for call_opportunity in sugar_call_leads :
val [ ' opportunity_id/id ' ] = call_opportunity
2011-04-06 13:22:20 +00:00
categ_id = get_category ( sugar_obj , cr , uid , ' crm.phonecall ' , val . get ( ' direction ' ) )
2011-04-28 06:31:55 +00:00
val [ ' categ_id/id ' ] = categ_id
2011-04-21 13:40:23 +00:00
partner_id , partner_address_id , partner_phone , partner_mobile = get_account ( sugar_obj , cr , uid , val , context )
2011-04-06 13:22:20 +00:00
val [ ' partner_id/.id ' ] = partner_id
val [ ' partner_address_id/.id ' ] = partner_address_id
2011-04-21 13:40:23 +00:00
val [ ' partner_phone ' ] = partner_phone
val [ ' partner_mobile ' ] = partner_mobile
2011-04-06 13:22:20 +00:00
val [ ' state ' ] = get_calls_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_calls , context )
2011-05-02 14:51:13 +00:00
phonecall_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-06 13:22:20 +00:00
return True
2011-05-04 10:03:29 +00:00
2011-03-24 13:46:24 +00:00
2011-04-13 12:25:11 +00:00
def get_bug_priority ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
priority_dict = { ' priority ' : #field in the sugarcrm database
{ #Mapping of sugarcrm priority : openerp bugs priority
' Urgent ' : ' 1 ' ,
' High ' : ' 2 ' ,
' Medium ' : ' 3 ' ,
' Low ' : ' 4 '
} , }
priority = priority_dict [ ' priority ' ] . get ( val , ' ' )
2011-04-25 05:48:20 +00:00
return priority
def get_claim_priority ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
priority_dict = { ' priority ' : #field in the sugarcrm database
{ #Mapping of sugarcrm priority : openerp claims priority
' High ' : ' 2 ' ,
' Medium ' : ' 3 ' ,
' Low ' : ' 4 '
} , }
priority = priority_dict [ ' priority ' ] . get ( val , ' ' )
return priority
2011-04-13 12:25:11 +00:00
def get_bug_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm status : openerp Bugs state
' New ' : ' draft ' ,
' Assigned ' : ' open ' ,
' Closed ' : ' done ' ,
' Pending ' : ' pending ' ,
' Rejected ' : ' cancel ' ,
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
2011-04-25 05:48:20 +00:00
def get_claim_state ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
state_dict = { ' status ' : #field in the sugarcrm database
{ #Mapping of sugarcrm status : openerp claim state
' New ' : ' draft ' ,
' Assigned ' : ' open ' ,
' Closed ' : ' done ' ,
' Pending Input ' : ' pending ' ,
' Rejected ' : ' cancel ' ,
' Duplicate ' : ' draft ' ,
} , }
state = state_dict [ ' status ' ] . get ( val , ' ' )
return state
2011-04-15 06:13:38 +00:00
2011-04-13 12:25:11 +00:00
2011-04-25 05:48:20 +00:00
def get_acc_contact_claim ( sugar_obj , cr , uid , val , context = None ) :
2011-04-15 06:13:38 +00:00
if not context :
2011-04-25 05:48:20 +00:00
context = { }
partner_id = False
partner_address_id = False
partner_phone = False
partner_email = False
2011-04-15 06:13:38 +00:00
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
2011-04-25 05:48:20 +00:00
address_obj = sugar_obj . pool . get ( ' res.partner.address ' )
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' = ' , val . get ( ' account_id ' ) ) , ( ' model ' , ' = ' , ' res.partner ' ) ] )
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
partner_id = model . res_id
address_ids = address_obj . search ( cr , uid , [ ( ' partner_id ' , ' = ' , partner_id ) ] )
if address_ids :
address_id = address_obj . browse ( cr , uid , address_ids [ 0 ] )
partner_address_id = address_id . id
partner_phone = address_id . phone
2011-05-04 10:03:29 +00:00
partner_email = address_id . email
2011-04-25 05:48:20 +00:00
return partner_id , partner_address_id , partner_phone , partner_email
def import_claims ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_claim = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date ' : [ ' __datetime__ ' , ' date_entered ' ] ,
2011-04-25 05:48:20 +00:00
' user_id/id ' : ' assigned_user_id ' ,
' priority ' : ' priority ' ,
' partner_id/.id ' : ' partner_id/.id ' ,
' partner_address_id/.id ' : ' partner_address_id/.id ' ,
' partner_phone ' : ' partner_phone ' ,
' partner_mobile ' : ' partner_email ' ,
' description ' : ' description ' ,
' state ' : ' state ' ,
}
claim_obj = sugar_obj . pool . get ( ' crm.claim ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Cases ' )
for val in sugar_data :
partner_id , partner_address_id , partner_phone , partner_email = get_acc_contact_claim ( sugar_obj , cr , uid , val , context )
val [ ' partner_id/.id ' ] = partner_id
val [ ' partner_address_id/.id ' ] = partner_address_id
val [ ' partner_phone ' ] = partner_phone
val [ ' email_from ' ] = partner_email
val [ ' priority ' ] = get_claim_priority ( sugar_obj , cr , uid , val . get ( ' priority ' ) , context )
val [ ' state ' ] = get_claim_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_claim , context )
2011-04-25 05:48:20 +00:00
claim_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = ' sugarcrm_import ' , noupdate = True , context = context )
return True
2011-04-13 12:25:11 +00:00
def import_bug ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_resource = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-15 06:13:38 +00:00
' project_id/.id ' : ' project_id/.id ' ,
2011-04-28 06:31:55 +00:00
' categ_id/id ' : ' categ_id/id ' ,
2011-04-13 12:25:11 +00:00
' priority ' : ' priority ' ,
2011-04-26 05:03:45 +00:00
' description ' : [ ' __prettyprint__ ' , ' description ' , ' bug_number ' , ' fixed_in_release_name ' , ' source ' , ' fixed_in_release ' , ' work_log ' , ' found_in_release ' , ' release_name ' , ' resolution ' ] ,
2011-04-15 06:13:38 +00:00
' state ' : ' state ' ,
2011-04-13 12:25:11 +00:00
}
issue_obj = sugar_obj . pool . get ( ' project.issue ' )
2011-04-26 05:03:45 +00:00
project_obj = sugar_obj . pool . get ( ' project.project ' )
2011-04-13 12:25:11 +00:00
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Bugs ' )
for val in sugar_data :
2011-04-26 05:03:45 +00:00
project_ids = project_obj . search ( cr , uid , [ ( ' name ' , ' like ' , ' sugarcrm_bugs ' ) ] )
if project_ids :
project_id = project_ids [ 0 ]
else :
2011-05-04 10:03:29 +00:00
project_id = project_obj . create ( cr , uid , { ' name ' : ' sugarcrm_bugs ' } )
2011-04-26 05:03:45 +00:00
val [ ' project_id/.id ' ] = project_id
2011-04-28 06:31:55 +00:00
val [ ' categ_id/id ' ] = get_category ( sugar_obj , cr , uid , ' project.issue ' , val . get ( ' type ' ) )
2011-04-13 12:25:11 +00:00
val [ ' priority ' ] = get_bug_priority ( sugar_obj , cr , uid , val . get ( ' priority ' ) , context )
val [ ' state ' ] = get_bug_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_resource , context )
2011-05-02 14:51:13 +00:00
issue_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-13 12:25:11 +00:00
return True
2011-04-08 06:52:51 +00:00
def get_job_id ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
2011-04-28 07:37:14 +00:00
fields = [ ' name ' ]
data = [ val ]
2011-04-29 05:00:46 +00:00
return import_object ( sugar_obj , cr , uid , fields , data , ' hr.job ' , ' hr_job ' , val , context = context )
2011-04-21 13:40:23 +00:00
def get_campaign_id ( sugar_obj , cr , uid , val , context = None ) :
if not context :
context = { }
2011-04-28 06:58:19 +00:00
fields = [ ' name ' ]
data = [ val ]
2011-04-29 05:00:46 +00:00
return import_object ( sugar_obj , cr , uid , fields , data , ' crm.case.resource.type ' , ' crm_campaign ' , val , context = context )
2011-04-08 06:52:51 +00:00
2011-04-27 11:35:43 +00:00
def get_attachment ( sugar_obj , cr , uid , val , model , File , Filename , parent_type , context = None ) :
2011-04-12 12:27:59 +00:00
if not context :
context = { }
attachment_obj = sugar_obj . pool . get ( ' ir.attachment ' )
2011-04-12 12:35:02 +00:00
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
mailgate_obj = sugar_obj . pool . get ( ' mailgate.message ' )
2011-04-27 07:19:43 +00:00
attach_ids = attachment_obj . search ( cr , uid , [ ( ' res_id ' , ' = ' , val . get ( ' res_id ' ) , ( ' res_model ' , ' = ' , val . get ( ' model ' ) ) ) ] )
2011-04-27 11:48:57 +00:00
if not attach_ids and Filename :
2011-04-27 11:35:43 +00:00
if parent_type == ' Accounts ' :
2011-04-27 11:48:57 +00:00
new_attachment_id = attachment_obj . create ( cr , uid , { ' name ' : Filename , ' datas_fname ' : Filename , ' datas ' : File , ' res_id ' : val . get ( ' res_id ' , False ) , ' res_model ' : val . get ( ' model ' , False ) , ' partner_id ' : val . get ( ' partner_id/.id ' ) } )
2011-04-27 11:35:43 +00:00
else :
2011-04-27 11:48:57 +00:00
new_attachment_id = attachment_obj . create ( cr , uid , { ' name ' : Filename , ' datas_fname ' : Filename , ' datas ' : File , ' res_id ' : val . get ( ' res_id ' , False ) , ' res_model ' : val . get ( ' model ' , False ) } )
2011-04-27 07:19:43 +00:00
message_model_ids = find_mapped_id ( sugar_obj , cr , uid , model , val . get ( ' id ' ) , context )
message_xml_id = model_obj . browse ( cr , uid , message_model_ids )
if message_xml_id :
2011-05-04 10:03:29 +00:00
if parent_type == ' Accounts ' :
mailgate_obj . write ( cr , uid , [ message_xml_id [ 0 ] . res_id ] , { ' attachment_ids ' : [ ( 4 , new_attachment_id ) ] , ' partner_id ' : val . get ( ' partner_id/.id ' ) } )
else :
mailgate_obj . write ( cr , uid , [ message_xml_id [ 0 ] . res_id ] , { ' attachment_ids ' : [ ( 4 , new_attachment_id ) ] } )
2011-04-12 12:27:59 +00:00
return True
2011-04-14 12:51:54 +00:00
def import_history ( sugar_obj , cr , uid , context = None ) :
2011-04-12 12:27:59 +00:00
if not context :
context = { }
map_attachment = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date ' : [ ' __datetime__ ' , ' date_entered ' ] ,
2011-04-12 12:27:59 +00:00
' user_id/id ' : ' assigned_user_id ' ,
2011-04-26 05:03:45 +00:00
' description ' : [ ' __prettyprint__ ' , ' description ' , ' description_html ' ] ,
2011-04-12 12:27:59 +00:00
' res_id ' : ' res_id ' ,
' model ' : ' model ' ,
2011-04-26 05:03:45 +00:00
' partner_id/.id ' : ' partner_id/.id ' ,
2011-04-12 12:27:59 +00:00
}
mailgate_obj = sugar_obj . pool . get ( ' mailgate.message ' )
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Notes ' )
for val in sugar_data :
2011-04-26 13:10:33 +00:00
File , Filename = sugar . attachment_search ( PortType , sessionid , ' Notes ' , val . get ( ' id ' ) )
2011-04-26 05:03:45 +00:00
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' like ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , OPENERP_FIEDS_MAPS [ val . get ( ' parent_type ' ) ] ) ] )
if model_ids :
model = model_obj . browse ( cr , uid , model_ids ) [ 0 ]
2011-04-27 07:19:43 +00:00
if model . model == ' res.partner ' :
val [ ' partner_id/.id ' ] = model . res_id
else :
val [ ' res_id ' ] = model . res_id
val [ ' model ' ] = model . model
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_attachment , context )
2011-04-21 12:40:47 +00:00
mailgate_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = ' sugarcrm_import ' , noupdate = True , context = context )
2011-04-27 11:35:43 +00:00
get_attachment ( sugar_obj , cr , uid , val , ' mailgate.message ' , File , Filename , val . get ( ' parent_type ' ) , context )
2011-04-12 12:27:59 +00:00
return True
2011-05-04 10:03:29 +00:00
2011-03-24 13:46:24 +00:00
def import_employees ( sugar_obj , cr , uid , context = None ) :
2011-05-04 10:03:29 +00:00
map_employee = { ' id ' : ' id_new ' ,
' resource_id/id ' : ' resource_id/id ' ,
2011-03-24 13:46:24 +00:00
' name ' : [ ' first_name ' , ' last_name ' ] ,
' work_phone ' : ' phone_work ' ,
' mobile_phone ' : ' phone_mobile ' ,
2011-05-04 10:03:29 +00:00
' user_id/id ' : ' user_id/id ' ,
' address_home_id/id ' : ' address_home_id/id ' ,
2011-03-24 13:46:24 +00:00
' notes ' : ' description ' ,
#TODO: Creation of Employee create problem.
2011-05-04 10:03:29 +00:00
#'coach_id/id': 'reports_to_id',
2011-04-28 07:37:14 +00:00
' job_id/id ' : ' job_id/id '
2011-03-24 13:46:24 +00:00
}
2011-05-04 10:03:29 +00:00
def get_ressource ( sugar_obj , cr , uid , val , context = None ) :
map_resource = {
' name ' : [ ' first_name ' , ' last_name ' ] ,
}
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_resource , context )
return import_object ( sugar_obj , cr , uid , fields , datas , ' resource.resource ' , TABLE_RESSOURCE , val [ ' id ' ] , DO_NOT_FIND_DOMAIN , context )
def get_user_address ( sugar_obj , cr , uid , val , context = None ) :
map_user_address = {
' name ' : [ ' first_name ' , ' last_name ' ] ,
' city ' : ' address_city ' ,
' country_id/id ' : ' country_id/id ' ,
' state_id/id ' : ' state_id/id ' ,
' street ' : ' address_street ' ,
' zip ' : ' address_postalcode ' ,
' fax ' : ' fax ' ,
}
if val . get ( ' address_country ' ) :
country_id = get_all_countries ( sugar_obj , cr , uid , val . get ( ' address_country ' ) , context )
state_id = get_all_states ( sugar_obj , cr , uid , val . get ( ' address_state ' ) , country_id , context )
val [ ' country_id/id ' ] = country_id
val [ ' state_id/id ' ] = state_id
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_user_address )
return import_object ( sugar_obj , cr , uid , fields , datas , ' res.partner.address ' , TABLE_CONTACT , val [ ' id ' ] , DO_NOT_FIND_DOMAIN , context = context )
if not context :
context = { }
2011-03-24 13:46:24 +00:00
employee_obj = sugar_obj . pool . get ( ' hr.employee ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Employees ' )
2011-05-04 10:03:29 +00:00
2011-03-24 13:46:24 +00:00
for val in sugar_data :
2011-05-04 10:03:29 +00:00
print " Employees "
xml_id = get_user_address ( sugar_obj , cr , uid , val , context )
print " xml_ids " , sugar_obj . pool . get ( ' ir.model.data ' ) . search ( cr , uid , [ ( ' module ' , ' = ' , MODULE_NAME ) , ( ' name ' , ' = ' , xml_id ) ] )
val [ ' address_home_id/id ' ] = xml_id
2011-04-28 07:37:14 +00:00
val [ ' job_id/id ' ] = get_job_id ( sugar_obj , cr , uid , val . get ( ' title ' ) , context )
2011-05-04 10:03:29 +00:00
x = xml_id_exist ( sugar_obj , cr , uid , TABLE_USER , val [ ' id ' ] )
print ' xml_id of user ' , x
val [ ' user_id/id ' ] = x
val [ ' resource_id/id ' ] = get_ressource ( sugar_obj , cr , uid , val , context )
val [ ' id_new ' ] = generate_xml_id ( val [ ' id ' ] , TABLE_EMPLOYEE )
pp . pprint ( val )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_employee , context )
2011-05-02 14:51:13 +00:00
employee_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-04 08:34:41 +00:00
return True
2011-03-24 13:46:24 +00:00
2011-04-06 13:22:20 +00:00
def import_emails ( sugar_obj , cr , uid , context = None ) :
2011-04-08 06:52:51 +00:00
if not context :
2011-04-14 10:39:21 +00:00
context = { }
2011-04-06 13:22:20 +00:00
map_emails = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date ' : [ ' __datetime__ ' , ' date_sent ' ] ,
2011-04-06 13:22:20 +00:00
' email_from ' : ' from_addr_name ' ,
' email_to ' : ' reply_to_addr ' ,
' email_cc ' : ' cc_addrs_names ' ,
' email_bcc ' : ' bcc_addrs_names ' ,
' message_id ' : ' message_id ' ,
' user_id/id ' : ' assigned_user_id ' ,
2011-04-26 05:03:45 +00:00
' description ' : [ ' __prettyprint__ ' , ' description ' , ' description_html ' ] ,
2011-04-06 13:22:20 +00:00
' res_id ' : ' res_id ' ,
' model ' : ' model ' ,
2011-04-26 13:10:33 +00:00
' partner_id/.id ' : ' partner_id/.id '
2011-04-06 13:22:20 +00:00
}
mailgate_obj = sugar_obj . pool . get ( ' mailgate.message ' )
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Emails ' )
for val in sugar_data :
2011-04-27 07:19:43 +00:00
model_ids = model_obj . search ( cr , uid , [ ( ' name ' , ' like ' , val . get ( ' parent_id ' ) ) , ( ' model ' , ' = ' , OPENERP_FIEDS_MAPS [ val . get ( ' parent_type ' ) ] ) ] )
2011-04-06 13:22:20 +00:00
for model in model_obj . browse ( cr , uid , model_ids ) :
2011-04-26 13:10:33 +00:00
if model . model == ' res.partner ' :
val [ ' partner_id/.id ' ] = model . res_id
2011-04-27 07:19:43 +00:00
else :
val [ ' res_id ' ] = model . res_id
val [ ' model ' ] = model . model
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_emails , context )
2011-04-06 13:22:20 +00:00
mailgate_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = ' sugarcrm_import ' , noupdate = True , context = context )
return True
2011-05-02 14:51:13 +00:00
2011-05-04 10:03:29 +00:00
2011-04-13 13:22:40 +00:00
2011-04-07 08:57:52 +00:00
def import_projects ( sugar_obj , cr , uid , context = None ) :
2011-05-04 10:03:29 +00:00
2011-04-07 08:57:52 +00:00
map_project = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date_start ' : [ ' __datetime__ ' , ' estimated_start_date ' ] ,
' date ' : [ ' __datetime__ ' , ' estimated_end_date ' ] ,
2011-04-07 08:57:52 +00:00
' user_id/id ' : ' assigned_user_id ' ,
2011-04-13 13:22:40 +00:00
' partner_id/.id ' : ' partner_id/.id ' ,
' contact_id/.id ' : ' contact_id/.id ' ,
2011-04-07 08:57:52 +00:00
' state ' : ' state '
}
2011-05-04 10:03:29 +00:00
#TODO more simplier use xml id please !!!!
def get_project_account ( sugar_obj , cr , uid , PortType , sessionid , val , context = None ) :
if not context :
context = { }
partner_id = False
partner_invoice_id = False
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
partner_obj = sugar_obj . pool . get ( ' res.partner ' )
partner_address_obj = sugar_obj . pool . get ( ' res.partner.address ' )
sugar_project_account = sugar . relation_search ( PortType , sessionid , ' Project ' , module_id = val . get ( ' id ' ) , related_module = ' Accounts ' , query = None , deleted = None )
for account_id in sugar_project_account :
model_ids = find_mapped_id ( sugar_obj , cr , uid , ' res.partner ' , account_id , context )
if model_ids :
model_id = model_obj . browse ( cr , uid , model_ids ) [ 0 ] . res_id
partner_id = partner_obj . browse ( cr , uid , model_id ) . id
address_ids = partner_address_obj . search ( cr , uid , [ ( ' partner_id ' , ' = ' , partner_id ) , ( ' type ' , ' = ' , ' invoice ' ) ] )
partner_invoice_id = address_ids [ 0 ]
return partner_id , partner_invoice_id
if not context :
context = { }
2011-04-07 08:57:52 +00:00
project_obj = sugar_obj . pool . get ( ' project.project ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Project ' )
for val in sugar_data :
2011-04-13 13:22:40 +00:00
partner_id , partner_invoice_id = get_project_account ( sugar_obj , cr , uid , PortType , sessionid , val , context )
val [ ' partner_id/.id ' ] = partner_id
val [ ' contact_id/.id ' ] = partner_invoice_id
2011-04-07 08:57:52 +00:00
val [ ' state ' ] = get_project_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_project , context )
2011-05-02 14:51:13 +00:00
project_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-07 08:57:52 +00:00
return True
def import_project_tasks ( sugar_obj , cr , uid , context = None ) :
if not context :
context = { }
map_project_task = { ' id ' : ' id ' ,
' name ' : ' name ' ,
2011-04-26 06:43:27 +00:00
' date_start ' : [ ' __datetime__ ' , ' date_start ' ] ,
' date_end ' : [ ' __datetime__ ' , ' date_finish ' ] ,
2011-04-07 08:57:52 +00:00
' progress ' : ' progress ' ,
' project_id/name ' : ' project_name ' ,
2011-04-08 06:52:51 +00:00
' planned_hours ' : ' planned_hours ' ,
' total_hours ' : ' total_hours ' ,
2011-04-07 08:57:52 +00:00
' priority ' : ' priority ' ,
' description ' : ' description ' ,
' user_id/id ' : ' assigned_user_id ' ,
' state ' : ' state '
}
task_obj = sugar_obj . pool . get ( ' project.task ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' ProjectTask ' )
for val in sugar_data :
val [ ' state ' ] = get_project_task_state ( sugar_obj , cr , uid , val . get ( ' status ' ) , context )
val [ ' priority ' ] = get_project_task_priority ( sugar_obj , cr , uid , val . get ( ' priority ' ) , context )
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_project_task , context )
2011-05-02 14:51:13 +00:00
task_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-07 08:57:52 +00:00
return True
2011-03-24 04:29:43 +00:00
def import_leads ( sugar_obj , cr , uid , context = None ) :
2011-05-04 10:03:29 +00:00
2011-03-24 04:29:43 +00:00
map_lead = {
' id ' : ' id ' ,
' name ' : [ ' first_name ' , ' last_name ' ] ,
' contact_name ' : [ ' first_name ' , ' last_name ' ] ,
2011-04-26 13:10:33 +00:00
' description ' : [ ' __prettyprint__ ' , ' description ' , ' refered_by ' , ' lead_source ' , ' lead_source_description ' , ' website ' , ' email2 ' , ' status_description ' , ' lead_source_description ' , ' do_not_call ' ] ,
2011-03-24 13:46:24 +00:00
' partner_name ' : ' account_name ' ,
2011-03-24 04:29:43 +00:00
' email_from ' : ' email1 ' ,
' phone ' : ' phone_work ' ,
' mobile ' : ' phone_mobile ' ,
2011-03-30 06:43:42 +00:00
' title.id ' : ' title.id ' ,
2011-03-24 04:29:43 +00:00
' function ' : ' title ' ,
' street ' : ' primary_address_street ' ,
2011-03-29 11:53:57 +00:00
' street2 ' : ' alt_address_street ' ,
2011-03-24 04:29:43 +00:00
' zip ' : ' primary_address_postalcode ' ,
' city ' : ' primary_address_city ' ,
' user_id/id ' : ' assigned_user_id ' ,
2011-04-28 06:31:55 +00:00
' stage_id/id ' : ' stage_id/id ' ,
2011-03-24 04:29:43 +00:00
' type ' : ' type ' ,
2011-03-24 13:46:24 +00:00
' state ' : ' state ' ,
2011-04-21 13:40:23 +00:00
' fax ' : ' phone_fax ' ,
' referred ' : ' refered_by ' ,
' optout ' : ' optout ' ,
2011-04-28 06:58:19 +00:00
' type_id/id ' : ' type_id/id ' ,
2011-04-27 09:22:37 +00:00
' country_id.id ' : ' country_id.id ' ,
' state_id.id ' : ' state_id.id '
2011-03-24 04:29:43 +00:00
}
2011-05-04 10:03:29 +00:00
def get_contact_title ( sugar_obj , cr , uid , salutation , domain , context = None ) :
fields = [ ' shortcut ' , ' name ' , ' domain ' ]
data = [ salutation , salutation , domain ]
return import_object ( sugar_obj , cr , uid , fields , data , ' res.partner.title ' , ' contact_title ' , salutation , [ ( ' shortcut ' , ' = ' , salutation ) ] , context = context )
if not context :
context = { }
2011-03-24 04:29:43 +00:00
lead_obj = sugar_obj . pool . get ( ' crm.lead ' )
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Leads ' )
for val in sugar_data :
2011-04-21 13:40:23 +00:00
if val . get ( ' do_not_call ' ) == ' 0 ' :
val [ ' optout ' ] = ' 1 '
2011-04-04 05:45:47 +00:00
if val . get ( ' opportunity_id ' ) :
continue
2011-04-28 06:31:55 +00:00
if val . get ( ' salutation ' ) :
title_id = get_contact_title ( sugar_obj , cr , uid , val . get ( ' salutation ' ) , ' Contact ' , context )
val [ ' title/id ' ] = title_id
2011-03-24 04:29:43 +00:00
val [ ' type ' ] = ' lead '
2011-04-28 06:58:19 +00:00
val [ ' type_id/id ' ] = get_campaign_id ( sugar_obj , cr , uid , val . get ( ' lead_source ' ) , context )
2011-03-24 04:29:43 +00:00
stage_id = get_lead_status ( sugar_obj , cr , uid , val , context )
2011-04-28 06:31:55 +00:00
val [ ' stage_id/id ' ] = stage_id
2011-03-24 13:46:24 +00:00
val [ ' state ' ] = get_lead_state ( sugar_obj , cr , uid , val , context )
2011-04-27 09:22:37 +00:00
if val . get ( ' primary_address_country ' ) :
country_id = get_all_countries ( sugar_obj , cr , uid , val . get ( ' primary_address_country ' ) , context )
state = get_all_states ( sugar_obj , cr , uid , val . get ( ' primary_address_state ' ) , country_id , context )
val [ ' country_id.id ' ] = country_id
val [ ' state_id.id ' ] = state
2011-04-26 06:43:27 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_lead , context )
2011-05-02 14:51:13 +00:00
lead_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-04 08:34:41 +00:00
return True
2011-03-31 14:02:39 +00:00
2011-05-04 10:03:29 +00:00
2011-03-31 14:02:39 +00:00
2011-03-24 04:29:43 +00:00
def import_opportunities ( sugar_obj , cr , uid , context = None ) :
map_opportunity = { ' id ' : ' id ' ,
' name ' : ' name ' ,
' probability ' : ' probability ' ,
2011-03-24 13:46:24 +00:00
' partner_id/name ' : ' account_name ' ,
' title_action ' : ' next_step ' ,
2011-03-31 14:02:39 +00:00
' partner_address_id/name ' : ' partner_address_id/name ' ,
2011-03-29 11:53:57 +00:00
' planned_revenue ' : ' amount ' ,
2011-04-26 06:43:27 +00:00
' date_deadline ' : [ ' __datetime__ ' , ' date_closed ' ] ,
2011-03-24 04:29:43 +00:00
' user_id/id ' : ' assigned_user_id ' ,
2011-04-27 16:13:55 +00:00
' stage_id/id ' : ' stage_id/id ' ,
2011-03-24 04:29:43 +00:00
' type ' : ' type ' ,
2011-04-28 06:31:55 +00:00
' categ_id/id ' : ' categ_id/id ' ,
2011-04-26 05:03:45 +00:00
' email_from ' : ' email_from '
2011-03-24 04:29:43 +00:00
}
2011-05-04 10:03:29 +00:00
def get_opportunity_contact ( sugar_obj , cr , uid , PortType , sessionid , val , partner_xml_id , context = None ) :
partner_contact_name = False
partner_contact_email = False
model_obj = sugar_obj . pool . get ( ' ir.model.data ' )
partner_address_obj = sugar_obj . pool . get ( ' res.partner.address ' )
model_account_ids = model_obj . search ( cr , uid , [ ( ' res_id ' , ' = ' , partner_xml_id [ 0 ] ) , ( ' model ' , ' = ' , ' res.partner ' ) , ( ' module ' , ' = ' , MODULE_NAME ) ] )
model_xml_id = model_obj . browse ( cr , uid , model_account_ids ) [ 0 ] . name
sugar_account_contact = set ( sugar . relation_search ( PortType , sessionid , ' Accounts ' , module_id = model_xml_id , related_module = ' Contacts ' , query = None , deleted = None ) )
sugar_opportunities_contact = set ( sugar . relation_search ( PortType , sessionid , ' Opportunities ' , module_id = val . get ( ' id ' ) , related_module = ' Contacts ' , query = None , deleted = None ) )
sugar_contact = list ( sugar_account_contact . intersection ( sugar_opportunities_contact ) )
if sugar_contact :
for contact in sugar_contact :
model_ids = find_mapped_id ( sugar_obj , cr , uid , ' res.partner.address ' , contact , context )
if model_ids :
model_id = model_obj . browse ( cr , uid , model_ids ) [ 0 ] . res_id
address_id = partner_address_obj . browse ( cr , uid , model_id )
partner_address_obj . write ( cr , uid , [ address_id . id ] , { ' partner_id ' : partner_xml_id [ 0 ] } )
partner_contact_name = address_id . name
partner_contact_email = address_id . email
else :
partner_contact_name = val . get ( ' account_name ' )
return partner_contact_name , partner_contact_email
def get_opportunity_status ( sugar_obj , cr , uid , sugar_val , context = None ) :
fields = [ ' name ' , ' type ' ]
name = ' Opportunity_ ' + sugar_val [ ' sales_stage ' ]
data = [ sugar_val [ ' sales_stage ' ] , ' Opportunity ' ]
return import_object ( sugar_obj , cr , uid , fields , data , ' crm.case.stage ' , ' crm_stage ' , name , [ ( ' type ' , ' = ' , ' opportunity ' ) , ( ' name ' , ' ilike ' , sugar_val [ ' sales_stage ' ] ) ] , context )
if not context :
context = { }
2011-03-24 04:29:43 +00:00
lead_obj = sugar_obj . pool . get ( ' crm.lead ' )
2011-03-29 06:45:41 +00:00
partner_obj = sugar_obj . pool . get ( ' res.partner ' )
2011-03-24 04:29:43 +00:00
PortType , sessionid = sugar . login ( context . get ( ' username ' , ' ' ) , context . get ( ' password ' , ' ' ) , context . get ( ' url ' , ' ' ) )
sugar_data = sugar . search ( PortType , sessionid , ' Opportunities ' )
for val in sugar_data :
2011-03-29 06:45:41 +00:00
partner_xml_id = partner_obj . search ( cr , uid , [ ( ' name ' , ' like ' , val . get ( ' account_name ' ) ) ] )
2011-03-29 04:34:57 +00:00
if not partner_xml_id :
2011-04-01 05:34:11 +00:00
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Reference Partner %s cannot be created, due to Lower Record Limit in SugarCRM Configuration. ' ) % val . get ( ' account_name ' ) )
2011-04-26 05:03:45 +00:00
partner_contact_name , partner_contact_email = get_opportunity_contact ( sugar_obj , cr , uid , PortType , sessionid , val , partner_xml_id , context )
2011-04-08 06:52:51 +00:00
val [ ' partner_address_id/name ' ] = partner_contact_name
2011-04-26 05:03:45 +00:00
val [ ' email_from ' ] = partner_contact_email
2011-04-28 06:31:55 +00:00
val [ ' categ_id/id ' ] = get_category ( sugar_obj , cr , uid , ' crm.lead ' , val . get ( ' opportunity_type ' ) )
2011-03-24 04:29:43 +00:00
val [ ' type ' ] = ' opportunity '
2011-04-27 16:13:55 +00:00
val [ ' stage_id/id ' ] = get_opportunity_status ( sugar_obj , cr , uid , val , context )
2011-03-24 04:29:43 +00:00
fields , datas = sugarcrm_fields_mapping . sugarcrm_fields_mapp ( val , map_opportunity )
2011-05-02 14:51:13 +00:00
lead_obj . import_data ( cr , uid , fields , [ datas ] , mode = ' update ' , current_module = MODULE_NAME , noupdate = True , context = context )
2011-04-04 08:34:41 +00:00
return True
2011-03-24 04:29:43 +00:00
2011-04-27 16:13:55 +00:00
2011-03-24 04:29:43 +00:00
MAP_FIELDS = { ' Opportunities ' : #Object Mapping name
2011-04-21 13:03:16 +00:00
{ ' dependencies ' : [ ' Users ' , ' Accounts ' , ' Contacts ' , ' Leads ' ] , #Object to import before this table
2011-03-24 04:29:43 +00:00
' process ' : import_opportunities ,
} ,
' Leads ' :
2011-03-24 13:46:24 +00:00
{ ' dependencies ' : [ ' Users ' , ' Accounts ' , ' Contacts ' ] , #Object to import before this table
2011-03-24 04:29:43 +00:00
' process ' : import_leads ,
} ,
' Contacts ' :
2011-05-04 10:03:29 +00:00
{ ' dependencies ' : [ ' Users ' , ' Accounts ' ] , #Object to import before this table
2011-03-24 04:29:43 +00:00
' process ' : import_partner_address ,
} ,
' Accounts ' :
2011-04-26 05:03:45 +00:00
{ ' dependencies ' : [ ' Users ' ] , #Object to import before this table
2011-03-24 04:29:43 +00:00
' process ' : import_partners ,
} ,
' Users ' :
{ ' dependencies ' : [ ] ,
' process ' : import_users ,
} ,
2011-04-21 13:40:23 +00:00
' Documents ' :
{ ' dependencies ' : [ ' Users ' ] ,
' process ' : import_documents ,
2011-04-26 13:10:33 +00:00
} ,
2011-04-06 07:32:08 +00:00
' Meetings ' :
2011-04-28 13:24:42 +00:00
{ ' dependencies ' : [ ' Accounts ' , ' Contacts ' , ' Users ' , ' Projects ' , ' Opportunities ' , ' Tasks ' ] ,
2011-04-06 07:32:08 +00:00
' process ' : import_meetings ,
} ,
' Tasks ' :
2011-04-27 16:13:55 +00:00
{ ' dependencies ' : [ ' Accounts ' , ' Contacts ' , ' Users ' ] ,
2011-04-06 07:32:08 +00:00
' process ' : import_tasks ,
} ,
' Calls ' :
2011-04-27 16:13:55 +00:00
{ ' dependencies ' : [ ' Accounts ' , ' Contacts ' , ' Users ' , ' Opportunities ' ] ,
2011-04-06 07:32:08 +00:00
' process ' : import_calls ,
2011-04-27 16:13:55 +00:00
} ,
2011-04-07 08:57:52 +00:00
' Projects ' :
2011-04-13 13:22:40 +00:00
{ ' dependencies ' : [ ' Users ' , ' Accounts ' , ' Contacts ' ] ,
2011-04-07 08:57:52 +00:00
' process ' : import_projects ,
} ,
' Project Tasks ' :
{ ' dependencies ' : [ ' Users ' , ' Projects ' ] ,
' process ' : import_project_tasks ,
2011-04-13 10:07:29 +00:00
} ,
2011-04-13 12:25:11 +00:00
' Bugs ' :
{ ' dependencies ' : [ ' Users ' , ' Projects ' , ' Project Tasks ' ] ,
' process ' : import_bug ,
2011-04-27 16:13:55 +00:00
} ,
2011-04-28 06:31:55 +00:00
' Claims ' :
{ ' dependencies ' : [ ' Users ' , ' Accounts ' , ' Contacts ' , ' Leads ' ] ,
' process ' : import_claims ,
} ,
2011-04-27 16:13:55 +00:00
' Emails ' :
2011-04-28 04:51:56 +00:00
{ ' dependencies ' : [ ' Users ' , ' Projects ' , ' Project Tasks ' , ' Accounts ' , ' Contacts ' , ' Leads ' , ' Opportunities ' , ' Meetings ' , ' Calls ' ] ,
2011-04-27 16:13:55 +00:00
' process ' : import_emails ,
} ,
2011-04-14 12:51:54 +00:00
' Notes ' :
{ ' dependencies ' : [ ' Users ' , ' Projects ' , ' Project Tasks ' , ' Accounts ' , ' Contacts ' , ' Leads ' , ' Opportunities ' , ' Meetings ' , ' Calls ' ] ,
' process ' : import_history ,
2011-04-27 16:13:55 +00:00
} ,
' Employees ' :
2011-03-24 13:46:24 +00:00
{ ' dependencies ' : [ ' Users ' ] ,
2011-05-04 10:03:29 +00:00
' process ' : import_employees ,
} ,
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-04-21 15:24:58 +00:00
' 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 " ) ,
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 " ) ,
' meeting ' : fields . boolean ( ' Meetings ' , help = " If Meetings is checked, SugarCRM Meetings data imported in OpenERP meetings form " ) ,
' call ' : fields . boolean ( ' Calls ' , help = " If Calls is checked, SugarCRM Calls data imported in OpenERP phonecalls form " ) ,
2011-04-25 05:48:20 +00:00
' claim ' : fields . boolean ( ' Claims ' , help = " If Claims is checked, SugarCRM Claims data imported in OpenERP Claims form " ) ,
2011-04-11 09:56:03 +00:00
' email ' : fields . boolean ( ' Emails ' , help = " If Emails is checked, SugarCRM Emails data imported in OpenERP Emails form " ) ,
' project ' : fields . boolean ( ' Projects ' , help = " If Projects is checked, SugarCRM Projects data imported in OpenERP Projects form " ) ,
' project_task ' : fields . boolean ( ' Project Tasks ' , help = " If Project Tasks is checked, SugarCRM Project Tasks data imported in OpenERP Project Tasks form " ) ,
2011-04-21 13:40:23 +00:00
' task ' : fields . boolean ( ' Tasks ' , help = " If Tasks is checked, SugarCRM Tasks data imported in OpenERP Meetings 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-04-14 12:51:54 +00:00
' attachment ' : fields . boolean ( ' Attachments ' , help = " If Attachments is checked, SugarCRM Notes data imported in OpenERP ' s Related module ' s History with attachment " ) ,
2011-05-04 10:03:29 +00:00
' document ' : fields . boolean ( ' Documents ' , help = " If Documents is checked, SugarCRM Documents data imported in OpenERP Document Form " ) ,
2011-03-24 04:29:43 +00:00
' username ' : fields . char ( ' User Name ' , size = 64 ) ,
' password ' : fields . char ( ' Password ' , size = 24 ) ,
}
2011-04-25 05:48:20 +00:00
_defaults = { #to be set to true, but easier for debugging
2011-04-21 12:40:47 +00:00
' opportunity ' : False ,
' user ' : False ,
' contact ' : False ,
' account ' : False ,
' employee ' : False ,
' meeting ' : False ,
2011-05-04 10:03:29 +00:00
' task ' : False ,
' call ' : False ,
' claim ' : False ,
2011-04-21 12:40:47 +00:00
' email ' : False ,
' project ' : False ,
' project_task ' : False ,
' bug ' : False ,
2011-05-04 10:03:29 +00:00
' document ' : False
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 = [ ]
for current in self . browse ( cr , uid , ids , context ) :
if current . opportunity :
key_list . append ( ' Opportunities ' )
if current . user :
key_list . append ( ' Users ' )
if current . contact :
key_list . append ( ' Contacts ' )
if current . account :
2011-03-24 13:46:24 +00:00
key_list . append ( ' Accounts ' )
if current . employee :
2011-04-06 07:32:08 +00:00
key_list . append ( ' Employees ' )
if current . meeting :
key_list . append ( ' Meetings ' )
2011-04-21 13:40:23 +00:00
if current . task :
key_list . append ( ' Tasks ' )
2011-04-06 07:32:08 +00:00
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 :
key_list . append ( ' Claims ' )
2011-04-06 13:22:20 +00:00
if current . email :
2011-04-07 08:57:52 +00:00
key_list . append ( ' Emails ' )
if current . project :
key_list . append ( ' Projects ' )
if current . project_task :
2011-04-12 12:27:59 +00:00
key_list . append ( ' Project Tasks ' )
2011-04-13 12:25:11 +00:00
if current . bug :
2011-04-14 12:51:54 +00:00
key_list . append ( ' Bugs ' )
if current . attachment :
2011-04-21 13:40:23 +00:00
key_list . append ( ' Notes ' )
if current . document :
key_list . append ( ' Documents ' )
2011-03-24 04:29:43 +00:00
return key_list
def import_all ( self , cr , uid , ids , context = None ) :
""" Import all sugarcrm data into openerp module """
if not context :
context = { }
keys = self . get_key ( cr , uid , ids , context )
imported = set ( ) #to invoid importing 2 times the sames modules
for key in keys :
if not key in imported :
self . resolve_dependencies ( cr , uid , MAP_FIELDS , MAP_FIELDS [ key ] [ ' dependencies ' ] , imported , context = context )
MAP_FIELDS [ key ] [ ' process ' ] ( self , cr , uid , context )
imported . add ( key )
obj_model = self . pool . get ( ' ir.model.data ' )
model_data_ids = obj_model . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' import.message.form ' ) ] )
resource_id = obj_model . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] )
return {
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' import.message ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
def resolve_dependencies ( self , cr , uid , dict , dep , imported , context = None ) :
for dependency in dep :
if not dependency in imported :
self . resolve_dependencies ( cr , uid , dict , dict [ dependency ] [ ' dependencies ' ] , imported , context = context )
dict [ dependency ] [ ' process ' ] ( self , cr , uid , context )
imported . add ( dependency )
2011-04-04 08:34:41 +00:00
return True
2011-03-24 04:29:43 +00:00
import_sugarcrm ( )