@ -20,7 +20,6 @@
##############################################################################
##############################################################################
from osv import osv , fields
from osv import osv , fields
# Lib required to open a socket (needed to communicate with Asterisk server)
import logging
import logging
# Lib to translate error messages
# Lib to translate error messages
from tools.translate import _
from tools.translate import _
@ -37,11 +36,11 @@ class wizard_open_calling_partner(osv.osv_memory):
# default_get (and thus connect to Asterisk) a second time when the user
# default_get (and thus connect to Asterisk) a second time when the user
# clicks on one of the buttons
# clicks on one of the buttons
' calling_number ' : fields . char ( ' Calling number ' , size = 30 , help = " Phone number of calling party that has been obtained from Asterisk. " ) ,
' calling_number ' : fields . char ( ' Calling number ' , size = 30 , help = " Phone number of calling party that has been obtained from Asterisk. " ) ,
' partner_address_ id ' : fields . many2one ( ' res.partner.address ' , ' Contact name' , help = " Partner contact related to the calling number. If there is none and you want to update an existing partner " ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , help = " Partner related to the calling number. " ) ,
' to_update_partner_address_ id ' : fields . many2one ( ' res.partner.address ' , ' Contact to update' , help = " Partner contact on which the phone or mobile number will be written " ) ,
' current_phone ' : fields . related ( ' to_update_partner_address_ id ' , ' phone ' , type = ' char ' , relation = ' res.partner.address ' , string = ' Current phone ' ) ,
' current_mobile ' : fields . related ( ' to_update_partner_address_ id ' , ' mobile ' , type = ' char ' , relation = ' res.partner.address ' , string = ' Current mobile ' ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner name' , help = " Partner related to the calling number. " ) ,
' parent_par tner_id ' : fields . many2one ( ' res.partner ' , ' Parent par tner ' , help = " Parent Partner related to the calling number." ) ,
' to_update_partner_id ' : fields . many2one ( ' res.partner ' , ' Partner to update' , help = " Partner on which the phone or mobile number will be written " ) ,
' current_phone ' : fields . related ( ' to_update_partner_id ' , ' phone ' , type = ' char ' , relation = ' res.partner ' , string = ' Current phone ' ) ,
' current_mobile ' : fields . related ( ' to_update_partner_id ' , ' mobile ' , type = ' char ' , relation = ' res.partner ' , string = ' Current mobile ' ) ,
}
}
@ -49,27 +48,28 @@ class wizard_open_calling_partner(osv.osv_memory):
''' Thanks to the default_get method, we are able to query Asterisk and
''' Thanks to the default_get method, we are able to query Asterisk and
get the corresponding partner when we launch the wizard '''
get the corresponding partner when we launch the wizard '''
res = { }
res = { }
calling_number = self . pool . get ( ' asterisk.server ' ) . _get_calling_number ( cr , uid , context = context )
# calling_number = self.pool.get('asterisk.server')._get_calling_number(cr, uid, context=context )
#To test the code without Asterisk server
#To test the code without Asterisk server
#calling_number = "0141981242 "
calling_number = " 0141981242 "
if calling_number :
if calling_number :
res [ ' calling_number ' ] = calling_number
res [ ' calling_number ' ] = calling_number
# We match only on the end of the phone number
# We match only on the end of the phone number
# TODO : make this parameter configurable
if len ( calling_number ) > = 9 :
if len ( calling_number ) > = 9 :
number_to_search = calling_number [ - 9 : len ( calling_number ) ]
number_to_search = calling_number [ - 9 : len ( calling_number ) ]
else :
else :
number_to_search = calling_number
number_to_search = calling_number
partner = self . pool . get ( ' res.partner.address ' ) . get_partner_from_phone_number ( cr , uid , number_to_search , context = context )
partner = self . pool . get ( ' res.partner ' ) . get_partner_from_phone_number ( cr , uid , number_to_search , context = context )
if partner :
if partner :
res [ ' partner_address_ id ' ] = partner [ 0 ]
res [ ' partner_id ' ] = partner [ 1 ]
res [ ' partner_id ' ] = partner [ 0 ]
res [ ' parent_par tner_id ' ] = partner [ 1 ]
else :
else :
res [ ' partner_id ' ] = False
res [ ' partner_id ' ] = False
res [ ' partner_address _id ' ] = False
res [ ' to_update_partner_address_ id ' ] = False
res [ ' parent_partner _id ' ] = False
res [ ' to_update_partner_id ' ] = False
else :
else :
_logger . debug ( " Could not get the calling number from Asterisk. " )
_logger . debug ( " Could not get the calling number from Asterisk. " )
raise osv . except_osv ( _ ( ' Error : ' ) , _ ( " Could not get the calling number from Asterisk. A re you currently on the phone ? If yes, check your setup and look at the OpenERP debug logs. " ) )
raise osv . except_osv ( _ ( ' Error : ' ) , _ ( " Could not get the calling number from Asterisk. Is your phone ringing or a re you currently on the phone ? If yes, check your setup and look at the OpenERP debug logs. " ) )
return res
return res
@ -83,8 +83,9 @@ class wizard_open_calling_partner(osv.osv_memory):
if not self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , oerp_object . _name ) ] , context = context ) :
if not self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , oerp_object . _name ) ] , context = context ) :
raise osv . except_osv ( _ ( ' Error : ' ) , _ ( " The object ' %s ' is not found in your OpenERP database, probably because the related module is not installed. " % oerp_object . _description ) )
raise osv . except_osv ( _ ( ' Error : ' ) , _ ( " The object ' %s ' is not found in your OpenERP database, probably because the related module is not installed. " % oerp_object . _description ) )
partner = self . read ( cr , uid , ids [ 0 ] , [ ' partner_id ' ] , context = context ) [ ' partner_id ' ]
if partner :
partner = self . read ( cr , uid , ids [ 0 ] , [ ' partner_id ' , ' parent_partner_id ' ] , context = context )
partner_id_to_filter = partner [ ' parent_partner_id ' ] and partner [ ' parent_partner_id ' ] [ 0 ] or ( partner [ ' partner_id ' ] and partner [ ' partner_id ' ] [ 0 ] or False )
if partner_id_to_filter :
action = {
action = {
' name ' : oerp_object . _description ,
' name ' : oerp_object . _description ,
' view_type ' : ' form ' ,
' view_type ' : ' form ' ,
@ -93,7 +94,7 @@ class wizard_open_calling_partner(osv.osv_memory):
' type ' : ' ir.actions.act_window ' ,
' type ' : ' ir.actions.act_window ' ,
' nodestroy ' : False , # close the pop-up wizard after action
' nodestroy ' : False , # close the pop-up wizard after action
' target ' : ' current ' ,
' target ' : ' current ' ,
' domain ' : [ ( ' partner_id ' , ' = ' , partner [ 0 ] ) ] ,
' domain ' : [ ( ' partner_id ' , ' = ' , partner_id_to_filter ) ] ,
}
}
return action
return action
else :
else :
@ -110,22 +111,14 @@ class wizard_open_calling_partner(osv.osv_memory):
return self . open_filtered_object ( cr , uid , ids , self . pool . get ( ' account.invoice ' ) , context = context )
return self . open_filtered_object ( cr , uid , ids , self . pool . get ( ' account.invoice ' ) , context = context )
def simple_open ( self , cr , uid , ids , object_name = ' res.partner ' , context = None ) :
if object_name == ' res.partner ' :
field = ' partner_id '
label = ' Partner '
elif object_name == ' res.partner.address ' :
field = ' partner_address_id '
label = ' Contact '
else :
raise osv . except_osv ( _ ( ' Error : ' ) , " This object ' %s ' is not supported " % object_name )
def simple_open ( self , cr , uid , ids , field = ' partner_id ' , context = None ) :
record_to_open = self . read ( cr , uid , ids [ 0 ] , [ field ] , context = context ) [ field ]
record_to_open = self . read ( cr , uid , ids [ 0 ] , [ field ] , context = context ) [ field ]
if record_to_open :
if record_to_open :
return {
return {
' name ' : label ,
' name ' : self . pool . get ( ' res.partner ' ) . _description ,
' view_type ' : ' form ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form,tree ' ,
' view_mode ' : ' form,tree ' ,
' res_model ' : object_name ,
' res_model ' : ' res.partner ' ,
' type ' : ' ir.actions.act_window ' ,
' type ' : ' ir.actions.act_window ' ,
' nodestroy ' : False , # close the pop-up wizard after action
' nodestroy ' : False , # close the pop-up wizard after action
' target ' : ' current ' ,
' target ' : ' current ' ,
@ -137,15 +130,16 @@ class wizard_open_calling_partner(osv.osv_memory):
def open_partner ( self , cr , uid , ids , context = None ) :
def open_partner ( self , cr , uid , ids , context = None ) :
''' Function called by the related button of the wizard '''
''' Function called by the related button of the wizard '''
return self . simple_open ( cr , uid , ids , object_name = ' res. partner' , context = context )
return self . simple_open ( cr , uid , ids , field = ' partner_id ' , context = context )
def open_partner_address ( self , cr , uid , ids , context = None ) :
# TODO
def open_parent_partner ( self , cr , uid , ids , context = None ) :
''' Function called by the related button of the wizard '''
''' Function called by the related button of the wizard '''
return self . simple_open ( cr , uid , ids , object_name = ' res.partner.address ' , context = context )
return self . simple_open ( cr , uid , ids , field = ' parent_partner_id ' , context = context )
def create_partner_address ( self , cr , uid , ids , phone_type = ' phone ' , context = None ) :
def create_partner ( self , cr , uid , ids , phone_type = ' phone ' , context = None ) :
''' Function called by the related button of the wizard '''
''' Function called by the related button of the wizard '''
calling_number = self . read ( cr , uid , ids [ 0 ] , [ ' calling_number ' ] , context = context ) [ ' calling_number ' ]
calling_number = self . read ( cr , uid , ids [ 0 ] , [ ' calling_number ' ] , context = context ) [ ' calling_number ' ]
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
@ -153,64 +147,67 @@ class wizard_open_calling_partner(osv.osv_memory):
# Convert the number to the international format
# Convert the number to the international format
number_to_write = self . pool . get ( ' asterisk.server ' ) . _convert_number_to_international_format ( cr , uid , calling_number , ast_server , context = context )
number_to_write = self . pool . get ( ' asterisk.server ' ) . _convert_number_to_international_format ( cr , uid , calling_number , ast_server , context = context )
new_partner_address_id = self . pool . get ( ' res.partner.address ' ) . create ( cr , uid , { phone_type : number_to_write } , context = context )
new_partner_id = self . pool . get ( ' res.partner ' ) . create ( cr , uid , {
' name ' : ' WRITE PARTNER NAME HERE ' ,
phone_type : number_to_write ,
} , context = context )
action = {
action = {
' name ' : ' Create new contact ' ,
' name ' : ' Create new partner ' ,
' view_type ' : ' form ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form,tree ' ,
' view_mode ' : ' form,tree ' ,
' res_model ' : ' res.partner.address ' ,
' res_model ' : ' res.partner ' ,
' type ' : ' ir.actions.act_window ' ,
' type ' : ' ir.actions.act_window ' ,
' nodestroy ' : False ,
' nodestroy ' : False ,
' target ' : ' current ' ,
' target ' : ' current ' ,
' res_id ' : new_partner_address_ id ,
' res_id ' : new_partner_id ,
}
}
return action
return action
def create_partner_address_ phone ( self , cr , uid , ids , context = None ) :
return self . create_partner_address ( cr , uid , ids , phone_type = ' phone ' , context = context )
def create_partner_phone ( self , cr , uid , ids , context = None ) :
return self . create_partner ( cr , uid , ids , phone_type = ' phone ' , context = context )
def create_partner_address_ mobile ( self , cr , uid , ids , context = None ) :
return self . create_partner_address ( cr , uid , ids , phone_type = ' mobile ' , context = context )
def create_partner_mobile ( self , cr , uid , ids , context = None ) :
return self . create_partner ( cr , uid , ids , phone_type = ' mobile ' , context = context )
def update_partner_address ( self , cr , uid , ids , phone_type = ' mobile ' , context = None ) :
def update_partner ( self , cr , uid , ids , phone_type = ' mobile ' , context = None ) :
cur_wizard = self . browse ( cr , uid , ids [ 0 ] , context = context )
cur_wizard = self . browse ( cr , uid , ids [ 0 ] , context = context )
if not cur_wizard . to_update_partner_address_ id :
raise osv . except_osv ( _ ( ' Error : ' ) , _ ( " Select the contact to update. " ) )
if not cur_wizard . to_update_partner_id :
raise osv . except_osv ( _ ( ' Error : ' ) , _ ( " Select the partner to update. " ) )
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
ast_server = self . pool . get ( ' asterisk.server ' ) . _get_asterisk_server_from_user ( cr , uid , user , context = context )
ast_server = self . pool . get ( ' asterisk.server ' ) . _get_asterisk_server_from_user ( cr , uid , user , context = context )
number_to_write = self . pool . get ( ' asterisk.server ' ) . _convert_number_to_international_format ( cr , uid , cur_wizard . calling_number , ast_server , context = context )
number_to_write = self . pool . get ( ' asterisk.server ' ) . _convert_number_to_international_format ( cr , uid , cur_wizard . calling_number , ast_server , context = context )
self . pool . get ( ' res.partner.address ' ) . write ( cr , uid , cur_wizard . to_update_partner_address _id . id , { phone_type : number_to_write } , context = context )
self . pool . get ( ' res.partner ' ) . write ( cr , uid , cur_wizard . to_update_partner_id . id , { phone_type : number_to_write } , context = context )
action = {
action = {
' name ' : ' Contact : ' + cur_wizard . to_update_partner_address _id . name ,
' name ' : ' Partner : ' + cur_wizard . to_update_partner_id . name ,
' view_type ' : ' form ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form,tree ' ,
' view_mode ' : ' form,tree ' ,
' res_model ' : ' res.partner.address ' ,
' res_model ' : ' res.partner ' ,
' type ' : ' ir.actions.act_window ' ,
' type ' : ' ir.actions.act_window ' ,
' nodestroy ' : False ,
' nodestroy ' : False ,
' target ' : ' current ' ,
' target ' : ' current ' ,
' res_id ' : cur_wizard . to_update_partner_address_ id . id
' res_id ' : cur_wizard . to_update_partner_id . id
}
}
return action
return action
def update_partner_address_ phone ( self , cr , uid , ids , context = None ) :
return self . update_partner_address ( cr , uid , ids , phone_type = ' phone ' , context = context )
def update_partner_phone ( self , cr , uid , ids , context = None ) :
return self . update_partner ( cr , uid , ids , phone_type = ' phone ' , context = context )
def update_partner_address_ mobile ( self , cr , uid , ids , context = None ) :
return self . update_partner_address ( cr , uid , ids , phone_type = ' mobile ' , context = context )
def update_partner_mobile ( self , cr , uid , ids , context = None ) :
return self . update_partner ( cr , uid , ids , phone_type = ' mobile ' , context = context )
def onchange_to_update_partner_address ( self , cr , uid , ids , to_update_partner_address _id , context = None ) :
def onchange_to_update_partner ( self , cr , uid , ids , to_update_partner_id , context = None ) :
res = { }
res = { }
res [ ' value ' ] = { }
res [ ' value ' ] = { }
if to_update_partner_address_ id :
to_update_partner_address = self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , to_update_partner_address _id , context = context )
res [ ' value ' ] . update ( { ' current_phone ' : to_update_partner_address . phone ,
' current_mobile ' : to_update_partner_address . mobile } )
if to_update_partner_id :
to_update_partner = self . pool . get ( ' res.partner ' ) . browse ( cr , uid , to_update_partner_id , context = context )
res [ ' value ' ] . update ( { ' current_phone ' : to_update_partner . phone ,
' current_mobile ' : to_update_partner . mobile } )
else :
else :
res [ ' value ' ] . update ( { ' current_phone ' : False , ' current_mobile ' : False } )
res [ ' value ' ] . update ( { ' current_phone ' : False , ' current_mobile ' : False } )
return res
return res