[7419] | 1 | ## $Id: utils.py 14358 2016-12-20 09:03:23Z 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] | 18 | import grok |
---|
[8598] | 19 | from time import time |
---|
[9459] | 20 | from zope.component import createObject, getUtility |
---|
[9513] | 21 | from waeup.kofa.interfaces import (IKofaUtils, |
---|
| 22 | CLEARED, RETURNING, PAID, REGISTERED, VALIDATED) |
---|
[13251] | 23 | from waeup.kofa.utils.helpers import to_timezone |
---|
| 24 | from waeup.kofa.students.utils import trans |
---|
[8821] | 25 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
[8020] | 26 | from waeup.uniben.interfaces import MessageFactory as _ |
---|
[6902] | 27 | |
---|
[8821] | 28 | class 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) |
---|
| 144 | if category == 'schoolfee' and \ |
---|
| 145 | 'sf_all' in academic_session.payment_disabled: |
---|
| 146 | return True |
---|
| 147 | if category == 'hostel_maintenance' and \ |
---|
| 148 | 'maint_all' in academic_session.payment_disabled: |
---|
| 149 | return True |
---|
| 150 | return False |
---|
| 151 | |
---|
[13251] | 152 | #def _hostelApplicationPaymentMade(self, student, session): |
---|
| 153 | # if len(student['payments']): |
---|
| 154 | # for ticket in student['payments'].values(): |
---|
| 155 | # if ticket.p_state == 'paid' and \ |
---|
| 156 | # ticket.p_category == 'hostel_application' and \ |
---|
| 157 | # ticket.p_session == session: |
---|
| 158 | # return True |
---|
| 159 | # return False |
---|
[12566] | 160 | |
---|
[9152] | 161 | def setPaymentDetails(self, category, student, |
---|
| 162 | previous_session, previous_level): |
---|
[8598] | 163 | """Create Payment object and set the payment data of a student for |
---|
| 164 | the payment category specified. |
---|
| 165 | |
---|
| 166 | """ |
---|
| 167 | p_item = u'' |
---|
| 168 | amount = 0.0 |
---|
[9152] | 169 | if previous_session: |
---|
[9520] | 170 | if previous_session < student['studycourse'].entry_session: |
---|
| 171 | return _('The previous session must not fall below ' |
---|
| 172 | 'your entry session.'), None |
---|
| 173 | if category == 'schoolfee': |
---|
| 174 | # School fee is always paid for the following session |
---|
| 175 | if previous_session > student['studycourse'].current_session: |
---|
| 176 | return _('This is not a previous session.'), None |
---|
| 177 | else: |
---|
| 178 | if previous_session > student['studycourse'].current_session - 1: |
---|
| 179 | return _('This is not a previous session.'), None |
---|
[9152] | 180 | p_session = previous_session |
---|
| 181 | p_level = previous_level |
---|
| 182 | p_current = False |
---|
| 183 | else: |
---|
| 184 | p_session = student['studycourse'].current_session |
---|
| 185 | p_level = student['studycourse'].current_level |
---|
| 186 | p_current = True |
---|
[9520] | 187 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 188 | if academic_session == None: |
---|
[8598] | 189 | return _(u'Session configuration object is not available.'), None |
---|
[8676] | 190 | # Determine fee. |
---|
[7151] | 191 | if category == 'transfer': |
---|
[8598] | 192 | amount = academic_session.transfer_fee |
---|
[10469] | 193 | elif category == 'transcript': |
---|
| 194 | amount = academic_session.transcript_fee |
---|
[7151] | 195 | elif category == 'gown': |
---|
[8598] | 196 | amount = academic_session.gown_fee |
---|
[13785] | 197 | elif category == 'jupeb': |
---|
| 198 | amount = academic_session.jupeb_fee |
---|
[14358] | 199 | elif category == 'clinexam': |
---|
| 200 | amount = academic_session.clinexam_fee |
---|
[13251] | 201 | elif category == 'bed_allocation': |
---|
| 202 | p_item = self.getAccommodationDetails(student)['bt'] |
---|
| 203 | amount = academic_session.booking_fee |
---|
[13283] | 204 | # Add student union dues |
---|
[13777] | 205 | if not student.is_postgrad: |
---|
| 206 | stage = self.getAccommodationDetails(student)['bt'] |
---|
| 207 | stage = stage.split('_')[2] |
---|
| 208 | if stage == 'fr': |
---|
| 209 | amount += 500.0 |
---|
| 210 | elif stage in ('fi', 're') and student[ |
---|
[14343] | 211 | 'studycourse'].previous_verdict in ( |
---|
| 212 | 'A', 'B', 'F', 'J', 'M', 'C', 'Z'): |
---|
[13777] | 213 | amount += 300.0 |
---|
| 214 | else: |
---|
| 215 | amount = 0.0 |
---|
[13251] | 216 | elif category == 'hostel_maintenance': |
---|
| 217 | amount = 0.0 |
---|
| 218 | bedticket = student['accommodation'].get( |
---|
| 219 | str(student.current_session), None) |
---|
[13500] | 220 | if bedticket is not None and bedticket.bed is not None: |
---|
[13251] | 221 | p_item = bedticket.bed_coordinates |
---|
| 222 | if bedticket.bed.__parent__.maint_fee > 0: |
---|
| 223 | amount = bedticket.bed.__parent__.maint_fee |
---|
| 224 | else: |
---|
| 225 | # fallback |
---|
| 226 | amount = academic_session.maint_fee |
---|
| 227 | else: |
---|
[13508] | 228 | return _(u'No bed allocated.'), None |
---|
[13251] | 229 | #elif category == 'hostel_application': |
---|
| 230 | # amount = 1000.0 |
---|
| 231 | #elif category.startswith('tempmaint'): |
---|
| 232 | # if not self._hostelApplicationPaymentMade( |
---|
| 233 | # student, student.current_session): |
---|
| 234 | # return _( |
---|
| 235 | # 'You have not yet paid the hostel application fee.'), None |
---|
| 236 | # if category == 'tempmaint_1': |
---|
| 237 | # amount = 8150.0 |
---|
| 238 | # elif category == 'tempmaint_2': |
---|
| 239 | # amount = 12650.0 |
---|
| 240 | # elif category == 'tempmaint_3': |
---|
| 241 | # amount = 9650.0 |
---|
[7151] | 242 | elif category == 'clearance': |
---|
[9796] | 243 | p_item = student.certcode |
---|
| 244 | if p_item is None: |
---|
[8598] | 245 | return _('Study course data are incomplete.'), None |
---|
[13310] | 246 | if student.faccode == 'JUPEB': |
---|
| 247 | return _('No payment required.'), None |
---|
[13869] | 248 | if student.faccode.startswith('FCETA'): |
---|
| 249 | # ASABA and AKOKA |
---|
[13631] | 250 | amount = 30000.0 |
---|
[11479] | 251 | elif p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS', |
---|
[14289] | 252 | 'MBBSMED', 'MBBSNDU', 'BSCPTY'): |
---|
[14237] | 253 | amount = 65000.0 |
---|
[11479] | 254 | else: |
---|
[14237] | 255 | amount = 45000.0 |
---|
| 256 | # Clearance fee and provider amount have been increased |
---|
| 257 | # for new students. |
---|
| 258 | if student.entry_session >= 2016: |
---|
| 259 | amount += 5000.0 |
---|
[7151] | 260 | elif category == 'schoolfee': |
---|
[8598] | 261 | try: |
---|
| 262 | certificate = student['studycourse'].certificate |
---|
| 263 | p_item = certificate.code |
---|
| 264 | except (AttributeError, TypeError): |
---|
| 265 | return _('Study course data are incomplete.'), None |
---|
[13332] | 266 | |
---|
| 267 | ##################################################### |
---|
[13340] | 268 | #if student.faccode == 'JUPEB': |
---|
| 269 | # return _('Payment temporarily disabled.'), None |
---|
[13332] | 270 | ##################################################### |
---|
| 271 | |
---|
| 272 | |
---|
[9152] | 273 | if previous_session: |
---|
[9520] | 274 | # Students can pay for previous sessions in all workflow states. |
---|
| 275 | # Fresh students are excluded by the update method of the |
---|
| 276 | # PreviousPaymentAddFormPage. |
---|
[9157] | 277 | if previous_session == student['studycourse'].entry_session: |
---|
[9152] | 278 | if student.is_foreigner: |
---|
| 279 | amount = getattr(certificate, 'school_fee_3', 0.0) |
---|
| 280 | else: |
---|
| 281 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
[9006] | 282 | else: |
---|
[9152] | 283 | if student.is_foreigner: |
---|
| 284 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
| 285 | else: |
---|
| 286 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
| 287 | else: |
---|
| 288 | if student.state == CLEARED: |
---|
| 289 | if student.is_foreigner: |
---|
| 290 | amount = getattr(certificate, 'school_fee_3', 0.0) |
---|
| 291 | else: |
---|
| 292 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
[9513] | 293 | elif student.state in (PAID, REGISTERED, VALIDATED): |
---|
| 294 | p_session += 1 |
---|
| 295 | # We don't know which level the student is paying for. |
---|
| 296 | p_level = None |
---|
[9520] | 297 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 298 | if academic_session == None: |
---|
[9513] | 299 | return _(u'Session configuration object is not available.'), None |
---|
[9570] | 300 | |
---|
[9520] | 301 | # Students are only allowed to pay for the next session |
---|
| 302 | # if current session payment |
---|
| 303 | # has really been made, i.e. payment object exists. |
---|
[9570] | 304 | #if not self._paymentMade( |
---|
| 305 | # student, student.current_session): |
---|
| 306 | # return _('You have not yet paid your current/active' + |
---|
| 307 | # ' session. Please use the previous session' + |
---|
| 308 | # ' payment form first.'), None |
---|
| 309 | |
---|
[9513] | 310 | if student.is_foreigner: |
---|
| 311 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
| 312 | else: |
---|
| 313 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
[9152] | 314 | elif student.state == RETURNING: |
---|
| 315 | # In case of returning school fee payment the payment session |
---|
| 316 | # and level contain the values of the session the student |
---|
| 317 | # has paid for. |
---|
| 318 | p_session, p_level = self.getReturningData(student) |
---|
[9520] | 319 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 320 | if academic_session == None: |
---|
[9152] | 321 | return _(u'Session configuration object is not available.'), None |
---|
[9570] | 322 | |
---|
[9520] | 323 | # Students are only allowed to pay for the next session |
---|
| 324 | # if current session payment has really been made, |
---|
| 325 | # i.e. payment object exists and is paid. |
---|
[9570] | 326 | #if not self._paymentMade( |
---|
| 327 | # student, student.current_session): |
---|
| 328 | # return _('You have not yet paid your current/active' + |
---|
| 329 | # ' session. Please use the previous session' + |
---|
| 330 | # ' payment form first.'), None |
---|
| 331 | |
---|
[9152] | 332 | if student.is_foreigner: |
---|
| 333 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
| 334 | else: |
---|
| 335 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
[9006] | 336 | # Give 50% school fee discount to staff members. |
---|
| 337 | if student.is_staff: |
---|
| 338 | amount /= 2 |
---|
[8598] | 339 | if amount in (0.0, None): |
---|
[9520] | 340 | return _('Amount could not be determined.'), None |
---|
[8676] | 341 | # Add session specific penalty fee. |
---|
| 342 | if category == 'schoolfee' and student.is_postgrad: |
---|
| 343 | amount += academic_session.penalty_pg |
---|
[13757] | 344 | elif category == 'schoolfee' and student.current_mode == ('ug_ft'): |
---|
[13756] | 345 | amount += academic_session.penalty_ug_ft |
---|
[13757] | 346 | elif category == 'schoolfee' and student.current_mode == ('ug_pt'): |
---|
[13756] | 347 | amount += academic_session.penalty_ug_pt |
---|
[13998] | 348 | elif category == 'schoolfee' and student.current_mode == ('ug_sw'): |
---|
| 349 | amount += academic_session.penalty_sw |
---|
[9727] | 350 | if category.startswith('tempmaint'): |
---|
| 351 | p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category] |
---|
| 352 | p_item = unicode(p_item) |
---|
| 353 | # Now we change the category because tempmaint payments |
---|
[12566] | 354 | # will be obsolete when Uniben returns to Kofa bed allocation. |
---|
[9727] | 355 | category = 'hostel_maintenance' |
---|
[8676] | 356 | # Create ticket. |
---|
[11644] | 357 | if self.samePaymentMade(student, category, p_item, p_session): |
---|
| 358 | return _('This type of payment has already been made.'), None |
---|
[11459] | 359 | if self._isPaymentDisabled(p_session, category, student): |
---|
[13814] | 360 | return _('This category of payments has been disabled.'), None |
---|
[8715] | 361 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
[8950] | 362 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
[8598] | 363 | payment.p_id = "p%s" % timestamp |
---|
| 364 | payment.p_category = category |
---|
| 365 | payment.p_item = p_item |
---|
| 366 | payment.p_session = p_session |
---|
| 367 | payment.p_level = p_level |
---|
[9152] | 368 | payment.p_current = p_current |
---|
[8598] | 369 | payment.amount_auth = amount |
---|
| 370 | return None, payment |
---|
[7621] | 371 | |
---|
[9831] | 372 | def maxCredits(self, studylevel): |
---|
| 373 | """Return maximum credits. |
---|
| 374 | |
---|
| 375 | """ |
---|
| 376 | studycourse = studylevel.__parent__ |
---|
| 377 | certificate = getattr(studycourse,'certificate', None) |
---|
| 378 | current_level = studycourse.current_level |
---|
| 379 | if None in (current_level, certificate): |
---|
| 380 | return 0 |
---|
| 381 | end_level = certificate.end_level |
---|
| 382 | if current_level >= end_level: |
---|
| 383 | return 51 |
---|
| 384 | return 50 |
---|
| 385 | |
---|
[11773] | 386 | def clearance_disabled_message(self, student): |
---|
| 387 | if student.is_postgrad: |
---|
| 388 | return None |
---|
| 389 | try: |
---|
| 390 | session_config = grok.getSite()[ |
---|
| 391 | 'configuration'][str(student.current_session)] |
---|
| 392 | except KeyError: |
---|
| 393 | return _('Session configuration object is not available.') |
---|
| 394 | if not session_config.clearance_enabled: |
---|
| 395 | return _('Clearance is disabled for this session.') |
---|
| 396 | return None |
---|
| 397 | |
---|
[14038] | 398 | #: A tuple containing the names of registration states in which changing of |
---|
| 399 | #: passport pictures is allowed. |
---|
| 400 | PORTRAIT_CHANGE_STATES = () |
---|
| 401 | |
---|
[8441] | 402 | # Uniben prefix |
---|
[8413] | 403 | STUDENT_ID_PREFIX = u'B' |
---|