pz admin fix
[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     writer.writerow([
14         'Identyfikator płatności (IDP)',
15         'Nazwa Płatnika',
16         '',
17         'Adres Płatnika Ulica + numer domu',
18         'Adres Płatnika kod+miejscowość',
19         'Numer kierunkowy banku Płatnika',
20         'Numer rachunku bankowego Płatnika',
21         'Identyfikator Odbiorcy (NIP Odbiorcy)',
22         'Osobowość prawna Płatnika (Osoba fizyczna)'
23     ])
24
25     # TODO: ansi encoding
26
27     for obj in queryset:
28         writer.writerow([
29             obj.payment_id,
30             obj.full_name,
31             '',
32             obj.street_address,
33             ' '.join([obj.postal_code, obj.town]).strip(),
34             obj.iban[2:10],
35             obj.iban,
36             settings.PZ_NIP,
37             'OF'
38         ])
39     return response
40
41
42 def parse_export_feedback(f):
43     lines = csv.reader(StringIO(f.read().decode('cp1250')))
44     for line in lines:
45         payment_id = line[0]
46         status = int(line[8])
47         comment = line[9]
48         yield payment_id, status, comment
49
50
51 def bank_order(date, queryset):
52     response = HttpResponse(content_type='application/octet-stream')
53     response['Content-Disposition'] = 'attachment; filename=order.PLD'
54     rows = []
55
56     no_dates = []
57     no_amounts = []
58
59     if date is None:
60         raise ValueError('Payment date not set yet.')
61
62     for debit in queryset:
63         if debit.bank_acceptance_date is None:
64             no_dates.append(debit)
65         if debit.amount is None:
66             no_amounts.append(debit)
67
68     if no_dates or no_amounts:
69         t = ''
70         if no_dates:
71             t += 'Bank acceptance not received for: '
72             t += ', '.join(
73                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
74                     debit.pk, debit
75                 )
76                 for debit in no_dates
77             )
78             t += '. '
79         if no_amounts:
80             t += 'Amount not set for: '
81             t += ', '.join(
82                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
83                     debit.pk, debit
84                 )
85                 for debit in no_amounts
86             )
87             t += '. '
88         raise ValueError(mark_safe(t))
89
90     for debit in queryset:
91         rows.append(
92             '{order_code},{date},{amount},{dest_bank_id},0,"{dest_iban}","{user_iban}",'
93             '"{dest_addr}","{user_addr}",0,{user_bank_id},'
94             '"/NIP/{dest_nip}/IDP/{payment_id}|/TXT/{payment_desc}||",'
95             '"","","01"'.format(
96                 order_code=210,
97                 date=date.strftime('%Y%m%d'),
98
99                 amount=debit.amount * 100,
100                 dest_bank_id=settings.PZ_IBAN[2:10],
101                 dest_iban=settings.PZ_IBAN,
102                 user_iban=debit.iban,
103                 dest_addr=settings.PZ_ADDRESS_STRING,
104                 user_bank_id=debit.iban[2:10],
105                 dest_nip=settings.PZ_NIP,
106                 payment_id=debit.payment_id,
107                 payment_desc=settings.PZ_PAYMENT_DESCRIPTION,
108                 user_addr = '|'.join((
109                     debit.full_name,
110                     '',
111                     debit.street_address,
112                     ' '.join((debit.postal_code, debit.town))
113                 ))
114             )
115         )
116     response.write('\r\n'.join(rows).encode('cp1250'))
117
118     return response