## $Id: utils.py 12188 2014-12-10 09:33:27Z 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
import random
from time import time
from zope.component import createObject, getUtility
from waeup.kofa.interfaces import CLEARED, RETURNING, PAID
from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
from waeup.kofa.accesscodes import create_accesscode
from waeup.kofa.interfaces import CLEARED, RETURNING
from waeup.fceokene.interfaces import MessageFactory as _
from waeup.kofa.browser.interfaces import IPDFCreator
from waeup.kofa.students.utils import trans
from waeup.fceokene.interswitch.browser import GATEWAY_AMT

# Very special school fee configuration, should be moved to
# a seperate file.

ARTS = ('CRS','ISS','HIS','MUS','ECO','GEO','POL','SOS','CCA','ECU',
        'THA','GED','GSE','PES','SPC','ENG','FRE','ARB','HAU','IGB',
        'YOR','NCRS','NISS','NHIS','NMUS','NECO','NGEO','NPOL',
        'NCCA','NECU','NTHA','NGED','NGSE','NPES','NSPC','NENG',
        'NFRE','NARB','NHAU','NIGB','NYOR','NSOS')

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

    """

    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 (Uniben)',
        #'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',
        'OPDE': 'PDE repeater',
        }

    def selectBed(self, available_beds):
        """Randomly select a bed from a list of available beds.

        """
        return random.choice(available_beds)

    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 in ('C','O'):
            # 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
        if cur_verdict == 'O':
            new_session = student['studycourse'].current_session
        else:
            new_session = student['studycourse'].current_session + 1
        return new_session, new_level

    def setPaymentDetails(self, category, student,
            previous_session=None, previous_level=None):
        """Create Payment object and set the payment data of a student for
        the payment category specified.

        """
        details = {}
        p_item = u''
        amount = 0.0
        error = u''
        if previous_session:
            return _('Previous session payment not yet implemented.'), None
        p_session = student['studycourse'].current_session
        p_level = student['studycourse'].current_level
        p_current = True
        academic_session = self._getSessionConfiguration(p_session)
        if academic_session == None:
            return _(u'Session configuration object is not available.'), None
        # Determine fee.
        if category == 'transfer':
            amount = academic_session.transfer_fee
        elif category == 'gown':
            amount = academic_session.gown_fee
        elif category == 'bed_allocation':
            amount = academic_session.booking_fee
        elif category == 'hostel_maintenance':
            current_session = student['studycourse'].current_session
            bedticket = student['accommodation'].get(str(current_session), None)
            if bedticket is not None and bedticket.bed is not None:
                p_item = bedticket.bed_coordinates
            else:
                return _(u'You have not yet booked accommodation.'), None
            acc_details = self.getAccommodationDetails(student)
            if current_session != acc_details['booking_session']:
                return _(u'Current session does not match accommodation session.'), None
            if student.current_mode.endswith('_sw') or student.current_mode == 'pd_ft':
                amount = 2500.0 #removed interswitch fee
            else:
                amount = 5000.0 #removed interswitch fee
        elif category == 'clearance':
            amount = academic_session.clearance_fee
            try:
                p_item = student['studycourse'].certificate.code
            except (AttributeError, TypeError):
                return _('Study course data are incomplete.'), None
        elif category == 'third_semester' and student.current_mode == 'nce_ft':
            if student.depcode in ARTS:
                amount = 6835
            else:
                amount = 7763
        elif category == 'schoolfee':
            try:
                certificate = student['studycourse'].certificate
                p_item = certificate.code
            except (AttributeError, TypeError):
                return _('Study course data are incomplete.'), None

            if student.state not in (CLEARED, RETURNING):
                return _('Wrong state.'), None

            # PDE repeater
            if student.current_verdict == 'OPDE':
                amount = 23000
            # PDE
            elif student.current_mode == 'pd_ft':
                amount = 35300
            # UG
            elif student.current_mode == 'ug_ft':
            # Introducing returning students fee for 'ug_ft' for 1st time
            # on 07/01/2014
                if student.state == CLEARED:
                    amount = 65650
                else:
                    amount = 56150
            # NCE
            elif not student.current_mode.endswith('_sw'):
                # PRENCE
                if student.current_level == 10 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 17500
                    else:
                        amount = 18000
                # NCE I fresh
                elif student.current_level == 100 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 14325
                    else:
                        amount = 14825
                # NCE II
                elif student.current_level in (100, 110, 120) and \
                    student.state == RETURNING:
                    if student.depcode in ARTS:
                        amount = 13375
                    else:
                        amount = 13875
                # NCE III
                elif student.current_level in (200, 210, 220):
                    if student.depcode in ARTS:
                        amount = 13375
                    else:
                        amount = 13875
                # NCE III repeater
                elif student.current_level in (300, 310, 320) and \
                    student.current_verdict == 'O':
                    if student.depcode in ARTS:
                        amount = 11475
                    else:
                        amount = 11975
                # NCE III spillover
                elif student.current_level in (300, 310, 320) and \
                    student.current_verdict == 'B':
                    if student.depcode in ARTS:
                        amount = 11975
                    else:
                        amount = 11975
                # NCE III second spillover
                elif student.current_level in (400, 410, 420) and \
                    student.current_verdict == 'B':
                    if student.depcode in ARTS:
                        amount = 11975
                    else:
                        amount = 11975
            else:
                if student.current_level == 100 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 22500
                    else:
                        amount = 23000
                # NCE II sw
                elif student.current_level in (100, 110, 120) and \
                    student.state == RETURNING:
                    if student.depcode in ARTS:
                        amount = 19000
                    else:
                        amount = 19500
                # NCE III sw
                elif student.current_level in (200, 210, 220):
                    if student.depcode in ARTS:
                        amount = 21000
                    else:
                        amount = 21000
                # NCE IV sw
                elif student.current_level in (300, 310, 320):
                    if student.depcode in ARTS:
                        amount = 19000
                    else:
                        amount = 19500
                # NCE V sw
                elif student.current_level in (400, 410, 420):
                    if student.depcode in ARTS:
                        amount = 19000
                    else:
                        amount = 19500
                # NCE V spillover sw
                elif student.current_level in (500, 510, 520) and \
                    student.current_verdict == 'B':
                    if student.depcode in ARTS:
                        amount = 17500
                    else:
                        amount = 18000
                # NCE V second spillover sw
                elif student.current_level in (600, 610, 620) and \
                    student.current_verdict == 'B':
                    if student.depcode in ARTS:
                        amount = 17500
                    else:
                        amount = 18000
            # NCE student payment can be disabled by
            # setting the base school fee to -1
            if academic_session.school_fee_base == -1 and \
                student.current_mode.startswith('nce'):
                return _(u'School fee payment is disabled.'), None
            if student.state == RETURNING:
                # Override p_session and p_level
                p_session, p_level = self.getReturningData(student)
                academic_session = self._getSessionConfiguration(p_session)
                if academic_session == None:
                    return _(u'Session configuration object is not available.'), None

        if amount in (0.0, None):
            return _(u'Amount could not be determined.'), None
        if self.samePaymentMade(student, category, p_item, p_session):
            return _('This type of payment has already been made.'), None
        if self._isPaymentDisabled(p_session, category, student):
            return _('Payment temporarily disabled.'), None
        payment = createObject(u'waeup.StudentOnlinePayment')
        timestamp = ("%d" % int(time()*10000))[1:]
        payment.p_id = "p%s" % timestamp
        payment.p_category = category
        payment.p_item = p_item
        payment.p_session = p_session
        payment.p_level = p_level
        payment.p_current = p_current
        # On June 26, 2013 FCEOkene realized that the Interswitch fee
        # is deducted from their amount. Therefore, we add this fee here.
        payment.amount_auth = float(amount) + GATEWAY_AMT
        return None, payment

    def getAccommodationDetails(self, student):
        """Determine the accommodation data of a student.
        """
        d = {}
        d['error'] = u''
        hostels = grok.getSite()['hostels']
        d['booking_session'] = hostels.accommodation_session
        d['allowed_states'] = hostels.accommodation_states
        d['startdate'] = hostels.startdate
        d['enddate'] = hostels.enddate
        d['expired'] = hostels.expired
        # Determine bed type
        studycourse = student['studycourse']
        certificate = getattr(studycourse,'certificate',None)
        current_level = studycourse.current_level
        if None in (current_level, certificate):
            return d
        end_level = certificate.end_level
        if current_level == 10:
            bt = 'pr'
        elif current_level == 100:
            bt = 'fr'
        elif current_level >= 300:
            bt = 'fi'
        else:
            bt = 're'
        if student.sex == 'f':
            sex = 'female'
        else:
            sex = 'male'
        special_handling = 'regular'
        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
        return d

    def maxCredits(self, studylevel):
        """Return maximum credits.

        """
        return 58

    def getPDFCreator(self, context):
        """Get a pdf creator suitable for `context`.

        The default implementation always returns the default creator.
        """
        mode = getattr(context, 'current_mode', None)
        if mode and mode.startswith('ug'):
            return getUtility(IPDFCreator, name='ibadan_pdfcreator')
        return getUtility(IPDFCreator)

    def _admissionText(self, student, portal_language):
        mode = getattr(student, 'current_mode', None)
        if mode and mode.startswith('ug'):
            text = trans(_(
                'With reference to your application for admission into Bachelor Degree '
                'Programme of the University of Ibadan, this is to inform you that you have '
                'been provisionally admitted to pursue a full-time Bachelor of Arts in '
                'Education Degree Programme as follows:'),
                portal_language)
        else:
            inst_name = grok.getSite()['configuration'].name
            text = trans(_(
                'This is to inform you that you have been provisionally'
                ' admitted into ${a} as follows:', mapping = {'a': inst_name}),
                portal_language)
        return text

    def getBedCoordinates(self, bedticket):
        """Return bed coordinates.

        Bed coordinates are invisible in FCEOkene.
        """
        return _('(see payment slip)')

    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'Advanced Level Record'),
        'form.hq2_type': _(u'Second Higher Education Record'),
        'form.nysc_year': _(u'NYSC Information'),
        'form.employer': _(u'Employment History'),
        'form.former_matric': _(u'Former Student'),
        }

    SKIP_UPLOAD_VIEWLETS = (
        'higherqualificationresultupload',
        'secondHigherqualificationresultupload',
        'certificateupload',
        'secondcertificateupload',
        'thirdcertificateupload',
        'resultstatementupload',
        'secondrefereeletterupload',
        'thirdrefereeletterupload',)

    # FCEOkene prefix
    STUDENT_ID_PREFIX = u'K'
