## $Id: utils.py 15674 2019-10-13 21:06:08Z 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, IKofaUtils
from waeup.kofa.fees import FeeTable
from waeup.aaua.interfaces import MessageFactory as _


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

    """

    def setPaymentDetails(self, category, student,
            previous_session=None, previous_level=None, p_combi=[]):
        """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:
            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.
        try:
            certificate = student['studycourse'].certificate
            p_item = certificate.code
        except (AttributeError, TypeError):
            return _('Study course data are incomplete.'), None
        ratio = getattr(certificate, 'ratio', 0.0)
        if ratio is None:
            ratio = 0.0
        if category in ('schoolfee', 'schoolfee_1'):
            if category == 'schoolfee' and ratio > 0:
                return _('Payment by instalments required.'), None
            if category =='schoolfee_1' and ratio == 0:
                return _('Payment by instalments not allowed.'), None
            if student.state == CLEARED:
                if category == 'schoolfee':
                    amount = getattr(certificate, 'school_fee_1', 0.0)
                elif category == 'schoolfee_1':
                    amount = getattr(certificate, 'school_fee_1', 0.0) * ratio
            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 category == 'schoolfee':
                    amount = getattr(certificate, 'school_fee_2', 0.0)
                elif category == 'schoolfee_1':
                    amount = getattr(certificate, 'school_fee_2', 0.0) * ratio
                    if student['studycourse'].entry_session in (
                        2005, 2006, 2007) and student.current_mode == 'ug_pt':
                        amount -= 20000.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
                if category == 'schoolfee':
                    amount = getattr(certificate, 'school_fee_2', 0.0)
                elif category == 'schoolfee_1':
                    amount = getattr(certificate, 'school_fee_2', 0.0) * ratio
        elif category ==  'schoolfee_2':
            if ratio == 0:
                return _('Payment by instalments not allowed.'), None
            if student['studycourse'].entry_session == student.current_session:
                amount = getattr(certificate, 'school_fee_1', 0.0) * (1- ratio)
            else:
                amount = getattr(certificate, 'school_fee_2', 0.0) * (1- ratio)
        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 _('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

    # AAUA prefix
    STUDENT_ID_PREFIX = u'A'