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