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 65d283b8db4..501ac0e0692 100644 --- a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js +++ b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js @@ -192,6 +192,9 @@ export default class PivotUIPlugin extends spreadsheet.UIPlugin { const evaluatedArgs = args .map(astToFormula) .map((arg) => this.getters.evaluateFormula(arg)); + if (evaluatedArgs.length <= 2) { + return []; + } const pivotId = evaluatedArgs[0]; const argField = evaluatedArgs[evaluatedArgs.length - 2]; const filters = this.getters.getGlobalFilters(); @@ -200,8 +203,9 @@ export default class PivotUIPlugin extends spreadsheet.UIPlugin { for (const filter of filters) { const dataSource = this.getters.getPivotDataSource(pivotId); const { field, aggregateOperator: time } = dataSource.parseGroupField(argField); - if (filter.pivotFields[pivotId].field === field.name) { - let value = dataSource.getPivotHeaderValue(evaluatedArgs.slice(1)); + const pivotField = filter.pivotFields[pivotId]; + if (pivotField && pivotField.field === field.name) { + let value = dataSource.getPivotHeaderValue(evaluatedArgs.slice(-2)); let transformedValue; const currentValue = this.getters.getGlobalFilterValue(filter.id); switch (filter.type) { diff --git a/addons/spreadsheet/static/tests/global_filters/global_filters_model_test.js b/addons/spreadsheet/static/tests/global_filters/global_filters_model_test.js index e58d014652a..2b294310955 100644 --- a/addons/spreadsheet/static/tests/global_filters/global_filters_model_test.js +++ b/addons/spreadsheet/static/tests/global_filters/global_filters_model_test.js @@ -1535,6 +1535,111 @@ QUnit.module("spreadsheet > Global filters model", {}, () => { } ); + QUnit.test( + "getFiltersMatchingPivot return correctly matching filter according to cell formula with __count and positional argument", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /*xml*/ ` + + + + `, + }); + setCellContent(model, "B3", '=ODOO.PIVOT(1, "__count", "#product_id", 1)'); + await addGlobalFilter(model, { + filter: { + id: "42", + type: "relation", + defaultValue: [], + pivotFields: { 1: { field: "product_id", type: "many2one" } }, + }, + }); + const filters = model.getters.getFiltersMatchingPivot(getCellFormula(model, "B3")); + assert.deepEqual(filters, [ + { + filterId: "42", + value: [37], + }, + ]); + } + ); + + QUnit.test( + "getFiltersMatchingPivot return correctly matching filter according to cell formula with positional argument", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /*xml*/ ` + + + + `, + }); + setCellContent(model, "B3", '=ODOO.PIVOT(1, "probability", "#product_id", 1)'); + await addGlobalFilter(model, { + filter: { + id: "42", + type: "relation", + defaultValue: [], + pivotFields: { 1: { field: "product_id", type: "many2one" } }, + }, + }); + const filters = model.getters.getFiltersMatchingPivot(getCellFormula(model, "B3")); + assert.deepEqual(filters, [ + { + filterId: "42", + value: [37], + }, + ]); + } + ); + + QUnit.test( + "getFiltersMatchingPivot return correctly matching filter when there is a filter with no field defined", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /*xml*/ ` + + + + `, + }); + await addGlobalFilter(model, { + filter: { + id: "42", + type: "relation", + defaultValue: [], + pivotFields: {}, + }, + }); + const filters = model.getters.getFiltersMatchingPivot(getCellFormula(model, "B3")); + assert.deepEqual(filters, []); + } + ); + + QUnit.test( + "getFiltersMatchingPivot return empty filter for cell formula without any argument", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /*xml*/ ` + + + + `, + }); + setCellContent(model, "B3", '=ODOO.PIVOT(1, "probability")'); + await addGlobalFilter(model, { + filter: { + id: "42", + type: "relation", + defaultValue: [], + pivotFields: { 1: { field: "product_id", type: "many2one" } }, + }, + }); + const filters = model.getters.getFiltersMatchingPivot(getCellFormula(model, "B3")); + assert.deepEqual(filters, []); + } + ); + QUnit.test("field matching is removed when pivot is deleted", async function (assert) { const { model } = await createSpreadsheetWithPivot(); await addGlobalFilter(model, LAST_YEAR_FILTER);