From 49caf7c06be4cd4f5c0cb523bdd62e6e4488fc56 Mon Sep 17 00:00:00 2001 From: Bruno Boi Date: Thu, 25 Aug 2022 11:48:36 +0000 Subject: [PATCH] [FIX] web: disable timezone convert for Date field custom filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reproduction: 1. Install Timesheet, and load demo data 2. Mimic a timezone in browser (Chrome): Right click->Inspect->Sensors, in the Location-> choose Other…, type “America/Puerto_Rico” in Timezone ID, refresh the page 3. Go to Timesheets->Timesheets->All timesheets, choose pivot view 4. Add custom filter, Date is between 1st/Aug/2022 and 5th/Aug/2022, apply 5. The filter result is correct but the tag in the search bar is “Date is between 31/07/2022 and 04/08/2022” This also happens to other fields with type Date, but not Datetime. For example, in Accounting->Reporting->Invoices Analysis->Pivot view, the same issue happens with Due Date filter. For Datetime field, it doesn’t have the issue, for example in CRM->Sales->My pipeline, change to pivot view, the custom filter for Assignment Date doesn’t have the time zone issue. Note: reproduction usually works during the daytime in Brussels. Reason: miswriting when rewrote custom_filter_item from V14 to V15. The value pushed to descriptionArray should not be changed on time zone. Fix: in parseField and formatField, use default formatters/parsers options. Add test for the Date filters. Related PR: odoo-dev@aeb8e49?diff=unified#diff-6d9230cfcd007b087b2851cbf0b4a33b26dc3c91087853da6fe7ce8c2b60e42f Reference code in V14: https://github.com/odoo/odoo/blob/14.0/addons/web/static/src/js/control_panel/custom_filter_item.js#L170-L173 opw-2941231 Part-of: odoo/odoo#98980 Co-authored-by: Liu Jinjiu --- .../search/filter_menu/custom_filter_item.js | 10 ++++---- .../tests/search/custom_filter_item_tests.js | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/search/filter_menu/custom_filter_item.js b/addons/web/static/src/search/filter_menu/custom_filter_item.js index 1703cec6937..a0e9142e613 100644 --- a/addons/web/static/src/search/filter_menu/custom_filter_item.js +++ b/addons/web/static/src/search/filter_menu/custom_filter_item.js @@ -86,22 +86,22 @@ const FIELD_OPERATORS = { ], }; -function parseField(field, value, opts = {}) { +function parseField(field, value) { if (FIELD_TYPES[field.type] === "char") { return value; } const type = field.type === "id" ? "integer" : field.type; const parse = parsers.contains(type) ? parsers.get(type) : (v) => v; - return parse(value, { field, ...opts }); + return parse(value); } -function formatField(field, value, opts = {}) { +function formatField(field, value) { if (FIELD_TYPES[field.type] === "char") { return value; } const type = field.type === "id" ? "integer" : field.type; const format = formatters.contains(type) ? formatters.get(type) : (v) => v; - return format(value, { digits: field.digits, ...opts }); + return format(value, { digits: field.digits }); } export class CustomFilterItem extends Component { @@ -224,7 +224,7 @@ export class CustomFilterItem extends Component { domainValue = condition.value.map(serialize); descriptionArray.push( `"${condition.value - .map((val) => formatField(field, val, { timezone: true })) + .map((val) => formatField(field, val)) .join(" " + this.env._t("and") + " ")}"` ); } else { diff --git a/addons/web/static/tests/search/custom_filter_item_tests.js b/addons/web/static/tests/search/custom_filter_item_tests.js index 694683ebf5b..43fe2230408 100644 --- a/addons/web/static/tests/search/custom_filter_item_tests.js +++ b/addons/web/static/tests/search/custom_filter_item_tests.js @@ -446,6 +446,30 @@ QUnit.module("Search", (hooks) => { } ); + QUnit.test("custom filter date with equal operator", async function (assert) { + patchTimeZone(-240); + patchDate(2017, 1, 22, 12, 30, 0); + + const controlPanel = await makeWithSearch({ + serverData, + resModel: "foo", + Component: ControlPanel, + searchViewId: false, + searchMenuTypes: ["filter"], + }); + + await toggleFilterMenu(target); + await toggleAddCustomFilter(target); + + await editConditionField(target, 0, "date_field"); + await editConditionOperator(target, 0, "="); + await editConditionValue(target, 0, "01/01/2017"); + await applyFilter(target); + + assert.deepEqual(getFacetTexts(target), ['A date is equal to "01/01/2017"']); + assert.deepEqual(getDomain(controlPanel), [["date_field", "=", "2017-01-01"]]); + }); + QUnit.test("custom filter datetime with equal operator", async function (assert) { assert.expect(5);