Skip to content

Commit

Permalink
[FIX] empty balance_column field
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Amaral committed Jun 27, 2024
1 parent 11e18fa commit 50fbb8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import itertools
import logging
import re
from datetime import datetime
from decimal import Decimal
from io import StringIO
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 50fbb8e

Please sign in to comment.