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 = $(''); 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)) { diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index ccd98c10962..96f80c96eb2 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -527,7 +527,7 @@