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.

117 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. _name = 'res.partner'
  39. _inherit = ['res.partner', 'asterisk.common']
  40. def action_dial(self, cr, uid, ids, context=None):
  41. '''
  42. This method open the phone call history when the phone click2dial
  43. button of asterisk_click2dial module is pressed
  44. :return the phone call history view of the partner
  45. '''
  46. if context is None:
  47. context = {}
  48. super(res_partner, self).action_dial(cr, uid, ids, context=context)
  49. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  50. context['partner_id'] = ids[0]
  51. action_start_wizard = {
  52. 'name': 'Create phone call in CRM',
  53. 'type': 'ir.actions.act_window',
  54. 'res_model': 'wizard.create.crm.phonecall',
  55. 'view_type': 'form',
  56. 'view_mode': 'form',
  57. 'nodestroy': True,
  58. 'target': 'new',
  59. 'context': context,
  60. }
  61. if user.context_propose_creation_crm_call:
  62. return action_start_wizard
  63. else:
  64. return True
  65. class res_users(osv.osv):
  66. _inherit = "res.users"
  67. _columns = {
  68. # Field name starts with 'context_' to allow modification by the user
  69. # in his preferences, cf server-61/openerp/addons/base/res/res_users.py
  70. # line 377 in "def write" of "class users"
  71. 'context_propose_creation_crm_call': fields.boolean('Propose to create a call in CRM after a click2dial'),
  72. }
  73. _defaults = {
  74. 'context_propose_creation_crm_call': True,
  75. }
  76. class crm_lead(osv.osv):
  77. _name = 'crm.lead'
  78. _inherit = ['crm.lead', 'asterisk.common']
  79. def format_phonenumber_to_e164(self, cr, uid, ids, name, arg, context=None):
  80. return self.generic_phonenumber_to_e164(cr, uid, ids, [('phone', 'phone_e164'), ('mobile', 'mobile_e164'), ('fax', 'fax_e164')], context=context)
  81. _columns = {
  82. 'phone_e164': fields.function(format_phonenumber_to_e164, type='char', size=64, string='Phone in E.164 format', readonly=True, multi="e164lead", store={
  83. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['phone'], 10),
  84. }),
  85. 'mobile_e164': fields.function(format_phonenumber_to_e164, type='char', size=64, string='Mobile in E.164 format', readonly=True, multi="e164lead", store={
  86. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['mobile'], 10),
  87. }),
  88. 'fax_e164': fields.function(format_phonenumber_to_e164, type='char', size=64, string='Fax in E.164 format', readonly=True, multi="e164lead", store={
  89. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['fax'], 10),
  90. }),
  91. }
  92. def create(self, cr, uid, vals, context=None):
  93. vals_reformated = self.pool['res.partner']._generic_reformat_phonenumbers(cr, uid, vals, context=context)
  94. return super(crm_lead, self).create(cr, uid, vals_reformated, context=context)
  95. def write(self, cr, uid, ids, vals, context=None):
  96. vals_reformated = self.pool['res.partner']._generic_reformat_phonenumbers(cr, uid, vals, context=context)
  97. return super(crm_lead, self).write(cr, uid, ids, vals_reformated, context=context)