Skip to content

Commit

Permalink
[MIG]crm_lead_product: Migration to 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillemCForgeFlow committed Mar 7, 2024
1 parent bdb5033 commit bc41bc1
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 75 deletions.
4 changes: 2 additions & 2 deletions crm_lead_product/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import models
from . import report
9 changes: 4 additions & 5 deletions crm_lead_product/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Copyright (C) 2017-19 ForgeFlow S.L. (https://www.forgeflow.com).
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
{
"name": "Lead Line Product",
"version": "13.0.1.0.1",
"version": "14.0.1.0.0",
"category": "Customer Relationship Management",
"license": "LGPL-3",
"summary": "Adds a lead line in the lead/opportunity model " "in odoo",
"summary": "Adds a lead line in the lead/opportunity model in odoo",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/crm",
"depends": ["crm", "product"],
Expand Down
17 changes: 17 additions & 0 deletions crm_lead_product/migrations/14.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
if openupgrade.column_exists(env.cr, "crm_lead_line", "expected_revenue"):
_field_rename_expected_revenue = {
"crm_lead_line": [("expected_revenue", "prorated_revenue")],
}
openupgrade.rename_fields(env.cr, _field_rename_expected_revenue)
if openupgrade.column_exists(env.cr, "crm_lead_line", "planned_revenue"):
_field_rename_planned_revenue = {
"crm_lead_line": [("planned_revenue", "expected_revenue")],
}
openupgrade.rename_fields(env.cr, _field_rename_planned_revenue)
4 changes: 2 additions & 2 deletions crm_lead_product/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import crm_lead_line
from . import crm_lead
21 changes: 8 additions & 13 deletions crm_lead_product/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2017-19 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models

Expand All @@ -13,20 +13,15 @@ class CrmLead(models.Model):

@api.onchange("lead_line_ids")
def _onchange_lead_line_ids(self):
planned_revenue = 0
expected_revenue = 0
for lead_line in self.lead_line_ids:
if lead_line.planned_revenue != 0:
planned_revenue += lead_line.planned_revenue
self.planned_revenue = planned_revenue
expected_revenue += lead_line.expected_revenue
self.expected_revenue = expected_revenue

def _convert_opportunity_data(self, customer, team_id=False):
res = super(CrmLead, self)._convert_opportunity_data(customer, team_id)

# Update planned_revenue
planned_revenue = 0
expected_revenue = 0
for lead_line in self.lead_line_ids:
planned_revenue += lead_line.planned_revenue

res["planned_revenue"] = planned_revenue

expected_revenue += lead_line.expected_revenue
res["expected_revenue"] = expected_revenue
return res
49 changes: 23 additions & 26 deletions crm_lead_product/models/crm_lead_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2017-19 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models

Expand All @@ -9,16 +9,16 @@ class CrmLeadLine(models.Model):
_description = "Line in CRM Lead"

@api.depends("price_unit", "product_qty")
def _compute_planned_revenue(self):
def _compute_expected_revenue(self):
for rec in self:
rec.planned_revenue = rec.product_qty * rec.price_unit
rec.expected_revenue = rec.product_qty * rec.price_unit

@api.depends("lead_id.probability", "planned_revenue")
def _compute_expected_revenue(self):
@api.depends("lead_id.probability", "expected_revenue")
def _compute_prorated_revenue(self):
for rec in self:
if rec.lead_id and rec.lead_id.type != "lead":
rec.expected_revenue = (
rec.planned_revenue * rec.lead_id.probability * (1 / 100)
rec.prorated_revenue = (
rec.expected_revenue * rec.lead_id.probability * (1 / 100)
)

lead_id = fields.Many2one("crm.lead", string="Lead")
Expand All @@ -32,16 +32,24 @@ def _compute_expected_revenue(self):
)
product_qty = fields.Integer(string="Product Quantity", default=1, required=True)
uom_id = fields.Many2one("uom.uom", string="Unit of Measure", readonly=True)
price_unit = fields.Float(string="Price Unit")
planned_revenue = fields.Float(
compute="_compute_planned_revenue",
string="Planned revenue",
compute_sudo=True,
store=True,
price_unit = fields.Float(string="Price Unit", digits="Product Price")
company_currency = fields.Many2one(
"res.currency",
string="Currency",
related="lead_id.company_currency",
readonly=True,
)
expected_revenue = fields.Float(
expected_revenue = fields.Monetary(
compute="_compute_expected_revenue",
string="Expected revenue",
currency_field="company_currency",
compute_sudo=True,
store=True,
)
prorated_revenue = fields.Monetary(
compute="_compute_prorated_revenue",
string="Prorated revenue",
currency_field="company_currency",
compute_sudo=True,
store=True,
)
Expand All @@ -51,7 +59,6 @@ def _onchange_product_id(self):
domain = {}
if not self.lead_id:
return

Check warning on line 61 in crm_lead_product/models/crm_lead_line.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_product/models/crm_lead_line.py#L61

Added line #L61 was not covered by tests

if not self.product_id:
self.price_unit = 0.0
domain["uom_id"] = []
Expand All @@ -62,22 +69,18 @@ def _onchange_product_id(self):
self.category_id = product.categ_id.id
self.product_tmpl_id = product.product_tmpl_id.id
self.price_unit = product.list_price

if product.name:
self.name = product.name

if (
not self.uom_id
or product.uom_id.category_id.id != self.uom_id.category_id.id
):
self.uom_id = product.uom_id.id
domain["uom_id"] = [("category_id", "=", product.uom_id.category_id.id)]

if self.uom_id and self.uom_id.id != product.uom_id.id:
self.price_unit = product.uom_id._compute_price(

Check warning on line 81 in crm_lead_product/models/crm_lead_line.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_product/models/crm_lead_line.py#L81

Added line #L81 was not covered by tests
self.price_unit, self.uom_id
)

return {"domain": domain}

@api.onchange("category_id")
Expand All @@ -89,15 +92,13 @@ def _onchange_category_id(self):
categ_id = self.category_id
if categ_id.name and not self.name:
self.name = categ_id.name

Check warning on line 94 in crm_lead_product/models/crm_lead_line.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_product/models/crm_lead_line.py#L94

Added line #L94 was not covered by tests

# Check if there are already defined product and product template
# and remove them if categories do not match
if self.product_id and self.product_id.categ_id != categ_id:
self.product_id = None
self.name = categ_id.name
if self.product_tmpl_id and self.product_tmpl_id.categ_id != categ_id:
self.product_tmpl_id = None

return {"domain": domain}

@api.onchange("product_tmpl_id")
Expand All @@ -110,26 +111,22 @@ def _onchange_product_tmpl_id(self):
if product_tmpl.name and not self.name:
self.name = product_tmpl.name

Check warning on line 112 in crm_lead_product/models/crm_lead_line.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_product/models/crm_lead_line.py#L112

Added line #L112 was not covered by tests
self.category_id = product_tmpl.categ_id

if self.product_id:
# Check if there are already defined product and remove
# if it does not match
if self.product_id.product_tmpl_id != product_tmpl:
self.product_id = None
self.name = product_tmpl.name

Check warning on line 119 in crm_lead_product/models/crm_lead_line.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_product/models/crm_lead_line.py#L118-L119

Added lines #L118 - L119 were not covered by tests

return {"domain": domain}

@api.onchange("uom_id")
def _onchange_uom_id(self):
result = {}
if not self.uom_id:
self.price_unit = 0.0

if self.product_id and self.uom_id:
price_unit = self.product_id.list_price
self.price_unit = self.product_id.uom_id._compute_price(

Check warning on line 129 in crm_lead_product/models/crm_lead_line.py

View check run for this annotation

Codecov / codecov/patch

crm_lead_product/models/crm_lead_line.py#L128-L129

Added lines #L128 - L129 were not covered by tests
price_unit, self.uom_id
)

return result
2 changes: 1 addition & 1 deletion crm_lead_product/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

* Adria Gil Sorribes <[email protected]>
* Guillem Casassas <[email protected]>
4 changes: 2 additions & 2 deletions crm_lead_product/report/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import crm_product_report
9 changes: 4 additions & 5 deletions crm_lead_product/report/crm_product_report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2017-19 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from psycopg2.extensions import AsIs

from odoo import fields, models, tools
Expand Down Expand Up @@ -38,7 +37,7 @@ class ActivityReport(models.Model):
user_id = fields.Many2one("res.users", "Salesperson", readonly=True)
category_id = fields.Many2one("product.category", "Category", readonly=True)
expected_revenue = fields.Float("Expected Revenue", readonly=True)
planned_revenue = fields.Float("Planned Revenue", readonly=True)
prorated_revenue = fields.Float("Prorated Revenue", readonly=True)
product_id = fields.Many2one("product.product", "Product", readonly=True)
product_qty = fields.Integer("Product Quantity", readonly=True)
product_tmpl_id = fields.Many2one(
Expand Down Expand Up @@ -70,7 +69,7 @@ def _select(self):
l.user_id,
ll.category_id,
ll.expected_revenue,
ll.planned_revenue,
ll.prorated_revenue,
ll.product_id,
ll.product_qty,
ll.product_tmpl_id
Expand Down
6 changes: 3 additions & 3 deletions crm_lead_product/report/crm_product_report_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<pivot string="Pipeline Analysis">
<field name="stage_id" type="col" />
<field name="product_id" type="row" />
<field name="planned_revenue" type="measure" />
<field name="expected_revenue" type="measure" />
</pivot>
</field>
</record>
Expand All @@ -21,7 +21,7 @@
<graph string="Pipeline Analysis" stacked="True">
<field name="product_id" type="row" />
<field name="stage_id" type="row" />
<field name="expected_revenue" type="measure" />
<field name="prorated_revenue" type="measure" />
</graph>
</field>
</record>
Expand All @@ -40,7 +40,7 @@
<field name="partner_name" />
<field name="country_id" />
<field name="stage_id" />
<field name="expected_revenue" />
<field name="prorated_revenue" />
<field name="team_id" />
</tree>
</field>
Expand Down
4 changes: 2 additions & 2 deletions crm_lead_product/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

# Copyright (C) 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import test_crm_lead_line
19 changes: 9 additions & 10 deletions crm_lead_product/tests/test_crm_lead_line.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Copyright 2017-19 ForgeFlow S.L. (https://www.forgeflow.com)
# Copyright 2017-2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo.tests import common
from odoo.tests.common import TransactionCase, tagged


@common.at_install(False)
@common.post_install(True)
class TestCrmLeadLine(common.TransactionCase):
@tagged("post_install", "-at_install")
class TestCrmLeadLine(TransactionCase):
def setUp(self):
super(TestCrmLeadLine, self).setUp()
self.product_obj = self.env["product.product"]
Expand Down Expand Up @@ -144,7 +143,7 @@ def test_02_lead_to_opportunity(self):

# Check if planned revenue is correctly set for lead line 1
self.assertEqual(
self.lead.lead_line_ids[0].planned_revenue,
self.lead.lead_line_ids[0].expected_revenue,
self.product_4.list_price,
"Planned revenue should be equal " "to the product standard price",
)
Expand All @@ -154,15 +153,15 @@ def test_02_lead_to_opportunity(self):
lead_line_1 = self.lead.lead_line_ids[0]

self.assertEqual(
lead_line_1.expected_revenue,
lead_line_1.planned_revenue * self.lead.probability * (1 / 100),
lead_line_1.prorated_revenue,
lead_line_1.expected_revenue * self.lead.probability * (1 / 100),
"Expected revenue should be planned " "revenue times the probability",
)

self.lead.write({"probability": 30})

self.assertEqual(
lead_line_1.expected_revenue,
round(lead_line_1.planned_revenue * self.lead.probability * (1 / 100), 5),
lead_line_1.prorated_revenue,
round(lead_line_1.expected_revenue * self.lead.probability * (1 / 100), 5),
"Expected revenue should be planned " "revenue times the probability",
)
6 changes: 3 additions & 3 deletions crm_lead_product/views/crm_lead_line_views.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<odoo>
<record id="view_tree_lead_line" model="ir.ui.view">
<field name="name">crm.lead.form.lead</field>
<field name="name">crm.lead.line.tree - crm_lead_product</field>
<field name="model">crm.lead.line</field>
<field name="arch" type="xml">
<tree string="Products" editable="bottom">
Expand All @@ -11,8 +11,8 @@
<field name="name" widget="section_and_note_text" />
<field name="product_qty" string="Quantity" />
<field name="price_unit" string="Price Unit" />
<field name="planned_revenue" string="Planned revenue" />
<field name="expected_revenue" string="Expected revenue" />
<field name="expected_revenue" />
<field name="prorated_revenue" />
</tree>
</field>
</record>
Expand Down
2 changes: 1 addition & 1 deletion crm_lead_product/views/crm_lead_views.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<odoo>
<record id="crm_lead_view_form" model="ir.ui.view">
<field name="name">crm.lead.form.lead</field>
<field name="name">crm.lead.form - crm_lead_product</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_lead_view_form" />
<field name="arch" type="xml">
Expand Down

0 comments on commit bc41bc1

Please sign in to comment.