diff --git a/addons/spreadsheet/static/src/pivot/pivot_data_source.js b/addons/spreadsheet/static/src/pivot/pivot_data_source.js index c66626f0e5b..584feef2ce0 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_data_source.js +++ b/addons/spreadsheet/static/src/pivot/pivot_data_source.js @@ -77,7 +77,7 @@ export class PivotDataSource extends OdooViewsDataSource { if (domainArgs.at(-2) === "measure") { return this.getMeasureDisplayName(domainArgs.at(-1)); } - return this._model.getGroupByDisplayLabel( + return this._model.getGroupByCellValue( domainArgs.at(-2), this._model.getLastPivotGroupValue(domainArgs) ); diff --git a/addons/spreadsheet/static/src/pivot/pivot_model.js b/addons/spreadsheet/static/src/pivot/pivot_model.js index c45419e91f0..eb9963e612d 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_model.js +++ b/addons/spreadsheet/static/src/pivot/pivot_model.js @@ -293,13 +293,16 @@ export class SpreadsheetPivotModel extends PivotModel { } /** - * Get the label the given field-value + * Get the value of a field + * + * @example + * getGroupByCellValue("stage_id", 42) // "Won" * * @param {string} groupFieldString Name of the field - * @param {string} groupValueString Value of the group by - * @returns {string} + * @param {string | number} groupValueString Value of the group by + * @returns {string | number} */ - getGroupByDisplayLabel(groupFieldString, groupValueString, locale = DEFAULT_LOCALE) { + getGroupByCellValue(groupFieldString, groupValueString) { if (groupValueString === NO_RECORD_AT_THIS_POSITION) { return ""; } @@ -307,12 +310,8 @@ export class SpreadsheetPivotModel extends PivotModel { const value = toNormalizedPivotValue(field, groupValueString, aggregateOperator); const undef = _t("None"); if (this._isDateField(field)) { - // TODO include this parsing to the pivot time adapters and extend it to other time periods - if (value && aggregateOperator === "day") { - return toNumber(value, DEFAULT_LOCALE); - } const adapter = pivotTimeAdapter(aggregateOperator); - return adapter.formatValue(value, locale); + return adapter.toCellValue(value); } if (field.relation) { const label = this.metadataRepository.getRecordDisplayName(field.relation, value); diff --git a/addons/spreadsheet/static/src/pivot/pivot_time_adapters.js b/addons/spreadsheet/static/src/pivot/pivot_time_adapters.js index 1231fc51ce3..9db870296a4 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_time_adapters.js +++ b/addons/spreadsheet/static/src/pivot/pivot_time_adapters.js @@ -71,6 +71,7 @@ export function pivotTimeAdapter(groupAggregate) { * @property {(normalizedValue: string, step: number) => string} increment * @property {(normalizedValue: string, locale: Object) => string} formatValue * @property {(locale: Object) => string} getFormat + * @property {(normalizedValue: string) => string | number} toCellValue */ /** @@ -102,6 +103,9 @@ const dayAdapter = { const value = toNumber(normalizedValue, DEFAULT_LOCALE); return formatValue(value, { locale, format: this.getFormat(locale) }); }, + toCellValue(normalizedValue) { + return toNumber(normalizedValue, DEFAULT_LOCALE); + }, }; /** @@ -133,6 +137,9 @@ const weekAdapter = { const [week, year] = normalizedValue.split("/"); return sprintf(_t("W%(week)s %(year)s"), { week, year }); }, + toCellValue(normalizedValue) { + return this.formatValue(normalizedValue); + }, }; /** @@ -162,6 +169,9 @@ const monthAdapter = { const value = toNumber(normalizedValue, DEFAULT_LOCALE); return formatValue(value, { locale, format: this.getFormat(locale) }); }, + toCellValue(normalizedValue) { + return toNumber(normalizedValue, DEFAULT_LOCALE); + }, }; /** @@ -192,6 +202,9 @@ const quarterAdapter = { const [quarter, year] = normalizedValue.split("/"); return sprintf(_t("Q%(quarter)s %(year)s"), { quarter, year }); }, + toCellValue(normalizedValue) { + return this.formatValue(normalizedValue); + }, }; /** * @type {PivotTimeAdapter} @@ -212,6 +225,9 @@ const yearAdapter = { formatValue(normalizedValue, locale) { return formatValue(normalizedValue, { locale, format: "0" }); }, + toCellValue(normalizedValue) { + return normalizedValue; + }, }; /** @@ -246,6 +262,12 @@ function falseHandlerDecorator(adapter) { } return adapter.formatValue(normalizedValue, locale); }, + toCellValue(normalizedValue) { + if (normalizedValue === false) { + return _t("None"); + } + return adapter.toCellValue(normalizedValue); + }, }; } diff --git a/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js b/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js index 3f217acab09..c483f664d44 100644 --- a/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js +++ b/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js @@ -676,6 +676,61 @@ QUnit.module("spreadsheet > pivot plugin", {}, () => { assert.strictEqual(getEvaluatedCell(model, "B1").formattedValue, "4/14/2016"); }); + QUnit.test("PIVOT.HEADER week are correctly formatted at evaluation", async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + assert.strictEqual(getEvaluatedCell(model, "B1").format, undefined); + assert.strictEqual(getEvaluatedCell(model, "B1").value, "W15 2016"); + assert.strictEqual(getEvaluatedCell(model, "B1").formattedValue, "W15 2016"); + }); + + QUnit.test("PIVOT.HEADER month are correctly formatted at evaluation", async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + assert.strictEqual(getEvaluatedCell(model, "B1").format, "mmmm yyyy"); + assert.strictEqual(getEvaluatedCell(model, "B1").value, 42461); + assert.strictEqual(getEvaluatedCell(model, "B1").formattedValue, "April 2016"); + }); + + QUnit.test( + "PIVOT.HEADER quarter are correctly formatted at evaluation", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + assert.strictEqual(getEvaluatedCell(model, "B1").format, undefined); + assert.strictEqual(getEvaluatedCell(model, "B1").value, "Q2 2016"); + assert.strictEqual(getEvaluatedCell(model, "B1").formattedValue, "Q2 2016"); + } + ); + + QUnit.test("PIVOT.HEADER year are correctly formatted at evaluation", async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + assert.strictEqual(getEvaluatedCell(model, "B1").format, "0"); + assert.strictEqual(getEvaluatedCell(model, "B1").value, 2016); + assert.strictEqual(getEvaluatedCell(model, "B1").formattedValue, "2016"); + }); + QUnit.test( "PIVOT.HEADER formulas are correctly formatted at evaluation", async function (assert) {