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.

149 lines
6.1 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. import logging
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. import phonenumbers
  10. except ImportError:
  11. _logger.debug('Cannot `import phonenumbers`.')
  12. class PhoneCommon(models.AbstractModel):
  13. _name = 'phone.common'
  14. _description = 'Common methods for phone features'
  15. @api.model
  16. def get_name_from_phone_number(self, presented_number):
  17. '''Function to get name from phone number. Usefull for use from IPBX
  18. to add CallerID name to incoming calls.'''
  19. res = self.get_record_from_phone_number(presented_number)
  20. if res:
  21. return res[2]
  22. else:
  23. return False
  24. @api.model
  25. def get_record_from_phone_number(self, presented_number):
  26. '''If it finds something, it returns (object name, ID, record name)
  27. For example : ('res.partner', 42, 'Alexis de Lattre (Akretion)')
  28. '''
  29. _logger.debug(
  30. "Call get_name_from_phone_number with number = %s"
  31. % presented_number)
  32. if not isinstance(presented_number, str):
  33. _logger.warning(
  34. "Number '%s' should be a 'str' but it is a '%s'"
  35. % (presented_number, type(presented_number)))
  36. return False
  37. if not presented_number.isdigit():
  38. _logger.warning(
  39. "Number '%s' should only contain digits." % presented_number)
  40. nr_digits_to_match_from_end = \
  41. self.env.user.company_id.number_of_digits_to_match_from_end
  42. if len(presented_number) >= nr_digits_to_match_from_end:
  43. end_number_to_match = presented_number[
  44. -nr_digits_to_match_from_end:len(presented_number)]
  45. else:
  46. end_number_to_match = presented_number
  47. sorted_phonemodels = self._get_phone_models()
  48. for obj_dict in sorted_phonemodels:
  49. obj = obj_dict['object']
  50. pg_search_number = '%' + end_number_to_match
  51. _logger.debug(
  52. "Will search phone and mobile numbers in %s ending with '%s'",
  53. obj._name, end_number_to_match)
  54. sql = "SELECT id FROM %s WHERE " % obj._table
  55. sql_where = []
  56. sql_args = []
  57. for field in obj_dict['fields']:
  58. sql_where.append("replace(%s, ' ', '') ilike %%s" % field)
  59. sql_args.append(pg_search_number)
  60. sql = sql + ' or '.join(sql_where)
  61. _logger.debug("get_record_from_phone_number sql=%s", sql)
  62. self._cr.execute(sql, tuple(sql_args))
  63. res_sql = self._cr.fetchall()
  64. if len(res_sql) > 1:
  65. res_ids = [x[0] for x in res_sql]
  66. _logger.warning(
  67. u"There are several %s (IDS = %s) with a phone number "
  68. "ending with '%s'. Taking the first one.",
  69. obj._name, res_ids, end_number_to_match)
  70. if res_sql:
  71. obj_id = res_sql[0][0]
  72. res_obj = obj.browse(obj_id)
  73. name = res_obj.display_name
  74. res = (obj._name, res_obj.id, name)
  75. _logger.debug(
  76. u"Answer get_record_from_phone_number: (%s, %d, %s)",
  77. res[0], res[1], res[2])
  78. return res
  79. else:
  80. _logger.debug(
  81. u"No match on %s for end of phone number '%s'",
  82. obj._name, end_number_to_match)
  83. return False
  84. @api.model
  85. def _get_phone_models(self):
  86. phoneobj = []
  87. for model_name in self.env.registry.keys():
  88. senv = False
  89. try:
  90. senv = self.with_context(callerid=True).env[model_name]
  91. except Exception:
  92. continue
  93. if (
  94. hasattr(senv, '_phone_name_sequence') and
  95. isinstance(senv._phone_name_sequence, int) and
  96. hasattr(senv, '_phone_name_fields') and
  97. isinstance(senv._phone_name_fields, list)):
  98. cdict = {
  99. 'object': senv,
  100. 'fields': senv._phone_name_fields,
  101. }
  102. phoneobj.append((senv._phone_name_sequence, cdict))
  103. phoneobj_sorted = sorted(phoneobj, key=lambda element: element[0])
  104. res = []
  105. for l in phoneobj_sorted:
  106. res.append(l[1])
  107. # [{'fields': ['fax', 'phone', 'mobile'], 'object': res.partner()},
  108. # {'fields': ['fax', 'phone', 'mobile'], 'object': crm.lead()}]
  109. return res
  110. @api.model
  111. def click2dial(self, erp_number):
  112. '''This function is designed to be overridden in IPBX-specific
  113. modules, such as asterisk_click2dial or ovh_telephony_connector'''
  114. return {'dialed_number': erp_number}
  115. @api.model
  116. def convert_to_dial_number(self, erp_number):
  117. '''
  118. This function is dedicated to the transformation of the number
  119. available in Odoo to the number that can be dialed.
  120. You may have to inherit this function in another module specific
  121. for your company if you are not happy with the way I reformat
  122. the numbers.
  123. '''
  124. assert(erp_number), 'Missing phone number'
  125. _logger.debug('Number before reformat = %s' % erp_number)
  126. # erp_number are supposed to be in International format, so no need to
  127. # give a country code here
  128. parsed_num = phonenumbers.parse(erp_number, None)
  129. country_code = self.env.user.company_id.country_id.code
  130. assert(country_code), 'Missing country on company'
  131. _logger.debug('Country code = %s' % country_code)
  132. to_dial_number = phonenumbers.format_out_of_country_calling_number(
  133. parsed_num, country_code.upper())
  134. to_dial_number = to_dial_number.translate(
  135. to_dial_number.maketrans('', '', ' -.()/'))
  136. _logger.debug('Number to be sent to phone system: %s' % to_dial_number)
  137. return to_dial_number