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.

75 lines
2.5 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 Exception:
  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. # Warning: when we edit or create a partner from the
  39. # POS frontend vals[country_key] is a string !
  40. country = self.env['res.country'].browse(
  41. int(vals[country_key]))
  42. else:
  43. country = self[country_key]
  44. if partner_key and not country:
  45. if partner_key in loc_vals:
  46. partner = self.env['res.partner'].browse(
  47. int(vals[partner_key]))
  48. else:
  49. partner = self[partner_key]
  50. if partner:
  51. country = partner.country_id
  52. if not country:
  53. country = self.env.user.company_id.country_id
  54. country_code = False
  55. if country:
  56. country_code = country.code.upper()
  57. if loc_vals[field]:
  58. loc_vals[field] = convert_phone_field(
  59. loc_vals[field], country_code)
  60. return loc_vals
  61. def get_phone_fields(self, vals):
  62. fields_to_convert = []
  63. for key in vals:
  64. if isinstance(self._fields.get(key), phone_fields.Fax):
  65. fields_to_convert.append(key)
  66. return fields_to_convert