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