summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views
diff options
context:
space:
mode:
authorMathieu Duckerts-Antoine <dam@odoo.com>2018-04-05 17:08:26 +0200
committerGéry Debongnie <ged@odoo.com>2018-04-10 17:41:12 +0200
commit47395c6e51ec82283d3cd46e386befc256d8a657 (patch)
treeba8f89cdd2e46ccbe16635939ba3afe318922139 /addons/web/static/src/js/views
parent742476fb20e8183f96c42836552381fff259712c (diff)
[IMP] web: allow using many2one as measure in pivot view
With this commit, we add the possibility of using many2ones as measure. The way a many2one is aggregated is by using the count distinct operator. Some care had to be done to gracefully support the situation when a pivot view is grouped by a m2o field F, and the same field F is used as a measure.
Diffstat (limited to 'addons/web/static/src/js/views')
-rw-r--r--addons/web/static/src/js/views/pivot/pivot_model.js54
-rw-r--r--addons/web/static/src/js/views/pivot/pivot_renderer.js3
-rw-r--r--addons/web/static/src/js/views/pivot/pivot_view.js2
3 files changed, 51 insertions, 8 deletions
diff --git a/addons/web/static/src/js/views/pivot/pivot_model.js b/addons/web/static/src/js/views/pivot/pivot_model.js
index 1c76c41ab71..fafeb8428d9 100644
--- a/addons/web/static/src/js/views/pivot/pivot_model.js
+++ b/addons/web/static/src/js/views/pivot/pivot_model.js
@@ -73,7 +73,15 @@ var PivotModel = AbstractModel.extend({
var other_root = header.root.other_root;
var other_groupbys = header.root.other_root.groupbys;
- var fields = [].concat(field, other_groupbys, this.data.measures);
+
+ var measures = _.map(this.data.measures, function(measure) {
+ var type = self.fields[measure].type;
+ return (type === 'many2one') ? measure + ":count_distinct" : measure;
+ });
+ var fields = _.map(other_groupbys, function (field) {
+ return field.split(':')[0];
+ });
+
var groupbys = [];
for (var i = 0; i <= other_groupbys.length; i++) {
@@ -86,7 +94,7 @@ var PivotModel = AbstractModel.extend({
method: 'read_group',
context: self.data.context,
domain: header.domain.length ? header.domain : self.data.domain,
- fields: _.map(fields, function (field) { return field.split(':')[0]; }),
+ fields: [].concat(field, fields, measures),
groupBy: groupBy,
lazy: false,
});
@@ -113,7 +121,19 @@ var PivotModel = AbstractModel.extend({
continue;
}
for (cell_value = {}, l=0; l < self.data.measures.length; l++) {
- cell_value[self.data.measures[l]] = datapt[self.data.measures[l]];
+ var _value = datapt[self.data.measures[l]];
+ if (_value instanceof Array) {
+ // when a many2one field is used as a measure AND as
+ // a grouped field, bad things happen. The server
+ // will only return the grouped value and will not
+ // aggregate it. Since there is a nameclash, we are
+ // then in the situation where this value is an
+ // array. Fortunately, if we group by a field,
+ // then we can say for certain that the group contains
+ // exactly one distinct value for that field.
+ _value = 1;
+ }
+ cell_value[self.data.measures[l]] =_value;
}
// cell_value.__count = attrs.length;
if (!self.data.cells[row.id]) {
@@ -523,7 +543,17 @@ var PivotModel = AbstractModel.extend({
var groupBys = [];
var rowGroupBys = this.data.groupedBy.length ? this.data.groupedBy : this.initialRowGroupBys;
var colGroupBys = this.data.colGroupBys;
- var fields = [].concat(rowGroupBys, colGroupBys, this.data.measures);
+ var measures = _.map(this.data.measures, function(measure) {
+ if (self.fields[measure].type === 'many2one') {
+ return measure + ":count_distinct";
+ }
+ else {
+ return measure;
+ }
+ });
+ var fields = _.map(rowGroupBys.concat(colGroupBys), function (field) {
+ return field.split(':')[0];
+ }).concat(measures);
for (var i = 0; i < rowGroupBys.length + 1; i++) {
for (var j = 0; j < colGroupBys.length + 1; j++) {
@@ -537,7 +567,7 @@ var PivotModel = AbstractModel.extend({
method: 'read_group',
context: self.data.context,
domain: self.data.domain,
- fields: _.map(fields, function (field) { return field.split(':')[0]; }),
+ fields: fields,
groupBy: groupBy,
lazy: false,
});
@@ -633,7 +663,19 @@ var PivotModel = AbstractModel.extend({
}
if (!this.data.cells[row.id]) this.data.cells[row.id] = [];
for (cell_value = {}, m=0; m < this.data.measures.length; m++) {
- cell_value[this.data.measures[m]] = datapt[this.data.measures[m]];
+ var _value = datapt[this.data.measures[m]];
+ if (_value instanceof Array) {
+ // when a many2one field is used as a measure AND as
+ // a grouped field, bad things happen. The server
+ // will only return the grouped value and will not
+ // aggregate it. Since there is a nameclash, we are
+ // then in the situation where this value is an
+ // array. Fortunately, if we group by a field,
+ // then we can say for certain that the group contains
+ // exactly one distinct value for that field.
+ _value = 1;
+ }
+ cell_value[this.data.measures[m]] = _value;
}
this.data.cells[row.id][col.id] = cell_value;
}
diff --git a/addons/web/static/src/js/views/pivot/pivot_renderer.js b/addons/web/static/src/js/views/pivot/pivot_renderer.js
index 83112277bb3..b5648d358a9 100644
--- a/addons/web/static/src/js/views/pivot/pivot_renderer.js
+++ b/addons/web/static/src/js/views/pivot/pivot_renderer.js
@@ -124,7 +124,8 @@ var PivotRenderer = AbstractRenderer.extend({
return self.state.fields[gb.split(':')[0]].string;
});
var measureTypes = this.state.measures.map(function (name) {
- return self.state.fields[name].type;
+ var type = self.state.fields[name].type;
+ return type === 'many2one' ? 'integer' : type;
});
for (i = 0; i < rows.length; i++) {
$row = $('<tr>');
diff --git a/addons/web/static/src/js/views/pivot/pivot_view.js b/addons/web/static/src/js/views/pivot/pivot_view.js
index 0d42fc7bd46..199042ec12a 100644
--- a/addons/web/static/src/js/views/pivot/pivot_view.js
+++ b/addons/web/static/src/js/views/pivot/pivot_view.js
@@ -45,7 +45,7 @@ var PivotView = AbstractView.extend({
this.fields.__count = {string: _t("Count"), type: "integer"};
_.each(this.fields, function (field, name) {
if ((name !== 'id') && (field.store === true)) {
- if (_.contains(['integer', 'float', 'monetary'], field.type)) {
+ if (_.contains(['integer', 'float', 'monetary', 'many2one'], field.type)) {
measures[name] = field;
}
if (_.contains(GROUPABLE_TYPES, field.type)) {