## $Id: test_utils.py 15360 2019-03-22 07:05:00Z henrik $
##
## Copyright (C) 2019 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 hurry.workflow.interfaces import IWorkflowState
from zope.component import getUtility, createObject
from waeup.kofa.students.studylevel import StudentStudyLevel
from waeup.kofa.students.tests.test_browser import StudentsFullSetup
from waeup.kofa.students.interfaces import IStudentsUtils
from waeup.kofa.students.studylevel import StudentStudyLevel
from kofacustom.dspg.testing import FunctionalLayer

class StudentsUtilsTests(StudentsFullSetup):

    layer = FunctionalLayer

    def test_setMatricNumber(self):
        IWorkflowState(self.student).setState('school fee paid')
        site = grok.getSite()
        utils = getUtility(IStudentsUtils)
        site['configuration'].next_matric_integer = 1
        site['configuration'].next_matric_integer_2 = 1
        site['configuration'].next_matric_integer_3 = 10001
        site['configuration'].next_matric_integer_4 = 10001
        self.student.matric_number = None
        # nd_ft
        self.certificate.study_mode ='nd_ft'
        msg, mnumber = utils.setMatricNumber(self.student)
        self.assertEqual(self.student.matric_number, 'dep1/ND/04/00001')
        self.assertEqual(site['configuration'].next_matric_integer, 2)
        self.assertEqual(msg, None)
        # hnd_ft
        self.student.matric_number = None
        self.certificate.study_mode ='hnd_ft'
        msg, mnumber = utils.setMatricNumber(self.student)
        self.assertEqual(self.student.matric_number, 'dep1/HND/04/00001')
        self.assertEqual(site['configuration'].next_matric_integer_2, 2)
        self.assertEqual(msg, None)
        # nd_pt
        self.student.matric_number = None
        self.certificate.study_mode ='nd_pt'
        msg, mnumber = utils.setMatricNumber(self.student)
        self.assertEqual(self.student.matric_number, 'dep1/ND/04/10001')
        self.assertEqual(site['configuration'].next_matric_integer_3, 10002)
        self.assertEqual(msg, None)
        # hnd_pt
        self.student.matric_number = None
        self.certificate.study_mode ='hnd_pt'
        msg, mnumber = utils.setMatricNumber(self.student)
        self.assertEqual(self.student.matric_number, 'dep1/HND/04/10001')
        self.assertEqual(site['configuration'].next_matric_integer_4, 10002)
        self.assertEqual(msg, None)
        return

    def test_set_payment_details(self):
        self.student['studycourse'].certificate.school_fee_1 = 6666.0
        self.student['studycourse'].certificate.school_fee_2 = 7777.0
        self.student['studycourse'].certificate.school_fee_3 = 8888.0
        self.student['studycourse'].certificate.school_fee_4 = 9999.0
        self.student['studycourse'].certificate.study_mode = 'hnd_ft'
        self.student.nationality = u'NG'
        self.student.lga = 'ebonyi_ukaba'
        utils = getUtility(IStudentsUtils)
        configuration = createObject('waeup.SessionConfiguration')
        configuration.academic_session = 2005
        self.app['configuration'].addSessionConfiguration(configuration)
        error, payment = utils.setPaymentDetails('dep_sug',self.student)
        self.assertEqual(payment.p_level, 100)
        self.assertEqual(payment.p_session, 2004)
        IWorkflowState(self.student).setState('returning')
        # Returning students are paying for next session
        error, payment = utils.setPaymentDetails('dep_sug',self.student)
        self.assertEqual(payment.p_level, 200)
        self.assertEqual(payment.p_session, 2005)
        self.assertEqual(payment.amount_auth, 3150.0)
        # Students can pay school fee if dep_sug has been paid first.
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error, u'You have to pay NADESU/SA/SUG Dues first.')
        error, payment = utils.setPaymentDetails('dep_sug',self.student)
        self.student['payments']['any_key'] = payment
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error, u'You have to pay NADESU/SA/SUG Dues first.')
        self.student['payments']['any_key'].p_state = 'paid'
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error, None)
        self.assertEqual(payment.p_level, 200)
        self.assertEqual(payment.p_session, 2005)
        self.assertEqual(payment.amount_auth, 9999.0)

