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.3 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2012-2013 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 osv, fields
  22. import logging
  23. from openerp.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. partner_obj = self.pool['res.partner']
  33. phonefields = ['phone', 'fax', 'mobile']
  34. _logger.info('Starting to reformat all the phone numbers')
  35. all_partner_ids = partner_obj.search(cr, uid, ['|', ('active', '=', True), ('active', '=', False)], context=context)
  36. phonenumbers_not_reformatted = ''
  37. for partner in partner_obj.read(cr, uid, all_partner_ids, ['name'] + phonefields, context=context):
  38. init_partner = partner.copy()
  39. # partner is _updated_ by the fonction _reformat_phonenumbers()
  40. try:
  41. partner_obj._reformat_phonenumbers(cr, uid, partner, context=context)
  42. except Exception, e:
  43. #raise osv.except_osv(_('Error :'), _("Problem on partner '%s'. Error message: %s" % (init_partner.get('name'), e[1])))
  44. phonenumbers_not_reformatted += "Problem on partner '%s'. Error message: %s" % (init_partner.get('name'), e[1]) + "\n"
  45. _logger.warning("Problem on partner '%s'. Error message: %s" % (init_partner.get('name'), e[1]))
  46. continue
  47. # Test if the phone numbers have been changed
  48. if any([init_partner.get(field) != partner.get(field) for field in phonefields]):
  49. partner.pop('id')
  50. partner.pop('name')
  51. _logger.info('Reformating phone number: FROM %s TO %s' % (unicode(init_partner), unicode(partner)))
  52. partner_obj.write(cr, uid, init_partner['id'], partner, 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