diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website_crm/models/crm_lead.py | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_crm/models/crm_lead.py')
| -rw-r--r-- | addons/website_crm/models/crm_lead.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/addons/website_crm/models/crm_lead.py b/addons/website_crm/models/crm_lead.py new file mode 100644 index 00000000..9612c999 --- /dev/null +++ b/addons/website_crm/models/crm_lead.py @@ -0,0 +1,73 @@ +# -*- 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_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 \ + 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.') |
