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 ceb15f24018..d52febc2913 100644 --- a/addons/web/static/src/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/js/views/pivot/pivot_model.js @@ -16,7 +16,7 @@ odoo.define('web.PivotModel', function (require) { * Basicaly the pivot table presents aggregated values for various groups of records * in one domain. If a comparison is asked for, two domains are considered. * - * Let us consider a simple example and let us fix the vocabulary: + * Let us consider a simple example and let us fix the vocabulary (let us suppose we are in June 2020): * ___________________________________________________________________________________________________________________________________________ * | | Total | * | |_____________________________________________________________________________________________________________________| @@ -24,14 +24,14 @@ odoo.define('web.PivotModel', function (require) { * | |_______________________________________|______________________________________|______________________________________| * | | Sales total | Sales total | Sales total | * | |_______________________________________|______________________________________|______________________________________| - * | | This Month | Last Month | Variation | This Month | Last Month | Variation | This Month | Last Month | Variation | + * | | May 2020 | June 2020 | Variation | May 2020 | June 2020 | Variation | May 2020 | June 2020 | Variation | * |____________________|______________|____________|___________|_____________|____________|___________|_____________|____________|___________| - * | Total | 110 | 85 | 29.4% | 30 | 40 | -25% | 140 | 125 | 12% | - * | Europe | 35 | 25 | 40% | 30 | 40 | -25% | 65 | 65 | 0% | - * | Brussels | 15 | 0 | 100% | 30 | 30 | 0% | 45 | 30 | 50% | - * | Paris | 20 | 25 | -20% | 0 | 10 | -100% | 20 | 35 | -42.8% | - * | North America | 75 | 60 | 25% | | | | 75 | 60 | 25% | - * | Washington | 75 | 60 | 25% | | | | 75 | 60 | 25% | + * | Total | 85 | 110 | 29.4% | 40 | 30 | -25% | 125 | 140 | 12% | + * | Europe | 25 | 35 | 40% | 40 | 30 | -25% | 65 | 65 | 0% | + * | Brussels | 0 | 15 | 100% | 30 | 30 | 0% | 30 | 45 | 50% | + * | Paris | 25 | 20 | -20% | 10 | 0 | -100% | 35 | 20 | -42.8% | + * | North America | 60 | 75 | 25% | | | | 60 | 75 | 25% | + * | Washington | 60 | 75 | 25% | | | | 60 | 75 | 25% | * |____________________|______________|____________|___________|_____________|____________|___________|_____________|____________|___________| * * @@ -49,7 +49,7 @@ odoo.define('web.PivotModel', function (require) { * * The measure is the field 'sales_total'. * - * Two domains are considered: 'This Month' and 'Last Month'. + * Two domains are considered: 'May 2020' and 'June 2020'. * * In the model, * @@ -57,9 +57,9 @@ odoo.define('web.PivotModel', function (require) { * - colGroupBys is the list [sale_team_id] * - measures is the list [sales_total] * - domains is the list [d1, d2] with d1 and d2 domain expressions - * for say sale_date in this month and last month, for instance - * d1 = [['sale_date', >=, 2019-05-01], ['sale_date', '<', 2019-05-31]] - * - origins is the list ['This Month', 'Last Month'] + * for say sale_date in May 2020 and June 2020, for instance + * d1 = [['sale_date', >=, 2020-05-01], ['sale_date', '<=', 2020-05-31]] + * - origins is the list ['May 2020', 'June 2020'] * * DATA: * @@ -116,7 +116,7 @@ odoo.define('web.PivotModel', function (require) { * A given list is thus of the form [f1,..., fi, g1,..., gj] or better [[f1,...,fi], [g1,...,gj]] * * For each list of fields possible and each domain considered, one read_group is done - * and gives results of the form (an exceptions for list []) + * and gives results of the form (an exception for list []) * * g = { * f1: v1, ..., fi: vi, @@ -132,10 +132,10 @@ odoo.define('web.PivotModel', function (require) { * For example, g = { * continent_id: [1, 'Europe'] * sale_team_id: [1, 'Sale Team 1'] - * sales_count: 35, + * sales_count: 25, * __count: 4 * __domain: [ - * ['sale_date', >=, 2019-05-01], ['sale_date', '<', 2019-05-31], + * ['sale_date', >=, 2020-05-01], ['sale_date', '<=', 2020-05-31], * ['continent_id', '=', 1], * ['sale_team_id', '=', 1] * ] @@ -256,9 +256,9 @@ odoo.define('web.PivotModel', function (require) { * * In the example: * { - * "[[], []]": [{'sales_total': 110}, {'sales_total': 85}] (total/total) + * "[[], []]": [{'sales_total': 125}, {'sales_total': 140}] (total/total) * ... - * "[[1, 2], [2]]": [{'sales_total': 0}, {'sales_total': 10}] (Europe/Paris/Sale Team 2) + * "[[1, 2], [2]]": [{'sales_total': 10}, {'sales_total': 0}] (Europe/Paris/Sale Team 2) * ... * } * @@ -765,7 +765,7 @@ var PivotModel = AbstractModel.extend({ return self.measurements[key][originIndex][measure]; }); if (originIndexes.length > 1) { - return computeVariation(values[0], values[1]); + return computeVariation(values[1], values[0]); } else { return values[0]; } @@ -1398,12 +1398,8 @@ var PivotModel = AbstractModel.extend({ _computeDerivedParams: function () { const { range, rangeDescription, comparisonRange, comparisonRangeDescription } = this.data.timeRanges; if (range) { - this.data.domains = [this.data.domain.concat(range)]; - this.data.origins = [rangeDescription]; - if (comparisonRange) { - this.data.domains.push(this.data.domain.concat(comparisonRange)); - this.data.origins.push(comparisonRangeDescription); - } + this.data.domains = [this.data.domain.concat(comparisonRange), this.data.domain.concat(range)]; + this.data.origins = [comparisonRangeDescription, rangeDescription]; } else { this.data.domains = [this.data.domain]; this.data.origins = [""]; diff --git a/addons/web/static/tests/views/pivot_tests.js b/addons/web/static/tests/views/pivot_tests.js index 3cceabb600c..5a47465d642 100644 --- a/addons/web/static/tests/views/pivot_tests.js +++ b/addons/web/static/tests/views/pivot_tests.js @@ -478,9 +478,9 @@ QUnit.module('Views', { assert.hasClass(pivot.$('table'), 'o_enable_linking', "root node should have classname 'o_enable_linking'"); // here we click on the group corresponding to Total/Total/This Month - pivot.$('.o_pivot_cell_value').eq(0).click(); // should trigger a do_action with appropriate domain + pivot.$('.o_pivot_cell_value').eq(1).click(); // should trigger a do_action with appropriate domain // here we click on the group corresponding to xphone/Total/This Month - pivot.$('.o_pivot_cell_value').eq(3).click(); // should trigger a do_action with appropriate domain + pivot.$('.o_pivot_cell_value').eq(4).click(); // should trigger a do_action with appropriate domain unpatchDate(); pivot.destroy(); @@ -2018,7 +2018,7 @@ QUnit.module('Views', { assert.containsN(pivot, '.o_pivot thead tr:last th', 9, "last header row should contains 9 cells (3*[December 2016, November 2016, Variation]"); var values = [ - "13", "0", "100%", "0", "19", "-100%", "13", "19", "-31.58%" + "19", "0", "-100%", "0", "13", "100%", "19", "13", "-31.58%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); @@ -2026,9 +2026,9 @@ QUnit.module('Views', { await testUtils.dom.click(pivot.$('.o_pivot .o_pivot_header_cell_closed').eq(2)); await testUtils.dom.click(pivot.$('.o_pivot .o_pivot_field_menu a[data-field="product_id"]')); values = [ - "13", "0", "100%", "0", "19", "-100%", "13", "19", "-31.58%", - "12", "0", "100%", "12", "0" , "100%", - "1" , "0", "100%", "0", "19", "-100%", "1" , "19", "-94.74%" + "19", "0", "-100%", "0", "13", "100%", "19", "13", "-31.58%", + "19", "0", "-100%", "0", "1" , "100%", "19", "1", "-94.74%", + "0", "12", "100%", "0" , "12", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); @@ -2036,26 +2036,26 @@ QUnit.module('Views', { await testUtils.dom.click(pivot.$('.o_control_panel div.o_pivot_measures_list a[data-field="foo"]')); await testUtils.dom.click(pivot.$('.o_control_panel div.o_pivot_measures_list a[data-field="product_id"]')); values = [ - "2", "0", "100%", "0", "1", "-100%", "2", "1", "100%", - "1", "0", "100%", "1", "0", "100%", - "1", "0", "100%", "0", "1", "-100%", "1", "1", "0%" + "1", "0", "-100%", "0", "2", "100%", "1", "2", "100%", + "1", "0", "-100%", "0", "1", "100%", "1", "1", "0%", + "0", "1", "100%", "0", "1", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); await testUtils.dom.click(pivot.$('.o_control_panel div.o_pivot_measures_list a[data-field="__count"]')); await testUtils.dom.click(pivot.$('.o_control_panel div.o_pivot_measures_list a[data-field="product_id"]')); values = [ - "2", "0", "100%", "0", "2", "-100%", "2", "2", "0%", - "1", "0", "100%", "1", "0", "100%", - "1", "0", "100%", "0", "2", "-100%", "1", "2", "-50%" + "2", "0", "-100%", "0", "2", "100%", "2", "2", "0%", + "2", "0", "-100%", "0", "1", "100%", "2", "1", "-50%", + "0", "1", "100%", "0", "1", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); await testUtils.dom.clickFirst(pivot.$('.o_pivot .o_pivot_header_cell_opened')); values = [ "2", "2", "0%", - "1", "0", "100%", - "1", "2", "-50%" + "2", "1", "-50%", + "0", "1", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); @@ -2149,12 +2149,12 @@ QUnit.module('Views', { assert.verifySteps([ // col group headers '["Total",""]', - '["December 2016","November 2016"]', + '["November 2016","December 2016"]', // measure headers '["Foo","Foo","Foo"]', // origin headers - '["December 2016","November 2016","Variation","December 2016","November 2016"' + - ',"Variation","December 2016","November 2016","Variation"]', + '["November 2016","December 2016","Variation","November 2016","December 2016"' + + ',"Variation","November 2016","December 2016","Variation"]', // number of 'measures' '1', // number of 'origins' @@ -2217,9 +2217,9 @@ QUnit.module('Views', { await cpHelpers.toggleMenuItem(pivot, 'Date: Previous period'); var values = [ - "4", "0", "100%", - "2", "0", "100%", - "2", "0", "100%" + "0", "4", "100%", + "0", "2", "100%", + "0", "2", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join(',')); assert.strictEqual(pivot.$('.o_pivot_header_cell_closed').length, 3, @@ -2266,66 +2266,66 @@ QUnit.module('Views', { // initial sanity check var values = [ - "12", "17", "-29.41%", "1", "2", "-50%" , "13", "19", "-31.58%", - "12", "0" , "100%", "12", "0" , "100%", - "1", "0", "100%" , "1" , "0" , "100%", - "0" , "17", "-100%", "0" , "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%" + "17", "12", "-29.41%", "2", "1", "-50%", "19", "13", "-31.58%", + "17", "0", "-100%", "17", "0", "-100%", + "2", "0", "-100%", "2", "0", "-100%", + "0", "12" , "100%", "0", "12" , "100%", + "0", "1", "100%" , "0" , "1" , "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); // click on 'Foo' in column Total/Company (should sort by the period of interest, ASC) await testUtils.dom.click(pivot.$('.o_pivot_measure_row').eq(0)); values = [ - "12", "17", "-29.41%", "1", "2", "-50%" , "13", "19", "-31.58%", - "1", "0", "100%" , "1" , "0" , "100%", - "0" , "17", "-100%", "0" , "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%", - "12", "0" , "100%", "12", "0" , "100%" + "17", "12", "-29.41%", "2", "1", "-50%" , "19", "13", "-31.58%", + "2", "0", "-100%", "2", "0", "-100%", + "0", "12", "100%", "0", "12", "100%", + "0", "1", "100%", "0", "1", "100%", + "17", "0", "-100%", "17", "0", "-100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); // click again on 'Foo' in column Total/Company (should sort by the period of interest, DESC) await testUtils.dom.click(pivot.$('.o_pivot_measure_row').eq(0)); values = [ - "12", "17", "-29.41%", "1", "2", "-50%" , "13", "19", "-31.58%", - "12", "0" , "100%", "12", "0" , "100%", - "1", "0", "100%" , "1" , "0" , "100%", - "0" , "17", "-100%", "0" , "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%" + "17", "12", "-29.41%", "2", "1", "-50%", "19", "13", "-31.58%", + "17", "0", "-100%", "17", "0", "-100%", + "2", "0", "-100%", "2", "0", "-100%", + "0", "12", "100%", "0", "12", "100%", + "0", "1", "100%", "0", "1", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); // click on 'This Month' in column Total/Individual/Foo await testUtils.dom.click(pivot.$('.o_pivot_origin_row').eq(3)); values = [ - "12", "17", "-29.41%", "1", "2", "-50%", "13", "19", "-31.58%", - "12", "0", "100%", "12", "0" , "100%", - "0", "17", "-100%", "0", "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%", - "1", "0", "100%", "1" , "0", "100%" + "17", "12", "-29.41%", "2", "1", "-50%", "19", "13", "-31.58%", + "17", "0", "-100%", "17", "0", "-100%", + "0", "12", "100%", "0", "12" , "100%", + "0", "1", "100%", "0", "1", "100%", + "2", "0", "-100%", "2", "0", "-100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); // click on 'Previous Period' in column Total/Individual/Foo await testUtils.dom.click(pivot.$('.o_pivot_origin_row').eq(4)); values = [ - "12", "17", "-29.41%", "1", "2", "-50%", "13", "19", "-31.58%", - "12", "0", "100%", "12", "0" , "100%", - "1", "0", "100%", "1" , "0", "100%", - "0", "17", "-100%", "0", "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%" + "17", "12", "-29.41%", "2", "1", "-50%", "19", "13", "-31.58%", + "17", "0", "-100%", "17", "0", "-100%", + "2", "0", "-100%", "2", "0", "-100%", + "0", "12", "100%", "0", "12", "100%", + "0", "1", "100%", "0", "1", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); // click on 'Variation' in column Total/Foo await testUtils.dom.click(pivot.$('.o_pivot_origin_row').eq(8)); values = [ - "12", "17", "-29.41%", "1", "2", "-50%", "13", "19", "-31.58%", - "0", "17", "-100%", "0", "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%", - "12", "0", "100%", "12", "0" , "100%", - "1", "0", "100%", "1" , "0", "100%" + "17", "12", "-29.41%", "2", "1", "-50%", "19", "13", "-31.58%", + "17", "0", "-100%", "17", "0", "-100%", + "2", "0", "-100%", "2" , "0" , "-100%", + "0", "12", "100%", "0", "12" , "100%", + "0", "1", "100%", "0" , "1", "100%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); @@ -2439,11 +2439,13 @@ QUnit.module('Views', { // initial sanity check var values = [ - "12", "17", "-29.41%", "1", "2", "-50%" , "13", "19", "-31.58%", - "12", "0" , "100%", "12", "0" , "100%", - "1", "0", "100%" , "1" , "0" , "100%", - "0" , "17", "-100%", "0" , "17", "-100%", - "0", "2", "-100%", "0" , "2" , "-100%" + "17", "12", "-29.41%", "2", "1", "-50%", "19", "13", "-31.58%", + "17", "0", "-100%", "17", "0", "-100%", + "2", "0", "-100%", "2", "0", "-100%", + "0", "12", "100%", "0", "12", "100%", + "0", "1", "100%", "0", "1", "100%", + + ]; assert.strictEqual(getCurrentValues(pivot), values.join()); @@ -2451,9 +2453,9 @@ QUnit.module('Views', { await testUtils.dom.click(pivot.$buttons.find('.o_pivot_flip_button')); values = [ - "12", "0", "100%", "1", "0", "100%", "0", "17", "-100%", "0", "2", "-100%", "13", "19", "-31.58%", - "12", "0", "100%", "0", "17", "-100%", "12", "17", "-29.41%", - "1", "0", "100%" , "0", "2", "-100%", "1", "2", "-50%" + "17", "0", "-100%", "2", "0", "-100%", "0", "12", "100%", "0", "1", "100%", "19", "13", "-31.58%", + "17", "0", "-100%", "0", "12", "100%", "17", "12", "-29.41%", + "2", "0", "-100%", "0", "1", "100%", "2", "1", "-50%" ]; assert.strictEqual(getCurrentValues(pivot), values.join()); @@ -2547,7 +2549,7 @@ QUnit.module('Views', { pivot.$('th').slice(0, 7).text(), [ '', 'Total', '', - '2016-12-15', '2016-12-17', '2016-11-22', '2016-11-03', + '2016-11-22', '2016-11-03', '2016-12-15', '2016-12-17', ].join(''), "The col headers should be as expected" );