[7419] | 1 | ## $Id: utils.py 9629 2012-11-13 10:37:03Z 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 | ## |
---|
[7151] | 18 | import grok |
---|
[9190] | 19 | import random |
---|
[8599] | 20 | from time import time |
---|
| 21 | from zope.component import createObject |
---|
[8475] | 22 | from waeup.kofa.interfaces import CLEARED, RETURNING, PAID |
---|
[8834] | 23 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
[8247] | 24 | from waeup.kofa.accesscodes import create_accesscode |
---|
[9143] | 25 | from waeup.kofa.interfaces import CLEARED, RETURNING |
---|
[8460] | 26 | from waeup.fceokene.interfaces import MessageFactory as _ |
---|
[6902] | 27 | |
---|
[8834] | 28 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
[7151] | 29 | """A collection of customized methods. |
---|
| 30 | |
---|
| 31 | """ |
---|
| 32 | |
---|
[9190] | 33 | def selectBed(self, available_beds): |
---|
| 34 | """Randomly select a bed from a list of available beds. |
---|
| 35 | |
---|
| 36 | """ |
---|
| 37 | return random.choice(available_beds) |
---|
| 38 | |
---|
[8270] | 39 | def getReturningData(self, student): |
---|
| 40 | """ This method defines what happens after school fee payment |
---|
[8319] | 41 | of returning students depending on the student's senate verdict. |
---|
[8270] | 42 | """ |
---|
[8319] | 43 | prev_level = student['studycourse'].current_level |
---|
| 44 | cur_verdict = student['studycourse'].current_verdict |
---|
| 45 | if cur_verdict in ('A','B','L','M','N','Z',): |
---|
| 46 | # Successful student |
---|
| 47 | new_level = divmod(int(prev_level),100)[0]*100 + 100 |
---|
| 48 | elif cur_verdict == 'C': |
---|
| 49 | # Student on probation |
---|
| 50 | new_level = int(prev_level) + 10 |
---|
| 51 | else: |
---|
| 52 | # Student is somehow in an undefined state. |
---|
| 53 | # Level has to be set manually. |
---|
| 54 | new_level = prev_level |
---|
[8270] | 55 | new_session = student['studycourse'].current_session + 1 |
---|
| 56 | return new_session, new_level |
---|
| 57 | |
---|
[9153] | 58 | def setPaymentDetails(self, category, student, |
---|
| 59 | previous_session=None, previous_level=None): |
---|
[8599] | 60 | """Create Payment object and set the payment data of a student for |
---|
| 61 | the payment category specified. |
---|
| 62 | |
---|
| 63 | """ |
---|
[8306] | 64 | details = {} |
---|
[8599] | 65 | p_item = u'' |
---|
| 66 | amount = 0.0 |
---|
| 67 | error = u'' |
---|
[9153] | 68 | if previous_session: |
---|
| 69 | return _('Previous session payment not yet implemented.'), None |
---|
[8599] | 70 | p_session = student['studycourse'].current_session |
---|
| 71 | p_level = student['studycourse'].current_level |
---|
[9153] | 72 | p_current = True |
---|
[9525] | 73 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 74 | if academic_session == None: |
---|
[8599] | 75 | return _(u'Session configuration object is not available.'), None |
---|
[9525] | 76 | # Determine fee. |
---|
[7151] | 77 | if category == 'transfer': |
---|
[8599] | 78 | amount = academic_session.transfer_fee |
---|
[7151] | 79 | elif category == 'gown': |
---|
[8599] | 80 | amount = academic_session.gown_fee |
---|
[7151] | 81 | elif category == 'bed_allocation': |
---|
[8599] | 82 | amount = academic_session.booking_fee |
---|
[7151] | 83 | elif category == 'hostel_maintenance': |
---|
[9611] | 84 | current_session = student['studycourse'].current_session |
---|
| 85 | bedticket = student['accommodation'].get(str(current_session), None) |
---|
| 86 | if bedticket is not None and bedticket.bed is not None: |
---|
| 87 | p_item = bedticket.bed_coordinates |
---|
| 88 | else: |
---|
| 89 | return _(u'You have not yet booked accommodation.'), None |
---|
| 90 | acc_details = self.getAccommodationDetails(student) |
---|
| 91 | if current_session != acc_details['booking_session']: |
---|
| 92 | return _(u'Current session does not match accommodation session.'), None |
---|
[9612] | 93 | if student.current_mode.endswith('_sw') or student.current_mode == 'pd_ft': |
---|
[9629] | 94 | amount = 2650.0 |
---|
[9612] | 95 | else: |
---|
[9628] | 96 | amount = 4150.0 |
---|
[7151] | 97 | elif category == 'clearance': |
---|
[9143] | 98 | amount = academic_session.clearance_fee |
---|
[8599] | 99 | try: |
---|
| 100 | p_item = student['studycourse'].certificate.code |
---|
| 101 | except (AttributeError, TypeError): |
---|
| 102 | return _('Study course data are incomplete.'), None |
---|
[7151] | 103 | elif category == 'schoolfee': |
---|
[8599] | 104 | try: |
---|
| 105 | certificate = student['studycourse'].certificate |
---|
| 106 | p_item = certificate.code |
---|
| 107 | except (AttributeError, TypeError): |
---|
| 108 | return _('Study course data are incomplete.'), None |
---|
[9143] | 109 | |
---|
| 110 | # Very special school fee configuration, should be moved to |
---|
| 111 | # a seperate file. |
---|
| 112 | |
---|
| 113 | ARTS = ('CRS','ISS','HIS','MUS','ECO','GEO','POL','SOS','CCA','ECU', |
---|
| 114 | 'THA','GED','GSE','PES','SPC','ENG','FRE','ARB','HAU','IGB', |
---|
| 115 | 'YOR','NCRS','NISS','NHIS','NMUS','NECO','NGEO','NPOL', |
---|
| 116 | 'NCCA','NECU','NTHA','NGED','NGSE','NPES','NSPC','NENG', |
---|
| 117 | 'NFRE','NARB','NHAU','NIGB','NYOR','NSOS') |
---|
| 118 | |
---|
| 119 | #PDE Students |
---|
| 120 | if student.current_mode == 'pd_ft': |
---|
| 121 | amount = 35000 |
---|
| 122 | elif not student.current_mode.endswith('_sw'): |
---|
| 123 | # PRENCE |
---|
| 124 | if student.current_level == 10 and student.state == CLEARED: |
---|
| 125 | if student.depcode in ARTS: |
---|
| 126 | amount = 14900 |
---|
| 127 | else: |
---|
| 128 | amount = 15400 |
---|
| 129 | # NCE I fresh |
---|
| 130 | elif student.current_level == 100 and student.state == CLEARED: |
---|
| 131 | if student.depcode in ARTS: |
---|
| 132 | amount = 12020 |
---|
| 133 | else: |
---|
| 134 | amount = 12495 |
---|
| 135 | # NCE II |
---|
| 136 | elif student.current_level in (100, 110, 120) and \ |
---|
| 137 | student.state == RETURNING: |
---|
| 138 | if student.depcode in ARTS: |
---|
| 139 | amount = 11070 |
---|
| 140 | else: |
---|
| 141 | amount = 11545 |
---|
| 142 | # NCE III |
---|
| 143 | elif student.current_level in (200, 210, 220): |
---|
| 144 | if student.depcode in ARTS: |
---|
| 145 | amount = 11070 |
---|
| 146 | else: |
---|
| 147 | amount = 11545 |
---|
| 148 | # NCE III repeater |
---|
| 149 | elif student.current_level in (300, 310, 320) and \ |
---|
| 150 | student.current_verdict == 'O': |
---|
| 151 | if student.depcode in ARTS: |
---|
| 152 | amount = 6535 |
---|
| 153 | else: |
---|
| 154 | amount = 6773 |
---|
| 155 | # NCE III spillover |
---|
| 156 | elif student.current_level in (300, 310, 320) and \ |
---|
| 157 | student.current_verdict == 'B': |
---|
| 158 | if student.depcode in ARTS: |
---|
| 159 | amount = 9170 |
---|
| 160 | else: |
---|
| 161 | amount = 9645 |
---|
| 162 | # NCE III second spillover |
---|
| 163 | elif student.current_level in (400, 410, 420) and \ |
---|
| 164 | student.current_verdict == 'B': |
---|
| 165 | if student.depcode in ARTS: |
---|
| 166 | amount = 9170 |
---|
| 167 | else: |
---|
| 168 | amount = 9645 |
---|
| 169 | else: |
---|
| 170 | if student.current_level == 100 and student.state == CLEARED: |
---|
| 171 | if student.depcode in ARTS: |
---|
| 172 | amount = 21900 |
---|
| 173 | else: |
---|
| 174 | amount = 22400 |
---|
| 175 | # NCE II |
---|
| 176 | elif student.current_level in (100, 110, 120) and \ |
---|
| 177 | student.state == RETURNING: |
---|
| 178 | if student.depcode in ARTS: |
---|
| 179 | amount = 18400 |
---|
| 180 | else: |
---|
| 181 | amount = 18900 |
---|
| 182 | # NCE III |
---|
| 183 | elif student.current_level in (200, 210, 220): |
---|
| 184 | if student.depcode in ARTS: |
---|
| 185 | amount = 20400 |
---|
| 186 | else: |
---|
| 187 | amount = 20900 |
---|
| 188 | # NCE IV |
---|
| 189 | elif student.current_level in (300, 310, 320): |
---|
| 190 | if student.depcode in ARTS: |
---|
| 191 | amount = 18400 |
---|
| 192 | else: |
---|
| 193 | amount = 18900 |
---|
| 194 | # NCE V |
---|
| 195 | elif student.current_level in (400, 410, 420): |
---|
| 196 | if student.depcode in ARTS: |
---|
| 197 | amount = 18400 |
---|
| 198 | else: |
---|
| 199 | amount = 18900 |
---|
| 200 | # NCE V spillover |
---|
| 201 | elif student.current_level in (500, 510, 520) and \ |
---|
| 202 | student.current_verdict == 'B': |
---|
| 203 | if student.depcode in ARTS: |
---|
| 204 | amount = 16900 |
---|
| 205 | else: |
---|
| 206 | amount = 17400 |
---|
| 207 | # NCE V second spillover |
---|
| 208 | elif student.current_level in (600, 610, 620) and \ |
---|
| 209 | student.current_verdict == 'B': |
---|
| 210 | if student.depcode in ARTS: |
---|
| 211 | amount = 16900 |
---|
| 212 | else: |
---|
| 213 | amount = 17400 |
---|
[9297] | 214 | if student.state == RETURNING: |
---|
[9525] | 215 | # Override p_session and p_level |
---|
[9297] | 216 | p_session, p_level = self.getReturningData(student) |
---|
[9525] | 217 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 218 | if academic_session == None: |
---|
| 219 | return _(u'Session configuration object is not available.'), None |
---|
[9143] | 220 | |
---|
[8599] | 221 | if amount in (0.0, None): |
---|
| 222 | return _(u'Amount could not be determined.'), None |
---|
| 223 | for key in student['payments'].keys(): |
---|
| 224 | ticket = student['payments'][key] |
---|
| 225 | if ticket.p_state == 'paid' and\ |
---|
| 226 | ticket.p_category == category and \ |
---|
| 227 | ticket.p_item == p_item and \ |
---|
| 228 | ticket.p_session == p_session: |
---|
| 229 | return _('This type of payment has already been made.'), None |
---|
[8713] | 230 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
[8953] | 231 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
[8599] | 232 | payment.p_id = "p%s" % timestamp |
---|
| 233 | payment.p_category = category |
---|
| 234 | payment.p_item = p_item |
---|
| 235 | payment.p_session = p_session |
---|
| 236 | payment.p_level = p_level |
---|
[9153] | 237 | payment.p_current = p_current |
---|
[9143] | 238 | payment.amount_auth = float(amount) |
---|
[8599] | 239 | return None, payment |
---|
[7621] | 240 | |
---|
[9207] | 241 | def getAccommodationDetails(self, student): |
---|
| 242 | """Determine the accommodation data of a student. |
---|
| 243 | """ |
---|
| 244 | d = {} |
---|
| 245 | d['error'] = u'' |
---|
| 246 | hostels = grok.getSite()['hostels'] |
---|
| 247 | d['booking_session'] = hostels.accommodation_session |
---|
| 248 | d['allowed_states'] = hostels.accommodation_states |
---|
| 249 | d['startdate'] = hostels.startdate |
---|
| 250 | d['enddate'] = hostels.enddate |
---|
| 251 | d['expired'] = hostels.expired |
---|
| 252 | # Determine bed type |
---|
| 253 | studycourse = student['studycourse'] |
---|
| 254 | certificate = getattr(studycourse,'certificate',None) |
---|
| 255 | current_level = studycourse.current_level |
---|
| 256 | if None in (current_level, certificate): |
---|
| 257 | return d |
---|
| 258 | end_level = certificate.end_level |
---|
| 259 | if current_level == 10: |
---|
| 260 | bt = 'pr' |
---|
| 261 | elif current_level == 100: |
---|
| 262 | bt = 'fr' |
---|
| 263 | elif current_level >= 300: |
---|
| 264 | bt = 'fi' |
---|
| 265 | else: |
---|
| 266 | bt = 're' |
---|
| 267 | if student.sex == 'f': |
---|
| 268 | sex = 'female' |
---|
| 269 | else: |
---|
| 270 | sex = 'male' |
---|
| 271 | special_handling = 'regular' |
---|
| 272 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
| 273 | return d |
---|
| 274 | |
---|
[8460] | 275 | # FCEOkene prefix |
---|
[8528] | 276 | STUDENT_ID_PREFIX = u'K' |
---|