## $Id: utils.py 8294 2012-04-27 06:32:43Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import grok
from waeup.kofa.students.workflow import CLEARED, RETURNING
from waeup.kofa.students.utils import StudentsUtils
from waeup.kofa.students.interfaces import IStudentsUtils
from waeup.kofa.accesscodes import create_accesscode
from waeup.uniben.interfaces import MessageFactory as _

def get_school_fee(student):
    study_mode = student['studycourse'].certificate.study_mode
    entry_mode = student['studycourse'].entry_mode
    state = student.state
    #lga = student.lga
    lga = 'nothing'
    current_level = student['studycourse'].current_level

    if study_mode.endswith('_ft'):
        # fresh
        if state == CLEARED:
            return 40000.0
        # returning
        elif state == RETURNING:
            return 20000.0
        else:
            return 0.0
    else:
        return 0.0

def actions_after_student_payment(student, payment, view):
    if payment.p_category == 'clearance':
        # Create CLR access code
        pin, error = create_accesscode('CLR',0,student.student_id)
        if error:
            view.flash(_('Valid callback received. ${a}',
                mapping = {'a':error}))
            return
        payment.ac = pin
    elif payment.p_category == 'schoolfee':
        # Create SFE access code
        pin, error = create_accesscode('SFE',0,student.student_id)
        if error:
            view.flash(_('Valid callback received. ${a}',
                mapping = {'a':error}))
            return
        payment.ac = pin
    elif payment.p_category == 'bed_allocation':
        # Create HOS access code
        pin, error = create_accesscode('HOS',0,student.student_id)
        if error:
            view.flash(_('Valid callback received. ${a}',
                mapping = {'a':error}))
            return
        payment.ac = pin
    view.flash(_('Valid callback received.'))
    return

class CustomStudentsUtils(StudentsUtils):
    """A collection of customized methods.

    """
    grok.implements(IStudentsUtils)

    # not yet changed
    def getReturningData(self, student):
        """ This method defines what happens after school fee payment
        depending on the student's senate verdict.

        In the base configuration current level is always increased
        by 100 no matter which verdict has been assigned.
        """
        new_level = student['studycourse'].current_level + 100
        new_session = student['studycourse'].current_session + 1
        return new_session, new_level

    def getPaymentDetails(self, category, student):
        d = {}
        d['p_item'] = u''
        d['amount'] = 0.0
        d['error'] = u''
        d['p_session'] = student['studycourse'].current_session
        session = str(d['p_session'])
        d['p_level'] = student['studycourse'].current_level
        try:
            academic_session = grok.getSite()['configuration'][session]
        except KeyError:
            d['error'] = _(u'Session configuration object is not available.')
            return d
        if category == 'transfer':
            d['amount'] = academic_session.transfer_fee
        elif category == 'gown':
            d['amount'] = academic_session.gown_fee
        elif category == 'bed_allocation':
            d['amount'] = academic_session.booking_fee
        elif category == 'hostel_maintenance':
            d['amount'] = academic_session.maint_fee
        elif category == 'clearance':
            d['p_item'] = student['studycourse'].certificate.code
            if d['p_item'] in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET',
                'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'):
                d['amount'] = 17250.0
            else:
                d['amount'] = 34250.0
        elif category == 'schoolfee':
            d['amount'] = get_school_fee(student)
            code = student['studycourse'].certificate.code
            d['p_item'] = code
            # In case of school fee payment the payment session and level
            # contain the values of the session the student has paid for
            d['p_session'], d['p_level'] = self.getReturningData(student)
        if d['amount'] == 0.0:
            d['error'] = _(u'Amount could not be determined.')
        return d

    VERDICTS_DICT = {
        '0': 'not yet',
        'A': 'Successful student',
        'B': 'Student with carryover courses',
        'C': 'Student on probation',
        'D': 'Withdrawn from the faculty',
        'E': 'Student who were previously on probation',
        'F': 'Medical case',
        'G': 'Absent from examination',
        'H': 'Withheld results',
        'I': 'Expelled/rusticated/suspended student',
        'J': 'Temporary withdrawn from the university',
        'K': 'Unregistered student',
        'L': 'Referred student',
        'M': 'Reinstatement',
        'N': 'Student on transfer',
        'O': 'NCE-III repeater',
        'Y': 'No previous verdict',
        'X': 'New 300 level student',
        'Z': 'Successful student (provisional)',
        'A1': 'First Class',
        'A2': 'Second Class Upper',
        'A3': 'Second Class Lower',
        'A4': 'Third Class',
        'A5': 'Pass',
        'A6': 'Distinction',
        'A7': 'Credit',
        'A8': 'Merit',
        }

    SEPARATORS_DICT = {
        'form.fst_sit_fname': _(u'First Sitting Record'),
        'form.scd_sit_fname': _(u'Second Sitting Record'),
        'form.alr_fname': _(u'Advanced Level Record'),
        'form.hq_type': _(u'Higher Education Record'),
        'form.hq2_type': _(u'Second Higher Education Record'),
        'form.nysc_year': _(u'NYSC Information'),
        'form.employer': _(u'Employment History'),
        'form.uniben_matric': _(u'Former Uniben Student'),
        }