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

Last change on this file since 13252 was 13252, checked in by Henrik Bettermann, 9 years ago

Only students with verdict 'A' or 'B' are eligible to book accommodation.

  • Property svn:keywords set to Id
File size: 14.0 KB
RevLine 
[7419]1## $Id: utils.py 13252 2015-09-06 07:23:27Z 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.")
[13251]67        if not student.current_verdict in ('A', 'B'):
[13252]68            return _("Your are not eligible to book accommodation.")
[13248]69        if not acc_details.get('bt'):
70            return _("Your data are incomplete.")
71        if not student.state in acc_details['allowed_states']:
72            return _("You are in the wrong registration state.")
73        if student['studycourse'].current_session != acc_details[
74            'booking_session']:
75            return _('Your current session does not '
76                     'match accommodation session.')
77        if str(acc_details['booking_session']) in student['accommodation'].keys():
78            return _('You already booked a bed space in '
79                     'current accommodation session.')
80        return
[13244]81
[9520]82    def _paymentMade(self, student, session):
83        if len(student['payments']):
84            for ticket in student['payments'].values():
85                if ticket.p_state == 'paid' and \
86                    ticket.p_category == 'schoolfee' and \
87                    ticket.p_session == session:
88                    return True
89        return False
90
[13251]91    #def _hostelApplicationPaymentMade(self, student, session):
92    #    if len(student['payments']):
93    #        for ticket in student['payments'].values():
94    #            if ticket.p_state == 'paid' and \
95    #                ticket.p_category == 'hostel_application' and \
96    #                ticket.p_session == session:
97    #                return True
98    #    return False
[12566]99
[9152]100    def setPaymentDetails(self, category, student,
101            previous_session, previous_level):
[8598]102        """Create Payment object and set the payment data of a student for
103        the payment category specified.
104
105        """
106        p_item = u''
107        amount = 0.0
[9152]108        if previous_session:
[9520]109            if previous_session < student['studycourse'].entry_session:
110                return _('The previous session must not fall below '
111                         'your entry session.'), None
112            if category == 'schoolfee':
113                # School fee is always paid for the following session
114                if previous_session > student['studycourse'].current_session:
115                    return _('This is not a previous session.'), None
116            else:
117                if previous_session > student['studycourse'].current_session - 1:
118                    return _('This is not a previous session.'), None
[9152]119            p_session = previous_session
120            p_level = previous_level
121            p_current = False
122        else:
123            p_session = student['studycourse'].current_session
124            p_level = student['studycourse'].current_level
125            p_current = True
[9520]126        academic_session = self._getSessionConfiguration(p_session)
127        if academic_session == None:
[8598]128            return _(u'Session configuration object is not available.'), None
[8676]129        # Determine fee.
[7151]130        if category == 'transfer':
[8598]131            amount = academic_session.transfer_fee
[10469]132        elif category == 'transcript':
133            amount = academic_session.transcript_fee
[7151]134        elif category == 'gown':
[8598]135            amount = academic_session.gown_fee
[13251]136        elif category == 'bed_allocation':
137            p_item = self.getAccommodationDetails(student)['bt']
138            amount = academic_session.booking_fee
139        elif category == 'hostel_maintenance':
140            amount = 0.0
141            bedticket = student['accommodation'].get(
142                str(student.current_session), None)
143            if bedticket:
144                p_item = bedticket.bed_coordinates
145                if bedticket.bed.__parent__.maint_fee > 0:
146                    amount = bedticket.bed.__parent__.maint_fee
147                else:
148                    # fallback
149                    amount = academic_session.maint_fee
150            else:
151                # Should not happen because this is already checked
152                # in the browser module, but anyway ...
153                portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
154                p_item = trans(_('no bed allocated'), portal_language)
155        #elif category == 'hostel_application':
156        #    amount = 1000.0
157        #elif category.startswith('tempmaint'):
158        #    if not self._hostelApplicationPaymentMade(
159        #        student, student.current_session):
160        #        return _(
161        #            'You have not yet paid the hostel application fee.'), None
162        #    if category == 'tempmaint_1':
163        #        amount = 8150.0
164        #    elif category == 'tempmaint_2':
165        #        amount = 12650.0
166        #    elif category == 'tempmaint_3':
167        #        amount = 9650.0
[7151]168        elif category == 'clearance':
[9796]169            p_item = student.certcode
170            if p_item is None:
[8598]171                return _('Study course data are incomplete.'), None
[11479]172            if student.faccode == 'FCETA':
173                amount = 22500.0
174            elif p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS',
175                'MBBSMED', 'MBBSNDU'):
176                amount = 65000.0
177            elif p_item in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET',
178                'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'):
179                amount = 22500.0
180            else:
[9346]181                amount = 45000.0
[7151]182        elif category == 'schoolfee':
[8598]183            try:
184                certificate = student['studycourse'].certificate
185                p_item = certificate.code
186            except (AttributeError, TypeError):
187                return _('Study course data are incomplete.'), None
[9152]188            if previous_session:
[9520]189                # Students can pay for previous sessions in all workflow states.
190                # Fresh students are excluded by the update method of the
191                # PreviousPaymentAddFormPage.
[9157]192                if previous_session == student['studycourse'].entry_session:
[9152]193                    if student.is_foreigner:
194                        amount = getattr(certificate, 'school_fee_3', 0.0)
195                    else:
196                        amount = getattr(certificate, 'school_fee_1', 0.0)
[9006]197                else:
[9152]198                    if student.is_foreigner:
199                        amount = getattr(certificate, 'school_fee_4', 0.0)
200                    else:
201                        amount = getattr(certificate, 'school_fee_2', 0.0)
202            else:
203                if student.state == CLEARED:
204                    if student.is_foreigner:
205                        amount = getattr(certificate, 'school_fee_3', 0.0)
206                    else:
207                        amount = getattr(certificate, 'school_fee_1', 0.0)
[9513]208                elif student.state in (PAID, REGISTERED, VALIDATED):
209                    p_session += 1
210                    # We don't know which level the student is paying for.
211                    p_level = None
[9520]212                    academic_session = self._getSessionConfiguration(p_session)
213                    if academic_session == None:
[9513]214                        return _(u'Session configuration object is not available.'), None
[9570]215
[9520]216                    # Students are only allowed to pay for the next session
217                    # if current session payment
218                    # has really been made, i.e. payment object exists.
[9570]219                    #if not self._paymentMade(
220                    #    student, student.current_session):
221                    #    return _('You have not yet paid your current/active' +
222                    #             ' session. Please use the previous session' +
223                    #             ' payment form first.'), None
224
[9513]225                    if student.is_foreigner:
226                        amount = getattr(certificate, 'school_fee_4', 0.0)
227                    else:
228                        amount = getattr(certificate, 'school_fee_2', 0.0)
[9152]229                elif student.state == RETURNING:
230                    # In case of returning school fee payment the payment session
231                    # and level contain the values of the session the student
232                    # has paid for.
233                    p_session, p_level = self.getReturningData(student)
[9520]234                    academic_session = self._getSessionConfiguration(p_session)
235                    if academic_session == None:
[9152]236                        return _(u'Session configuration object is not available.'), None
[9570]237
[9520]238                    # Students are only allowed to pay for the next session
239                    # if current session payment has really been made,
240                    # i.e. payment object exists and is paid.
[9570]241                    #if not self._paymentMade(
242                    #    student, student.current_session):
243                    #    return _('You have not yet paid your current/active' +
244                    #             ' session. Please use the previous session' +
245                    #             ' payment form first.'), None
246
[9152]247                    if student.is_foreigner:
248                        amount = getattr(certificate, 'school_fee_4', 0.0)
249                    else:
250                        amount = getattr(certificate, 'school_fee_2', 0.0)
[9006]251            # Give 50% school fee discount to staff members.
252            if student.is_staff:
253                amount /= 2
[8598]254        if amount in (0.0, None):
[9520]255            return _('Amount could not be determined.'), None
[8676]256        # Add session specific penalty fee.
257        if category == 'schoolfee' and student.is_postgrad:
258            amount += academic_session.penalty_pg
259        elif category == 'schoolfee':
260            amount += academic_session.penalty_ug
[9727]261        if category.startswith('tempmaint'):
262            p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category]
263            p_item = unicode(p_item)
264            # Now we change the category because tempmaint payments
[12566]265            # will be obsolete when Uniben returns to Kofa bed allocation.
[9727]266            category = 'hostel_maintenance'
[8676]267        # Create ticket.
[11644]268        if self.samePaymentMade(student, category, p_item, p_session):
269            return _('This type of payment has already been made.'), None
[11459]270        if self._isPaymentDisabled(p_session, category, student):
271            return _('Payment temporarily disabled.'), None
[8715]272        payment = createObject(u'waeup.StudentOnlinePayment')
[8950]273        timestamp = ("%d" % int(time()*10000))[1:]
[8598]274        payment.p_id = "p%s" % timestamp
275        payment.p_category = category
276        payment.p_item = p_item
277        payment.p_session = p_session
278        payment.p_level = p_level
[9152]279        payment.p_current = p_current
[8598]280        payment.amount_auth = amount
281        return None, payment
[7621]282
[9831]283    def maxCredits(self, studylevel):
284        """Return maximum credits.
285
286        """
287        studycourse = studylevel.__parent__
288        certificate = getattr(studycourse,'certificate', None)
289        current_level = studycourse.current_level
290        if None in (current_level, certificate):
291            return 0
292        end_level = certificate.end_level
293        if current_level >= end_level:
294            return 51
295        return 50
296
[11773]297    def clearance_disabled_message(self, student):
298        if student.is_postgrad:
299            return None
300        try:
301            session_config = grok.getSite()[
302                'configuration'][str(student.current_session)]
303        except KeyError:
304            return _('Session configuration object is not available.')
305        if not session_config.clearance_enabled:
306            return _('Clearance is disabled for this session.')
307        return None
308
[8441]309    # Uniben prefix
[8413]310    STUDENT_ID_PREFIX = u'B'
Note: See TracBrowser for help on using the repository browser.