## $Id: test_utils.py 15763 2019-11-07 06:39:29Z 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 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.iuokada.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 = 21305
        self.student.matric_number = None
        msg, mnumber = utils.setMatricNumber(self.student)
        self.assertEqual(self.student.matric_number, '04/021305/fac1')
        self.assertEqual(msg, None)
        return

    def disabled_test_set_payment_details(self):
        self.app['configuration']['2004'].book = 150.0
        self.app['configuration']['2004'].develop = 90.0
        self.app['configuration']['2004'].registration = 180.0
        self.app['configuration']['2004'].municipal_fresh = 280.0
        self.app['configuration']['2004'].pcf = 380.0
        self.student['studycourse'].certificate.school_fee_2 = 6666.0
        IWorkflowState(self.student).setState('cleared')
        utils = getUtility(IStudentsUtils)
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error,
            'Registration Fee, Book Deposit, Development Fee, Fresh Students Municipal Fee, '
            'Parents Consultative Forum (PCF) Fee must be paid before Tution Fee. '
            'Make either single payments or make a \'Required Combi Payment\'.')
        # A 'Required Combi Payment' is sufficient.
        reg_payment = createObject('waeup.StudentOnlinePayment')
        reg_payment.p_category = u'required_combi'
        reg_payment.p_id = u'anyid'
        reg_payment.p_state = u'paid'
        reg_payment.p_session = 2004
        self.student['payments']['anykey'] = reg_payment
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error, None)
        # Let's try with single payments.
        reg_payment.p_category = u'registration'
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error,
            'Book Deposit, Development Fee, Fresh Students Municipal Fee, Parents Consultative '
            'Forum (PCF) Fee must be paid before Tution Fee. '
            'Make either single payments or make a \'Required Combi Payment\'.')
        IWorkflowState(self.student).setState('returning')
        configuration = createObject('waeup.SessionConfiguration')
        configuration.academic_session = 2005
        self.app['configuration'].addSessionConfiguration(configuration)
        self.app['configuration']['2005'].registration = 170.0
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error,
            'Registration Fee, Book Deposit, Development Fee, Fresh Students Municipal Fee, '
            'Parents Consultative Forum (PCF) Fee must be paid before Tution Fee. '
            'Make either single payments or make a \'Required Combi Payment\'.')
        reg_payment.p_session = 2005
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error,
            'Book Deposit, Development Fee, Fresh Students Municipal Fee, Parents Consultative '
            'Forum (PCF) Fee must be paid before Tution Fee. '
            'Make either single payments or make a \'Required Combi Payment\'.')
        pcf_payment = createObject('waeup.StudentOnlinePayment')
        pcf_payment.p_category = u'parentsconsult'
        pcf_payment.p_id = u'anyid'
        pcf_payment.p_state = u'paid'
        pcf_payment.p_session = 2005
        self.student['payments']['anykey2'] = pcf_payment
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error,
            'Book Deposit, Development Fee, Fresh Students Municipal Fee must be paid before Tution Fee. '
            'Make either single payments or make a \'Required Combi Payment\'.')
        munic_payment = createObject('waeup.StudentOnlinePayment')
        munic_payment.p_category = u'municipal_fresh'
        munic_payment.p_id = u'anyid'
        munic_payment.p_state = u'paid'
        munic_payment.p_session = 2005
        self.student['payments']['anykey3'] = munic_payment
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error,
            'Book Deposit, Development Fee must be paid before Tution Fee. '
            'Make either single payments or make a \'Required Combi Payment\'.')
        book_payment = createObject('waeup.StudentOnlinePayment')
        book_payment.p_category = u'book'
        book_payment.p_id = u'anyid'
        book_payment.p_state = u'paid'
        book_payment.p_session = 2005
        self.student['payments']['anykey4'] = book_payment
        develop_payment = createObject('waeup.StudentOnlinePayment')
        develop_payment.p_category = u'develop'
        develop_payment.p_id = u'anyid'
        develop_payment.p_state = u'paid'
        develop_payment.p_session = 2005
        self.student['payments']['anykey5'] = develop_payment
        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, 6666.0)
        return
