## $Id: utils.py 9459 2012-10-29 13:13:18Z 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 from time import time from zope.component import createObject, getUtility from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import Paragraph, Image, Table, Spacer from waeup.kofa.interfaces import IKofaUtils, CLEARED, RETURNING, PAID from kofacustom.nigeria.students.utils import NigeriaStudentsUtils from waeup.kofa.accesscodes import create_accesscode from waeup.uniben.interfaces import MessageFactory as _ from waeup.kofa.students.utils import (trans, render_student_data, render_table_data, get_signature_table) class CustomStudentsUtils(NigeriaStudentsUtils): """A collection of customized methods. """ 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 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 setPaymentDetails(self, category, student, previous_session, previous_level): """Create Payment object and set the payment data of a student for the payment category specified. """ details = {} p_item = u'' amount = 0.0 error = u'' if previous_session: 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 session = str(p_session) try: academic_session = grok.getSite()['configuration'][session] except KeyError: return _(u'Session configuration object is not available.'), None # Determine fee. if category == 'transfer': amount = academic_session.transfer_fee elif category == 'gown': amount = academic_session.gown_fee elif category == 'bed_allocation': amount = academic_session.booking_fee elif category == 'hostel_maintenance': amount = academic_session.maint_fee elif category == 'clearance': try: p_item = student['studycourse'].certificate.code except (AttributeError, TypeError): return _('Study course data are incomplete.'), None if p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS', 'MBBSMED', 'MBBSNDU'): amount = 65000.0 elif p_item in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET', 'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'): amount = 22500.0 else: amount = 45000.0 elif category == 'schoolfee': try: certificate = student['studycourse'].certificate p_item = certificate.code except (AttributeError, TypeError): return _('Study course data are incomplete.'), None if previous_session: if previous_session < student['studycourse'].entry_session: return _('The previous session must not fall below ' 'your entry session.'), None if previous_session > student['studycourse'].current_session - 1: return _('This is not a previous session.'), None if previous_session == student['studycourse'].entry_session: if student.is_foreigner: amount = getattr(certificate, 'school_fee_3', 0.0) else: amount = getattr(certificate, 'school_fee_1', 0.0) else: if student.is_foreigner: amount = getattr(certificate, 'school_fee_4', 0.0) else: amount = getattr(certificate, 'school_fee_2', 0.0) else: if student.state == CLEARED: if student.is_foreigner: amount = getattr(certificate, 'school_fee_3', 0.0) else: amount = getattr(certificate, 'school_fee_1', 0.0) 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) if student.is_foreigner: amount = getattr(certificate, 'school_fee_4', 0.0) else: amount = getattr(certificate, 'school_fee_2', 0.0) try: academic_session = grok.getSite()[ 'configuration'][str(p_session)] except KeyError: return _(u'Session configuration object is not available.'), None elif student.state == PAID and student.is_postgrad and \ not student.is_special_postgrad: # Regular returning postgraduate students also pay # for the next session but their level always remains the same. p_session += 1 if student.is_foreigner: amount = getattr(certificate, 'school_fee_4', 0.0) else: amount = getattr(certificate, 'school_fee_2', 0.0) try: academic_session = grok.getSite()[ 'configuration'][str(p_session)] except KeyError: return _(u'Session configuration object is not available.'), None # Give 50% school fee discount to staff members. if student.is_staff: amount /= 2 if amount in (0.0, None): return _('Amount could not be determined.' + ' Would you like to pay for a previous session?'), None # Add session specific penalty fee. if category == 'schoolfee' and student.is_postgrad: amount += academic_session.penalty_pg elif category == 'schoolfee': amount += academic_session.penalty_ug # Create ticket. 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 _('This type of payment has already been made.' + ' Would you like to pay for a previous session?'), 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 renderPDF(self, view, filename='slip.pdf', student=None, studentview=None, tableheader=None, tabledata=None, note=None, signatures=None): """Render pdf slips for various pages. """ # XXX: we have to fix the import problems here. from waeup.kofa.browser.interfaces import IPDFCreator from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE style = getSampleStyleSheet() creator = getUtility(IPDFCreator) data = [] doc_title = view.label author = '%s (%s)' % (view.request.principal.title, view.request.principal.id) footer_text = view.label if getattr(student, 'student_id', None) is not None: footer_text = "%s - %s - " % (student.student_id, footer_text) # Insert student data table portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE if student is not None: bd_translation = trans(_('Base Data'), portal_language) data.append(Paragraph(bd_translation, style["Heading3"])) data.append(render_student_data(studentview)) # Insert widgets if view.form_fields: data.append(Paragraph(view.title, style["Heading3"])) portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE separators = getattr(self, 'SEPARATORS_DICT', {}) table = creator.getWidgetsTable( view.form_fields, view.context, None, lang=portal_language, separators=separators) data.append(table) # Insert scanned docs #data.extend(docs_as_flowables(view, portal_language)) # Insert content table (optionally on second page) if tabledata and tableheader: #data.append(PageBreak()) data.append(Spacer(1, 20)) data.append(Paragraph(view.content_title, style["Heading3"])) contenttable = render_table_data(tableheader,tabledata) data.append(contenttable) # Insert signatures if signatures: data.append(Spacer(1, 20)) signaturetable = get_signature_table(signatures) data.append(signaturetable) # Insert history if filename.startswith('clearance') or filename.startswith('course'): data.append(Spacer(1, 20)) hist_translation = trans(_('Workflow History'), portal_language) data.append(Paragraph(hist_translation, style["Heading3"])) data.extend(creator.fromStringList(student.history.messages)) view.response.setHeader( 'Content-Type', 'application/pdf') try: pdf_stream = creator.create_pdf( data, None, doc_title, author=author, footer=footer_text, note=note) except IOError: view.flash('Error in image file.') return view.redirect(view.url(view.context)) return pdf_stream # Uniben prefix STUDENT_ID_PREFIX = u'B'