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

Last change on this file since 17601 was 17599, checked in by Henrik Bettermann, 14 months ago

Reconfigure clearance (acceptance) fee.

  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1## $Id: utils.py 17599 2023-10-01 20:32:58Z 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    # refix
38    STUDENT_ID_PREFIX = u'D'
39
40    def _clearancePaymentMade(self, student):
41        if len(student['payments']):
42            for ticket in student['payments'].values():
43                if ticket.p_state == 'paid' and \
44                    ticket.p_category == 'clearance':
45                    return True
46        return False
47
48    def _isPaymentDisabled(self, p_session, category, student):
49        academic_session = self._getSessionConfiguration(p_session)
50        if category == 'schoolfee':
51            if 'sf_all' in academic_session.payment_disabled:
52                return True
53            if student.current_mode == 'ug_ft' and \
54                'sf_ugft' in academic_session.payment_disabled:
55                return True
56        return False
57
58    def setPaymentDetails(self, category, student,
59            previous_session=None, previous_level=None, combi=[]):
60        """Create a payment ticket and set the payment data of a
61        student for the payment category specified.
62        """
63        p_item = u''
64        amount = 0.0
65        if previous_session:
66            if previous_session < student['studycourse'].entry_session:
67                return _('The previous session must not fall below '
68                         'your entry session.'), None
69            if category == 'schoolfee':
70                # School fee is always paid for the following session
71                if previous_session > student['studycourse'].current_session:
72                    return _('This is not a previous session.'), None
73            else:
74                if previous_session > student['studycourse'].current_session - 1:
75                    return _('This is not a previous session.'), None
76            p_session = previous_session
77            p_level = previous_level
78            p_current = False
79        else:
80            p_session = student['studycourse'].current_session
81            p_level = student['studycourse'].current_level
82            p_current = True
83        academic_session = self._getSessionConfiguration(p_session)
84        if academic_session == None:
85            return _(u'Session configuration object is not available.'), None
86        # Determine fee.
87        if category == 'schoolfee':
88            try:
89                certificate = student['studycourse'].certificate
90                p_item = certificate.code
91            except (AttributeError, TypeError):
92                return _('Study course data are incomplete.'), None
93            if previous_session:
94                # Students can pay for previous sessions in all
95                # workflow states.  Fresh students are excluded by the
96                # update method of the PreviousPaymentAddFormPage.
97                if previous_level == 100:
98                    amount = getattr(certificate, 'school_fee_1', 0.0)
99                else:
100                    amount = getattr(certificate, 'school_fee_2', 0.0)
101            else:
102                if student.faccode == 'JUPEB':
103                    if not self._clearancePaymentMade(student):
104                        return _(u'Acceptance fee must be paid first.'), None
105                if student.state == CLEARED:
106                    amount = getattr(certificate, 'school_fee_1', 0.0)
107                elif student.state == RETURNING:
108                    # In case of returning school fee payment the
109                    # payment session and level contain the values of
110                    # the session the student has paid for. Payment
111                    # session is always next session.
112                    p_session, p_level = self.getReturningData(student)
113                    academic_session = self._getSessionConfiguration(p_session)
114                    if academic_session == None:
115                        return _(
116                            u'Session configuration object is not available.'
117                            ), None
118                    amount = getattr(certificate, 'school_fee_2', 0.0)
119                elif student.is_postgrad and student.state == PAID:
120                    # Returning postgraduate students also pay for the
121                    # next session but their level always remains the
122                    # same.
123                    p_session += 1
124                    academic_session = self._getSessionConfiguration(p_session)
125                    if academic_session == None:
126                        return _(
127                            u'Session configuration object is not available.'
128                            ), None
129                    amount = getattr(certificate, 'school_fee_2', 0.0)
130            if amount and not local(student):
131                if student.faccode == 'NCE':
132                    amount += 14600
133                elif student.faccode not in ('PRE', 'JUPEB', 'DELSU'):
134                    amount += 40000
135        elif category == 'clearance':
136            try:
137                p_item = student['studycourse'].certificate.code
138            except (AttributeError, TypeError):
139                return _('Study course data are incomplete.'), None
140            amount = academic_session.clearance_fee
141            if student.current_mode in ('ug_ft', 'de_ft'):
142                if local(student):
143                    amount = academic_session.ugftlocal_clearance_fee
144                else:
145                    amount = academic_session.ugft_clearance_fee
146        elif category == 'bed_allocation':
147            acco_details = self.getAccommodationDetails(student)
148            p_session = acco_details['booking_session']
149            p_item = acco_details['bt']
150            amount = academic_session.booking_fee
151        elif category == 'hostel_maintenance':
152            amount = 0.0
153            booking_session = grok.getSite()['hostels'].accommodation_session
154            bedticket = student['accommodation'].get(str(booking_session), None)
155            if bedticket is not None and bedticket.bed is not None:
156                p_session = booking_session
157                p_item = bedticket.bed_coordinates
158                if bedticket.bed.__parent__.maint_fee > 0:
159                    amount = bedticket.bed.__parent__.maint_fee
160                else:
161                    # fallback
162                    amount = academic_session.maint_fee
163            else:
164                return _(u'No bed allocated.'), None
165        elif category == 'combi' and combi:
166            categories = getUtility(IKofaUtils).COMBI_PAYMENT_CATEGORIES
167            for cat in combi:
168                fee_name = cat + '_fee'
169                cat_amount = getattr(academic_session, fee_name, 0.0)
170                if not cat_amount:
171                    return _('%s undefined.' % categories[cat]), None
172                amount += cat_amount
173                p_item += u'%s + ' % categories[cat]
174            p_item = p_item.strip(' + ')
175        else:
176            fee_name = category + '_fee'
177            amount = getattr(academic_session, fee_name, 0.0)
178        if category != 'bed_allocation' and amount in (0.0, None):
179            return _('Amount could not be determined.'), None
180        if self.samePaymentMade(student, category, p_item, p_session):
181            return _('This type of payment has already been made.'), None
182        if self._isPaymentDisabled(p_session, category, student):
183            return _('This category of payments has been disabled.'), None
184        payment = createObject(u'waeup.StudentOnlinePayment')
185        timestamp = ("%d" % int(time()*10000))[1:]
186        payment.p_id = "p%s" % timestamp
187        payment.p_category = category
188        payment.p_item = p_item
189        payment.p_session = p_session
190        payment.p_level = p_level
191        payment.p_current = p_current
192        payment.amount_auth = amount
193        payment.p_combi = combi
194        return None, payment
195
196    def getAccommodationDetails(self, student):
197        """Determine the accommodation data of a student.
198        """
199        d = {}
200        d['error'] = u''
201        hostels = grok.getSite()['hostels']
202        d['booking_session'] = hostels.accommodation_session
203        d['allowed_states'] = hostels.accommodation_states
204        d['startdate'] = hostels.startdate
205        d['enddate'] = hostels.enddate
206        d['expired'] = hostels.expired
207        # Determine bed type
208        bt = 'all'
209        #if student.current_level >= 300:
210        #    bt = 'na'
211        if student.sex == 'f':
212            sex = 'female'
213        else:
214            sex = 'male'
215        special_handling = 'regular'
216        #if student.faccode in ('FLW', 'FMS'):
217        #    special_handling = 'oyibu'
218        #if student.faccode in ('FET', 'FES'):
219        #    special_handling = 'alero'
220        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
221        return d
222
Note: See TracBrowser for help on using the repository browser.