mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[MIG] web_pivot_computed_measure: Migration to 13.0
This commit is contained in:
committed by
CarlosRoca13
parent
bea8716db7
commit
b66c149b71
@@ -4,11 +4,10 @@
|
||||
odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
"use strict";
|
||||
|
||||
var core = require("web.core");
|
||||
var PivotModel = require("web.PivotModel");
|
||||
var ComparisonUtils = require("web.dataComparisonUtils");
|
||||
const core = require("web.core");
|
||||
const PivotModel = require("web.PivotModel");
|
||||
|
||||
var _t = core._t;
|
||||
const _t = core._t;
|
||||
|
||||
PivotModel.include({
|
||||
_computed_measures: [],
|
||||
@@ -22,9 +21,10 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
* @param {String} operation
|
||||
* @param {String} name
|
||||
* @param {String} format
|
||||
* @returns a promise
|
||||
*/
|
||||
createComputedMeasure: function(id, field1, field2, operation, name, format) {
|
||||
var measure = _.find(this._computed_measures, function(item) {
|
||||
const measure = _.find(this._computed_measures, item => {
|
||||
return (
|
||||
item.field1 === field1 &&
|
||||
item.field2 === field2 &&
|
||||
@@ -32,15 +32,13 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
);
|
||||
});
|
||||
if (measure) {
|
||||
return $.Deferred(function(d) {
|
||||
d.resolve();
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
var fieldM1 = this.fields[field1];
|
||||
var fieldM2 = this.fields[field2];
|
||||
var cmId = "__computed_" + id;
|
||||
var oper = operation.replace(/m1/g, field1).replace(/m2/g, field2);
|
||||
var oper_human = operation
|
||||
const fieldM1 = this.fields[field1];
|
||||
const fieldM2 = this.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
|
||||
@@ -49,7 +47,7 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
/m2/g,
|
||||
fieldM2.__computed_id ? "(" + fieldM2.string + ")" : fieldM2.string
|
||||
);
|
||||
var cmTotal = this._computed_measures.push({
|
||||
const cmTotal = this._computed_measures.push({
|
||||
field1: field1,
|
||||
field2: field2,
|
||||
operation: oper,
|
||||
@@ -64,16 +62,21 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
/**
|
||||
* Create and enable a measure based on a 'fake' field
|
||||
*
|
||||
* @private
|
||||
* @param {Object} cmDef
|
||||
* @param {List} fields *Optional*
|
||||
* @returns a promise
|
||||
*/
|
||||
_createVirtualMeasure: function(cmDef, fields) {
|
||||
var arrFields = fields || this.fields;
|
||||
const arrFields = fields || this.fields;
|
||||
// This is a minimal 'fake' field info
|
||||
arrFields[cmDef.id] = {
|
||||
type: cmDef.format, // Used to format the value
|
||||
string: cmDef.name, // Used to print the header name
|
||||
__computed_id: cmDef.id, // Used to know if is a computed measure field
|
||||
// Used to format the value
|
||||
type: cmDef.format,
|
||||
// Used to print the header name
|
||||
string: cmDef.name,
|
||||
// Used to know if is a computed measure field
|
||||
__computed_id: cmDef.id,
|
||||
};
|
||||
this.trigger_up("add_measure", {
|
||||
id: cmDef.id,
|
||||
@@ -82,14 +85,13 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
return this._activeMeasures([cmDef.field1, cmDef.field2, cmDef.id]);
|
||||
},
|
||||
|
||||
/**
|
||||
/*
|
||||
* @private
|
||||
* @param {List of Strings} fields
|
||||
*/
|
||||
_activeMeasures: function(fields) {
|
||||
var needLoad = false;
|
||||
var l = fields.length;
|
||||
for (var x = 0; x < l; ++x) {
|
||||
var field = fields[x];
|
||||
let needLoad = false;
|
||||
for (const field of fields) {
|
||||
if (!this._isMeasureEnabled(field)) {
|
||||
this.data.measures.push(field);
|
||||
needLoad = true;
|
||||
@@ -98,12 +100,11 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
if (needLoad) {
|
||||
return this._loadData();
|
||||
}
|
||||
return $.Deferred(function(d) {
|
||||
d.resolve();
|
||||
});
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
||||
/**
|
||||
/*
|
||||
* @private
|
||||
* @param {String} field
|
||||
*/
|
||||
_isMeasureEnabled: function(field) {
|
||||
@@ -111,81 +112,56 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
},
|
||||
|
||||
/**
|
||||
* Helper function to add computed measure fields data into 'dataPoint'
|
||||
* Helper function to add computed measure fields data into a 'subGroupData'
|
||||
*
|
||||
* @param {Object} dataPoint
|
||||
* @param {Object} dataPointComp
|
||||
* @private
|
||||
* @param {Object} subGroupData
|
||||
*/
|
||||
_fillComputedMeasuresData: function(dataPoint, dataPointComp) {
|
||||
var self = this;
|
||||
_.each(this._computed_measures, function(cm) {
|
||||
if (!self._isMeasureEnabled(cm.id)) {
|
||||
return;
|
||||
}
|
||||
if (dataPointComp) {
|
||||
var resData = py.eval(cm.operation, dataPointComp.data);
|
||||
var resComparison = py.eval(cm.operation, dataPointComp.comparison);
|
||||
dataPoint[cm.id] = {
|
||||
data: resData,
|
||||
comparisonData: resComparison,
|
||||
variation: ComparisonUtils.computeVariation(
|
||||
resData,
|
||||
resComparison
|
||||
),
|
||||
};
|
||||
_fillComputedMeasuresData: function(subGroupData) {
|
||||
for (const cm of this._computed_measures) {
|
||||
if (!this._isMeasureEnabled(cm.id)) return;
|
||||
if (subGroupData.__count === 0) {
|
||||
subGroupData[cm.id] = false;
|
||||
} else {
|
||||
dataPoint[cm.id] = py.eval(cm.operation, dataPoint);
|
||||
// eslint-disable-next-line no-undef
|
||||
subGroupData[cm.id] = py.eval(cm.operation, subGroupData);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Fill the dataPoints with the computed measures values
|
||||
* Fill the groupSubdivisions with the computed measures and their values
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_mergeData: function(data, comparisonData, groupBys) {
|
||||
var res = this._super.apply(this, arguments);
|
||||
var len = groupBys.length; // Cached loop (This is not python! hehe)
|
||||
for (var index = 0; index < len; ++index) {
|
||||
if (res.length) {
|
||||
var len2 = res[index].length;
|
||||
for (var k = 0; k < len2; ++k) {
|
||||
var dataPoint = res[index][k];
|
||||
if (_.isEmpty(dataPoint)) {
|
||||
break;
|
||||
}
|
||||
if ("__comparisonDomain" in dataPoint) {
|
||||
// Transform comparison dataPoint object to be compatible
|
||||
var pairsDataPoint = _.pairs(dataPoint);
|
||||
var dataPointComp = {
|
||||
data: _.object(
|
||||
_.map(pairsDataPoint, item => {
|
||||
return [item[0], item[1] && item[1].data];
|
||||
})
|
||||
),
|
||||
comparison: _.object(
|
||||
_.map(pairsDataPoint, item => {
|
||||
return [
|
||||
item[0],
|
||||
item[1] && item[1].comparisonData,
|
||||
];
|
||||
})
|
||||
),
|
||||
};
|
||||
// Update datas. Required by computed measures that uses
|
||||
// other computed measures to work
|
||||
this._fillComputedMeasuresData(dataPointComp.data);
|
||||
this._fillComputedMeasuresData(dataPointComp.comparison);
|
||||
// Update comparison dataPoint
|
||||
this._fillComputedMeasuresData(dataPoint, dataPointComp);
|
||||
} else {
|
||||
// Update standard dataPoint
|
||||
this._fillComputedMeasuresData(dataPoint);
|
||||
}
|
||||
}
|
||||
_prepareData: function(group, groupSubdivisions) {
|
||||
for (const groupSubdivision of groupSubdivisions) {
|
||||
for (const subGroup of groupSubdivision.subGroups) {
|
||||
this._fillComputedMeasuresData(subGroup);
|
||||
}
|
||||
}
|
||||
this._super.apply(this, 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.data.measures, so we remove the
|
||||
* computed measures form this.data.measures before calling _super
|
||||
* to prevent an exception
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_getGroupSubdivision: function() {
|
||||
const computed_measures = [];
|
||||
for (let i = 0; i < this.data.measures.length; i++)
|
||||
if (this.data.measures[i].startsWith("__computed_")) {
|
||||
computed_measures.push(this.data.measures[i]);
|
||||
this.data.measures.splice(i, 1);
|
||||
i--;
|
||||
}
|
||||
const res = this._super.apply(this, arguments);
|
||||
$.merge(this.data.measures, computed_measures);
|
||||
return res;
|
||||
},
|
||||
|
||||
@@ -195,15 +171,12 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
* @override
|
||||
*/
|
||||
load: function(params) {
|
||||
var self = this;
|
||||
this._computed_measures =
|
||||
params.context.pivot_computed_measures ||
|
||||
params.computed_measures ||
|
||||
[];
|
||||
var toActive = [];
|
||||
var l = this._computed_measures.length;
|
||||
for (var x = 0; x < l; ++x) {
|
||||
var cmDef = this._computed_measures[x];
|
||||
const toActive = [];
|
||||
for (const cmDef of this._computed_measures) {
|
||||
params.fields[cmDef.id] = {
|
||||
type: cmDef.format,
|
||||
string: cmDef.name,
|
||||
@@ -211,17 +184,16 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
};
|
||||
toActive.push(cmDef.field1, cmDef.field2, cmDef.id);
|
||||
}
|
||||
return this._super(params).then(function() {
|
||||
_.defer(function() {
|
||||
for (var x = 0; x < l; ++x) {
|
||||
var cmDef = self._computed_measures[x];
|
||||
self.trigger_up("add_measure", {
|
||||
return this._super(params).then(() => {
|
||||
_.defer(() => {
|
||||
for (const cmDef of this._computed_measures) {
|
||||
this.trigger_up("add_measure", {
|
||||
id: cmDef.id,
|
||||
def: self.fields[cmDef.id],
|
||||
def: this.fields[cmDef.id],
|
||||
});
|
||||
}
|
||||
});
|
||||
self._activeMeasures(toActive);
|
||||
this._activeMeasures(toActive);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -237,24 +209,21 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
params.computed_measures ||
|
||||
[];
|
||||
}
|
||||
var l = this._computed_measures.length;
|
||||
for (var x = 0; x < l; ++x) {
|
||||
this._createVirtualMeasure(this._computed_measures[x]);
|
||||
for (const cmDef of this._computed_measures) {
|
||||
this._createVirtualMeasure(cmDef);
|
||||
}
|
||||
// Clean unused 'fake' fields
|
||||
var fieldNames = Object.keys(this.fields);
|
||||
for (var x = 0; x < fieldNames.length; ++x) {
|
||||
var field = this.fields[fieldNames[x]];
|
||||
const fieldNames = Object.keys(this.fields);
|
||||
for (const fieldName of fieldNames) {
|
||||
const field = this.fields[fieldName];
|
||||
if (field.__computed_id) {
|
||||
var cm = _.find(this._computed_measures, {id: field.__computed_id});
|
||||
const cm = _.find(this._computed_measures, {
|
||||
id: field.__computed_id,
|
||||
});
|
||||
if (!cm) {
|
||||
delete this.fields[fieldNames[x]];
|
||||
this.data.measures = _.without(
|
||||
this.data.measures,
|
||||
fieldNames[x]
|
||||
);
|
||||
delete this.fields[fieldName];
|
||||
this.data.measures = _.without(this.data.measures, fieldName);
|
||||
this.trigger_up("remove_measure", {
|
||||
id: fieldNames[x],
|
||||
id: fieldName,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -268,7 +237,7 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
* @override
|
||||
*/
|
||||
get: function() {
|
||||
var res = this._super.apply(this, arguments);
|
||||
const res = this._super.apply(this, arguments);
|
||||
res.computed_measures = this._computed_measures;
|
||||
return res;
|
||||
},
|
||||
@@ -282,31 +251,29 @@ odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
toggleMeasure: function(field) {
|
||||
if (this._isMeasureEnabled(field)) {
|
||||
// Measure is disabled
|
||||
var umeasures = _.filter(this._computed_measures, function(item) {
|
||||
const umeasures = _.filter(this._computed_measures, item => {
|
||||
return item.field1 === field || item.field2 === field;
|
||||
});
|
||||
if (umeasures.length && this._isMeasureEnabled(umeasures[0].id)) {
|
||||
return $.Deferred(function(d) {
|
||||
d.reject(
|
||||
_t(
|
||||
"This measure is currently used by a 'computed measure'. Please, disable the computed measure first."
|
||||
)
|
||||
);
|
||||
});
|
||||
return Promise.reject(
|
||||
_t(
|
||||
"This measure is currently used by a 'computed measure'. Please, disable the computed measure first."
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Mesaure is enabled
|
||||
var toEnable = [];
|
||||
var toAnalize = [field];
|
||||
const toEnable = [];
|
||||
const toAnalize = [field];
|
||||
while (toAnalize.length) {
|
||||
var afield = toAnalize.shift();
|
||||
var fieldDef = this.fields[afield];
|
||||
const afield = toAnalize.shift();
|
||||
const fieldDef = this.fields[afield];
|
||||
if (fieldDef.__computed_id) {
|
||||
var cm = _.find(this._computed_measures, {
|
||||
const cm = _.find(this._computed_measures, {
|
||||
id: fieldDef.__computed_id,
|
||||
});
|
||||
toAnalize.push(cm.field1, cm.field2);
|
||||
var toEnableFields = [];
|
||||
const toEnableFields = [];
|
||||
if (!this.fields[cm.field1].__computed_id) {
|
||||
toEnableFields.push(cm.field1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user