## $Id: utils.py 16964 2022-06-14 11:40: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 time import time
from zope.component import createObject, getUtility
from waeup.kofa.interfaces import (IKofaUtils,
    ADMITTED, CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
from kofacustom.dspg.interfaces import MessageFactory as _

def local(student):
    lga = getattr(student, 'lga')
    if lga and lga.startswith('delta'):
        return True
    return False

def MICROSOFT_FEE(student):
    if student.state == CLEARED \
        or (student.current_level in (100, 110, 120, 130)
            and student.current_verdict == 'C') \
        or (student.current_level in (300, 310, 320, 330) \
            and student.current_mode == 'hnd_ft' \
            and student.current_verdict == 'C'):
        return 5950.0
    return 0.0

def DEVELOP_FEE(student):
    if local(student) and student.faccode != 'SPAT':
        return 10000.0
    return 15000.0

def TECH_FEE(student):
    if student.state == CLEARED \
        or (student.current_level in (100, 110, 120, 130)
            and student.current_verdict == 'C') \
        or (student.current_level in (300, 310, 320, 330) \
            and student.current_mode == 'hnd_ft' \
            and student.current_verdict == 'C'):
        return 4250.0
    return 1200.0

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

    # prefix
    STUDENT_ID_PREFIX = u'P'

    def _dep_sug_gns_paymentMade(self, student, session):
        if student.state == RETURNING:
            session += 1
        dep_sug = False
        gns = False
        if len(student['payments']):
            for ticket in student['payments'].values():
                if ticket.p_state == 'paid' and \
                    ticket.p_category == 'dep_sug' and \
                    ticket.p_session == session:
                    dep_sug = True
                if ticket.p_state == 'paid' and \
                    ticket.p_category.startswith('gns') and \
                    ticket.p_session == session:
                    gns = True
                if dep_sug and gns:
                    return True
        return False

    def _lsfp_penalty_paymentMade(self, student, session):
        if student.current_mode not in ('hnd_ft', 'nd_ft'):
            return True
        if len(student['payments']):
            for ticket in student['payments'].values():
                if ticket.p_state == 'paid' and \
                    ticket.p_category == 'lsfp_penalty' and \
                    ticket.p_session == session:
                    return True
        return False

    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
        bt = 'all'
        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 getBedCoordinates(self, bedticket):
        """Translated coordinates:

        Hall Name, Block Letter, Room Number, Bed Letter =
        Hall Name, M/F + Room Number-100, Bed Letter

        Requirements: Gender must be part of bed_coordinates and all hostels
        have only one block with only one floor
        """
        ### ToDo
        return bedticket.bed_coordinates

    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.current_level
        cur_verdict = student.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 _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_spat' in academic_session.payment_disabled and \
                student.faccode == 'SPAT':
                return True
        return False

    def setPaymentDetails(self, category, student,
            previous_session=None, previous_level=None, combi=[]):
        """Create a payment ticket and set the payment data of a
        student for the payment category specified.
        """
        p_item = u''
        amount = 0.0
        if previous_session:
            if previous_session < student['studycourse'].entry_session:
                return _('The previous session must not fall below '
                         'your entry session.'), None
            if category == 'schoolfee':
                # School fee is always paid for the following session
                if previous_session > student['studycourse'].current_session:
                    return _('This is not a previous session.'), None
            else:
                if previous_session > student['studycourse'].current_session - 1:
                    return _('This is not a previous session.'), None
            p_session = previous_session
            p_level = previous_level
            p_current = False
        else:
            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 == 'schoolfee':
            try:
                certificate = student['studycourse'].certificate
                p_item = certificate.code
            except (AttributeError, TypeError):
                return _('Study course data are incomplete.'), None
            if previous_session:
                # Students can pay for previous sessions in all
                # workflow states.  Fresh students are excluded by the
                # update method of the PreviousPaymentAddFormPage.
                if previous_level == 100:
                    if local(student):
                        amount = getattr(certificate, 'school_fee_1', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_3', 0.0)
                else:
                    if local(student):
                        amount = getattr(certificate, 'school_fee_2', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_4', 0.0)
            else:
                # Students are only allowed to pay school fee 
                # if current session dep_sug_gns payment has been made.
                if not self._dep_sug_gns_paymentMade(student, student.current_session):
                    return _('You have to pay NADESU/SA/SUG and GNS Dues first.'), None
                if student.state == CLEARED:
                    if local(student):
                        amount = getattr(certificate, 'school_fee_1', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_3', 0.0)
                elif 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. Payment
                    # session is always next session.
                    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 p_level in (100, 110, 120, 130) or (
                        p_level in (300, 310, 320, 330) and
                        student.current_mode == 'hnd_ft'):
                        # First-year probating ND students or third year
                        # probating hnd students are treated like
                        # fresh students.
                        if local(student):
                            amount = getattr(certificate, 'school_fee_1', 0.0)
                        else:
                            amount = getattr(certificate, 'school_fee_3', 0.0)
                    else:
                        if local(student):
                            amount = getattr(certificate, 'school_fee_2', 0.0)
                        else:
                            amount = getattr(certificate, 'school_fee_4', 0.0)
                    if student.current_mode.endswith('_we') and p_level in (
                        300, 310, 320, 330, 600, 610, 620, 630):
                        amount -= 7000
                elif student.is_postgrad and student.state == PAID:
                    # Returning postgraduate students also pay for the
                    # next session but their level always remains the
                    # same.
                    p_session += 1
                    academic_session = self._getSessionConfiguration(p_session)
                    if academic_session == None:
                        return _(
                            u'Session configuration object is not available.'
                            ), None
                    if local(student):
                        amount = getattr(certificate, 'school_fee_2', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_4', 0.0)
                penalty = getattr(academic_session, 'lsfp_penalty_fee')
                if  penalty and not self._lsfp_penalty_paymentMade(
                    student, student.current_session):
                    return _('You have to pay late school fee payment penalty first.'), None
            if amount:
                amount += MICROSOFT_FEE(student)
                amount += DEVELOP_FEE(student)
                amount += TECH_FEE(student)
        elif category == 'clearance':
            try:
                p_item = student['studycourse'].certificate.code
            except (AttributeError, TypeError):
                return _('Study course data are incomplete.'), None
            if student.current_mode == 'hnd_ft':
                if local(student):
                    amount = academic_session.hndlocal_clearance_fee
                else:
                    amount = academic_session.hnd_clearance_fee
            elif student.current_mode == 'nd_ft':
                if local(student):
                    amount = academic_session.ndlocal_clearance_fee
                else:
                    amount = academic_session.nd_clearance_fee
            elif student.current_mode == 'hnd_pt':
                amount = academic_session.hndpt_clearance_fee
            elif student.current_mode == 'nd_pt':
                amount = academic_session.ndpt_clearance_fee
            else:
                amount = academic_session.clearance_fee
        elif category == 'bed_allocation':
            p_item = self.getAccommodationDetails(student)['bt']
            amount = academic_session.booking_fee
        elif category == 'hostel_maintenance':
            amount = 0.0
            bedticket = student['accommodation'].get(
                str(student.current_session), None)
            if bedticket is not None and bedticket.bed is not None:
                p_item = bedticket.bed_coordinates
                if bedticket.bed.__parent__.maint_fee > 0:
                    amount = bedticket.bed.__parent__.maint_fee
                else:
                    # fallback
                    amount = academic_session.maint_fee
            else:
                return _(u'No bed allocated.'), None
        elif category == 'dep_sug':
            amount = 5150.0 # includes GATEWAY_AMT
            #if student.faccode == 'SPAT':
            #    amount = 1650.0 # includes GATEWAY_AMT
            #    amount = 0.0
            if student.state == RETURNING and not previous_session:
                p_session, p_level = self.getReturningData(student)
        elif category.startswith('gns'):
            if student.state == RETURNING and not previous_session:
                p_session, p_level = self.getReturningData(student)
                fee_name = category + '_fee'
                academic_session = self._getSessionConfiguration(p_session)
                amount = getattr(academic_session, fee_name, 0.0)
            else:
                fee_name = category + '_fee'
                amount = getattr(academic_session, fee_name, 0.0)
        else:
            fee_name = category + '_fee'
            amount = getattr(academic_session, fee_name, 0.0)
        if amount in (0.0, None):
            return _('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 _('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 = amount
        return None, payment

    def warnCreditsOOR(self, studylevel, course=None):
        """DSPG requires total credits on semester
        basis.
        """
        total = [0, 0, 0]
        for ticket in studylevel.values():
            if ticket.outstanding:
                continue
            if not ticket.semester in (1, 2):
                total[ticket.semester] += ticket.credits
            else:
                total[0] += ticket.credits
        if course:
            if course.semester == 1 and total[1] + course.credits > 40:
                return _('Maximum credits (40) in 1st semester exceeded.')
            if course.semester == 2 and total[2] + course.credits > 40:
                return _('Maximum credits (40) in 2nd semester exceeded.')
        else:
            if total[1] > 40:
                return _('Maximum credits (40) in 1st semester exceeded.')
            if total[2] > 40:
                return _('Maximum credits (40) in 2nd semester exceeded.')
        return

    #: A tuple containing names of file upload viewlets which are not shown
    #: on the `StudentClearanceManageFormPage`. Nothing is being skipped
    #: in the base package. This attribute makes only sense, if intermediate
    #: custom packages are being used, like we do for all Nigerian portals.
    SKIP_UPLOAD_VIEWLETS = ('acceptanceletterupload',
                            'higherqualificationresultupload',
                            'advancedlevelresultupload',
                            'evidencenameupload',
                            'refereeletterupload',
                            'statutorydeclarationupload',
                            'firstsittingresultupload',
                            'secondsittingresultupload',
                            'certificateupload',
                            'resultstatementupload',
                            )

    #: A tuple containing the names of registration states in which changing of
    #: passport pictures is allowed.
    PORTRAIT_CHANGE_STATES = (ADMITTED, RETURNING,)

    def constructMatricNumber(self, student):
        #faccode = student.faccode
        depcode = student.depcode
        #certcode = student.certcode
        year = unicode(student.entry_session)[2:]
        if not student.state in (PAID, ) or not student.is_fresh:
            return _('Matriculation number cannot be set.'), None

        # ACC/ND/17/00001
        if student.current_mode == 'nd_ft':
            next_integer = grok.getSite()['configuration'].next_matric_integer
            if next_integer == 0:
                return _('Matriculation number cannot be set.'), None
            return None, "%s/ND/%s/%05d" % (depcode, year, next_integer)

        # ACC/HND/17/00001
        if student.current_mode == 'hnd_ft':
            next_integer = grok.getSite()['configuration'].next_matric_integer_2
            if next_integer == 0:
                return _('Matriculation number cannot be set.'), None
            return None, "%s/HND/%s/%05d" % (depcode, year, next_integer)

        # PT students get the same prefixes but their counter should start
        # with 10001. This is inconsistent because after 9999 ft students
        # the ft and pt number ranges will overlap. Issoufou pointed this
        # out but the director said:
        # "when the counter gets to 9999 for ft, it can start counting
        # along where pt is, at that moment."

        # ACC/ND/17/10001
        if student.current_mode in ('nd_pt, nd_we'):
            next_integer = grok.getSite()['configuration'].next_matric_integer_3
            if next_integer == 0:
                return _('Matriculation number cannot be set.'), None
            return None, "%s/ND/%s/%05d" % (depcode, year, next_integer)
        # ACC/HND/17/10001
        if student.current_mode in ('hnd_pt, hnd_we'):
            next_integer = grok.getSite()['configuration'].next_matric_integer_4
            if next_integer == 0:
                return _('Matriculation number cannot be set.'), None
            return None, "%s/HND/%s/%05d" % (depcode, year, next_integer)

        return _('Matriculation number cannot be set.'), None

    def increaseMatricInteger(self, student):
        """Increase counter for matric numbers.
        """
        if student.current_mode == 'nd_ft':
            grok.getSite()['configuration'].next_matric_integer += 1
            return
        elif student.current_mode == 'hnd_ft':
            grok.getSite()['configuration'].next_matric_integer_2 += 1
            return
        elif student.current_mode in ('nd_pt, nd_we'):
            grok.getSite()['configuration'].next_matric_integer_3 += 1
            return
        elif student.current_mode in ('hnd_pt, hnd_we'):
            grok.getSite()['configuration'].next_matric_integer_4 += 1
            return
        return