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.

71 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Akretion (http://www.akretion.com)
  3. # Sébastien BEAU <sebastien.beau@akretion.com>
  4. # Alexis de Lattre <alexis.delattre@akretion.com>
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from . import fields as phone_fields
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. try:
  10. import phonenumbers
  11. except ImportError:
  12. _logger.debug('Cannot `import phonenumbers`.')
  13. def convert_phone_field(value, country_code):
  14. _logger.debug(
  15. 'convert_phone_field value=%s country=%s', value, country_code)
  16. try:
  17. res_parse = phonenumbers.parse(
  18. value, country_code)
  19. _logger.debug('res_parse=%s', res_parse)
  20. new_value = phonenumbers.format_number(
  21. res_parse, phonenumbers.PhoneNumberFormat.E164)
  22. _logger.debug('new_value=%s', new_value)
  23. except:
  24. _logger.error(
  25. "Cannot reformat the phone number '%s' to "
  26. "international format with region=%s",
  27. value, country_code)
  28. new_value = value
  29. return new_value
  30. def convert_all_phone_fields(self, vals, fields_to_convert):
  31. loc_vals = vals.copy()
  32. for field in fields_to_convert:
  33. country_key = self._fields[field].country_field
  34. partner_key = self._fields[field].partner_field
  35. country = False
  36. if country_key:
  37. if country_key in loc_vals:
  38. country = self.env['res.country'].browse(vals[country_key])
  39. else:
  40. country = self[country_key]
  41. if partner_key and not country:
  42. if partner_key in loc_vals:
  43. partner = self.env['res.partner'].browse(vals[partner_key])
  44. else:
  45. partner = self[partner_key]
  46. if partner:
  47. country = partner.country_id
  48. if not country:
  49. country = self.env.user.company_id.country_id
  50. country_code = False
  51. if country:
  52. country_code = country.code.upper()
  53. if loc_vals[field]:
  54. loc_vals[field] = convert_phone_field(
  55. loc_vals[field], country_code)
  56. return loc_vals
  57. def get_phone_fields(self, vals):
  58. fields_to_convert = []
  59. for key in vals:
  60. if isinstance(self._fields.get(key), phone_fields.Fax):
  61. fields_to_convert.append(key)
  62. return fields_to_convert