Initial commit: Odoo 18.0-20251222 extra-addons
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
tests / Detect unreleased dependencies (push) Has been cancelled
tests / test with OCB (push) Has been cancelled
tests / test with Odoo (push) Has been cancelled

This commit is contained in:
tocmo0nlord
2026-03-13 20:43:25 +00:00
parent 36e847a7df
commit adbe430761
9472 changed files with 1265727 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
/* Copyright 2022 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
import {Component, useState} from "@odoo/owl";
/**
* @extends Component
*/
export class DropdownItemCustomMeasure extends Component {
setup() {
this.isOpen = useState({value: false});
}
onClickComputedMeasure() {
this.isOpen.value = !this.isOpen.value;
}
addMeasure(ev) {
const target = ev.target.closest("#add_computed_measure_wrapper");
const id = new Date().getTime();
const field1 = target.querySelector("#computed_measure_field_1").value;
const field2 = target.querySelector("#computed_measure_field_2").value;
let operation = target.querySelector("#computed_measure_operation").value;
if (operation === "custom") {
operation = target.querySelector(
"#computed_measure_operation_custom"
).value;
}
const name = target.querySelector("#computed_measure_name").value;
const format = target.querySelector("#computed_measure_format").value;
this.props.model.addComputedMeasure(
id,
field1,
field2,
operation,
name,
format
);
}
}
DropdownItemCustomMeasure.template =
"web_pivot_computed_measure.DropdownItemCustomMeasure";
DropdownItemCustomMeasure.props = {
measures: Object,
// Set as model because this module can be extended to be used on views that
// uses Measures like the graph view.
model: Object,
};

View File

@@ -0,0 +1,9 @@
#add_computed_measure_wrapper {
padding: 0 20px;
min-width: 300px;
white-space: nowrap;
.d-table-cell {
vertical-align: middle;
padding: 3px 0;
}
}

View File

@@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="web_pivot_computed_measure.ComputedMeasureOperations" owl="1">
<option name="sum" value="m1+m2">
Sum (m1 + m2)
</option>
<option name="sub" value="m1-m2">
Sub (m1 - m2)
</option>
<option name="mult" value="m1*m2">
Mult (m1 * m2)
</option>
<option name="div" data-format="float" value="m1/m2">
Div (m1 / m2)
</option>
<option name="perc" data-format="percentage" value="m1/m2">
Perc (m1 * 100 / m2)
</option>
<option t-if="debug" name="custom" value="custom">
Custom
</option>
</t>
<t t-name="web_pivot_computed_measure.ComputedMeasureFormats" owl="1">
<option name="int" value="integer">
Integer
</option>
<option name="float" value="float" selected="selected">
Float
</option>
<option name="percentage" value="percentage">
Percentage
</option>
</t>
<t t-name="web_pivot_computed_measure.DropdownItemCustomMeasure" owl="1">
<div class="o_menu_item dropdown-item" data-id="__computed__">
<a href="#" role="menuitem" t-on-click="onClickComputedMeasure">
Computed Measure
<span class="o_submenu_switcher" data-id="__computed__">
<span
t-att-class="isOpen.value ? 'fa fa-caret-down' : 'fa fa-caret-right'"
/>
</span>
</a>
<t t-if="isOpen.value">
<div id="add_computed_measure_wrapper" class="d-table">
<div class="d-table-row">
<div class="d-table-cell">
<label for="computed_measure_field_1">Measure 1</label>
</div>
<div class="d-table-cell">
<select class="o_input" id="computed_measure_field_1">
<t
t-foreach="props.measures"
t-as="measure"
t-key="measure"
>
<option
t-att-value="measure"
t-if="measure != '__count'"
>
<t t-esc="props.measures[measure].string" />
</option>
</t>
</select>
</div>
</div>
<div class="d-table-row">
<div class="d-table-cell">
<label for="computed_measure_field_2">Measure 2</label>
</div>
<div class="d-table-cell">
<select class="o_input" id="computed_measure_field_2">
<t
t-foreach="props.measures"
t-as="measure"
t-key="measure"
>
<option
t-att-value="measure"
t-if="measure != '__count'"
>
<t t-esc="props.measures[measure].string" />
</option>
</t>
</select>
</div>
</div>
<div class="d-table-row">
<div class="d-table-cell">
<label for="computed_measure_operation">Operation</label>
</div>
<div class="d-table-cell">
<select class="o_input" id="computed_measure_operation">
<t
t-call="web_pivot_computed_measure.ComputedMeasureOperations"
/>
</select>
</div>
</div>
<div
t-if="debug"
class="d-none"
id="container_computed_measure_operation_custom"
>
<div class="d-table-cell">
<label
for="computed_measure_operation_custom"
>Formula</label>
</div>
<div class="d-table-cell">
<input
type="text"
class="o_input"
id="computed_measure_operation_custom"
/>
</div>
</div>
<div class="d-table-row">
<div class="d-table-cell">
<label for="computed_measure_name">Name</label>
</div>
<div class="d-table-cell">
<input
placeholder="Can be empty"
type="text"
class="o_input"
id="computed_measure_name"
/>
</div>
</div>
<div class="d-table-row">
<div class="d-table-cell">
<label for="computed_measure_format">Format</label>
</div>
<div class="d-table-cell">
<select class="o_input" id="computed_measure_format">
<t
t-call="web_pivot_computed_measure.ComputedMeasureFormats"
/>
</select>
</div>
</div>
<div class="d-table-row">
<div class="d-table-cell">
<button
class="btn btn-primary o_add_computed_measure"
type="button"
t-on-click="addMeasure"
>Add</button>
</div>
</div>
</div>
</t>
</div>
</t>
</templates>

View File

@@ -0,0 +1,123 @@
/* Copyright 2023 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
/**
* Function that traverse the text given character by character
*
* @param {String} text
* @returns {Array}
*/
function getTokensFromText(text) {
const symbols = ["+", "-", "*", "/", "(", ")"];
const tokens = [];
let token = "";
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (c === " ") continue;
if (symbols.includes(c)) {
if (token !== "") {
tokens.push(token);
token = "";
}
tokens.push(c);
} else {
token += c;
}
}
if (token !== "") {
tokens.push(token);
}
return tokens;
}
/**
* Function that executes an operation between the last two operands in the operands stack
* and the last operator in the operators stack, and saves the result in the operands stack.
*
* @param {Array} operands
* @param {Array} operators
*/
function executeOperation(operands, operators) {
const b = operands.pop();
const a = operands.pop();
const op = operators.pop();
switch (op) {
case "+":
operands.push(a + b);
break;
case "-":
operands.push(a - b);
break;
case "*":
operands.push(a * b);
break;
case "/":
operands.push(a / b);
break;
}
}
/**
* Function that returns the precedence of an operator
*
* @param {String} op
* @returns {Number}
*/
function precedence(op) {
if (op === "+" || op === "-") {
return 1;
}
if (op === "*" || op === "/") {
return 2;
}
if (op === "(" || op === ")") {
return 0;
}
}
/**
* Helper function that takes a mathematical expression in text form and an object
* of variable values, evaluates the expression, and returns the result.
*
* @param {String} text
* @param {Object} values
* @returns {any}
*/
export function evalOperation(text, values) {
const tokens = getTokensFromText(text);
const operands = [];
const operators = [];
for (const token of tokens) {
if (!isNaN(token)) {
// If the token is a number, convert it to a number and add it to the operands stack
operands.push(Number(token));
} else if (token in values) {
// If the token is a variable, get its value from the object and add it to the operands stack
operands.push(values[token]);
} else if (token === "(") {
// If the token is an open parenthesis, add it to the operators stack
operators.push(token);
} else if (token === ")") {
// If the token is a closing parenthesis, pop and execute operators from the stack until an open parenthesis is found
while (operators.length > 0 && operators[operators.length - 1] !== "(") {
executeOperation(operands, operators);
}
// Pop the open parenthesis from the operators stack
operators.pop();
} else {
// If the token is an operator, pop and execute operators from the stack while they have equal or higher precedence than the token
while (
operators.length > 0 &&
precedence(operators[operators.length - 1]) >= precedence(token)
) {
executeOperation(operands, operators);
}
// Add the token to the operators stack
operators.push(token);
}
}
while (operators.length > 0) {
executeOperation(operands, operators);
}
return operands.pop();
}

View File

@@ -0,0 +1,19 @@
/* Copyright 2024 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
import {PivotController} from "@web/views/pivot/pivot_controller";
import {patch} from "@web/core/utils/patch";
patch(PivotController.prototype, {
/**
* Add computed_measures to context key to avoid loosing info when saving the
* filter to favorites.
*
* @override
*/
getContext() {
var res = super.getContext(...arguments);
res.pivot_computed_measures = this.model._computed_measures;
return res;
},
});

View File

@@ -0,0 +1,293 @@
/* Copyright 2020 Tecnativa - Alexandre Díaz
* Copyright 2022 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
import {PivotModel} from "@web/views/pivot/pivot_model";
import {computeReportMeasures} from "@web/views/utils";
import {evalOperation} from "../helpers/utils.esm";
import {patch} from "@web/core/utils/patch";
patch(PivotModel.prototype, {
/**
* Add _computed_measures to avoid recompute them until page is recharged
*
* @override
*/
setup() {
super.setup(...arguments);
this._computed_measures = [];
},
/**
* Create a new computed measure
*
* @param {String} id
* @param {String} field1
* @param {String} field2
* @param {String} operation
* @param {String} name
* @param {String} format
* @returns a promise
*/
addComputedMeasure(id, field1, field2, operation, name, format) {
const measure = this._computed_measures.find((item) => {
return (
item.field1 === field1 &&
item.field2 === field2 &&
item.operation === operation
);
});
if (measure) {
return Promise.resolve();
}
const fieldM1 = this.metaData.fields[field1];
const fieldM2 = this.metaData.fields[field2];
const cmId = "__computed_" + id;
const oper = operation.replace(/m1/g, field1).replace(/m2/g, field2);
const oper_human = operation
.replace(
/m1/g,
fieldM1.__computed_id ? "(" + fieldM1.string + ")" : fieldM1.string
)
.replace(
/m2/g,
fieldM2.__computed_id ? "(" + fieldM2.string + ")" : fieldM2.string
);
const cmTotal = this._computed_measures.push({
field1: field1,
field2: field2,
operation: oper,
name: name || oper_human,
id: cmId,
format: format,
});
return this._createVirtualMeasure(this._computed_measures[cmTotal - 1]);
},
/**
* Create and enable a measure based on a 'fake' field
*
* @private
* @param {Object} cmDef
* @param {List} fields *Optional*
* @returns a promise
*/
_createVirtualMeasure(cmDef, fields) {
this._createVirtualField(cmDef, fields);
// Activate computed field
return this.toggleMeasure(cmDef.id);
},
_createVirtualField(cmDef, fields, config) {
const arrFields = fields || this.metaData.fields;
// This is a minimal 'fake' field info
arrFields[cmDef.id] = {
// Used to format the value
type: cmDef.format,
// Used to print the header name
string: cmDef.name,
// Referenced on payload prop at DropdownItem, used to interact with
// created measures
name: cmDef.id,
// Used to know if is a computed measure field
__computed_id: cmDef.id,
// Operator used for group the measure added.
aggregator: "sum",
};
const metaData = (config && config.metaData) || this.metaData;
metaData.measures[cmDef.id] = arrFields[cmDef.id];
},
/**
* Active the measures related to the 'fake' field
*
* @private
* @param {List of Strings} fields
*/
async _activeMeasures(fields) {
let needLoad = false;
for (const field of fields) {
if (await !this._isMeasureEnabled(field)) {
this.metaData.activeMeasures.push(field);
needLoad = true;
}
}
if (needLoad) {
const config = {metaData: this.metaData, data: this.data};
return this._loadData(config).then(() => {
// Notify changes to renderer for show it on the pivot view
this.notify();
});
}
return Promise.resolve();
},
/**
* Check if the measure is enabled
*
* @private
* @param {String} field
*/
_isMeasureEnabled(field, config) {
const activeMeasures =
(config && config.metaData.activeMeasures) ||
this.metaData.activeMeasures ||
[];
return activeMeasures.includes(field);
},
/**
* Helper function to add computed measure fields data into a 'subGroupData'
*
* @private
* @param {Object} subGroupData
*/
_fillComputedMeasuresData(subGroupData, config) {
for (const cm of this._computed_measures) {
if (!this._isMeasureEnabled(cm.id, config)) continue;
if (subGroupData.__count === 0) {
subGroupData[cm.id] = false;
} else {
subGroupData[cm.id] = evalOperation(cm.operation, subGroupData);
}
}
},
/**
* Fill the groupSubdivisions with the computed measures and their values
*
* @override
*/
_prepareData(group, groupSubdivisions, config) {
for (const groupSubdivision of groupSubdivisions) {
for (const subGroup of groupSubdivision.subGroups) {
this._fillComputedMeasuresData(subGroup, config);
}
}
super._prepareData(...arguments);
},
/**
* _getGroupSubdivision method invokes the read_group method of the
* model via rpc and the passed 'fields' argument is the list of
* measure names that is in this.metaData.activeMeasures, so we remove the
* computed measures form this.metaData.activeMeasures before calling _super
* to prevent any possible exception.
*
* @override
*/
async _getGroupSubdivision(group, rowGroupBy, colGroupBy, config) {
const computed_measures = [];
for (let i = 0; i < config.measureSpecs.length; i++)
if (config.measureSpecs[i].startsWith("__computed_")) {
computed_measures.push(config.measureSpecs[i]);
config.measureSpecs.splice(i, 1);
i--;
}
const res = await super._getGroupSubdivision(...arguments);
Object.assign(config.measureSpecs, computed_measures);
return res;
},
/**
* Adds a rule to deny that measures can be disabled if are being used by a computed measure.
* In the other hand, when enables a measure analyzes it to active all involved measures.
*
* @override
*/
toggleMeasure(fieldName) {
if (this._isMeasureEnabled(fieldName)) {
// Mesaure is enabled
const umeasures = this._computed_measures.filter((item) => {
return item.field1 === fieldName || item.field2 === fieldName;
});
if (umeasures.length && this._isMeasureEnabled(umeasures[0].id)) {
return Promise.reject(
this.env._t(
"This measure is currently used by a 'computed measure'. Please, disable the computed measure first."
)
);
}
} else {
// Measure is disabled
const toEnable = [];
const toAnalyze = [fieldName];
while (toAnalyze.length) {
// Analyze all items involved on computed measures to enable them
const afield = toAnalyze.shift();
const fieldDef = this.metaData.fields[afield];
// Need to check if fieldDef exists to avoid problems with __count
if (fieldDef?.__computed_id) {
const cm = this._computed_measures.find((item) => {
return item.id === fieldDef.__computed_id;
});
toAnalyze.push(cm.field1, cm.field2);
const toEnableFields = [];
if (!this.metaData.fields[cm.field1].__computed_id) {
toEnableFields.push(cm.field1);
}
if (!this.metaData.fields[cm.field2].__computed_id) {
toEnableFields.push(cm.field2);
}
toEnableFields.push(afield);
toEnable.push(toEnableFields);
}
}
if (toEnable.length) {
this._activeMeasures(
// Transform the array of arrays to a simple array.
// [1, [2, 3]] => [1, 2, 3]
toEnable.reverse().flat(Infinity)
);
}
}
return super.toggleMeasure(...arguments);
},
/**
* Load the measures added to selected favorite filters
*
* @override
*/
async load(searchParams) {
var _super = super.load.bind(this);
var config = {metaData: this.metaData, data: this.data};
if (!this.metaData.measures) {
const metaData = this._buildMetaData();
metaData.measures = computeReportMeasures(
metaData.fields,
metaData.fieldAttrs,
metaData.activeMeasures,
metaData.additionalMeasures
);
config = {metaData, data: this.data};
}
if ("context" in searchParams) {
this._computed_measures =
searchParams.context.pivot_computed_measures ||
searchParams.computed_measures ||
[];
}
for (const cmDef of this._computed_measures) {
if (this._isMeasureEnabled(cmDef.id, config)) {
continue;
}
await this._createVirtualField(cmDef, undefined, config);
}
const fieldNames = Object.keys(this.metaData.fields);
for (const fieldName of fieldNames) {
const field = this.metaData.fields[fieldName];
if (field?.__computed_id) {
const cm = this._computed_measures.find(
(item) => item.id === field.__computed_id
);
if (!cm) {
delete this.metaData.fields[fieldName];
delete this.metaData.measures[fieldName];
this.metaData.activeMeasures = this.metaData.activeMeasures.filter(
(item) => item !== fieldName
);
}
}
}
return _super(...arguments);
},
});

View File

@@ -0,0 +1,14 @@
/* Copyright 2022 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
import {PivotRenderer} from "@web/views/pivot/pivot_renderer";
import {patch} from "@web/core/utils/patch";
patch(PivotRenderer.prototype, {
getFormattedValue(cell) {
if (cell.value === Infinity) {
return "-";
}
return super.getFormattedValue(...arguments);
},
});

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-inherit="web.PivotView.Buttons" t-inherit-mode="extension">
<xpath expr="//ReportViewMeasures" position="attributes">
<attribute name="add_computed_measures">true</attribute>
<attribute name="model">model</attribute>
</xpath>
</t>
</templates>

View File

@@ -0,0 +1,15 @@
/* Copyright 2024 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
import {DropdownItemCustomMeasure} from "../dropdown_item_custom_measure/dropdown_item_custom_measure.esm";
import {ReportViewMeasures} from "@web/views/view_components/report_view_measures";
ReportViewMeasures.components = {
...ReportViewMeasures.components,
DropdownItemCustomMeasure,
};
ReportViewMeasures.props = {
...ReportViewMeasures.props,
add_computed_measures: {type: Boolean, optional: true},
model: {type: Object, optional: true},
};

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-inherit="web.ReportViewMeasures" t-inherit-mode="extension">
<xpath expr="//DropdownItem" position="attributes">
<attribute name="t-if">!measure.startsWith('__computed_')</attribute>
</xpath>
<xpath expr="//t[@t-foreach='props.measures']" position="after">
<div
role="separator"
class="dropdown-divider"
t-if="props.add_computed_measures"
/>
<t t-foreach="props.measures" t-as="measure" t-key="measure_value.name">
<DropdownItem
class="{ o_menu_item: true, selected: props.activeMeasures.includes(measure) }"
t-if="props.add_computed_measures and measure.startsWith('__computed_')"
t-esc="props.measures[measure].string"
closingMode="'none'"
onSelected="() => this.props.onMeasureSelected({ measure: measure_value.name })"
/>
</t>
<DropdownItemCustomMeasure
measures="props.measures"
model="props.model"
t-if="props.add_computed_measures"
/>
</xpath>
</t>
</templates>

View File

@@ -0,0 +1,78 @@
/* Copyright 2022 Tecnativa - Carlos Roca
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
import {registry} from "@web/core/registry";
registry.category("web_tour.tours").add("web_pivot_computed_measure_tour", {
url: "/odoo",
test: true,
steps: () => [
{
trigger: ".o_navbar_apps_menu button",
run: "click",
},
{
trigger:
'.o_app[data-menu-xmlid="web_pivot_computed_measure.demo_menu_res_partner_report_pivot"]:visible',
run: "click",
},
{
trigger: '.o_pivot_buttons div[aria-label="Main actions"] button',
run: "click",
},
{
trigger: 'div[data-id="__computed__"] a',
run: "click",
},
{
trigger: "select#computed_measure_field_1",
run() {
this.anchor.value = "partner_latitude";
},
},
{
trigger: "select#computed_measure_field_2",
run() {
this.anchor.value = "partner_longitude";
},
},
{
trigger: "select#computed_measure_operation",
run() {
this.anchor.value = "m1+m2";
},
},
{
trigger: "select#computed_measure_format",
run() {
this.anchor.value = "float";
},
},
{
trigger: "button.o_add_computed_measure",
run: "click",
},
{
trigger: '.o_pivot_buttons div[aria-label="Main actions"] button',
run: "click",
},
{
trigger: 'th.o_pivot_measure_row:contains("Geo Latitude")',
},
{
trigger: 'th.o_pivot_measure_row:contains("Geo Longitude")',
},
{
trigger: 'th.o_pivot_measure_row:contains("Geo Latitude+Geo Longitude")',
},
{
trigger: 'div.o_value:contains("40.7127760")',
},
{
trigger: 'div.o_value:contains("-74.0059740")',
},
{
trigger: 'div.o_value:contains("-33.29")',
},
],
});