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