mirror of
https://github.com/bringout/oca-ocb-crm.git
synced 2026-04-22 17:52:07 +02:00
19.0 vanilla
This commit is contained in:
parent
dc68f80d3f
commit
7221b9ac46
610 changed files with 135477 additions and 161677 deletions
|
|
@ -1,6 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import chatbot_script
|
||||
from . import chatbot_script_step
|
||||
from . import mail_channel
|
||||
from . import crm_lead
|
||||
from . import discuss_channel
|
||||
from . import res_users
|
||||
|
|
|
|||
|
|
@ -11,12 +11,9 @@ class ChatbotScript(models.Model):
|
|||
string='Generated Lead Count', compute='_compute_lead_count')
|
||||
|
||||
def _compute_lead_count(self):
|
||||
mapped_leads = {}
|
||||
if self.ids:
|
||||
leads_data = self.env['crm.lead'].with_context(active_test=False).sudo()._read_group(
|
||||
[('source_id', 'in', self.mapped('source_id').ids)], ['source_id'], ['source_id'])
|
||||
mapped_leads = {lead['source_id'][0]: lead['source_id_count'] for lead in leads_data}
|
||||
|
||||
leads_data = self.env['crm.lead'].with_context(active_test=False).sudo()._read_group(
|
||||
[('source_id', 'in', self.mapped('source_id').ids)], ['source_id'], ['__count'])
|
||||
mapped_leads = {source.id: count for source, count in leads_data}
|
||||
for script in self:
|
||||
script.lead_count = mapped_leads.get(script.source_id.id, 0)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,40 +1,62 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from ast import literal_eval
|
||||
|
||||
from odoo import _, models, fields
|
||||
from odoo.fields import Domain
|
||||
from odoo.tools import html2plaintext
|
||||
|
||||
|
||||
class ChatbotScriptStep(models.Model):
|
||||
_inherit = 'chatbot.script.step'
|
||||
|
||||
step_type = fields.Selection(
|
||||
selection_add=[('create_lead', 'Create Lead')], ondelete={'create_lead': 'cascade'})
|
||||
selection_add=[
|
||||
("create_lead", "Create Lead"),
|
||||
("create_lead_and_forward", "Create Lead & Forward"),
|
||||
],
|
||||
ondelete={"create_lead": "cascade", "create_lead_and_forward": "cascade"},
|
||||
)
|
||||
crm_team_id = fields.Many2one(
|
||||
'crm.team', string='Sales Team', ondelete='set null',
|
||||
'crm.team', string='Sales Team', ondelete='set null', index="btree_not_null",
|
||||
help="Used in combination with 'create_lead' step type in order to automatically "
|
||||
"assign the created lead/opportunity to the defined team")
|
||||
|
||||
def _chatbot_crm_prepare_lead_values(self, mail_channel, description):
|
||||
return {
|
||||
'description': description + mail_channel._get_channel_history(),
|
||||
'name': _("%s's New Lead", self.chatbot_script_id.title),
|
||||
def _compute_is_forward_operator(self):
|
||||
super()._compute_is_forward_operator()
|
||||
self.filtered(lambda s: s.step_type == "create_lead_and_forward").is_forward_operator = True
|
||||
|
||||
def _chatbot_crm_prepare_lead_values(self, discuss_channel, description):
|
||||
name = self.env._("%s's New Lead", self.chatbot_script_id.title)
|
||||
if msg := self._find_first_user_free_input(discuss_channel):
|
||||
name = html2plaintext(msg.body)[:100]
|
||||
partner = self.env.user.partner_id
|
||||
team = self.crm_team_id
|
||||
if partner.company_id and team.company_id and partner.company_id != team.company_id:
|
||||
team = self.env["crm.team"]
|
||||
vals = {
|
||||
'description': description + discuss_channel._get_channel_history(),
|
||||
"name": name,
|
||||
"origin_channel_id": discuss_channel.id,
|
||||
'source_id': self.chatbot_script_id.source_id.id,
|
||||
'team_id': self.crm_team_id.id,
|
||||
'type': 'lead' if self.crm_team_id.use_leads else 'opportunity',
|
||||
"team_id": team.id,
|
||||
'user_id': False,
|
||||
}
|
||||
if team:
|
||||
vals["type"] = "lead" if team.use_leads else "opportunity"
|
||||
return vals
|
||||
|
||||
def _process_step(self, mail_channel):
|
||||
|
||||
def _process_step(self, discuss_channel):
|
||||
self.ensure_one()
|
||||
|
||||
posted_message = super()._process_step(mail_channel)
|
||||
|
||||
if self.step_type == "create_lead_and_forward":
|
||||
return self._process_step_create_lead_and_forward(discuss_channel)
|
||||
posted_message = super()._process_step(discuss_channel)
|
||||
if self.step_type == 'create_lead':
|
||||
self._process_step_create_lead(mail_channel)
|
||||
|
||||
self._process_step_create_lead(discuss_channel)
|
||||
return posted_message
|
||||
|
||||
def _process_step_create_lead(self, mail_channel):
|
||||
def _process_step_create_lead(self, discuss_channel):
|
||||
""" When reaching a 'create_lead' step, we extract the relevant information: visitor's
|
||||
email, phone and conversation history to create a crm.lead.
|
||||
|
||||
|
|
@ -44,22 +66,62 @@ class ChatbotScriptStep(models.Model):
|
|||
The whole conversation history will be saved into the lead's description for reference.
|
||||
This also allows having a question of type 'free_input_multi' to let the visitor explain
|
||||
their interest / needs before creating the lead. """
|
||||
|
||||
customer_values = self._chatbot_prepare_customer_values(
|
||||
mail_channel, create_partner=False, update_partner=True)
|
||||
discuss_channel, create_partner=False, update_partner=True)
|
||||
if self.env.user._is_public():
|
||||
create_values = {
|
||||
'email_from': customer_values['email'],
|
||||
'phone': customer_values['phone'],
|
||||
}
|
||||
else:
|
||||
partner = self.env.user.partner_id
|
||||
create_values = {
|
||||
'partner_id': partner.id,
|
||||
'company_id': partner.company_id.id,
|
||||
}
|
||||
|
||||
create_values = {"partner_id": self.env.user.partner_id.id}
|
||||
create_values.update(self._chatbot_crm_prepare_lead_values(
|
||||
mail_channel, customer_values['description']))
|
||||
discuss_channel, customer_values['description']))
|
||||
new_leads = self.env["crm.lead"].create(create_values)
|
||||
new_leads._assign_userless_lead_in_team(_('livechat discussion'))
|
||||
return new_leads
|
||||
|
||||
self.env['crm.lead'].create(create_values)
|
||||
def _process_step_create_lead_and_forward(self, discuss_channel):
|
||||
lead = self._process_step_create_lead(discuss_channel)
|
||||
teams = lead.team_id
|
||||
if not teams:
|
||||
possible_teams = self.env["crm.team"].search(
|
||||
Domain("assignment_optout", "=", False) & (
|
||||
Domain("use_leads", "=", True) | Domain("use_opportunities", "=", True)
|
||||
),
|
||||
)
|
||||
teams = possible_teams.filtered(
|
||||
lambda team: team.assignment_max
|
||||
and lead.filtered_domain(literal_eval(team.assignment_domain or "[]"))
|
||||
)
|
||||
if self.env.user.partner_id.company_id:
|
||||
teams = teams.filtered(
|
||||
lambda team: not team.company_id
|
||||
or team.company_id == self.env.user.partner_id.company_id
|
||||
)
|
||||
assignable_user_ids = [
|
||||
member.user_id.id
|
||||
for member in teams.crm_team_member_ids
|
||||
if not member.assignment_optout
|
||||
and member._get_assignment_quota() > 0
|
||||
and lead.filtered_domain(literal_eval(member.assignment_domain or "[]"))
|
||||
]
|
||||
previous_operator = discuss_channel.livechat_operator_id
|
||||
users = self.env["res.users"]
|
||||
if discuss_channel.livechat_channel_id:
|
||||
# sudo: im_livechat.channel - getting available operators is acceptable
|
||||
users = discuss_channel.livechat_channel_id.sudo()._get_available_operators_by_livechat_channel(
|
||||
self.env["res.users"].browse(assignable_user_ids)
|
||||
)[discuss_channel.livechat_channel_id]
|
||||
message = discuss_channel._forward_human_operator(self, users=users)
|
||||
if previous_operator != discuss_channel.livechat_operator_id:
|
||||
user = next(user for user in users if user.partner_id == discuss_channel.livechat_operator_id)
|
||||
lead.user_id = user
|
||||
lead.team_id = next(team for team in teams if user in team.crm_team_member_ids.user_id)
|
||||
msg = self.env._("Created a new lead: %s", lead._get_html_link())
|
||||
user._bus_send_transient_message(discuss_channel, msg)
|
||||
# Call flush_recordset() now (as sudo), otherwise flush_all() is called at the end of
|
||||
# the request with a non-sudo env, which fails (as public user) to compute some crm.lead
|
||||
# fields having dependencies on assigned user_id.
|
||||
lead.flush_recordset()
|
||||
return message
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo.exceptions import AccessError
|
||||
from odoo.addons.mail.tools.discuss import Store
|
||||
|
||||
|
||||
class CrmLead(models.Model):
|
||||
_inherit = "crm.lead"
|
||||
|
||||
origin_channel_id = fields.Many2one(
|
||||
"discuss.channel",
|
||||
"Live chat from which the lead was created",
|
||||
readonly=True,
|
||||
index="btree_not_null",
|
||||
)
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
origin_channel_ids = [
|
||||
vals["origin_channel_id"] for vals in vals_list if vals.get("origin_channel_id")
|
||||
]
|
||||
if not self.env["discuss.channel"].browse(origin_channel_ids).has_access("read"):
|
||||
raise AccessError(
|
||||
self.env._("You cannot create leads linked to channels you don't have access to.")
|
||||
)
|
||||
return super().create(vals_list)
|
||||
|
||||
def write(self, vals):
|
||||
if origin_channel_id := vals.get("origin_channel_id"):
|
||||
if not self.env["discuss.channel"].browse(origin_channel_id).has_access("read"):
|
||||
raise AccessError(
|
||||
self.env._(
|
||||
"You cannot update a lead and link it to a channel you don't have access to."
|
||||
)
|
||||
)
|
||||
return super().write(vals)
|
||||
|
||||
def action_open_livechat(self):
|
||||
Store(bus_channel=self.env.user).add(
|
||||
self.origin_channel_id,
|
||||
extra_fields={"open_chat_window": True},
|
||||
).bus_send()
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from markupsafe import Markup
|
||||
from odoo.addons.mail.tools.discuss import Store
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.tools import html2plaintext
|
||||
|
||||
|
||||
class DiscussChannel(models.Model):
|
||||
_inherit = 'discuss.channel'
|
||||
|
||||
lead_ids = fields.One2many(
|
||||
"crm.lead",
|
||||
"origin_channel_id",
|
||||
string="Leads",
|
||||
groups="sales_team.group_sale_salesman",
|
||||
help="The channel becomes accessible to sales users when leads are set.",
|
||||
)
|
||||
has_crm_lead = fields.Boolean(compute="_compute_has_crm_lead", store=True)
|
||||
_has_crm_lead_index = models.Index("(has_crm_lead) WHERE has_crm_lead IS TRUE")
|
||||
|
||||
@api.depends("lead_ids")
|
||||
def _compute_has_crm_lead(self):
|
||||
for channel in self:
|
||||
channel.has_crm_lead = bool(channel.lead_ids)
|
||||
|
||||
def execute_command_lead(self, **kwargs):
|
||||
key = kwargs['body']
|
||||
lead_command = "/lead"
|
||||
if key.strip() == lead_command:
|
||||
msg = _(
|
||||
"Create a new lead with: "
|
||||
"%(pre_start)s%(lead_command)s %(i_start)slead title%(i_end)s%(pre_end)s",
|
||||
lead_command=lead_command,
|
||||
pre_start=Markup("<pre>"),
|
||||
pre_end=Markup("</pre>"),
|
||||
i_start=Markup("<i>"),
|
||||
i_end=Markup("</i>"),
|
||||
)
|
||||
else:
|
||||
lead = self._convert_visitor_to_lead(self.env.user.partner_id, key)
|
||||
msg = _("Created a new lead: %s", lead._get_html_link())
|
||||
self.env.user._bus_send_transient_message(self, msg)
|
||||
|
||||
def _convert_visitor_to_lead(self, partner, key):
|
||||
""" Create a lead from channel /lead command
|
||||
:param partner: internal user partner (operator) that created the lead;
|
||||
:param key: operator input in chat ('/lead Lead about Product')
|
||||
"""
|
||||
# if public user is part of the chat: consider lead to be linked to an
|
||||
# anonymous user whatever the participants. Otherwise keep only share
|
||||
# partners (no user or portal user) to link to the lead.
|
||||
customers = self.env['res.partner']
|
||||
for customer in self.with_context(active_test=False).channel_partner_ids.filtered(lambda p: p != partner and p.partner_share):
|
||||
if customer.is_public:
|
||||
customers = self.env['res.partner']
|
||||
break
|
||||
else:
|
||||
customers |= customer
|
||||
|
||||
utm_source = self.env.ref('crm_livechat.utm_source_livechat', raise_if_not_found=False)
|
||||
return self.env['crm.lead'].create({
|
||||
"origin_channel_id": self.id,
|
||||
'name': html2plaintext(key[5:]),
|
||||
'partner_id': customers[0].id if customers else False,
|
||||
'user_id': False,
|
||||
'team_id': False,
|
||||
'description': self._get_channel_history(),
|
||||
'referred': partner.name,
|
||||
'source_id': utm_source and utm_source.id,
|
||||
})
|
||||
|
||||
def _get_livechat_session_fields_to_store(self):
|
||||
fields_to_store = super()._get_livechat_session_fields_to_store()
|
||||
if not self.env["crm.lead"].has_access("read"):
|
||||
return fields_to_store
|
||||
fields_to_store.append(
|
||||
Store.Many(
|
||||
"livechat_customer_partner_ids",
|
||||
[Store.Many("opportunity_ids", ["id", "name"])],
|
||||
only_data=True,
|
||||
),
|
||||
)
|
||||
return fields_to_store
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models, _
|
||||
from odoo.tools import html2plaintext
|
||||
|
||||
|
||||
class MailChannel(models.Model):
|
||||
_inherit = 'mail.channel'
|
||||
|
||||
def execute_command_lead(self, **kwargs):
|
||||
partner = self.env.user.partner_id
|
||||
key = kwargs['body']
|
||||
if key.strip() == '/lead':
|
||||
msg = _('Create a new lead (/lead lead title)')
|
||||
else:
|
||||
lead = self._convert_visitor_to_lead(partner, key)
|
||||
msg = _(
|
||||
'Created a new lead: %s',
|
||||
lead._get_html_link(),
|
||||
)
|
||||
self._send_transient_message(partner, msg)
|
||||
|
||||
def _convert_visitor_to_lead(self, partner, key):
|
||||
""" Create a lead from channel /lead command
|
||||
:param partner: internal user partner (operator) that created the lead;
|
||||
:param key: operator input in chat ('/lead Lead about Product')
|
||||
"""
|
||||
# if public user is part of the chat: consider lead to be linked to an
|
||||
# anonymous user whatever the participants. Otherwise keep only share
|
||||
# partners (no user or portal user) to link to the lead.
|
||||
customers = self.env['res.partner']
|
||||
for customer in self.with_context(active_test=False).channel_partner_ids.filtered(lambda p: p != partner and p.partner_share):
|
||||
if customer.is_public:
|
||||
customers = self.env['res.partner']
|
||||
break
|
||||
else:
|
||||
customers |= customer
|
||||
|
||||
utm_source = self.env.ref('crm_livechat.utm_source_livechat', raise_if_not_found=False)
|
||||
return self.env['crm.lead'].create({
|
||||
'name': html2plaintext(key[5:]),
|
||||
'partner_id': customers[0].id if customers else False,
|
||||
'user_id': False,
|
||||
'team_id': False,
|
||||
'description': self._get_channel_history(),
|
||||
'referred': partner.name,
|
||||
'source_id': utm_source and utm_source.id,
|
||||
})
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
from odoo import models
|
||||
from odoo.addons.mail.tools.discuss import Store
|
||||
|
||||
|
||||
class ResUsers(models.Model):
|
||||
_inherit = "res.users"
|
||||
|
||||
def _init_store_data(self, store: Store):
|
||||
super()._init_store_data(store)
|
||||
store.add_global_values(has_access_create_lead=self.env.user.has_group("sales_team.group_sale_salesman"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue