Skip to content

Commit c7aaeb9

Browse files
committed
[MIG] crm_multicompany_reporting_currency: Migration to 19.0
1 parent d39f644 commit c7aaeb9

File tree

7 files changed

+348
-151
lines changed

7 files changed

+348
-151
lines changed

crm_multicompany_reporting_currency/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Credits
6060
Authors
6161
-------
6262

63-
* Camptocamp SA
63+
* Camptocamp
6464

6565
Contributors
6666
------------

crm_multicompany_reporting_currency/__manifest__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
{
44
"name": "CRM Multicompany Reporting Currency",
55
"summary": "Adds Amount in multicompany reporting currency to CRM Lead",
6-
"version": "15.0.1.0.3",
6+
"version": "19.0.1.0.0",
77
"category": "Sales",
8-
"author": "Camptocamp SA, Odoo Community Association (OCA)",
8+
"author": "Camptocamp, Odoo Community Association (OCA)",
99
"license": "AGPL-3",
10-
"depends": ["crm", "base_multicompany_reporting_currency"],
10+
"depends": [
11+
# Odoo
12+
"crm",
13+
# OCA/sale-reporting
14+
"base_multicompany_reporting_currency",
15+
],
1116
"website": "https://github.com/OCA/crm",
12-
"data": ["views/crm_lead_views.xml"],
17+
"data": [
18+
# Views
19+
"views/crm_lead.xml"
20+
],
1321
"installable": True,
1422
"maintainers": ["yankinmax"],
1523
}
Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,80 @@
11
# Copyright 2022 Camptocamp SA
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
33

4+
from logging import getLogger
5+
46
from odoo import api, fields, models
57

8+
_logger = getLogger(__name__)
9+
610

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

10-
def _get_multicompany_reporting_currency_id(self):
11-
multicompany_reporting_currency_parameter = (
12-
self.env["ir.config_parameter"]
13-
.sudo()
14-
.get_param(
15-
"base_multicompany_reporting_currency.multicompany_reporting_currency"
16-
)
17-
)
18-
return self.env["res.currency"].browse(
19-
int(multicompany_reporting_currency_parameter)
20-
)
21-
2214
multicompany_reporting_currency_id = fields.Many2one(
23-
"res.currency",
24-
compute="_compute_multicompany_reporting_currency_id",
25-
readonly=True,
15+
comodel_name="res.currency",
16+
compute="_compute_multicompany_reporting_currency",
2617
store=True,
27-
default=_get_multicompany_reporting_currency_id,
28-
)
29-
currency_rate = fields.Float(
30-
compute="_compute_currency_rate", store=True, digits=(12, 6)
18+
compute_sudo=True,
3119
)
3220
amount_multicompany_reporting_currency = fields.Monetary(
3321
currency_field="multicompany_reporting_currency_id",
3422
compute="_compute_amount_multicompany_reporting_currency",
3523
store=True,
36-
index=True,
37-
readonly=True,
3824
)
3925

40-
@api.depends("company_currency")
41-
def _compute_multicompany_reporting_currency_id(self):
42-
multicompany_reporting_currency_id = (
43-
self._get_multicompany_reporting_currency_id()
44-
)
45-
for record in self:
46-
record.multicompany_reporting_currency_id = (
47-
multicompany_reporting_currency_id
26+
@api.model
27+
def _get_multicompany_reporting_currency(self) -> models.Model:
28+
currency = self.env["res.currency"]
29+
30+
# We try to retrieve the multicompany reporting currency from the system params,
31+
# but ``get_param(key)`` will return either ``None`` or a ``str`` object; since
32+
# we cannot be 100% sure we'll be able to convert it to a ``res.currency``
33+
# record ID, we use the user's environmental company currency as fallback
34+
IrConfigParam = self.env["ir.config_parameter"].sudo()
35+
key = "base_multicompany_reporting_currency.multicompany_reporting_currency"
36+
if value := IrConfigParam.get_param(key, default=""):
37+
try:
38+
currency = currency.browse(int(value)).exists()
39+
except ValueError: # pylint: disable=except-pass
40+
pass
41+
if not currency:
42+
currency = self.env.company.currency_id
43+
_logger.warning(
44+
"Could not get multicompany reporting currency from system"
45+
f" parameters, using user's company currency '{currency.name}'"
4846
)
47+
return currency
4948

50-
@api.depends("create_date", "company_id", "multicompany_reporting_currency_id")
51-
def _compute_currency_rate(self):
52-
# similar to currency_rate on sale.order
53-
for record in self:
54-
date = record.create_date or fields.Date.today()
55-
if not record.company_id:
56-
record.currency_rate = (
57-
record.multicompany_reporting_currency_id.with_context(
58-
date=date
59-
).rate
60-
or 1.0
61-
)
62-
elif (
63-
record.company_currency and record.multicompany_reporting_currency_id
64-
): # the following crashes if any one is undefined
65-
record.currency_rate = self.env["res.currency"]._get_conversion_rate(
66-
record.company_currency,
67-
record.multicompany_reporting_currency_id,
68-
record.company_id,
69-
date,
70-
)
71-
else:
72-
record.currency_rate = 1.0
49+
# Dummy dependencies to trigger the recomputation
50+
@api.depends("create_date", "company_id")
51+
def _compute_multicompany_reporting_currency(self):
52+
mcr_currency = self._get_multicompany_reporting_currency()
53+
self.multicompany_reporting_currency_id = mcr_currency
7354

7455
@api.depends(
75-
"expected_revenue", "currency_rate", "multicompany_reporting_currency_id"
56+
# NB: we need to convert ``expected_revenue`` from ``company_currency`` to
57+
# ``multicompany_reporting_currency_id``; however, ``company_currency`` is not
58+
# stored, so we cannot depend on it directly, thus we need to add ``company_id``
59+
# which is also a dependency of ``company_currency``
60+
"company_id",
61+
"create_date",
62+
"expected_revenue",
63+
"multicompany_reporting_currency_id",
7664
)
7765
def _compute_amount_multicompany_reporting_currency(self):
78-
for record in self:
79-
if record.company_currency == record.multicompany_reporting_currency_id:
80-
to_amount = record.expected_revenue
81-
else:
82-
to_amount = record.expected_revenue * record.currency_rate
83-
record.amount_multicompany_reporting_currency = to_amount
66+
for lead in self:
67+
amount = lead.expected_revenue
68+
from_curr = lead.company_currency
69+
to_curr = lead.multicompany_reporting_currency_id
70+
if from_curr != to_curr:
71+
amount = from_curr._convert(
72+
from_amount=amount,
73+
to_currency=to_curr,
74+
# Let ``res.currency._convert()`` handle company and date if empty
75+
company=lead.company_id or None,
76+
date=lead.create_date or None,
77+
# Leave roundings to field's ``convert_to_[cache|column_insert]()``
78+
round=False,
79+
)
80+
lead.amount_multicompany_reporting_currency = amount

crm_multicompany_reporting_currency/models/res_config_settings.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
33

44
from odoo import models
5+
from odoo.orm.domains import Domain
6+
from odoo.tools.misc import split_every
57

68

79
class ResConfigSettings(models.TransientModel):
810
_inherit = "res.config.settings"
911

1012
def set_values(self):
11-
crm_lead = self.env["crm.lead"]
12-
applied_currency = crm_lead._get_multicompany_reporting_currency_id()
13-
super().set_values()
14-
to_apply_currency = self.multicompany_reporting_currency
15-
if applied_currency.id != to_apply_currency.id:
16-
crm_lead.with_context(active_test=False).search([]).write(
17-
{"multicompany_reporting_currency_id": to_apply_currency.id}
18-
)
19-
return True
13+
# OVERRIDE: when the multicompany reporting currency changes, force-recompute
14+
# the multicompany reporting currency on all ``crm.lead`` records
15+
# We do this by retrieving the
16+
crm_lead = self.env["crm.lead"].sudo()
17+
old_lead_mcr_currency = crm_lead._get_multicompany_reporting_currency()
18+
res = super().set_values()
19+
if old_lead_mcr_currency != self.multicompany_reporting_currency:
20+
# Update in batches and clean the record cache at each iteration to prevent
21+
# ``MemoryError`` when running on DBs with lots of leads
22+
for leads in split_every(
23+
100,
24+
# pylint: disable=no-search-all
25+
crm_lead.search(Domain.TRUE).ids,
26+
crm_lead.browse,
27+
):
28+
leads._compute_multicompany_reporting_currency()
29+
leads.invalidate_recordset()
30+
return res

crm_multicompany_reporting_currency/static/description/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ <h2><a class="toc-backref" href="#toc-entry-2">Credits</a></h2>
406406
<div class="section" id="authors">
407407
<h3><a class="toc-backref" href="#toc-entry-3">Authors</a></h3>
408408
<ul class="simple">
409-
<li>Camptocamp SA</li>
409+
<li>Camptocamp</li>
410410
</ul>
411411
</div>
412412
<div class="section" id="contributors">

0 commit comments

Comments
 (0)