## $Id: utils.py 13631 2016-01-17 09:29:44Z 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 waeup.kofa.utils.helpers import to_timezone
from waeup.kofa.students.utils import trans
from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
from waeup.uniben.interfaces import MessageFactory as _

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

    """

    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 checkAccommodationRequirements(self, student, acc_details):
        if acc_details.get('expired', False):
            startdate = acc_details.get('startdate')
            enddate = acc_details.get('enddate')
            if startdate and enddate:
                tz = getUtility(IKofaUtils).tzinfo
                startdate = to_timezone(
                    startdate, tz).strftime("%d/%m/%Y %H:%M:%S")
                enddate = to_timezone(
                    enddate, tz).strftime("%d/%m/%Y %H:%M:%S")
                return _("Outside booking period: ${a} - ${b}",
                         mapping = {'a': startdate, 'b': enddate})
            else:
                return _("Outside booking period.")
        if student.current_mode != 'ug_ft':
            return _("Only undergraduate full-time students are eligible to book accommodation.")
        bt = acc_details.get('bt')
        if not bt:
            return _("Your data are incomplete.")
        if not student.state in acc_details['allowed_states']:
            return _("You are in the wrong registration state.")
        if student['studycourse'].current_session != acc_details[
            'booking_session']:
            return _('Your current session does not '
                     'match accommodation session.')
        stage = bt.split('_')[2]
        if stage != 'fr' and not student[
            'studycourse'].previous_verdict in ('A', 'B', 'F', 'J', 'C', 'Z'):
            return _("Your are not eligible to book accommodation.")
        if str(acc_details[
            'booking_session']) in student['accommodation'].keys():
            return _('You already booked a bed space in '
                     'current accommodation session.')
        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
        # Determine bed type
        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
        if current_level == 10:
            bt = 'pr'
        elif entry_session == grok.getSite()['hostels'].accommodation_session:
            bt = 'fr'
        elif current_level >= end_level:
            bt = 'fi'
        else:
            bt = 're'
        if student.sex == 'f':
            sex = 'female'
        else:
            sex = 'male'
        special_handling = 'regular'
        if student.faccode in ('MED', 'DEN'):
            special_handling = 'clinical'
        elif student.certcode in ('BARTMAS', 'BARTTHR', 'BARTFAA',
                                  'BAEDFAA', 'BSCEDECHED'):
            special_handling = 'ekenwan'
        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
        return d

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

    def _isPaymentDisabled(self, p_session, category, student):
        academic_session = self._getSessionConfiguration(p_session)
        if category == 'schoolfee' and \
            'sf_all' in academic_session.payment_disabled:
            return True
        if category == 'hostel_maintenance' and \
            'maint_all' in academic_session.payment_disabled:
            return True
        return False

    #def _hostelApplicationPaymentMade(self, student, session):
    #    if len(student['payments']):
    #        for ticket in student['payments'].values():
    #            if ticket.p_state == 'paid' and \
    #                ticket.p_category == 'hostel_application' and \
    #                ticket.p_session == session:
    #                return True
    #    return False

    def setPaymentDetails(self, category, student,
            previous_session, previous_level):
        """Create Payment object 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 == 'transfer':
            amount = academic_session.transfer_fee
        elif category == 'transcript':
            amount = academic_session.transcript_fee
        elif category == 'gown':
            amount = academic_session.gown_fee
        elif category == 'bed_allocation':
            p_item = self.getAccommodationDetails(student)['bt']
            amount = academic_session.booking_fee
            # Add student union dues
            stage = self.getAccommodationDetails(student)['bt']
            stage = stage.split('_')[2]
            #####################################################
            #if stage == 'fi':
            #    return _('Payment temporarily disabled.'), None
            #####################################################
            if stage == 'fr':
                amount += 500.0
            elif stage in ('fi', 're') and student[
                'studycourse'].previous_verdict in ('A', 'B', 'F', 'J', 'C', 'Z'):
                amount += 300.0
            else:
                amount = 0.0
        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 == 'hostel_application':
        #    amount = 1000.0
        #elif category.startswith('tempmaint'):
        #    if not self._hostelApplicationPaymentMade(
        #        student, student.current_session):
        #        return _(
        #            'You have not yet paid the hostel application fee.'), None
        #    if category == 'tempmaint_1':
        #        amount = 8150.0
        #    elif category == 'tempmaint_2':
        #        amount = 12650.0
        #    elif category == 'tempmaint_3':
        #        amount = 9650.0
        elif category == 'clearance':
            p_item = student.certcode
            if p_item is None:
                return _('Study course data are incomplete.'), None
            if student.faccode == 'JUPEB':
                return _('No payment required.'), None
            if student.faccode == 'FCETA':
                amount = 30000.0
            elif p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS',
                'MBBSMED', 'MBBSNDU'):
                amount = 65000.0
            elif p_item in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET',
                'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'):
                amount = 22500.0
            else:
                amount = 45000.0
        elif category == 'schoolfee':
            try:
                certificate = student['studycourse'].certificate
                p_item = certificate.code
            except (AttributeError, TypeError):
                return _('Study course data are incomplete.'), None

            #####################################################
            #if student.faccode == 'JUPEB':
            #    return _('Payment temporarily disabled.'), 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_session == student['studycourse'].entry_session:
                    if student.is_foreigner:
                        amount = getattr(certificate, 'school_fee_3', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_1', 0.0)
                else:
                    if student.is_foreigner:
                        amount = getattr(certificate, 'school_fee_4', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_2', 0.0)
            else:
                if student.state == CLEARED:
                    if student.is_foreigner:
                        amount = getattr(certificate, 'school_fee_3', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_1', 0.0)
                elif student.state in (PAID, REGISTERED, VALIDATED):
                    p_session += 1
                    # We don't know which level the student is paying for.
                    p_level = None
                    academic_session = self._getSessionConfiguration(p_session)
                    if academic_session == None:
                        return _(u'Session configuration object is not available.'), None

                    # Students are only allowed to pay for the next session
                    # if current session payment
                    # has really been made, i.e. payment object exists.
                    #if not self._paymentMade(
                    #    student, student.current_session):
                    #    return _('You have not yet paid your current/active' +
                    #             ' session. Please use the previous session' +
                    #             ' payment form first.'), None

                    if student.is_foreigner:
                        amount = getattr(certificate, 'school_fee_4', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_2', 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.
                    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

                    # Students are only allowed to pay for the next session
                    # if current session payment has really been made,
                    # i.e. payment object exists and is paid.
                    #if not self._paymentMade(
                    #    student, student.current_session):
                    #    return _('You have not yet paid your current/active' +
                    #             ' session. Please use the previous session' +
                    #             ' payment form first.'), None

                    if student.is_foreigner:
                        amount = getattr(certificate, 'school_fee_4', 0.0)
                    else:
                        amount = getattr(certificate, 'school_fee_2', 0.0)
            # Give 50% school fee discount to staff members.
            if student.is_staff:
                amount /= 2
        if amount in (0.0, None):
            return _('Amount could not be determined.'), None
        # Add session specific penalty fee.
        if category == 'schoolfee' and student.is_postgrad:
            amount += academic_session.penalty_pg
        elif category == 'schoolfee':
            amount += academic_session.penalty_ug
        if category.startswith('tempmaint'):
            p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category]
            p_item = unicode(p_item)
            # Now we change the category because tempmaint payments
            # will be obsolete when Uniben returns to Kofa bed allocation.
            category = 'hostel_maintenance'
        # Create ticket.
        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
        payment.amount_auth = amount
        return None, payment

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

        """
        studycourse = studylevel.__parent__
        certificate = getattr(studycourse,'certificate', None)
        current_level = studycourse.current_level
        if None in (current_level, certificate):
            return 0
        end_level = certificate.end_level
        if current_level >= end_level:
            return 51
        return 50

    def clearance_disabled_message(self, student):
        if student.is_postgrad:
            return None
        try:
            session_config = grok.getSite()[
                'configuration'][str(student.current_session)]
        except KeyError:
            return _('Session configuration object is not available.')
        if not session_config.clearance_enabled:
            return _('Clearance is disabled for this session.')
        return None

    # Uniben prefix
    STUDENT_ID_PREFIX = u'B'