[FIX] web_pivot_computed_measure: Use js eval to avoid different types operation error

pull/2755/head
Carlos Roca 2023-11-21 15:18:34 +01:00
parent 6b5fbfb993
commit c5f5c7274d
2 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,27 @@
/** @odoo-module **/
/* Copyright 2022 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
/**
* Helper function to eval text for a given object
*
* @param {String} text
* @param {Object} vals
* @returns {any}
*/
export const evalOperation = (text, vals) => {
for (const variable in vals) {
if (vals.hasOwnProperty(variable)) {
const regex = new RegExp(variable, "g");
text = text.replace(regex, vals[variable]);
}
}
try {
// eslint-disable-next-line no-eval
const res = eval(text);
return res;
} catch (error) {
console.error("Error trying to eval operation:", error);
return;
}
};

View File

@ -6,6 +6,7 @@
import {PivotModel} from "@web/views/pivot/pivot_model";
import {patch} from "web.utils";
import {computeReportMeasures} from "@web/views/helpers/utils";
import {evalOperation} from "../helpers/utils.esm";
patch(PivotModel.prototype, "web_pivot_computed_measure.PivotModel", {
/**
@ -148,7 +149,7 @@ patch(PivotModel.prototype, "web_pivot_computed_measure.PivotModel", {
subGroupData[cm.id] = false;
} else {
// eslint-disable-next-line no-undef
subGroupData[cm.id] = py.eval(cm.operation, subGroupData);
subGroupData[cm.id] = evalOperation(cm.operation, subGroupData);
}
}
},