Ir al contenido
Menú
Se marcó esta pregunta

The original function I want to modify is:


// @spreadsheet/global_filters/helpers.js

export function getRelativeDateDomain(now, offset, rangeType, fieldName, fieldType) {

    // original logic...

}


Since it’s a standalone exported function (not inside a class), I tried to patch it using Odoo’s patch() utility.

Here’s what I’ve done in my custom module:


/** @odoo-module **/


import { patch } from "@web/core/utils/patch";

import * as helpers from "@spreadsheet/global_filters/helpers";

import { RELATIVE_DATE_RANGE_TYPES } from "@spreadsheet/helpers/constants";

import { Domain } from "@web/core/domain";

import { serializeDate, serializeDateTime } from "@web/core/l10n/dates";


console.log("[Custom Patch] relative_date_patch.js loaded ✅");


// Step 1: Add new range types

if (!RELATIVE_DATE_RANGE_TYPES.find((r) => r.type === "today")) {

    RELATIVE_DATE_RANGE_TYPES.unshift(

        { type: "today", description: "Today" },

        { type: "yesterday", description: "Yesterday" }

    );

    console.log("[Custom Patch] Added Today and Yesterday to RELATIVE_DATE_RANGE_TYPES");

}


// Step 2: Patch the helper function

patch(helpers, {

    getRelativeDateDomain(now, offset, rangeType, fieldName, fieldType) {

        console.log("[Custom Patch] getRelativeDateDomain called", rangeType);


        switch (rangeType) {

            case "today":

                const startToday = now.startOf("day").plus({ days: offset });

                const endToday = now.endOf("day").plus({ days: offset });

                return new Domain([

                    "&",

                    [fieldName, ">=", serializeDate(startToday)],

                    [fieldName, "<=", serializeDate(endToday)],

                ]);


            case "yesterday":

                const yesterday = now.minus({ days: 1 }).plus({ days: offset });

                const startYesterday = yesterday.startOf("day");

                const endYesterday = yesterday.endOf("day");

                return new Domain([

                    "&",

                    [fieldName, ">=", serializeDate(startYesterday)],

                    [fieldName, "<=", serializeDate(endYesterday)],

                ]);


            default:

                // fallback to original

                return helpers.getRelativeDateDomain(...arguments);

        }

    },

});


console.log("[Custom Patch] getRelativeDateDomain patch applied successfully ✅");

 

Current logs show:

[Custom Patch] relative_date_patch.js loaded ✅

[Custom Patch] Added Today and Yesterday to RELATIVE_DATE_RANGE_TYPES

[Custom Patch] getRelativeDateDomain patch applied successfully ✅


But none of the logs inside the patched function appear - it seems Odoo still calls the original function from @spreadsheet/global_filters/helpers instead of my patched version.


How can I override or patch a standalone exported JS function like getRelativeDateDomain() in Odoo 18, so my patched version is actually used?

Avatar
Descartar
Mejor respuesta

Hi,


The issue occurs because patch() in Odoo is meant for patching classes or objects, not standalone exported functions. When you try to patch a standalone function like getRelativeDateDomain, Odoo’s internal modules that already imported it keep their own reference to the original function. This means your patched version never actually replaces the one being called, which is why no logs appear from inside your custom function.


To fix this, you need to override the function directly instead of using patch(). The solution is to import the helpers module, store the original getRelativeDateDomain function in a variable, and then reassign it with your custom implementation. Inside the new version, you can handle your new range types (“today” and “yesterday”) and fall back to the original function for other cases. This direct reassignment ensures that any code importing the helpers module as a namespace (import * as helpers) will now use your modified function.


Try the code below.


/** @odoo-module **/


import * as helpers from "@spreadsheet/global_filters/helpers";

import { Domain } from "@web/core/domain";

import { serializeDate } from "@web/core/l10n/dates";

import { RELATIVE_DATE_RANGE_TYPES } from "@spreadsheet/helpers/constants";


console.log("[Custom Patch] relative_date_patch.js loaded");


// Add new range types if not already present

if (!RELATIVE_DATE_RANGE_TYPES.find((r) => r.type === "today")) {

    RELATIVE_DATE_RANGE_TYPES.unshift(

        { type: "today", description: "Today" },

        { type: "yesterday", description: "Yesterday" }

    );

    console.log("[Custom Patch] Added Today and Yesterday to RELATIVE_DATE_RANGE_TYPES");

}


// Save reference to original function

const originalGetRelativeDateDomain = helpers.getRelativeDateDomain;


// Override the function directly

helpers.getRelativeDateDomain = function (now, offset, rangeType, fieldName, fieldType) {

    console.log("[Custom Patch] getRelativeDateDomain called", rangeType);


    switch (rangeType) {

        case "today":

            const startToday = now.startOf("day").plus({ days: offset });

            const endToday = now.endOf("day").plus({ days: offset });

            return new Domain([

                "&",

                [fieldName, ">=", serializeDate(startToday)],

                [fieldName, "<=", serializeDate(endToday)],

            ]);


        case "yesterday":

            const yesterday = now.minus({ days: 1 }).plus({ days: offset });

            const startYesterday = yesterday.startOf("day");

            const endYesterday = yesterday.endOf("day");

            return new Domain([

                "&",

                [fieldName, ">=", serializeDate(startYesterday)],

                [fieldName, "<=", serializeDate(endYesterday)],

            ]);


        default:

            return originalGetRelativeDateDomain(...arguments);

    }

};


console.log("[Custom Patch] getRelativeDateDomain overridden successfully");


Finally, make sure your JavaScript file is loaded after the original spreadsheet helpers module by declaring it in your module’s manifest under the web.assets_backend section. Once done, you can confirm the patch is active by checking your browser console logs when applying relative date filters in the spreadsheet.


Hope it helps

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
dic 18
6065
2
sept 25
1758
2
ago 25
2777
0
feb 25
929
1
nov 24
3204