PZ: prevent generationg orders for cancelled debits.
[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, sent_at, 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     cancelled = []
59
60     if date is None:
61         raise ValueError('Payment date not set yet.')
62
63     for debit in queryset:
64         if debit.bank_acceptance_date is None:
65             no_dates.append(debit)
66         if debit.amount is None:
67             no_amounts.append(debit)
68         if debit.cancelled_at and debit.cancelled_at.date() <= date and (sent_at is None or debit.cancelled_at < sent_at):
69             cancelled.append(debit)
70
71     if no_dates or no_amounts or cancelled:
72         t = ''
73         if no_dates:
74             t += 'Bank acceptance not received for: '
75             t += ', '.join(
76                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
77                     debit.pk, debit
78                 )
79                 for debit in no_dates
80             )
81             t += '. '
82         if no_amounts:
83             t += 'Amount not set for: '
84             t += ', '.join(
85                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
86                     debit.pk, debit
87                 )
88                 for debit in no_amounts
89             )
90             t += '. '
91         if cancelled:
92             t += 'Debits have been cancelled: '
93             t += ', '.join(
94                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
95                     debit.pk, debit
96                 )
97                 for debit in cancelled
98             )
99             t += '. '
100         raise ValueError(mark_safe(t))
101
102     for debit in queryset:
103         rows.append(
104             '{order_code},{date},{amount},{dest_bank_id},0,"{dest_iban}","{user_iban}",'
105             '"{dest_addr}","{user_addr}",0,{user_bank_id},'
106             '"/NIP/{dest_nip}/IDP/{payment_id}|/TXT/{payment_desc}||",'
107             '"","","01"'.format(
108                 order_code=210,
109                 date=date.strftime('%Y%m%d'),
110
111                 amount=debit.amount * 100,
112                 dest_bank_id=settings.PZ_IBAN[2:10],
113                 dest_iban=settings.PZ_IBAN,
114                 user_iban=debit.iban,
115                 dest_addr=settings.PZ_ADDRESS_STRING,
116                 user_bank_id=debit.iban[2:10],
117                 dest_nip=settings.PZ_NIP,
118                 payment_id=debit.payment_id,
119                 payment_desc=settings.PZ_PAYMENT_DESCRIPTION,
120                 user_addr = '|'.join((
121                     debit.full_name,
122                     '',
123                     debit.street_address,
124                     ' '.join((debit.postal_code, debit.town))
125                 ))
126             )
127         )
128     response.write('\r\n'.join(rows).encode('cp1250'))
129
130     return response