diff --git a/web_pivot_computed_measure/README.rst b/web_pivot_computed_measure/README.rst new file mode 100644 index 000000000..e9b36a989 --- /dev/null +++ b/web_pivot_computed_measure/README.rst @@ -0,0 +1,110 @@ +========================== +Web Pivot Computed Measure +========================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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_pivot_computed_measure + :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_pivot_computed_measure + :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| + +Adds support for computed measures on the pivot view + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Go to pivot view and click on the "Measures" menu, you will see +a new option called 'Computed Measure'. + +You have the follow options to create a 'computed measure': + - Measure 1: Used in 'operation formula' as 'm1' + - Measure 2: Used in 'operation formula' as 'm2' + - Operation: The formula + - Sum: m1 + m2 + - Sub: m1 - m2 + - Mult: m1 * m2 + - Div: m1 / m2 (Format: Float) + - Perc m1 / m2 (Format: Percentage) + - Custom: Special option only visible in debug mode to write a custom formula. + - Name: The name of the new measure (emtpy = auto-generated) + - Format: How will the value be printed + - Integer + - Float + - Percentage (value * 100) + - Formula*: Custom operation formula + These formula is evaluated using 'PY.eval' + +These computed measures can be mixed (You can reuse it to make new computed measures) and saved as favorites. + +Notice that "measures/computed measures" involved in an active 'computed measure' +can't be deactivated until you have deactivate the 'computed measure'. + +Known issues / Roadmap +====================== + +#. Add support to define a style for a computed measure (ex. colored) + +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 +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * Alexandre D. Díaz + * Pedro M. Baeza + +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_pivot_computed_measure/__init__.py b/web_pivot_computed_measure/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/web_pivot_computed_measure/__manifest__.py b/web_pivot_computed_measure/__manifest__.py new file mode 100644 index 000000000..9f7547b95 --- /dev/null +++ b/web_pivot_computed_measure/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2020 Tecnativa - Alexandre Díaz +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +{ + 'name': "Web Pivot Computed Measure", + 'category': "web", + 'version': "12.0.1.0.0", + 'author': "Tecnativa, " + "Odoo Community Association (OCA)", + 'license': 'AGPL-3', + 'website': 'https://github.com/OCA/web', + 'depends': ['web'], + 'data': [ + 'view/assets.xml' + ], + 'qweb': [ + 'static/src/xml/web_pivot_computed_measure.xml', + ], + 'auto_install': False, + 'installable': True, +} diff --git a/web_pivot_computed_measure/i18n/web_pivot_computed_measure.pot b/web_pivot_computed_measure/i18n/web_pivot_computed_measure.pot new file mode 100644 index 000000000..69141cae3 --- /dev/null +++ b/web_pivot_computed_measure/i18n/web_pivot_computed_measure.pot @@ -0,0 +1,148 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_pivot_computed_measure +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:96 +#, python-format +msgid "Add" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:87 +#, python-format +msgid "Can be empty" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:47 +#, python-format +msgid "Computed Measure" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:24 +#, python-format +msgid "Custom" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:18 +#, python-format +msgid "Div (m1 / m2)" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:33 +#, python-format +msgid "Float" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:90 +#, python-format +msgid "Format" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:82 +#, python-format +msgid "Formula" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:30 +#, python-format +msgid "Integer" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:56 +#, python-format +msgid "Measure 1" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:66 +#, python-format +msgid "Measure 2" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:15 +#, python-format +msgid "Mult (m1 * m2)" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:86 +#, python-format +msgid "Name" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:76 +#, python-format +msgid "Operation" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:21 +#, python-format +msgid "Perc (m1 * 100 / m2)" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:36 +#, python-format +msgid "Percentage" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:12 +#, python-format +msgid "Sub (m1 - m2)" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml:9 +#, python-format +msgid "Sum (m1 + m2)" +msgstr "" + +#. module: web_pivot_computed_measure +#. openerp-web +#: code:addons/web_pivot_computed_measure/static/src/js/pivot_model.js:224 +#, python-format +msgid "This measure is currently used by a 'computed measure'. Please, disable the computed measure first." +msgstr "" + diff --git a/web_pivot_computed_measure/readme/CONTRIBUTORS.rst b/web_pivot_computed_measure/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..3ce122c7f --- /dev/null +++ b/web_pivot_computed_measure/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* `Tecnativa `_: + + * Alexandre D. Díaz + * Pedro M. Baeza diff --git a/web_pivot_computed_measure/readme/DESCRIPTION.rst b/web_pivot_computed_measure/readme/DESCRIPTION.rst new file mode 100644 index 000000000..d8bdacf3d --- /dev/null +++ b/web_pivot_computed_measure/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Adds support for computed measures on the pivot view \ No newline at end of file diff --git a/web_pivot_computed_measure/readme/ROADMAP.rst b/web_pivot_computed_measure/readme/ROADMAP.rst new file mode 100644 index 000000000..af507d470 --- /dev/null +++ b/web_pivot_computed_measure/readme/ROADMAP.rst @@ -0,0 +1 @@ +#. Add support to define a style for a computed measure (ex. colored) diff --git a/web_pivot_computed_measure/readme/USAGE.rst b/web_pivot_computed_measure/readme/USAGE.rst new file mode 100644 index 000000000..02f354433 --- /dev/null +++ b/web_pivot_computed_measure/readme/USAGE.rst @@ -0,0 +1,25 @@ +Go to pivot view and click on the "Measures" menu, you will see +a new option called 'Computed Measure'. + +You have the follow options to create a 'computed measure': + - Measure 1: Used in 'operation formula' as 'm1' + - Measure 2: Used in 'operation formula' as 'm2' + - Operation: The formula + - Sum: m1 + m2 + - Sub: m1 - m2 + - Mult: m1 * m2 + - Div: m1 / m2 (Format: Float) + - Perc m1 / m2 (Format: Percentage) + - Custom: Special option only visible in debug mode to write a custom formula. + - Name: The name of the new measure (emtpy = auto-generated) + - Format: How will the value be printed + - Integer + - Float + - Percentage (value * 100) + - Formula*: Custom operation formula + These formula is evaluated using 'PY.eval' + +These computed measures can be mixed (You can reuse it to make new computed measures) and saved as favorites. + +Notice that "measures/computed measures" involved in an active 'computed measure' +can't be deactivated until you have deactivate the 'computed measure'. diff --git a/web_pivot_computed_measure/static/description/icon.png b/web_pivot_computed_measure/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/web_pivot_computed_measure/static/description/icon.png differ diff --git a/web_pivot_computed_measure/static/description/index.html b/web_pivot_computed_measure/static/description/index.html new file mode 100644 index 000000000..f6c627832 --- /dev/null +++ b/web_pivot_computed_measure/static/description/index.html @@ -0,0 +1,476 @@ + + + + + + +Web Pivot Computed Measure + + + +
+

Web Pivot Computed Measure

+ + +

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runbot

+

Adds support for computed measures on the pivot view

+

Table of contents

+ +
+

Usage

+

Go to pivot view and click on the “Measures” menu, you will see +a new option called ‘Computed Measure’.

+
+
You have the follow options to create a ‘computed measure’:
+
    +
  • Measure 1: Used in ‘operation formula’ as ‘m1’
  • +
  • Measure 2: Used in ‘operation formula’ as ‘m2’
  • +
  • +
    Operation: The formula
    +
      +
    • Sum: m1 + m2
    • +
    • Sub: m1 - m2
    • +
    • Mult: m1 * m2
    • +
    • Div: m1 / m2 (Format: Float)
    • +
    • Perc m1 / m2 (Format: Percentage)
    • +
    • Custom: Special option only visible in debug mode to write a custom formula.
    • +
    +
    +
    +
  • +
  • Name: The name of the new measure (emtpy = auto-generated)
  • +
  • +
    Format: How will the value be printed
    +
      +
    • Integer
    • +
    • Float
    • +
    • Percentage (value * 100)
    • +
    +
    +
    +
  • +
  • +
    Formula*: Custom operation formula
    +
    These formula is evaluated using ‘PY.eval’
    +
    +
  • +
+
+
+

These computed measures can be mixed (You can reuse it to make new computed measures) and saved as favorites.

+

Notice that “measures/computed measures” involved in an active ‘computed measure’ +can’t be deactivated until you have deactivate the ‘computed measure’.

+
+
+

Known issues / Roadmap

+
    +
  1. Add support to define a style for a computed measure (ex. colored)
  2. +
+
+
+

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

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:
      +
    • Alexandre D. Díaz
    • +
    • Pedro M. Baeza
    • +
    +
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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_pivot_computed_measure/static/src/js/pivot_controller.js b/web_pivot_computed_measure/static/src/js/pivot_controller.js new file mode 100644 index 000000000..0b2581af0 --- /dev/null +++ b/web_pivot_computed_measure/static/src/js/pivot_controller.js @@ -0,0 +1,145 @@ +/* Copyright 2020 Tecnativa - Alexandre Díaz + * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */ + +odoo.define('web_pivot_computed_measure.PivotController', function (require) { + "use strict"; + + var core = require('web.core'); + var config = require('web.config'); + var PivotController = require('web.PivotController'); + + var QWeb = core.qweb; + + + PivotController.include({ + custom_events: { + 'add_measure': '_onAddMeasure', + 'remove_measure': '_onRemoveMeasure', + }, + + computed_measures_open: false, + + /** + * Add the computed measures to the context. This + * will be used when save a filter. + * + * @override + */ + getContext: function () { + var res = this._super.apply(this, arguments); + var state = this.model.get(); + res.pivot_computed_measures = state.computed_measures; + return res; + }, + + /** + * @override + */ + renderButtons: function($node) { + this._super.apply(this, arguments); + if ($node) { + this._renderComputedMeasures(); + } + }, + + /** + * Handle click event on measures menu to support computed measures sub-menu + * + * @override + */ + _onButtonClick: function (event) { + var $target = $(event.target); + if ($target.parents("div[data-id='__computed__']").length) { + var hideMenu = false; + event.preventDefault(); + + if ($target.hasClass('dropdown-item') || $target.hasClass('o_submenu_switcher')) { + this.computed_measures_open = !this.computed_measures_open; + this._renderComputedMeasures(); + } else if ($target.hasClass('o_add_computed_measure')) { + hideMenu = true; + var field1 = this.$buttons_measures_ex.find('#computed_measure_field_1').val(); + var field2 = this.$buttons_measures_ex.find('#computed_measure_field_2').val(); + var oper = this.$buttons_measures_ex.find('#computed_measure_operation').val(); + if (oper === "custom") { + oper = this.$buttons_measures_ex.find('#computed_measure_operation_custom').val(); + } + var name = this.$buttons_measures_ex.find('#computed_measure_name').val(); + var format = this.$buttons_measures_ex.find('#computed_measure_format').val(); + var uniqueId = (new Date()).getTime(); + this.model.createComputedMeasure(uniqueId, field1, field2, oper, name, format) + .then(this.update.bind(this, {}, {reload: false})); + } + + if (!hideMenu) { + event.stopPropagation(); + } + + return; + } + + this._super.apply(this, arguments); + }, + + /** + * Render computed measures menu + */ + _renderComputedMeasures: function() { + if (this.$buttons_measures_ex && this.$buttons_measures_ex.length) { + this.$buttons_measures_ex.remove(); + } + var self = this; + var measures = _.sortBy(_.pairs(_.omit(this.measures, '__count')), function (x) { return x[1].string.toLowerCase(); }); + this.$buttons_measures_ex = $(QWeb.render('web_pivot_computed_measure.ExtendedMenu', { + isOpen: this.computed_measures_open, + debug: config.debug, + measures: measures, + computed_measures: _.map(_.reject(measures, function(item) { return !item[1].__computed_id; }), function(item) { + item[1].active = _.contains(self.model.data.measures, item[0]); + return item; + }), + })); + this.$buttons_measures_ex.find('#computed_measure_operation').on('change', this._onChangeComputedMeasureOperation.bind(this)); + this.$buttons.find('.o_pivot_measures_list').append(this.$buttons_measures_ex); + }, + + /** + * Custom event to add a new measure + * + * @param {CustomEvent} ev + */ + _onAddMeasure: function(ev) { + this.measures[ev.data.id] = ev.data.def; + this._renderComputedMeasures(); + }, + + /** + * Custom event to remove a measure + * + * @param {CustomEvent} ev + */ + _onRemoveMeasure: function(ev) { + delete this.measures[ev.data.id]; + this._renderComputedMeasures(); + }, + + /** + * Set default values related with the selected operation + * + * @param {ChangeEvent} ev + */ + _onChangeComputedMeasureOperation: function(ev) { + var $option = $(ev.target.options[ev.target.selectedIndex]); + if ($(ev.target).val() === "custom") { + this.$buttons_measures_ex.find('#container_computed_measure_operation_custom').removeClass('d-none'); + } else { + var format = $option.data('format'); + if (format) { + this.$buttons_measures_ex.find('#computed_measure_format').val(format); + } + this.$buttons_measures_ex.find('#container_computed_measure_operation_custom').addClass('d-none'); + } + } + }); + +}); diff --git a/web_pivot_computed_measure/static/src/js/pivot_model.js b/web_pivot_computed_measure/static/src/js/pivot_model.js new file mode 100644 index 000000000..3c4f4ab6d --- /dev/null +++ b/web_pivot_computed_measure/static/src/js/pivot_model.js @@ -0,0 +1,256 @@ +/* Copyright 2020 Tecnativa - Alexandre Díaz + * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */ + +odoo.define('web_pivot_computed_measure.PivotModel', function (require) { + "use strict"; + + var core = require('web.core'); + var PivotModel = require('web.PivotModel'); + + var _t = core._t; + + + PivotModel.include({ + _computed_measures: [], + + /** + * Create a new computed measure + * + * @param {string} id + * @param {string} field1 + * @param {string} field2 + * @param {string} operation + * @param {string} name + * @param {string} format + */ + createComputedMeasure: function(id, field1, field2, operation, name, format) { + var measure = _.find(this._computed_measures, function(item) { + return item.field1 === field1 && item.field2 === field2 && item.operation === operation; + }); + if (measure) { + return $.Deferred(function(d) { + d.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.replace( + /m1/g, + fieldM1.__computed_id?"("+fieldM1.string+")":fieldM1.string).replace( + /m2/g, + fieldM2.__computed_id?"("+fieldM2.string+")":fieldM2.string); + var cmTotal = this._computed_measures.push({ + field1: field1, + field2: field2, + operation: oper, + name: name || oper_human, + id: cmId, + format: format, + }); + + return this._createVirtualMeasure(this._computed_measures[cmTotal-1]); + }, + + /** + * Create and enable a measure based on a 'fake' field + * + * @param {Object} cmDef + * @param {List} fields *Optional* + */ + _createVirtualMeasure: function(cmDef, fields) { + var 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 + } + this.trigger_up("add_measure", { + id: cmDef.id, + def: arrFields[cmDef.id], + }); + return this._activeMeasures([cmDef.field1, cmDef.field2, cmDef.id]); + }, + + /** + * @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]; + if (!this._isMeasureEnabled(field)) { + this.data.measures.push(field); + needLoad = true; + } + } + if (needLoad) { + return this._loadData(); + } + return $.Deferred(function(d) { + d.resolve(); + }) + }, + + /** + * @param {String} field + */ + _isMeasureEnabled: function(field) { + return _.contains(this.data.measures, field); + }, + + /** + * Fill the dataPoints with the computed measures values + * + * @override + */ + _mergeData: function (data, comparisonData, groupBys) { + var res = this._super.apply(this, arguments); + var l = groupBys.length; // Cached loop (This is not python! hehe) + for (var index = 0; index < l; ++index) { + if (data.length) { + var l2 = data[index].length; + for (var k = 0; k < l2; ++k) { + var dataPoint = data[index][k]; + if (_.isEmpty(dataPoint)) { + break; + } + var l3 = this._computed_measures.length; + for (var x = 0; x < l3; ++x) { + var cm = this._computed_measures[x]; + if (!this._isMeasureEnabled(cm.id)) { + continue; + } + dataPoint[cm.id] = py.eval(cm.operation, dataPoint); + } + } + } + } + return res; + }, + + /** + * Load the computed measures in context. This is used by filters. + * + * @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]; + params.fields[cmDef.id] = { + type: cmDef.format, + string: cmDef.name, + __computed_id: cmDef.id, + } + 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", { + id: cmDef.id, + def: self.fields[cmDef.id], + }); + } + }); + self._activeMeasures(toActive); + }); + }, + + /** + * Load the computed measures in context. This is used by filters. + * + * @override + */ + reload: function (handle, params) { + if ('context' in params) { + this._computed_measures = params.context.pivot_computed_measures || params.computed_measures || []; + } + var l = this._computed_measures.length; + for (var x = 0; x < l; ++x) { + this._createVirtualMeasure(this._computed_measures[x]); + } + // Clean unused 'fake' fields + var fieldNames = Object.keys(this.fields); + for (var x = 0; x < fieldNames.length; ++x) { + var field = this.fields[fieldNames[x]]; + if (field.__computed_id) { + var 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]); + this.trigger_up("remove_measure", { + id: fieldNames[x], + }); + } + } + } + return this._super.apply(this, arguments); + }, + + /** + * Add the computed measures to the state. This is used by filters. + * + * @override + */ + get: function () { + var res = this._super.apply(this, arguments); + res.computed_measures = this._computed_measures; + return res; + }, + + /** + * Adds a rule to deny that measures can be disabled if are being used by a computed measure. + * In the other hand, when enables a measure analyzes it to active all involved measures. + * + * @override + */ + toggleMeasure: function (field) { + if (this._isMeasureEnabled(field)) { + // Measure is disabled + var umeasures = _.filter(this._computed_measures, function(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.")); + }); + } + } else { + // Mesaure is enabled + var toEnable = []; + var toAnalize = [field]; + while (toAnalize.length) { + var afield = toAnalize.shift(); + var fieldDef = this.fields[afield]; + if (fieldDef.__computed_id) { + var cm = _.find(this._computed_measures, {id:fieldDef.__computed_id}); + toAnalize.push(cm.field1, cm.field2); + var toEnableFields = []; + if (!this.fields[cm.field1].__computed_id) { + toEnableFields.push(cm.field1); + } + if (!this.fields[cm.field2].__computed_id) { + toEnableFields.push(cm.field2); + } + toEnableFields.push(afield); + toEnable.push(toEnableFields); + } + } + if (toEnable.length) { + return this._activeMeasures(_.flatten(toEnable.reverse())); + } + } + return this._super.apply(this, arguments); + }, + }); + +}); diff --git a/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml b/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml new file mode 100644 index 000000000..7cdce42f2 --- /dev/null +++ b/web_pivot_computed_measure/static/src/xml/web_pivot_computed_measure.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + +