Skip to content

Commit

Permalink
[IMP] Always format monetary values with currency and digits.
Browse files Browse the repository at this point in the history
  • Loading branch information
amh-mw committed Jun 10, 2024
1 parent 4e53ef2 commit bfcef5d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
19 changes: 11 additions & 8 deletions addons/web/static/src/fields/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ export function formatFloat(value, options = {}) {
} else {
precision = 2;
}
const formatted = (value || 0).toFixed(precision || 2).split(".");
formatted[0] = insertThousandsSep(formatted[0], thousandsSep, grouping);
if (options.noTrailingZeros) {
formatted[1] = formatted[1].replace(/0+$/, "");
let [characteristic, mantissa] = (value || 0).toFixed(precision).split(".");
characteristic = insertThousandsSep(characteristic, thousandsSep, grouping);
if (precision && options.noTrailingZeros) {
mantissa = mantissa.replace(/0+$/, "");
}
return formatted[1].length ? formatted.join(decimalPoint) : formatted[0];
return (mantissa && mantissa.length) ? [characteristic, mantissa].join(decimalPoint) : characteristic;
}

/**
Expand Down Expand Up @@ -284,8 +284,11 @@ export function formatMonetary(value, options = {}) {
"currency_id";
currencyId = options.data[currencyField] && options.data[currencyField].res_id;
}
if (!currencyId) {
currencyId = Object.keys(session.currencies)[0];
}
const currency = session.currencies[currencyId];
const digits = (currency && currency.digits) || options.digits;
const digits = options.digits || (currency && currency.digits);

let formatted;
if (options.humanReadable) {
Expand All @@ -294,13 +297,13 @@ export function formatMonetary(value, options = {}) {
formatted = formatFloat(value, { digits });
}

if (!currency || options.noSymbol) {
if (!currency || !formatted || options.noSymbol) {
return formatted;
}
if (currency.position === "after") {
return `${formatted} ${currency.symbol}`;
} else {
return `${currency.symbol} ${formatted}`;
return `${currency.symbol}${formatted}`;
}
}

Expand Down
7 changes: 6 additions & 1 deletion addons/web/static/src/legacy/js/fields/field_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ function formatMonetary(value, field, options) {
var currency_field = options.currency_field || field.currency_field || 'currency_id';
currency_id = options.data[currency_field] && options.data[currency_field].res_id;
}
if (!currency_id) {
if (Object.keys(session.currencies).length === 1) {
currency_id = Object.keys(session.currencies)[0];
}
}
currency = session.get_currency(currency_id);
}

Expand All @@ -373,7 +378,7 @@ function formatMonetary(value, field, options) {
if (options.forceString) {
return val.join(' ');
}
return utils.Markup(val.map((v) => _.escape(v)).join(NBSP));
return utils.Markup(val.map((v) => _.escape(v)).join(''));
}
/**
* Returns a string representing the given value (multiplied by 100)
Expand Down
6 changes: 4 additions & 2 deletions addons/web/static/src/legacy/js/views/list/list_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import core from 'web.core';
import dom from 'web.dom';
import field_utils from 'web.field_utils';
import Pager from 'web.Pager';
import pyUtils from 'web.py_utils';
import utils from 'web.utils';
import viewUtils from 'web.viewUtils';

Expand Down Expand Up @@ -473,10 +474,11 @@ var ListRenderer = BasicRenderer.extend({
if (!formatFunc) {
formatFunc = field_utils.format[field.type];
}
var formattedValue = formatFunc(value, field, {
var options = column.attrs.options ? pyUtils.py_eval(column.attrs.options) : {};
var formattedValue = formatFunc(value, field, _.extend(options, {
escape: true,
digits: column.attrs.digits ? JSON.parse(column.attrs.digits) : undefined,
});
}));
$cell.addClass('o_list_number').attr('title', help).html(formattedValue);
}
return $cell;
Expand Down

0 comments on commit bfcef5d

Please sign in to comment.