From 6be52fb718eb9b7a64e787a7a8147f8688ab7b22 Mon Sep 17 00:00:00 2001 From: Yingtong Li Date: Tue, 2 Mar 2021 10:16:09 +1100 Subject: [PATCH] Display ABA errors on export page --- .../jinja2/sstreasury/claim_processing.html | 8 ++- sstreasury/views.py | 62 ++++++++++--------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/sstreasury/jinja2/sstreasury/claim_processing.html b/sstreasury/jinja2/sstreasury/claim_processing.html index d1af607..7569396 100644 --- a/sstreasury/jinja2/sstreasury/claim_processing.html +++ b/sstreasury/jinja2/sstreasury/claim_processing.html @@ -2,7 +2,7 @@ {# Society Self-Service - Copyright © 2018–2020 Yingtong Li (RunasSudo) + Copyright © 2018–2021 Yingtong Li (RunasSudo) This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by @@ -23,6 +23,12 @@ {% block maincontent %}

Claims processing

+ {% if error %} +
+ An error occurred while generating the ABA file: {{ error }} +
+ {% endif %} +
diff --git a/sstreasury/views.py b/sstreasury/views.py index 94cfb8e..95b87ee 100644 --- a/sstreasury/views.py +++ b/sstreasury/views.py @@ -1,5 +1,5 @@ # Society Self-Service -# Copyright © 2018–2020 Yingtong Li (RunasSudo) +# Copyright © 2018–2021 Yingtong Li (RunasSudo) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -680,33 +680,39 @@ def claim_processing(request): claims = [c for c in claims if request.POST.get('claim_{}'.format(c.id), False)] claims.sort(key=lambda c: '{}/{}{}/{}'.format(c.payee_name.strip(), c.payee_bsb.strip()[:3], c.payee_bsb.strip()[-3:], c.payee_account.strip())) - aba_file = io.BytesIO() - - aba.write_descriptive(aba_file, bank_name=settings.ABA_BANK_NAME, user_name=settings.ABA_USER_NAME, bank_code=settings.ABA_BANK_CODE, description='Reimburse', date=timezone.localtime(timezone.now())) - - # CommBank requires only one entry per payee - num_records = 0 - for _, payee_claims in itertools.groupby(claims, key=lambda c: '{}/{}{}/{}'.format(c.payee_name.strip(), c.payee_bsb.strip()[:3], c.payee_bsb.strip()[-3:], c.payee_account.strip())): - payee_claims = list(payee_claims) - aba.write_detail( - aba_file, - dest_bsb=payee_claims[0].payee_bsb, - dest_account=payee_claims[0].payee_account, - cents=sum(c.get_total() for c in payee_claims)*100, - dest_name=payee_claims[0].payee_name[:32], - reference='RE{}'.format(' '.join(str(c.id) for c in payee_claims)), - src_bsb=settings.ABA_SRC_BSB, - src_account=settings.ABA_SRC_ACC, - src_name=settings.ABA_USER_NAME - ) - num_records += 1 - - aba.write_total(aba_file, credit_cents=sum(c.get_total() for c in claims)*100, num_detail_records=num_records) - aba_file.flush() - - response = HttpResponse(aba_file.getvalue(), content_type='text/plain') - response['Content-Disposition'] = 'attachment; filename="claims.aba"' - return response + try: + aba_file = io.BytesIO() + + aba.write_descriptive(aba_file, bank_name=settings.ABA_BANK_NAME, user_name=settings.ABA_USER_NAME, bank_code=settings.ABA_BANK_CODE, description='Reimburse', date=timezone.localtime(timezone.now())) + + # CommBank requires only one entry per payee + num_records = 0 + for _, payee_claims in itertools.groupby(claims, key=lambda c: '{}/{}{}/{}'.format(c.payee_name.strip(), c.payee_bsb.strip()[:3], c.payee_bsb.strip()[-3:], c.payee_account.strip())): + payee_claims = list(payee_claims) + aba.write_detail( + aba_file, + dest_bsb=payee_claims[0].payee_bsb, + dest_account=payee_claims[0].payee_account, + cents=sum(c.get_total() for c in payee_claims)*100, + dest_name=payee_claims[0].payee_name[:32], + reference='RE{}'.format(' '.join(str(c.id) for c in payee_claims)), + src_bsb=settings.ABA_SRC_BSB, + src_account=settings.ABA_SRC_ACC, + src_name=settings.ABA_USER_NAME + ) + num_records += 1 + + aba.write_total(aba_file, credit_cents=sum(c.get_total() for c in claims)*100, num_detail_records=num_records) + aba_file.flush() + + response = HttpResponse(aba_file.getvalue(), content_type='text/plain') + response['Content-Disposition'] = 'attachment; filename="claims.aba"' + return response + except aba.ABAException as ex: + return render(request, 'sstreasury/claim_processing.html', { + 'claims': claims, + 'error': ex + }) if request.POST['action'] == 'ExportXero': #claims = models.ReimbursementClaim.objects.filter(state=models.ClaimState.APPROVED.value).all()