[FIX] base: fixed inverse currency rates in list view
In the currencies list view, the current rate and inverse rate were swapped. Their strings were also changed to match the string in the form view. Currency rates were inversed in the currencies list view. task-3856386 closes odoo/odoo#160882 Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
@@ -50,33 +50,33 @@ class TestCurrencyRates(TransactionCase):
|
||||
)
|
||||
|
||||
def test_currency_without_date(self):
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet("USD", "EUR"),
|
||||
CURRENT_EUR / CURRENT_USD,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet("EUR", "USD"),
|
||||
CURRENT_USD,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet("USD", "CAD"),
|
||||
CURRENT_CAD / CURRENT_USD,
|
||||
)
|
||||
|
||||
def test_currency_with_date(self):
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet(
|
||||
"USD", "EUR", "2021-11-11"
|
||||
),
|
||||
CURRENT_EUR / USD_11,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet(
|
||||
"EUR", "USD", "2021-11-11"
|
||||
),
|
||||
USD_11,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet(
|
||||
"USD", "CAD", "2021-11-11"
|
||||
),
|
||||
@@ -124,11 +124,11 @@ class TestCurrencyRates(TransactionCase):
|
||||
"rate": CAD_AUS,
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]._get_rate_for_spreadsheet("CAD", "EUR"),
|
||||
CURRENT_EUR / CAD_AUS,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertAlmostEqual(
|
||||
self.env["res.currency.rate"]
|
||||
.with_context(tz="UTC")
|
||||
._get_rate_for_spreadsheet("CAD", "EUR"),
|
||||
|
||||
@@ -148,7 +148,7 @@ class Currency(models.Model):
|
||||
# the subquery selects the last rate before 'date' for the given currency/company
|
||||
currency_rates = (self + to_currency)._get_rates(self.env.company, date)
|
||||
for currency in self:
|
||||
currency.rate = currency_rates.get(to_currency.id) / currency_rates.get(currency.id)
|
||||
currency.rate = currency_rates.get(currency.id) / currency_rates.get(to_currency.id)
|
||||
currency.inverse_rate = 1 / currency.rate
|
||||
if currency != company.currency_id:
|
||||
currency.rate_string = '1 %s = %.6f %s' % (to_currency.name, currency.rate, currency.name)
|
||||
@@ -261,7 +261,7 @@ class Currency(models.Model):
|
||||
return 1
|
||||
company = company or self.env.company
|
||||
date = date or fields.Date.context_today(self)
|
||||
return from_currency.with_company(company).with_context(to_currency=to_currency.id, date=str(date)).rate
|
||||
return from_currency.with_company(company).with_context(to_currency=to_currency.id, date=str(date)).inverse_rate
|
||||
|
||||
def _convert(self, from_amount, to_currency, company=None, date=None, round=True): # noqa: A002 builtin-argument-shadowing
|
||||
"""Returns the converted amount of ``from_amount``` from the currency
|
||||
@@ -313,11 +313,15 @@ class Currency(models.Model):
|
||||
arch, view = super()._get_view(view_id, view_type, **options)
|
||||
if view_type in ('tree', 'form'):
|
||||
currency_name = (self.env['res.company'].browse(self._context.get('company_id')) or self.env.company.root_id).currency_id.name
|
||||
for field in [['company_rate', _('Unit per %s', currency_name)],
|
||||
['inverse_company_rate', _('%s per Unit', currency_name)]]:
|
||||
node = arch.xpath("//tree//field[@name='%s']" % field[0])
|
||||
fields_maps = [
|
||||
[['company_rate', 'rate'], _('Unit per %s', currency_name)],
|
||||
[['inverse_company_rate', 'inverse_rate'], _('%s per Unit', currency_name)],
|
||||
]
|
||||
for fnames, label in fields_maps:
|
||||
xpath_expression = '//tree//field[' + " or ".join(f"@name='{f}'" for f in fnames) + "][1]"
|
||||
node = arch.xpath(xpath_expression)
|
||||
if node:
|
||||
node[0].set('string', field[1])
|
||||
node[0].set('string', label)
|
||||
return arch, view
|
||||
|
||||
|
||||
@@ -366,12 +370,12 @@ class CurrencyRate(models.Model):
|
||||
return vals
|
||||
|
||||
def write(self, vals):
|
||||
self.env['res.currency'].invalidate_model(['rate'])
|
||||
self.env['res.currency'].invalidate_model(['inverse_rate'])
|
||||
return super().write(self._sanitize_vals(vals))
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
self.env['res.currency'].invalidate_model(['rate'])
|
||||
self.env['res.currency'].invalidate_model(['inverse_rate'])
|
||||
return super().create([self._sanitize_vals(vals) for vals in vals_list])
|
||||
|
||||
def _get_latest_rate(self):
|
||||
|
||||
Reference in New Issue
Block a user