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 _
 
   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)
 
  14         'Identyfikator płatności (IDP)',
 
  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)'
 
  33             ' '.join([obj.postal_code, obj.town]).strip(),
 
  42 def parse_export_feedback(f):
 
  43     lines = csv.reader(StringIO(f.read().decode('cp1250')))
 
  48         yield payment_id, status, comment
 
  51 def bank_order(date, sent_at, queryset):
 
  52     response = HttpResponse(content_type='application/octet-stream')
 
  53     response['Content-Disposition'] = 'attachment; filename=order.PLD'
 
  61         raise ValueError('Payment date not set yet.')
 
  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)
 
  71     if no_dates or no_amounts or cancelled:
 
  74             t += 'Bank acceptance not received for: '
 
  76                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
 
  83             t += 'Amount not set for: '
 
  85                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
 
  88                 for debit in no_amounts
 
  92             t += 'Debits have been cancelled: '
 
  94                 '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
 
  97                 for debit in cancelled
 
 100         raise ValueError(mark_safe(t))
 
 102     for debit in queryset:
 
 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}||",'
 
 109                 date=date.strftime('%Y%m%d'),
 
 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((
 
 123                     debit.street_address,
 
 124                     ' '.join((debit.postal_code, debit.town))
 
 128     response.write('\r\n'.join(rows).encode('cp1250'))