[16717] | 1 | ## $Id: utils.py 17173 2022-11-25 07:12:58Z henrik $ |
---|
[15614] | 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 | ## |
---|
[16893] | 18 | import grok |
---|
[15614] | 19 | from time import time |
---|
| 20 | from zope.component import createObject, getUtility |
---|
| 21 | from waeup.kofa.interfaces import (IKofaUtils, |
---|
| 22 | CLEARED, RETURNING, PAID, REGISTERED, VALIDATED) |
---|
| 23 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
[16721] | 24 | from kofacustom.unidel.interfaces import MessageFactory as _ |
---|
[15614] | 25 | |
---|
[16790] | 26 | def local(student): |
---|
| 27 | lga = getattr(student, 'lga') |
---|
| 28 | if lga and lga.startswith('delta'): |
---|
| 29 | return True |
---|
| 30 | return False |
---|
| 31 | |
---|
[15614] | 32 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
| 33 | """A collection of customized methods. |
---|
| 34 | |
---|
| 35 | """ |
---|
| 36 | |
---|
| 37 | # refix |
---|
[16789] | 38 | STUDENT_ID_PREFIX = u'D' |
---|
[16788] | 39 | |
---|
[17123] | 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 | |
---|
[16788] | 48 | def setPaymentDetails(self, category, student, |
---|
| 49 | previous_session=None, previous_level=None, combi=[]): |
---|
| 50 | """Create a payment ticket and set the payment data of a |
---|
| 51 | student for the payment category specified. |
---|
| 52 | """ |
---|
| 53 | p_item = u'' |
---|
| 54 | amount = 0.0 |
---|
| 55 | if previous_session: |
---|
| 56 | if previous_session < student['studycourse'].entry_session: |
---|
| 57 | return _('The previous session must not fall below ' |
---|
| 58 | 'your entry session.'), None |
---|
| 59 | if category == 'schoolfee': |
---|
| 60 | # School fee is always paid for the following session |
---|
| 61 | if previous_session > student['studycourse'].current_session: |
---|
| 62 | return _('This is not a previous session.'), None |
---|
| 63 | else: |
---|
| 64 | if previous_session > student['studycourse'].current_session - 1: |
---|
| 65 | return _('This is not a previous session.'), None |
---|
| 66 | p_session = previous_session |
---|
| 67 | p_level = previous_level |
---|
| 68 | p_current = False |
---|
| 69 | else: |
---|
| 70 | p_session = student['studycourse'].current_session |
---|
| 71 | p_level = student['studycourse'].current_level |
---|
| 72 | p_current = True |
---|
| 73 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 74 | if academic_session == None: |
---|
| 75 | return _(u'Session configuration object is not available.'), None |
---|
| 76 | # Determine fee. |
---|
| 77 | if category == 'schoolfee': |
---|
| 78 | try: |
---|
| 79 | certificate = student['studycourse'].certificate |
---|
| 80 | p_item = certificate.code |
---|
| 81 | except (AttributeError, TypeError): |
---|
| 82 | return _('Study course data are incomplete.'), None |
---|
| 83 | if previous_session: |
---|
| 84 | # Students can pay for previous sessions in all |
---|
| 85 | # workflow states. Fresh students are excluded by the |
---|
| 86 | # update method of the PreviousPaymentAddFormPage. |
---|
| 87 | if previous_level == 100: |
---|
| 88 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
| 89 | else: |
---|
| 90 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
| 91 | else: |
---|
[17123] | 92 | if student.is_jupeb: |
---|
| 93 | if not self._clearancePaymentMade(student): |
---|
| 94 | return _(u'Acceptance fee must be paid first.'), None |
---|
[16788] | 95 | if student.state == CLEARED: |
---|
| 96 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
| 97 | elif student.state == RETURNING: |
---|
| 98 | # In case of returning school fee payment the |
---|
| 99 | # payment session and level contain the values of |
---|
| 100 | # the session the student has paid for. Payment |
---|
| 101 | # session is always next session. |
---|
| 102 | p_session, p_level = self.getReturningData(student) |
---|
| 103 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 104 | if academic_session == None: |
---|
| 105 | return _( |
---|
| 106 | u'Session configuration object is not available.' |
---|
| 107 | ), None |
---|
| 108 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
| 109 | elif student.is_postgrad and student.state == PAID: |
---|
| 110 | # Returning postgraduate students also pay for the |
---|
| 111 | # next session but their level always remains the |
---|
| 112 | # same. |
---|
| 113 | p_session += 1 |
---|
| 114 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 115 | if academic_session == None: |
---|
| 116 | return _( |
---|
| 117 | u'Session configuration object is not available.' |
---|
| 118 | ), None |
---|
| 119 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
[17170] | 120 | if amount and not local(student) and not student.is_jupeb \ |
---|
| 121 | and student.faccode != 'PRE': |
---|
[16835] | 122 | amount += 40000 |
---|
[16788] | 123 | elif category == 'clearance': |
---|
| 124 | try: |
---|
| 125 | p_item = student['studycourse'].certificate.code |
---|
| 126 | except (AttributeError, TypeError): |
---|
| 127 | return _('Study course data are incomplete.'), None |
---|
| 128 | amount = academic_session.clearance_fee |
---|
[17173] | 129 | if local(student): |
---|
| 130 | amount *= 2.0 |
---|
| 131 | else: |
---|
| 132 | amount *= 2.4 |
---|
| 133 | if student.faccode == 'PRE': |
---|
| 134 | amount = 20000.0 |
---|
| 135 | if student.is_jupeb: |
---|
| 136 | amount = 25000.0 |
---|
[16788] | 137 | elif category == 'bed_allocation': |
---|
| 138 | p_item = self.getAccommodationDetails(student)['bt'] |
---|
[16882] | 139 | amount = 0.0 |
---|
[16788] | 140 | elif category == 'hostel_maintenance': |
---|
| 141 | amount = 0.0 |
---|
| 142 | bedticket = student['accommodation'].get( |
---|
| 143 | str(student.current_session), None) |
---|
| 144 | if bedticket is not None and bedticket.bed is not None: |
---|
| 145 | p_item = bedticket.bed_coordinates |
---|
| 146 | if bedticket.bed.__parent__.maint_fee > 0: |
---|
| 147 | amount = bedticket.bed.__parent__.maint_fee |
---|
| 148 | else: |
---|
| 149 | # fallback |
---|
| 150 | amount = academic_session.maint_fee |
---|
| 151 | else: |
---|
| 152 | return _(u'No bed allocated.'), None |
---|
| 153 | elif category == 'combi' and combi: |
---|
| 154 | categories = getUtility(IKofaUtils).COMBI_PAYMENT_CATEGORIES |
---|
| 155 | for cat in combi: |
---|
| 156 | fee_name = cat + '_fee' |
---|
| 157 | cat_amount = getattr(academic_session, fee_name, 0.0) |
---|
| 158 | if not cat_amount: |
---|
| 159 | return _('%s undefined.' % categories[cat]), None |
---|
| 160 | amount += cat_amount |
---|
| 161 | p_item += u'%s + ' % categories[cat] |
---|
| 162 | p_item = p_item.strip(' + ') |
---|
| 163 | else: |
---|
| 164 | fee_name = category + '_fee' |
---|
| 165 | amount = getattr(academic_session, fee_name, 0.0) |
---|
[16882] | 166 | if category != 'bed_allocation' and amount in (0.0, None): |
---|
[16788] | 167 | return _('Amount could not be determined.'), None |
---|
| 168 | if self.samePaymentMade(student, category, p_item, p_session): |
---|
| 169 | return _('This type of payment has already been made.'), None |
---|
| 170 | if self._isPaymentDisabled(p_session, category, student): |
---|
| 171 | return _('This category of payments has been disabled.'), 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 = amount |
---|
| 181 | payment.p_combi = combi |
---|
[16892] | 182 | return None, payment |
---|
| 183 | |
---|
| 184 | def getAccommodationDetails(self, student): |
---|
| 185 | """Determine the accommodation data of a student. |
---|
| 186 | """ |
---|
| 187 | d = {} |
---|
| 188 | d['error'] = u'' |
---|
| 189 | hostels = grok.getSite()['hostels'] |
---|
| 190 | d['booking_session'] = hostels.accommodation_session |
---|
| 191 | d['allowed_states'] = hostels.accommodation_states |
---|
| 192 | d['startdate'] = hostels.startdate |
---|
| 193 | d['enddate'] = hostels.enddate |
---|
| 194 | d['expired'] = hostels.expired |
---|
| 195 | # Determine bed type |
---|
| 196 | bt = 'all' |
---|
| 197 | if student.sex == 'f': |
---|
| 198 | sex = 'female' |
---|
| 199 | else: |
---|
| 200 | sex = 'male' |
---|
| 201 | special_handling = 'regular' |
---|
| 202 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
| 203 | return d |
---|