source: main/waeup.kwarapoly/trunk/src/waeup/kwarapoly/students/utils.py @ 9396

Last change on this file since 9396 was 9396, checked in by Henrik Bettermann, 12 years ago

Shorten lines slightly.

  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1## $Id: utils.py 9396 2012-10-23 15:15:10Z 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
19import random
20from time import time
21from zope.component import createObject
22from waeup.kofa.interfaces import CLEARED, RETURNING, PAID
23from kofacustom.nigeria.students.utils import NigeriaStudentsUtils
24from waeup.kofa.accesscodes import create_accesscode
25from waeup.kofa.interfaces import CLEARED, RETURNING
26from waeup.kofa.fees import FeeTable
27from waeup.kwarapoly.interfaces import MessageFactory as _
28
29PAYMENT_LEVELS = (10, 100, 200, 210, 300, 400, 500, 510, 600, 999)
30
31FEES_PARAMS = (
32        ('ft', 'pt'),
33        ('local', 'non-local'),
34        ('science','arts'),
35        PAYMENT_LEVELS
36    )
37
38FEES_VALUES = (
39        (
40          (
41            (34400.0, 37100.0, 43000.0, 29300.0, 0.0, 37900.0, 29700.0, 29900.0, 0.0, 47550.0), # science
42            (32900.0, 35600.0, 28000.0, 27800.0, 0.0, 36400.0, 28200.0, 28400.0, 0.0, 47550.0)  # arts
43          ), # local
44          (
45            (41930.0, 44630.0, 44590.0, 36400.0, 0.0, 46330.0, 32900.0, 38100.0, 0.0, 60980.0), # science
46            (40430.0, 43130.0, 29590.0, 34900.0, 0.0, 44830.0, 31400.0, 36600.0, 0.0, 60980.0)  # arts
47          ), # non-local
48        ), # ft
49        (
50          (
51            (0.0, 38400.0, 30900.0, 0.0, 30900.0, 39000.0, 31550.0, 0.0, 31550.0, 0.0), # science
52            (0.0, 36900.0, 29400.0, 0.0, 29400.0, 37500.0, 30050.0, 0.0, 30050.0, 0.0)  # arts
53          ), # local
54          (
55            (0.0, 45600.0, 32350.0, 0.0, 32350.0, 47200.0, 35750.0, 0.0, 35750.0, 0.0), # science
56            (0.0, 44100.0, 30850.0, 0.0, 30850.0, 45700.0, 34250.0, 0.0, 34250.0, 0.0)  # arts
57          ), # non-local
58        ), # pt
59    )
60
61SCHOOL_FEES = FeeTable(FEES_PARAMS, FEES_VALUES)
62
63def local_nonlocal(student):
64    lga = getattr(student, 'lga')
65    if lga and student.lga.startswith('kwara'):
66        return 'local'
67    else:
68        return 'non-local'
69
70def arts_science(student):
71    if student.faccode == 'IFMS':
72        return 'arts'
73    else:
74        return 'science'
75
76def pt_ft(student):
77    if student.current_mode.endswith('pt'):
78        return 'pt'
79    else:
80        return 'ft'
81
82class CustomStudentsUtils(NigeriaStudentsUtils):
83    """A collection of customized methods.
84
85    """
86
87
88    def selectBed(self, available_beds):
89        """Randomly select a bed from a list of available beds.
90
91        """
92        return random.choice(available_beds)
93
94    def getReturningData(self, student):
95        """ This method defines what happens after school fee payment
96        of returning students depending on the student's senate verdict.
97        """
98        prev_level = student['studycourse'].current_level
99        cur_verdict = student['studycourse'].current_verdict
100        if cur_verdict in ('A','B','L','M','N','Z',):
101            # Successful student
102            new_level = divmod(int(prev_level),100)[0]*100 + 100
103        elif cur_verdict == 'C':
104            # Student on probation
105            new_level = int(prev_level) + 10
106        else:
107            # Student is somehow in an undefined state.
108            # Level has to be set manually.
109            new_level = prev_level
110        new_session = student['studycourse'].current_session + 1
111        return new_session, new_level
112
113    def setPaymentDetails(self, category, student,
114            previous_session=None, previous_level=None):
115        """Create Payment object and set the payment data of a student for
116        the payment category specified.
117
118        """
119        details = {}
120        p_item = u''
121        amount = 0.0
122        error = u''
123        if previous_session:
124            return _('Previous session payment not yet implemented.'), None
125        p_session = student['studycourse'].current_session
126        p_level = student['studycourse'].current_level
127        p_current = True
128        session = str(p_session)
129        try:
130            academic_session = grok.getSite()['configuration'][session]
131        except KeyError:
132            return _(u'Session configuration object is not available.'), None
133        if category == 'transfer':
134            amount = academic_session.transfer_fee
135        elif category == 'gown':
136            amount = academic_session.gown_fee
137        elif category == 'bed_allocation':
138            amount = academic_session.booking_fee
139        elif category == 'hostel_maintenance':
140            amount = academic_session.maint_fee
141        elif category == 'clearance':
142            amount = academic_session.clearance_fee
143            try:
144                p_item = student['studycourse'].certificate.code
145            except (AttributeError, TypeError):
146                return _('Study course data are incomplete.'), None
147        elif category == 'schoolfee':
148            try:
149                certificate = student['studycourse'].certificate
150                p_item = certificate.code
151            except (AttributeError, TypeError):
152                return _('Study course data are incomplete.'), None
153            if student.state == RETURNING:
154                p_session, p_level = self.getReturningData(student)
155            if student.state in (RETURNING, CLEARED):
156                if p_level in PAYMENT_LEVELS:
157                    amount = SCHOOL_FEES.get_fee(
158                        (pt_ft(student),
159                         local_nonlocal(student),
160                         arts_science(student),
161                         p_level)
162                        )
163        if amount in (0.0, None):
164            return _(u'Amount could not be determined.'), None
165        for key in student['payments'].keys():
166            ticket = student['payments'][key]
167            if ticket.p_state == 'paid' and\
168               ticket.p_category == category and \
169               ticket.p_item == p_item and \
170               ticket.p_session == p_session:
171                  return _('This type of payment has already been made.'), None
172        payment = createObject(u'waeup.StudentOnlinePayment')
173        timestamp = ("%d" % int(time()*10000))[1:]
174        payment.p_id = "p%s" % timestamp
175        payment.p_category = category
176        payment.p_item = p_item
177        payment.p_session = p_session
178        payment.p_level = p_level
179        payment.p_current = p_current
180        payment.amount_auth = float(amount)
181        return None, payment
182
183    def getAccommodationDetails(self, student):
184        """Determine the accommodation data of a student.
185        """
186        d = {}
187        d['error'] = u''
188        hostels = grok.getSite()['hostels']
189        d['booking_session'] = hostels.accommodation_session
190        d['allowed_states'] = hostels.accommodation_states
191        d['startdate'] = hostels.startdate
192        d['enddate'] = hostels.enddate
193        d['expired'] = hostels.expired
194        # Determine bed type
195        studycourse = student['studycourse']
196        certificate = getattr(studycourse,'certificate',None)
197        current_level = studycourse.current_level
198        if None in (current_level, certificate):
199            return d
200        end_level = certificate.end_level
201        if current_level == 10:
202            bt = 'pr'
203        elif current_level == 100:
204            bt = 'fr'
205        elif current_level >= 300:
206            bt = 'fi'
207        else:
208            bt = 're'
209        if student.sex == 'f':
210            sex = 'female'
211        else:
212            sex = 'male'
213        special_handling = 'regular'
214        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
215        return d
216
217    # KwaraPoly prefix
218    STUDENT_ID_PREFIX = u'W'
Note: See TracBrowser for help on using the repository browser.