source: main/waeup.fceokene/trunk/src/waeup/fceokene/students/utils.py @ 15505

Last change on this file since 15505 was 15299, checked in by Henrik Bettermann, 6 years ago

Fix tests.

  • Property svn:keywords set to Id
File size: 19.9 KB
Line 
1## $Id: utils.py 15299 2019-01-18 07:41:36Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18import grok
19import random
20from time import time
21from zope.component import createObject, getUtility
22from waeup.kofa.interfaces import (
23    CLEARED, RETURNING, PAID, ADMITTED, CLEARANCE, REQUESTED)
24from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
25from waeup.kofa.accesscodes import create_accesscode
26from waeup.kofa.interfaces import CLEARED, RETURNING
27from waeup.fceokene.interfaces import MessageFactory as _
28from waeup.kofa.browser.interfaces import IPDFCreator
29from waeup.kofa.students.utils import trans
30
31# Very special school fee configuration, should be moved to
32# a seperate file.
33
34ARTS = ('CRS','ISS','HIS','MUS','ECO','GEO','POL','SOS','CCA','ECU',
35        'THA','GED','GSE','PES','SPC','ENG','FRE','ARB','HAU','IGB',
36        'YOR','NCRS','NISS','NHIS','NMUS','NECO','NGEO','NPOL',
37        'NCCA','NECU','NTHA','NGED','NGSE','NPES','NSPC','NENG',
38        'NFRE','NARB','NHAU','NIGB','NYOR','NSOS')
39
40GATEWAY_AMT = 157.50
41
42class CustomStudentsUtils(NigeriaStudentsUtils):
43    """A collection of customized methods.
44
45    """
46
47    def selectBed(self, available_beds, desired_hostel):
48        """Randomly select a bed from the list of available beds.
49        """
50        return random.choice(available_beds)
51
52    def getReturningData(self, student):
53        """ This method defines what happens after school fee payment
54        of returning students depending on the student's senate verdict.
55        """
56        prev_level = student['studycourse'].current_level
57        cur_verdict = student['studycourse'].current_verdict
58        if cur_verdict in ('A','B','L','M','N','Z',):
59            # Successful student
60            new_level = divmod(int(prev_level),100)[0]*100 + 100
61        elif cur_verdict in ('C','O'):
62            # Student on probation
63            new_level = int(prev_level) + 10
64        else:
65            # Student is somehow in an undefined state.
66            # Level has to be set manually.
67            new_level = prev_level
68        if cur_verdict == 'O':
69            new_session = student['studycourse'].current_session
70        else:
71            new_session = student['studycourse'].current_session + 1
72        return new_session, new_level
73
74    def setPaymentDetails(self, category, student,
75            previous_session=None, previous_level=None):
76        """Create Payment object and set the payment data of a student for
77        the payment category specified.
78
79        """
80        details = {}
81        p_item = u''
82        amount = 0.0
83        error = u''
84        if previous_session:
85            return _('Previous session payment not yet implemented.'), None
86        p_session = student['studycourse'].current_session
87        p_level = student['studycourse'].current_level
88        p_current = True
89        academic_session = self._getSessionConfiguration(p_session)
90        if academic_session == None:
91            return _(u'Session configuration object is not available.'), None
92        # Determine fee.
93        if category == 'transfer':
94            amount = academic_session.transfer_fee
95        elif category == 'gown':
96            amount = academic_session.gown_fee
97        elif category == 'bed_allocation':
98            amount = academic_session.booking_fee
99        elif category == 'hostel_maintenance':
100            current_session = student['studycourse'].current_session
101            bedticket = student['accommodation'].get(str(current_session), None)
102            if bedticket is not None and bedticket.bed is not None:
103                p_item = bedticket.bed_coordinates
104                if bedticket.bed.__parent__.maint_fee > 0:
105                    amount = bedticket.bed.__parent__.maint_fee
106            else:
107                return _(u'No bed space allocated.'), None
108            if student.current_mode.endswith('_sw') \
109                or student.current_mode == 'pd_ft':
110                amount *= 0.625
111        elif category == 'clearance':
112            amount = academic_session.clearance_fee
113            try:
114                p_item = student['studycourse'].certificate.code
115            except (AttributeError, TypeError):
116                return _('Study course data are incomplete.'), None
117            if student.state not in (ADMITTED, CLEARANCE, REQUESTED, CLEARED):
118                return _(u'Acceptance Fee payments not allowed.'), None
119        elif category == 'third_semester' and student.current_mode == 'nce_ft':
120            if student.depcode in ARTS:
121                amount = 7688
122            else:
123                amount = 7938
124        elif category == 'schoolfee':
125            p_item =  student.certcode
126            if not p_item:
127                return _('Study course data are incomplete.'), None
128
129            if student.state not in (CLEARED, RETURNING):
130                return _('Wrong state.'), None
131
132            # PDE repeater
133            if student.current_verdict == 'OPDE':
134                amount = 23000
135            # PDE new
136            elif student.current_mode == 'pd_ft' and student.state == CLEARED:
137                amount = 70300
138            # PDE
139            elif student.current_mode == 'pd_ft':
140                amount = 35300
141
142            #Short Duration ICT Programs
143            elif p_item in ('CCO','DPMTS','DTPGD') and \
144                 student.state == CLEARED:
145                amount = 15000
146            elif p_item in ('ADTPGD','ADPMTS') and \
147                 student.state == CLEARED:
148                amount = 16000
149            elif p_item in ('ADPMTSI','ADTPGDI','DPMTSI','DTPGDI') and \
150                 student.state == CLEARED:
151                amount = 25000
152            elif p_item in ('ADPMTSA','ADTPGDA') and \
153               student.state == CLEARED:
154                amount = 35000
155
156            # UG
157            elif student.current_mode == 'ug_ft':
158                if student.state == CLEARED:
159                    amount = 65650
160                # Introducing repeater fee for 'ug_ft' for 1st time
161                # on 15/03/2016
162                elif student.current_verdict == 'O':
163                    amount = 56150
164                else:
165                    amount = 56150
166            # NCE
167            elif not student.current_mode.endswith('_sw'):
168                # PRENCE
169                if student.current_level == 10 and student.state == CLEARED:
170                    if student.depcode in ARTS:
171                        amount = 24500
172                    else:
173                        amount = 25000
174                # NCE I fresh
175                elif student.current_level == 100 and student.state == CLEARED:
176                    if student.depcode in ARTS:
177                        amount = 28000
178                    else:
179                        amount = 28500
180                    # SIWES Fee
181                    if student.depcode in ('AGE', 'BED', 'FAA', 'HEC', 'CSC', 'MUS'):
182                        amount += 3000
183                # NCE II
184                elif student.current_level in (100, 110, 120) and \
185                    student.state == RETURNING:
186                    if student.depcode in ARTS:
187                        amount = 24500
188                    else:
189                        amount = 25000
190                # NCE III
191                elif student.current_level in (200, 210, 220):
192                    if student.depcode in ARTS:
193                        amount = 15375
194                    else:
195                        amount = 15875
196                # NCE III repeater
197                elif student.current_level in (300, 310, 320) and \
198                    student.current_verdict == 'O':
199                    if student.depcode in ARTS:
200                        amount = 13475
201                    else:
202                        amount = 13975
203                # NCE III spillover
204                elif student.current_level in (300, 310, 320) and \
205                    student.current_verdict == 'B':
206                    if student.depcode in ARTS:
207                        amount = 13475
208                    else:
209                        amount = 13975
210                # NCE III second spillover
211                elif student.current_level in (400, 410, 420) and \
212                    student.current_verdict == 'B':
213                    if student.depcode in ARTS:
214                        amount = 13475
215                    else:
216                        amount = 13975
217            else:
218                # NCE I fresh sw
219                if student.current_level == 100 and student.state == CLEARED:
220                    if student.depcode in ARTS:
221                        amount = 23100
222                    else:
223                        amount = 23600
224                # NCE II fresh sw
225                elif student.current_level == 200 and student.state == CLEARED:
226                    if student.depcode in ARTS:
227                        amount = 19000
228                    else:
229                        amount = 19500
230                # NCE II sw
231                elif student.current_level in (100, 110, 120) and \
232                    student.state == RETURNING:
233                    if student.depcode in ARTS:
234                        amount = 19000
235                    else:
236                        amount = 19500
237                # NCE III sw
238                elif student.current_level in (200, 210, 220):
239                    if student.depcode in ARTS:
240                        amount = 21000
241                    else:
242                        amount = 21500
243                # NCE IV sw
244                elif student.current_level in (300, 310, 320):
245                    if student.depcode in ARTS:
246                        amount = 19000
247                    else:
248                        amount = 19500
249                # NCE V sw
250                elif student.current_level in (400, 410, 420):
251                    if student.depcode in ARTS:
252                        amount = 19000
253                    else:
254                        amount = 19500
255                # NCE V spillover sw
256                elif student.current_level in (500, 510, 520) and \
257                    student.current_verdict == 'B':
258                    if student.depcode in ARTS:
259                        amount = 17500
260                    else:
261                        amount = 18000
262                # NCE V second spillover sw
263                elif student.current_level in (600, 610, 620) and \
264                    student.current_verdict == 'B':
265                    if student.depcode in ARTS:
266                        amount = 17500
267                    else:
268                        amount = 18000
269            # NCE student payment can be disabled by
270            # setting the base school fee to -1
271            if academic_session.school_fee_base == -1 and \
272                student.current_mode.startswith('nce'):
273                return _(u'School fee payment is disabled.'), None
274            if student.state == RETURNING:
275                # Override p_session and p_level
276                p_session, p_level = self.getReturningData(student)
277                academic_session = self._getSessionConfiguration(p_session)
278                if academic_session == None:
279                    return _(u'Session configuration object is not available.'), None
280
281        if amount in (0.0, None):
282            return _(u'Amount could not be determined.'), None
283
284        # Add session and level specific penalty fee.
285        if category == 'schoolfee' and student.current_mode in (
286            'ug_ft', 'de_ft') and student.state != CLEARED:
287            amount += academic_session.penalty_ug_ft
288        elif category == 'schoolfee' and student.current_mode in (
289            'nce_ft',) and student['studycourse'].previous_verdict != 'O':
290            # NCE I fresh
291            if student.current_level == 100 and student.state == CLEARED:
292                amount += academic_session.penalty_nce1_ft
293            # NCE II
294            elif student.current_level in (100, 110, 120) and \
295                student.state == RETURNING:
296                amount += academic_session.penalty_nce2_ft
297            # NCE III (except repeaters and spillovers)
298            elif student.current_level in (200, 210, 220):
299                amount += academic_session.penalty_nce3_ft
300        elif category == 'schoolfee' and student.current_mode in ('nce_sw',
301            'nce_pt') and student['studycourse'].previous_verdict != 'O':
302            # NCE I fresh
303            if student.current_level == 100 and student.state == CLEARED:
304                amount += academic_session.penalty_nce1_pt
305            # NCE II
306            elif student.current_level in (100, 110, 120) and \
307                student.state == RETURNING:
308                amount += academic_session.penalty_nce2_pt
309            # NCE III (except repeaters and spillovers)
310            elif student.current_level in (200, 210, 220):
311                amount += academic_session.penalty_nce3_pt
312        elif category == 'schoolfee' and student.current_mode in ('prence',):
313            amount += academic_session.penalty_prence
314        if self.samePaymentMade(student, category, p_item, p_session):
315            return _('This type of payment has already been made.'), None
316        if self._isPaymentDisabled(p_session, category, student):
317            return _('This category of payments has been disabled.'), None
318        payment = createObject(u'waeup.StudentOnlinePayment')
319        timestamp = ("%d" % int(time()*10000))[1:]
320        payment.p_id = "p%s" % timestamp
321        payment.p_category = category
322        payment.p_item = p_item
323        payment.p_session = p_session
324        payment.p_level = p_level
325        payment.p_current = p_current
326        payment.amount_auth = float(amount) + GATEWAY_AMT
327        return None, payment
328
329    def getAccommodationDetails(self, student):
330        """Determine the accommodation data of a student.
331        """
332        d = {}
333        d['error'] = u''
334        hostels = grok.getSite()['hostels']
335        d['booking_session'] = hostels.accommodation_session
336        d['allowed_states'] = hostels.accommodation_states
337        d['startdate'] = hostels.startdate
338        d['enddate'] = hostels.enddate
339        d['expired'] = hostels.expired
340        # Determine bed type
341        studycourse = student['studycourse']
342        certificate = getattr(studycourse,'certificate',None)
343        current_level = studycourse.current_level
344        if None in (current_level, certificate):
345            return d
346        end_level = certificate.end_level
347        if current_level == 10:
348            bt = 'pr'
349        elif current_level == 100:
350            bt = 'fr'
351        elif current_level >= 300:
352            bt = 'fi'
353        else:
354            bt = 're'
355        if student.sex == 'f':
356            sex = 'female'
357        else:
358            sex = 'male'
359        special_handling = 'regular'
360        if certificate.study_mode == 'ug_ft':
361            special_handling = 'ugft'
362        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
363        return d
364
365    #def warnCreditsOOR(self, studylevel, course=None):
366    #    """Return message if credits are out of range.
367    #    """
368    #    # adding a course ticket
369    #    if course:
370    #        if course.semester == 1:
371    #            if studylevel.total_credits_s1 + course.credits > 24:
372    #                return _('Maximum credits in 1st semester exceeded.')
373    #        if course.semester == 2:
374    #            if studylevel.total_credits_s2 + course.credits > 24:
375    #                return _('Maximum credits in 2nd semester exceeded.')
376    #    # registering course list
377    #    else:
378    #        if studylevel.total_credits_s1 > 24:
379    #            return _('Maximum credits in 1st semester exceeded.')
380    #        if studylevel.total_credits_s1 < 18:
381    #            return _('Minimum credits in 1st semester not reached.')
382    #        if studylevel.total_credits_s2 > 24:
383    #            return _('Maximum credits in 2nd semester exceeded.')
384    #        if studylevel.total_credits_s2 < 18:
385    #            return _('Minimum credits in 2nd semester not reached.')
386    #    return
387
388    def warnCreditsOOR(self, studylevel, course=None):
389        """Return message if credits are out of range.
390        """
391        # adding a course ticket
392        if course:
393            if studylevel.total_credits + course.credits > 52:
394                return _('Maximum credits exceeded.')
395        # registering course list
396        else:
397            if studylevel.total_credits > 52:
398                return _('Maximum credits exceeded.')
399            if studylevel.__parent__.previous_verdict == 'O':
400                return
401            if studylevel.total_credits_s1 < 18:
402                return _('Minimum credits in 1st semester not reached.')
403            if studylevel.total_credits_s2 < 18:
404                return _('Minimum credits in 2nd semester not reached.')
405        return
406
407    def getPDFCreator(self, context):
408        """Get a pdf creator suitable for `context`.
409
410        The default implementation always returns the default creator.
411        """
412        mode = getattr(context, 'current_mode', None)
413        if mode and mode.startswith('ug'):
414            return getUtility(IPDFCreator, name='ibadan_pdfcreator')
415        return getUtility(IPDFCreator)
416
417    def _admissionText(self, student, portal_language):
418        mode = getattr(student, 'current_mode', None)
419        if mode and mode.startswith('ug'):
420            text = trans(_(
421                'With reference to your application for admission into Bachelor Degree '
422                'Programme of the University of Ibadan, this is to inform you that you have '
423                'been provisionally admitted to pursue a full-time Bachelor of Arts in '
424                'Education Degree Programme as follows:'),
425                portal_language)
426        else:
427            inst_name = grok.getSite()['configuration'].name
428            text = trans(_(
429                'This is to inform you that you have been provisionally'
430                ' admitted into ${a} as follows:', mapping = {'a': inst_name}),
431                portal_language)
432        return text
433
434    def getBedCoordinates(self, bedticket):
435        """Return bed coordinates.
436
437        Bed coordinates are invisible in FCEOkene.
438        """
439        return _('(see payment slip)')
440
441    def _isPaymentDisabled(self, p_session, category, student):
442        academic_session = self._getSessionConfiguration(p_session)
443        if category == 'schoolfee':
444            if 'sf_all' in academic_session.payment_disabled:
445                return True
446            if 'sf_nce1' in academic_session.payment_disabled and \
447                student.current_level == 100 and student.state == CLEARED and \
448                student.current_mode == 'nce_ft':
449                return True
450        return False
451
452    SEPARATORS_DICT = {
453        'form.fst_sit_fname': _(u'First Sitting Record'),
454        'form.scd_sit_fname': _(u'Second Sitting Record'),
455        #'form.alr_fname': _(u'Advanced Level Record'),
456        'form.hq_type': _(u'Advanced Level Record'),
457        'form.hq2_type': _(u'Second Higher Education Record'),
458        'form.nysc_year': _(u'NYSC Information'),
459        'form.employer': _(u'Employment History'),
460        'form.former_matric': _(u'Former Student'),
461        }
462
463    SKIP_UPLOAD_VIEWLETS = (
464        'higherqualificationresultupload',
465        'secondHigherqualificationresultupload',
466        'certificateupload',
467        'secondcertificateupload',
468        'thirdcertificateupload',
469        'resultstatementupload',
470        'secondrefereeletterupload',
471        'thirdrefereeletterupload',)
472
473    # FCEOkene prefix
474    STUDENT_ID_PREFIX = u'K'
Note: See TracBrowser for help on using the repository browser.