Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix: support CAMT.052 intraday reports in CAMT importer (#416)

Hoovercraft24337c2dbafb
diff --git a/backend/app/services/import_service.py b/backend/app/services/import_service.py
index 7e8d9c77..f26144c5 100644
--- a/backend/app/services/import_service.py
+++ b/backend/app/services/import_service.py
@@ -184,7 +184,7 @@ def parse_qif(content: bytes) -> list[TransactionImport]:
def parse_camt(content: bytes) -> list[TransactionImport]:
- """Parse CAMT.053 (ISO 20022) XML file content and return transactions."""
+ """Parse CAMT.052/CAMT.053 (ISO 20022) XML file content and return transactions."""
root = ET.fromstring(content)
# Detect namespace dynamically
@@ -213,9 +213,26 @@ def parse_camt(content: bytes) -> list[TransactionImport]:
transactions = []
- # Navigate: Document > BkToCstmrStmt > Stmt > Ntry
- for stmt in findall(root, 'BkToCstmrStmt/Stmt'):
+ # Navigate: Document > BkToCstmrStmt > Stmt > Ntry (CAMT.053, end-of-day statement)
+ # Fallback: Document > BkToCstmrAcctRpt > Rpt > Ntry (CAMT.052, intraday report —
+ # same Ntry sub-schema, different root/container element). Several European banks,
+ # including German Volksbanken/Raiffeisenbanken, only offer CAMT.052 exports.
+ stmts = findall(root, 'BkToCstmrStmt/Stmt') or findall(root, 'BkToCstmrAcctRpt/Rpt')
+ for stmt in stmts:
for ntry in findall(stmt, 'Ntry'):
+ # Entry status: BOOK (final) vs. PDNG/INFO (pending/informational).
+ # CAMT.052 intraday reports commonly include PDNG entries for
+ # transactions that haven't settled yet; the same transaction is
+ # reported again as BOOK once it settles. Skip anything that
+ # isn't BOOK to avoid importing it twice. Status is either a
+ # plain code (<Sts>BOOK</Sts>) or wrapped (<Sts><Cd>BOOK</Cd></Sts>)
+ # depending on the schema version. Check the wrapped form first:
+ # in pretty-printed XML the <Sts> element's own text is the
+ # whitespace before <Cd>, which would otherwise mask the real code.
+ status = find_text(ntry, 'Sts/Cd') or find_text(ntry, 'Sts')
+ if status and status.strip().upper() != 'BOOK':
+ continue
+
# Amount
amt_el = find(ntry, 'Amt')
if amt_el is None:
diff --git a/backend/tests/test_import_service.py b/backend/tests/test_import_service.py
index 636e4a40..ba3afe7c 100644
--- a/backend/tests/test_import_service.py
+++ b/backend/tests/test_import_service.py
@@ -739,6 +739,91 @@ class TestParseCamt:
assert transactions[0].description == "No NS"
assert transactions[0].amount == Decimal("300.00")
+ def test_parse_camt052_bktocstmracctrpt(self):
+ """CAMT.052 (intraday report) uses BkToCstmrAcctRpt/Rpt instead of
+ BkToCstmrStmt/Stmt. Several European banks — including German
+ Volksbanken/Raiffeisenbanken — only offer CAMT.052 exports, not .053.
+ """
+ xml = (
+ '<?xml version="1.0" encoding="UTF-8"?>'
+ '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.08">'
+ '<BkToCstmrAcctRpt><Rpt>'
+ '<Ntry>'
+ '<Amt Ccy="EUR">42.50</Amt>'
+ '<CdtDbtInd>DBIT</CdtDbtInd>'
+ '<BookgDt><Dt>2026-07-14</Dt></BookgDt>'
+ '<NtryDtls><TxDtls><RmtInf><Ustrd>Supermarket</Ustrd></RmtInf></TxDtls></NtryDtls>'
+ '</Ntry>'
+ '</Rpt></BkToCstmrAcctRpt>'
+ '</Document>'
+ ).encode('utf-8')
+ transactions = parse_camt(xml)
+ assert len(transactions) == 1
+ assert transactions[0].description == "Supermarket"
+ assert transactions[0].amount == Decimal("42.50")
+ assert transactions[0].type == "debit"
+ assert transactions[0].date == date(2026, 7, 14)
+
+ def test_parse_camt052_skips_pending_entries(self):
+ """CAMT.052 intraday reports can include PDNG (pending) entries for
+ transactions that haven't settled yet. These must be skipped, since
+ the same transaction reappears as BOOK once it settles - importing
+ both would create a duplicate.
+ """
+ xml = (
+ '<?xml version="1.0" encoding="UTF-8"?>'
+ '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.08">'
+ '<BkToCstmrAcctRpt><Rpt>'
+ '<Ntry>'
+ '<Amt Ccy="EUR">42.50</Amt>'
+ '<CdtDbtInd>DBIT</CdtDbtInd>'
+ '<Sts>PDNG</Sts>'
+ '<BookgDt><Dt>2026-07-14</Dt></BookgDt>'
+ '<NtryDtls><TxDtls><RmtInf><Ustrd>Supermarket (pending)</Ustrd></RmtInf></TxDtls></NtryDtls>'
+ '</Ntry>'
+ '<Ntry>'
+ '<Amt Ccy="EUR">42.50</Amt>'
+ '<CdtDbtInd>DBIT</CdtDbtInd>'
+ '<Sts><Cd>BOOK</Cd></Sts>'
+ '<BookgDt><Dt>2026-07-15</Dt></BookgDt>'
+ '<NtryDtls><TxDtls><RmtInf><Ustrd>Supermarket (booked)</Ustrd></RmtInf></TxDtls></NtryDtls>'
+ '</Ntry>'
+ '</Rpt></BkToCstmrAcctRpt>'
+ '</Document>'
+ ).encode('utf-8')
+ transactions = parse_camt(xml)
+ assert len(transactions) == 1
+ assert transactions[0].description == "Supermarket (booked)"
+
+ def test_parse_camt_keeps_pretty_printed_booked_entries(self):
+ """A wrapped <Sts><Cd>BOOK</Cd></Sts> in pretty-printed (indented) XML
+ must still be recognized as BOOK. The <Sts> element's own text is the
+ whitespace before <Cd>, so the status lookup has to prefer Sts/Cd -
+ otherwise the whitespace masks the code and booked entries get skipped.
+ """
+ xml = (
+ '<?xml version="1.0" encoding="UTF-8"?>\n'
+ '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.08">\n'
+ ' <BkToCstmrStmt>\n'
+ ' <Stmt>\n'
+ ' <Ntry>\n'
+ ' <Amt Ccy="EUR">42.50</Amt>\n'
+ ' <CdtDbtInd>DBIT</CdtDbtInd>\n'
+ ' <Sts>\n'
+ ' <Cd>BOOK</Cd>\n'
+ ' </Sts>\n'
+ ' <BookgDt><Dt>2026-07-15</Dt></BookgDt>\n'
+ ' <NtryDtls><TxDtls><RmtInf><Ustrd>Booked</Ustrd></RmtInf></TxDtls></NtryDtls>\n'
+ ' </Ntry>\n'
+ ' </Stmt>\n'
+ ' </BkToCstmrStmt>\n'
+ '</Document>\n'
+ ).encode('utf-8')
+ transactions = parse_camt(xml)
+ assert len(transactions) == 1
+ assert transactions[0].description == "Booked"
+ assert transactions[0].amount == Decimal("42.50")
+
class TestParseOfx:
"""Tests for the parse_ofx function."""