[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) <nim@odoo.com>
This commit is contained in:
Nicolas Martinelli
2020-11-24 16:02:46 +00:00
parent d4892f26ea
commit 4dcbb1e62f
3 changed files with 27 additions and 22 deletions
+21 -21
View File
@@ -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'],
])
+5 -1
View File
@@ -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
@@ -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);
},