Skip to content

Commit

Permalink
[IMP] account_statement_import_camt: multi account support
Browse files Browse the repository at this point in the history
The module account_statement_import_file supports handling files
containing informations related to multiple accounts, but this was not
taken advantage of by account_statement_import_camt.

We update the structure returned by parser.parse() to use the expected
format in case of multiple accounts encountered in CAMT file.

This has the side effect of fixing a bug in the previous implementation,
when all the transactions in a CAMT file would be parsed as being on the
same account (the one on the last statement found in the file), and
possibly even more wrong, in the currency of the last statement).
  • Loading branch information
gurneyalex committed Feb 5, 2024
1 parent 401b855 commit 204da98
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions account_statement_import_camt/models/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2013-2016 Therp BV <https://therp.nl>
# Copyright 2017 Open Net Sàrl
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from collections import defaultdict
import re

from lxml import etree
Expand Down Expand Up @@ -444,7 +445,7 @@ def parse(self, data):
raise ValueError("Not a valid xml file, or not an xml file at all.")
ns = root.tag[1 : root.tag.index("}")]
self.check_version(ns, root)
statements = []
statements = defaultdict(list)
currency = None
account_number = None
for node in root[0][1:]:
Expand All @@ -454,5 +455,5 @@ def parse(self, data):
currency = statement.pop("currency")
if "account_number" in statement:
account_number = statement.pop("account_number")
statements.append(statement)
return currency, account_number, statements
statements[(currency, account_number)].append(statement)
return [(currency, account_number, statement_list) for (currency, account_number), statement_list in statements.items()]

0 comments on commit 204da98

Please sign in to comment.