From 50fbb8ede4c83d5df5b64ededa01764886f69779 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 --- .../account_statement_import_sheet_parser.py | 20 ++++++++++++++----- .../test_account_statement_import_txt_xlsx.py | 3 +-- 2 files changed, 16 insertions(+), 7 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 1d56b6ab87..3d1fb7dc85 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 @@ -61,7 +62,6 @@ def parse(self, data_file, mapping, filename): lines = self._parse_lines(mapping, data_file, currency_code) if not lines: return currency_code, account_number, [{"transactions": []}] - lines = list(sorted(lines, key=lambda line: line["timestamp"])) first_line = lines[0] last_line = lines[-1] @@ -280,12 +280,14 @@ def _decimal(column_name): return {} if isinstance(timestamp, str): - timestamp = datetime.strptime(timestamp, mapping.timestamp_format) + if not (timestamp == "" and description): + timestamp = datetime.strptime(timestamp, mapping.timestamp_format) - if balance: + if balance or balance == 0.0: balance = self._parse_decimal(balance, mapping) else: - balance = None + if not balance: + balance = 0.0 if debit_credit: amount = amount.copy_abs() @@ -434,8 +436,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 685807378c..166147cffb 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()