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.

109 lines
4.0 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 odoo import models, fields, api, _
  5. from .. import fields as phone_fields
  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 = phone_fields.Phone(
  32. related='to_update_partner_id.phone', string='Current Phone',
  33. readonly=True)
  34. current_partner_mobile = phone_fields.Phone(
  35. related='to_update_partner_id.mobile', string='Current Mobile',
  36. readonly=True)
  37. @api.model
  38. def default_get(self, fields_list):
  39. res = super(NumberNotFound, self).default_get(fields_list)
  40. if not res:
  41. res = {}
  42. if res.get('calling_number'):
  43. if not self.env.user.company_id.country_id:
  44. raise UserError(_(
  45. 'Missing country on company %s'
  46. % self.env.user.company_id.name))
  47. country_code = self.env.user.company_id.country_id.code
  48. try:
  49. parsed_num = phonenumbers.parse(
  50. res['calling_number'], country_code)
  51. res['e164_number'] = phonenumbers.format_number(
  52. parsed_num, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  53. number_type = phonenumbers.number_type(parsed_num)
  54. if number_type == 1:
  55. res['number_type'] = 'mobile'
  56. else:
  57. res['number_type'] = 'phone'
  58. except Exception, e:
  59. _logger.error(
  60. "Cannot reformat the phone number '%s': %s",
  61. res['calling_number'], e)
  62. pass
  63. return res
  64. @api.multi
  65. def create_partner(self):
  66. '''Function called by the related button of the wizard'''
  67. wiz = self[0]
  68. parsed_num = phonenumbers.parse(wiz.e164_number, None)
  69. phonenumbers.number_type(parsed_num)
  70. context = dict(self._context or {})
  71. context.update({'default_%s' % wiz.number_type: wiz.e164_number})
  72. action = {
  73. 'name': _('Create New Partner'),
  74. 'view_mode': 'form,tree,kanban',
  75. 'res_model': 'res.partner',
  76. 'type': 'ir.actions.act_window',
  77. 'nodestroy': False,
  78. 'target': 'current',
  79. 'context': context,
  80. }
  81. return action
  82. @api.multi
  83. def update_partner(self):
  84. self.ensure_one()
  85. wiz = self[0]
  86. if not wiz.to_update_partner_id:
  87. raise UserError(_('Select the Partner to Update.'))
  88. wiz.to_update_partner_id.write(
  89. {wiz.number_type: wiz.e164_number})
  90. action = {
  91. 'name': _('Partner: %s' % wiz.to_update_partner_id.name),
  92. 'type': 'ir.actions.act_window',
  93. 'res_model': 'res.partner',
  94. 'view_mode': 'form,tree,kanban',
  95. 'nodestroy': False,
  96. 'target': 'current',
  97. 'res_id': wiz.to_update_partner_id.id,
  98. 'context': self._context,
  99. }
  100. return action