# $Id: payment_export.py 5163 2010-04-21 04:38:00Z henrik $
"""
export students who paid for the transfer form
"""

try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass

from Products.AdvancedQuery import Eq, Between, Le,In
request = context.REQUEST
setheader = request.RESPONSE.setHeader
scat = context.students_catalog
fields = ("id",
          "name",
          "matric_no",
          "jamb_reg_no",
#          "sex",
          "email",
#          "phone",
#          "faculty",
#          "department",
          "course",
          "level",
          "session",
          "review_state",
          "entry_session",
          "payment_id",
          "payment_title",
          "indexdata",
          )
res_list = []
lines = []
lines.append(','.join(fields))
format = '"%(' + ')s","%('.join(fields) + ')s"'

portal = context.portal_catalog_real
aq_portal = context.portal_catalog_real.evalAdvancedQuery
payments = aq_portal(Eq('portal_type','Payment') & Eq("review_state","closed"))

#payments = context.portal_catalog(portal_type='Payment')

#return len(payments)

for payment in payments:
    #if not payment.id.startswith('transfer'):
    #    continue
    sid = payment.getPath().split('/')[-3]
    if not sid:
        continue
    erg = scat(id=sid)
    if not erg:
        continue
    d = context.getFormattedStudentEntry(erg[0])
    indexdata = portal.getIndexDataForRID(payment.getRID())
    d['payment_id'] = payment.id
    d['payment_title'] = payment.title
    d['indexdata'] = indexdata['SearchableText']
    lines.append(format % d)

#setheader('Content-type', 'text/x-cvs')
#setheader('Content-type','application/vnd.ms-excel')
setheader('Content-type','text/semicolon-seperated-values')
setheader('Content-Disposition:', 'attachment; filename="transfer_list.csv"')
setheader('Expires',  'Mon, 26 Jul 1997 05:00:00GMT') # Date in the past
setheader('Cache-Control', 'no-store, no-cache,must-revalidate') # HTTP/1.1
setheader('Cache-Control', 'post-check=0,pre-check=0')
setheader('Pragma', 'no-cache') # HTTP/1.0
return '\n'.join(lines)

