diff --git a/web_group_by_percentage/README.rst b/web_group_by_percentage/README.rst new file mode 100644 index 00000000..ec71f08a --- /dev/null +++ b/web_group_by_percentage/README.rst @@ -0,0 +1,84 @@ +==================================== +Show percentage (of total) in groups +==================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/12.0/web_group_by_percentage + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-12-0/web-12-0-web_group_by_percentage + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/162/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module shows the percentage of the total sum in group by rows. + +.. image:: ../static/description/screenshot.png + :alt: Printscreen of percentages in group rows + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +#. Go to any list view; +#. group by one or more fields. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Onestein + +Contributors +~~~~~~~~~~~~ + +* Dennis Sluijk + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/web `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_group_by_percentage/__init__.py b/web_group_by_percentage/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/web_group_by_percentage/__manifest__.py b/web_group_by_percentage/__manifest__.py new file mode 100644 index 00000000..44285045 --- /dev/null +++ b/web_group_by_percentage/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Show percentage (of total) in groups', + 'summary': 'Show the percentage of the total sum in group by rows', + 'author': 'Onestein, Odoo Community Association (OCA)', + 'development_status': 'Beta', + 'website': 'https://github.com/OCA/web', + 'category': 'Web', + 'license': 'AGPL-3', + 'version': '12.0.1.0.0', + 'depends': [ + 'web', + ], + 'data': [ + 'templates/assets.xml' + ], + 'installable': True, +} diff --git a/web_group_by_percentage/readme/CONTRIBUTORS.rst b/web_group_by_percentage/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..47b6403d --- /dev/null +++ b/web_group_by_percentage/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Dennis Sluijk diff --git a/web_group_by_percentage/readme/DESCRIPTION.rst b/web_group_by_percentage/readme/DESCRIPTION.rst new file mode 100644 index 00000000..d8d3c20b --- /dev/null +++ b/web_group_by_percentage/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +This module shows the percentage of the total sum in group by rows. + +.. image:: ../static/description/screenshot.png + :alt: Printscreen of percentages in group rows diff --git a/web_group_by_percentage/readme/USAGE.rst b/web_group_by_percentage/readme/USAGE.rst new file mode 100644 index 00000000..e46283a9 --- /dev/null +++ b/web_group_by_percentage/readme/USAGE.rst @@ -0,0 +1,4 @@ +To use this module, you need to: + +#. Go to any list view; +#. group by one or more fields. diff --git a/web_group_by_percentage/static/description/screenshot.png b/web_group_by_percentage/static/description/screenshot.png new file mode 100644 index 00000000..cf3abdce Binary files /dev/null and b/web_group_by_percentage/static/description/screenshot.png differ diff --git a/web_group_by_percentage/static/src/js/backend.js b/web_group_by_percentage/static/src/js/backend.js new file mode 100644 index 00000000..dd38a533 --- /dev/null +++ b/web_group_by_percentage/static/src/js/backend.js @@ -0,0 +1,94 @@ +/* Copyright 2019 Onestein + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ + +odoo.define('web_group_by_percentage', function (require) { + "use strict"; + + var ListRenderer = require('web.ListRenderer'), + BasicModel = require('web.BasicModel'); + + ListRenderer.include({ + /** + * Render the percentage to the group row. + * + * @override + */ + _renderGroupRow: function (group) { + var self = this; + var res = this._super.apply(this, arguments); + _.each(group.aggregatePercentages, function (percentage, field) { + var cellIndex = _.findIndex(self.columns, function (column) { + if (field === column.attrs.name) { + return true; + } + }); + + var $cell = $(res.find('td').get(cellIndex - 1)); + var $b = $('') + .addClass('web_group_by_percentage') + .html(_.str.sprintf('%s%% ', percentage.toFixed(2))) + .data('percentage', percentage); + $cell.prepend($b); + }); + return res; + }, + }); + + BasicModel.include({ + /** + * Adds aggregatePercentages to the result. + * + * @override + */ + get: function () { + var result = this._super.apply(this, arguments), + dp = result && this.localData[result.id]; + if (dp) { + if (dp.aggregatePercentages) { + result.aggregatePercentages = $.extend({}, dp.aggregatePercentages); + } + } + return result; + }, + + /** + * Calculate percentages. + * + * @override + */ + _readGroup: function () { + var self = this, + res = this._super.apply(this, arguments); + res.done(function (list) { + // Calculate totals + var sums = {}; + _.each(list.data, function (groupId) { + var group = self.get(groupId); + _.each(group.aggregateValues, function (value, field) { + if (!(field in sums)) { + sums[field] = 0; + } + sums[field] += value; + }); + }); + + // Calculate percentages + _.each(list.data, function (groupId) { + var group = self.get(groupId), + aggregatePercentages = {}; + _.each(_.keys(sums), function (field) { + var percentage = 0; + if (sums[field]) { + percentage = (group.aggregateValues[field] / sums[field]) * 100; + } + aggregatePercentages[field] = percentage; + }); + var dp = self.localData[groupId]; + dp.aggregatePercentages = aggregatePercentages; + }); + return list; + }); + return res; + }, + }); +}); diff --git a/web_group_by_percentage/static/src/scss/backend.scss b/web_group_by_percentage/static/src/scss/backend.scss new file mode 100644 index 00000000..60215903 --- /dev/null +++ b/web_group_by_percentage/static/src/scss/backend.scss @@ -0,0 +1,3 @@ +.web_group_by_percentage { + font-weight: bold; +} diff --git a/web_group_by_percentage/templates/assets.xml b/web_group_by_percentage/templates/assets.xml new file mode 100644 index 00000000..89be0949 --- /dev/null +++ b/web_group_by_percentage/templates/assets.xml @@ -0,0 +1,14 @@ + + + + + +