1 | ## $Id: utils.py 17944 2024-10-16 14:09:00Z 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 | from time import time |
---|
20 | from zope.component import createObject, getUtility |
---|
21 | from waeup.kofa.interfaces import (IKofaUtils, |
---|
22 | CLEARED, RETURNING, PAID, REGISTERED, VALIDATED) |
---|
23 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
24 | from kofacustom.unidel.interfaces import MessageFactory as _ |
---|
25 | |
---|
26 | def local(student): |
---|
27 | lga = getattr(student, 'lga') |
---|
28 | if lga and lga.startswith('delta'): |
---|
29 | return True |
---|
30 | return False |
---|
31 | |
---|
32 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
33 | """A collection of customized methods. |
---|
34 | |
---|
35 | """ |
---|
36 | |
---|
37 | STUDENT_ID_PREFIX = u'D' |
---|
38 | |
---|
39 | def warnCreditsOOR(self, studylevel, course=None): |
---|
40 | """ |
---|
41 | """ |
---|
42 | if course and studylevel.total_credits + course.credits > 48: |
---|
43 | return _('Maximum credits exceeded.') |
---|
44 | if studylevel.total_credits > 48: |
---|
45 | return _('Maximum credits exceeded.') |
---|
46 | if not course: |
---|
47 | if studylevel.level != 300 and studylevel.total_credits < 30: |
---|
48 | return _('Minimum credits not reached.') |
---|
49 | if studylevel.level == 300 and studylevel.total_credits < 27: |
---|
50 | return _('Minimum credits not reached.') |
---|
51 | return |
---|
52 | |
---|
53 | def _clearancePaymentMade(self, student): |
---|
54 | if len(student['payments']): |
---|
55 | for ticket in student['payments'].values(): |
---|
56 | if ticket.p_state == 'paid' and \ |
---|
57 | ticket.p_category == 'clearance': |
---|
58 | return True |
---|
59 | return False |
---|
60 | |
---|
61 | def _isPaymentDisabled(self, p_session, category, student): |
---|
62 | academic_session = self._getSessionConfiguration(p_session) |
---|
63 | if category == 'schoolfee': |
---|
64 | if 'sf_all' in academic_session.payment_disabled: |
---|
65 | return True |
---|
66 | if student.current_mode == 'ug_ft' and \ |
---|
67 | 'sf_ugft' in academic_session.payment_disabled: |
---|
68 | return True |
---|
69 | return False |
---|
70 | |
---|
71 | def setPaymentDetails(self, category, student, |
---|
72 | previous_session=None, previous_level=None, combi=[]): |
---|
73 | """Create a payment ticket and set the payment data of a |
---|
74 | student for the payment category specified. |
---|
75 | """ |
---|
76 | p_item = u'' |
---|
77 | amount = 0.0 |
---|
78 | if previous_session: |
---|
79 | if previous_session < student['studycourse'].entry_session: |
---|
80 | return _('The previous session must not fall below ' |
---|
81 | 'your entry session.'), None |
---|
82 | if category == 'schoolfee': |
---|
83 | # School fee is always paid for the following session |
---|
84 | if previous_session > student['studycourse'].current_session: |
---|
85 | return _('This is not a previous session.'), None |
---|
86 | else: |
---|
87 | if previous_session > student['studycourse'].current_session - 1: |
---|
88 | return _('This is not a previous session.'), None |
---|
89 | p_session = previous_session |
---|
90 | p_level = previous_level |
---|
91 | p_current = False |
---|
92 | else: |
---|
93 | p_session = student['studycourse'].current_session |
---|
94 | p_level = student['studycourse'].current_level |
---|
95 | p_current = True |
---|
96 | academic_session = self._getSessionConfiguration(p_session) |
---|
97 | if academic_session == None: |
---|
98 | return _(u'Session configuration object is not available.'), None |
---|
99 | # Determine fee. |
---|
100 | if category == 'schoolfee': |
---|
101 | try: |
---|
102 | certificate = student['studycourse'].certificate |
---|
103 | p_item = certificate.code |
---|
104 | except (AttributeError, TypeError): |
---|
105 | return _('Study course data are incomplete.'), None |
---|
106 | if previous_session: |
---|
107 | # Students can pay for previous sessions in all |
---|
108 | # workflow states. Fresh students are excluded by the |
---|
109 | # update method of the PreviousPaymentAddFormPage. |
---|
110 | if previous_level == 100: |
---|
111 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
112 | else: |
---|
113 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
114 | else: |
---|
115 | if student.faccode == 'JUPEB': |
---|
116 | if not self._clearancePaymentMade(student): |
---|
117 | return _(u'Acceptance fee must be paid first.'), None |
---|
118 | if student.state == CLEARED: |
---|
119 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
120 | elif student.state == RETURNING: |
---|
121 | # In case of returning school fee payment the |
---|
122 | # payment session and level contain the values of |
---|
123 | # the session the student has paid for. Payment |
---|
124 | # session is always next session. |
---|
125 | p_session, p_level = self.getReturningData(student) |
---|
126 | academic_session = self._getSessionConfiguration(p_session) |
---|
127 | if academic_session == None: |
---|
128 | return _( |
---|
129 | u'Session configuration object is not available.' |
---|
130 | ), None |
---|
131 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
132 | elif student.is_postgrad and student.state == PAID: |
---|
133 | # Returning postgraduate students also pay for the |
---|
134 | # next session but their level always remains the |
---|
135 | # same. |
---|
136 | p_session += 1 |
---|
137 | academic_session = self._getSessionConfiguration(p_session) |
---|
138 | if academic_session == None: |
---|
139 | return _( |
---|
140 | u'Session configuration object is not available.' |
---|
141 | ), None |
---|
142 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
143 | # Add surcharges for students in FMED and FBM |
---|
144 | if student.faccode == 'FMED': |
---|
145 | fmed_surcharge = 0.0 |
---|
146 | if divmod(p_level,100)[0] == 3: |
---|
147 | fmed_surcharge = 296000.0 |
---|
148 | elif divmod(p_level,100)[0] == 4: |
---|
149 | fmed_surcharge = 224700.0 |
---|
150 | elif divmod(p_level,100)[0] == 5: |
---|
151 | fmed_surcharge = 220700.0 |
---|
152 | elif divmod(p_level,100)[0] == 6: |
---|
153 | fmed_surcharge = 244950.0 |
---|
154 | amount += fmed_surcharge |
---|
155 | if student.faccode == 'FBM': |
---|
156 | fbm_surcharge = 0.0 |
---|
157 | if divmod(p_level,100)[0] == 3: |
---|
158 | fbm_surcharge = 146000.0 |
---|
159 | elif divmod(p_level,100)[0] == 4: |
---|
160 | fbm_surcharge = 204700.0 |
---|
161 | elif divmod(p_level,100)[0] == 5: |
---|
162 | fbm_surcharge = 229950.0 |
---|
163 | if student.depcode == 'NSG' and divmod(p_level,100)[0] == 3: |
---|
164 | fbm_surcharge -= 50000.0 |
---|
165 | amount += fbm_surcharge |
---|
166 | # Add non-local surcharges |
---|
167 | if amount and not local(student): |
---|
168 | non_local_surcharge = 0 |
---|
169 | if student.faccode not in ('IJMB', 'PRE', 'JUPEB'): |
---|
170 | non_local_surcharge = 40000 |
---|
171 | #if student.faccode in ('FAG', 'FCP', 'FES', |
---|
172 | # 'FSS', 'FMS', 'FSC', |
---|
173 | # 'FAT', 'FED'): |
---|
174 | # if p_level == 200: |
---|
175 | # non_local_surcharge = 47000 |
---|
176 | # if p_level == 500: |
---|
177 | # non_local_surcharge = 50000 |
---|
178 | #if student.faccode == 'FET': |
---|
179 | # if p_level == 500: |
---|
180 | # non_local_surcharge = 31000 |
---|
181 | amount += non_local_surcharge |
---|
182 | elif category == 'clearance': |
---|
183 | try: |
---|
184 | p_item = student['studycourse'].certificate.code |
---|
185 | except (AttributeError, TypeError): |
---|
186 | return _('Study course data are incomplete.'), None |
---|
187 | amount = academic_session.clearance_fee |
---|
188 | if student.current_mode in ('ug_ft', 'de_ft'): |
---|
189 | if local(student): |
---|
190 | amount = academic_session.ugftlocal_clearance_fee |
---|
191 | else: |
---|
192 | amount = academic_session.ugft_clearance_fee |
---|
193 | elif student.faccode == 'PRE': |
---|
194 | amount = 20000.0 |
---|
195 | elif student.faccode == 'JUPEB': |
---|
196 | amount = 25000.0 |
---|
197 | elif student.current_mode.startswith('dp'): |
---|
198 | amount = 20000.0 |
---|
199 | elif category == 'bed_allocation': |
---|
200 | acco_details = self.getAccommodationDetails(student) |
---|
201 | p_session = acco_details['booking_session'] |
---|
202 | p_item = acco_details['bt'] |
---|
203 | amount = academic_session.booking_fee |
---|
204 | elif category == 'hostel_maintenance': |
---|
205 | amount = 0.0 |
---|
206 | booking_session = grok.getSite()['hostels'].accommodation_session |
---|
207 | bedticket = student['accommodation'].get(str(booking_session), None) |
---|
208 | if bedticket is not None and bedticket.bed is not None: |
---|
209 | p_session = booking_session |
---|
210 | p_item = bedticket.bed_coordinates |
---|
211 | if bedticket.bed.__parent__.maint_fee > 0: |
---|
212 | amount = bedticket.bed.__parent__.maint_fee |
---|
213 | else: |
---|
214 | # fallback |
---|
215 | amount = academic_session.maint_fee |
---|
216 | else: |
---|
217 | return _(u'No bed allocated.'), None |
---|
218 | elif category == 'combi' and combi: |
---|
219 | categories = getUtility(IKofaUtils).COMBI_PAYMENT_CATEGORIES |
---|
220 | for cat in combi: |
---|
221 | fee_name = cat + '_fee' |
---|
222 | cat_amount = getattr(academic_session, fee_name, 0.0) |
---|
223 | if not cat_amount: |
---|
224 | return _('%s undefined.' % categories[cat]), None |
---|
225 | amount += cat_amount |
---|
226 | p_item += u'%s + ' % categories[cat] |
---|
227 | p_item = p_item.strip(' + ') |
---|
228 | else: |
---|
229 | fee_name = category + '_fee' |
---|
230 | amount = getattr(academic_session, fee_name, 0.0) |
---|
231 | if category != 'bed_allocation' and amount in (0.0, None): |
---|
232 | return _('Amount could not be determined.'), None |
---|
233 | if self.samePaymentMade(student, category, p_item, p_session): |
---|
234 | return _('This type of payment has already been made.'), None |
---|
235 | if self._isPaymentDisabled(p_session, category, student): |
---|
236 | return _('This category of payments has been disabled.'), None |
---|
237 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
238 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
239 | payment.p_id = "p%s" % timestamp |
---|
240 | payment.p_category = category |
---|
241 | payment.p_item = p_item |
---|
242 | payment.p_session = p_session |
---|
243 | payment.p_level = p_level |
---|
244 | payment.p_current = p_current |
---|
245 | payment.amount_auth = amount |
---|
246 | payment.p_combi = combi |
---|
247 | return None, payment |
---|
248 | |
---|
249 | #def checkAccommodationRequirements(self, student, acc_details): |
---|
250 | # msg = super(CustomStudentsUtils, self).checkAccommodationRequirements( |
---|
251 | # student, acc_details) |
---|
252 | # if msg: |
---|
253 | # return msg |
---|
254 | # if student.faccode not in ('FLW', 'FMED',): |
---|
255 | # return _('You are not eligible to book accommodation.') |
---|
256 | # return |
---|
257 | |
---|
258 | def getAccommodationDetails(self, student): |
---|
259 | """Determine the accommodation data of a student. |
---|
260 | """ |
---|
261 | d = {} |
---|
262 | d['error'] = u'' |
---|
263 | hostels = grok.getSite()['hostels'] |
---|
264 | d['booking_session'] = hostels.accommodation_session |
---|
265 | d['allowed_states'] = hostels.accommodation_states |
---|
266 | d['startdate'] = hostels.startdate |
---|
267 | d['enddate'] = hostels.enddate |
---|
268 | d['expired'] = hostels.expired |
---|
269 | studycourse = student['studycourse'] |
---|
270 | certificate = getattr(studycourse,'certificate',None) |
---|
271 | entry_session = studycourse.entry_session |
---|
272 | current_level = studycourse.current_level |
---|
273 | if None in (entry_session, current_level, certificate): |
---|
274 | return d |
---|
275 | end_level = certificate.end_level |
---|
276 | # Determine bed type |
---|
277 | if entry_session == grok.getSite()['hostels'].accommodation_session: |
---|
278 | bt = 'fr' |
---|
279 | elif current_level >= end_level: |
---|
280 | bt = 'fi' |
---|
281 | else: |
---|
282 | bt = 're' |
---|
283 | #if student.current_level >= 300: |
---|
284 | # bt = 'na' |
---|
285 | if student.sex == 'f': |
---|
286 | sex = 'female' |
---|
287 | else: |
---|
288 | sex = 'male' |
---|
289 | special_handling = 'regular' |
---|
290 | if student.faccode in ('FLW',): |
---|
291 | special_handling = 'oyibu' |
---|
292 | elif student.faccode in ('FMED',): |
---|
293 | special_handling = 'alero' |
---|
294 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
295 | return d |
---|
296 | |
---|