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.

73 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2010-2018 Akretion France
  3. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import models, api, _
  6. from odoo.exceptions import UserError
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class PhoneCommon(models.AbstractModel):
  10. _inherit = 'phone.common'
  11. @api.model
  12. def click2dial(self, erp_number):
  13. res = super(PhoneCommon, self).click2dial(erp_number)
  14. if not erp_number:
  15. raise UserError(_('Missing phone number'))
  16. user, ast_server, ast_manager = \
  17. self.env['asterisk.server']._connect_to_asterisk()
  18. ast_number = self.convert_to_dial_number(erp_number)
  19. # Add 'out prefix'
  20. if ast_server.out_prefix:
  21. _logger.debug('Out prefix = %s', ast_server.out_prefix)
  22. ast_number = '%s%s' % (ast_server.out_prefix, ast_number)
  23. _logger.debug('Number to be sent to Asterisk = %s', ast_number)
  24. # The user should have a CallerID
  25. if not user.callerid:
  26. raise UserError(_('No callerID configured for the current user'))
  27. variable = []
  28. if user.asterisk_chan_type == 'SIP':
  29. # We can only have one alert-info header in a SIP request
  30. if user.alert_info:
  31. variable.append(
  32. 'SIPAddHeader=Alert-Info: %s' % user.alert_info)
  33. elif ast_server.alert_info:
  34. variable.append(
  35. 'SIPAddHeader=Alert-Info: %s' % ast_server.alert_info)
  36. if user.variable:
  37. for user_variable in user.variable.split('|'):
  38. variable.append(user_variable.strip())
  39. channel = '%s/%s' % (user.asterisk_chan_type, user.resource)
  40. if user.dial_suffix:
  41. channel += '/%s' % user.dial_suffix
  42. try:
  43. ast_manager.Originate(
  44. channel,
  45. context=ast_server.context,
  46. extension=ast_number,
  47. priority=str(ast_server.extension_priority),
  48. timeout=str(ast_server.wait_time * 1000),
  49. caller_id=user.callerid,
  50. account=user.cdraccount,
  51. variable=variable)
  52. except Exception as e:
  53. _logger.error(
  54. "Error in the Originate request to Asterisk server %s",
  55. ast_server.ip_address)
  56. _logger.error(
  57. "Here are the details of the error: '%s'", str(e))
  58. raise UserError(_(
  59. "Click to dial with Asterisk failed.\nHere is the error: "
  60. "'%s'") % str(e))
  61. finally:
  62. ast_manager.Logoff()
  63. res['dialed_number'] = ast_number
  64. return res