Open-source personal finance manager. Self-hosted, privacy-first.
fix(csv): normalize BRL amounts and fix split column parsing edge cases (#20)
Lucas Wasilewskibe0da6a165f4
diff --git a/backend/app/services/import_service.py b/backend/app/services/import_service.pyindex cce4616d..656989f1 100644--- a/backend/app/services/import_service.py+++ b/backend/app/services/import_service.py@@ -249,8 +249,9 @@ def parse_csv(# Parse amountif use_split:- inflow_str = row.get(inflow_col, '').strip().replace(',', '.').replace('R$', '').strip()- outflow_str = row.get(outflow_col, '').strip().replace(',', '.').replace('R$', '').strip()+ inflow_str = normalize_amount(row.get(inflow_col, ""))+ outflow_str = normalize_amount(row.get(outflow_col, ""))+try:inflow = Decimal(inflow_str) if inflow_str else Decimal('0')except Exception:@@ -269,7 +270,8 @@ def parse_csv(else:continue # Skip rows with no amountelse:- amount_str = row[amount_col].strip().replace(',', '.').replace('R$', '').strip()+ amount_str = normalize_amount(row[amount_col])+try:amount = Decimal(amount_str)except Exception:@@ -367,3 +369,24 @@ async def import_transactions(await session.commit()return imported, skipped, import_log.id++def normalize_amount(amount_str: str) -> str:+ """+ Normalize monetary string into a standard decimal format compatible with Decimal.++ Example:+ 1.442,20 -> 1442.20+ 1,442.20 -> 1442.20+ """++ amount_str = amount_str.replace('R$', '').strip()++ if ',' in amount_str and '.' in amount_str:+ if amount_str.rfind(',') > amount_str.rfind('.'):+ amount_str = amount_str.replace('.', '').replace(',', '.')+ else:+ amount_str = amount_str.replace(',', '')+ elif ',' in amount_str:+ amount_str = amount_str.replace(',', '.')++ return amount_str\ No newline at end of filediff --git a/backend/tests/test_import_service.py b/backend/tests/test_import_service.pyindex 35e396ea..5376758e 100644--- a/backend/tests/test_import_service.py+++ b/backend/tests/test_import_service.py@@ -192,6 +192,19 @@ class TestParseCsv:assert transactions[1].type == "debit"assert transactions[1].amount == Decimal("1200.00")+ def test_parse_csv_brazilian_amount(self):+ """CSV using comma as the decimal separator in the amount field."""+ csv_content = (+ "date,description,amount\n"+ '2026-01-10,SALARY,"5,000.00"\n'+ "2026-01-11,RENT,1200.00\n"+ )++ transactions = parse_csv(csv_content.encode("utf-8"))+ assert len(transactions) == 2+ assert transactions[0].amount == Decimal("5000.00")+ assert transactions[1].amount == Decimal("1200.00")+class TestParseQif:"""Tests for the parse_qif function."""