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.

116 lines
4.9 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk click2dial CRM module for OpenERP
  5. # Copyright (c) 2011 Zikzakmedia S.L. (http://zikzakmedia.com)
  6. # Copyright (c) 2012-2013 Akretion (http://www.akretion.com)
  7. # Copyright (C) 2013 Invitu <contact@invitu.com>
  8. # @author: Jesús Martín <jmartin@zikzakmedia.com>
  9. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  10. #
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Affero General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Affero General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Affero General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ##############################################################################
  25. from openerp.osv import osv, fields
  26. # Lib required to print logs
  27. import logging
  28. # Lib to translate error messages
  29. from openerp.tools.translate import _
  30. # Lib for phone number reformating -> pip install phonenumbers
  31. import phonenumbers
  32. # Lib py-asterisk from http://code.google.com/p/py-asterisk/
  33. # We need a version which has this commit : http://code.google.com/p/py-asterisk/source/detail?r=8d0e1c941cce727c702582f3c9fcd49beb4eeaa4
  34. # so a version after Nov 20th, 2012
  35. from Asterisk import Manager
  36. _logger = logging.getLogger(__name__)
  37. class res_partner(osv.osv):
  38. _inherit = "res.partner"
  39. def dial(self, cr, uid, ids, phone_field='phone', context=None):
  40. '''
  41. This method open the phone call history when the phone click2dial
  42. button of asterisk_click2dial module is pressed
  43. :return the phone call history view of the partner
  44. '''
  45. if context is None:
  46. context = {}
  47. super(res_partner, self).dial(cr, uid, ids, phone_field=phone_field, context=context)
  48. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  49. context['partner_id'] = ids[0]
  50. action_start_wizard = {
  51. 'name': 'Create phone call in CRM',
  52. 'type': 'ir.actions.act_window',
  53. 'res_model': 'wizard.create.crm.phonecall',
  54. 'view_type': 'form',
  55. 'view_mode': 'form',
  56. 'nodestroy': True,
  57. 'target': 'new',
  58. 'context': context,
  59. }
  60. if user.context_propose_creation_crm_call:
  61. return action_start_wizard
  62. else:
  63. return True
  64. class res_users(osv.osv):
  65. _inherit = "res.users"
  66. _columns = {
  67. # Field name starts with 'context_' to allow modification by the user
  68. # in his preferences, cf server-61/openerp/addons/base/res/res_users.py
  69. # line 377 in "def write" of "class users"
  70. 'context_propose_creation_crm_call': fields.boolean('Propose to create a call in CRM after a click2dial'),
  71. }
  72. _defaults = {
  73. 'context_propose_creation_crm_call': True,
  74. }
  75. class crm_lead(osv.osv):
  76. _name = 'crm.lead'
  77. _inherit = ['crm.lead', 'asterisk.common']
  78. def format_phonenumber_to_e164(self, cr, uid, ids, name, arg, context=None):
  79. return self.generic_phonenumber_to_e164(cr, uid, ids, [('phone', 'phone_e164'), ('mobile', 'mobile_e164'), ('fax', 'fax_e164')], context=context)
  80. _columns = {
  81. 'phone_e164': fields.function(format_phonenumber_to_e164, type='char', size=64, string='Phone in E.164 format', readonly=True, multi="e164lead", store={
  82. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['phone'], 10),
  83. }),
  84. 'mobile_e164': fields.function(format_phonenumber_to_e164, type='char', size=64, string='Mobile in E.164 format', readonly=True, multi="e164lead", store={
  85. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['mobile'], 10),
  86. }),
  87. 'fax_e164': fields.function(format_phonenumber_to_e164, type='char', size=64, string='Fax in E.164 format', readonly=True, multi="e164lead", store={
  88. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['fax'], 10),
  89. }),
  90. }
  91. def create(self, cr, uid, vals, context=None):
  92. vals_reformated = self.pool['res.partner']._generic_reformat_phonenumbers(cr, uid, vals, context=context)
  93. return super(crm_lead, self).create(cr, uid, vals_reformated, context=context)
  94. def write(self, cr, uid, ids, vals, context=None):
  95. vals_reformated = self.pool['res.partner']._generic_reformat_phonenumbers(cr, uid, vals, context=context)
  96. return super(crm_lead, self).write(cr, uid, ids, vals_reformated, context=context)