Accept all bank feedbacks
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 16 May 2022 11:02:40 +0000 (13:02 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 16 May 2022 11:02:40 +0000 (13:02 +0200)
src/pz/admin.py
src/pz/bank.py
src/pz/migrations/0011_payment.py [new file with mode: 0644]
src/pz/models.py

index 1373a56..bc1ee8a 100644 (file)
@@ -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]
 
 
 
index 0c75c7b..de22ac0 100644 (file)
@@ -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 (file)
index 0000000..cef356b
--- /dev/null
@@ -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')),
+            ],
+        ),
+    ]
index 87e25fc..13afcba 100644 (file)
@@ -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)