2 from datetime import datetime
3 from io import StringIO
4 from django.conf import settings
5 from django.http import HttpResponse
6 from django.utils.safestring import mark_safe
9 def bank_export(modeladmin, request, queryset):
10 response = HttpResponse(content_type='text/csv; charset=cp1250')
11 response['Content-Disposition'] = 'attachment; filename=export.csv'
12 writer = csv.writer(response)
20 ' '.join([obj.postal_code, obj.town]).strip(),
29 def parse_payment_feedback(f):
30 lines = csv.reader(StringIO(f.read().decode('cp1250')))
34 assert line[0] in ('1', '2')
38 booking_date = line[3]
39 booking_date = datetime.strptime(booking_date, '%Y%m%d')
41 is_dd = line[16] == 'DD'
42 realised = line[17] == '1'
43 reject_code = line[18]
44 yield payment_id, booking_date, is_dd, realised, reject_code
49 def parse_export_feedback(f):
51 lines = csv.reader(StringIO(f.read().decode('cp1250')))
57 yield payment_id, status, comment
62 def bank_order(date, sent_at, queryset):
63 response = HttpResponse(content_type='application/octet-stream')
64 response['Content-Disposition'] = 'attachment; filename=order.PLD'
72 raise ValueError('Payment date not set yet.')
74 for debit in queryset:
75 if debit.bank_acceptance_date is None:
76 no_dates.append(debit)
77 if debit.amount is None:
78 no_amounts.append(debit)
79 if debit.cancelled_at and debit.cancelled_at.date() <= date and (sent_at is None or debit.cancelled_at < sent_at):
80 cancelled.append(debit)
82 if no_dates or no_amounts or cancelled:
85 t += 'Bank acceptance not received for: '
87 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
94 t += 'Amount not set for: '
96 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
99 for debit in no_amounts
103 t += 'Debits have been cancelled: '
105 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
108 for debit in cancelled
111 raise ValueError(mark_safe(t))
113 for debit in queryset:
115 '{order_code},{date},{amount},{dest_bank_id},0,"{dest_iban}","{user_iban}",'
116 '"{dest_addr}","{user_addr}",0,{user_bank_id},'
117 '"/NIP/{dest_nip}/IDP/{payment_id}|/TXT/{payment_desc}||",'
120 date=date.strftime('%Y%m%d'),
122 amount=debit.amount * 100,
123 dest_bank_id=settings.PZ_IBAN[2:10],
124 dest_iban=settings.PZ_IBAN,
125 user_iban=debit.iban,
126 dest_addr=settings.PZ_ADDRESS_STRING,
127 user_bank_id=debit.iban[2:10],
128 dest_nip=settings.PZ_NIP,
129 payment_id=debit.payment_id,
130 payment_desc=settings.PZ_PAYMENT_DESCRIPTION,
131 user_addr = '|'.join((
134 debit.street_address,
135 ' '.join((debit.postal_code, debit.town))
139 response.write('\r\n'.join(rows).encode('cp1250'))