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.

301 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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
  32. # user clicks on one of the buttons
  33. 'calling_number': fields.char(
  34. 'Calling number',
  35. size=30,
  36. help="Phone number of calling party that has been obtained from "
  37. "Asterisk."
  38. ),
  39. 'partner_address_id': fields.many2one(
  40. 'res.partner.address',
  41. 'Contact name',
  42. help="Partner contact related to the calling number. If there is "
  43. "none and you want to update an existing partner"
  44. ),
  45. 'partner_id': fields.many2one(
  46. 'res.partner',
  47. 'Partner',
  48. help="Partner related to the calling number."
  49. ),
  50. 'to_update_partner_address_id': fields.many2one(
  51. 'res.partner.address',
  52. 'Contact to update',
  53. help="Partner contact on which the phone or mobile number will be "
  54. "written"
  55. ),
  56. 'current_phone': fields.related(
  57. 'to_update_partner_address_id',
  58. 'phone',
  59. type='char',
  60. relation='res.partner.address',
  61. string='Current phone'
  62. ),
  63. 'current_mobile': fields.related(
  64. 'to_update_partner_address_id',
  65. 'mobile',
  66. type='char',
  67. relation='res.partner.address',
  68. string='Current mobile'
  69. ),
  70. }
  71. def default_get(self, cr, uid, fields, context=None):
  72. """Thanks to the default_get method, we are able to query Asterisk and
  73. get the corresponding partner when we launch the wizard"""
  74. res = {}
  75. calling_number = self.pool.get('asterisk.server')._get_calling_number(
  76. cr, uid, context=context
  77. )
  78. # To test the code without Asterisk server
  79. # calling_number = "0141981242"
  80. if calling_number:
  81. res['calling_number'] = calling_number
  82. address_obj = self.pool.get('res.partner.address')
  83. partner = address_obj.get_partner_from_phone_number(
  84. cr, uid, calling_number, context=context
  85. )
  86. if partner:
  87. res['partner_address_id'] = partner[0]
  88. res['partner_id'] = partner[1]
  89. else:
  90. res['partner_id'] = False
  91. res['partner_address_id'] = False
  92. res['to_update_partner_address_id'] = False
  93. else:
  94. logger.notifyChannel(
  95. 'click2dial',
  96. netsvc.LOG_DEBUG,
  97. "Could not get the calling number from Asterisk."
  98. )
  99. raise osv.except_osv(
  100. _('Error :'),
  101. _("Could not get the calling number from Asterisk. "
  102. "Are you currently on the phone ? If yes, check your setup "
  103. "and look at the OpenERP debug logs.")
  104. )
  105. return res
  106. def open_filtered_object(self, cr, uid, ids, oerp_object, context=None):
  107. """Returns the action that opens the list view of the 'oerp_object'
  108. given as argument filtered on the partner"""
  109. # This module only depends on "base"
  110. # and I don't want to add a dependancy on "sale" or "account"
  111. # So I just check here that the model exists, to avoid a crash
  112. if not self.pool.get('ir.model').search(
  113. cr, uid, [('model', '=', oerp_object._name)], context=context
  114. ):
  115. raise osv.except_osv(
  116. _('Error :'),
  117. _("The object '%s' is not found in your OpenERP database, "
  118. "probably because the related module is not installed."
  119. % oerp_object._description)
  120. )
  121. partner_id = self.browse(
  122. cr, uid, ids[0], context=context).partner_id.id
  123. if partner_id:
  124. action = {
  125. 'name': oerp_object._description,
  126. 'view_type': 'form',
  127. 'view_mode': 'tree,form',
  128. 'res_model': oerp_object._name,
  129. 'type': 'ir.actions.act_window',
  130. 'nodestroy': False, # close the pop-up wizard after action
  131. 'target': 'current',
  132. 'domain': [('partner_id', '=', partner_id)],
  133. }
  134. return action
  135. else:
  136. return False
  137. def open_sale_orders(self, cr, uid, ids, context=None):
  138. """Function called by the related button of the wizard"""
  139. return self.open_filtered_object(
  140. cr, uid, ids, self.pool.get('sale.order'), context=context
  141. )
  142. def open_invoices(self, cr, uid, ids, context=None):
  143. """Function called by the related button of the wizard"""
  144. return self.open_filtered_object(
  145. cr, uid, ids, self.pool.get('account.invoice'), context=context
  146. )
  147. def simple_open(self, cr, uid, ids, object_name='res.partner',
  148. context=None):
  149. if object_name == 'res.partner':
  150. record_id = self.browse(
  151. cr, uid, ids[0], context=context).partner_id.id
  152. label = 'Partner'
  153. elif object_name == 'res.partner.address':
  154. record_id = self.browse(
  155. cr, uid, ids[0], context=context).partner_address_id.id
  156. label = 'Contact'
  157. else:
  158. raise osv.except_osv(
  159. _('Error :'),
  160. "This object '%s' is not supported" % object_name
  161. )
  162. if record_id:
  163. return {
  164. 'name': label,
  165. 'view_type': 'form',
  166. 'view_mode': 'form,tree',
  167. 'res_model': object_name,
  168. 'type': 'ir.actions.act_window',
  169. 'nodestroy': False, # close the pop-up wizard after action
  170. 'target': 'current',
  171. 'res_id': record_id,
  172. }
  173. else:
  174. return False
  175. def open_partner(self, cr, uid, ids, context=None):
  176. """Function called by the related button of the wizard"""
  177. return self.simple_open(
  178. cr, uid, ids, object_name='res.partner', context=context
  179. )
  180. def open_partner_address(self, cr, uid, ids, context=None):
  181. """Function called by the related button of the wizard"""
  182. return self.simple_open(
  183. cr, uid, ids, object_name='res.partner.address', context=context
  184. )
  185. def create_partner_address(self, cr, uid, ids, phone_type='phone',
  186. context=None):
  187. """Function called by the related button of the wizard"""
  188. calling_number = self.read(
  189. cr, uid, ids[0], ['calling_number'], context=context
  190. )['calling_number']
  191. ast_srv_obj = self.pool.get('asterisk.server')
  192. ast_server = ast_srv_obj._get_asterisk_server_from_user(
  193. cr, uid, context=context
  194. )
  195. # Convert the number to the international format
  196. number_to_write = ast_srv_obj._convert_number_to_international_format(
  197. cr, uid, calling_number, ast_server, context=context
  198. )
  199. context['default_' + phone_type] = number_to_write
  200. action = {
  201. 'name': 'Create new contact',
  202. 'view_type': 'form',
  203. 'view_mode': 'form,tree',
  204. 'res_model': 'res.partner.address',
  205. 'type': 'ir.actions.act_window',
  206. 'nodestroy': False,
  207. 'target': 'current',
  208. 'context': context,
  209. }
  210. return action
  211. def create_partner_address_phone(self, cr, uid, ids, context=None):
  212. return self.create_partner_address(
  213. cr, uid, ids, phone_type='phone', context=context
  214. )
  215. def create_partner_address_mobile(self, cr, uid, ids, context=None):
  216. return self.create_partner_address(
  217. cr, uid, ids, phone_type='mobile', context=context
  218. )
  219. def update_partner_address(self, cr, uid, ids, phone_type='mobile',
  220. context=None):
  221. cur_wizard = self.browse(cr, uid, ids[0], context=context)
  222. if not cur_wizard.to_update_partner_address_id:
  223. raise osv.except_osv(
  224. _('Error :'),
  225. _("Select the contact to update.")
  226. )
  227. ast_srv_obj = self.pool.get('asterisk.server')
  228. ast_server = ast_srv_obj._get_asterisk_server_from_user(
  229. cr, uid, context=context
  230. )
  231. number_to_write = ast_srv_obj._convert_number_to_international_format(
  232. cr, uid, cur_wizard.calling_number, ast_server, context=context
  233. )
  234. self.pool.get('res.partner.address').write(
  235. cr, uid, cur_wizard.to_update_partner_address_id.id,
  236. {phone_type: number_to_write},
  237. context=context
  238. )
  239. action = {
  240. 'name': 'Contact: ' + cur_wizard.to_update_partner_address_id.name,
  241. 'view_type': 'form',
  242. 'view_mode': 'form,tree',
  243. 'res_model': 'res.partner.address',
  244. 'type': 'ir.actions.act_window',
  245. 'nodestroy': False,
  246. 'target': 'current',
  247. 'res_id': cur_wizard.to_update_partner_address_id.id
  248. }
  249. return action
  250. def update_partner_address_phone(self, cr, uid, ids, context=None):
  251. return self.update_partner_address(
  252. cr, uid, ids, phone_type='phone', context=context
  253. )
  254. def update_partner_address_mobile(self, cr, uid, ids, context=None):
  255. return self.update_partner_address(
  256. cr, uid, ids, phone_type='mobile', context=context
  257. )
  258. def onchange_to_update_partner_address(self, cr, uid, ids,
  259. to_update_partner_address_id,
  260. context=None):
  261. res = {}
  262. res['value'] = {}
  263. if to_update_partner_address_id:
  264. address_obj = self.pool.get('res.partner.address')
  265. to_update_partner_address = address_obj.browse(
  266. cr, uid, to_update_partner_address_id, context=context
  267. )
  268. res['value'].update({
  269. 'current_phone': to_update_partner_address.phone,
  270. 'current_mobile': to_update_partner_address.mobile
  271. })
  272. else:
  273. res['value'].update({
  274. 'current_phone': False,
  275. 'current_mobile': False,
  276. })
  277. return res
  278. wizard_open_calling_partner()