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.

63 lines
3.2 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2012 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 osv import osv, fields
  22. import logging
  23. from tools.translate import _
  24. _logger = logging.getLogger(__name__)
  25. class reformat_all_phonenumbers(osv.osv_memory):
  26. _name = "reformat.all.phonenumbers"
  27. _description = "Reformat all phone numbers"
  28. _columns = {
  29. 'phonenumbers_not_reformatted': fields.text("Phone numbers that couldn't be reformatted"),
  30. }
  31. def run_reformat_all_phonenumbers(self, cr, uid, ids, context=None):
  32. addr_obj = self.pool.get('res.partner.address')
  33. phonefields = ['phone', 'fax', 'mobile']
  34. _logger.info('Starting to reformat all the phone numbers')
  35. all_addr_ids = addr_obj.search(cr, uid, ['|', ('active', '=', True), ('active', '=', False)], context=context)
  36. phonenumbers_not_reformatted = ''
  37. for addr in addr_obj.read(cr, uid, all_addr_ids, ['name'] + phonefields, context=context):
  38. init_addr = addr.copy()
  39. # addr is _updated_ by the fonction _reformat_phonenumbers()
  40. try:
  41. addr_obj._reformat_phonenumbers(cr, uid, addr, context=context)
  42. except Exception, e:
  43. #raise osv.except_osv(_('Error :'), _("Problem on partner contact '%s'. Error message: %s" % (init_addr.get('name'), e[1])))
  44. phonenumbers_not_reformatted += "Problem on partner contact '%s'. Error message: %s" % (init_addr.get('name'), e[1]) + "\n"
  45. _logger.warning("Problem on partner contact '%s'. Error message: %s" % (init_addr.get('name'), e[1]))
  46. continue
  47. # Test if the phone numbers have been changed
  48. if any([init_addr.get(field) != addr.get(field) for field in phonefields]):
  49. addr.pop('id')
  50. addr.pop('name')
  51. _logger.info('Reformating phone number: FROM %s TO %s' % (unicode(init_addr), unicode(addr)))
  52. addr_obj.write(cr, uid, init_addr['id'], addr, context=context)
  53. if not phonenumbers_not_reformatted:
  54. phonenumbers_not_reformatted = 'All phone numbers have been reformatted successfully.'
  55. self.write(cr, uid, ids[0], {'phonenumbers_not_reformatted': phonenumbers_not_reformatted}, context=context)
  56. _logger.info('End of the phone number reformatting wizard')
  57. return True