Initial commit: Crm packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:49 +02:00
commit 21a345b5b9
654 changed files with 418312 additions and 0 deletions

View file

@ -0,0 +1,6 @@
# -*- 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

View file

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, fields
class ChatbotScript(models.Model):
_inherit = 'chatbot.script'
lead_count = fields.Integer(
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}
for script in self:
script.lead_count = mapped_leads.get(script.source_id.id, 0)
def action_view_leads(self):
self.ensure_one()
action = self.env['ir.actions.act_window']._for_xml_id('crm.crm_lead_all_leads')
action['domain'] = [('source_id', '=', self.source_id.id)]
action['context'] = {'create': False}
return action

View file

@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, models, fields
class ChatbotScriptStep(models.Model):
_inherit = 'chatbot.script.step'
step_type = fields.Selection(
selection_add=[('create_lead', 'Create Lead')], ondelete={'create_lead': 'cascade'})
crm_team_id = fields.Many2one(
'crm.team', string='Sales Team', ondelete='set 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),
'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',
'user_id': False,
}
def _process_step(self, mail_channel):
self.ensure_one()
posted_message = super()._process_step(mail_channel)
if self.step_type == 'create_lead':
self._process_step_create_lead(mail_channel)
return posted_message
def _process_step_create_lead(self, mail_channel):
""" When reaching a 'create_lead' step, we extract the relevant information: visitor's
email, phone and conversation history to create a crm.lead.
We use the email and phone to update the environment partner's information (if not a public
user) if they differ from the current values.
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)
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.update(self._chatbot_crm_prepare_lead_values(
mail_channel, customer_values['description']))
self.env['crm.lead'].create(create_values)

View file

@ -0,0 +1,49 @@
# -*- 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,
})