diff --git a/addons/spreadsheet/static/src/helpers/constants.js b/addons/spreadsheet/static/src/helpers/constants.js index 58ba8e634cb..476c8b992f8 100644 --- a/addons/spreadsheet/static/src/helpers/constants.js +++ b/addons/spreadsheet/static/src/helpers/constants.js @@ -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" }; diff --git a/addons/spreadsheet/static/src/pivot/pivot_model.js b/addons/spreadsheet/static/src/pivot/pivot_model.js index b3f14077227..2e6d790d2d9 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_model.js +++ b/addons/spreadsheet/static/src/pivot/pivot_model.js @@ -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(); } /** diff --git a/addons/spreadsheet/static/tests/pivots/model/pivot_positional_formula_test.js b/addons/spreadsheet/static/tests/pivots/model/pivot_positional_formula_test.js index be1f14392be..f6db0d96637 100644 --- a/addons/spreadsheet/static/tests/pivots/model/pivot_positional_formula_test.js +++ b/addons/spreadsheet/static/tests/pivots/model/pivot_positional_formula_test.js @@ -338,6 +338,6 @@ QUnit.module("spreadsheet > positional pivot formula", {}, () => { `, }); 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"); }); });