From 805d2c08202a6bfed6a1e582740b63becf5ddf04 Mon Sep 17 00:00:00 2001 From: "Habib (ayh)" Date: Thu, 22 Sep 2022 11:59:31 +0000 Subject: [PATCH] [FIX] web: monetary formatter in readonly ignores currency_field in field definition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce: Select a company whose currency is USD. Create an invoice in EUR. Go to the "Journal Items" tab. The amounts in the "Debit" and "Credit" columns are correct, but the currency symbol is € (invoice currency) instead of $ (company currency). This should not happen since both of these fields have `currency_field='company_currency_id'` in their definition: https://github.com/odoo/odoo/blob/702adc878e897461c4c9976110cee84d397ff4af/addons/account/models/account_move_line.py#L90-L99 But since those fields fulfill the conditions in `canUseFormatter`: https://github.com/odoo/odoo/blob/702adc878e897461c4c9976110cee84d397ff4af/addons/web/static/src/views/list/list_renderer.xml#L228-L229 `currencyField` remains undifined in the `formatOptions` when going through `getFormattedValue`: https://github.com/odoo/odoo/blob/702adc878e897461c4c9976110cee84d397ff4af/addons/web/static/src/views/list/list_renderer.js#L722-L734 Which is why the current fix looks at `options.field.currency_field` before falling back to `currency_id` when setting `currencyField`. A test is also added to make sure the issue does not creep back in again. closes odoo/odoo#101995 X-original-commit: a860370b8ed73c192c652b4a97ac0aa1d1c23936 Signed-off-by: Simon Genin (ges@odoo) Co-authored-by: Habib (ayh) Co-authored-by: John Laterre (jol) --- .../web/static/src/views/fields/formatters.js | 5 ++- .../web/static/tests/views/list_view_tests.js | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/views/fields/formatters.js b/addons/web/static/src/views/fields/formatters.js index d84fcc9c9c9..033959e4b7e 100644 --- a/addons/web/static/src/views/fields/formatters.js +++ b/addons/web/static/src/views/fields/formatters.js @@ -346,7 +346,10 @@ export function formatMonetary(value, options = {}) { let currencyId = options.currencyId; if (!currencyId && options.data) { - const currencyField = options.currencyField || "currency_id"; + const currencyField = + options.currencyField || + (options.field && options.field.currency_field) || + "currency_id"; const dataValue = options.data[currencyField]; currencyId = Array.isArray(dataValue) ? dataValue[0] : dataValue; } diff --git a/addons/web/static/tests/views/list_view_tests.js b/addons/web/static/tests/views/list_view_tests.js index d16544adcf6..407c647168b 100644 --- a/addons/web/static/tests/views/list_view_tests.js +++ b/addons/web/static/tests/views/list_view_tests.js @@ -101,12 +101,19 @@ QUnit.module("Views", (hooks) => { o2m: { string: "O2M field", type: "one2many", relation: "bar" }, m2m: { string: "M2M field", type: "many2many", relation: "bar" }, amount: { string: "Monetary field", type: "monetary" }, + amount_currency: { string: "Monetary field (currency)", type: "monetary", currency_field: "company_currency_id" }, currency_id: { string: "Currency", type: "many2one", relation: "res_currency", default: 1, }, + company_currency_id: { + string: "Company Currency", + type: "many2one", + relation: "res_currency", + default: 2, + }, datetime: { string: "Datetime Field", type: "datetime" }, reference: { string: "Reference Field", @@ -128,7 +135,9 @@ QUnit.module("Views", (hooks) => { m2o: 1, m2m: [1, 2], amount: 1200, + amount_currency: 1100, currency_id: 2, + company_currency_id: 1, date: "2017-01-25", datetime: "2016-12-12 10:55:05", reference: "bar,1", @@ -2974,6 +2983,32 @@ QUnit.module("Views", (hooks) => { ); }); + QUnit.test("currency_field is taken into account when formatting monetary values", async (assert) => { + await makeView({ + type: "list", + resModel: "foo", + serverData, + arch: ` + + + + + + `, + }); + + assert.strictEqual( + target.querySelectorAll('.o_data_row td[name="amount"]')[0].textContent, + "1200.00\u00a0€", + "field should be formatted based on currency_id" + ); + assert.strictEqual( + target.querySelectorAll('.o_data_row td[name="amount_currency"]')[0].textContent, + "$\u00a01100.00", + "field should be formatted based on company_currency_id" + ); + }); + QUnit.test( "groups can not be sorted on a different field than the first field of the groupBy - 1", async function (assert) {