Merge remote-tracking branch 'zawadzki/new-design'
[wolnelektury.git] / src / pz / bank.py
1 import csv
2 from io import StringIO
3 from django.conf import settings
4 from django.http import HttpResponse
5 from django.utils.safestring import mark_safe
6 from django.utils.translation import ugettext_lazy as _
7
8
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)
13
14     for obj in queryset:
15         writer.writerow([
16             obj.payment_id,
17             obj.full_name,
18             '',
19             obj.street_address,
20             ' '.join([obj.postal_code, obj.town]).strip(),
21             obj.iban[2:10],
22             obj.iban,
23             settings.PZ_NIP,
24             'OF'
25         ])
26     return response
27
28
29 def parse_export_feedback(f):
30     lines = csv.reader(StringIO(f.read().decode('cp1250')))
31     for line in lines:
32         payment_id = line[0]
33         status = int(line[8])
34         comment = line[9]
35         yield payment_id, status, comment
36
37
38 def bank_order(date, sent_at, queryset):
39     response = HttpResponse(content_type='application/octet-stream')
40     response['Content-Disposition'] = 'attachment; filename=order.PLD'
41     rows = []
42
43     no_dates = []
44     no_amounts = []
45     cancelled = []
46
47     if date is None:
48         raise ValueError('Payment date not set yet.')
49
50     for debit in queryset:
51         if debit.bank_acceptance_date is None:
52             no_dates.append(debit)
53         if debit.amount is None:
54             no_amounts.append(debit)
55         if debit.cancelled_at and debit.cancelled_at.date() <= date and (sent_at is None or debit.cancelled_at < sent_at):
56             cancelled.append(debit)
57
58     if no_dates or no_amounts or cancelled:
59         t = ''
60         if no_dates:
61             t += 'Bank acceptance not received for: '
62             t += ', '.join(
63                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
64                     debit.pk, debit
65                 )
66                 for debit in no_dates
67             )
68             t += '. '
69         if no_amounts:
70             t += 'Amount not set for: '
71             t += ', '.join(
72                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
73                     debit.pk, debit
74                 )
75                 for debit in no_amounts
76             )
77             t += '. '
78         if cancelled:
79             t += 'Debits have been cancelled: '
80             t += ', '.join(
81                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
82                     debit.pk, debit
83                 )
84                 for debit in cancelled
85             )
86             t += '. '
87         raise ValueError(mark_safe(t))
88
89     for debit in queryset:
90         rows.append(
91             '{order_code},{date},{amount},{dest_bank_id},0,"{dest_iban}","{user_iban}",'
92             '"{dest_addr}","{user_addr}",0,{user_bank_id},'
93             '"/NIP/{dest_nip}/IDP/{payment_id}|/TXT/{payment_desc}||",'
94             '"","","01"'.format(
95                 order_code=210,
96                 date=date.strftime('%Y%m%d'),
97
98                 amount=debit.amount * 100,
99                 dest_bank_id=settings.PZ_IBAN[2:10],
100                 dest_iban=settings.PZ_IBAN,
101                 user_iban=debit.iban,
102                 dest_addr=settings.PZ_ADDRESS_STRING,
103                 user_bank_id=debit.iban[2:10],
104                 dest_nip=settings.PZ_NIP,
105                 payment_id=debit.payment_id,
106                 payment_desc=settings.PZ_PAYMENT_DESCRIPTION,
107                 user_addr = '|'.join((
108                     debit.full_name,
109                     '',
110                     debit.street_address,
111                     ' '.join((debit.postal_code, debit.town))
112                 ))
113             )
114         )
115     response.write('\r\n'.join(rows).encode('cp1250'))
116
117     return response