From a7d1e3cd75cd56c3f9a5ddceb63196aad9d1db65 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 16 May 2022 13:02:40 +0200 Subject: [PATCH] Accept all bank feedbacks --- src/pz/admin.py | 7 ++++-- src/pz/bank.py | 23 ++++++++++++++++++++ src/pz/migrations/0011_payment.py | 26 ++++++++++++++++++++++ src/pz/models.py | 36 ++++++++++++++++++++++++++++++- 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/pz/migrations/0011_payment.py diff --git a/src/pz/admin.py b/src/pz/admin.py index 1373a5687..bc1ee8afd 100644 --- a/src/pz/admin.py +++ b/src/pz/admin.py @@ -54,6 +54,9 @@ class BankExportFeedbackLineInline(admin.TabularInline): model = models.BankExportFeedbackLine extra = 0 +class BankPaymentInline(admin.TabularInline): + model = models.Payment + extra = 0 @admin.register(models.DirectDebit) class DirectDebitAdmin(admin.ModelAdmin): @@ -121,7 +124,7 @@ class DirectDebitAdmin(admin.ModelAdmin): }) ] readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning', 'latest_status'] - inlines = [BankExportFeedbackLineInline] + inlines = [BankExportFeedbackLineInline, BankPaymentInline] def set_bank_submission(m, r, q): q.update(bank_submission_date=now()) @@ -155,7 +158,7 @@ class DirectDebitAdmin(admin.ModelAdmin): @admin.register(models.BankExportFeedback) class BankExportFeedbackAdmin(admin.ModelAdmin): - inlines = [BankExportFeedbackLineInline] + inlines = [BankExportFeedbackLineInline, BankPaymentInline] diff --git a/src/pz/bank.py b/src/pz/bank.py index 0c75c7b53..de22ac0c8 100644 --- a/src/pz/bank.py +++ b/src/pz/bank.py @@ -1,4 +1,5 @@ import csv +from datetime import datetime from io import StringIO from django.conf import settings from django.http import HttpResponse @@ -26,7 +27,27 @@ def bank_export(modeladmin, request, queryset): return response +def parse_payment_feedback(f): + lines = csv.reader(StringIO(f.read().decode('cp1250'))) + for line in lines: + print(line) + assert line[0] in ('1', '2') + if line[0] == '1': + # Totals line. + continue + booking_date = line[3] + booking_date = datetime.strptime(booking_date, '%Y%m%d') + payment_id = line[7] + is_dd = line[16] == 'DD' + realised = line[17] == '1' + reject_code = line[18] + yield payment_id, booking_date, is_dd, realised, reject_code + + + + def parse_export_feedback(f): + # The AU file. lines = csv.reader(StringIO(f.read().decode('cp1250'))) for line in lines: payment_id = line[0] @@ -35,6 +56,8 @@ def parse_export_feedback(f): yield payment_id, status, comment + + def bank_order(date, sent_at, queryset): response = HttpResponse(content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=order.PLD' diff --git a/src/pz/migrations/0011_payment.py b/src/pz/migrations/0011_payment.py new file mode 100644 index 000000000..cef356b50 --- /dev/null +++ b/src/pz/migrations/0011_payment.py @@ -0,0 +1,26 @@ +# Generated by Django 2.2.27 on 2022-05-16 10:58 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('pz', '0010_auto_20220127_1359'), + ] + + operations = [ + migrations.CreateModel( + name='Payment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('booking_date', models.DateField()), + ('is_dd', models.BooleanField()), + ('realised', models.BooleanField()), + ('reject_code', models.CharField(blank=True, max_length=128)), + ('debit', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pz.DirectDebit')), + ('feedback', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pz.BankExportFeedback')), + ], + ), + ] diff --git a/src/pz/models.py b/src/pz/models.py index 87e25fc0e..13afcba4d 100644 --- a/src/pz/models.py +++ b/src/pz/models.py @@ -2,7 +2,7 @@ import re from django.db import models from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ -from .bank import parse_export_feedback +from .bank import parse_export_feedback, parse_payment_feedback class Campaign(models.Model): @@ -139,6 +139,31 @@ class BankExportFeedback(models.Model): def save(self, **kwargs): super().save(**kwargs) + try: + self.save_payment_items() + except AssertionError: + self.save_export_feedback_items() + + def save_payment_items(self): + for payment_id, booking_date, is_dd, realised, reject_code in parse_payment_feedback(self.csv): + debit = DirectDebit.objects.get(payment_id = payment_id) + b, created = self.payment_set.get_or_create( + debit=debit, + defaults={ + 'booking_date': booking_date, + 'is_dd': is_dd, + 'realised': realised, + 'reject_code': reject_code, + } + ) + if not created: + b.booking_date = booking_date + b.is_dd = is_dd + b.realised = realised + b.reject_code = reject_code + b.save() + + def save_export_feedback_items(self): for payment_id, status, comment in parse_export_feedback(self.csv): debit = DirectDebit.objects.get(payment_id = payment_id) b, created = self.bankexportfeedbackline_set.get_or_create( @@ -164,6 +189,15 @@ class BankExportFeedbackLine(models.Model): comment = models.CharField(max_length=255) +class Payment(models.Model): + feedback = models.ForeignKey(BankExportFeedback, models.CASCADE) + debit = models.ForeignKey(DirectDebit, models.CASCADE) + booking_date = models.DateField() + is_dd = models.BooleanField() + realised = models.BooleanField() + reject_code = models.CharField(max_length=128, blank=True) + + class BankOrder(models.Model): payment_date = models.DateField(null=True, blank=True) -- 2.20.1