From 943a2fb0217331160ac4de7977b4c8df3a64a9cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Fri, 20 Oct 2023 16:17:15 +0200 Subject: [PATCH] [FIX] spreadsheet: format pivot days header This commit fixes 2 very similar issue. See enterprise commit. - Group a pivot by any date field, with the day aggregate - insert it in spreadsheet - autofill a pivot function => the autofill tooltip displays the day number instead of the day formatted as a date. - on the same pivot - click on menu "Data > Insert pivot > insert pivot cell > [you pivot]" => in the dialog, numbers appears for the headers. Task: 3570281 Part-of: odoo/odoo#139295 --- .../src/pivot/plugins/pivot_ui_plugin.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js index dffddb39f52..4749805dba0 100644 --- a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js +++ b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js @@ -9,7 +9,8 @@ import { globalFiltersFieldMatchers } from "@spreadsheet/global_filters/plugins/ import { PivotDataSource } from "../pivot_data_source"; import { pivotTimeAdapter } from "../pivot_time_adapters"; -const { astToFormula } = spreadsheet; +const { astToFormula, helpers } = spreadsheet; +const { formatValue } = helpers; const { DateTime } = luxon; /** @@ -317,6 +318,23 @@ export class PivotUIPlugin extends spreadsheet.UIPlugin { return dataSource.computeOdooPivotHeaderValue(domainArgs); } + /** + * High level method computing the formatted result of ODOO.PIVOT.HEADER functions. + * + * @param {string} pivotId + * @param {(string | number)[]} pivotArgs arguments of the function (except the first one which is the pivot id) + */ + getPivotHeaderFormattedValue(pivotId, pivotArgs) { + const dataSource = this.getters.getPivotDataSource(pivotId); + const value = dataSource.computeOdooPivotHeaderValue(pivotArgs); + if (typeof value === "string") { + return value; + } + const format = this.getPivotFieldFormat(pivotId, pivotArgs.at(-2)); + const locale = this.getters.getLocale(); + return formatValue(value, { format, locale }); + } + getPivotFieldFormat(pivotId, fieldName) { const dataSource = this.getPivotDataSource(pivotId); const { field, aggregateOperator } = dataSource.parseGroupField(fieldName); @@ -547,6 +565,7 @@ PivotUIPlugin.getters = [ "getSelectedPivotId", "getPivotComputedDomain", "computeOdooPivotHeaderValue", + "getPivotHeaderFormattedValue", "getPivotFieldFormat", "getPivotIdFromPosition", "getPivotCellValue",