fix(import): read comma-decimal amounts in QIF files

This commit is contained in:
Jay Hemnani committed 2026-11-24 06:48:56 +05:30
1 parent 6d56958deb
commit e8f1478c6d
2 files changed
+47 -2

No files matched your search

+13 -2
View File
@@ -721,6 +721,18 @@ class ImportService:
return mapped_data
@staticmethod
def _parse_qif_amount(value: str) -> Decimal:
# QIF does not declare a number format. Digit groups have three digits, so a
# comma followed by one or two final digits can only be a decimal mark
# ("-19,78", "1.234,56"); otherwise commas separate thousands.
value = value.strip()
if re.search(r",\d{1,2}$", value):
value = value.replace(".", "").replace(",", ".")
else:
value = value.replace(",", "")
return Decimal(value)
@staticmethod
def _prepare_numeric_value(
value: str, thousand_separator: str, decimal_separator: str
@@ -1204,8 +1216,7 @@ class ImportService:
elif code == "T":
try:
cleaned_value = value.replace(",", "")
amount = Decimal(cleaned_value)
amount = self._parse_qif_amount(value)
if amount < 0:
current_transaction["type"] = Transaction.Type.EXPENSE
current_transaction["amount"] = abs(amount)
@@ -507,3 +507,37 @@ POK
self.assertEqual(run.successful_rows, 0)
self.assertEqual(run.skipped_rows, 1)
self.assertFalse(Transaction.objects.exists())
def test_import_comma_decimal_amounts(self):
content = """!Type:Bank
D05/01/2015
T-19,78
MCoffee
^
D06/01/2015
T-1.234,56
MRent
^
D07/01/2015
T1,234.56
MSalary
^
D08/01/2015
T2,500
MBonus
^
"""
self._import_qif(content, account=self.account)
amounts = {
t.description: (t.type, t.amount) for t in Transaction.objects.all()
}
self.assertEqual(
amounts,
{
"Coffee": (Transaction.Type.EXPENSE, Decimal("19.78")),
"Rent": (Transaction.Type.EXPENSE, Decimal("1234.56")),
"Salary": (Transaction.Type.INCOME, Decimal("1234.56")),
"Bonus": (Transaction.Type.INCOME, Decimal("2500")),
},
)