## Script (Python) "approve_epayment"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: aprove_epayment.py 1425 2007-02-15 17:52:01Z joachim $
"""
"""
import logging
logger = logging.getLogger('Skins.approve_epayment')
import DateTime
if context.portal_membership.isAnonymousUser():
    return None

request = context.REQUEST
students = context.portal_url.getPortalObject().campus.students
wftool = context.portal_workflow
mtool = context.portal_membership
is_anon = mtool.isAnonymousUser()
member = mtool.getAuthenticatedMember()
student_id = context.getStudentId()
is_so = context.isSectionOfficer()
if student_id is None or not is_so:
    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]
session = s_brain.session

pd = {}
doc = context.getContent()
pd['resp_code'] = 'AP'
pd['resp_desc'] = 'Payment approved by %s, %s' % (member,DateTime.DateTime())
pd['status'] = 'paid'
doc.edit(mapping=pd)

if doc.category == 'schoolfee':
    next_info = context.getNextInfo(s_brain)
    next_session_id = next_info['next_session_id']
    next_session_str = next_info['next_session_str']
    next_level_id = next_info['next_level_id']
    next_transition = next_info['next_transition']
    next_verdict = next_info['next_verdict']    
    study_course = getattr(student,'study_course')
    try:
        wftool.doActionFor(study_course,'open')
    except:
        pass
    verdict = s_brain.verdict
    if verdict == 'N/A':
        verdict = ''
    study_course.getContent().edit(mapping= {'current_level': next_level_id,
                                             'current_session': next_session_id,
                                             'current_verdict': next_verdict,
                                             'previous_verdict': verdict,
                                             })
    if next_transition:
        wftool.doActionFor(student,next_transition)

elif doc.category == 'acceptance':
    if context.getStudentReviewState() in ('admitted', 'objection_raised'):
        logger.info('%s paid acceptance fee' % (student_id))
        current = DateTime.DateTime()
        wf = context.portal_workflow
        info = context.getClearanceInfo()
        wftool.doActionFor(student,'enter_clearance_pin')
        context.waeup_tool.changeWorkflowState(info['clear'], 'opened')
        context.waeup_tool.changeWorkflowState(info['app'], 'closed')
        dc = {}
        app_doc = info['app_doc']
        #dc['clr_ac_pin'] = pay_doc.order_id
        dc['clr_ac_date'] = current
        dc['entry_date'] = current
        info['clear_doc'].edit(mapping = dc)
        if info['penalty']:
            logger.info('%s started late clearance' % (info['id']))

logger.info('%s approves epayment for %s' % (member,student_id))
review_state = wftool.getInfoFor(context,'review_state',None)
if review_state == 'opened':
    wftool.doActionFor(context,'close')
return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url())
