[FIX] web: show decimal part of floats in exported xlsx
If the decimal separator of the currently selected language is a comma, exporting data in an xlsx would use a wrong float format Steps to reproduce: 1. Install Invoicing 2. Go to Settings > Languages, add 'French / Français' language and switch to it 3. Go to Facturation > Fournisseurs > Factures 4. Export the data (there should be at least one amount with a decimal part) 5. The decimal part of the amounts is not displayed Solution: Always use the same decimal separator to print in the xlsx as we can only use a dot as a decimal separator (the comma is used as the thousand separator). The value will then be displayed to the user according to his OS regional settings (see https://xlsxwriter.readthedocs.io/format.html#number-formats-in-different-locales) Problem: When using a comma for the float format, we actually specify the format of the integral part of the number (the thousands) without displaying the decimal part (which is represented with the dot) opw-2965984 closes odoo/odoo#102255 X-original-commit: 21efa4f18266a5c5c57157035c2357ed3e147a69 Signed-off-by: Julien Castiaux <juc@odoo.com> Signed-off-by: Guillaume Merlin (megu) <megu@odoo.com>
This commit is contained in:
@@ -6,6 +6,7 @@ from unittest.mock import patch
|
||||
|
||||
from odoo import http
|
||||
from odoo.tests import common, tagged
|
||||
from odoo.tools.misc import get_lang
|
||||
from odoo.addons.web.controllers.export import ExportXlsxWriter
|
||||
from odoo.addons.mail.tests.common import mail_new_test_user
|
||||
|
||||
@@ -373,3 +374,20 @@ class TestGroupedExport(XlsxCreatorCase):
|
||||
[' 1000.0 (1)', '1000.000'],
|
||||
['3', '1000.00'],
|
||||
])
|
||||
|
||||
def test_decimal_separator(self):
|
||||
""" The decimal separator of the language used shouldn't impact the float representation in the exported xlsx """
|
||||
get_lang(self.env).decimal_point = ','
|
||||
get_lang(self.env).thousands_sep = '.'
|
||||
|
||||
values = [
|
||||
{'int_sum': 1, 'float_min': 86420.864},
|
||||
]
|
||||
export = self.export(values, fields=['int_sum', 'float_min'], params={'groupby': ['int_sum', 'float_min']})
|
||||
|
||||
self.assertExportEqual(export, [
|
||||
['Int Sum' ,'Float Min'],
|
||||
['1 (1)' ,'86420.86'],
|
||||
[' 86420.864 (1)','86420.86'],
|
||||
['1' ,'86420.86'],
|
||||
])
|
||||
|
||||
@@ -17,7 +17,7 @@ from odoo import http
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.http import content_disposition, request
|
||||
from odoo.tools import lazy_property, osutil, pycompat
|
||||
from odoo.tools.misc import xlsxwriter, get_lang
|
||||
from odoo.tools.misc import xlsxwriter
|
||||
from odoo.tools.translate import _
|
||||
|
||||
|
||||
@@ -183,11 +183,10 @@ class ExportXlsxWriter:
|
||||
self.datetime_style = self.workbook.add_format({'text_wrap': True, 'num_format': 'yyyy-mm-dd hh:mm:ss'})
|
||||
self.worksheet = self.workbook.add_worksheet()
|
||||
self.value = False
|
||||
decimal_separator = get_lang(request.env).decimal_point
|
||||
self.float_format = f'0{decimal_separator}00'
|
||||
self.float_format = '#,##0.00'
|
||||
decimal_places = [res['decimal_places'] for res in
|
||||
request.env['res.currency'].search_read([], ['decimal_places'])]
|
||||
self.monetary_format = f'0{decimal_separator}{max(decimal_places or [2]) * "0"}'
|
||||
self.monetary_format = f'#,##0.{max(decimal_places or [2]) * "0"}'
|
||||
|
||||
if row_count > self.worksheet.xls_rowmax:
|
||||
raise UserError(_('There are too many rows (%s rows, limit: %s) to export as Excel 2007-2013 (.xlsx) format. Consider splitting the export.') % (row_count, self.worksheet.xls_rowmax))
|
||||
|
||||
Reference in New Issue
Block a user