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

Last change on this file since 17921 was 17749, checked in by Henrik Bettermann, 5 months ago

The logic was wrong.

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