Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crm_lead_currency/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from .hooks import post_init_hook
1 change: 1 addition & 0 deletions crm_lead_currency/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"data": [
"views/crm_lead_views.xml",
],
"post_init_hook": "post_init_hook",
"installable": True,
}
21 changes: 21 additions & 0 deletions crm_lead_currency/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def post_init_hook(env):
"""Align existing leads with their company currency during installation."""
env.cr.execute(
"""
UPDATE crm_lead AS lead
SET customer_currency_id = COALESCE(company.currency_id, %s)
FROM res_company AS company
WHERE company.id = lead.company_id
AND lead.customer_currency_id IS DISTINCT FROM company.currency_id
""",
(env.company.currency_id.id,),
)
env.cr.execute(
"""
UPDATE crm_lead
SET customer_currency_id = %s
WHERE company_id IS NULL
AND customer_currency_id IS DISTINCT FROM %s
""",
(env.company.currency_id.id, env.company.currency_id.id),
)
39 changes: 37 additions & 2 deletions crm_lead_currency/tests/test_crm_opportunity_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@

from odoo.tests import TransactionCase

from ..hooks import post_init_hook


class TestCrmOpportunityCurrency(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.lead = cls.env["crm.lead"].create({"name": "test lead"})
cls.foreign_currency = cls.env["res.currency"].search(
[("id", "!=", cls.lead.company_currency.id)],
limit=1,
)
cls.other_company = cls.env["res.company"].create(
{
"name": "Foreign Currency Company",
"currency_id": cls.foreign_currency.id,
}
)

def test_is_same_currency(self):
self.lead.customer_currency_id = self.lead.company_currency
self.assertTrue(self.lead.is_same_currency)
self.lead.customer_currency_id = self.env.ref("base.CHF")
self.lead.customer_currency_id = self.foreign_currency
self.assertFalse(self.lead.is_same_currency)

def test_same_currency_expected_revenue_not_updated(self):
Expand All @@ -25,7 +37,30 @@ def test_same_currency_expected_revenue_not_updated(self):

def test_different_currency_expected_revenue_updated(self):
self.lead.expected_revenue = 100
self.lead.customer_currency_id = self.env.ref("base.CHF")
self.lead.customer_currency_id = self.foreign_currency
self.lead.amount_customer_currency = 124
self.lead._onchange_currency()
self.assertNotEqual(self.lead.expected_revenue, 100)

def test_post_init_hook_sets_missing_customer_currency(self):
lead = self.env["crm.lead"].create({"name": "lead without customer currency"})
lead.customer_currency_id = False

post_init_hook(self.env)

lead.invalidate_recordset(["customer_currency_id"])
self.assertEqual(lead.customer_currency_id, lead.company_currency)

def test_post_init_hook_replaces_install_default_with_company_currency(self):
lead = self.env["crm.lead"].create(
{
"name": "lead with install default currency",
"company_id": self.other_company.id,
}
)
lead.customer_currency_id = self.env.company.currency_id

post_init_hook(self.env)

lead.invalidate_recordset(["customer_currency_id"])
self.assertEqual(lead.customer_currency_id, lead.company_currency)