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.

186 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2012-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import models, fields, api, _
  5. from openerp.addons.base_phone.fields import Phone
  6. class CrmLead(models.Model):
  7. _inherit = 'crm.lead'
  8. _phone_name_sequence = 20
  9. phone = Phone(country_field='country_id', partner_field='partner_id')
  10. mobile = Phone(country_field='country_id', partner_field='partner_id')
  11. fax = Phone(country_field='country_id', partner_field='partner_id')
  12. phonecall_ids = fields.One2many(
  13. 'crm.phonecall', 'opportunity_id', string='Phone Calls')
  14. phonecall_count = fields.Integer(
  15. compute='_count_phonecalls', string='Number of Phonecalls',
  16. readonly=True)
  17. @api.multi
  18. def name_get(self):
  19. if self._context.get('callerid'):
  20. res = []
  21. for lead in self:
  22. if lead.partner_name and lead.contact_name:
  23. name = u'%s (%s)' % (lead.contact_name, lead.partner_name)
  24. elif lead.partner_name:
  25. name = lead.partner_name
  26. elif lead.contact_name:
  27. name = lead.contact_name
  28. else:
  29. name = lead.name
  30. res.append((lead.id, name))
  31. return res
  32. else:
  33. return super(CrmLead, self).name_get()
  34. @api.multi
  35. @api.depends('phonecall_ids')
  36. def _count_phonecalls(self):
  37. cpo = self.env['crm.phonecall']
  38. for lead in self:
  39. try:
  40. lead.phonecall_count = cpo.search_count(
  41. [('opportunity_id', '=', lead.id)])
  42. except:
  43. lead.phonecall_count = 0
  44. class CrmPhonecall(models.Model):
  45. _name = 'crm.phonecall'
  46. _inherit = ['mail.thread']
  47. _order = "id desc"
  48. # Restore the object that existed in v8
  49. # and doesn't exist in v9 community any more
  50. name = fields.Char(
  51. string='Call Summary', required=True, track_visibility='onchange')
  52. date = fields.Datetime(
  53. string='Date', track_visibility='onchange', copy=False,
  54. default=lambda self: fields.Datetime.now())
  55. description = fields.Text(string='Description', copy=False)
  56. company_id = fields.Many2one(
  57. 'res.company', string='Company',
  58. default=lambda self: self.env['res.company']._company_default_get(
  59. 'crm.phonecall'))
  60. user_id = fields.Many2one(
  61. 'res.users', string='Responsible', track_visibility='onchange',
  62. default=lambda self: self.env.user)
  63. team_id = fields.Many2one(
  64. 'crm.team', string='Sales Team', track_visibility='onchange',
  65. default=lambda self: self.env['crm.team'].browse(
  66. self.env['crm.team']._get_default_team_id()))
  67. partner_id = fields.Many2one(
  68. 'res.partner', string='Contact', ondelete='cascade')
  69. partner_phone = Phone(string='Phone', partner_field='partner_id')
  70. partner_mobile = Phone(string='Mobile', partner_field='partner_id')
  71. priority = fields.Selection([
  72. ('0', 'Low'),
  73. ('1', 'Normal'),
  74. ('2', 'High')
  75. ], string='Priority', track_visibility='onchange', default='1')
  76. opportunity_id = fields.Many2one(
  77. 'crm.lead', string='Lead/Opportunity',
  78. ondelete='cascade', track_visibility='onchange')
  79. state = fields.Selection([
  80. ('open', 'To Do'),
  81. ('done', 'Held'),
  82. ('cancel', 'Cancelled'),
  83. ], string='Status', default='open', copy=False, required=True,
  84. track_visibility='onchange',
  85. help='The status is set to Confirmed, when a case is created.\n'
  86. 'When the call is over, the status is set to Held.\n'
  87. 'If the call is not applicable anymore, the status can be set to '
  88. 'Cancelled.')
  89. direction = fields.Selection([
  90. ('inbound', 'Inbound'),
  91. ('outbound', 'Outbound'),
  92. ], string='Type', required=True, default='outbound')
  93. @api.onchange('partner_id')
  94. def onchange_partner_id(self):
  95. if self.partner_id:
  96. self.partner_phone = self.partner_id.phone
  97. self.partner_mobile = self.partner_id.mobile
  98. @api.onchange('opportunity_id')
  99. def onchange_opportunity_id(self):
  100. if self.opportunity_id:
  101. self.partner_phone = self.opportunity_id.phone
  102. self.partner_mobile = self.opportunity_id.mobile
  103. self.team_id = self.opportunity_id.team_id.id
  104. self.partner_id = self.opportunity_id.partner_id.id
  105. @api.multi
  106. def schedule_another_call(self):
  107. self.ensure_one()
  108. cur_call = self[0]
  109. ctx = self._context.copy()
  110. ctx.update({
  111. 'default_date': False,
  112. 'default_partner_id': cur_call.partner_id.id,
  113. 'default_opportunity_id': cur_call.opportunity_id.id,
  114. 'default_direction': 'outbound',
  115. 'default_partner_phone': cur_call.partner_phone,
  116. 'default_partner_mobile': cur_call.partner_mobile,
  117. })
  118. action = {
  119. 'name': _('Phone Call'),
  120. 'type': 'ir.actions.act_window',
  121. 'res_model': 'crm.phonecall',
  122. 'view_mode': 'form,tree,calendar',
  123. 'context': ctx,
  124. }
  125. return action
  126. class ResPartner(models.Model):
  127. _inherit = 'res.partner'
  128. phonecall_ids = fields.One2many(
  129. 'crm.phonecall', 'partner_id', string='Phone Calls')
  130. phonecall_count = fields.Integer(
  131. compute='_count_phonecalls', string='Number of Phonecalls',
  132. readonly=True)
  133. @api.multi
  134. @api.depends('phonecall_ids')
  135. def _count_phonecalls(self):
  136. cpo = self.env['crm.phonecall']
  137. for partner in self:
  138. try:
  139. partner.phonecall_count = cpo.search_count(
  140. [('partner_id', 'child_of', partner.id)])
  141. except:
  142. partner.phonecall_count = 0
  143. class ResUsers(models.Model):
  144. _inherit = "res.users"
  145. # Field name starts with 'context_' to allow modification by the user
  146. # in his preferences, cf server/openerp/addons/base/res/res_users.py
  147. # in "def write()" of "class res_users(osv.osv)"
  148. context_propose_creation_crm_call = fields.Boolean(
  149. string='Propose to create a call in CRM after a click2dial',
  150. default=True)
  151. class PhoneCommon(models.AbstractModel):
  152. _inherit = 'phone.common'
  153. @api.model
  154. def click2dial(self, erp_number):
  155. res = super(PhoneCommon, self).click2dial(erp_number)
  156. if (
  157. self.env.user.context_propose_creation_crm_call and
  158. self.env.context.get('click2dial_model')
  159. in ('res.partner', 'crm.lead')):
  160. res.update({
  161. 'action_name': _('Create Call in CRM'),
  162. 'action_model': 'wizard.create.crm.phonecall',
  163. })
  164. return res