[FIX] documents_spreadsheet: localize date pivot values
Steps to reproduce the issue
-Go to Document and create a spreadsheet document SD (for example Sales Commission)
-Display SD and notice that there is some data in it
-Add lang Czech and set it on your profile
-Display SD
Bug:
There is no data in SD
The issue arise because `read_group` values are already translated to
the user's language.
e.g. let's we group by `create_date:month`, in Czech, we get
`"create_date:month": "září 2022"` (september 2022).
The previous strategy to map the `read_group` result to the PIVOT functions[1]
was to let momentJS parse the result, then format it to the PIVOT function format.
However, we can't realistically assume momentJS can parse any date in any language.
About the adapted tests:
The tests were actually wrong because the mock server is also wrong.
The mock implementation of `read_group` returns days formatted as "2022-09-08" ("yyyy-MM-dd")
instead of "08 Sept 2022". Because the business code didn't expect this format,
it resulted with wrong values.
See odoo/odoo#105546
[1] PIVOT function argument is "09/2022" for september 2022
opw-3052858
closes odoo/odoo#109026
X-original-commit: efb59010e3029ff4d5c77c0b2402093c8df4c44e
Related: odoo/enterprise#35395
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
This commit is contained in:
@@ -5,11 +5,11 @@ import { _lt } from "@web/core/l10n/translation";
|
||||
export const DEFAULT_LINES_NUMBER = 20;
|
||||
|
||||
export const FORMATS = {
|
||||
day: { in: "DD MMM YYYY", out: "MM/DD/YYYY", display: "DD MMM YYYY", interval: "d" },
|
||||
week: { in: "[W]W YYYY", out: "WW/YYYY", display: "[W]W YYYY", interval: "w" },
|
||||
month: { in: "MMMM YYYY", out: "MM/YYYY", display: "MMMM YYYY", interval: "M" },
|
||||
quarter: { in: "Q YYYY", out: "Q/YYYY", display: "[Q]Q YYYY", interval: "Q" },
|
||||
year: { in: "YYYY", out: "YYYY", display: "YYYY", interval: "y" },
|
||||
day: { out: "MM/DD/YYYY", display: "DD MMM YYYY", interval: "d" },
|
||||
week: { out: "WW/YYYY", display: "[W]W YYYY", interval: "w" },
|
||||
month: { out: "MM/YYYY", display: "MMMM YYYY", interval: "M" },
|
||||
quarter: { out: "Q/YYYY", display: "[Q]Q YYYY", interval: "Q" },
|
||||
year: { out: "YYYY", display: "YYYY", interval: "y" },
|
||||
};
|
||||
|
||||
export const HEADER_STYLE = { fillColor: "#f2f2f2" };
|
||||
|
||||
@@ -448,23 +448,38 @@ export class SpreadsheetPivotModel extends PivotModel {
|
||||
*/
|
||||
_getGroupValues(group, groupBys) {
|
||||
return groupBys.map((groupBy) => {
|
||||
return this._sanitizeValue(group[groupBy], groupBy);
|
||||
const { field, aggregateOperator } = this.parseGroupField(groupBy);
|
||||
if (this._isDateField(field)) {
|
||||
const value = this._getGroupStartingDay(groupBy, group);
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
const fOut = FORMATS[aggregateOperator]["out"];
|
||||
// eslint-disable-next-line no-undef
|
||||
const date = moment(value);
|
||||
return date.isValid() ? date.format(fOut) : false;
|
||||
}
|
||||
return this._sanitizeValue(group[groupBy]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
* When grouping by a time field, return
|
||||
* the group starting day (local to the timezone)
|
||||
* @param {string} groupBy
|
||||
* @param {object} readGroup
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
_sanitizeValue(value, groupBy) {
|
||||
const { aggregateOperator, field } = this.parseGroupField(groupBy);
|
||||
if (this._isDateField(field)) {
|
||||
const fIn = FORMATS[aggregateOperator]["in"];
|
||||
const fOut = FORMATS[aggregateOperator]["out"];
|
||||
// eslint-disable-next-line no-undef
|
||||
const date = moment(value, fIn);
|
||||
return date.isValid() ? date.format(fOut) : false;
|
||||
_getGroupStartingDay(groupBy, readGroup) {
|
||||
if (!readGroup["__range"] || !readGroup["__range"][groupBy]) {
|
||||
return undefined;
|
||||
}
|
||||
return super._sanitizeValue(value);
|
||||
const { field } = this.parseGroupField(groupBy);
|
||||
const sqlValue = readGroup["__range"][groupBy].from;
|
||||
if (this.metaData.fields[field.name].type === "date") {
|
||||
return sqlValue;
|
||||
}
|
||||
return luxon.DateTime.fromSQL(sqlValue, { zone: "utc" }).toLocal().toISODate();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -338,6 +338,6 @@ QUnit.module("spreadsheet > positional pivot formula", {}, () => {
|
||||
</pivot>`,
|
||||
});
|
||||
setCellContent(model, "A1", `=ODOO.PIVOT.HEADER(1,"#date:day",1)`);
|
||||
assert.strictEqual(getEvaluatedCell(model, "A1").formattedValue, "01/20/2016");
|
||||
assert.strictEqual(getEvaluatedCell(model, "A1").formattedValue, "04/14/2016");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user