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.

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