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.

213 lines
10 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2010-2012 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 osv import osv, fields
  22. import logging
  23. # Lib to translate error messages
  24. from tools.translate import _
  25. _logger = logging.getLogger(__name__)
  26. class wizard_open_calling_partner(osv.osv_memory):
  27. _name = "wizard.open.calling.partner"
  28. _description = "Open calling partner"
  29. _columns = {
  30. # I can't set any field to readonly, because otherwize it would call
  31. # default_get (and thus connect to Asterisk) a second time when the user
  32. # clicks on one of the buttons
  33. 'calling_number': fields.char('Calling number', size=30, help="Phone number of calling party that has been obtained from Asterisk."),
  34. 'partner_id': fields.many2one('res.partner', 'Partner name', help="Partner related to the calling number."),
  35. 'parent_partner_id': fields.many2one('res.partner', 'Parent partner', help="Parent Partner related to the calling number."),
  36. 'to_update_partner_id': fields.many2one('res.partner', 'Partner to update', help="Partner on which the phone or mobile number will be written"),
  37. 'current_phone': fields.related('to_update_partner_id', 'phone', type='char', relation='res.partner', string='Current phone'),
  38. 'current_mobile': fields.related('to_update_partner_id', 'mobile', type='char', relation='res.partner', string='Current mobile'),
  39. }
  40. def default_get(self, cr, uid, fields, context=None):
  41. '''Thanks to the default_get method, we are able to query Asterisk and
  42. get the corresponding partner when we launch the wizard'''
  43. res = {}
  44. calling_number = self.pool.get('asterisk.server')._get_calling_number(cr, uid, context=context)
  45. #To test the code without Asterisk server
  46. #calling_number = "0141981242"
  47. if calling_number:
  48. res['calling_number'] = calling_number
  49. # We match only on the end of the phone number
  50. # TODO : make this parameter configurable
  51. if len(calling_number) >= 9:
  52. number_to_search = calling_number[-9:len(calling_number)]
  53. else:
  54. number_to_search = calling_number
  55. partner = self.pool.get('res.partner').get_partner_from_phone_number(cr, uid, number_to_search, context=context)
  56. if partner:
  57. res['partner_id'] = partner[0]
  58. res['parent_partner_id'] = partner[1]
  59. else:
  60. res['partner_id'] = False
  61. res['parent_partner_id'] = False
  62. res['to_update_partner_id'] = False
  63. else:
  64. _logger.debug("Could not get the calling number from Asterisk.")
  65. raise osv.except_osv(_('Error :'), _("Could not get the calling number from Asterisk. Is your phone ringing or are you currently on the phone ? If yes, check your setup and look at the OpenERP debug logs."))
  66. return res
  67. def open_filtered_object(self, cr, uid, ids, oerp_object, context=None):
  68. '''Returns the action that opens the list view of the 'oerp_object'
  69. given as argument filtered on the partner'''
  70. # This module only depends on "base"
  71. # and I don't want to add a dependancy on "sale" or "account"
  72. # So I just check here that the model exists, to avoid a crash
  73. if not self.pool.get('ir.model').search(cr, uid, [('model', '=', oerp_object._name)], context=context):
  74. raise osv.except_osv(_('Error :'), _("The object '%s' is not found in your OpenERP database, probably because the related module is not installed." % oerp_object._description))
  75. partner = self.read(cr, uid, ids[0], ['partner_id', 'parent_partner_id'], context=context)
  76. partner_id_to_filter = partner['parent_partner_id'] and partner['parent_partner_id'][0] or (partner['partner_id'] and partner['partner_id'][0] or False)
  77. if partner_id_to_filter:
  78. action = {
  79. 'name': oerp_object._description,
  80. 'view_type': 'form',
  81. 'view_mode': 'tree,form',
  82. 'res_model': oerp_object._name,
  83. 'type': 'ir.actions.act_window',
  84. 'nodestroy': False, # close the pop-up wizard after action
  85. 'target': 'current',
  86. 'domain': [('partner_id', '=', partner_id_to_filter)],
  87. }
  88. return action
  89. else:
  90. return False
  91. def open_sale_orders(self, cr, uid, ids, context=None):
  92. '''Function called by the related button of the wizard'''
  93. return self.open_filtered_object(cr, uid, ids, self.pool.get('sale.order'), context=context)
  94. def open_invoices(self, cr, uid, ids, context=None):
  95. '''Function called by the related button of the wizard'''
  96. return self.open_filtered_object(cr, uid, ids, self.pool.get('account.invoice'), context=context)
  97. def simple_open(self, cr, uid, ids, field='partner_id', context=None):
  98. record_to_open = self.read(cr, uid, ids[0], [field], context=context)[field]
  99. if record_to_open:
  100. return {
  101. 'name': self.pool.get('res.partner')._description,
  102. 'view_type': 'form',
  103. 'view_mode': 'form,tree',
  104. 'res_model': 'res.partner',
  105. 'type': 'ir.actions.act_window',
  106. 'nodestroy': False, # close the pop-up wizard after action
  107. 'target': 'current',
  108. 'res_id': record_to_open[0],
  109. }
  110. else:
  111. return False
  112. def open_partner(self, cr, uid, ids, context=None):
  113. '''Function called by the related button of the wizard'''
  114. return self.simple_open(cr, uid, ids, field='partner_id', context=context)
  115. # TODO
  116. def open_parent_partner(self, cr, uid, ids, context=None):
  117. '''Function called by the related button of the wizard'''
  118. return self.simple_open(cr, uid, ids, field='parent_partner_id', context=context)
  119. def create_partner(self, cr, uid, ids, phone_type='phone', context=None):
  120. '''Function called by the related button of the wizard'''
  121. calling_number = self.read(cr, uid, ids[0], ['calling_number'], context=context)['calling_number']
  122. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  123. ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, user, context=context)
  124. # Convert the number to the international format
  125. number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, calling_number, ast_server, context=context)
  126. context['default_' + phone_type] = number_to_write
  127. action = {
  128. 'name': 'Create new partner',
  129. 'view_type': 'form',
  130. 'view_mode': 'form,tree',
  131. 'res_model': 'res.partner',
  132. 'type': 'ir.actions.act_window',
  133. 'nodestroy': False,
  134. 'target': 'current',
  135. 'context': context,
  136. }
  137. return action
  138. def create_partner_phone(self, cr, uid, ids, context=None):
  139. return self.create_partner(cr, uid, ids, phone_type='phone', context=context)
  140. def create_partner_mobile(self, cr, uid, ids, context=None):
  141. return self.create_partner(cr, uid, ids, phone_type='mobile', context=context)
  142. def update_partner(self, cr, uid, ids, phone_type='mobile', context=None):
  143. cur_wizard = self.browse(cr, uid, ids[0], context=context)
  144. if not cur_wizard.to_update_partner_id:
  145. raise osv.except_osv(_('Error :'), _("Select the partner to update."))
  146. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  147. ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, user, context=context)
  148. number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, cur_wizard.calling_number, ast_server, context=context)
  149. self.pool.get('res.partner').write(cr, uid, cur_wizard.to_update_partner_id.id, {phone_type: number_to_write}, context=context)
  150. action = {
  151. 'name': 'Partner: ' + cur_wizard.to_update_partner_id.name,
  152. 'view_type': 'form',
  153. 'view_mode': 'form,tree',
  154. 'res_model': 'res.partner',
  155. 'type': 'ir.actions.act_window',
  156. 'nodestroy': False,
  157. 'target': 'current',
  158. 'res_id': cur_wizard.to_update_partner_id.id
  159. }
  160. return action
  161. def update_partner_phone(self, cr, uid, ids, context=None):
  162. return self.update_partner(cr, uid, ids, phone_type='phone', context=context)
  163. def update_partner_mobile(self, cr, uid, ids, context=None):
  164. return self.update_partner(cr, uid, ids, phone_type='mobile', context=context)
  165. def onchange_to_update_partner(self, cr, uid, ids, to_update_partner_id, context=None):
  166. res = {}
  167. res['value'] = {}
  168. if to_update_partner_id:
  169. to_update_partner = self.pool.get('res.partner').browse(cr, uid, to_update_partner_id, context=context)
  170. res['value'].update({'current_phone': to_update_partner.phone,
  171. 'current_mobile': to_update_partner.mobile})
  172. else:
  173. res['value'].update({'current_phone': False, 'current_mobile': False})
  174. return res
  175. wizard_open_calling_partner()