model = models.BankExportFeedbackLine
extra = 0
+class BankPaymentInline(admin.TabularInline):
+ model = models.Payment
+ extra = 0
@admin.register(models.DirectDebit)
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())
@admin.register(models.BankExportFeedback)
class BankExportFeedbackAdmin(admin.ModelAdmin):
- inlines = [BankExportFeedbackLineInline]
+ inlines = [BankExportFeedbackLineInline, BankPaymentInline]
import csv
+from datetime import datetime
from io import StringIO
from django.conf import settings
from django.http import HttpResponse
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]
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'
--- /dev/null
+# 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')),
+ ],
+ ),
+ ]
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):
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(
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)