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

Last change on this file since 17935 was 17932, checked in by Henrik Bettermann, 5 weeks ago

Set non-local student surcharges.

  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1## $Id: utils.py 17932 2024-09-30 20:49:43Z 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 non-local surcharges
144            if amount and not local(student):
145                non_local_surcharge = 40000
146                if student.faccode in ('FAG', 'FCP', 'FES',
147                                       'FSS', 'FMS', 'FSC',
148                                       'FAT', 'FED'):
149                    if p_level == 200:
150                        non_local_surcharge = 47000
151                    if p_level == 500:
152                        non_local_surcharge = 50000
153                if student.faccode == 'FET':
154                    if p_level == 500:
155                        non_local_surcharge = 31000
156                amount += non_local_surcharge
157        elif category == 'clearance':
158            try:
159                p_item = student['studycourse'].certificate.code
160            except (AttributeError, TypeError):
161                return _('Study course data are incomplete.'), None
162            amount = academic_session.clearance_fee
163            if student.current_mode in ('ug_ft', 'de_ft'):
164                if local(student):
165                    amount = academic_session.ugftlocal_clearance_fee
166                else:
167                    amount = academic_session.ugft_clearance_fee
168            elif student.faccode == 'PRE':
169                amount = 20000.0
170            elif student.faccode == 'JUPEB':
171                amount = 25000.0
172            elif student.current_mode.startswith('dp'):
173                amount = 20000.0
174        elif category == 'bed_allocation':
175            acco_details = self.getAccommodationDetails(student)
176            p_session = acco_details['booking_session']
177            p_item = acco_details['bt']
178            amount = academic_session.booking_fee
179        elif category == 'hostel_maintenance':
180            amount = 0.0
181            booking_session = grok.getSite()['hostels'].accommodation_session
182            bedticket = student['accommodation'].get(str(booking_session), None)
183            if bedticket is not None and bedticket.bed is not None:
184                p_session = booking_session
185                p_item = bedticket.bed_coordinates
186                if bedticket.bed.__parent__.maint_fee > 0:
187                    amount = bedticket.bed.__parent__.maint_fee
188                else:
189                    # fallback
190                    amount = academic_session.maint_fee
191            else:
192                return _(u'No bed allocated.'), None
193        elif category == 'combi' and combi:
194            categories = getUtility(IKofaUtils).COMBI_PAYMENT_CATEGORIES
195            for cat in combi:
196                fee_name = cat + '_fee'
197                cat_amount = getattr(academic_session, fee_name, 0.0)
198                if not cat_amount:
199                    return _('%s undefined.' % categories[cat]), None
200                amount += cat_amount
201                p_item += u'%s + ' % categories[cat]
202            p_item = p_item.strip(' + ')
203        else:
204            fee_name = category + '_fee'
205            amount = getattr(academic_session, fee_name, 0.0)
206        if category != 'bed_allocation' and amount in (0.0, None):
207            return _('Amount could not be determined.'), None
208        if self.samePaymentMade(student, category, p_item, p_session):
209            return _('This type of payment has already been made.'), None
210        if self._isPaymentDisabled(p_session, category, student):
211            return _('This category of payments has been disabled.'), None
212        payment = createObject(u'waeup.StudentOnlinePayment')
213        timestamp = ("%d" % int(time()*10000))[1:]
214        payment.p_id = "p%s" % timestamp
215        payment.p_category = category
216        payment.p_item = p_item
217        payment.p_session = p_session
218        payment.p_level = p_level
219        payment.p_current = p_current
220        payment.amount_auth = amount
221        payment.p_combi = combi
222        return None, payment
223
224    def getAccommodationDetails(self, student):
225        """Determine the accommodation data of a student.
226        """
227        d = {}
228        d['error'] = u''
229        hostels = grok.getSite()['hostels']
230        d['booking_session'] = hostels.accommodation_session
231        d['allowed_states'] = hostels.accommodation_states
232        d['startdate'] = hostels.startdate
233        d['enddate'] = hostels.enddate
234        d['expired'] = hostels.expired
235        studycourse = student['studycourse']
236        certificate = getattr(studycourse,'certificate',None)
237        entry_session = studycourse.entry_session
238        current_level = studycourse.current_level
239        if None in (entry_session, current_level, certificate):
240            return d
241        end_level = certificate.end_level
242        # Determine bed type
243        if entry_session == grok.getSite()['hostels'].accommodation_session:
244            bt = 'fr'
245        elif current_level >= end_level:
246            bt = 'fi'
247        else:
248            bt = 're'
249        #if student.current_level >= 300:
250        #    bt = 'na'
251        if student.sex == 'f':
252            sex = 'female'
253        else:
254            sex = 'male'
255        special_handling = 'regular'
256        #if student.faccode in ('FLW', 'FMS'):
257        #    special_handling = 'oyibu'
258        #if student.faccode in ('FET', 'FES'):
259        #    special_handling = 'alero'
260        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
261        return d
262
Note: See TracBrowser for help on using the repository browser.