## $Id: utils.py 17457 2023-06-26 21:30:00Z henrik $ ## ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## import grok import os import csv from time import time from reportlab.platypus import Paragraph, Table from zope.component import createObject, getUtility from reportlab.lib.styles import getSampleStyleSheet from waeup.kofa.browser.pdf import ENTRY1_STYLE from waeup.kofa.interfaces import (IKofaUtils, ADMITTED, CLEARANCE, CLEARED, REQUESTED, RETURNING, PAID, REGISTERED, VALIDATED, GRADUATED) from waeup.kofa.utils.helpers import to_timezone from waeup.kofa.students.utils import ( trans, render_student_data, formatted_text, render_transcript_data, SLIP_STYLE) from kofacustom.nigeria.students.utils import NigeriaStudentsUtils from waeup.uniben.interfaces import MessageFactory as _ SCHOOLFEES = dict() schoolfees_path = os.path.join( os.path.dirname(__file__), 'schoolfees_22.csv') reader = csv.DictReader(open(schoolfees_path, 'rb')) SCHOOLFEES[22] = {line['code']: {item[0]:item[1] for item in line.items()} for line in reader} schoolfees_path = os.path.join( os.path.dirname(__file__), 'schoolfees_20.csv') reader = csv.DictReader(open(schoolfees_path, 'rb')) SCHOOLFEES[20] = {line['code']: {item[0]:item[1] for item in line.items()} for line in reader} schoolfees_path = os.path.join( os.path.dirname(__file__), 'schoolfees_17.csv') reader = csv.DictReader(open(schoolfees_path, 'rb')) SCHOOLFEES[17] = {line['code']: {item[0]:item[1] for item in line.items()} for line in reader} schoolfees_path = os.path.join( os.path.dirname(__file__), 'schoolfees_12.csv') reader = csv.DictReader(open(schoolfees_path, 'rb')) SCHOOLFEES[12] = {line['code']: {item[0]:item[1] for item in line.items()} for line in reader} class CustomStudentsUtils(NigeriaStudentsUtils): """A collection of customized methods. """ SEPARATORS_DICT = { 'form.fst_sit_fname': _(u'First Sitting Record'), 'form.scd_sit_fname': _(u'Second Sitting Record'), 'form.alr_fname': _(u'Advanced Level Record'), 'form.hq_type': _(u'Higher Education Record'), 'form.hq2_type': _(u'Second Higher Education Record'), 'form.nysc_year': _(u'NYSC Information'), 'form.employer': _(u'Employment History'), 'form.former_matric': _(u'Former Student'), 'form.fever': _(u'History of Symptoms'), 'form.asthma': _(u'Medical History'), 'form.lagos_abuja': _(u'Travel History'), 'form.company_suspected': _(u'History of Contact/Infection'), 'form.genotype': _(u'TISHIP Registration Form Data'), } def getReturningData(self, student): """ This method defines what happens after school fee payment of returning students depending on the student's senate verdict. """ prev_level = student['studycourse'].current_level cur_verdict = student['studycourse'].current_verdict if cur_verdict == 'N' and prev_level in (100, 200): new_level = prev_level elif cur_verdict in ('A','B','L','M','N','Z',): # Successful student new_level = divmod(int(prev_level),100)[0]*100 + 100 elif cur_verdict == 'C': # Student on probation new_level = int(prev_level) + 10 else: # Student is somehow in an undefined state. # Level has to be set manually. new_level = prev_level new_session = student['studycourse'].current_session + 1 return new_session, new_level def checkAccommodationRequirements(self, student, acc_details): if acc_details.get('expired', False): startdate = acc_details.get('startdate') enddate = acc_details.get('enddate') if startdate and enddate: tz = getUtility(IKofaUtils).tzinfo startdate = to_timezone( startdate, tz).strftime("%d/%m/%Y %H:%M:%S") enddate = to_timezone( enddate, tz).strftime("%d/%m/%Y %H:%M:%S") return _("Outside booking period: ${a} - ${b}", mapping = {'a': startdate, 'b': enddate}) else: return _("Outside booking period.") if not student.is_postgrad and student.current_mode != 'ug_ft': return _("Only undergraduate full-time students are eligible to book accommodation.") bt = acc_details.get('bt') if not bt: return _("Your data are incomplete.") if not student.state in acc_details['allowed_states']: return _("You are in the wrong registration state.") #if student['studycourse'].current_session != acc_details[ # 'booking_session']: # return _('Your current session does not ' # 'match accommodation session.') if acc_details['booking_session'] - student[ 'studycourse'].current_session > self.ACCOMMODATION_SPAN: return _('Your current session does not allow ' + \ 'to book accommodation.') stage = bt.split('_')[2] if not student.is_postgrad and stage != 'fr' and not student[ 'studycourse'].previous_verdict in ( 'A', 'B', 'F', 'J', 'L', 'M', 'C', 'Z'): return _("Your are not eligible to book accommodation.") bsession = str(acc_details['booking_session']) if bsession in student['accommodation'].keys() \ and not 'booking expired' in \ student['accommodation'][bsession].bed_coordinates: return _('You already booked a bed space in ' 'current accommodation session.') return def getAccommodationDetails(self, student): """Determine the accommodation data of a student. """ d = {} d['error'] = u'' hostels = grok.getSite()['hostels'] d['booking_session'] = hostels.accommodation_session d['allowed_states'] = hostels.accommodation_states d['startdate'] = hostels.startdate d['enddate'] = hostels.enddate d['expired'] = hostels.expired # Determine bed type studycourse = student['studycourse'] certificate = getattr(studycourse,'certificate',None) entry_session = studycourse.entry_session current_level = studycourse.current_level if None in (entry_session, current_level, certificate): return d if student.sex == 'f': sex = 'female' else: sex = 'male' if student.is_postgrad: bt = 'all' special_handling = 'pg' else: end_level = certificate.end_level if current_level == 10: bt = 'pr' elif entry_session == grok.getSite()['hostels'].accommodation_session: bt = 'fr' elif current_level >= end_level: bt = 'fi' else: bt = 're' special_handling = 'regular' desired_hostel = student['accommodation'].desired_hostel if student.faccode in ('MED', 'DEN') and ( not desired_hostel or desired_hostel.startswith('clinical')): special_handling = 'clinical' elif student.certcode in ('BARTMAS', 'BARTTHR', 'BARTFAA', 'BAEDFAA', 'BSCEDECHED', 'BAFAA', 'BARTMUS', 'BSCEDECHED'): special_handling = 'ekenwan' d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) return d def _paymentMade(self, student, session): if len(student['payments']): for ticket in student['payments'].values(): if ticket.p_state == 'paid' and \ ticket.p_category == 'schoolfee' and \ ticket.p_session == session: return True return False def _isPaymentDisabled(self, p_session, category, student): academic_session = self._getSessionConfiguration(p_session) if category == 'schoolfee': if 'sf_all' in academic_session.payment_disabled: return True if student.state == RETURNING and \ 'sf_return' in academic_session.payment_disabled: return True if student.current_mode == 'found' and \ 'sf_found' in academic_session.payment_disabled: return True if student.is_postgrad: if 'sf_pg' in academic_session.payment_disabled: return True return False if student.current_mode.endswith('ft') and \ 'sf_ft' in academic_session.payment_disabled: return True if student.current_mode.endswith('pt') and \ 'sf_pt' in academic_session.payment_disabled: return True if student.current_mode.startswith('dp') and \ 'sf_dp' in academic_session.payment_disabled: return True if student.current_mode.endswith('sw') and \ 'sf_sw' in academic_session.payment_disabled: return True if category == 'hostel_maintenance' and \ 'maint_all' in academic_session.payment_disabled: return True if category == 'clearance': if 'cl_all' in academic_session.payment_disabled: return True if student.is_jupeb and \ 'cl_jupeb' in academic_session.payment_disabled: return True if not student.is_jupeb and \ 'cl_allexj' in academic_session.payment_disabled: return True return False #def _hostelApplicationPaymentMade(self, student, session): # if len(student['payments']): # for ticket in student['payments'].values(): # if ticket.p_state == 'paid' and \ # ticket.p_category == 'hostel_application' and \ # ticket.p_session == session: # return True # return False def _pharmdInstallments(self, student): installments = 0.0 if len(student['payments']): for ticket in student['payments'].values(): if ticket.p_state == 'paid' and \ ticket.p_category.startswith('pharmd') and \ ticket.p_session == student.current_session: installments += ticket.amount_auth return installments def samePaymentMade(self, student, category, p_item, p_session): if category in ('bed_allocation', 'transcript'): return False for key in student['payments'].keys(): ticket = student['payments'][key] if ticket.p_state == 'paid' and\ ticket.p_category == category and \ ticket.p_item == p_item and \ ticket.p_session == p_session: return True return False def setPaymentDetails(self, category, student, previous_session, previous_level, combi): """Create Payment object and set the payment data of a student for the payment category specified. """ p_item = u'' amount = 0.0 if previous_session: if previous_session < student['studycourse'].entry_session: return _('The previous session must not fall below ' 'your entry session.'), None if category == 'schoolfee': # School fee is always paid for the following session if previous_session > student['studycourse'].current_session: return _('This is not a previous session.'), None else: if previous_session > student['studycourse'].current_session - 1: return _('This is not a previous session.'), None p_session = previous_session p_level = previous_level p_current = False else: p_session = student['studycourse'].current_session p_level = student['studycourse'].current_level p_current = True academic_session = self._getSessionConfiguration(p_session) if academic_session == None: return _(u'Session configuration object is not available.'), None # Determine fee. if category.startswith('pharmd') \ and student.current_mode == 'special_ft': amount = 80000.0 elif category == 'plag_test': amount = 2500.0 if student.is_postgrad: amount = 5000.0 #elif category == 'develop' and student.is_postgrad: # amount = academic_session.development_fee elif category == 'bed_allocation': acco_details = self.getAccommodationDetails(student) p_session = acco_details['booking_session'] p_item = acco_details['bt'] desired_hostel = student['accommodation'].desired_hostel if not desired_hostel: return _(u'Select your favoured hostel first.'), None if desired_hostel and desired_hostel != 'no': p_item = u'%s (%s)' % (p_item, desired_hostel) amount = academic_session.booking_fee if student.is_postgrad: amount += 500 elif category == 'hostel_maintenance': amount = 0.0 booking_session = grok.getSite()['hostels'].accommodation_session bedticket = student['accommodation'].get(str(booking_session), None) if bedticket is not None and bedticket.bed is not None: p_item = bedticket.bed_coordinates p_session = booking_session if bedticket.bed.__parent__.maint_fee > 0: amount = bedticket.bed.__parent__.maint_fee else: # fallback amount = academic_session.maint_fee else: return _(u'No bed allocated.'), None #elif category == 'hostel_application': # amount = 1000.0 #elif category.startswith('tempmaint'): # if not self._hostelApplicationPaymentMade( # student, student.current_session): # return _( # 'You have not yet paid the hostel application fee.'), None # if category == 'tempmaint_1': # amount = 8150.0 # elif category == 'tempmaint_2': # amount = 12650.0 # elif category == 'tempmaint_3': # amount = 9650.0 elif category == 'clearance': p_item = student.certcode if p_item is None: return _('Study course data are incomplete.'), None if student.is_jupeb: amount = 50000.0 elif student.faccode.startswith('FCETA'): # ASABA and AKOKA amount = 35000.0 elif student.depcode == 'DMIC': amount = 70000.0 elif student.faccode in ('BMS', 'MED', 'DEN') \ and not student.is_postgrad: amount = 80000.0 elif student.faccode == 'DCOEM': return _('Acceptance fee payment not necessary.'), None else: amount = 60000.0 elif category == 'schoolfee': try: certificate = student['studycourse'].certificate p_item = certificate.code except (AttributeError, TypeError): return _('Study course data are incomplete.'), None try: if student.entry_session < 2017: schoolfees_dict = SCHOOLFEES[12][p_item] elif student.entry_session < 2020: schoolfees_dict = SCHOOLFEES[17][p_item] elif student.entry_session < 2022: schoolfees_dict = SCHOOLFEES[20][p_item] else: schoolfees_dict = SCHOOLFEES[22][p_item] except KeyError: return _('School fee not yet fixed: p_item = %s' % p_item), None if previous_session: # Students can pay for previous sessions in all workflow states. # Fresh students are excluded by the update method of the # PreviousPaymentAddFormPage. if previous_session == student['studycourse'].entry_session: if student.is_foreigner: amount = schoolfees_dict['initial_foreigner'] else: amount = schoolfees_dict['initial'] else: if student.is_foreigner: amount = schoolfees_dict['returning_foreigner'] else: amount = schoolfees_dict['returning'] else: if student.state == CLEARED: if student.is_foreigner: amount = schoolfees_dict['initial_foreigner'] else: amount = schoolfees_dict['initial'] elif student.state == PAID and student.is_postgrad: p_session += 1 academic_session = self._getSessionConfiguration(p_session) if academic_session == None: return _(u'Session configuration object is not available.'), None if student.is_foreigner: amount = schoolfees_dict['returning_foreigner'] else: amount = schoolfees_dict['returning'] elif student.state == RETURNING: # In case of returning school fee payment the payment session # and level contain the values of the session the student # has paid for. p_session, p_level = self.getReturningData(student) academic_session = self._getSessionConfiguration(p_session) if academic_session == None: return _(u'Session configuration object is not available.'), None # Students are only allowed to pay for the next session # if current session payment has really been made, # i.e. payment object exists and is paid. #if not self._paymentMade( # student, student.current_session): # return _('You have not yet paid your current/active' + # ' session. Please use the previous session' + # ' payment form first.'), None if student.is_foreigner: amount = schoolfees_dict['returning_foreigner'] else: amount = schoolfees_dict['returning'] # PHARMD school fee amount is fixed and previously paid # installments in current session are deducted. if student.current_mode == 'special_ft' \ and student.state in (RETURNING, CLEARED): if student.is_foreigner: amount = 260000.0 - self._pharmdInstallments(student) else: amount = 160000.0 - self._pharmdInstallments(student) try: amount = float(amount) except ValueError: return _(u'School fee not yet fixed: p_item = %s' % p_item), None # Give 50% school fee discount to staff members. if student.is_staff: amount /= 2 else: fee_name = category + '_fee' amount = getattr(academic_session, fee_name, 0.0) if amount in (0.0, None): return _('Amount could not be determined.'), None # Add session specific penalty fee. if category == 'schoolfee' and student.is_postgrad: amount += academic_session.penalty_pg amount += academic_session.development_fee elif category == 'schoolfee' and student.current_mode == ('ug_ft'): amount += academic_session.penalty_ug_ft elif category == 'schoolfee' and student.current_mode == ('ug_pt'): amount += academic_session.penalty_ug_pt elif category == 'schoolfee' and student.current_mode == ('ug_sw'): amount += academic_session.penalty_sw elif category == 'schoolfee' and student.current_mode in ( 'dp_ft', 'dp_pt'): amount += academic_session.penalty_dp if category.startswith('tempmaint'): p_item = getUtility(IKofaUtils).PAYMENT_CATEGORIES[category] p_item = unicode(p_item) # Now we change the category because tempmaint payments # will be obsolete when Uniben returns to Kofa bed allocation. category = 'hostel_maintenance' # Create ticket. if self.samePaymentMade(student, category, p_item, p_session): return _('This type of payment has already been made.'), None if self._isPaymentDisabled(p_session, category, student): return _('This category of payments has been disabled.'), None payment = createObject(u'waeup.StudentOnlinePayment') timestamp = ("%d" % int(time()*10000))[1:] payment.p_id = "p%s" % timestamp payment.p_category = category payment.p_item = p_item payment.p_session = p_session payment.p_level = p_level payment.p_current = p_current payment.amount_auth = amount return None, payment def warnCreditsOOR(self, studylevel, course=None): studycourse = studylevel.__parent__ certificate = getattr(studycourse,'certificate', None) current_level = studycourse.current_level if None in (current_level, certificate): return if studylevel.__parent__.previous_verdict == 'R': return end_level = certificate.end_level if studylevel.student.faccode in ( 'MED', 'DEN', 'BMS') and studylevel.level == 200: limit = 61 elif current_level >= end_level: limit = 51 else: limit = 50 if course and studylevel.total_credits + course.credits > limit: return _('Maximum credits exceeded.') elif studylevel.total_credits > limit: return _('Maximum credits exceeded.') return def warnCourseAlreadyPassed(self, studylevel, course): """Return message if course has already been passed at previous levels. """ # med: we may need to put this matter on hold and allow # all the students to register. return False previous_verdict = studylevel.__parent__.previous_verdict if previous_verdict in ('C', 'M'): return False for slevel in studylevel.__parent__.values(): for cticket in slevel.values(): if cticket.code == course.code \ and cticket.total_score >= cticket.passmark: return _('Course has already been passed at previous level.') return False def clearance_disabled_message(self, student): if student.is_postgrad: return None try: session_config = grok.getSite()[ 'configuration'][str(student.current_session)] except KeyError: return _('Session configuration object is not available.') if not session_config.clearance_enabled: return _('Clearance is disabled for this session.') return None def renderPDFTranscript(self, view, filename='transcript.pdf', student=None, studentview=None, note=None, signatures=(), sigs_in_footer=(), digital_sigs=(), show_scans=True, topMargin=1.5, omit_fields=(), tableheader=None, no_passport=False, save_file=False): """Render pdf slip of a transcripts. """ portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE # XXX: tell what the different parameters mean style = getSampleStyleSheet() creator = self.getPDFCreator(student) data = [] doc_title = view.label author = '%s (%s)' % (view.request.principal.title, view.request.principal.id) footer_text = view.label.split('\n') if len(footer_text) > 2: # We can add a department in first line footer_text = footer_text[1] else: # Only the first line is used for the footer footer_text = footer_text[0] if getattr(student, 'student_id', None) is not None: footer_text = "%s - %s - " % (student.student_id, footer_text) # Insert student data table if student is not None: #bd_translation = trans(_('Base Data'), portal_language) #data.append(Paragraph(bd_translation, HEADING_STYLE)) data.append(render_student_data( studentview, view.context, omit_fields, lang=portal_language, slipname=filename, no_passport=no_passport)) transcript_data = view.context.getTranscriptData() levels_data = transcript_data[0] contextdata = [] f_label = trans(_('Course of Study:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text(view.context.certificate.longtitle) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Faculty:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text( view.context.certificate.__parent__.__parent__.__parent__.longtitle) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Department:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text( view.context.certificate.__parent__.__parent__.longtitle) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Entry Session:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text( view.session_dict.get(view.context.entry_session)) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Final Session:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text( view.session_dict.get(view.context.current_session)) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Entry Mode:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text(view.studymode_dict.get( view.context.entry_mode)) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Final Verdict:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) f_text = formatted_text(view.studymode_dict.get( view.context.current_verdict)) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) f_label = trans(_('Cumulative GPA:'), portal_language) f_label = Paragraph(f_label, ENTRY1_STYLE) format_float = getUtility(IKofaUtils).format_float cgpa = format_float(transcript_data[1], 3) if student.state == GRADUATED: f_text = formatted_text('%s (%s)' % ( cgpa, self.getClassFromCGPA(transcript_data[1], student)[1])) else: f_text = formatted_text('%s' % cgpa) f_text = Paragraph(f_text, ENTRY1_STYLE) contextdata.append([f_label,f_text]) contexttable = Table(contextdata,style=SLIP_STYLE) data.append(contexttable) transcripttables = render_transcript_data( view, tableheader, levels_data, lang=portal_language) data.extend(transcripttables) # Insert signatures # XXX: We are using only sigs_in_footer in waeup.kofa, so we # do not have a test for the following lines. if signatures and not sigs_in_footer: data.append(Spacer(1, 20)) # Render one signature table per signature to # get date and signature in line. for signature in signatures: signaturetables = get_signature_tables(signature) data.append(signaturetables[0]) # Insert digital signatures if digital_sigs: data.append(Spacer(1, 20)) sigs = digital_sigs.split('\n') for sig in sigs: data.append(Paragraph(sig, NOTE_STYLE)) view.response.setHeader( 'Content-Type', 'application/pdf') try: pdf_stream = creator.create_pdf( data, None, doc_title, author=author, footer=footer_text, note=note, sigs_in_footer=sigs_in_footer, topMargin=topMargin, view=view) except IOError: view.flash(_('Error in image file.')) return view.redirect(view.url(view.context)) if save_file: self._saveTranscriptPDF(student, pdf_stream) return return pdf_stream #: A tuple containing the names of registration states in which changing of #: passport pictures is allowed. PORTRAIT_CHANGE_STATES = (CLEARANCE, REQUESTED) #: A tuple containing the names of registration states in which changing of #: scanned signatures is allowed. SIGNATURE_CHANGE_STATES = (CLEARED, RETURNING, PAID, REGISTERED, VALIDATED, ) # Uniben prefix @property def STUDENT_ID_PREFIX(self): if grok.getSite().__name__ == 'uniben-cdl': return u'C' return u'B' STUDENT_EXPORTER_NAMES = ( 'students', 'studentstudycourses', 'studentstudycourses_1', 'studentstudylevels', #'studentstudylevels_1', 'coursetickets', #'coursetickets_1', 'studentpayments', 'bedtickets', 'trimmed', 'outstandingcourses', 'unpaidpayments', 'sfpaymentsoverview', 'sessionpaymentsoverview', 'studylevelsoverview', 'combocard', 'bursary', 'accommodationpayments', 'transcriptdata', 'trimmedpayments', 'medicalhistory', 'nysc', )