[7419] | 1 | ## $Id: utils.py 9811 2012-12-18 17:25:36Z 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 |
---|
[9737] | 21 | from zope.component import createObject, getUtility |
---|
[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 |
---|
[9737] | 25 | from waeup.kofa.interfaces import CLEARED, RETURNING, IKofaUtils |
---|
[9393] | 26 | from waeup.kofa.fees import FeeTable |
---|
[9347] | 27 | from waeup.kwarapoly.interfaces import MessageFactory as _ |
---|
[6902] | 28 | |
---|
[9393] | 29 | |
---|
[9811] | 30 | # 10 = PreND (1) |
---|
| 31 | # 100 = ND1 (2) |
---|
| 32 | # 110 = ND1R (3) |
---|
| 33 | # 200 = ND2 (4) |
---|
| 34 | # 210 = ND2R (5) |
---|
| 35 | # 300 = ND3 (6) |
---|
| 36 | # 400 = HND1 (7) |
---|
| 37 | # 410 = HND1R (8) |
---|
| 38 | # 500 = HND2 (9) |
---|
| 39 | # 510 = HND2R (10) |
---|
| 40 | # 600 = HND3 (11) |
---|
| 41 | # 999 = PGD (12) |
---|
| 42 | |
---|
| 43 | PAYMENT_LEVELS = (10, 100, 110, 200, 210, 300, 400, 410, 500, 510, 600, 999) |
---|
| 44 | |
---|
[9393] | 45 | FEES_PARAMS = ( |
---|
| 46 | ('ft', 'pt'), |
---|
| 47 | ('local', 'non-local'), |
---|
| 48 | ('science','arts'), |
---|
| 49 | PAYMENT_LEVELS |
---|
| 50 | ) |
---|
| 51 | |
---|
| 52 | FEES_VALUES = ( |
---|
| 53 | ( |
---|
[9811] | 54 | ( # 10 100 110 200 210 300 400 410 500 510 600 999 |
---|
| 55 | (34400.0, 36600.0, 28800.0, 29500.0, 28800.0, 0.0, 38700.0, 29900.0, 30700.0, 29900.0, 0.0, 47250.0), # science |
---|
| 56 | (34400.0, 35100.0, 27300.0, 28000.0, 27300.0, 0.0, 37200.0, 28400.0, 29200.0, 28400.0, 0.0, 47250.0) # arts |
---|
[9393] | 57 | ), # local |
---|
[9811] | 58 | ( # 10 100 110 200 210 300 400 410 500 510 600 999 |
---|
| 59 | (49500.0, 52100.0, 35900.0, 38850.0, 35900.0, 0.0, 55600.0, 38600.0, 40850.0, 38600.0, 0.0, 71880.0), # science |
---|
| 60 | (49500.0, 50600.0, 34400.0, 37350.0, 34400.0, 0.0, 54100.0, 37100.0, 39350.0, 37100.0, 0.0, 71880.0) # arts |
---|
[9393] | 61 | ), # non-local |
---|
| 62 | ), # ft |
---|
| 63 | ( |
---|
[9811] | 64 | ( # 10 100 110 200 210 300 400 410 500 510 600 999 |
---|
| 65 | (0.0, 39400.0, 0.0, 30900.0, 0.0, 30900.0, 39900.0, 0.0, 31050.0, 0.0, 31050.0, 0.0), # science |
---|
| 66 | (0.0, 37900.0, 0.0, 29400.0, 0.0, 29400.0, 38400.0, 0.0, 29550.0, 0.0, 29550.0, 0.0) # arts |
---|
[9393] | 67 | ), # local |
---|
[9811] | 68 | ( # 10 100 110 200 210 300 400 410 500 510 600 999 |
---|
| 69 | (0.0, 54600.0, 0.0, 32350.0, 0.0, 32350.0, 57100.0, 0.0, 42350.0, 0.0, 42350.0, 0.0), # science |
---|
| 70 | (0.0, 53100.0, 0.0, 30850.0, 0.0, 30850.0, 55600.0, 0.0, 40850.0, 0.0, 40850.0, 0.0) # arts |
---|
[9393] | 71 | ), # non-local |
---|
| 72 | ), # pt |
---|
| 73 | ) |
---|
| 74 | |
---|
| 75 | SCHOOL_FEES = FeeTable(FEES_PARAMS, FEES_VALUES) |
---|
| 76 | |
---|
| 77 | def local_nonlocal(student): |
---|
| 78 | lga = getattr(student, 'lga') |
---|
[9568] | 79 | if lga and lga.startswith('kwara'): |
---|
[9393] | 80 | return 'local' |
---|
| 81 | else: |
---|
| 82 | return 'non-local' |
---|
| 83 | |
---|
| 84 | def arts_science(student): |
---|
| 85 | if student.faccode == 'IFMS': |
---|
| 86 | return 'arts' |
---|
| 87 | else: |
---|
| 88 | return 'science' |
---|
| 89 | |
---|
| 90 | def pt_ft(student): |
---|
| 91 | if student.current_mode.endswith('pt'): |
---|
| 92 | return 'pt' |
---|
| 93 | else: |
---|
| 94 | return 'ft' |
---|
| 95 | |
---|
[8834] | 96 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
[7151] | 97 | """A collection of customized methods. |
---|
| 98 | |
---|
| 99 | """ |
---|
| 100 | |
---|
[9393] | 101 | |
---|
[9190] | 102 | def selectBed(self, available_beds): |
---|
| 103 | """Randomly select a bed from a list of available beds. |
---|
| 104 | |
---|
| 105 | """ |
---|
| 106 | return random.choice(available_beds) |
---|
| 107 | |
---|
[8270] | 108 | def getReturningData(self, student): |
---|
| 109 | """ This method defines what happens after school fee payment |
---|
[8319] | 110 | of returning students depending on the student's senate verdict. |
---|
[8270] | 111 | """ |
---|
[8319] | 112 | prev_level = student['studycourse'].current_level |
---|
| 113 | cur_verdict = student['studycourse'].current_verdict |
---|
| 114 | if cur_verdict in ('A','B','L','M','N','Z',): |
---|
| 115 | # Successful student |
---|
| 116 | new_level = divmod(int(prev_level),100)[0]*100 + 100 |
---|
| 117 | elif cur_verdict == 'C': |
---|
| 118 | # Student on probation |
---|
| 119 | new_level = int(prev_level) + 10 |
---|
| 120 | else: |
---|
| 121 | # Student is somehow in an undefined state. |
---|
| 122 | # Level has to be set manually. |
---|
| 123 | new_level = prev_level |
---|
[8270] | 124 | new_session = student['studycourse'].current_session + 1 |
---|
| 125 | return new_session, new_level |
---|
| 126 | |
---|
[9153] | 127 | def setPaymentDetails(self, category, student, |
---|
| 128 | previous_session=None, previous_level=None): |
---|
[8599] | 129 | """Create Payment object and set the payment data of a student for |
---|
| 130 | the payment category specified. |
---|
| 131 | |
---|
| 132 | """ |
---|
[8306] | 133 | details = {} |
---|
[8599] | 134 | p_item = u'' |
---|
| 135 | amount = 0.0 |
---|
| 136 | error = u'' |
---|
[9153] | 137 | if previous_session: |
---|
| 138 | return _('Previous session payment not yet implemented.'), None |
---|
[8599] | 139 | p_session = student['studycourse'].current_session |
---|
| 140 | p_level = student['studycourse'].current_level |
---|
[9153] | 141 | p_current = True |
---|
[9526] | 142 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 143 | if academic_session == None: |
---|
[8599] | 144 | return _(u'Session configuration object is not available.'), None |
---|
[9526] | 145 | # Determine fee. |
---|
[7151] | 146 | if category == 'transfer': |
---|
[8599] | 147 | amount = academic_session.transfer_fee |
---|
[7151] | 148 | elif category == 'gown': |
---|
[8599] | 149 | amount = academic_session.gown_fee |
---|
[7151] | 150 | elif category == 'bed_allocation': |
---|
[8599] | 151 | amount = academic_session.booking_fee |
---|
[7151] | 152 | elif category == 'hostel_maintenance': |
---|
[8599] | 153 | amount = academic_session.maint_fee |
---|
[7151] | 154 | elif category == 'clearance': |
---|
[9143] | 155 | amount = academic_session.clearance_fee |
---|
[8599] | 156 | try: |
---|
| 157 | p_item = student['studycourse'].certificate.code |
---|
| 158 | except (AttributeError, TypeError): |
---|
| 159 | return _('Study course data are incomplete.'), None |
---|
[7151] | 160 | elif category == 'schoolfee': |
---|
[8599] | 161 | try: |
---|
| 162 | certificate = student['studycourse'].certificate |
---|
| 163 | p_item = certificate.code |
---|
| 164 | except (AttributeError, TypeError): |
---|
| 165 | return _('Study course data are incomplete.'), None |
---|
[9297] | 166 | if student.state == RETURNING: |
---|
[9526] | 167 | # Override p_session and p_level |
---|
[9297] | 168 | p_session, p_level = self.getReturningData(student) |
---|
[9526] | 169 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 170 | if academic_session == None: |
---|
| 171 | return _(u'Session configuration object is not available.'), None |
---|
[9388] | 172 | if student.state in (RETURNING, CLEARED): |
---|
[9393] | 173 | if p_level in PAYMENT_LEVELS: |
---|
| 174 | amount = SCHOOL_FEES.get_fee( |
---|
| 175 | (pt_ft(student), |
---|
| 176 | local_nonlocal(student), |
---|
| 177 | arts_science(student), |
---|
| 178 | p_level) |
---|
| 179 | ) |
---|
[9737] | 180 | elif category == 'carryover1': |
---|
[9724] | 181 | amount = 6000.0 |
---|
[9737] | 182 | elif category == 'carryover2': |
---|
[9724] | 183 | amount = 7000.0 |
---|
[9737] | 184 | elif category == 'carryover3': |
---|
[9724] | 185 | amount = 8000.0 |
---|
[8599] | 186 | if amount in (0.0, None): |
---|
| 187 | return _(u'Amount could not be determined.'), None |
---|
| 188 | for key in student['payments'].keys(): |
---|
| 189 | ticket = student['payments'][key] |
---|
| 190 | if ticket.p_state == 'paid' and\ |
---|
| 191 | ticket.p_category == category and \ |
---|
| 192 | ticket.p_item == p_item and \ |
---|
| 193 | ticket.p_session == p_session: |
---|
| 194 | return _('This type of payment has already been made.'), None |
---|
[9737] | 195 | if category.startswith('carryover'): |
---|
| 196 | p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category] |
---|
| 197 | p_item = unicode(p_item) |
---|
| 198 | # Now we change the category to reduce the number of categories. |
---|
| 199 | category = 'schoolfee' |
---|
[8713] | 200 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
[8953] | 201 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
[8599] | 202 | payment.p_id = "p%s" % timestamp |
---|
| 203 | payment.p_category = category |
---|
| 204 | payment.p_item = p_item |
---|
| 205 | payment.p_session = p_session |
---|
| 206 | payment.p_level = p_level |
---|
[9153] | 207 | payment.p_current = p_current |
---|
[9143] | 208 | payment.amount_auth = float(amount) |
---|
[8599] | 209 | return None, payment |
---|
[7621] | 210 | |
---|
[9207] | 211 | def getAccommodationDetails(self, student): |
---|
| 212 | """Determine the accommodation data of a student. |
---|
| 213 | """ |
---|
| 214 | d = {} |
---|
| 215 | d['error'] = u'' |
---|
| 216 | hostels = grok.getSite()['hostels'] |
---|
| 217 | d['booking_session'] = hostels.accommodation_session |
---|
| 218 | d['allowed_states'] = hostels.accommodation_states |
---|
| 219 | d['startdate'] = hostels.startdate |
---|
| 220 | d['enddate'] = hostels.enddate |
---|
| 221 | d['expired'] = hostels.expired |
---|
| 222 | # Determine bed type |
---|
| 223 | studycourse = student['studycourse'] |
---|
| 224 | certificate = getattr(studycourse,'certificate',None) |
---|
| 225 | current_level = studycourse.current_level |
---|
| 226 | if None in (current_level, certificate): |
---|
| 227 | return d |
---|
| 228 | end_level = certificate.end_level |
---|
| 229 | if current_level == 10: |
---|
| 230 | bt = 'pr' |
---|
[9614] | 231 | elif current_level in (100, 400): |
---|
[9207] | 232 | bt = 'fr' |
---|
[9614] | 233 | elif current_level in (300, 600): |
---|
[9207] | 234 | bt = 'fi' |
---|
| 235 | else: |
---|
| 236 | bt = 're' |
---|
| 237 | if student.sex == 'f': |
---|
| 238 | sex = 'female' |
---|
| 239 | else: |
---|
| 240 | sex = 'male' |
---|
| 241 | special_handling = 'regular' |
---|
| 242 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
| 243 | return d |
---|
| 244 | |
---|
[9347] | 245 | # KwaraPoly prefix |
---|
[9366] | 246 | STUDENT_ID_PREFIX = u'W' |
---|