## Script (Python) "getPaymentsFolderInfo"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: getPaymentsFolderInfo.py 1783 2007-05-15 07:49:50Z henrik $
"""
Info for the PaymentsFolder
"""

mtool = context.portal_membership
if mtool.isAnonymousUser():
    return None


#import logging
#logger = logging.getLogger('Skins.getPaymentsFolderInfo')
def cmp_id(a,b):
    s1 = "%(id)s" % a
    s2 = "%(id)s" % b
    if s1 == s2:
        return 0
    if s1 > s2:
        return 1
    return -1

import DateTime
request = context.REQUEST
students = context.portal_url.getPortalObject().campus.students

student_id = context.getStudentId()
if student_id is None:
    return context.REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url())
#student = getattr(students,student_id)
s_brain = context.students_catalog(id = student_id)[0]

payments = []
#from Products.zdb import set_trace;set_trace()

info = {}
is_so = info['is_so'] = context.isSectionOfficer()

#res = context.portal_catalog(portal_type='Student',id = student_id)
#res = context.students_catalog(id = student_id)
info['review_state'] = s_brain.review_state
session = s_brain.session
info['next_session'], info['next_session_str'] = context.getNextSessionId(session)

info['student_name'] = s_brain.name
payments_path = "%s/campus/students/%s/payments" % (context.portal_url.getPortalPath(),student_id)
sos = context.portal_catalog(container_path=payments_path)
info['is_so'] = is_so

for so in sos:

    row = {}
    row['id'] = so.getId
    row['title'] = so.Title
    url = row['url'] = "%s/%s" % (payments_path,so.getId)
    so_object = context.restrictedTraverse(url,default=None).getContent()
    row['type'] = so.portal_type
    review_state = row['review_state'] = so.review_state
    if so_object.type_description.startswith('School Fee'):
        row['is_approvable'] = is_so and (review_state == "opened") and info['review_state'] == 'cleared_and_validated'
    else:
        row['is_approvable'] = is_so and (review_state == "opened")
    if so_object.type_description.startswith('School Fee'):
        row['is_requeryable'] = (review_state == "opened") and info['review_state'] == 'cleared_and_validated'
    else:
        row['is_requeryable'] = is_so and (review_state == "opened")
    if (review_state == "closed") and so_object.resp_code in ('SC','00','AP','IP',):
        row['confirmed'] = 'active'
    else:
        row['confirmed'] = 'unsuccessful'
    payments.append(row)

payments.sort(cmp_id)
info['payments'] = payments
return info

