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.

137 lines
5.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- encoding: 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.osv import orm, fields
  22. from openerp.tools.translate import _
  23. import logging
  24. _logger = logging.getLogger(__name__)
  25. try:
  26. import phonenumbers
  27. except ImportError:
  28. _logger.debug('Cannot import phonenumbers')
  29. class number_not_found(orm.TransientModel):
  30. _name = "number.not.found"
  31. _description = "Number not found"
  32. _columns = {
  33. 'calling_number': fields.char(
  34. 'Calling Number', size=64, readonly=True,
  35. help="Phone number of calling party that has been obtained "
  36. "from the telephony server, in the format used by the "
  37. "telephony server (not E.164)."),
  38. 'e164_number': fields.char(
  39. 'E.164 Number', size=64,
  40. help="E.164 equivalent of the calling number."),
  41. 'number_type': fields.selection(
  42. [('phone', 'Fixed'), ('mobile', 'Mobile')],
  43. 'Fixed/Mobile', required=True),
  44. 'to_update_partner_id': fields.many2one(
  45. 'res.partner', 'Partner to Update',
  46. help="Partner on which the phone number will be written"),
  47. 'current_partner_phone': fields.related(
  48. 'to_update_partner_id', 'phone', type='char',
  49. relation='res.partner', string='Current Phone', readonly=True),
  50. 'current_partner_mobile': fields.related(
  51. 'to_update_partner_id', 'mobile', type='char',
  52. relation='res.partner', string='Current Mobile', readonly=True),
  53. }
  54. def default_get(self, cr, uid, fields_list, context=None):
  55. res = super(number_not_found, self).default_get(
  56. cr, uid, fields_list, context=context
  57. )
  58. if not res:
  59. res = {}
  60. if res.get('calling_number'):
  61. convert = self.pool['res.partner']._generic_reformat_phonenumbers(
  62. cr, uid, None, {'phone': res.get('calling_number')},
  63. context=context)
  64. parsed_num = phonenumbers.parse(convert.get('phone'))
  65. res['e164_number'] = phonenumbers.format_number(
  66. parsed_num, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  67. number_type = phonenumbers.number_type(parsed_num)
  68. if number_type == 1:
  69. res['number_type'] = 'mobile'
  70. else:
  71. res['number_type'] = 'phone'
  72. return res
  73. def create_partner(self, cr, uid, ids, context=None):
  74. '''Function called by the related button of the wizard'''
  75. if context is None:
  76. context = {}
  77. wiz = self.browse(cr, uid, ids[0], context=context)
  78. parsed_num = phonenumbers.parse(wiz.e164_number, None)
  79. phonenumbers.number_type(parsed_num)
  80. context['default_%s' % wiz.number_type] = wiz.e164_number
  81. action = {
  82. 'name': _('Create New Partner'),
  83. 'view_mode': 'form,tree,kanban',
  84. 'res_model': 'res.partner',
  85. 'type': 'ir.actions.act_window',
  86. 'nodestroy': False,
  87. 'target': 'current',
  88. 'context': context,
  89. }
  90. return action
  91. def update_partner(self, cr, uid, ids, context=None):
  92. wiz = self.browse(cr, uid, ids[0], context=context)
  93. if not wiz.to_update_partner_id:
  94. raise orm.except_orm(
  95. _('Error:'),
  96. _("Select the Partner to Update."))
  97. self.pool['res.partner'].write(
  98. cr, uid, wiz.to_update_partner_id.id,
  99. {wiz.number_type: wiz.e164_number}, context=context)
  100. action = {
  101. 'name': _('Partner: %s' % wiz.to_update_partner_id.name),
  102. 'type': 'ir.actions.act_window',
  103. 'res_model': 'res.partner',
  104. 'view_mode': 'form,tree,kanban',
  105. 'nodestroy': False,
  106. 'target': 'current',
  107. 'res_id': wiz.to_update_partner_id.id,
  108. 'context': context,
  109. }
  110. return action
  111. def onchange_to_update_partner(
  112. self, cr, uid, ids, to_update_partner_id, context=None):
  113. res = {'value': {}}
  114. if to_update_partner_id:
  115. to_update_partner = self.pool['res.partner'].browse(
  116. cr, uid, to_update_partner_id, context=context)
  117. res['value'].update({
  118. 'current_partner_phone': to_update_partner.phone,
  119. 'current_partner_mobile': to_update_partner.mobile,
  120. })
  121. else:
  122. res['value'].update({
  123. 'current_partner_phone': False,
  124. 'current_partner_mobile': False,
  125. })
  126. return res