Purpose of this commit is to make clearer how final opportunity value are computed or chosen when merging leads. Functionally nothing should change with this commit. LINKS Task ID-2446759 (lead merge priority field management) Task ID-2446883 (query counters fix) PR odoo/odoo#65016
74 lines
3.4 KiB
Python
74 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, SUPERUSER_ID
|
|
|
|
|
|
class Lead(models.Model):
|
|
_inherit = 'crm.lead'
|
|
|
|
visitor_ids = fields.Many2many('website.visitor', string="Web Visitors")
|
|
visitor_page_count = fields.Integer('# Page Views', compute="_compute_visitor_page_count")
|
|
|
|
@api.depends('visitor_ids.page_ids')
|
|
def _compute_visitor_page_count(self):
|
|
mapped_data = {}
|
|
if self.ids:
|
|
self.flush(['visitor_ids'])
|
|
sql = """ SELECT l.id as lead_id, count(*) as page_view_count
|
|
FROM crm_lead l
|
|
JOIN crm_lead_website_visitor_rel lv ON l.id = lv.crm_lead_id
|
|
JOIN website_visitor v ON v.id = lv.website_visitor_id
|
|
JOIN website_track p ON p.visitor_id = v.id
|
|
WHERE l.id in %s
|
|
GROUP BY l.id"""
|
|
self.env.cr.execute(sql, (tuple(self.ids),))
|
|
page_data = self.env.cr.dictfetchall()
|
|
mapped_data = {data['lead_id']: data['page_view_count'] for data in page_data}
|
|
for lead in self:
|
|
lead.visitor_page_count = mapped_data.get(lead.id, 0)
|
|
|
|
def action_redirect_to_page_views(self):
|
|
visitors = self.visitor_ids
|
|
action = self.env["ir.actions.actions"]._for_xml_id("website.website_visitor_page_action")
|
|
action['domain'] = [('visitor_id', 'in', visitors.ids)]
|
|
# avoid grouping if only few records
|
|
if len(visitors.website_track_ids.ids) > 15 and len(visitors.page_ids.ids) > 1:
|
|
action['context'] = {'search_default_group_by_page': '1'}
|
|
return action
|
|
|
|
def _merge_get_fields_specific(self):
|
|
fields_info = super(Lead, self)._merge_get_fields_specific()
|
|
# add all the visitors from all lead to merge
|
|
fields_info['visitor_ids'] = lambda fname, leads: [(6, 0, leads.visitor_ids.ids)]
|
|
return fields_info
|
|
|
|
def website_form_input_filter(self, request, values):
|
|
values['medium_id'] = values.get('medium_id') or \
|
|
self.default_get(['medium_id']).get('medium_id') or \
|
|
self.sudo().env.ref('utm.utm_medium_website').id
|
|
values['team_id'] = values.get('team_id') or \
|
|
request.website.crm_default_team_id.id
|
|
values['user_id'] = values.get('user_id') or \
|
|
request.website.crm_default_user_id.id
|
|
values['type'] = 'lead' if self.with_user(SUPERUSER_ID).env['res.users'].has_group('crm.group_use_lead') else 'opportunity'
|
|
return values
|
|
|
|
|
|
class Website(models.Model):
|
|
_inherit = 'website'
|
|
|
|
def _get_crm_default_team_domain(self):
|
|
if not self.env.user.has_group('crm.group_use_lead'):
|
|
return [('use_opportunities', '=', True)]
|
|
return [('use_leads', '=', True)]
|
|
|
|
crm_default_team_id = fields.Many2one(
|
|
'crm.team', string='Default Sales Teams',
|
|
default=lambda self: self.env['crm.team'].search([], limit=1),
|
|
domain=lambda self: self._get_crm_default_team_domain(),
|
|
help='Default Sales Team for new leads created through the Contact Us form.')
|
|
crm_default_user_id = fields.Many2one(
|
|
'res.users', string='Default Salesperson', domain=[('share', '=', False)],
|
|
help='Default salesperson for new leads created through the Contact Us form.')
|