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.

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