[REF-IMP] website, website_crm : handle visitors at merge leads and readd page_views stat button on leads
In order to keep all the visitors from all the leads to merge, the relation between leads and visitors becomes a many2many. So that after the lead merge, a lead can have multiple visitors. This commit re-add the page view stat button that were removed during website_visitor introduction and page tracking refactoring. This button counts all the pages views from all the visitors linked tho the current lead. Clicking on it redirect to the complete navigation history, from all visitors linked to the lead. Task ID : 2057428 PR #36180
This commit is contained in:
@@ -13,7 +13,7 @@ from odoo.http import request
|
||||
class WebsitVisitorPage(models.Model):
|
||||
_name = 'website.visitor.page'
|
||||
_description = 'Visited Pages'
|
||||
_order = 'visit_datetime ASC'
|
||||
_order = 'visit_datetime DESC'
|
||||
_log_access = False
|
||||
|
||||
visitor_id = fields.Many2one('website.visitor', ondelete="cascade", index=True, required=True, readonly=True)
|
||||
|
||||
@@ -14,6 +14,7 @@ This module includes contact phone and mobile numbers validation.""",
|
||||
'depends': ['website_form', 'crm'],
|
||||
'data': [
|
||||
'data/website_crm_data.xml',
|
||||
'views/website_crm_lead_views.xml',
|
||||
'views/website_crm_templates.xml',
|
||||
'views/res_config_settings_views.xml',
|
||||
'views/website_visitor_views.xml',
|
||||
|
||||
@@ -1,13 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class Lead(models.Model):
|
||||
_inherit = 'crm.lead'
|
||||
|
||||
visitor_id = fields.Many2one('website.visitor', "Web Visitor")
|
||||
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):
|
||||
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_visitor_page 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.ref('website.website_visitor_page_action').read()[0]
|
||||
action['domain'] = [('visitor_id', 'in', visitors.ids)]
|
||||
# avoid grouping if only few records
|
||||
if len(visitors.visitor_page_ids.ids) > 15 and len(visitors.page_ids.ids) > 1:
|
||||
action['context'] = {'search_default_group_by_page': '1'}
|
||||
return action
|
||||
|
||||
def _merge_data(self, fields):
|
||||
merged_data = super(Lead, self)._merge_data(fields)
|
||||
# add all the visitors from all lead to merge
|
||||
merged_data['visitor_ids'] = [(6, 0, self.visitor_ids.ids)]
|
||||
return merged_data
|
||||
|
||||
def website_form_input_filter(self, request, values):
|
||||
values['medium_id'] = values.get('medium_id') or \
|
||||
|
||||
@@ -7,12 +7,10 @@ from odoo import fields, models, api
|
||||
class WebsiteVisitor(models.Model):
|
||||
_inherit = 'website.visitor'
|
||||
|
||||
lead_ids = fields.One2many('crm.lead', 'visitor_id', string='Leads')
|
||||
lead_count = fields.Integer('# Leads', compute="_compute_lead_count")
|
||||
lead_ids = fields.Many2many('crm.lead', string='Leads', groups="sales_team.group_sale_salesman")
|
||||
lead_count = fields.Integer('# Leads', compute="_compute_lead_count", groups="sales_team.group_sale_salesman")
|
||||
|
||||
@api.depends('lead_ids.visitor_id')
|
||||
@api.depends('lead_ids')
|
||||
def _compute_lead_count(self):
|
||||
page_data = self.env['crm.lead'].read_group([('visitor_id', 'in', self.ids)], ['visitor_id'], ['visitor_id'])
|
||||
mapped_data = dict([(data['visitor_id'][0], data['visitor_id_count']) for data in page_data])
|
||||
for visitor in self:
|
||||
visitor.lead_count = mapped_data.get(visitor.id, 0)
|
||||
visitor.lead_count = len(visitor.lead_ids)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
access_website_visitor_salesman,access_website_visitor_salesman,model_website_visitor,sales_team.group_sale_salesman,1,0,0,0
|
||||
|
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo><data>
|
||||
<record id="crm_lead_view_form" model="ir.ui.view">
|
||||
<field name="name">crm.lead.view.form.inherit.website.crm</field>
|
||||
<field name="model">crm.lead</field>
|
||||
<field name="inherit_id" ref="crm.crm_lead_view_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//div[hasclass('oe_button_box')]" position="inside">
|
||||
<button name="action_redirect_to_page_views" type="object" class="oe_stat_button" icon="fa-tags"
|
||||
attrs="{'invisible': [('visitor_page_count', '=', 0)]}">
|
||||
<field name="visitor_page_count" widget="statinfo" string="Page views"/>
|
||||
</button>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</data></odoo>
|
||||
@@ -5,7 +5,7 @@
|
||||
<field name="name">Leads</field>
|
||||
<field name="res_model">crm.lead</field>
|
||||
<field name="view_mode">tree,form</field>
|
||||
<field name="domain">[('visitor_id', '=', active_id)]</field>
|
||||
<field name="domain">[('visitor_ids', 'in', [active_id])]</field>
|
||||
<field name="help" type="html">
|
||||
<p class="o_view_nocontent_empty_folder">
|
||||
No lead linked for this visitor
|
||||
|
||||
Reference in New Issue
Block a user