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