source: main/waeup.uniben/trunk/src/waeup/uniben/students/utils.py @ 14855

Last change on this file since 14855 was 14855, checked in by Henrik Bettermann, 7 years ago

JUPEB students also have to pay acceptance fee.

  • Property svn:keywords set to Id
File size: 19.0 KB
RevLine 
[7419]1## $Id: utils.py 14855 2017-09-28 05:54:18Z 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##
[11773]18import grok
[8598]19from time import time
[9459]20from zope.component import createObject, getUtility
[9513]21from waeup.kofa.interfaces import (IKofaUtils,
22    CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
[13251]23from waeup.kofa.utils.helpers import to_timezone
24from waeup.kofa.students.utils import trans
[8821]25from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
[8020]26from waeup.uniben.interfaces import MessageFactory as _
[6902]27
[8821]28class CustomStudentsUtils(NigeriaStudentsUtils):
[7151]29    """A collection of customized methods.
30
31    """
32
[8270]33    def getReturningData(self, student):
34        """ This method defines what happens after school fee payment
[8319]35        of returning students depending on the student's senate verdict.
[8270]36        """
[8319]37        prev_level = student['studycourse'].current_level
38        cur_verdict = student['studycourse'].current_verdict
39        if cur_verdict in ('A','B','L','M','N','Z',):
40            # Successful student
41            new_level = divmod(int(prev_level),100)[0]*100 + 100
42        elif cur_verdict == 'C':
43            # Student on probation
44            new_level = int(prev_level) + 10
45        else:
46            # Student is somehow in an undefined state.
47            # Level has to be set manually.
48            new_level = prev_level
[8270]49        new_session = student['studycourse'].current_session + 1
50        return new_session, new_level
51
[13251]52
[13248]53    def checkAccommodationRequirements(self, student, acc_details):
54        if acc_details.get('expired', False):
55            startdate = acc_details.get('startdate')
56            enddate = acc_details.get('enddate')
57            if startdate and enddate:
58                tz = getUtility(IKofaUtils).tzinfo
59                startdate = to_timezone(
60                    startdate, tz).strftime("%d/%m/%Y %H:%M:%S")
61                enddate = to_timezone(
62                    enddate, tz).strftime("%d/%m/%Y %H:%M:%S")
63                return _("Outside booking period: ${a} - ${b}",
64                         mapping = {'a': startdate, 'b': enddate})
65            else:
66                return _("Outside booking period.")
[13777]67        if not student.is_postgrad and student.current_mode != 'ug_ft':
[13446]68            return _("Only undergraduate full-time students are eligible to book accommodation.")
[13283]69        bt = acc_details.get('bt')
70        if not bt:
[13248]71            return _("Your data are incomplete.")
72        if not student.state in acc_details['allowed_states']:
73            return _("You are in the wrong registration state.")
74        if student['studycourse'].current_session != acc_details[
75            'booking_session']:
76            return _('Your current session does not '
77                     'match accommodation session.')
[13283]78        stage = bt.split('_')[2]
[13777]79        if not student.is_postgrad and stage != 'fr' and not student[
[14328]80            'studycourse'].previous_verdict in (
[14343]81                'A', 'B', 'F', 'J', 'M', 'C', 'Z'):
[13283]82            return _("Your are not eligible to book accommodation.")
[13510]83        if str(acc_details[
84            'booking_session']) in student['accommodation'].keys():
[13248]85            return _('You already booked a bed space in '
86                     'current accommodation session.')
87        return
[13244]88
[13295]89    def getAccommodationDetails(self, student):
90        """Determine the accommodation data of a student.
91        """
92        d = {}
93        d['error'] = u''
94        hostels = grok.getSite()['hostels']
95        d['booking_session'] = hostels.accommodation_session
96        d['allowed_states'] = hostels.accommodation_states
97        d['startdate'] = hostels.startdate
98        d['enddate'] = hostels.enddate
99        d['expired'] = hostels.expired
100        # Determine bed type
101        studycourse = student['studycourse']
102        certificate = getattr(studycourse,'certificate',None)
103        entry_session = studycourse.entry_session
104        current_level = studycourse.current_level
105        if None in (entry_session, current_level, certificate):
106            return d
107        if student.sex == 'f':
108            sex = 'female'
109        else:
110            sex = 'male'
[13777]111        if student.is_postgrad:
112            bt = 'all'
113            special_handling = 'pg'
114        else:
115            end_level = certificate.end_level
116            if current_level == 10:
117                bt = 'pr'
118            elif entry_session == grok.getSite()['hostels'].accommodation_session:
119                bt = 'fr'
120            elif current_level >= end_level:
121                bt = 'fi'
122            else:
123                bt = 're'
124            special_handling = 'regular'
125            if student.faccode in ('MED', 'DEN'):
126                special_handling = 'clinical'
127            elif student.certcode in ('BARTMAS', 'BARTTHR', 'BARTFAA',
128                                      'BAEDFAA', 'BSCEDECHED'):
129                special_handling = 'ekenwan'
[13295]130        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
131        return d
132
[9520]133    def _paymentMade(self, student, session):
134        if len(student['payments']):
135            for ticket in student['payments'].values():
136                if ticket.p_state == 'paid' and \
137                    ticket.p_category == 'schoolfee' and \
138                    ticket.p_session == session:
139                    return True
140        return False
141
[13566]142    def _isPaymentDisabled(self, p_session, category, student):
143        academic_session = self._getSessionConfiguration(p_session)
[14688]144        if category == 'schoolfee':
145            if 'sf_all' in academic_session.payment_disabled:
146                return True
147            if student.current_mode == 'found' and \
148                'sf_found' in academic_session.payment_disabled:
149                return True
150            if student.is_postgrad:
151                if 'sf_pg' in academic_session.payment_disabled:
152                    return True
153                return False
154            if student.current_mode.endswith('ft') and \
155                'sf_ft' in academic_session.payment_disabled:
156                return True
157            if student.current_mode.endswith('pt') and \
158                'sf_pt' in academic_session.payment_disabled:
159                return True
160            if student.current_mode.startswith('dp') and \
161                'sf_dp' in academic_session.payment_disabled:
162                return True
163            if student.current_mode.endswith('sw') and \
164                'sf_sw' in academic_session.payment_disabled:
165                return True
[13566]166        if category == 'hostel_maintenance' and \
167            'maint_all' in academic_session.payment_disabled:
168            return True
169        return False
170
[13251]171    #def _hostelApplicationPaymentMade(self, student, session):
172    #    if len(student['payments']):
173    #        for ticket in student['payments'].values():
174    #            if ticket.p_state == 'paid' and \
175    #                ticket.p_category == 'hostel_application' and \
176    #                ticket.p_session == session:
177    #                return True
178    #    return False
[12566]179
[9152]180    def setPaymentDetails(self, category, student,
181            previous_session, previous_level):
[8598]182        """Create Payment object and set the payment data of a student for
183        the payment category specified.
184
185        """
186        p_item = u''
187        amount = 0.0
[9152]188        if previous_session:
[9520]189            if previous_session < student['studycourse'].entry_session:
190                return _('The previous session must not fall below '
191                         'your entry session.'), None
192            if category == 'schoolfee':
193                # School fee is always paid for the following session
194                if previous_session > student['studycourse'].current_session:
195                    return _('This is not a previous session.'), None
196            else:
197                if previous_session > student['studycourse'].current_session - 1:
198                    return _('This is not a previous session.'), None
[9152]199            p_session = previous_session
200            p_level = previous_level
201            p_current = False
202        else:
203            p_session = student['studycourse'].current_session
204            p_level = student['studycourse'].current_level
205            p_current = True
[9520]206        academic_session = self._getSessionConfiguration(p_session)
207        if academic_session == None:
[8598]208            return _(u'Session configuration object is not available.'), None
[8676]209        # Determine fee.
[7151]210        if category == 'transfer':
[8598]211            amount = academic_session.transfer_fee
[10469]212        elif category == 'transcript':
213            amount = academic_session.transcript_fee
[7151]214        elif category == 'gown':
[8598]215            amount = academic_session.gown_fee
[13785]216        elif category == 'jupeb':
217            amount = academic_session.jupeb_fee
[14358]218        elif category == 'clinexam':
219            amount = academic_session.clinexam_fee
[13251]220        elif category == 'bed_allocation':
221            p_item = self.getAccommodationDetails(student)['bt']
222            amount = academic_session.booking_fee
[13283]223            # Add student union dues
[13777]224            if not student.is_postgrad:
225                stage = self.getAccommodationDetails(student)['bt']
226                stage = stage.split('_')[2]
227                if stage == 'fr':
228                    amount += 500.0
229                elif stage in ('fi', 're') and student[
[14343]230                    'studycourse'].previous_verdict in (
231                        'A', 'B', 'F', 'J', 'M', 'C', 'Z'):
[13777]232                    amount += 300.0
233                else:
234                    amount = 0.0
[13251]235        elif category == 'hostel_maintenance':
236            amount = 0.0
237            bedticket = student['accommodation'].get(
238                str(student.current_session), None)
[13500]239            if bedticket is not None and bedticket.bed is not None:
[13251]240                p_item = bedticket.bed_coordinates
241                if bedticket.bed.__parent__.maint_fee > 0:
242                    amount = bedticket.bed.__parent__.maint_fee
243                else:
244                    # fallback
245                    amount = academic_session.maint_fee
246            else:
[13508]247                return _(u'No bed allocated.'), None
[13251]248        #elif category == 'hostel_application':
249        #    amount = 1000.0
250        #elif category.startswith('tempmaint'):
251        #    if not self._hostelApplicationPaymentMade(
252        #        student, student.current_session):
253        #        return _(
254        #            'You have not yet paid the hostel application fee.'), None
255        #    if category == 'tempmaint_1':
256        #        amount = 8150.0
257        #    elif category == 'tempmaint_2':
258        #        amount = 12650.0
259        #    elif category == 'tempmaint_3':
260        #        amount = 9650.0
[7151]261        elif category == 'clearance':
[9796]262            p_item = student.certcode
263            if p_item is None:
[8598]264                return _('Study course data are incomplete.'), None
[14855]265            #if student.faccode == 'JUPEB':
266            #    return _('No payment required.'), None
[13869]267            if student.faccode.startswith('FCETA'):
268                # ASABA and AKOKA
[13631]269                amount = 30000.0
[11479]270            elif p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS',
[14289]271                'MBBSMED', 'MBBSNDU', 'BSCPTY'):
[14237]272                amount = 65000.0
[11479]273            else:
[14237]274                amount = 45000.0
275            # Clearance fee and provider amount have been increased
276            # for new students.
277            if student.entry_session >= 2016:
278                amount += 5000.0
[7151]279        elif category == 'schoolfee':
[8598]280            try:
281                certificate = student['studycourse'].certificate
282                p_item = certificate.code
283            except (AttributeError, TypeError):
284                return _('Study course data are incomplete.'), None
[13332]285
286            #####################################################
[13340]287            #if student.faccode == 'JUPEB':
288            #    return _('Payment temporarily disabled.'), None
[13332]289            #####################################################
290
291
[9152]292            if previous_session:
[9520]293                # Students can pay for previous sessions in all workflow states.
294                # Fresh students are excluded by the update method of the
295                # PreviousPaymentAddFormPage.
[9157]296                if previous_session == student['studycourse'].entry_session:
[9152]297                    if student.is_foreigner:
298                        amount = getattr(certificate, 'school_fee_3', 0.0)
299                    else:
300                        amount = getattr(certificate, 'school_fee_1', 0.0)
[9006]301                else:
[9152]302                    if student.is_foreigner:
303                        amount = getattr(certificate, 'school_fee_4', 0.0)
304                    else:
305                        amount = getattr(certificate, 'school_fee_2', 0.0)
306            else:
307                if student.state == CLEARED:
308                    if student.is_foreigner:
309                        amount = getattr(certificate, 'school_fee_3', 0.0)
310                    else:
311                        amount = getattr(certificate, 'school_fee_1', 0.0)
[9513]312                elif student.state in (PAID, REGISTERED, VALIDATED):
313                    p_session += 1
314                    # We don't know which level the student is paying for.
315                    p_level = None
[9520]316                    academic_session = self._getSessionConfiguration(p_session)
317                    if academic_session == None:
[9513]318                        return _(u'Session configuration object is not available.'), None
[9570]319
[9520]320                    # Students are only allowed to pay for the next session
321                    # if current session payment
322                    # has really been made, i.e. payment object exists.
[9570]323                    #if not self._paymentMade(
324                    #    student, student.current_session):
325                    #    return _('You have not yet paid your current/active' +
326                    #             ' session. Please use the previous session' +
327                    #             ' payment form first.'), None
328
[9513]329                    if student.is_foreigner:
330                        amount = getattr(certificate, 'school_fee_4', 0.0)
331                    else:
332                        amount = getattr(certificate, 'school_fee_2', 0.0)
[9152]333                elif student.state == RETURNING:
334                    # In case of returning school fee payment the payment session
335                    # and level contain the values of the session the student
336                    # has paid for.
337                    p_session, p_level = self.getReturningData(student)
[9520]338                    academic_session = self._getSessionConfiguration(p_session)
339                    if academic_session == None:
[9152]340                        return _(u'Session configuration object is not available.'), None
[9570]341
[9520]342                    # Students are only allowed to pay for the next session
343                    # if current session payment has really been made,
344                    # i.e. payment object exists and is paid.
[9570]345                    #if not self._paymentMade(
346                    #    student, student.current_session):
347                    #    return _('You have not yet paid your current/active' +
348                    #             ' session. Please use the previous session' +
349                    #             ' payment form first.'), None
350
[9152]351                    if student.is_foreigner:
352                        amount = getattr(certificate, 'school_fee_4', 0.0)
353                    else:
354                        amount = getattr(certificate, 'school_fee_2', 0.0)
[9006]355            # Give 50% school fee discount to staff members.
356            if student.is_staff:
357                amount /= 2
[8598]358        if amount in (0.0, None):
[9520]359            return _('Amount could not be determined.'), None
[8676]360        # Add session specific penalty fee.
361        if category == 'schoolfee' and student.is_postgrad:
362            amount += academic_session.penalty_pg
[13757]363        elif category == 'schoolfee' and student.current_mode == ('ug_ft'):
[13756]364            amount += academic_session.penalty_ug_ft
[13757]365        elif category == 'schoolfee' and student.current_mode == ('ug_pt'):
[13756]366            amount += academic_session.penalty_ug_pt
[13998]367        elif category == 'schoolfee' and student.current_mode == ('ug_sw'):
368            amount += academic_session.penalty_sw
[14717]369        elif category == 'schoolfee' and student.current_mode in (
370            'dp_ft', 'dp_pt'):
371            amount += academic_session.penalty_dp
[9727]372        if category.startswith('tempmaint'):
373            p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category]
374            p_item = unicode(p_item)
375            # Now we change the category because tempmaint payments
[12566]376            # will be obsolete when Uniben returns to Kofa bed allocation.
[9727]377            category = 'hostel_maintenance'
[8676]378        # Create ticket.
[11644]379        if self.samePaymentMade(student, category, p_item, p_session):
380            return _('This type of payment has already been made.'), None
[11459]381        if self._isPaymentDisabled(p_session, category, student):
[13814]382            return _('This category of payments has been disabled.'), None
[8715]383        payment = createObject(u'waeup.StudentOnlinePayment')
[8950]384        timestamp = ("%d" % int(time()*10000))[1:]
[8598]385        payment.p_id = "p%s" % timestamp
386        payment.p_category = category
387        payment.p_item = p_item
388        payment.p_session = p_session
389        payment.p_level = p_level
[9152]390        payment.p_current = p_current
[8598]391        payment.amount_auth = amount
392        return None, payment
[7621]393
[14588]394    def warnCreditsOOR(self, studylevel, course=None):
[9831]395        studycourse = studylevel.__parent__
396        certificate = getattr(studycourse,'certificate', None)
397        current_level = studycourse.current_level
398        if None in (current_level, certificate):
[14588]399            return
[9831]400        end_level = certificate.end_level
401        if current_level >= end_level:
[14588]402            limit = 51
403        else:
404            limit = 50
405        if course and studylevel.total_credits + course.credits > limit:
406            return _('Maximum credits exceeded.')
407        elif studylevel.total_credits > limit:
408            return _('Maximum credits exceeded.')
409        return
[9831]410
[11773]411    def clearance_disabled_message(self, student):
412        if student.is_postgrad:
413            return None
414        try:
415            session_config = grok.getSite()[
416                'configuration'][str(student.current_session)]
417        except KeyError:
418            return _('Session configuration object is not available.')
419        if not session_config.clearance_enabled:
420            return _('Clearance is disabled for this session.')
421        return None
422
[14038]423    #: A tuple containing the names of registration states in which changing of
424    #: passport pictures is allowed.
425    PORTRAIT_CHANGE_STATES = ()
426
[8441]427    # Uniben prefix
[8413]428    STUDENT_ID_PREFIX = u'B'
Note: See TracBrowser for help on using the repository browser.