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.

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