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