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.

105 lines
4.2 KiB

10 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2010-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import models, fields, api, _
  5. from openerp.exceptions import UserError
  6. import logging
  7. import phonenumbers
  8. _logger = logging.getLogger(__name__)
  9. class NumberNotFound(models.TransientModel):
  10. _name = "number.not.found"
  11. _description = "Number not found"
  12. calling_number = fields.Char(string='Calling Number', size=64,
  13. readonly=True,
  14. help="Phone number of calling party that has "
  15. "been obtained from the telephony server, in "
  16. "the format used by the telephony server "
  17. "(not E.164).")
  18. e164_number = fields.Char(string='E.164 Number', size=64,
  19. help="E.164 equivalent of the calling number.")
  20. number_type = fields.Selection(selection=[
  21. ('phone', 'Fixed'),
  22. ('mobile', 'Mobile')
  23. ], string='Fixed/Mobile', required=True)
  24. to_update_partner_id = fields.Many2one(comodel_name='res.partner',
  25. string='Partner to Update',
  26. help="Partner on which the phone "
  27. "number will be written")
  28. current_partner_phone = fields.Char(related='to_update_partner_id.phone',
  29. string='Current Phone', readonly=True)
  30. current_partner_mobile = fields.Char(related='to_update_partner_id.mobile',
  31. string='Current Mobile',
  32. readonly=True)
  33. @api.model
  34. def default_get(self, fields_list):
  35. res = super(NumberNotFound, self).default_get(fields_list)
  36. if not res:
  37. res = {}
  38. if res.get('calling_number'):
  39. if not self.env.user.company_id.country_id:
  40. raise UserError(_(
  41. 'Missing country on company %s'
  42. % self.env.user.company_id.name))
  43. country_code = self.env.user.company_id.country_id.code
  44. try:
  45. parsed_num = phonenumbers.parse(
  46. res['calling_number'], country_code)
  47. res['e164_number'] = phonenumbers.format_number(
  48. parsed_num, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  49. number_type = phonenumbers.number_type(parsed_num)
  50. if number_type == 1:
  51. res['number_type'] = 'mobile'
  52. else:
  53. res['number_type'] = 'phone'
  54. except Exception, e:
  55. _logger.error(
  56. "Cannot reformat the phone number '%s': %s",
  57. res['calling_number'], e)
  58. pass
  59. return res
  60. @api.multi
  61. def create_partner(self):
  62. '''Function called by the related button of the wizard'''
  63. wiz = self[0]
  64. parsed_num = phonenumbers.parse(wiz.e164_number, None)
  65. phonenumbers.number_type(parsed_num)
  66. context = dict(self._context or {})
  67. context.update({'default_%s' % wiz.number_type: wiz.e164_number})
  68. action = {
  69. 'name': _('Create New Partner'),
  70. 'view_mode': 'form,tree,kanban',
  71. 'res_model': 'res.partner',
  72. 'type': 'ir.actions.act_window',
  73. 'nodestroy': False,
  74. 'target': 'current',
  75. 'context': context,
  76. }
  77. return action
  78. @api.multi
  79. def update_partner(self):
  80. self.ensure_one()
  81. wiz = self[0]
  82. if not wiz.to_update_partner_id:
  83. raise UserError(_('Select the Partner to Update.'))
  84. wiz.to_update_partner_id.write(
  85. {wiz.number_type: wiz.e164_number})
  86. action = {
  87. 'name': _('Partner: %s' % wiz.to_update_partner_id.name),
  88. 'type': 'ir.actions.act_window',
  89. 'res_model': 'res.partner',
  90. 'view_mode': 'form,tree,kanban',
  91. 'nodestroy': False,
  92. 'target': 'current',
  93. 'res_id': wiz.to_update_partner_id.id,
  94. 'context': self._context,
  95. }
  96. return action