source: main/kofacustom.edopoly/trunk/src/kofacustom/edopoly/students/utils.py @ 15448

Last change on this file since 15448 was 15448, checked in by Henrik Bettermann, 5 years ago

Multiple transcript payments are allowed.

  • Property svn:keywords set to Id
File size: 13.7 KB
Line 
1## $Id: utils.py 15448 2019-06-03 12:20:44Z 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##
18from time import time
19import grok
20from zope.component import createObject, getUtility
21from waeup.kofa.interfaces import (IKofaUtils,
22    CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
23from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
24from kofacustom.edopoly.interfaces import MessageFactory as _
25
26class CustomStudentsUtils(NigeriaStudentsUtils):
27    """A collection of customized methods.
28
29    """
30
31    # prefix
32    STUDENT_ID_PREFIX = u'U'
33
34    def warnCreditsOOR(self, studylevel, course=None):
35        """No maximum credits.
36        """
37        return
38
39    def GPABoundaries(self, faccode=None, depcode=None,
40                            certcode=None, student=None):
41        if student and student.current_mode.startswith('dp'):
42                return ((1, 'IRNS / NER / NYV'),
43                        (2, 'Pass'),
44                        (2.5, 'Lower Credit'),
45                        (3, 'Upper Credit'),
46                        (3.5, 'Distinction'))
47        elif student:
48            return ((1, 'FRNS / NER / NYV'),
49                    (1.5, 'Pass'),
50                    (2.4, '3rd Class Honours'),
51                    (3.5, '2nd Class Honours Lower Division'),
52                    (4.5, '2nd Class Honours Upper Division'),
53                    (5, '1st Class Honours'))
54        # Session Results Presentations depend on certificate
55        results = None
56        if certcode:
57            cat = queryUtility(ICatalog, name='certificates_catalog')
58            results = list(
59                cat.searchResults(code=(certcode, certcode)))
60        if results and results[0].study_mode.startswith('dp'):
61            return ((1, 'IRNS / NER / NYV'),
62                    (2, 'Pass'),
63                    (2.5, 'Lower Credit'),
64                    (3, 'Upper Credit'),
65                    (3.5, 'Distinction'))
66        else:
67            return ((1, 'FRNS / NER / NYV'),
68                    (1.5, 'Pass'),
69                    (2.4, '3rd Class Honours'),
70                    (3.5, '2nd Class Honours Lower Division'),
71                    (4.5, '2nd Class Honours Upper Division'),
72                    (5, '1st Class Honours'))
73
74    def getClassFromCGPA(self, gpa, student):
75        gpa_boundaries = self.GPABoundaries(student=student)
76        if gpa < gpa_boundaries[0][0]:
77            return 0, gpa_boundaries[0][1]
78        if gpa < gpa_boundaries[1][0]:
79            return 1, gpa_boundaries[1][1]
80        if gpa < gpa_boundaries[2][0]:
81            return 2, gpa_boundaries[2][1]
82        if gpa < gpa_boundaries[3][0]:
83            return 3, gpa_boundaries[3][1]
84        if gpa < gpa_boundaries[4][0]:
85            return 4, gpa_boundaries[4][1]
86        if gpa <= gpa_boundaries[5][0]:
87            return 5, gpa_boundaries[5][1]
88        return 'N/A'
89
90    def _requiredPaymentsMade(self, student, session):
91        # SIWESS fee requirement removed on 18/01/2019
92        req_payments = ('ict_entre', 'logbook_combo')
93        req_payments_titles = 'ICT and Logbook'
94        # All ND and HND part time do not pay for LOGBOOK
95        if student.current_mode.endswith('_pt'):
96            req_payments = ('ict_entre',)
97            req_payments_titles = 'ICT'
98        # HND1 and HND2 full time do not pay for LOGBOOK
99        elif student.current_mode == 'hnd_ft' and student.state in (
100            CLEARED, RETURNING):
101            req_payments = ('ict_entre',)
102            req_payments_titles = 'ICT'
103        # ND2 FULL TIME do not pay LOGBOOK
104        elif student.current_mode == 'nd_ft' and student.state == RETURNING:
105            req_payments = ('ict_entre',)
106            req_payments_titles = 'ICT'
107        num = 0
108        if len(student['payments']):
109            for ticket in student['payments'].values():
110                if ticket.p_state == 'paid' and \
111                    ticket.p_category in req_payments and \
112                    ticket.p_session == session:
113                    num += 1
114            if num == len(req_payments):
115                return True, None
116        return False, req_payments_titles
117
118    def samePaymentMade(self, student, category, p_item, p_session):
119        for key in student['payments'].keys():
120            ticket = student['payments'][key]
121            if ticket.p_state == 'paid' and\
122               ticket.p_category == category and \
123               ticket.p_item == p_item and \
124               ticket.p_session == p_session:
125                  return True
126        return False
127
128    def setPaymentDetails(self, category, student,
129            previous_session=None, previous_level=None):
130        """Create a payment ticket and set the payment data of a
131        student for the payment category specified.
132        """
133        p_item = u''
134        amount = 0.0
135        if previous_session:
136            if previous_session < student['studycourse'].entry_session:
137                return _('The previous session must not fall below '
138                         'your entry session.'), None
139            if category == 'schoolfee':
140                # School fee is always paid for the following session
141                if previous_session > student['studycourse'].current_session:
142                    return _('This is not a previous session.'), None
143            else:
144                if previous_session > student['studycourse'].current_session - 1:
145                    return _('This is not a previous session.'), None
146            p_session = previous_session
147            p_level = previous_level
148            p_current = False
149        else:
150            p_session = student['studycourse'].current_session
151            p_level = student['studycourse'].current_level
152            p_current = True
153        # The following three fees are part of the school fee which must be
154        # paid before tuition fee. They are paid for next session.
155        if category in ('ict_entre', 'logbook_combo', 'siwess_combo') and \
156            student.state == RETURNING and not previous_session:
157            p_session, p_level = self.getReturningData(student)
158        academic_session = self._getSessionConfiguration(p_session)
159        if academic_session == None:
160            return _(u'Session configuration object is not available.'), None
161        # Determine fee.
162        if category == 'schoolfee':
163            try:
164                certificate = student['studycourse'].certificate
165                p_item = certificate.code
166            except (AttributeError, TypeError):
167                return _('Study course data are incomplete.'), None
168            if previous_session:
169                amount = getattr(certificate, 'school_fee_1', 0.0)
170            else:
171                if student.state == CLEARED:
172                    amount = getattr(certificate, 'school_fee_1', 0.0)
173                elif student.state == RETURNING:
174                    amount = getattr(certificate, 'school_fee_1', 0.0)
175                    # In case of returning school fee payment the
176                    # payment session and level contain the values of
177                    # the session the student has paid for. Payment
178                    # session is always next session.
179                    p_session, p_level = self.getReturningData(student)
180                    academic_session = self._getSessionConfiguration(p_session)
181                    if academic_session == None:
182                        return _(
183                            u'Session configuration object is not available.'
184                            ), None
185                elif student.is_postgrad and student.state == PAID:
186                    # Returning postgraduate students also pay for the
187                    # next session but their level always remains the
188                    # same.
189                    amount = getattr(certificate, 'school_fee_1', 0.0)
190                    p_session += 1
191                    academic_session = self._getSessionConfiguration(p_session)
192                    if academic_session == None:
193                        return _(
194                            u'Session configuration object is not available.'
195                            ), None
196            rpm, rpt = self._requiredPaymentsMade(student, p_session)
197            if not rpm:
198                return 'Pay %s fee(s) first.' % rpt, None
199        elif category == 'clearance':
200            try:
201                p_item = student['studycourse'].certificate.code
202            except (AttributeError, TypeError):
203                return _('Study course data are incomplete.'), None
204            amount = academic_session.clearance_fee
205        elif category == 'bed_allocation':
206            p_item = self.getAccommodationDetails(student)['bt']
207            amount = academic_session.booking_fee
208        elif category == 'hostel_maintenance':
209            amount = 0.0
210            bedticket = student['accommodation'].get(
211                str(student.current_session), None)
212            if bedticket is not None and bedticket.bed is not None:
213                p_item = bedticket.bed_coordinates
214                if bedticket.bed.__parent__.maint_fee > 0:
215                    amount = bedticket.bed.__parent__.maint_fee
216                else:
217                    # fallback
218                    amount = academic_session.maint_fee
219            else:
220                return _(u'No bed allocated.'), None
221        elif category == 'ict_entre':
222            amount = 11000.0
223            if student['studycourse'].entry_session < 2018:
224                amount = 16000.0
225        else:
226            fee_name = category + '_fee'
227            amount = getattr(academic_session, fee_name, 0.0)
228        if amount in (0.0, None):
229            return _('Amount could not be determined.'), None
230        if self.samePaymentMade(student, category, p_item, p_session):
231            return _('This type of payment has already been made.'), None
232        if self._isPaymentDisabled(p_session, category, student):
233            return _('This category of payments has been disabled.'), None
234        payment = createObject(u'waeup.StudentOnlinePayment')
235        timestamp = ("%d" % int(time()*10000))[1:]
236        payment.p_id = "p%s" % timestamp
237        payment.p_category = category
238        payment.p_item = p_item
239        payment.p_session = p_session
240        payment.p_level = p_level
241        payment.p_current = p_current
242        payment.amount_auth = amount
243        return None, payment
244
245    def constructMatricNumber(self, student):
246        faccode = student.faccode
247        #depcode = student.depcode
248        #certcode = student.certcode
249        year = unicode(student.entry_session)[2:]
250        if not student.state in (PAID, ) or not student.is_fresh:
251            return _('Matriculation number cannot be set.'), None
252
253        # SASND1809001
254        if student.current_mode == 'nd_ft':
255            next_integer = grok.getSite()['configuration'].next_matric_integer
256            if next_integer == 0:
257                return _('Matriculation number cannot be set.'), None
258            return None, "%s/ND/%s/%05d" % (faccode, year, next_integer)
259
260        # SASNH1809001
261        if student.current_mode == 'hnd_ft':
262            next_integer = grok.getSite()['configuration'].next_matric_integer_2
263            if next_integer == 0:
264                return _('Matriculation number cannot be set.'), None
265            return None, "%s/HD/%s/%05d" % (faccode, year, next_integer)
266
267        # SASPT1809001
268        if student.current_mode in ('nd_pt', 'hnd_pt'):
269            next_integer = grok.getSite()['configuration'].next_matric_integer_3
270            if next_integer == 0:
271                return _('Matriculation number cannot be set.'), None
272            return None, "%s/PT/%s/%05d" % (faccode, year, next_integer)
273
274        return _('Matriculation number cannot be set.'), None
275
276
277    def increaseMatricInteger(self, student):
278        """Increase counter for matric numbers.
279        """
280        if student.current_mode == 'nd_ft':
281            grok.getSite()['configuration'].next_matric_integer += 1
282            return
283        elif student.current_mode == 'hnd_ft':
284            grok.getSite()['configuration'].next_matric_integer_2 += 1
285            return
286        elif student.current_mode in ('nd_pt', 'hnd_pt'):
287            grok.getSite()['configuration'].next_matric_integer_3 += 1
288            return
289        return
290
291    def getAccommodationDetails(self, student):
292        """Determine the accommodation data of a student.
293        """
294        d = {}
295        d['error'] = u''
296        hostels = grok.getSite()['hostels']
297        d['booking_session'] = hostels.accommodation_session
298        d['allowed_states'] = hostels.accommodation_states
299        d['startdate'] = hostels.startdate
300        d['enddate'] = hostels.enddate
301        d['expired'] = hostels.expired
302        # Determine bed type
303        studycourse = student['studycourse']
304        certificate = getattr(studycourse,'certificate',None)
305        entry_session = studycourse.entry_session
306        current_level = studycourse.current_level
307        if None in (entry_session, current_level, certificate):
308            return d
309        bt = 'all'
310        if student.sex == 'f':
311            sex = 'female'
312        else:
313            sex = 'male'
314        special_handling = 'regular'
315        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
316        return d
Note: See TracBrowser for help on using the repository browser.