From 98dcc090ca132c695591284960abd14b6a1b2eaa Mon Sep 17 00:00:00 2001 From: Quoc - Pham Ngoc Date: Fri, 10 Apr 2026 16:31:30 +0700 Subject: [PATCH] [IMP] crm_lead_currency: Set customer currency default value for existing records --- crm_lead_currency/__init__.py | 1 + crm_lead_currency/__manifest__.py | 1 + crm_lead_currency/hooks.py | 21 ++++++++++ .../tests/test_crm_opportunity_currency.py | 39 ++++++++++++++++++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 crm_lead_currency/hooks.py diff --git a/crm_lead_currency/__init__.py b/crm_lead_currency/__init__.py index 0650744f6bc..cc6b6354ad8 100644 --- a/crm_lead_currency/__init__.py +++ b/crm_lead_currency/__init__.py @@ -1 +1,2 @@ from . import models +from .hooks import post_init_hook diff --git a/crm_lead_currency/__manifest__.py b/crm_lead_currency/__manifest__.py index 4e83625ba94..f98778ef7bb 100644 --- a/crm_lead_currency/__manifest__.py +++ b/crm_lead_currency/__manifest__.py @@ -18,5 +18,6 @@ "data": [ "views/crm_lead_views.xml", ], + "post_init_hook": "post_init_hook", "installable": True, } diff --git a/crm_lead_currency/hooks.py b/crm_lead_currency/hooks.py new file mode 100644 index 00000000000..5887f3742aa --- /dev/null +++ b/crm_lead_currency/hooks.py @@ -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), + ) diff --git a/crm_lead_currency/tests/test_crm_opportunity_currency.py b/crm_lead_currency/tests/test_crm_opportunity_currency.py index 5809c999b68..b4f42949ab9 100644 --- a/crm_lead_currency/tests/test_crm_opportunity_currency.py +++ b/crm_lead_currency/tests/test_crm_opportunity_currency.py @@ -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): @@ -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)