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.

185 lines
6.7 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']._get_default_team_id())
  66. partner_id = fields.Many2one(
  67. 'res.partner', string='Contact', ondelete='cascade')
  68. partner_phone = Phone(string='Phone', partner_field='partner_id')
  69. partner_mobile = Phone(string='Mobile', partner_field='partner_id')
  70. priority = fields.Selection([
  71. ('0', 'Low'),
  72. ('1', 'Normal'),
  73. ('2', 'High')
  74. ], string='Priority', track_visibility='onchange', default='1')
  75. opportunity_id = fields.Many2one(
  76. 'crm.lead', string='Lead/Opportunity',
  77. ondelete='cascade', track_visibility='onchange')
  78. state = fields.Selection([
  79. ('open', 'To Do'),
  80. ('done', 'Held'),
  81. ('cancel', 'Cancelled'),
  82. ], string='Status', default='open', copy=False, required=True,
  83. track_visibility='onchange',
  84. help='The status is set to Confirmed, when a case is created.\n'
  85. 'When the call is over, the status is set to Held.\n'
  86. 'If the call is not applicable anymore, the status can be set to '
  87. 'Cancelled.')
  88. direction = fields.Selection([
  89. ('inbound', 'Inbound'),
  90. ('outbound', 'Outbound'),
  91. ], string='Type', required=True, default='outbound')
  92. @api.onchange('partner_id')
  93. def onchange_partner_id(self):
  94. if self.partner_id:
  95. self.partner_phone = self.partner_id.phone
  96. self.partner_mobile = self.partner_id.mobile
  97. @api.onchange('opportunity_id')
  98. def onchange_opportunity_id(self):
  99. if self.opportunity_id:
  100. self.partner_phone = self.opportunity_id.phone
  101. self.partner_mobile = self.opportunity_id.mobile
  102. self.team_id = self.opportunity_id.team_id.id
  103. self.partner_id = self.opportunity_id.partner_id.id
  104. @api.multi
  105. def schedule_another_call(self):
  106. self.ensure_one()
  107. cur_call = self[0]
  108. ctx = self._context.copy()
  109. ctx.update({
  110. 'default_date': False,
  111. 'default_partner_id': cur_call.partner_id.id,
  112. 'default_opportunity_id': cur_call.opportunity_id.id,
  113. 'default_direction': 'outbound',
  114. 'default_partner_phone': cur_call.partner_phone,
  115. 'default_partner_mobile': cur_call.partner_mobile,
  116. })
  117. action = {
  118. 'name': _('Phone Call'),
  119. 'type': 'ir.actions.act_window',
  120. 'res_model': 'crm.phonecall',
  121. 'view_mode': 'form,tree,calendar',
  122. 'context': ctx,
  123. }
  124. return action
  125. class ResPartner(models.Model):
  126. _inherit = 'res.partner'
  127. phonecall_ids = fields.One2many(
  128. 'crm.phonecall', 'partner_id', string='Phone Calls')
  129. phonecall_count = fields.Integer(
  130. compute='_count_phonecalls', string='Number of Phonecalls',
  131. readonly=True)
  132. @api.multi
  133. @api.depends('phonecall_ids')
  134. def _count_phonecalls(self):
  135. cpo = self.env['crm.phonecall']
  136. for partner in self:
  137. try:
  138. partner.phonecall_count = cpo.search_count(
  139. [('partner_id', 'child_of', partner.id)])
  140. except:
  141. partner.phonecall_count = 0
  142. class ResUsers(models.Model):
  143. _inherit = "res.users"
  144. # Field name starts with 'context_' to allow modification by the user
  145. # in his preferences, cf server/openerp/addons/base/res/res_users.py
  146. # in "def write()" of "class res_users(osv.osv)"
  147. context_propose_creation_crm_call = fields.Boolean(
  148. string='Propose to create a call in CRM after a click2dial',
  149. default=True)
  150. class PhoneCommon(models.AbstractModel):
  151. _inherit = 'phone.common'
  152. @api.model
  153. def click2dial(self, erp_number):
  154. res = super(PhoneCommon, self).click2dial(erp_number)
  155. if (
  156. self.env.user.context_propose_creation_crm_call and
  157. self.env.context.get('click2dial_model')
  158. in ('res.partner', 'crm.lead')):
  159. res.update({
  160. 'action_name': _('Create Call in CRM'),
  161. 'action_model': 'wizard.create.crm.phonecall',
  162. })
  163. return res