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.

99 lines
3.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 osv import osv, fields
  22. import netsvc
  23. logger = netsvc.Logger()
  24. class reformat_all_phonenumbers(osv.osv_memory):
  25. _name = "reformat.all.phonenumbers"
  26. _description = "Reformat all phone numbers"
  27. _columns = {
  28. 'phonenumbers_not_reformatted': fields.text(
  29. "Phone numbers that couldn't be reformatted"
  30. ),
  31. }
  32. def run_reformat_all_phonenumbers(self, cr, uid, ids, context=None):
  33. print "RUN ids=", ids
  34. addr_obj = self.pool.get('res.partner.address')
  35. phonefields = ['phone', 'fax', 'mobile']
  36. logger.notifyChannel(
  37. 'click2dial',
  38. netsvc.LOG_INFO,
  39. 'Starting to reformat all the phone numbers'
  40. )
  41. all_addr_ids = addr_obj.search(
  42. cr, uid, [
  43. '|',
  44. ('active', '=', True),
  45. ('active', '=', False)
  46. ], context=context
  47. )
  48. phonenumbers_not_reformatted = ''
  49. for addr in addr_obj.read(
  50. cr, uid, all_addr_ids, ['name'] + phonefields, context=context
  51. ):
  52. init_addr = addr.copy()
  53. # addr is _updated_ by the fonction _reformat_phonenumbers()
  54. try:
  55. addr_obj._reformat_phonenumbers(cr, uid, addr, context=context)
  56. except Exception, e:
  57. phonenumbers_not_reformatted += (
  58. "Problem on partner contact '%s'. "
  59. "Error message: %s" % (init_addr.get('name'), e[1]) + "\n"
  60. )
  61. logger.notifyChannel(
  62. 'click2dial',
  63. netsvc.LOG_WARNING,
  64. "Problem on partner contact '%s'. Error message: %s"
  65. % (init_addr.get('name'), e[1])
  66. )
  67. continue
  68. # Test if the phone numbers have been changed
  69. if any([init_addr.get(field) != addr.get(field)
  70. for field in phonefields]):
  71. addr.pop('id')
  72. addr.pop('name')
  73. logger.notifyChannel(
  74. 'click2dial',
  75. netsvc.LOG_INFO,
  76. 'Reformating phone number: FROM %s TO %s'
  77. % (unicode(init_addr), unicode(addr))
  78. )
  79. addr_obj.write(cr, uid, init_addr['id'], addr, context=context)
  80. if not phonenumbers_not_reformatted:
  81. phonenumbers_not_reformatted = (
  82. 'All phone numbers have been reformatted successfully.'
  83. )
  84. self.write(cr, uid, ids, {
  85. 'phonenumbers_not_reformatted': phonenumbers_not_reformatted
  86. }, context=context)
  87. logger.notifyChannel(
  88. 'click2dial',
  89. netsvc.LOG_INFO,
  90. 'End of the phone number reformatting wizard'
  91. )
  92. return True
  93. reformat_all_phonenumbers()