From 518f1946675b13d72ef3b3a7f9110a93a121f439 Mon Sep 17 00:00:00 2001 From: Tiago Amaral Date: Wed, 19 Jun 2024 17:30:05 -0300 Subject: [PATCH] [FIX] empty balance_column field --- .../models/account_statement_import_sheet_parser.py | 11 ++++++++++- .../tests/test_account_statement_import_txt_xlsx.py | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py b/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py index 1d56b6ab8..47cd324da 100644 --- a/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py +++ b/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py @@ -4,6 +4,7 @@ import itertools import logging +import re from datetime import datetime from decimal import Decimal from io import StringIO @@ -434,8 +435,16 @@ def _parse_decimal(self, value, mapping): if isinstance(value, Decimal): return value elif isinstance(value, float): - return Decimal(value) + return Decimal(str(value)) thousands, decimal = mapping._get_float_separators() + # Remove all characters except digits, thousands separator, + # decimal separator, and signs + value = ( + re.sub( + r"[^\d\-+" + re.escape(thousands) + re.escape(decimal) + "]+", "", value + ) + or "0" + ) value = value.replace(thousands, "") value = value.replace(decimal, ".") return Decimal(value) diff --git a/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py b/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py index 685807378..166147cff 100644 --- a/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py +++ b/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py @@ -1,7 +1,6 @@ # Copyright 2019 ForgeFlow, S.L. # Copyright 2020 CorporateHub (https://corporatehub.eu) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -import decimal from base64 import b64encode from os import path @@ -516,7 +515,7 @@ def test_skip_empty_lines(self): "sheet_mapping_id": self.sample_statement_map.id, } ) - with self.assertRaises(decimal.InvalidOperation): + with self.assertRaises(ValueError): wizard.with_context( account_statement_import_txt_xlsx_test=True ).import_file_button()