1 | ## $Id: utils.py 9431 2012-10-26 20:59: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 | ## |
---|
18 | import grok |
---|
19 | import random |
---|
20 | from time import time |
---|
21 | from zope.component import createObject |
---|
22 | from waeup.kofa.interfaces import CLEARED, RETURNING, PAID |
---|
23 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
24 | from waeup.kofa.accesscodes import create_accesscode |
---|
25 | from waeup.futminna.interfaces import MessageFactory as _ |
---|
26 | |
---|
27 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
28 | """A collection of customized methods. |
---|
29 | |
---|
30 | """ |
---|
31 | |
---|
32 | def selectBed(self, available_beds): |
---|
33 | """Randomly select a bed from a list of available beds. |
---|
34 | |
---|
35 | """ |
---|
36 | return random.choice(available_beds) |
---|
37 | |
---|
38 | def getReturningData(self, student): |
---|
39 | """ This method defines what happens after school fee payment |
---|
40 | of returning students depending on the student's senate verdict. |
---|
41 | """ |
---|
42 | prev_level = student['studycourse'].current_level |
---|
43 | cur_verdict = student['studycourse'].current_verdict |
---|
44 | if cur_verdict in ('A','B','L','M','N','Z',): |
---|
45 | # Successful student |
---|
46 | new_level = divmod(int(prev_level),100)[0]*100 + 100 |
---|
47 | elif cur_verdict == 'C': |
---|
48 | # Student on probation |
---|
49 | new_level = int(prev_level) + 10 |
---|
50 | else: |
---|
51 | # Student is somehow in an undefined state. |
---|
52 | # Level has to be set manually. |
---|
53 | new_level = prev_level |
---|
54 | new_session = student['studycourse'].current_session + 1 |
---|
55 | return new_session, new_level |
---|
56 | |
---|
57 | def setPaymentDetails(self, category, student, |
---|
58 | previous_session=None, previous_level=None): |
---|
59 | """Create Payment object and set the payment data of a student for |
---|
60 | the payment category specified. |
---|
61 | |
---|
62 | """ |
---|
63 | p_item = u'' |
---|
64 | amount = 0.0 |
---|
65 | if previous_session: |
---|
66 | return _('Previous session payment not yet implemented.'), None |
---|
67 | p_session = student['studycourse'].current_session |
---|
68 | p_level = student['studycourse'].current_level |
---|
69 | p_current = True |
---|
70 | session = str(p_session) |
---|
71 | try: |
---|
72 | academic_session = grok.getSite()['configuration'][session] |
---|
73 | except KeyError: |
---|
74 | return _(u'Session configuration object is not available.'), None |
---|
75 | if category == 'schoolfee': |
---|
76 | try: |
---|
77 | certificate = student['studycourse'].certificate |
---|
78 | p_item = certificate.code |
---|
79 | except (AttributeError, TypeError): |
---|
80 | return _('Study course data are incomplete.'), None |
---|
81 | if student.current_mode.endswith('_ft'): |
---|
82 | # fresh remedial |
---|
83 | if student.current_level == 10 and student.state == CLEARED: |
---|
84 | if student['studycourse'].entry_mode == 'rmd_ft': |
---|
85 | amount = 80200.0 |
---|
86 | else: |
---|
87 | amount = 74200.0 |
---|
88 | # fresh |
---|
89 | elif student.state == CLEARED: |
---|
90 | if student.current_mode == 'jm_ft': |
---|
91 | amount = 72700.0 |
---|
92 | elif student.is_foreigner: |
---|
93 | amount = 131500.0 |
---|
94 | else: |
---|
95 | amount = 37000.0 # School Fee reduced by 8000 |
---|
96 | # returning |
---|
97 | elif student.state == RETURNING: |
---|
98 | if student.current_mode == 'jm_ft': |
---|
99 | amount = 37000.0 |
---|
100 | elif student.is_foreigner: |
---|
101 | amount = 109500.0 |
---|
102 | else: |
---|
103 | amount = 20000.0 |
---|
104 | else: |
---|
105 | amount = 0.0 |
---|
106 | if student.state == RETURNING: |
---|
107 | p_session, p_level = self.getReturningData(student) |
---|
108 | elif category == 'clearance': |
---|
109 | try: |
---|
110 | p_item = student['studycourse'].certificate.code |
---|
111 | except (AttributeError, TypeError): |
---|
112 | return _('Study course data are incomplete.'), None |
---|
113 | if student.faccode in ['EET','ICT'] or student.depcode in ['ARC']: |
---|
114 | amount = 25000.0 |
---|
115 | else: |
---|
116 | amount = 20000.0 |
---|
117 | elif category == 'hostel_maintenance': |
---|
118 | current_session = student['studycourse'].current_session |
---|
119 | bedticket = student['accommodation'].get(str(current_session), None) |
---|
120 | if bedticket: |
---|
121 | p_item = bedticket.bed_coordinates |
---|
122 | else: |
---|
123 | return _(u'You have not yet booked accommodation.'), None |
---|
124 | acc_details = self.getAccommodationDetails(student) |
---|
125 | if current_session != acc_details['booking_session']: |
---|
126 | return _(u'Current session does not match accommodation session.'), None |
---|
127 | if bedticket.bed.bed_id.startswith('block-h'): |
---|
128 | amount = 15000.0 |
---|
129 | elif student.current_level in (100,200,300,400,500) and \ |
---|
130 | student.faccode in ('EET','SET','AAT','ICT','EMT'): |
---|
131 | amount = 12000.0 |
---|
132 | else: |
---|
133 | amount = 10000.0 |
---|
134 | elif category == 'bed_allocation': |
---|
135 | p_item = self.getAccommodationDetails(student)['bt'] |
---|
136 | amount = academic_session.booking_fee |
---|
137 | if amount in (0.0, None): |
---|
138 | return _('Amount could not be determined.'), None |
---|
139 | for key in student['payments'].keys(): |
---|
140 | ticket = student['payments'][key] |
---|
141 | if ticket.p_state == 'paid' and\ |
---|
142 | ticket.p_category == category and \ |
---|
143 | ticket.p_item == p_item and \ |
---|
144 | ticket.p_session == p_session: |
---|
145 | return _('This type of payment has already been made.'), None |
---|
146 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
147 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
148 | payment.p_id = "p%s" % timestamp |
---|
149 | payment.p_category = category |
---|
150 | payment.p_item = p_item |
---|
151 | payment.p_session = p_session |
---|
152 | payment.p_level = p_level |
---|
153 | payment.p_current = p_current |
---|
154 | payment.amount_auth = amount |
---|
155 | return None, payment |
---|
156 | |
---|
157 | def getAccommodationDetails(self, student): |
---|
158 | """Determine the accommodation data of a student. |
---|
159 | """ |
---|
160 | d = {} |
---|
161 | d['error'] = u'' |
---|
162 | hostels = grok.getSite()['hostels'] |
---|
163 | d['booking_session'] = hostels.accommodation_session |
---|
164 | d['allowed_states'] = hostels.accommodation_states |
---|
165 | d['startdate'] = hostels.startdate |
---|
166 | d['enddate'] = hostels.enddate |
---|
167 | d['expired'] = hostels.expired |
---|
168 | # Determine bed type |
---|
169 | studycourse = student['studycourse'] |
---|
170 | certificate = getattr(studycourse,'certificate',None) |
---|
171 | entry_session = studycourse.entry_session |
---|
172 | current_level = studycourse.current_level |
---|
173 | if None in (entry_session, current_level, certificate): |
---|
174 | return d |
---|
175 | end_level = certificate.end_level |
---|
176 | if current_level == 10: |
---|
177 | bt = 'pr' |
---|
178 | elif entry_session == grok.getSite()['hostels'].accommodation_session: |
---|
179 | bt = 'fr' |
---|
180 | elif current_level >= end_level: |
---|
181 | bt = 'fi' |
---|
182 | else: |
---|
183 | bt = 're' |
---|
184 | sex = 'male' |
---|
185 | if student.sex == 'f': |
---|
186 | sex = 'female' |
---|
187 | special_handling = 'regular' |
---|
188 | if student.faccode == 'SSE': |
---|
189 | special_handling = 'sse' |
---|
190 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
191 | return d |
---|
192 | |
---|
193 | |
---|
194 | # FUTMinna prefix |
---|
195 | STUDENT_ID_PREFIX = u'M' |
---|