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.

182 lines
8.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) All Rights Reserved.
  6. # Copyright (c) 2012-2013 Akretion (http://www.akretion.com)
  7. # @author: Jesús Martín <jmartin@zikzakmedia.com>
  8. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. from openerp.osv import osv, fields
  25. # Lib required to print logs
  26. import logging
  27. # Lib to translate error messages
  28. from openerp.tools.translate import _
  29. # Lib for phone number reformating -> pip install phonenumbers
  30. import phonenumbers
  31. # Lib py-asterisk from http://code.google.com/p/py-asterisk/
  32. # We need a version which has this commit : http://code.google.com/p/py-asterisk/source/detail?r=8d0e1c941cce727c702582f3c9fcd49beb4eeaa4
  33. # so a version after Nov 20th, 2012
  34. from Asterisk import Manager
  35. _logger = logging.getLogger(__name__)
  36. class res_partner(osv.osv):
  37. _inherit = "res.partner"
  38. def dial(self, cr, uid, ids, phone_field='phone', context=None):
  39. '''
  40. This method open the phone call history when the phone click2dial
  41. button of asterisk_click2dial module is pressed
  42. :return the phone call history view of the partner
  43. '''
  44. if context is None:
  45. context = {}
  46. super(res_partner, self).dial(cr, uid, ids, phone_field=phone_field, context=context)
  47. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  48. context['partner_id'] = ids[0]
  49. action_start_wizard = {
  50. 'name': 'Create phone call in CRM',
  51. 'type': 'ir.actions.act_window',
  52. 'res_model': 'wizard.create.crm.phonecall',
  53. 'view_type': 'form',
  54. 'view_mode': 'form',
  55. 'nodestroy': True,
  56. 'target': 'new',
  57. 'context': context,
  58. }
  59. if user.context_propose_creation_crm_call:
  60. return action_start_wizard
  61. else:
  62. return True
  63. class res_users(osv.osv):
  64. _inherit = "res.users"
  65. _columns = {
  66. # Field name starts with 'context_' to allow modification by the user
  67. # in his preferences, cf server-61/openerp/addons/base/res/res_users.py
  68. # line 377 in "def write" of "class users"
  69. 'context_propose_creation_crm_call': fields.boolean('Propose to create a call in CRM after a click2dial'),
  70. }
  71. _defaults = {
  72. 'context_propose_creation_crm_call': True,
  73. }
  74. class crm_lead(osv.osv):
  75. _inherit = "crm.lead"
  76. def _format_phonenumber_to_e164(self, cr, uid, ids, name, arg, context=None):
  77. result = {}
  78. for lead in self.read(cr, uid, ids, ['phone', 'mobile', 'fax'], context=context):
  79. result[lead['id']] = {}
  80. for fromfield, tofield in [('phone', 'phone_e164'), ('mobile', 'mobile_e164'), ('fax', 'fax_e164')]:
  81. if not lead.get(fromfield):
  82. res = False
  83. else:
  84. try:
  85. res = phonenumbers.format_number(phonenumbers.parse(lead.get(fromfield), None), phonenumbers.PhoneNumberFormat.E164)
  86. except Exception, e:
  87. _logger.error("Cannot reformat the phone number '%s' to E.164 format. Error message: %s" % (lead.get(fromfield), e))
  88. _logger.error("You should fix this number and run the wizard 'Reformat all phone numbers' from the menu Settings > Configuration > Asterisk")
  89. # If I raise an exception here, it won't be possible to install
  90. # the module on a DB with bad phone numbers
  91. #raise osv.except_osv(_('Error :'), _("Cannot reformat the phone number '%s' to E.164 format. Error message: %s" % (lead.get(fromfield), e)))
  92. res = False
  93. result[lead['id']][tofield] = res
  94. #print "RESULT _format_phonenumber_to_e164", result
  95. return result
  96. _columns = {
  97. 'phone_e164': fields.function(_format_phonenumber_to_e164, type='char', size=64, string='Phone in E.164 format', readonly=True, multi="e164", store={
  98. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['phone'], 10),
  99. }),
  100. 'mobile_e164': fields.function(_format_phonenumber_to_e164, type='char', size=64, string='Mobile in E.164 format', readonly=True, multi="e164", store={
  101. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['mobile'], 10),
  102. }),
  103. 'fax_e164': fields.function(_format_phonenumber_to_e164, type='char', size=64, string='Fax in E.164 format', readonly=True, multi="e164", store={
  104. 'crm.lead': (lambda self, cr, uid, ids, c={}: ids, ['fax'], 10),
  105. }),
  106. }
  107. def _reformat_phonenumbers(self, cr, uid, vals, context=None):
  108. """Reformat phone numbers in international format i.e. +33141981242"""
  109. phonefields = ['phone', 'fax', 'mobile']
  110. if any([vals.get(field) for field in phonefields]):
  111. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  112. # country_id on res.company is a fields.function that looks at
  113. # company_id.partner_id.addres(default).country_id
  114. if user.company_id.country_id:
  115. user_countrycode = user.company_id.country_id.code
  116. else:
  117. # We need to raise an exception here because, if we pass None as second arg of phonenumbers.parse(), it will raise an exception when you try to enter a phone number in national format... so it's better to raise the exception here
  118. raise osv.except_osv(_('Error :'), _("You should set a country on the company '%s'" % user.company_id.name))
  119. #print "user_countrycode=", user_countrycode
  120. for field in phonefields:
  121. if vals.get(field):
  122. try:
  123. res_parse = phonenumbers.parse(vals.get(field), user_countrycode)
  124. except Exception, e:
  125. raise osv.except_osv(_('Error :'), _("Cannot reformat the phone number '%s' to international format. Error message: %s" % (vals.get(field), e)))
  126. #print "res_parse=", res_parse
  127. vals[field] = phonenumbers.format_number(res_parse, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  128. return vals
  129. def create(self, cr, uid, vals, context=None):
  130. vals_reformated = self._reformat_phonenumbers(cr, uid, vals, context=context)
  131. return super(crm_lead, self).create(cr, uid, vals_reformated, context=context)
  132. def write(self, cr, uid, ids, vals, context=None):
  133. vals_reformated = self._reformat_phonenumbers(cr, uid, vals, context=context)
  134. return super(crm_lead, self).write(cr, uid, ids, vals_reformated, context=context)
  135. def dial(self, cr, uid, ids, phone_field=['phone', 'phone_e164'], context=None):
  136. '''Read the number to dial and call _connect_to_asterisk the right way'''
  137. erp_number_read = self.read(cr, uid, ids[0], phone_field, context=context)
  138. erp_number_e164 = erp_number_read[phone_field[1]]
  139. erp_number_display = erp_number_read[phone_field[0]]
  140. # Check if the number to dial is not empty
  141. if not erp_number_display:
  142. raise osv.except_osv(_('Error :'), _('There is no phone number !'))
  143. elif erp_number_display and not erp_number_e164:
  144. raise osv.except_osv(_('Error :'), _("The phone number isn't stored in the standard E.164 format. Try to run the wizard 'Reformat all phone numbers' from the menu Settings > Configuration > Asterisk."))
  145. return self.pool['asterisk.server']._dial_with_asterisk(cr, uid, erp_number_e164, context=context)
  146. def action_dial_phone(self, cr, uid, ids, context=None):
  147. '''Function called by the button 'Dial' next to the 'phone' field
  148. in the lead view'''
  149. return self.dial(cr, uid, ids, phone_field=['phone', 'phone_e164'], context=context)
  150. def action_dial_mobile(self, cr, uid, ids, context=None):
  151. '''Function called by the button 'Dial' next to the 'mobile' field
  152. in the lead view'''
  153. return self.dial(cr, uid, ids, phone_field=['mobile', 'mobile_e164'], context=context)