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.

311 lines
14 KiB

10 years ago
7 years ago
10 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Base Phone module for Odoo/OpenERP
  5. # Copyright (C) 2010-2014 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 openerp import models, fields, api, _
  22. from openerp.tools.safe_eval import safe_eval
  23. from openerp.exceptions import Warning
  24. import logging
  25. _logger = logging.getLogger(__name__)
  26. # Lib for phone number reformating -> pip install phonenumbers
  27. try:
  28. import phonenumbers
  29. except ImportError:
  30. _logger.debug('Cannot import phonenumbers')
  31. class PhoneCommon(models.AbstractModel):
  32. _name = 'phone.common'
  33. def _generic_reformat_phonenumbers(
  34. self, cr, uid, ids, vals, context=None):
  35. """Reformat phone numbers in E.164 format i.e. +33141981242"""
  36. assert isinstance(self._country_field, (str, unicode, type(None))),\
  37. 'Wrong self._country_field'
  38. assert isinstance(self._partner_field, (str, unicode, type(None))),\
  39. 'Wrong self._partner_field'
  40. assert isinstance(self._phone_fields, list),\
  41. 'self._phone_fields must be a list'
  42. if context is None:
  43. context = {}
  44. if ids and isinstance(ids, (int, long)):
  45. ids = [ids]
  46. if any([vals.get(field) for field in self._phone_fields]):
  47. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  48. # country_id on res.company is a fields.function that looks at
  49. # company_id.partner_id.addres(default).country_id
  50. if self._country_field in vals and isinstance(
  51. vals[self._country_field], (str, unicode)):
  52. vals[self._country_field] = int(vals[self._country_field])
  53. countrycode = None
  54. if self._country_field:
  55. if vals.get(self._country_field):
  56. country = self.pool['res.country'].browse(
  57. cr, uid, vals[self._country_field], context=context)
  58. countrycode = country.code
  59. elif ids:
  60. rec = self.browse(cr, uid, ids[0], context=context)
  61. country = safe_eval(
  62. 'rec.' + self._country_field, {'rec': rec})
  63. countrycode = country and country.code or None
  64. elif self._partner_field:
  65. if vals.get(self._partner_field):
  66. partner = self.pool['res.partner'].browse(
  67. cr, uid, vals[self._partner_field], context=context)
  68. countrycode = partner.country_id and\
  69. partner.country_id.code or None
  70. elif ids:
  71. rec = self.browse(cr, uid, ids[0], context=context)
  72. partner = safe_eval(
  73. 'rec.' + self._partner_field, {'rec': rec})
  74. if partner:
  75. countrycode = partner.country_id and\
  76. partner.country_id.code or None
  77. if not countrycode:
  78. if user.company_id.country_id:
  79. countrycode = user.company_id.country_id.code
  80. else:
  81. _logger.warning(
  82. "You should set a country on the company '%s' "
  83. "to allow the reformat of phone numbers",
  84. user.company_id.name)
  85. countrycode = None
  86. if countrycode:
  87. countrycode = countrycode.upper()
  88. # with country code = None, phonenumbers.parse() will work
  89. # with phonenumbers formatted in E164, but will fail with
  90. # phone numbers in national format
  91. for field in self._phone_fields:
  92. if vals.get(field):
  93. init_value = vals.get(field)
  94. try:
  95. res_parse = phonenumbers.parse(
  96. vals.get(field), countrycode)
  97. vals[field] = phonenumbers.format_number(
  98. res_parse, phonenumbers.PhoneNumberFormat.E164)
  99. if init_value != vals[field]:
  100. _logger.debug(
  101. "%s initial value: '%s' updated value: '%s'",
  102. field, init_value, vals[field])
  103. except Exception, e:
  104. # I do BOTH logger and raise, because:
  105. # raise is usefull when the record is created/written
  106. # by a user via the Web interface
  107. # logger is usefull when the record is created/written
  108. # via the webservices
  109. _logger.error(
  110. "Cannot reformat the phone number '%s' to "
  111. "international format with region=%s"
  112. % (vals.get(field), countrycode))
  113. if context.get('raise_if_phone_parse_fails'):
  114. raise Warning(
  115. _("Cannot reformat the phone number '%s' to "
  116. "international format. Error message: %s")
  117. % (vals.get(field), e))
  118. return vals
  119. @api.model
  120. def get_name_from_phone_number(self, presented_number):
  121. '''Function to get name from phone number. Usefull for use from IPBX
  122. to add CallerID name to incoming calls.'''
  123. res = self.get_record_from_phone_number(presented_number)
  124. if res:
  125. return res[2]
  126. else:
  127. return False
  128. @api.model
  129. def get_record_from_phone_number(self, presented_number):
  130. '''If it finds something, it returns (object name, ID, record name)
  131. For example : ('res.partner', 42, u'Alexis de Lattre (Akretion)')
  132. '''
  133. _logger.debug(
  134. u"Call get_name_from_phone_number with number = %s"
  135. % presented_number)
  136. if not isinstance(presented_number, (str, unicode)):
  137. _logger.warning(
  138. u"Number '%s' should be a 'str' or 'unicode' but it is a '%s'"
  139. % (presented_number, type(presented_number)))
  140. return False
  141. if not presented_number.isdigit():
  142. _logger.warning(
  143. u"Number '%s' should only contain digits." % presented_number)
  144. nr_digits_to_match_from_end = \
  145. self.env.user.company_id.number_of_digits_to_match_from_end
  146. if len(presented_number) >= nr_digits_to_match_from_end:
  147. end_number_to_match = presented_number[
  148. -nr_digits_to_match_from_end:len(presented_number)]
  149. else:
  150. end_number_to_match = presented_number
  151. phoneobjects = self._get_phone_fields()
  152. phonefieldslist = [] # [('res.parter', 10), ('crm.lead', 20)]
  153. for objname in phoneobjects:
  154. if (
  155. '_phone_name_sequence' in dir(self.env[objname]) and
  156. self.env[objname]._phone_name_sequence):
  157. phonefieldslist.append(
  158. (objname, self.env[objname]._phone_name_sequence))
  159. phonefieldslist_sorted = sorted(
  160. phonefieldslist,
  161. key=lambda element: element[1])
  162. _logger.debug('phonefieldslist_sorted=%s' % phonefieldslist_sorted)
  163. for (objname, prio) in phonefieldslist_sorted:
  164. obj = self.with_context(callerid=True).env[objname]
  165. pg_search_number = str('%' + end_number_to_match)
  166. _logger.debug(
  167. "Will search phone and mobile numbers in %s ending with '%s'"
  168. % (objname, end_number_to_match))
  169. domain = []
  170. for phonefield in obj._phone_fields:
  171. domain.append((phonefield, '=like', pg_search_number))
  172. if len(obj._phone_fields) > 1:
  173. domain = ['|'] * (len(obj._phone_fields) - 1) + domain
  174. res_obj = obj.search(domain)
  175. if len(res_obj) > 1:
  176. _logger.warning(
  177. u"There are several %s (IDS = %s) with a phone number "
  178. "ending with '%s'. Taking the first one."
  179. % (objname, res_obj.ids, end_number_to_match))
  180. res_obj = res_obj[0]
  181. if res_obj:
  182. name = res_obj.name_get()[0][1]
  183. res = (objname, res_obj.id, name)
  184. _logger.debug(
  185. u"Answer get_record_from_phone_number: (%s, %d, %s)"
  186. % (res[0], res[1], res[2]))
  187. return res
  188. else:
  189. _logger.debug(
  190. u"No match on %s for end of phone number '%s'"
  191. % (objname, end_number_to_match))
  192. return False
  193. @api.model
  194. def _get_phone_fields(self):
  195. '''Returns a dict with key = object name
  196. and value = list of phone fields'''
  197. models = self.env['ir.model'].search([('osv_memory', '=', False)])
  198. res = []
  199. for model in models:
  200. senv = False
  201. try:
  202. senv = self.env[model.model]
  203. except:
  204. continue
  205. if (
  206. '_phone_fields' in dir(senv) and
  207. isinstance(senv._phone_fields, list)):
  208. res.append(model.model)
  209. return res
  210. def click2dial(self, cr, uid, erp_number, context=None):
  211. '''This function is designed to be overridden in IPBX-specific
  212. modules, such as asterisk_click2dial or ovh_telephony_connector'''
  213. return {'dialed_number': erp_number}
  214. @api.model
  215. def convert_to_dial_number(self, erp_number):
  216. '''
  217. This function is dedicated to the transformation of the number
  218. available in Odoo to the number that can be dialed.
  219. You may have to inherit this function in another module specific
  220. for your company if you are not happy with the way I reformat
  221. the numbers.
  222. '''
  223. assert(erp_number), 'Missing phone number'
  224. _logger.debug('Number before reformat = %s' % erp_number)
  225. # erp_number are supposed to be in E.164 format, so no need to
  226. # give a country code here
  227. parsed_num = phonenumbers.parse(erp_number, None)
  228. country_code = self.env.user.company_id.country_id.code
  229. assert(country_code), 'Missing country on company'
  230. _logger.debug('Country code = %s' % country_code)
  231. to_dial_number = phonenumbers.format_out_of_country_calling_number(
  232. parsed_num, country_code.upper())
  233. to_dial_number = str(to_dial_number).translate(None, ' -.()/')
  234. _logger.debug('Number to be sent to phone system: %s' % to_dial_number)
  235. return to_dial_number
  236. class ResPartner(models.Model):
  237. _name = 'res.partner'
  238. _inherit = ['res.partner', 'phone.common']
  239. _phone_fields = ['phone', 'mobile', 'fax']
  240. _phone_name_sequence = 10
  241. _country_field = 'country_id'
  242. _partner_field = None
  243. def create(self, cr, uid, vals, context=None):
  244. vals_reformated = self._generic_reformat_phonenumbers(
  245. cr, uid, None, vals, context=context)
  246. return super(ResPartner, self).create(
  247. cr, uid, vals_reformated, context=context)
  248. def write(self, cr, uid, ids, vals, context=None):
  249. vals_reformated = self._generic_reformat_phonenumbers(
  250. cr, uid, ids, vals, context=context)
  251. return super(ResPartner, self).write(
  252. cr, uid, ids, vals_reformated, context=context)
  253. def name_get(self, cr, uid, ids, context=None):
  254. if context is None:
  255. context = {}
  256. if context.get('callerid'):
  257. res = []
  258. if isinstance(ids, (int, long)):
  259. ids = [ids]
  260. for partner in self.browse(cr, uid, ids, context=context):
  261. if partner.parent_id and partner.parent_id.is_company:
  262. name = u'%s (%s)' % (partner.name, partner.parent_id.name)
  263. else:
  264. name = partner.name
  265. res.append((partner.id, name))
  266. return res
  267. else:
  268. return super(ResPartner, self).name_get(
  269. cr, uid, ids, context=context)
  270. class ResCompany(models.Model):
  271. _inherit = 'res.company'
  272. number_of_digits_to_match_from_end = fields.Integer(
  273. string='Number of Digits To Match From End',
  274. default=8,
  275. help="In several situations, OpenERP will have to find a "
  276. "Partner/Lead/Employee/... from a phone number presented by the "
  277. "calling party. As the phone numbers presented by your phone "
  278. "operator may not always be displayed in a standard format, "
  279. "the best method to find the related Partner/Lead/Employee/... "
  280. "in OpenERP is to try to match the end of the phone number in "
  281. "OpenERP with the N last digits of the phone number presented "
  282. "by the calling party. N is the value you should enter in this "
  283. "field.")
  284. _sql_constraints = [(
  285. 'number_of_digits_to_match_from_end_positive',
  286. 'CHECK (number_of_digits_to_match_from_end > 0)',
  287. "The value of the field 'Number of Digits To Match From End' must "
  288. "be positive."),
  289. ]