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.

119 lines
4.9 KiB

10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Base Phone module for Odoo
  5. # Copyright (C) 2010-2015 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 import models, fields, api, exceptions
  22. from openerp.tools.translate import _
  23. import logging
  24. import phonenumbers
  25. _logger = logging.getLogger(__name__)
  26. class NumberNotFound(models.TransientModel):
  27. _name = "number.not.found"
  28. _description = "Number not found"
  29. calling_number = fields.Char(string='Calling Number', size=64,
  30. readonly=True,
  31. help="Phone number of calling party that has "
  32. "been obtained from the telephony server, in "
  33. "the format used by the telephony server "
  34. "(not E.164).")
  35. e164_number = fields.Char(string='E.164 Number', size=64,
  36. help="E.164 equivalent of the calling number.")
  37. number_type = fields.Selection(selection=[
  38. ('phone', 'Fixed'),
  39. ('mobile', 'Mobile')
  40. ], string='Fixed/Mobile', required=True)
  41. to_update_partner_id = fields.Many2one(comodel_name='res.partner',
  42. string='Partner to Update',
  43. help="Partner on which the phone "
  44. "number will be written")
  45. current_partner_phone = fields.Char(related='to_update_partner_id.phone',
  46. string='Current Phone', readonly=True)
  47. current_partner_mobile = fields.Char(related='to_update_partner_id.mobile',
  48. string='Current Mobile',
  49. readonly=True)
  50. @api.model
  51. def default_get(self, fields_list):
  52. res = super(NumberNotFound, self).default_get(fields_list)
  53. if not res:
  54. res = {}
  55. if res.get('calling_number'):
  56. convert = self.env['res.partner']._generic_reformat_phonenumbers(
  57. None, {'phone': res.get('calling_number')})
  58. parsed_num = phonenumbers.parse(convert.get('phone'))
  59. res['e164_number'] = phonenumbers.format_number(
  60. parsed_num, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  61. number_type = phonenumbers.number_type(parsed_num)
  62. if number_type == 1:
  63. res['number_type'] = 'mobile'
  64. else:
  65. res['number_type'] = 'phone'
  66. return res
  67. @api.multi
  68. def create_partner(self):
  69. '''Function called by the related button of the wizard'''
  70. wiz = self[0]
  71. parsed_num = phonenumbers.parse(wiz.e164_number, None)
  72. phonenumbers.number_type(parsed_num)
  73. context = dict(self._context or {})
  74. context.update({'default_%s' % wiz.number_type: wiz.e164_number})
  75. action = {
  76. 'name': _('Create New Partner'),
  77. 'view_mode': 'form,tree,kanban',
  78. 'res_model': 'res.partner',
  79. 'type': 'ir.actions.act_window',
  80. 'nodestroy': False,
  81. 'target': 'current',
  82. 'context': context,
  83. }
  84. return action
  85. @api.multi
  86. def update_partner(self):
  87. wiz = self[0]
  88. if not wiz.to_update_partner_id:
  89. raise exceptions.Warning(
  90. _('Error'),
  91. _('Select the Partner to Update.'))
  92. self.env['res.partner'].write(
  93. wiz.to_update_partner_id.id,
  94. {wiz.number_type: wiz.e164_number})
  95. action = {
  96. 'name': _('Partner: %s' % wiz.to_update_partner_id.name),
  97. 'type': 'ir.actions.act_window',
  98. 'res_model': 'res.partner',
  99. 'view_mode': 'form,tree,kanban',
  100. 'nodestroy': False,
  101. 'target': 'current',
  102. 'res_id': wiz.to_update_partner_id.id,
  103. 'context': self._context,
  104. }
  105. return action
  106. @api.onchange('to_update_partner_id')
  107. def onchange_to_update_partner(self):
  108. self.current_partner_phone = self.to_update_partner.phone or False
  109. self.current_partner_mobile = self.to_update_partner.mobile or False