X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/d25a3a166a0ec2e730a126c23f8855876384607c..ce097db0af8c96297501b569ec49477b76c1ceec:/src/pz/bank.py?ds=inline diff --git a/src/pz/bank.py b/src/pz/bank.py index 683767281..99f7d85a8 100644 --- a/src/pz/bank.py +++ b/src/pz/bank.py @@ -1,28 +1,15 @@ import csv +from datetime import datetime from io import StringIO from django.conf import settings from django.http import HttpResponse from django.utils.safestring import mark_safe -from django.utils.translation import ugettext_lazy as _ def bank_export(modeladmin, request, queryset): response = HttpResponse(content_type='text/csv; charset=cp1250') response['Content-Disposition'] = 'attachment; filename=export.csv' writer = csv.writer(response) - writer.writerow([ - 'Identyfikator płatności (IDP)', - 'Nazwa Płatnika', - '', - 'Adres Płatnika Ulica + numer domu', - 'Adres Płatnika kod+miejscowość', - 'Numer kierunkowy banku Płatnika', - 'Numer rachunku bankowego Płatnika', - 'Identyfikator Odbiorcy (NIP Odbiorcy)', - 'Osobowość prawna Płatnika (Osoba fizyczna)' - ]) - - # TODO: ansi encoding for obj in queryset: writer.writerow([ @@ -39,22 +26,47 @@ 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: + if not line: continue + 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: + if not line: continue payment_id = line[0] status = int(line[8]) comment = line[9] yield payment_id, status, comment -def bank_order(date, queryset): + + +def bank_order(date, sent_at, queryset): response = HttpResponse(content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=order.PLD' rows = [] no_dates = [] no_amounts = [] + cancelled = [] if date is None: raise ValueError('Payment date not set yet.') @@ -64,8 +76,10 @@ def bank_order(date, queryset): no_dates.append(debit) if debit.amount is None: no_amounts.append(debit) + if debit.cancelled_at and debit.cancelled_at.date() <= date and (sent_at is None or debit.cancelled_at < sent_at): + cancelled.append(debit) - if no_dates or no_amounts: + if no_dates or no_amounts or cancelled: t = '' if no_dates: t += 'Bank acceptance not received for: ' @@ -85,6 +99,15 @@ def bank_order(date, queryset): for debit in no_amounts ) t += '. ' + if cancelled: + t += 'Debits have been cancelled: ' + t += ', '.join( + '{}'.format( + debit.pk, debit + ) + for debit in cancelled + ) + t += '. ' raise ValueError(mark_safe(t)) for debit in queryset: