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.

120 lines
5.4 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Base Phone module for 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.osv import orm
  22. from openerp.tools.translate import _
  23. import logging
  24. # Lib for phone number reformating -> pip install phonenumbers
  25. import phonenumbers
  26. _logger = logging.getLogger(__name__)
  27. class phone_common(orm.AbstractModel):
  28. _name = 'phone.common'
  29. def generic_phonenumber_to_e164(
  30. self, cr, uid, ids, field_from_to_seq, context=None):
  31. result = {}
  32. from_field_seq = [item[0] for item in field_from_to_seq]
  33. for record in self.read(cr, uid, ids, from_field_seq, context=context):
  34. result[record['id']] = {}
  35. for fromfield, tofield in field_from_to_seq:
  36. if not record.get(fromfield):
  37. res = False
  38. else:
  39. try:
  40. res = phonenumbers.format_number(
  41. phonenumbers.parse(record.get(fromfield), None),
  42. phonenumbers.PhoneNumberFormat.E164)
  43. except Exception, e:
  44. _logger.error(
  45. "Cannot reformat the phone number '%s' to E.164 "
  46. "format. Error message: %s"
  47. % (record.get(fromfield), e))
  48. _logger.error(
  49. "You should fix this number and run the wizard "
  50. "'Reformat all phone numbers' from the menu "
  51. "Settings > Configuration > Phones")
  52. # If I raise an exception here, it won't be possible to
  53. # install the module on a DB with bad phone numbers
  54. res = False
  55. result[record['id']][tofield] = res
  56. return result
  57. def _generic_reformat_phonenumbers(
  58. self, cr, uid, vals,
  59. phonefields=['phone', 'partner_phone', 'fax', 'mobile'],
  60. context=None):
  61. """Reformat phone numbers in E.164 format i.e. +33141981242"""
  62. if any([vals.get(field) for field in phonefields]):
  63. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  64. # country_id on res.company is a fields.function that looks at
  65. # company_id.partner_id.addres(default).country_id
  66. if user.company_id.country_id:
  67. user_countrycode = user.company_id.country_id.code
  68. else:
  69. # We need to raise an exception here because, if we pass None
  70. # as second arg of phonenumbers.parse(), it will raise an
  71. # exception when you try to enter a phone number in
  72. # national format... so it's better to raise the exception here
  73. raise orm.except_orm(
  74. _('Error :'),
  75. _("You should set a country on the company '%s'")
  76. % user.company_id.name)
  77. #print "user_countrycode=", user_countrycode
  78. for field in phonefields:
  79. if vals.get(field):
  80. init_value = vals.get(field)
  81. try:
  82. res_parse = phonenumbers.parse(
  83. vals.get(field), user_countrycode)
  84. except Exception, e:
  85. raise orm.except_orm(
  86. _('Error :'),
  87. _("Cannot reformat the phone number '%s' to "
  88. "international format. Error message: %s")
  89. % (vals.get(field), e))
  90. #print "res_parse=", res_parse
  91. vals[field] = phonenumbers.format_number(
  92. res_parse, phonenumbers.PhoneNumberFormat.E164)
  93. if init_value != vals[field]:
  94. _logger.info(
  95. "%s initial value: '%s' updated value: '%s'"
  96. % (field, init_value, vals[field]))
  97. return vals
  98. class res_partner(orm.Model):
  99. _name = 'res.partner'
  100. _inherit = ['res.partner', 'phone.common']
  101. def create(self, cr, uid, vals, context=None):
  102. vals_reformated = self._generic_reformat_phonenumbers(
  103. cr, uid, vals, context=context)
  104. return super(res_partner, self).create(
  105. cr, uid, vals_reformated, context=context)
  106. def write(self, cr, uid, ids, vals, context=None):
  107. vals_reformated = self._generic_reformat_phonenumbers(
  108. cr, uid, vals, context=context)
  109. return super(res_partner, self).write(
  110. cr, uid, ids, vals_reformated, context=context)