## $Id: utils.py 18023 2025-02-13 12:08:21Z 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,
    CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
from kofacustom.unidel.interfaces import MessageFactory as _

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

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

    """

    STUDENT_ID_PREFIX = u'D'

    def warnCreditsOOR(self, studylevel, course=None):
        """
        """
        upper_limit = 53
        #if studylevel.certcode == 'BSCEDBIO' and current_level >= end_level:
        #    studycourse = studylevel.__parent__
        #    certificate = getattr(studycourse,'certificate', None)
        #    current_level = studycourse.current_level
        #    if None in (current_level, certificate):
        #        return
        #    upper_limit = 53
        if course and studylevel.total_credits + course.credits > upper_limit:
            return _('Maximum credits exceeded.')
        if studylevel.total_credits > upper_limit:
            return _('Maximum credits exceeded.')
        if not course:
            #if studylevel.student.faccode in ('FET', 'FES'):
            #    if studylevel.level == 400 and studylevel.total_credits < 29:
            #        return _('Minimum credits not reached.')
            if studylevel.level in (300, 400):
                if studylevel.total_credits < 27:
                    return _('Minimum credits not reached.')
            else:
                if studylevel.total_credits < 30:
                    return _('Minimum credits not reached.')
        return

    def _clearancePaymentMade(self, student):
        if len(student['payments']):
            for ticket in student['payments'].values():
                if ticket.p_state == 'paid' and \
                    ticket.p_category == 'clearance':
                    return True
        return False

    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 student.current_mode == 'ug_ft' and \
                'sf_ugft' in academic_session.payment_disabled:
                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:
                    amount = getattr(certificate, 'school_fee_1', 0.0)
                else:
                    amount = getattr(certificate, 'school_fee_2', 0.0)
            else:
                if student.faccode == 'JUPEB':
                    if not self._clearancePaymentMade(student):
                        return _(u'Acceptance fee must be paid first.'), None
                if student.state == CLEARED:
                    amount = getattr(certificate, 'school_fee_1', 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
                    amount = getattr(certificate, 'school_fee_2', 0.0)
                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
                    amount = getattr(certificate, 'school_fee_2', 0.0)
            # Add surcharges for students in FMED and FBM
            if student.faccode == 'FMED':
                fmed_surcharge = 0.0
                if divmod(p_level,100)[0] == 3:
                    fmed_surcharge = 296000.0
                elif divmod(p_level,100)[0] == 4:
                    fmed_surcharge = 224700.0
                elif divmod(p_level,100)[0] == 5:
                    fmed_surcharge = 220700.0
                elif divmod(p_level,100)[0] == 6:
                    fmed_surcharge = 244950.0
                amount += fmed_surcharge
            if student.faccode == 'FBM':
                fbm_surcharge = 0.0
                if divmod(p_level,100)[0] == 3:
                    fbm_surcharge = 146000.0
                elif divmod(p_level,100)[0] == 4:
                    fbm_surcharge = 204700.0
                elif divmod(p_level,100)[0] == 5:
                    fbm_surcharge = 229950.0
                if student.depcode == 'NSG' and divmod(p_level,100)[0] == 3:
                    fbm_surcharge -= 50000.0
                amount += fbm_surcharge
            # Add non-local surcharges
            if amount and not local(student):
                non_local_surcharge = 0
                if student.faccode not in ('IJMB', 'PRE', 'JUPEB', 'PDED'):
                    non_local_surcharge = 40000
                #if student.faccode in ('FAG', 'FCP', 'FES',
                #                       'FSS', 'FMS', 'FSC',
                #                       'FAT', 'FED'):
                #    if p_level == 200:
                #        non_local_surcharge = 47000
                #    if p_level == 500:
                #        non_local_surcharge = 50000
                #if student.faccode == 'FET':
                #    if p_level == 500:
                #        non_local_surcharge = 31000
                amount += non_local_surcharge
        elif category == 'clearance':
            try:
                p_item = student['studycourse'].certificate.code
            except (AttributeError, TypeError):
                return _('Study course data are incomplete.'), None
            amount = academic_session.clearance_fee
            if student.current_mode in ('ug_ft', 'de_ft'):
                if local(student):
                    amount = academic_session.ugftlocal_clearance_fee
                else:
                    amount = academic_session.ugft_clearance_fee
            elif student.faccode == 'PRE':
                amount = 20000.0
            elif student.faccode == 'JUPEB':
                amount = 25000.0
            elif student.current_mode.startswith('dp'):
                amount = 20000.0
        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
                else:
                    # fallback
                    amount = academic_session.maint_fee
            else:
                return _(u'No bed allocated.'), None
        elif category == 'combi' and combi:
            categories = getUtility(IKofaUtils).COMBI_PAYMENT_CATEGORIES
            for cat in combi:
                fee_name = cat + '_fee'
                cat_amount = getattr(academic_session, fee_name, 0.0)
                if not cat_amount:
                    return _('%s undefined.' % categories[cat]), None
                amount += cat_amount
                p_item += u'%s + ' % categories[cat]
            p_item = p_item.strip(' + ')
        else:
            fee_name = category + '_fee'
            amount = getattr(academic_session, fee_name, 0.0)
        if category != 'bed_allocation' and 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
        payment.p_combi = combi
        return None, payment

    #def checkAccommodationRequirements(self, student, acc_details):
    #    msg = super(CustomStudentsUtils, self).checkAccommodationRequirements(
    #        student, acc_details)
    #    if msg:
    #        return msg
    #    if student.faccode not in ('FLW', 'FMED',):
    #        return _('You are not eligible to book accommodation.')
    #    return

    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
        studycourse = student['studycourse']
        certificate = getattr(studycourse,'certificate',None)
        entry_session = studycourse.entry_session
        current_level = studycourse.current_level
        if None in (entry_session, current_level, certificate):
            return d
        end_level = certificate.end_level
        # Determine bed type
        if entry_session == grok.getSite()['hostels'].accommodation_session:
            bt = 'fr'
        elif current_level >= end_level:
            bt = 'fi'
        else:
            bt = 're'
        #if student.current_level >= 300:
        #    bt = 'na'
        if student.sex == 'f':
            sex = 'female'
        else:
            sex = 'male'
        special_handling = 'regular'
        if student.faccode in ('FLW',):
            special_handling = 'oyibu'
        elif student.faccode in ('FMED',):
            special_handling = 'alero'
        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
        return d

