[FIX] base: retrieve last rates only from current company

Steps to reproduce:

  - Install accounting module
  - Ensure there is 2 companies
  - Activate both companies (with company switcher)
  - Go to Accounting -> Configuration -> Currencies
  - Select currency `EUR`
  - Add a rate and set second company as company
  - Disable second company (with company switcher)
  - Click on `Show Currency Rates` in action menu
  - Click on create

Issue:

  Access Error

Cause:

  Currency Rate model have a compute field `rate`.
  This last one calls `_get_latest_rate` that will retrieve the last
  rate for the currency. To do so, it will first retrieve all rates for
  the currency (that might include rates with company not same as the
  current one) before fitering regarding company.

Solution:

  Use sudo(). Same issue/fix for `_get_last_rates_for_companies`.

opw-2832708

closes odoo/odoo#96714

X-original-commit: dda5c9084b9913968e67c8b280de870c81227e16
Signed-off-by: Nasreddin Boulif (bon) <bon@odoo.com>
This commit is contained in:
Nasreddin Boulif (bon)
2022-07-27 21:21:40 +02:00
parent a5568b4622
commit 00fe2a4ba2
+2 -2
View File
@@ -349,7 +349,7 @@ class CurrencyRate(models.Model):
return super().create([self._sanitize_vals(vals) for vals in vals_list])
def _get_latest_rate(self):
return self.currency_id.rate_ids.filtered(lambda x: (
return self.currency_id.rate_ids.sudo().filtered(lambda x: (
x.rate
and x.company_id == (self.company_id or self.env.company)
and x.name < (self.name or fields.Date.today())
@@ -357,7 +357,7 @@ class CurrencyRate(models.Model):
def _get_last_rates_for_companies(self, companies):
return {
company: company.currency_id.rate_ids.filtered(lambda x: (
company: company.currency_id.rate_ids.sudo().filtered(lambda x: (
x.rate
and x.company_id == company or not x.company_id
)).sorted('name')[-1:].rate or 1