Skip to content

Commit

Permalink
[ADD] module crm_lead_to_opportunity_contact
Browse files Browse the repository at this point in the history
With this module, in the lead to opportunity wizard, you have a new
option: create a new contact on an existing customer
  • Loading branch information
alexis-via committed Jan 19, 2023
1 parent cf2a150 commit e04f0c8
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions crm_lead_to_opportunity_contact/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Will be auto-generated from the readme subdir
2 changes: 2 additions & 0 deletions crm_lead_to_opportunity_contact/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizards
20 changes: 20 additions & 0 deletions crm_lead_to_opportunity_contact/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2023 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "CRM Lead to opportunity - Contact",
"version": "14.0.1.0.0",
"category": "CRM",
"license": "AGPL-3",
"summary": "Lead to opportunity: option to create a contact on an existing "
"customer",
"author": "Akretion,Odoo Community Association (OCA)",
"maintainers": ["alexis-via"],
"website": "https://github.com/OCA/crm",
"depends": ["crm"],
"data": [
"wizards/crm_lead_to_opportunity_view.xml",
],
"installable": True,
}
1 change: 1 addition & 0 deletions crm_lead_to_opportunity_contact/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import crm_lead
31 changes: 31 additions & 0 deletions crm_lead_to_opportunity_contact/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, models
from odoo.exceptions import UserError


class CrmLead(models.Model):
_inherit = "crm.lead"

def _create_child_partner(self, parent_partner_id):
self.ensure_one()
assert parent_partner_id
rpo = self.env["res.partner"]
contact_name = self.contact_name

Check warning on line 16 in crm_lead_to_opportunity_contact/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/models/crm_lead.py#L13-L16

Added lines #L13 - L16 were not covered by tests
if not contact_name and self.email_from:
contact_name = rpo._parse_partner_name(self.email_from)[0]

Check warning on line 18 in crm_lead_to_opportunity_contact/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/models/crm_lead.py#L18

Added line #L18 was not covered by tests
if not contact_name:
raise UserError(

Check warning on line 20 in crm_lead_to_opportunity_contact/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/models/crm_lead.py#L20

Added line #L20 was not covered by tests
_("Contact name is not set on lead '%s'.") % self.display_name
)
vals = self.with_context(

Check warning on line 23 in crm_lead_to_opportunity_contact/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/models/crm_lead.py#L23

Added line #L23 was not covered by tests
default_user_id=self.user_id.id
)._prepare_customer_values(
contact_name,
is_company=False,
parent_id=parent_partner_id,
)
partner = rpo.create(vals)
return partner

Check warning on line 31 in crm_lead_to_opportunity_contact/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/models/crm_lead.py#L30-L31

Added lines #L30 - L31 were not covered by tests
1 change: 1 addition & 0 deletions crm_lead_to_opportunity_contact/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Alexis de Lattre <[email protected]>
10 changes: 10 additions & 0 deletions crm_lead_to_opportunity_contact/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
In the wizard to convert a lead to an opportunity, you have 3 options:

* Create a new customer
* Link to an existing customer
* Do not link to a customer

This modules adds a fourth option: **Create a new contact on an existing customer**.

.. figure:: ../static/description/crm_lead_to_opportunity_contact-sshot.png
:alt: Screenshot lead to opportunity wizard
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions crm_lead_to_opportunity_contact/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import crm_lead_to_opportunity
30 changes: 30 additions & 0 deletions crm_lead_to_opportunity_contact/wizards/crm_lead_to_opportunity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2023 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, fields, models
from odoo.exceptions import UserError


class CrmLead2OpportunityPartner(models.TransientModel):
_inherit = "crm.lead2opportunity.partner"

action = fields.Selection(
selection_add=[
("create_child", "Create a new contact on an existing customer")
],
ondelete={"create_child": "set null"},
)
# I don't re-use the native field 'partner_id' because I need a specific domain
create_child_partner_id = fields.Many2one(
"res.partner", string="Existing Customer", domain=[("parent_id", "=", False)]
)

def _convert_handle_partner(self, lead, action, partner_id):
if action == "create_child":
if not self.create_child_partner_id:
raise UserError(_("You must select an Existing Customer."))
partner = lead._create_child_partner(self.create_child_partner_id.id)
lead.write({"partner_id": partner.id})

Check warning on line 28 in crm_lead_to_opportunity_contact/wizards/crm_lead_to_opportunity.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/wizards/crm_lead_to_opportunity.py#L26-L28

Added lines #L26 - L28 were not covered by tests
else:
super()._convert_handle_partner(lead, action, partner_id)

Check warning on line 30 in crm_lead_to_opportunity_contact/wizards/crm_lead_to_opportunity.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_to_opportunity_contact/wizards/crm_lead_to_opportunity.py#L30

Added line #L30 was not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2023 Akretion France (http://www.akretion.com/)
@author: Alexis de Lattre <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>

<record id="view_crm_lead2opportunity_partner" model="ir.ui.view">
<field name="model">crm.lead2opportunity.partner</field>
<field name="inherit_id" ref="crm.view_crm_lead2opportunity_partner" />
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field
name="create_child_partner_id"
widget="res_partner_many2one"
context="{'res_partner_search_mode': 'customer', 'show_vat': True}"
attrs="{'required': [('action', '=', 'create_child')], 'invisible': [('action', '!=', 'create_child')]}"
/>
</field>
</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions setup/crm_lead_to_opportunity_contact/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit e04f0c8

Please sign in to comment.