source: main/kofacustom.unidel/trunk/src/kofacustom/unidel/students/utils.py @ 17940

Last change on this file since 17940 was 17940, checked in by Henrik Bettermann, 4 weeks ago

Customize getAccommodationDetails

  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1## $Id: utils.py 17940 2024-10-08 01:15:11Z 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##
18import grok
19from time import time
20from zope.component import createObject, getUtility
21from waeup.kofa.interfaces import (IKofaUtils,
22    CLEARED, RETURNING, PAID, REGISTERED, VALIDATED)
23from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
24from kofacustom.unidel.interfaces import MessageFactory as _
25
26def local(student):
27    lga = getattr(student, 'lga')
28    if lga and lga.startswith('delta'):
29        return True
30    return False
31
32class 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 = 40000
169                #if student.faccode in ('FAG', 'FCP', 'FES',
170                #                       'FSS', 'FMS', 'FSC',
171                #                       'FAT', 'FED'):
172                #    if p_level == 200:
173                #        non_local_surcharge = 47000
174                #    if p_level == 500:
175                #        non_local_surcharge = 50000
176                #if student.faccode == 'FET':
177                #    if p_level == 500:
178                #        non_local_surcharge = 31000
179                amount += non_local_surcharge
180        elif category == 'clearance':
181            try:
182                p_item = student['studycourse'].certificate.code
183            except (AttributeError, TypeError):
184                return _('Study course data are incomplete.'), None
185            amount = academic_session.clearance_fee
186            if student.current_mode in ('ug_ft', 'de_ft'):
187                if local(student):
188                    amount = academic_session.ugftlocal_clearance_fee
189                else:
190                    amount = academic_session.ugft_clearance_fee
191            elif student.faccode == 'PRE':
192                amount = 20000.0
193            elif student.faccode == 'JUPEB':
194                amount = 25000.0
195            elif student.current_mode.startswith('dp'):
196                amount = 20000.0
197        elif category == 'bed_allocation':
198            acco_details = self.getAccommodationDetails(student)
199            p_session = acco_details['booking_session']
200            p_item = acco_details['bt']
201            amount = academic_session.booking_fee
202        elif category == 'hostel_maintenance':
203            amount = 0.0
204            booking_session = grok.getSite()['hostels'].accommodation_session
205            bedticket = student['accommodation'].get(str(booking_session), None)
206            if bedticket is not None and bedticket.bed is not None:
207                p_session = booking_session
208                p_item = bedticket.bed_coordinates
209                if bedticket.bed.__parent__.maint_fee > 0:
210                    amount = bedticket.bed.__parent__.maint_fee
211                else:
212                    # fallback
213                    amount = academic_session.maint_fee
214            else:
215                return _(u'No bed allocated.'), None
216        elif category == 'combi' and combi:
217            categories = getUtility(IKofaUtils).COMBI_PAYMENT_CATEGORIES
218            for cat in combi:
219                fee_name = cat + '_fee'
220                cat_amount = getattr(academic_session, fee_name, 0.0)
221                if not cat_amount:
222                    return _('%s undefined.' % categories[cat]), None
223                amount += cat_amount
224                p_item += u'%s + ' % categories[cat]
225            p_item = p_item.strip(' + ')
226        else:
227            fee_name = category + '_fee'
228            amount = getattr(academic_session, fee_name, 0.0)
229        if category != 'bed_allocation' and amount in (0.0, None):
230            return _('Amount could not be determined.'), None
231        if self.samePaymentMade(student, category, p_item, p_session):
232            return _('This type of payment has already been made.'), None
233        if self._isPaymentDisabled(p_session, category, student):
234            return _('This category of payments has been disabled.'), None
235        payment = createObject(u'waeup.StudentOnlinePayment')
236        timestamp = ("%d" % int(time()*10000))[1:]
237        payment.p_id = "p%s" % timestamp
238        payment.p_category = category
239        payment.p_item = p_item
240        payment.p_session = p_session
241        payment.p_level = p_level
242        payment.p_current = p_current
243        payment.amount_auth = amount
244        payment.p_combi = combi
245        return None, payment
246
247    def checkAccommodationRequirements(self, student, acc_details):
248        msg = super(CustomStudentsUtils, self).checkAccommodationRequirements(
249            student, acc_details)
250        if msg:
251            return msg
252        if student.faccode not in ('FLW', 'FMED',):
253            return _('You are not eligible to book accommodation.')
254        return
255
256    def getAccommodationDetails(self, student):
257        """Determine the accommodation data of a student.
258        """
259        d = {}
260        d['error'] = u''
261        hostels = grok.getSite()['hostels']
262        d['booking_session'] = hostels.accommodation_session
263        d['allowed_states'] = hostels.accommodation_states
264        d['startdate'] = hostels.startdate
265        d['enddate'] = hostels.enddate
266        d['expired'] = hostels.expired
267        studycourse = student['studycourse']
268        certificate = getattr(studycourse,'certificate',None)
269        entry_session = studycourse.entry_session
270        current_level = studycourse.current_level
271        if None in (entry_session, current_level, certificate):
272            return d
273        end_level = certificate.end_level
274        # Determine bed type
275        if entry_session == grok.getSite()['hostels'].accommodation_session:
276            bt = 'fr'
277        elif current_level >= end_level:
278            bt = 'fi'
279        else:
280            bt = 're'
281        #if student.current_level >= 300:
282        #    bt = 'na'
283        if student.sex == 'f':
284            sex = 'female'
285        else:
286            sex = 'male'
287        special_handling = 'regular'
288        if student.faccode in ('FLW',):
289            special_handling = 'oyibu'
290        elif student.faccode in ('FMED',):
291            special_handling = 'alero'
292        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
293        return d
294
Note: See TracBrowser for help on using the repository browser.