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.

141 lines
5.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2010-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import models, api
  5. from .. import fields as phone_fields
  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. @api.model
  15. def get_name_from_phone_number(self, presented_number):
  16. '''Function to get name from phone number. Usefull for use from IPBX
  17. to add CallerID name to incoming calls.'''
  18. res = self.get_record_from_phone_number(presented_number)
  19. if res:
  20. return res[2]
  21. else:
  22. return False
  23. @api.model
  24. def get_record_from_phone_number(self, presented_number):
  25. '''If it finds something, it returns (object name, ID, record name)
  26. For example : ('res.partner', 42, u'Alexis de Lattre (Akretion)')
  27. '''
  28. _logger.debug(
  29. u"Call get_name_from_phone_number with number = %s"
  30. % presented_number)
  31. if not isinstance(presented_number, (str, unicode)):
  32. _logger.warning(
  33. u"Number '%s' should be a 'str' or 'unicode' but it is a '%s'"
  34. % (presented_number, type(presented_number)))
  35. return False
  36. if not presented_number.isdigit():
  37. _logger.warning(
  38. u"Number '%s' should only contain digits." % presented_number)
  39. nr_digits_to_match_from_end = \
  40. self.env.user.company_id.number_of_digits_to_match_from_end
  41. if len(presented_number) >= nr_digits_to_match_from_end:
  42. end_number_to_match = presented_number[
  43. -nr_digits_to_match_from_end:len(presented_number)]
  44. else:
  45. end_number_to_match = presented_number
  46. sorted_phonemodels = self._get_phone_models()
  47. for obj_dict in sorted_phonemodels:
  48. obj = obj_dict['object']
  49. pg_search_number = str('%' + end_number_to_match)
  50. _logger.debug(
  51. "Will search phone and mobile numbers in %s ending with '%s'",
  52. obj._name, end_number_to_match)
  53. domain = []
  54. for field in obj_dict['fields']:
  55. domain.append((field, '=like', pg_search_number))
  56. if len(domain) > 1:
  57. domain = ['|'] * (len(domain) - 1) + domain
  58. _logger.debug('searching on %s with domain=%s', obj._name, domain)
  59. res_obj = obj.search(domain)
  60. if len(res_obj) > 1:
  61. _logger.warning(
  62. u"There are several %s (IDS = %s) with a phone number "
  63. "ending with '%s'. Taking the first one.",
  64. obj._name, res_obj.ids, end_number_to_match)
  65. res_obj = res_obj[0]
  66. if res_obj:
  67. name = res_obj.name_get()[0][1]
  68. res = (obj._name, res_obj.id, name)
  69. _logger.debug(
  70. u"Answer get_record_from_phone_number: (%s, %d, %s)",
  71. res[0], res[1], res[2])
  72. return res
  73. else:
  74. _logger.debug(
  75. u"No match on %s for end of phone number '%s'",
  76. obj._name, end_number_to_match)
  77. return False
  78. @api.model
  79. def _get_phone_models(self):
  80. phoneobj = []
  81. for model_name in self.env.registry.keys():
  82. senv = False
  83. try:
  84. senv = self.with_context(callerid=True).env[model_name]
  85. except:
  86. continue
  87. if (
  88. hasattr(senv, '_phone_name_sequence') and
  89. isinstance(senv._phone_name_sequence, int)):
  90. phoneobj.append((senv, senv._phone_name_sequence))
  91. phoneobj_sorted = sorted(phoneobj, key=lambda element: element[1])
  92. res = []
  93. for (obj, prio) in phoneobj_sorted:
  94. entry = {'object': obj, 'fields': []}
  95. for field in obj._fields:
  96. if isinstance(obj._fields[field], phone_fields.Phone):
  97. entry['fields'].append(field)
  98. res.append(entry)
  99. # [{'fields': ['fax', 'phone', 'mobile'], 'object': res.partner()},
  100. # {'fields': ['fax', 'phone', 'mobile'], 'object': crm.lead()}]
  101. return res
  102. @api.model
  103. def click2dial(self, erp_number):
  104. '''This function is designed to be overridden in IPBX-specific
  105. modules, such as asterisk_click2dial or ovh_telephony_connector'''
  106. return {'dialed_number': erp_number}
  107. @api.model
  108. def convert_to_dial_number(self, erp_number):
  109. '''
  110. This function is dedicated to the transformation of the number
  111. available in Odoo to the number that can be dialed.
  112. You may have to inherit this function in another module specific
  113. for your company if you are not happy with the way I reformat
  114. the numbers.
  115. '''
  116. assert(erp_number), 'Missing phone number'
  117. _logger.debug('Number before reformat = %s' % erp_number)
  118. # erp_number are supposed to be in International format, so no need to
  119. # give a country code here
  120. parsed_num = phonenumbers.parse(erp_number, None)
  121. country_code = self.env.user.company_id.country_id.code
  122. assert(country_code), 'Missing country on company'
  123. _logger.debug('Country code = %s' % country_code)
  124. to_dial_number = phonenumbers.format_out_of_country_calling_number(
  125. parsed_num, country_code.upper())
  126. to_dial_number = str(to_dial_number).translate(None, ' -.()/')
  127. _logger.debug('Number to be sent to phone system: %s' % to_dial_number)
  128. return to_dial_number