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.

209 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 osv import osv, fields
  22. import netsvc
  23. # Lib to translate error messages
  24. from tools.translate import _
  25. logger = netsvc.Logger()
  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_address_id': fields.many2one('res.partner.address', 'Contact name', help="Partner contact related to the calling number. If there is none and you want to update an existing partner"),
  35. 'partner_id': fields.many2one('res.partner', 'Partner', help="Partner related to the calling number."),
  36. 'to_update_partner_address_id': fields.many2one('res.partner.address', 'Contact to update', help="Partner contact on which the phone or mobile number will be written"),
  37. 'current_phone': fields.related('to_update_partner_address_id', 'phone', type='char', relation='res.partner.address', string='Current phone'),
  38. 'current_mobile': fields.related('to_update_partner_address_id', 'mobile', type='char', relation='res.partner.address', 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. partner = self.pool.get('res.partner.address').get_partner_from_phone_number(cr, uid, calling_number, context=context)
  50. if partner:
  51. res['partner_address_id'] = partner[0]
  52. res['partner_id'] = partner[1]
  53. else:
  54. res['partner_id'] = False
  55. res['partner_address_id'] = False
  56. res['to_update_partner_address_id'] = False
  57. else:
  58. logger.notifyChannel('click2dial', netsvc.LOG_DEBUG, "Could not get the calling number from Asterisk.")
  59. raise osv.except_osv(_('Error :'), _("Could not get the calling number from Asterisk. Are you currently on the phone ? If yes, check your setup and look at the OpenERP debug logs."))
  60. return res
  61. def open_filtered_object(self, cr, uid, ids, oerp_object, context=None):
  62. '''Returns the action that opens the list view of the 'oerp_object'
  63. given as argument filtered on the partner'''
  64. # This module only depends on "base"
  65. # and I don't want to add a dependancy on "sale" or "account"
  66. # So I just check here that the model exists, to avoid a crash
  67. if not self.pool.get('ir.model').search(cr, uid, [('model', '=', oerp_object._name)], context=context):
  68. 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))
  69. partner_id = self.browse(cr, uid, ids[0], context=context).partner_id.id
  70. if partner_id:
  71. action = {
  72. 'name': oerp_object._description,
  73. 'view_type': 'form',
  74. 'view_mode': 'tree,form',
  75. 'res_model': oerp_object._name,
  76. 'type': 'ir.actions.act_window',
  77. 'nodestroy': False, # close the pop-up wizard after action
  78. 'target': 'current',
  79. 'domain': [('partner_id', '=', partner_id)],
  80. }
  81. return action
  82. else:
  83. return False
  84. def open_sale_orders(self, cr, uid, ids, context=None):
  85. '''Function called by the related button of the wizard'''
  86. return self.open_filtered_object(cr, uid, ids, self.pool.get('sale.order'), context=context)
  87. def open_invoices(self, cr, uid, ids, context=None):
  88. '''Function called by the related button of the wizard'''
  89. return self.open_filtered_object(cr, uid, ids, self.pool.get('account.invoice'), context=context)
  90. def simple_open(self, cr, uid, ids, object_name='res.partner', context=None):
  91. if object_name == 'res.partner':
  92. record_id = self.browse(cr, uid, ids[0], context=context).partner_id.id
  93. label = 'Partner'
  94. elif object_name == 'res.partner.address':
  95. record_id = self.browse(cr, uid, ids[0], context=context).partner_address_id.id
  96. label = 'Contact'
  97. else:
  98. raise osv.except_osv(_('Error :'), "This object '%s' is not supported" % object_name)
  99. if record_id:
  100. return {
  101. 'name': label,
  102. 'view_type': 'form',
  103. 'view_mode': 'form,tree',
  104. 'res_model': object_name,
  105. 'type': 'ir.actions.act_window',
  106. 'nodestroy': False, # close the pop-up wizard after action
  107. 'target': 'current',
  108. 'res_id': record_id,
  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, object_name='res.partner', context=context)
  115. def open_partner_address(self, cr, uid, ids, context=None):
  116. '''Function called by the related button of the wizard'''
  117. return self.simple_open(cr, uid, ids, object_name='res.partner.address', context=context)
  118. def create_partner_address(self, cr, uid, ids, phone_type='phone', context=None):
  119. '''Function called by the related button of the wizard'''
  120. calling_number = self.read(cr, uid, ids[0], ['calling_number'], context=context)['calling_number']
  121. ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, context=context)
  122. # Convert the number to the international format
  123. number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, calling_number, ast_server, context=context)
  124. context['default_' + phone_type] = number_to_write
  125. action = {
  126. 'name': 'Create new contact',
  127. 'view_type': 'form',
  128. 'view_mode': 'form,tree',
  129. 'res_model': 'res.partner.address',
  130. 'type': 'ir.actions.act_window',
  131. 'nodestroy': False,
  132. 'target': 'current',
  133. 'context': context,
  134. }
  135. return action
  136. def create_partner_address_phone(self, cr, uid, ids, context=None):
  137. return self.create_partner_address(cr, uid, ids, phone_type='phone', context=context)
  138. def create_partner_address_mobile(self, cr, uid, ids, context=None):
  139. return self.create_partner_address(cr, uid, ids, phone_type='mobile', context=context)
  140. def update_partner_address(self, cr, uid, ids, phone_type='mobile', context=None):
  141. cur_wizard = self.browse(cr, uid, ids[0], context=context)
  142. if not cur_wizard.to_update_partner_address_id:
  143. raise osv.except_osv(_('Error :'), _("Select the contact to update."))
  144. ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, context=context)
  145. number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, cur_wizard.calling_number, ast_server, context=context)
  146. self.pool.get('res.partner.address').write(cr, uid, cur_wizard.to_update_partner_address_id.id, {phone_type: number_to_write}, context=context)
  147. action = {
  148. 'name': 'Contact: ' + cur_wizard.to_update_partner_address_id.name,
  149. 'view_type': 'form',
  150. 'view_mode': 'form,tree',
  151. 'res_model': 'res.partner.address',
  152. 'type': 'ir.actions.act_window',
  153. 'nodestroy': False,
  154. 'target': 'current',
  155. 'res_id': cur_wizard.to_update_partner_address_id.id
  156. }
  157. return action
  158. def update_partner_address_phone(self, cr, uid, ids, context=None):
  159. return self.update_partner_address(cr, uid, ids, phone_type='phone', context=context)
  160. def update_partner_address_mobile(self, cr, uid, ids, context=None):
  161. return self.update_partner_address(cr, uid, ids, phone_type='mobile', context=context)
  162. def onchange_to_update_partner_address(self, cr, uid, ids, to_update_partner_address_id, context=None):
  163. res = {}
  164. res['value'] = {}
  165. if to_update_partner_address_id:
  166. to_update_partner_address = self.pool.get('res.partner.address').browse(cr, uid, to_update_partner_address_id, context=context)
  167. res['value'].update({'current_phone': to_update_partner_address.phone,
  168. 'current_mobile': to_update_partner_address.mobile})
  169. else:
  170. res['value'].update({'current_phone': False, 'current_mobile': False})
  171. return res
  172. wizard_open_calling_partner()