## $Id: utils.py 17187 2022-12-01 06:33:13Z 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, ADMITTED, CLEARANCE, REQUESTED)
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

# 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')

GATEWAY_AMT = 0.0

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

    """

    def selectBed(self, available_beds):
        """Randomly select a bed from the 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 _nce3PaymentMade(self, student, p_level):
    #    if len(student['payments']):
    #        for ticket in student['payments'].values():
    #            if ticket.p_state in ('paid', 'scholarship', 'waived') and \
    #                ticket.p_level == p_level and \
    #                ticket.p_category == 'schoolfee':
    #                return True
    #    return False

    def setPaymentDetails(self, category, student,
            previous_session=None, previous_level=None, combi=[]):
        """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':
            acco_details = self.getAccommodationDetails(student)
            p_session = acco_details['booking_session']
            p_item = acco_details['bt']
            amount = academic_session.booking_fee
        elif category == 'hostel_maintenance':
            amount = 0.0
            booking_session = grok.getSite()['hostels'].accommodation_session
            bedticket = student['accommodation'].get(str(booking_session), None)
            if bedticket is not None and bedticket.bed is not None:
                p_session = booking_session
                p_item = bedticket.bed_coordinates
                #if bedticket.bed.__parent__.maint_fee > 0:
                #    amount = bedticket.bed.__parent__.maint_fee
                if student['studycourse'].current_session < 2022:
                    amount = 6100.0
                else:
                    amount = 10090.0
            else:
                return _(u'No bed space allocated.'), None
            if student.current_mode.endswith('_sw') \
                or student.current_mode == 'pd_ft':
                amount *= 0.625
        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
            if student.state not in (ADMITTED, CLEARANCE, REQUESTED, CLEARED):
                return _(u'Acceptance Fee payments not allowed.'), None
        #elif category == 'third_semester' and student.current_mode == 'nce_ft' \
        #    and p_level in (300, 310, 320, 400, 410, 420):
        #    #if not self._nce3PaymentMade(student, p_level):
        #    #    return _(u'Make NCE 3 school fee payment first.'), None
        #    no_additional_courses = 0
        #    courses_level = student['studycourse'].get(str(p_level))
        #    if courses_level and len(courses_level)> 6:
        #        no_additional_courses = len(courses_level)-6
        #        amount = 2000.0 * no_additional_courses
        elif category.startswith('schoolfee'):
            if category == 'schoolfee_pde1':
                if not student.current_mode.startswith('pd'):
                    return _('You are not a PDE student.'), None
                if student.state != CLEARED:
                        return _('You are not a fresh student.'), None
            if category == 'schoolfee_third':
                if not student.current_mode == 'nce_ft' \
                    or not p_level in (300, 310, 320, 400, 410, 420):
                    return _('You are not an NCE 3 student.'), None
            p_item =  student.certcode
            if not p_item:
                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 new
            elif student.current_mode == 'pd_ft' and student.state == CLEARED:
                amount = 70000
                if category == 'schoolfee_pde1':
                    amount = 40000
            # PDE
            elif student.current_mode == 'pd_ft':
                amount = 35300

            #Short Duration ICT Programs
            elif p_item in ('CCO','DPMTS','DTPGD') and \
                 student.state == CLEARED:
                amount = 15000
            elif p_item in ('ADTPGD','ADPMTS') and \
                 student.state == CLEARED:
                amount = 16000
            elif p_item in ('ADPMTSI','ADTPGDI','DPMTSI','DTPGDI') and \
                 student.state == CLEARED:
                amount = 25000
            elif p_item in ('ADPMTSA','ADTPGDA') and \
               student.state == CLEARED:
                amount = 35000

            # UG
            elif student.current_mode == 'ug_ft':
                if student.state == CLEARED:
                    amount = 87200
                # Introducing repeater fee for 'ug_ft' for 1st time
                # on 15/03/2016
                elif student.current_verdict == 'O':
                    amount = 70700
                else:
                    amount = 70700
            # NCE
            elif not student.current_mode.endswith('_sw'):
                # in 2022 NCE school fees have encreased
                increment_2022 = 11000
                # PRENCE
                if student.current_level == 10 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 24500
                    else:
                        amount = 25000
                # NCE I fresh
                elif student.current_level == 100 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 28000
                    else:
                        amount = 28500
                    # SIWES Fee
                    if student.depcode in ('AGE', 'BED', 'FAA', 'HEC', 'CSC', 'MUS') \
                       or student.certcode in ('NCECHECSC', 
                                               'NCEBIOCSC', 
                                               'NCEPHYCSC', 
                                               'NCEPHECSC',
                                               'NCETHAMUS', 
                                               'NCEANFMUS', 
                                               'NCEHISMUS', 
                                               'NCEIGBMUS', 
                                               'NCESPCCSC',
                                               ):
                        amount += 3000
                # NCE II
                elif student.current_level in (100, 110, 120) and \
                    student.state == RETURNING:
                    if student.depcode in ARTS:
                        amount = 24500
                    else:
                        amount = 25000
                # NCE III third semester
                elif category == 'schoolfee_third':
                    amount = 7000
                    increment_2022 = 20125
                # NCE III
                elif student.current_level in (200, 210, 220):
                    if student.depcode in ARTS:
                        amount = 15375
                    else:
                        amount = 15875
                    increment_2022 = 20125
                # NCE III repeater
                elif student.current_level in (300, 310, 320) and \
                    student.current_verdict == 'O':
                    if student.depcode in ARTS:
                        amount = 13475
                    else:
                        amount = 13975
                    increment_2022 = 20125
                # NCE III spillover
                elif student.current_level in (300, 310, 320) and \
                    student.current_verdict == 'B':
                    if student.depcode in ARTS:
                        amount = 13475
                    else:
                        amount = 13975
                    increment_2022 = 20125
                # NCE III second spillover
                elif student.current_level in (400, 410, 420) and \
                    student.current_verdict == 'B':
                    if student.depcode in ARTS:
                        amount = 13475
                    else:
                        amount = 13975
                    increment_2022 = 20125
                
                if student.entry_session > 2021:
                    amount += increment_2022
            else:
                # NCE I fresh sw
                if student.current_level == 100 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 23100
                    else:
                        amount = 23600
                # NCE II fresh sw
                elif student.current_level == 200 and student.state == CLEARED:
                    if student.depcode in ARTS:
                        amount = 19000
                    else:
                        amount = 19500
                # 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 = 21500
                # 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
        else:
            fee_name = category + '_fee'
            amount = getattr(academic_session, fee_name, 0.0)
            
        if amount in (0.0, None):
            return _(u'Amount could not be determined.'), None

        # Add session and level specific penalty fee.
        if category == 'schoolfee' and student.current_mode in (
            'ug_ft', 'de_ft') and student.state != CLEARED:
            amount += academic_session.penalty_ug_ft
        elif category == 'schoolfee' and student.current_mode in (
            'nce_ft',) and student['studycourse'].previous_verdict != 'O':
            # NCE I fresh
            if student.current_level == 100 and student.state == CLEARED:
                amount += academic_session.penalty_nce1_ft
            # NCE II
            elif student.current_level in (100, 110, 120) and \
                student.state == RETURNING:
                amount += academic_session.penalty_nce2_ft
            # NCE III (except repeaters and spillovers)
            elif student.current_level in (200, 210, 220):
                amount += academic_session.penalty_nce3_ft
        elif category == 'schoolfee' and student.current_mode in ('nce_sw',
            'nce_pt') and student['studycourse'].previous_verdict != 'O':
            # NCE I fresh
            if student.current_level == 100 and student.state == CLEARED:
                amount += academic_session.penalty_nce1_pt
            # NCE II
            elif student.current_level in (100, 110, 120) and \
                student.state == RETURNING:
                amount += academic_session.penalty_nce2_pt
            # NCE III (except repeaters and spillovers)
            elif student.current_level in (200, 210, 220):
                amount += academic_session.penalty_nce3_pt
        elif category == 'schoolfee' and student.current_mode in ('prence',):
            amount += academic_session.penalty_prence
        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 _('This category of payments has been 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
        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'
        if certificate.study_mode == 'ug_ft':
            special_handling = 'ugft'
        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
        return d

    #def warnCreditsOOR(self, studylevel, course=None):
    #    """Return message if credits are out of range.
    #    """
    #    # adding a course ticket
    #    if course:
    #        if course.semester == 1:
    #            if studylevel.total_credits_s1 + course.credits > 24:
    #                return _('Maximum credits in 1st semester exceeded.')
    #        if course.semester == 2:
    #            if studylevel.total_credits_s2 + course.credits > 24:
    #                return _('Maximum credits in 2nd semester exceeded.')
    #    # registering course list
    #    else:
    #        if studylevel.total_credits_s1 > 24:
    #            return _('Maximum credits in 1st semester exceeded.')
    #        if studylevel.total_credits_s1 < 18:
    #            return _('Minimum credits in 1st semester not reached.')
    #        if studylevel.total_credits_s2 > 24:
    #            return _('Maximum credits in 2nd semester exceeded.')
    #        if studylevel.total_credits_s2 < 18:
    #            return _('Minimum credits in 2nd semester not reached.')
    #    return

    def warnCreditsOOR(self, studylevel, course=None):
        """Return message if credits are out of range. 
        """
        # adding a course ticket
        if course:
            if studylevel.total_credits + course.credits > 52:
                return _('Maximum credits exceeded.')
        # registering course list
        else:
            if studylevel.total_credits > 52:
                return _('Maximum credits exceeded.')
            if studylevel.__parent__.previous_verdict == 'O':
                return
            if studylevel.total_credits_s1 < 18:
                return _('Minimum credits in 1st semester not reached.')
            if studylevel.total_credits_s2 < 18:
                return _('Minimum credits in 2nd semester not reached.')
        return

    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\n'
                'admitted into ${a}\n'
                '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)')

    def _isPaymentDisabled(self, p_session, category, student):
        academic_session = self._getSessionConfiguration(p_session)
        if category == 'schoolfee':
            if 'sf_all' in academic_session.payment_disabled:
                return True
            if 'sf_nce1' in academic_session.payment_disabled and \
                student.current_level == 100 and student.state == CLEARED and \
                student.current_mode == 'nce_ft':
                return True
        if category == 'clearance':
            if 'cl_except_ug' in academic_session.payment_disabled and \
                student.current_mode != 'ug_ft':
                return True
        return False

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

    ACCOMMODATION_SPAN = 1

    # FCEOkene prefix
    STUDENT_ID_PREFIX = u'K'
