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

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

Uups, that was the wrong utility class.

  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1## $Id: utils.py 13244 2015-09-04 17:17:03Z 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    CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
23from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
24from waeup.uniben.interfaces import MessageFactory as _
25
26class CustomStudentsUtils(NigeriaStudentsUtils):
27    """A collection of customized methods.
28
29    """
30
31    def getReturningData(self, student):
32        """ This method defines what happens after school fee payment
33        of returning students depending on the student's senate verdict.
34        """
35        prev_level = student['studycourse'].current_level
36        cur_verdict = student['studycourse'].current_verdict
37        if cur_verdict in ('A','B','L','M','N','Z',):
38            # Successful student
39            new_level = divmod(int(prev_level),100)[0]*100 + 100
40        elif cur_verdict == 'C':
41            # Student on probation
42            new_level = int(prev_level) + 10
43        else:
44            # Student is somehow in an undefined state.
45            # Level has to be set manually.
46            new_level = prev_level
47        new_session = student['studycourse'].current_session + 1
48        return new_session, new_level
49
50    def getAccommodationDetails(self, student):
51        """Determine the accommodation data of a student.
52        """
53        d = {}
54        d['error'] = u''
55        hostels = grok.getSite()['hostels']
56        d['booking_session'] = hostels.accommodation_session
57        d['allowed_states'] = hostels.accommodation_states
58        d['startdate'] = hostels.startdate
59        d['enddate'] = hostels.enddate
60        d['expired'] = hostels.expired
61        # Determine bed type
62        studycourse = student['studycourse']
63        certificate = getattr(studycourse,'certificate',None)
64        entry_session = studycourse.entry_session
65        current_level = studycourse.current_level
66        if None in (entry_session, current_level, certificate):
67            return d
68        end_level = certificate.end_level
69        if current_level == 10:
70            bt = 'pr'
71        elif entry_session == grok.getSite()['hostels'].accommodation_session:
72            bt = 'fr'
73        elif current_level >= end_level:
74            bt = 'fi'
75        else:
76            bt = 're'
77        if student.sex == 'f':
78            sex = 'female'
79        else:
80            sex = 'male'
81        special_handling = 'regular'
82        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
83        return d
84
85    def _paymentMade(self, student, session):
86        if len(student['payments']):
87            for ticket in student['payments'].values():
88                if ticket.p_state == 'paid' and \
89                    ticket.p_category == 'schoolfee' and \
90                    ticket.p_session == session:
91                    return True
92        return False
93
94    def _hostelApplicationPaymentMade(self, student, session):
95        if len(student['payments']):
96            for ticket in student['payments'].values():
97                if ticket.p_state == 'paid' and \
98                    ticket.p_category == 'hostel_application' and \
99                    ticket.p_session == session:
100                    return True
101        return False
102
103    def setPaymentDetails(self, category, student,
104            previous_session, previous_level):
105        """Create Payment object and set the payment data of a student for
106        the payment category specified.
107
108        """
109        p_item = u''
110        amount = 0.0
111        if previous_session:
112            if previous_session < student['studycourse'].entry_session:
113                return _('The previous session must not fall below '
114                         'your entry session.'), None
115            if category == 'schoolfee':
116                # School fee is always paid for the following session
117                if previous_session > student['studycourse'].current_session:
118                    return _('This is not a previous session.'), None
119            else:
120                if previous_session > student['studycourse'].current_session - 1:
121                    return _('This is not a previous session.'), None
122            p_session = previous_session
123            p_level = previous_level
124            p_current = False
125        else:
126            p_session = student['studycourse'].current_session
127            p_level = student['studycourse'].current_level
128            p_current = True
129        academic_session = self._getSessionConfiguration(p_session)
130        if academic_session == None:
131            return _(u'Session configuration object is not available.'), None
132        # Determine fee.
133        if category == 'transfer':
134            amount = academic_session.transfer_fee
135        elif category == 'transcript':
136            amount = academic_session.transcript_fee
137        elif category == 'gown':
138            amount = academic_session.gown_fee
139        #elif category == 'bed_allocation':
140        #    amount = academic_session.booking_fee
141        #elif category == 'hostel_maintenance':
142        #    amount = academic_session.maint_fee
143        elif category == 'hostel_application':
144            amount = 1000.0
145        elif category.startswith('tempmaint'):
146            if not self._hostelApplicationPaymentMade(
147                student, student.current_session):
148                return _(
149                    'You have not yet paid the hostel application fee.'), None
150            if category == 'tempmaint_1':
151                amount = 8150.0
152            elif category == 'tempmaint_2':
153                amount = 12650.0
154            elif category == 'tempmaint_3':
155                amount = 9650.0
156        elif category == 'clearance':
157            p_item = student.certcode
158            if p_item is None:
159                return _('Study course data are incomplete.'), None
160            if student.faccode == 'FCETA':
161                amount = 22500.0
162            elif p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS',
163                'MBBSMED', 'MBBSNDU'):
164                amount = 65000.0
165            elif p_item in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET',
166                'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'):
167                amount = 22500.0
168            else:
169                amount = 45000.0
170        elif category == 'schoolfee':
171            try:
172                certificate = student['studycourse'].certificate
173                p_item = certificate.code
174            except (AttributeError, TypeError):
175                return _('Study course data are incomplete.'), None
176            if previous_session:
177                # Students can pay for previous sessions in all workflow states.
178                # Fresh students are excluded by the update method of the
179                # PreviousPaymentAddFormPage.
180                if previous_session == student['studycourse'].entry_session:
181                    if student.is_foreigner:
182                        amount = getattr(certificate, 'school_fee_3', 0.0)
183                    else:
184                        amount = getattr(certificate, 'school_fee_1', 0.0)
185                else:
186                    if student.is_foreigner:
187                        amount = getattr(certificate, 'school_fee_4', 0.0)
188                    else:
189                        amount = getattr(certificate, 'school_fee_2', 0.0)
190            else:
191                if student.state == CLEARED:
192                    if student.is_foreigner:
193                        amount = getattr(certificate, 'school_fee_3', 0.0)
194                    else:
195                        amount = getattr(certificate, 'school_fee_1', 0.0)
196                elif student.state in (PAID, REGISTERED, VALIDATED):
197                    p_session += 1
198                    # We don't know which level the student is paying for.
199                    p_level = None
200                    academic_session = self._getSessionConfiguration(p_session)
201                    if academic_session == None:
202                        return _(u'Session configuration object is not available.'), None
203
204                    # Students are only allowed to pay for the next session
205                    # if current session payment
206                    # has really been made, i.e. payment object exists.
207                    #if not self._paymentMade(
208                    #    student, student.current_session):
209                    #    return _('You have not yet paid your current/active' +
210                    #             ' session. Please use the previous session' +
211                    #             ' payment form first.'), None
212
213                    if student.is_foreigner:
214                        amount = getattr(certificate, 'school_fee_4', 0.0)
215                    else:
216                        amount = getattr(certificate, 'school_fee_2', 0.0)
217                elif student.state == RETURNING:
218                    # In case of returning school fee payment the payment session
219                    # and level contain the values of the session the student
220                    # has paid for.
221                    p_session, p_level = self.getReturningData(student)
222                    academic_session = self._getSessionConfiguration(p_session)
223                    if academic_session == None:
224                        return _(u'Session configuration object is not available.'), None
225
226                    # Students are only allowed to pay for the next session
227                    # if current session payment has really been made,
228                    # i.e. payment object exists and is paid.
229                    #if not self._paymentMade(
230                    #    student, student.current_session):
231                    #    return _('You have not yet paid your current/active' +
232                    #             ' session. Please use the previous session' +
233                    #             ' payment form first.'), None
234
235                    if student.is_foreigner:
236                        amount = getattr(certificate, 'school_fee_4', 0.0)
237                    else:
238                        amount = getattr(certificate, 'school_fee_2', 0.0)
239            # Give 50% school fee discount to staff members.
240            if student.is_staff:
241                amount /= 2
242        if amount in (0.0, None):
243            return _('Amount could not be determined.'), None
244        # Add session specific penalty fee.
245        if category == 'schoolfee' and student.is_postgrad:
246            amount += academic_session.penalty_pg
247        elif category == 'schoolfee':
248            amount += academic_session.penalty_ug
249        if category.startswith('tempmaint'):
250            p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category]
251            p_item = unicode(p_item)
252            # Now we change the category because tempmaint payments
253            # will be obsolete when Uniben returns to Kofa bed allocation.
254            category = 'hostel_maintenance'
255        # Create ticket.
256        if self.samePaymentMade(student, category, p_item, p_session):
257            return _('This type of payment has already been made.'), None
258        if self._isPaymentDisabled(p_session, category, student):
259            return _('Payment temporarily disabled.'), None
260        payment = createObject(u'waeup.StudentOnlinePayment')
261        timestamp = ("%d" % int(time()*10000))[1:]
262        payment.p_id = "p%s" % timestamp
263        payment.p_category = category
264        payment.p_item = p_item
265        payment.p_session = p_session
266        payment.p_level = p_level
267        payment.p_current = p_current
268        payment.amount_auth = amount
269        return None, payment
270
271    def maxCredits(self, studylevel):
272        """Return maximum credits.
273
274        """
275        studycourse = studylevel.__parent__
276        certificate = getattr(studycourse,'certificate', None)
277        current_level = studycourse.current_level
278        if None in (current_level, certificate):
279            return 0
280        end_level = certificate.end_level
281        if current_level >= end_level:
282            return 51
283        return 50
284
285    def clearance_disabled_message(self, student):
286        if student.is_postgrad:
287            return None
288        try:
289            session_config = grok.getSite()[
290                'configuration'][str(student.current_session)]
291        except KeyError:
292            return _('Session configuration object is not available.')
293        if not session_config.clearance_enabled:
294            return _('Clearance is disabled for this session.')
295        return None
296
297    # Uniben prefix
298    STUDENT_ID_PREFIX = u'B'
Note: See TracBrowser for help on using the repository browser.