Skip to content

Commit

Permalink
[IMP] Improve aggregate sum on views list with the sum of roundings
Browse files Browse the repository at this point in the history
  • Loading branch information
isabellerichard committed Dec 11, 2019
1 parent cc7e31b commit 1e1a8b8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions smile_decimal_precision/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"data": [
"views/decimal_precision_view.xml",
"views/res_currency_view.xml",
"views/templates.xml",
],
"auto_install": True,
"installable": True,
Expand Down
57 changes: 57 additions & 0 deletions smile_decimal_precision/static/src/js/list_renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
odoo.define('smile_decimal_precision.ListRenderer', function (require) {
"use strict";

var ListRenderer = require('web.ListRenderer');
var field_utils = require('web.field_utils');

ListRenderer.include({

_computeColumnAggregates: function (data, column) {
var attrs = column.attrs;
var field = this.state.fields[attrs.name];
if (!field) {
return;
}
var type = field.type;
if (type !== 'integer' && type !== 'float' && type !== 'monetary') {
return;
}
var func = (attrs.sum && 'sum') || (attrs.avg && 'avg') ||
(attrs.max && 'max') || (attrs.min && 'min');
if (func) {
var count = 0;
var aggregateValue = (func === 'max') ? -Infinity : (func === 'min') ? Infinity : 0;
_.each(data, function (d) {
count += 1;
var value = (d.type === 'record') ? d.data[attrs.name] : d.aggregateValues[attrs.name];
// Added by Smile
var formatFunc = field_utils.format[column.attrs.widget];
if (!formatFunc) {
formatFunc = field_utils.format[field.type];
}
value = field_utils.parse.float(
formatFunc(value, d.fields[attrs.name], {noSymbol: true}));
// End by Smile
if (func === 'avg') {
aggregateValue += value;
} else if (func === 'sum') {
aggregateValue += value;
} else if (func === 'max') {
aggregateValue = Math.max(aggregateValue, value);
} else if (func === 'min') {
aggregateValue = Math.min(aggregateValue, value);
}
});
if (func === 'avg') {
aggregateValue = count ? aggregateValue / count : aggregateValue;
}
column.aggregate = {
help: attrs[func],
value: aggregateValue,
};
}
},

});

});
13 changes: 13 additions & 0 deletions smile_decimal_precision/views/templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>

<odoo>
<data>

<template id="assets_backend" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/smile_decimal_precision/static/src/js/list_renderer.js"></script>
</xpath>
</template>

</data>
</odoo>

0 comments on commit 1e1a8b8

Please sign in to comment.