You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
11 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2010-2012 Alexis de Lattre <alexis@via.ecp.fr>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from osv import osv, fields
  22. # Lib required to open a socket (needed to communicate with Asterisk server)
  23. import logging
  24. # Lib to translate error messages
  25. from tools.translate import _
  26. _logger = logging.getLogger(__name__)
  27. class wizard_open_calling_partner(osv.osv_memory):
  28. _name = "wizard.open.calling.partner"
  29. _description = "Open calling partner"
  30. _columns = {
  31. # I can't set any field to readonly, because otherwize it would call
  32. # default_get (and thus connect to Asterisk) a second time when the user
  33. # clicks on one of the buttons
  34. 'calling_number': fields.char('Calling number', size=30, help="Phone number of calling party that has been obtained from Asterisk."),
  35. '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"),
  36. 'partner_id': fields.many2one('res.partner', 'Partner', help="Partner related to the calling number."),
  37. '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"),
  38. 'current_phone': fields.related('to_update_partner_address_id', 'phone', type='char', relation='res.partner.address', string='Current phone'),
  39. 'current_mobile': fields.related('to_update_partner_address_id', 'mobile', type='char', relation='res.partner.address', string='Current mobile'),
  40. }
  41. def default_get(self, cr, uid, fields, context=None):
  42. '''Thanks to the default_get method, we are able to query Asterisk and
  43. get the corresponding partner when we launch the wizard'''
  44. res = {}
  45. calling_number = self.pool.get('asterisk.server')._connect_to_asterisk(cr, uid, method='get_calling_number', context=context)
  46. #To test the code without Asterisk server
  47. #calling_number = "0141981242"
  48. if calling_number:
  49. res['calling_number'] = calling_number
  50. # We match only on the end of the phone number
  51. if len(calling_number) >= 9:
  52. number_to_search = calling_number[-9:len(calling_number)]
  53. else:
  54. number_to_search = calling_number
  55. partner = self.pool.get('res.partner.address').get_partner_from_phone_number(cr, uid, number_to_search, context=context)
  56. if partner:
  57. res['partner_address_id'] = partner[0]
  58. res['partner_id'] = partner[1]
  59. else:
  60. res['partner_id'] = False
  61. res['partner_address_id'] = False
  62. res['to_update_partner_address_id'] = False
  63. else:
  64. _logger.debug("Could not get the calling number from Asterisk.")
  65. raise osv.except_osv(_('Error :'), _("Could not get the calling number from Asterisk. Are you currently on the phone ? If yes, check your setup and look at the OpenERP debug logs."))
  66. return res
  67. def open_filtered_object(self, cr, uid, ids, oerp_object, context=None):
  68. '''Returns the action that opens the list view of the 'oerp_object'
  69. given as argument filtered on the partner'''
  70. # This module only depends on "base"
  71. # and I don't want to add a dependancy on "sale" or "account"
  72. # So I just check here that the model exists, to avoid a crash
  73. if not self.pool.get('ir.model').search(cr, uid, [('model', '=', oerp_object._name)], context=context):
  74. 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))
  75. partner = self.read(cr, uid, ids[0], ['partner_id'], context=context)['partner_id']
  76. if partner:
  77. action = {
  78. 'name': oerp_object._description,
  79. 'view_type': 'form',
  80. 'view_mode': 'tree,form',
  81. 'res_model': oerp_object._name,
  82. 'type': 'ir.actions.act_window',
  83. 'nodestroy': False, # close the pop-up wizard after action
  84. 'target': 'current',
  85. 'domain': [('partner_id', '=', partner[0])],
  86. }
  87. return action
  88. else:
  89. return False
  90. def open_sale_orders(self, cr, uid, ids, context=None):
  91. '''Function called by the related button of the wizard'''
  92. return self.open_filtered_object(cr, uid, ids, self.pool.get('sale.order'), context=context)
  93. def open_invoices(self, cr, uid, ids, context=None):
  94. '''Function called by the related button of the wizard'''
  95. return self.open_filtered_object(cr, uid, ids, self.pool.get('account.invoice'), context=context)
  96. def simple_open(self, cr, uid, ids, object_name='res.partner', context=None):
  97. if object_name == 'res.partner':
  98. field = 'partner_id'
  99. label = 'Partner'
  100. elif object_name == 'res.partner.address':
  101. field = 'partner_address_id'
  102. label = 'Contact'
  103. else:
  104. raise osv.except_osv(_('Error :'), "This object '%s' is not supported" % object_name)
  105. record_to_open = self.read(cr, uid, ids[0], [field], context=context)[field]
  106. if record_to_open:
  107. return {
  108. 'name': label,
  109. 'view_type': 'form',
  110. 'view_mode': 'form,tree',
  111. 'res_model': object_name,
  112. 'type': 'ir.actions.act_window',
  113. 'nodestroy': False, # close the pop-up wizard after action
  114. 'target': 'current',
  115. 'res_id': record_to_open[0],
  116. }
  117. else:
  118. return False
  119. def open_partner(self, cr, uid, ids, context=None):
  120. '''Function called by the related button of the wizard'''
  121. return self.simple_open(cr, uid, ids, object_name='res.partner', context=context)
  122. def open_partner_address(self, cr, uid, ids, context=None):
  123. '''Function called by the related button of the wizard'''
  124. return self.simple_open(cr, uid, ids, object_name='res.partner.address', context=context)
  125. def create_partner_address(self, cr, uid, ids, phone_type='phone', context=None):
  126. '''Function called by the related button of the wizard'''
  127. calling_number = self.read(cr, uid, ids[0], ['calling_number'], context=context)['calling_number']
  128. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  129. ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, user, context=context)
  130. # Convert the number to the international format
  131. number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, calling_number, ast_server, context=context)
  132. new_partner_address_id = self.pool.get('res.partner.address').create(cr, uid, {phone_type: number_to_write}, context=context)
  133. action = {
  134. 'name': 'Create new contact',
  135. 'view_type': 'form',
  136. 'view_mode': 'form,tree',
  137. 'res_model': 'res.partner.address',
  138. 'type': 'ir.actions.act_window',
  139. 'nodestroy': False,
  140. 'target': 'current',
  141. 'res_id': new_partner_address_id,
  142. }
  143. return action
  144. def create_partner_address_phone(self, cr, uid, ids, context=None):
  145. return self.create_partner_address(cr, uid, ids, phone_type='phone', context=context)
  146. def create_partner_address_mobile(self, cr, uid, ids, context=None):
  147. return self.create_partner_address(cr, uid, ids, phone_type='mobile', context=context)
  148. def update_partner_address(self, cr, uid, ids, phone_type='mobile', context=None):
  149. cur_wizard = self.browse(cr, uid, ids[0], context=context)
  150. if not cur_wizard.to_update_partner_address_id:
  151. raise osv.except_osv(_('Error :'), _("Select the contact to update."))
  152. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  153. ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, user, context=context)
  154. number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, cur_wizard.calling_number, ast_server, context=context)
  155. self.pool.get('res.partner.address').write(cr, uid, cur_wizard.to_update_partner_address_id.id, {phone_type: number_to_write}, context=context)
  156. action = {
  157. 'name': 'Contact: ' + cur_wizard.to_update_partner_address_id.name,
  158. 'view_type': 'form',
  159. 'view_mode': 'form,tree',
  160. 'res_model': 'res.partner.address',
  161. 'type': 'ir.actions.act_window',
  162. 'nodestroy': False,
  163. 'target': 'current',
  164. 'res_id': cur_wizard.to_update_partner_address_id.id
  165. }
  166. return action
  167. def update_partner_address_phone(self, cr, uid, ids, context=None):
  168. return self.update_partner_address(cr, uid, ids, phone_type='phone', context=context)
  169. def update_partner_address_mobile(self, cr, uid, ids, context=None):
  170. return self.update_partner_address(cr, uid, ids, phone_type='mobile', context=context)
  171. def onchange_to_update_partner_address(self, cr, uid, ids, to_update_partner_address_id, context=None):
  172. res = {}
  173. res['value'] = {}
  174. if to_update_partner_address_id:
  175. to_update_partner_address = self.pool.get('res.partner.address').browse(cr, uid, to_update_partner_address_id, context=context)
  176. res['value'].update({'current_phone': to_update_partner_address.phone,
  177. 'current_mobile': to_update_partner_address.mobile})
  178. else:
  179. res['value'].update({'current_phone': False, 'current_mobile': False})
  180. return res
  181. wizard_open_calling_partner()