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
3.9 KiB

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