[FIX] web: monetary formatter in readonly ignores currency_field in field definition

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) <ges@odoo.com>
Co-authored-by: Habib (ayh) <ayh@odoo.com>
Co-authored-by: John Laterre (jol) <jol@odoo.com>
This commit is contained in:
Habib (ayh)
2022-10-03 21:23:03 +02:00
committed by John Laterre (jol)
co-authored by John Laterre
parent 3ae786465e
commit 805d2c0820
2 changed files with 39 additions and 1 deletions
@@ -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;
}
@@ -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: `
<tree>
<field name="company_currency_id" invisible="1"/>
<field name="currency_id" invisible="1"/>
<field name="amount"/>
<field name="amount_currency"/>
</tree>`,
});
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) {