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.

130 lines
5.3 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2010-2013 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. import phonenumbers
  25. _logger = logging.getLogger(__name__)
  26. class number_not_found(orm.TransientModel):
  27. _name = "number.not.found"
  28. _description = "Number not found"
  29. _columns = {
  30. 'calling_number': fields.char(
  31. 'Calling Number', size=64, readonly=True,
  32. help="Phone number of calling party that has been obtained "
  33. "from Asterisk, in the format used by Asterisk (not E.164)."),
  34. 'e164_number': fields.char(
  35. 'E.164 Number', size=64,
  36. help="E.164 equivalent of the calling number."),
  37. 'number_type': fields.selection(
  38. [('phone', 'Fixed'), ('mobile', 'Mobile')],
  39. 'Fixed/Mobile', required=True),
  40. 'to_update_partner_id': fields.many2one(
  41. 'res.partner', 'Partner to Update',
  42. help="Partner on which the phone number will be written"),
  43. 'current_partner_phone': fields.related(
  44. 'to_update_partner_id', 'phone', type='char',
  45. relation='res.partner', string='Current Phone', readonly=True),
  46. 'current_partner_mobile': fields.related(
  47. 'to_update_partner_id', 'mobile', type='char',
  48. relation='res.partner', string='Current Mobile', readonly=True),
  49. }
  50. def default_get(self, cr, uid, fields_list, context=None):
  51. res = super(number_not_found, self).default_get(
  52. cr, uid, fields_list, context=context)
  53. if not res:
  54. res = {}
  55. if res.get('calling_number'):
  56. convert = self.pool['phone.common']._generic_reformat_phonenumbers(
  57. cr, uid, {'phone': res.get('calling_number')}, context=context)
  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. def create_partner(self, cr, uid, ids, context=None):
  68. '''Function called by the related button of the wizard'''
  69. if context is None:
  70. context = {}
  71. wiz = self.browse(cr, uid, ids[0], context=context)
  72. parsed_num = phonenumbers.parse(wiz.e164_number, None)
  73. number_type = phonenumbers.number_type(parsed_num)
  74. context['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. def update_partner(self, cr, uid, ids, context=None):
  86. wiz = self.browse(cr, uid, ids[0], context=context)
  87. if not wiz.to_update_partner_id:
  88. raise orm.except_orm(
  89. _('Error:'),
  90. _("Select the Partner to Update."))
  91. self.pool['res.partner'].write(
  92. cr, uid, wiz.to_update_partner_id.id,
  93. {wiz.number_type: wiz.e164_number}, context=context)
  94. action = {
  95. 'name': _('Partner: %s' % wiz.to_update_partner_id.name),
  96. 'type': 'ir.actions.act_window',
  97. 'res_model': 'res.partner',
  98. 'view_mode': 'form,tree,kanban',
  99. 'nodestroy': False,
  100. 'target': 'current',
  101. 'res_id': wiz.to_update_partner_id.id,
  102. 'context': context,
  103. }
  104. return action
  105. def onchange_to_update_partner(
  106. self, cr, uid, ids, to_update_partner_id, context=None):
  107. res = {'value': {}}
  108. if to_update_partner_id:
  109. to_update_partner = self.pool['res.partner'].browse(
  110. cr, uid, to_update_partner_id, context=context)
  111. res['value'].update({
  112. 'current_partner_phone': to_update_partner.phone,
  113. 'current_partner_mobile': to_update_partner.mobile,
  114. })
  115. else:
  116. res['value'].update({
  117. 'current_partner_phone': False,
  118. 'current_partner_mobile': False,
  119. })
  120. return res