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.

306 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-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
  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. # We match only on the end of the phone number
  83. if len(calling_number) >= 9:
  84. number_to_search = calling_number[-9:len(calling_number)]
  85. else:
  86. number_to_search = calling_number
  87. address_obj = self.pool.get('res.partner.address')
  88. partner = address_obj.get_partner_from_phone_number(
  89. cr, uid, number_to_search, context=context
  90. )
  91. if partner:
  92. res['partner_address_id'] = partner[0]
  93. res['partner_id'] = partner[1]
  94. else:
  95. res['partner_id'] = False
  96. res['partner_address_id'] = False
  97. res['to_update_partner_address_id'] = False
  98. else:
  99. _logger.debug(
  100. "Could not get the calling number from Asterisk."
  101. )
  102. raise osv.except_osv(
  103. _('Error :'),
  104. _("Could not get the calling number from Asterisk. Are you "
  105. "currently on the phone ? If yes, check your setup and "
  106. "look at the OpenERP debug logs.")
  107. )
  108. return res
  109. def open_filtered_object(self, cr, uid, ids, oerp_object, context=None):
  110. """Returns the action that opens the list view of the 'oerp_object'
  111. given as argument filtered on the partner"""
  112. # This module only depends on "base"
  113. # and I don't want to add a dependancy on "sale" or "account"
  114. # So I just check here that the model exists, to avoid a crash
  115. if not self.pool.get('ir.model').search(
  116. cr, uid, [('model', '=', oerp_object._name)], context=context):
  117. raise osv.except_osv(
  118. _('Error :'),
  119. _("The object '%s' is not found in your OpenERP database, "
  120. "probably because the related module is not installed."
  121. % oerp_object._description)
  122. )
  123. partner = self.read(
  124. cr, uid, ids[0], ['partner_id'], context=context)['partner_id']
  125. if partner:
  126. action = {
  127. 'name': oerp_object._description,
  128. 'view_type': 'form',
  129. 'view_mode': 'tree,form',
  130. 'res_model': oerp_object._name,
  131. 'type': 'ir.actions.act_window',
  132. 'nodestroy': False, # close the pop-up wizard after action
  133. 'target': 'current',
  134. 'domain': [('partner_id', '=', partner[0])],
  135. }
  136. return action
  137. else:
  138. return False
  139. def open_sale_orders(self, cr, uid, ids, context=None):
  140. """Function called by the related button of the wizard"""
  141. return self.open_filtered_object(
  142. cr, uid, ids, self.pool.get('sale.order'), context=context
  143. )
  144. def open_invoices(self, cr, uid, ids, context=None):
  145. """Function called by the related button of the wizard"""
  146. return self.open_filtered_object(
  147. cr, uid, ids, self.pool.get('account.invoice'), context=context
  148. )
  149. def simple_open(self, cr, uid, ids, object_name='res.partner',
  150. context=None):
  151. if object_name == 'res.partner':
  152. field = 'partner_id'
  153. label = 'Partner'
  154. elif object_name == 'res.partner.address':
  155. field = 'partner_address_id'
  156. label = 'Contact'
  157. else:
  158. raise osv.except_osv(
  159. _('Error :'),
  160. "This object '%s' is not supported"
  161. % object_name
  162. )
  163. record_to_open = self.read(
  164. cr, uid, ids[0], [field], context=context)[field]
  165. if record_to_open:
  166. return {
  167. 'name': label,
  168. 'view_type': 'form',
  169. 'view_mode': 'form,tree',
  170. 'res_model': object_name,
  171. 'type': 'ir.actions.act_window',
  172. 'nodestroy': False, # close the pop-up wizard after action
  173. 'target': 'current',
  174. 'res_id': record_to_open[0],
  175. }
  176. else:
  177. return False
  178. def open_partner(self, cr, uid, ids, context=None):
  179. """Function called by the related button of the wizard"""
  180. return self.simple_open(
  181. cr, uid, ids, object_name='res.partner', context=context
  182. )
  183. def open_partner_address(self, cr, uid, ids, context=None):
  184. """Function called by the related button of the wizard"""
  185. return self.simple_open(
  186. cr, uid, ids, object_name='res.partner.address', context=context
  187. )
  188. def create_partner_address(self, cr, uid, ids, phone_type='phone',
  189. context=None):
  190. """Function called by the related button of the wizard"""
  191. calling_number = self.read(
  192. cr, uid, ids[0], ['calling_number'], context=context
  193. )['calling_number']
  194. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  195. ast_srv_obj = self.pool.get('asterisk.server')
  196. ast_server = ast_srv_obj._get_asterisk_server_from_user(
  197. cr, uid, user, context=context
  198. )
  199. # Convert the number to the international format
  200. number_to_write = ast_srv_obj._convert_number_to_international_format(
  201. cr, uid, calling_number, ast_server, context=context
  202. )
  203. context['default_' + phone_type] = number_to_write
  204. action = {
  205. 'name': 'Create new contact',
  206. 'view_type': 'form',
  207. 'view_mode': 'form,tree',
  208. 'res_model': 'res.partner.address',
  209. 'type': 'ir.actions.act_window',
  210. 'nodestroy': False,
  211. 'target': 'current',
  212. 'context': context,
  213. }
  214. return action
  215. def create_partner_address_phone(self, cr, uid, ids, context=None):
  216. return self.create_partner_address(
  217. cr, uid, ids, phone_type='phone', context=context
  218. )
  219. def create_partner_address_mobile(self, cr, uid, ids, context=None):
  220. return self.create_partner_address(
  221. cr, uid, ids, phone_type='mobile', context=context
  222. )
  223. def update_partner_address(self, cr, uid, ids, phone_type='mobile',
  224. context=None):
  225. cur_wizard = self.browse(cr, uid, ids[0], context=context)
  226. if not cur_wizard.to_update_partner_address_id:
  227. raise osv.except_osv(
  228. _('Error :'),
  229. _("Select the contact to update.")
  230. )
  231. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  232. ast_srv_obj = self.pool.get('asterisk.server')
  233. ast_server = ast_srv_obj._get_asterisk_server_from_user(
  234. cr, uid, user, context=context
  235. )
  236. number_to_write = ast_srv_obj._convert_number_to_international_format(
  237. cr, uid, cur_wizard.calling_number, ast_server, context=context
  238. )
  239. self.pool.get('res.partner.address').write(
  240. cr, uid, cur_wizard.to_update_partner_address_id.id,
  241. {phone_type: number_to_write},
  242. context=context
  243. )
  244. action = {
  245. 'name': 'Contact: ' + cur_wizard.to_update_partner_address_id.name,
  246. 'view_type': 'form',
  247. 'view_mode': 'form,tree',
  248. 'res_model': 'res.partner.address',
  249. 'type': 'ir.actions.act_window',
  250. 'nodestroy': False,
  251. 'target': 'current',
  252. 'res_id': cur_wizard.to_update_partner_address_id.id
  253. }
  254. return action
  255. def update_partner_address_phone(self, cr, uid, ids, context=None):
  256. return self.update_partner_address(
  257. cr, uid, ids, phone_type='phone', context=context
  258. )
  259. def update_partner_address_mobile(self, cr, uid, ids, context=None):
  260. return self.update_partner_address(
  261. cr, uid, ids, phone_type='mobile', context=context
  262. )
  263. def onchange_to_update_partner_address(self, cr, uid, ids,
  264. to_update_partner_address_id,
  265. context=None):
  266. res = {}
  267. res['value'] = {}
  268. if to_update_partner_address_id:
  269. address_obj = self.pool.get('res.partner.address')
  270. to_update_partner_address = address_obj.browse(
  271. cr, uid, to_update_partner_address_id, context=context
  272. )
  273. res['value'].update({
  274. 'current_phone': to_update_partner_address.phone,
  275. 'current_mobile': to_update_partner_address.mobile
  276. })
  277. else:
  278. res['value'].update({
  279. 'current_phone': False,
  280. 'current_mobile': False,
  281. })
  282. return res
  283. wizard_open_calling_partner()