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