|
1 | 1 | # Copyright 2022 Camptocamp SA |
2 | 2 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) |
3 | 3 |
|
| 4 | +from logging import getLogger |
| 5 | + |
4 | 6 | from odoo import api, fields, models |
5 | 7 |
|
| 8 | +_logger = getLogger(__name__) |
| 9 | + |
6 | 10 |
|
7 | 11 | class CrmLead(models.Model): |
8 | 12 | _inherit = "crm.lead" |
9 | 13 |
|
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 | | - |
22 | 14 | 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", |
26 | 17 | 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, |
31 | 19 | ) |
32 | 20 | amount_multicompany_reporting_currency = fields.Monetary( |
33 | 21 | currency_field="multicompany_reporting_currency_id", |
34 | 22 | compute="_compute_amount_multicompany_reporting_currency", |
35 | 23 | store=True, |
36 | | - index=True, |
37 | | - readonly=True, |
38 | 24 | ) |
39 | 25 |
|
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}'" |
48 | 46 | ) |
| 47 | + return currency |
49 | 48 |
|
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 |
73 | 54 |
|
74 | 55 | @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", |
76 | 64 | ) |
77 | 65 | 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 |
0 commit comments