source: main/kofacustom.dspg/trunk/src/kofacustom/dspg/students/utils.py @ 16026

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

First-year probating ND students or third year
probating hnd students are treated like
fresh students.

  • Property svn:keywords set to Id
File size: 16.4 KB
Line 
1## $Id: utils.py 16013 2020-02-26 18:35:30Z 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
19from time import time
20from zope.component import createObject, getUtility
21from waeup.kofa.interfaces import (IKofaUtils,
22    ADMITTED, CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
23from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
24from kofacustom.dspg.interfaces import MessageFactory as _
25
26
27def local(student):
28    lga = getattr(student, 'lga')
29    if lga and lga.startswith('delta'):
30        return True
31    return False
32
33class CustomStudentsUtils(NigeriaStudentsUtils):
34    """A collection of customized methods.
35    """
36
37    # prefix
38    STUDENT_ID_PREFIX = u'P'
39
40    def _dep_sug_paymentMade(self, student, session):
41        if student.state == RETURNING:
42            session += 1
43        if len(student['payments']):
44            for ticket in student['payments'].values():
45                if ticket.p_state == 'paid' and \
46                    ticket.p_category == 'dep_sug' and \
47                    ticket.p_session == session:
48                    return True
49        return False
50
51    def _lsfp_penalty_paymentMade(self, student, session):
52        if student.current_mode not in ('hnd_ft', 'nd_ft'):
53            return True
54        if len(student['payments']):
55            for ticket in student['payments'].values():
56                if ticket.p_state == 'paid' and \
57                    ticket.p_category == 'lsfp_penalty' and \
58                    ticket.p_session == session:
59                    return True
60        return False
61
62    def getAccommodationDetails(self, student):
63        """Determine the accommodation data of a student.
64        """
65        d = {}
66        d['error'] = u''
67        hostels = grok.getSite()['hostels']
68        d['booking_session'] = hostels.accommodation_session
69        d['allowed_states'] = hostels.accommodation_states
70        d['startdate'] = hostels.startdate
71        d['enddate'] = hostels.enddate
72        d['expired'] = hostels.expired
73        # Determine bed type
74        bt = 'all'
75        if student.sex == 'f':
76            sex = 'female'
77        else:
78            sex = 'male'
79        special_handling = 'regular'
80        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
81        return d
82
83    def getBedCoordinates(self, bedticket):
84        """Translated coordinates:
85
86        Hall Name, Block Letter, Room Number, Bed Letter =
87        Hall Name, M/F + Room Number-100, Bed Letter
88
89        Requirements: Gender must be part of bed_coordinates and all hostels
90        have only one block with only one floor
91        """
92        ### ToDo
93        return bedticket.bed_coordinates
94
95    def getReturningData(self, student):
96        """ This method defines what happens after school fee payment
97        of returning students depending on the student's senate verdict.
98        """
99        prev_level = student['studycourse'].current_level
100        cur_verdict = student['studycourse'].current_verdict
101        if cur_verdict in ('A','B','L','M','N','Z',):
102            # Successful student
103            new_level = divmod(int(prev_level),100)[0]*100 + 100
104        elif cur_verdict == 'C':
105            # Student on probation
106            new_level = int(prev_level) + 10
107        else:
108            # Student is somehow in an undefined state.
109            # Level has to be set manually.
110            new_level = prev_level
111        new_session = student['studycourse'].current_session + 1
112        return new_session, new_level
113
114    def setPaymentDetails(self, category, student,
115            previous_session=None, previous_level=None, combi=[]):
116        """Create a payment ticket and set the payment data of a
117        student for the payment category specified.
118        """
119        p_item = u''
120        amount = 0.0
121        if previous_session:
122            if previous_session < student['studycourse'].entry_session:
123                return _('The previous session must not fall below '
124                         'your entry session.'), None
125            if category == 'schoolfee':
126                # School fee is always paid for the following session
127                if previous_session > student['studycourse'].current_session:
128                    return _('This is not a previous session.'), None
129            else:
130                if previous_session > student['studycourse'].current_session - 1:
131                    return _('This is not a previous session.'), None
132            p_session = previous_session
133            p_level = previous_level
134            p_current = False
135        else:
136            p_session = student['studycourse'].current_session
137            p_level = student['studycourse'].current_level
138            p_current = True
139        academic_session = self._getSessionConfiguration(p_session)
140        if academic_session == None:
141            return _(u'Session configuration object is not available.'), None
142        # Determine fee.
143        if category == 'schoolfee':
144            try:
145                certificate = student['studycourse'].certificate
146                p_item = certificate.code
147            except (AttributeError, TypeError):
148                return _('Study course data are incomplete.'), None
149            if previous_session:
150                # Students can pay for previous sessions in all
151                # workflow states.  Fresh students are excluded by the
152                # update method of the PreviousPaymentAddFormPage.
153                if previous_level == 100:
154                    if local(student):
155                        amount = getattr(certificate, 'school_fee_1', 0.0)
156                    else:
157                        amount = getattr(certificate, 'school_fee_3', 0.0)
158                else:
159                    if local(student):
160                        amount = getattr(certificate, 'school_fee_2', 0.0)
161                    else:
162                        amount = getattr(certificate, 'school_fee_4', 0.0)
163            else:
164                # Students are only allowed to pay school fee
165                # if current session dep_sug payment has been made.
166                if student.faccode != 'SPAT' and not self._dep_sug_paymentMade(
167                    student, student.current_session):
168                    return _('You have to pay NADESU/SA/SUG Dues first.'), None
169                penalty = getattr(academic_session, 'lsfp_penalty_fee')
170                if  penalty and not self._lsfp_penalty_paymentMade(
171                    student, student.current_session):
172                    return _('You have to pay late school fee payment penalty first.'), None
173                if student.state == CLEARED:
174                    if local(student):
175                        amount = getattr(certificate, 'school_fee_1', 0.0)
176                    else:
177                        amount = getattr(certificate, 'school_fee_3', 0.0)
178                elif student.state == RETURNING:
179                    # In case of returning school fee payment the
180                    # payment session and level contain the values of
181                    # the session the student has paid for. Payment
182                    # session is always next session.
183                    p_session, p_level = self.getReturningData(student)
184                    academic_session = self._getSessionConfiguration(p_session)
185                    if academic_session == None:
186                        return _(
187                            u'Session configuration object is not available.'
188                            ), None
189                    if p_level in (100, 110, 120, 130) or (
190                        p_level in (300, 310, 320, 330) and
191                        student.current_mode == 'hnd_ft'):
192                        # First-year probating ND students or third year
193                        # probating hnd students are treated like
194                        # fresh students.
195                        if local(student):
196                            amount = getattr(certificate, 'school_fee_1', 0.0)
197                        else:
198                            amount = getattr(certificate, 'school_fee_3', 0.0)
199                    else:
200                        if local(student):
201                            amount = getattr(certificate, 'school_fee_2', 0.0)
202                        else:
203                            amount = getattr(certificate, 'school_fee_4', 0.0)
204                elif student.is_postgrad and student.state == PAID:
205                    # Returning postgraduate students also pay for the
206                    # next session but their level always remains the
207                    # same.
208                    p_session += 1
209                    academic_session = self._getSessionConfiguration(p_session)
210                    if academic_session == None:
211                        return _(
212                            u'Session configuration object is not available.'
213                            ), None
214                    if local(student):
215                        amount = getattr(certificate, 'school_fee_2', 0.0)
216                    else:
217                        amount = getattr(certificate, 'school_fee_4', 0.0)
218        elif category == 'clearance':
219            try:
220                p_item = student['studycourse'].certificate.code
221            except (AttributeError, TypeError):
222                return _('Study course data are incomplete.'), None
223            amount = academic_session.clearance_fee
224            # HND students pay more
225            if student.current_mode == 'hnd_ft':
226                amount += 3000
227            # Local ND and HND students are given a rebate which has
228            # to be adjusted if the basic acceptance fee changes.
229            if amount and student.current_mode == 'nd_ft' and local(student):
230                amount -= 10000.0
231            if amount and student.current_mode == 'hnd_ft' and local(student):
232                amount -= 5000.0
233        elif category == 'bed_allocation':
234            p_item = self.getAccommodationDetails(student)['bt']
235            amount = academic_session.booking_fee
236        elif category == 'hostel_maintenance':
237            amount = 0.0
238            bedticket = student['accommodation'].get(
239                str(student.current_session), None)
240            if bedticket is not None and bedticket.bed is not None:
241                p_item = bedticket.bed_coordinates
242                if bedticket.bed.__parent__.maint_fee > 0:
243                    amount = bedticket.bed.__parent__.maint_fee
244                else:
245                    # fallback
246                    amount = academic_session.maint_fee
247            else:
248                return _(u'No bed allocated.'), None
249        elif category == 'dep_sug':
250            amount = 3150.0 # includes GATEWAY_AMT
251            if student.faccode == 'SPAT':
252            #    amount = 1650.0 # includes GATEWAY_AMT
253                amount = 0.0
254            if student.state == RETURNING and not previous_session:
255                p_session, p_level = self.getReturningData(student)
256        else:
257            fee_name = category + '_fee'
258            amount = getattr(academic_session, fee_name, 0.0)
259        if amount in (0.0, None):
260            return _('Amount could not be determined.'), None
261        if self.samePaymentMade(student, category, p_item, p_session):
262            return _('This type of payment has already been made.'), None
263        if self._isPaymentDisabled(p_session, category, student):
264            return _('This category of payments has been disabled.'), None
265        payment = createObject(u'waeup.StudentOnlinePayment')
266        timestamp = ("%d" % int(time()*10000))[1:]
267        payment.p_id = "p%s" % timestamp
268        payment.p_category = category
269        payment.p_item = p_item
270        payment.p_session = p_session
271        payment.p_level = p_level
272        payment.p_current = p_current
273        payment.amount_auth = amount
274        return None, payment
275
276    def warnCreditsOOR(self, studylevel, course=None):
277        """Return message if credits are out of range. In the base
278        package only maximum credits is set.
279        """
280        if course and studylevel.total_credits + course.credits > 80:
281            return _('Maximum credits exceeded.')
282        elif studylevel.total_credits > 80:
283            return _('Maximum credits exceeded.')
284        return
285
286    #: A tuple containing names of file upload viewlets which are not shown
287    #: on the `StudentClearanceManageFormPage`. Nothing is being skipped
288    #: in the base package. This attribute makes only sense, if intermediate
289    #: custom packages are being used, like we do for all Nigerian portals.
290    SKIP_UPLOAD_VIEWLETS = ('acceptanceletterupload',
291                            'higherqualificationresultupload',
292                            'advancedlevelresultupload',
293                            'evidencenameupload',
294                            'refereeletterupload',
295                            'statutorydeclarationupload',
296                            'firstsittingresultupload',
297                            'secondsittingresultupload',
298                            'certificateupload',
299                            'resultstatementupload',
300                            )
301
302    #: A tuple containing the names of registration states in which changing of
303    #: passport pictures is allowed.
304    PORTRAIT_CHANGE_STATES = (ADMITTED, RETURNING,)
305
306    def constructMatricNumber(self, student):
307        #faccode = student.faccode
308        depcode = student.depcode
309        #certcode = student.certcode
310        year = unicode(student.entry_session)[2:]
311        if not student.state in (PAID, ) or not student.is_fresh:
312            return _('Matriculation number cannot be set.'), None
313
314        # ACC/ND/17/00001
315        if student.current_mode == 'nd_ft':
316            next_integer = grok.getSite()['configuration'].next_matric_integer
317            if next_integer == 0:
318                return _('Matriculation number cannot be set.'), None
319            return None, "%s/ND/%s/%05d" % (depcode, year, next_integer)
320
321        # ACC/HND/17/00001
322        if student.current_mode == 'hnd_ft':
323            next_integer = grok.getSite()['configuration'].next_matric_integer_2
324            if next_integer == 0:
325                return _('Matriculation number cannot be set.'), None
326            return None, "%s/HND/%s/%05d" % (depcode, year, next_integer)
327
328        # PT students get the same prefixes but their counter should start
329        # with 10001. This is inconsistent because after 9999 ft students
330        # the ft and pt number ranges will overlap. Issoufou pointed this
331        # out but the director said:
332        # "when the counter gets to 9999 for ft, it can start counting
333        # along where pt is, at that moment."
334
335        # ACC/ND/17/10001
336        if student.current_mode in ('nd_pt, nd_we'):
337            next_integer = grok.getSite()['configuration'].next_matric_integer_3
338            if next_integer == 0:
339                return _('Matriculation number cannot be set.'), None
340            return None, "%s/ND/%s/%05d" % (depcode, year, next_integer)
341        # ACC/HND/17/10001
342        if student.current_mode in ('hnd_pt, hnd_we'):
343            next_integer = grok.getSite()['configuration'].next_matric_integer_4
344            if next_integer == 0:
345                return _('Matriculation number cannot be set.'), None
346            return None, "%s/HND/%s/%05d" % (depcode, year, next_integer)
347
348        return _('Matriculation number cannot be set.'), None
349
350    def increaseMatricInteger(self, student):
351        """Increase counter for matric numbers.
352        """
353        if student.current_mode == 'nd_ft':
354            grok.getSite()['configuration'].next_matric_integer += 1
355            return
356        elif student.current_mode == 'hnd_ft':
357            grok.getSite()['configuration'].next_matric_integer_2 += 1
358            return
359        elif student.current_mode in ('nd_pt, nd_we'):
360            grok.getSite()['configuration'].next_matric_integer_3 += 1
361            return
362        elif student.current_mode in ('hnd_pt, hnd_we'):
363            grok.getSite()['configuration'].next_matric_integer_4 += 1
364            return
365        return
Note: See TracBrowser for help on using the repository browser.