1 | ## $Id: utils.py 13631 2016-01-17 09:29: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 | ## |
---|
18 | import grok |
---|
19 | from time import time |
---|
20 | from zope.component import createObject, getUtility |
---|
21 | from waeup.kofa.interfaces import (IKofaUtils, |
---|
22 | CLEARED, RETURNING, PAID, REGISTERED, VALIDATED) |
---|
23 | from waeup.kofa.utils.helpers import to_timezone |
---|
24 | from waeup.kofa.students.utils import trans |
---|
25 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
26 | from waeup.uniben.interfaces import MessageFactory as _ |
---|
27 | |
---|
28 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
29 | """A collection of customized methods. |
---|
30 | |
---|
31 | """ |
---|
32 | |
---|
33 | def getReturningData(self, student): |
---|
34 | """ This method defines what happens after school fee payment |
---|
35 | of returning students depending on the student's senate verdict. |
---|
36 | """ |
---|
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 |
---|
49 | new_session = student['studycourse'].current_session + 1 |
---|
50 | return new_session, new_level |
---|
51 | |
---|
52 | |
---|
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.") |
---|
67 | if student.current_mode != 'ug_ft': |
---|
68 | return _("Only undergraduate full-time students are eligible to book accommodation.") |
---|
69 | bt = acc_details.get('bt') |
---|
70 | if not bt: |
---|
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.') |
---|
78 | stage = bt.split('_')[2] |
---|
79 | if stage != 'fr' and not student[ |
---|
80 | 'studycourse'].previous_verdict in ('A', 'B', 'F', 'J', 'C', 'Z'): |
---|
81 | return _("Your are not eligible to book accommodation.") |
---|
82 | if str(acc_details[ |
---|
83 | 'booking_session']) in student['accommodation'].keys(): |
---|
84 | return _('You already booked a bed space in ' |
---|
85 | 'current accommodation session.') |
---|
86 | return |
---|
87 | |
---|
88 | def getAccommodationDetails(self, student): |
---|
89 | """Determine the accommodation data of a student. |
---|
90 | """ |
---|
91 | d = {} |
---|
92 | d['error'] = u'' |
---|
93 | hostels = grok.getSite()['hostels'] |
---|
94 | d['booking_session'] = hostels.accommodation_session |
---|
95 | d['allowed_states'] = hostels.accommodation_states |
---|
96 | d['startdate'] = hostels.startdate |
---|
97 | d['enddate'] = hostels.enddate |
---|
98 | d['expired'] = hostels.expired |
---|
99 | # Determine bed type |
---|
100 | studycourse = student['studycourse'] |
---|
101 | certificate = getattr(studycourse,'certificate',None) |
---|
102 | entry_session = studycourse.entry_session |
---|
103 | current_level = studycourse.current_level |
---|
104 | if None in (entry_session, current_level, certificate): |
---|
105 | return d |
---|
106 | end_level = certificate.end_level |
---|
107 | if current_level == 10: |
---|
108 | bt = 'pr' |
---|
109 | elif entry_session == grok.getSite()['hostels'].accommodation_session: |
---|
110 | bt = 'fr' |
---|
111 | elif current_level >= end_level: |
---|
112 | bt = 'fi' |
---|
113 | else: |
---|
114 | bt = 're' |
---|
115 | if student.sex == 'f': |
---|
116 | sex = 'female' |
---|
117 | else: |
---|
118 | sex = 'male' |
---|
119 | special_handling = 'regular' |
---|
120 | if student.faccode in ('MED', 'DEN'): |
---|
121 | special_handling = 'clinical' |
---|
122 | elif student.certcode in ('BARTMAS', 'BARTTHR', 'BARTFAA', |
---|
123 | 'BAEDFAA', 'BSCEDECHED'): |
---|
124 | special_handling = 'ekenwan' |
---|
125 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
126 | return d |
---|
127 | |
---|
128 | def _paymentMade(self, student, session): |
---|
129 | if len(student['payments']): |
---|
130 | for ticket in student['payments'].values(): |
---|
131 | if ticket.p_state == 'paid' and \ |
---|
132 | ticket.p_category == 'schoolfee' and \ |
---|
133 | ticket.p_session == session: |
---|
134 | return True |
---|
135 | return False |
---|
136 | |
---|
137 | def _isPaymentDisabled(self, p_session, category, student): |
---|
138 | academic_session = self._getSessionConfiguration(p_session) |
---|
139 | if category == 'schoolfee' and \ |
---|
140 | 'sf_all' in academic_session.payment_disabled: |
---|
141 | return True |
---|
142 | if category == 'hostel_maintenance' and \ |
---|
143 | 'maint_all' in academic_session.payment_disabled: |
---|
144 | return True |
---|
145 | return False |
---|
146 | |
---|
147 | #def _hostelApplicationPaymentMade(self, student, session): |
---|
148 | # if len(student['payments']): |
---|
149 | # for ticket in student['payments'].values(): |
---|
150 | # if ticket.p_state == 'paid' and \ |
---|
151 | # ticket.p_category == 'hostel_application' and \ |
---|
152 | # ticket.p_session == session: |
---|
153 | # return True |
---|
154 | # return False |
---|
155 | |
---|
156 | def setPaymentDetails(self, category, student, |
---|
157 | previous_session, previous_level): |
---|
158 | """Create Payment object and set the payment data of a student for |
---|
159 | the payment category specified. |
---|
160 | |
---|
161 | """ |
---|
162 | p_item = u'' |
---|
163 | amount = 0.0 |
---|
164 | if previous_session: |
---|
165 | if previous_session < student['studycourse'].entry_session: |
---|
166 | return _('The previous session must not fall below ' |
---|
167 | 'your entry session.'), None |
---|
168 | if category == 'schoolfee': |
---|
169 | # School fee is always paid for the following session |
---|
170 | if previous_session > student['studycourse'].current_session: |
---|
171 | return _('This is not a previous session.'), None |
---|
172 | else: |
---|
173 | if previous_session > student['studycourse'].current_session - 1: |
---|
174 | return _('This is not a previous session.'), None |
---|
175 | p_session = previous_session |
---|
176 | p_level = previous_level |
---|
177 | p_current = False |
---|
178 | else: |
---|
179 | p_session = student['studycourse'].current_session |
---|
180 | p_level = student['studycourse'].current_level |
---|
181 | p_current = True |
---|
182 | academic_session = self._getSessionConfiguration(p_session) |
---|
183 | if academic_session == None: |
---|
184 | return _(u'Session configuration object is not available.'), None |
---|
185 | # Determine fee. |
---|
186 | if category == 'transfer': |
---|
187 | amount = academic_session.transfer_fee |
---|
188 | elif category == 'transcript': |
---|
189 | amount = academic_session.transcript_fee |
---|
190 | elif category == 'gown': |
---|
191 | amount = academic_session.gown_fee |
---|
192 | elif category == 'bed_allocation': |
---|
193 | p_item = self.getAccommodationDetails(student)['bt'] |
---|
194 | amount = academic_session.booking_fee |
---|
195 | # Add student union dues |
---|
196 | stage = self.getAccommodationDetails(student)['bt'] |
---|
197 | stage = stage.split('_')[2] |
---|
198 | ##################################################### |
---|
199 | #if stage == 'fi': |
---|
200 | # return _('Payment temporarily disabled.'), None |
---|
201 | ##################################################### |
---|
202 | if stage == 'fr': |
---|
203 | amount += 500.0 |
---|
204 | elif stage in ('fi', 're') and student[ |
---|
205 | 'studycourse'].previous_verdict in ('A', 'B', 'F', 'J', 'C', 'Z'): |
---|
206 | amount += 300.0 |
---|
207 | else: |
---|
208 | amount = 0.0 |
---|
209 | elif category == 'hostel_maintenance': |
---|
210 | amount = 0.0 |
---|
211 | bedticket = student['accommodation'].get( |
---|
212 | str(student.current_session), None) |
---|
213 | if bedticket is not None and bedticket.bed is not None: |
---|
214 | p_item = bedticket.bed_coordinates |
---|
215 | if bedticket.bed.__parent__.maint_fee > 0: |
---|
216 | amount = bedticket.bed.__parent__.maint_fee |
---|
217 | else: |
---|
218 | # fallback |
---|
219 | amount = academic_session.maint_fee |
---|
220 | else: |
---|
221 | return _(u'No bed allocated.'), None |
---|
222 | #elif category == 'hostel_application': |
---|
223 | # amount = 1000.0 |
---|
224 | #elif category.startswith('tempmaint'): |
---|
225 | # if not self._hostelApplicationPaymentMade( |
---|
226 | # student, student.current_session): |
---|
227 | # return _( |
---|
228 | # 'You have not yet paid the hostel application fee.'), None |
---|
229 | # if category == 'tempmaint_1': |
---|
230 | # amount = 8150.0 |
---|
231 | # elif category == 'tempmaint_2': |
---|
232 | # amount = 12650.0 |
---|
233 | # elif category == 'tempmaint_3': |
---|
234 | # amount = 9650.0 |
---|
235 | elif category == 'clearance': |
---|
236 | p_item = student.certcode |
---|
237 | if p_item is None: |
---|
238 | return _('Study course data are incomplete.'), None |
---|
239 | if student.faccode == 'JUPEB': |
---|
240 | return _('No payment required.'), None |
---|
241 | if student.faccode == 'FCETA': |
---|
242 | amount = 30000.0 |
---|
243 | elif p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS', |
---|
244 | 'MBBSMED', 'MBBSNDU'): |
---|
245 | amount = 65000.0 |
---|
246 | elif p_item in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET', |
---|
247 | 'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'): |
---|
248 | amount = 22500.0 |
---|
249 | else: |
---|
250 | amount = 45000.0 |
---|
251 | elif category == 'schoolfee': |
---|
252 | try: |
---|
253 | certificate = student['studycourse'].certificate |
---|
254 | p_item = certificate.code |
---|
255 | except (AttributeError, TypeError): |
---|
256 | return _('Study course data are incomplete.'), None |
---|
257 | |
---|
258 | ##################################################### |
---|
259 | #if student.faccode == 'JUPEB': |
---|
260 | # return _('Payment temporarily disabled.'), None |
---|
261 | ##################################################### |
---|
262 | |
---|
263 | |
---|
264 | if previous_session: |
---|
265 | # Students can pay for previous sessions in all workflow states. |
---|
266 | # Fresh students are excluded by the update method of the |
---|
267 | # PreviousPaymentAddFormPage. |
---|
268 | if previous_session == student['studycourse'].entry_session: |
---|
269 | if student.is_foreigner: |
---|
270 | amount = getattr(certificate, 'school_fee_3', 0.0) |
---|
271 | else: |
---|
272 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
273 | else: |
---|
274 | if student.is_foreigner: |
---|
275 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
276 | else: |
---|
277 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
278 | else: |
---|
279 | if student.state == CLEARED: |
---|
280 | if student.is_foreigner: |
---|
281 | amount = getattr(certificate, 'school_fee_3', 0.0) |
---|
282 | else: |
---|
283 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
284 | elif student.state in (PAID, REGISTERED, VALIDATED): |
---|
285 | p_session += 1 |
---|
286 | # We don't know which level the student is paying for. |
---|
287 | p_level = None |
---|
288 | academic_session = self._getSessionConfiguration(p_session) |
---|
289 | if academic_session == None: |
---|
290 | return _(u'Session configuration object is not available.'), None |
---|
291 | |
---|
292 | # Students are only allowed to pay for the next session |
---|
293 | # if current session payment |
---|
294 | # has really been made, i.e. payment object exists. |
---|
295 | #if not self._paymentMade( |
---|
296 | # student, student.current_session): |
---|
297 | # return _('You have not yet paid your current/active' + |
---|
298 | # ' session. Please use the previous session' + |
---|
299 | # ' payment form first.'), None |
---|
300 | |
---|
301 | if student.is_foreigner: |
---|
302 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
303 | else: |
---|
304 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
305 | elif student.state == RETURNING: |
---|
306 | # In case of returning school fee payment the payment session |
---|
307 | # and level contain the values of the session the student |
---|
308 | # has paid for. |
---|
309 | p_session, p_level = self.getReturningData(student) |
---|
310 | academic_session = self._getSessionConfiguration(p_session) |
---|
311 | if academic_session == None: |
---|
312 | return _(u'Session configuration object is not available.'), None |
---|
313 | |
---|
314 | # Students are only allowed to pay for the next session |
---|
315 | # if current session payment has really been made, |
---|
316 | # i.e. payment object exists and is paid. |
---|
317 | #if not self._paymentMade( |
---|
318 | # student, student.current_session): |
---|
319 | # return _('You have not yet paid your current/active' + |
---|
320 | # ' session. Please use the previous session' + |
---|
321 | # ' payment form first.'), None |
---|
322 | |
---|
323 | if student.is_foreigner: |
---|
324 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
325 | else: |
---|
326 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
327 | # Give 50% school fee discount to staff members. |
---|
328 | if student.is_staff: |
---|
329 | amount /= 2 |
---|
330 | if amount in (0.0, None): |
---|
331 | return _('Amount could not be determined.'), None |
---|
332 | # Add session specific penalty fee. |
---|
333 | if category == 'schoolfee' and student.is_postgrad: |
---|
334 | amount += academic_session.penalty_pg |
---|
335 | elif category == 'schoolfee': |
---|
336 | amount += academic_session.penalty_ug |
---|
337 | if category.startswith('tempmaint'): |
---|
338 | p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category] |
---|
339 | p_item = unicode(p_item) |
---|
340 | # Now we change the category because tempmaint payments |
---|
341 | # will be obsolete when Uniben returns to Kofa bed allocation. |
---|
342 | category = 'hostel_maintenance' |
---|
343 | # Create ticket. |
---|
344 | if self.samePaymentMade(student, category, p_item, p_session): |
---|
345 | return _('This type of payment has already been made.'), None |
---|
346 | if self._isPaymentDisabled(p_session, category, student): |
---|
347 | return _('Payment temporarily disabled.'), None |
---|
348 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
349 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
350 | payment.p_id = "p%s" % timestamp |
---|
351 | payment.p_category = category |
---|
352 | payment.p_item = p_item |
---|
353 | payment.p_session = p_session |
---|
354 | payment.p_level = p_level |
---|
355 | payment.p_current = p_current |
---|
356 | payment.amount_auth = amount |
---|
357 | return None, payment |
---|
358 | |
---|
359 | def maxCredits(self, studylevel): |
---|
360 | """Return maximum credits. |
---|
361 | |
---|
362 | """ |
---|
363 | studycourse = studylevel.__parent__ |
---|
364 | certificate = getattr(studycourse,'certificate', None) |
---|
365 | current_level = studycourse.current_level |
---|
366 | if None in (current_level, certificate): |
---|
367 | return 0 |
---|
368 | end_level = certificate.end_level |
---|
369 | if current_level >= end_level: |
---|
370 | return 51 |
---|
371 | return 50 |
---|
372 | |
---|
373 | def clearance_disabled_message(self, student): |
---|
374 | if student.is_postgrad: |
---|
375 | return None |
---|
376 | try: |
---|
377 | session_config = grok.getSite()[ |
---|
378 | 'configuration'][str(student.current_session)] |
---|
379 | except KeyError: |
---|
380 | return _('Session configuration object is not available.') |
---|
381 | if not session_config.clearance_enabled: |
---|
382 | return _('Clearance is disabled for this session.') |
---|
383 | return None |
---|
384 | |
---|
385 | # Uniben prefix |
---|
386 | STUDENT_ID_PREFIX = u'B' |
---|