Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Holvi account statement import handling to use new header names #563

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added utils/holvi-account-test-statement-2025-01.xlsx
Binary file not shown.
21 changes: 15 additions & 6 deletions utils/holvitoolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ def parse_account_statement(uploaded_file: InMemoryUploadedFile):
been uodated to handle both file types.

Expected fields:
"Date"/"Payment date", "Amount", "Currency", "Counterparty", "Description", "Reference",
"Value date", "Booking date", "Amount", "Currency", "Counterparty", "Description", "Reference",
"Message", "Filing ID"

Unused fields:
"Execution date" after "Payment date"
"""
sheet = load_workbook(filename=BytesIO(uploaded_file.read())).active

date_fields = ["Payment date", "Date"]
date_fields = ["Value date", "Payment date", "Date"]
date_found = False
date_error = None
headers = []
items = []
for row_index, row in enumerate(sheet.values):
Expand All @@ -44,7 +43,17 @@ def parse_account_statement(uploaded_file: InMemoryUploadedFile):
item = dict(zip(headers, list(row)))

# Parse payment date
item["Date_parsed"] = dateparser.parse(item["Payment date"])
for field_name in date_fields:
try:
item["Date_parsed"] = dateparser.parse(item[field_name])
date_found = True
break
except KeyError as exc:
date_error = exc

# Reraise last date field not found exception if none found
if not date_found:
raise date_error

if not item["Reference"]:
match = re.search(r"\b0*(\d+)\b", str(item["Message"]))
Expand Down
41 changes: 41 additions & 0 deletions utils/test_dataimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,47 @@ def test_holvi_import_2022_10_format(self):
lastimported.archival_reference, "1924ceba5a3b1c5ffea892fb4850e00d"
)

def test_holvi_import_2025_01_format(self):
"""
Test import with data format from 2025-01-01

Changed field headers.
"""
models.BankTransaction.objects.all().delete()
xls = open("utils/holvi-account-test-statement-2025-01.xlsx", "rb")
name = xls.name
data = xls.read()
res = DataImport.import_holvi(SimpleUploadedFile(name, data))
self.assertDictEqual(
res, {"imported": 11, "exists": 0, "error": 0, "failedrows": []}
)

# and again to test that it found the same rows
res = DataImport.import_holvi(SimpleUploadedFile(name, data))
self.assertDictEqual(
res, {"imported": 0, "exists": 11, "error": 0, "failedrows": []}
)

# quick check for the first imported item data, check the first line of
# holvi-account-test-statement-2025-01.xlsx
firstimported = models.BankTransaction.objects.first()
self.assertEqual(firstimported.date, date(2022, 3, 30))
self.assertEqual(firstimported.amount, Decimal("-135.9"))
self.assertEqual(firstimported.reference_number, "1122002246684")
self.assertEqual(
firstimported.archival_reference, "285ef4ccf4957ea2ba807b961360bf26"
)

# and the last as it has 'Sept' in the dateformat
lastimported = models.BankTransaction.objects.last()
self.assertEqual(lastimported.date, date(2022, 9, 4))
self.assertEqual(lastimported.amount, Decimal("30"))
# note, BankTransaction does not keep leading zeroes
self.assertEqual(lastimported.reference_number, "200047")
self.assertEqual(
lastimported.archival_reference, "1924ceba5a3b1c5ffea892fb4850e00d"
)

def test_holvi_cents(self):
models.BankTransaction.objects.all().delete()
xls = open("utils/holvi-account-test-statement-2022-05.xlsx", "rb")
Expand Down