## $Id: utils.py 8430 2012-05-12 08:43:51Z 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.interfaces 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):
    state = student.state
    fee = None
    if state == CLEARED:
        fee = getattr(student['studycourse'].certificate,'school_fee_1')
    elif state == RETURNING:
        fee = getattr(student['studycourse'].certificate,'school_fee_2')
    if fee is not None:
        return fee
    return 0.0

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

    """
    grok.implements(IStudentsUtils)

    def getReturningData(self, student):
        """ This method defines what happens after school fee payment
        of returning students depending on the student's senate verdict.
        """
        prev_level = student['studycourse'].current_level
        cur_verdict = student['studycourse'].current_verdict
        if cur_verdict in ('A','B','L','M','N','Z',):
            # Successful student
            new_level = divmod(int(prev_level),100)[0]*100 + 100
        elif cur_verdict == 'C':
            # Student on probation
            new_level = int(prev_level) + 10
        else:
            # Student is somehow in an undefined state.
            # Level has to be set manually.
            new_level = prev_level
        new_session = student['studycourse'].current_session + 1
        return new_session, new_level

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

    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'),
        }

    STUDENT_ID_PREFIX = u'B'