From 4dcbb1e62fdef1192fd6d3e97f9530fcb9dfa04b Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Fri, 20 Nov 2020 11:14:23 +0000 Subject: [PATCH] [FIX] web: grouped export - Create a product with: Cost: 60.80 Quantity On Hand: 999.0 - Go to Inventory / Reporting / Inventory Report - Export as XLS The header values have too many decimals: 60739.2000000007 The root cause is `convert_to_cache` returns this value: https://github.com/odoo/odoo/blob/042298f8c949fba470eda6ad90f94c95ca291030/odoo/fields.py#L1333 In this case, `currency.round()` keeps the extra digits. Since the field is not stored, the useless digits are kept. A simple solution is to use `float_repr` on the non-stored float fields to make sure that doesn't happen. Another solution could be to not convert the floats to strings, but that doesn't seem intended. opw-2378895 closes odoo/odoo#62265 X-original-commit: 2ebcbb16f113dee1416097b7a52f8068a40a34cf Signed-off-by: Nicolas Martinelli (nim) --- addons/test_xlsx_export/tests/test_export.py | 42 +++++++++---------- addons/web/controllers/main.py | 6 ++- .../web/static/src/js/widgets/data_export.js | 1 + 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/addons/test_xlsx_export/tests/test_export.py b/addons/test_xlsx_export/tests/test_export.py index 5ba8c0a0de5..22b759e7940 100644 --- a/addons/test_xlsx_export/tests/test_export.py +++ b/addons/test_xlsx_export/tests/test_export.py @@ -116,13 +116,13 @@ class TestGroupedExport(XlsxCreatorCase): self.assertExportEqual(export, [ ['Int Sum' ,'Float Min'], - ['10 (2)' ,'111.0'], - [' 111.0 (1)','111.0'], + ['10 (2)' ,'111.00'], + [' 111.0 (1)','111.00'], ['10' ,'111.0'], - [' 222.0 (1)','222.0'], + [' 222.0 (1)','222.00'], ['10' ,'222.0'], - ['20 (1)' ,'333.0'], - [' 333.0 (1)','333.0'], + ['20 (1)' ,'333.00'], + [' 333.0 (1)','333.00'], ['20' ,'333.0'], ]) @@ -136,13 +136,13 @@ class TestGroupedExport(XlsxCreatorCase): self.assertExportEqual(export, [ ['Int Sum' ,'Float Avg'], - ['10 (2)' ,'150.0'], - [' 100.0 (1)','100.0'], + ['10 (2)' ,'150.00'], + [' 100.0 (1)','100.00'], ['10' ,'100.0'], - [' 200.0 (1)','200.0'], + [' 200.0 (1)','200.00'], ['10' ,'200.0'], - ['20 (1)' ,'300.0'], - [' 300.0 (1)','300.0'], + ['20 (1)' ,'300.00'], + [' 300.0 (1)','300.00'], ['20' ,'300.0'], ]) @@ -157,14 +157,14 @@ class TestGroupedExport(XlsxCreatorCase): self.assertExportEqual(export, [ ['Int Sum' ,'Float Avg'], - ['10 (3)' ,'300.0'], - [' 20 (1)' ,'600.0'], - [' 600.0 (1)','600.0'], + ['10 (3)' ,'300.00'], + [' 20 (1)' ,'600.00'], + [' 600.0 (1)','600.00'], ['10' ,'600.0'], - [' 30 (2)' ,'150.0'], - [' 100.0 (1)','100.0'], + [' 30 (2)' ,'150.00'], + [' 100.0 (1)','100.00'], ['10' ,'100.0'], - [' 200.0 (1)','200.0'], + [' 200.0 (1)','200.00'], ['10' ,'200.0'], ]) @@ -179,12 +179,12 @@ class TestGroupedExport(XlsxCreatorCase): self.assertExportEqual(export, [ ['Int Sum' ,'Float Avg'], - ['10 (3)' ,'0.0'], - [' 20 (1)' ,'0.0'], - [' Undefined (1)','0.0'], + ['10 (3)' ,'0.00'], + [' 20 (1)' ,'0.00'], + [' Undefined (1)','0.00'], ['10' ,'0.0'], - [' 30 (2)' ,'0.0'], - [' Undefined (2)','0.0'], + [' 30 (2)' ,'0.00'], + [' Undefined (2)','0.00'], ['10' ,'0.0'], ['10' ,'0.0'], ]) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 4682842314f..f1095773a6d 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -34,7 +34,7 @@ import odoo import odoo.modules.registry from odoo.api import call_kw, Environment from odoo.modules import get_module_path, get_resource_path -from odoo.tools import image_process, topological_sort, html_escape, pycompat, ustr, apply_inheritance_specs, lazy_property +from odoo.tools import image_process, topological_sort, html_escape, pycompat, ustr, apply_inheritance_specs, lazy_property, float_repr from odoo.tools.mimetypes import guess_mimetype from odoo.tools.translate import _ from odoo.tools.misc import str2bool, xlsxwriter, file_open @@ -843,6 +843,10 @@ class GroupExportXlsxWriter(ExportXlsxWriter): for field in self.fields[1:]: # No aggregates allowed in the first column because of the group title column += 1 aggregated_value = aggregates.get(field['name']) + # Non-stored float fields may not be displayed properly because of float representation + # => we force 2 digits + if not field.get('store') and isinstance(aggregated_value, float): + aggregated_value = float_repr(aggregated_value, 2) self.write(row, column, str(aggregated_value if aggregated_value is not None else ''), self.header_bold_style) return row + 1, 0 diff --git a/addons/web/static/src/js/widgets/data_export.js b/addons/web/static/src/js/widgets/data_export.js index 61b2cffe862..85c6886ea0b 100644 --- a/addons/web/static/src/js/widgets/data_export.js +++ b/addons/web/static/src/js/widgets/data_export.js @@ -119,6 +119,7 @@ var DataExport = Dialog.extend({ let exportedFields = this.defaultExportFields.map(field => ({ name: field, label: this.record.fields[field].string, + store: this.record.fields[field].store, })); this._exportData(exportedFields, 'xlsx', false); },