[7419] | 1 | ## $Id: utils.py 9513 2012-11-02 16:58:01Z 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 | ## |
---|
[7151] | 18 | import grok |
---|
[8598] | 19 | from time import time |
---|
[9459] | 20 | from zope.component import createObject, getUtility |
---|
| 21 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 22 | from reportlab.platypus import Paragraph, Image, Table, Spacer |
---|
[9513] | 23 | from waeup.kofa.interfaces import (IKofaUtils, |
---|
| 24 | CLEARED, RETURNING, PAID, REGISTERED, VALIDATED) |
---|
[8821] | 25 | from kofacustom.nigeria.students.utils import NigeriaStudentsUtils |
---|
[8247] | 26 | from waeup.kofa.accesscodes import create_accesscode |
---|
[8020] | 27 | from waeup.uniben.interfaces import MessageFactory as _ |
---|
[9459] | 28 | from waeup.kofa.students.utils import (trans, render_student_data, |
---|
| 29 | render_table_data, get_signature_table) |
---|
[6902] | 30 | |
---|
[8821] | 31 | class CustomStudentsUtils(NigeriaStudentsUtils): |
---|
[7151] | 32 | """A collection of customized methods. |
---|
| 33 | |
---|
| 34 | """ |
---|
| 35 | |
---|
[8270] | 36 | def getReturningData(self, student): |
---|
| 37 | """ This method defines what happens after school fee payment |
---|
[8319] | 38 | of returning students depending on the student's senate verdict. |
---|
[8270] | 39 | """ |
---|
[8319] | 40 | prev_level = student['studycourse'].current_level |
---|
| 41 | cur_verdict = student['studycourse'].current_verdict |
---|
| 42 | if cur_verdict in ('A','B','L','M','N','Z',): |
---|
| 43 | # Successful student |
---|
| 44 | new_level = divmod(int(prev_level),100)[0]*100 + 100 |
---|
| 45 | elif cur_verdict == 'C': |
---|
| 46 | # Student on probation |
---|
| 47 | new_level = int(prev_level) + 10 |
---|
| 48 | else: |
---|
| 49 | # Student is somehow in an undefined state. |
---|
| 50 | # Level has to be set manually. |
---|
| 51 | new_level = prev_level |
---|
[8270] | 52 | new_session = student['studycourse'].current_session + 1 |
---|
| 53 | return new_session, new_level |
---|
| 54 | |
---|
[9152] | 55 | def setPaymentDetails(self, category, student, |
---|
| 56 | previous_session, previous_level): |
---|
[8598] | 57 | """Create Payment object and set the payment data of a student for |
---|
| 58 | the payment category specified. |
---|
| 59 | |
---|
| 60 | """ |
---|
[8306] | 61 | details = {} |
---|
[8598] | 62 | p_item = u'' |
---|
| 63 | amount = 0.0 |
---|
| 64 | error = u'' |
---|
[9152] | 65 | if previous_session: |
---|
| 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 |
---|
[8598] | 73 | session = str(p_session) |
---|
[7151] | 74 | try: |
---|
| 75 | academic_session = grok.getSite()['configuration'][session] |
---|
| 76 | except KeyError: |
---|
[8598] | 77 | return _(u'Session configuration object is not available.'), None |
---|
[8676] | 78 | # Determine fee. |
---|
[7151] | 79 | if category == 'transfer': |
---|
[8598] | 80 | amount = academic_session.transfer_fee |
---|
[7151] | 81 | elif category == 'gown': |
---|
[8598] | 82 | amount = academic_session.gown_fee |
---|
[7151] | 83 | elif category == 'bed_allocation': |
---|
[8598] | 84 | amount = academic_session.booking_fee |
---|
[7151] | 85 | elif category == 'hostel_maintenance': |
---|
[8598] | 86 | amount = academic_session.maint_fee |
---|
[7151] | 87 | elif category == 'clearance': |
---|
[8598] | 88 | try: |
---|
| 89 | p_item = student['studycourse'].certificate.code |
---|
| 90 | except (AttributeError, TypeError): |
---|
| 91 | return _('Study course data are incomplete.'), None |
---|
[9346] | 92 | if p_item in ('BSCANA', 'BSCMBC', 'BMLS', 'BSCNUR', 'BSCPHS', 'BDS', |
---|
| 93 | 'MBBSMED', 'MBBSNDU'): |
---|
| 94 | amount = 65000.0 |
---|
| 95 | elif p_item in ('BEDCET', 'BIOEDCET', 'CHMEDCET', 'ISEDCET', |
---|
[8294] | 96 | 'MTHEDCET', 'PHYEDCET', 'ITECET', 'AGREDCET', 'HEEDCET'): |
---|
[9346] | 97 | amount = 22500.0 |
---|
[8294] | 98 | else: |
---|
[9346] | 99 | amount = 45000.0 |
---|
[7151] | 100 | elif category == 'schoolfee': |
---|
[8598] | 101 | try: |
---|
| 102 | certificate = student['studycourse'].certificate |
---|
| 103 | p_item = certificate.code |
---|
| 104 | except (AttributeError, TypeError): |
---|
| 105 | return _('Study course data are incomplete.'), None |
---|
[9152] | 106 | if previous_session: |
---|
[9206] | 107 | if previous_session < student['studycourse'].entry_session: |
---|
| 108 | return _('The previous session must not fall below ' |
---|
| 109 | 'your entry session.'), None |
---|
| 110 | if previous_session > student['studycourse'].current_session - 1: |
---|
| 111 | return _('This is not a previous session.'), None |
---|
[9157] | 112 | if previous_session == student['studycourse'].entry_session: |
---|
[9152] | 113 | if student.is_foreigner: |
---|
| 114 | amount = getattr(certificate, 'school_fee_3', 0.0) |
---|
| 115 | else: |
---|
| 116 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
[9006] | 117 | else: |
---|
[9152] | 118 | if student.is_foreigner: |
---|
| 119 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
| 120 | else: |
---|
| 121 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
| 122 | else: |
---|
| 123 | if student.state == CLEARED: |
---|
| 124 | if student.is_foreigner: |
---|
| 125 | amount = getattr(certificate, 'school_fee_3', 0.0) |
---|
| 126 | else: |
---|
| 127 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
[9513] | 128 | elif student.state in (PAID, REGISTERED, VALIDATED): |
---|
| 129 | # Students are either postgraduate or are paying in advance. |
---|
| 130 | # Both are paying for next session. |
---|
| 131 | p_session += 1 |
---|
| 132 | # We don't know which level the student is paying for. |
---|
| 133 | p_level = None |
---|
| 134 | try: |
---|
| 135 | academic_session = grok.getSite()[ |
---|
| 136 | 'configuration'][str(p_session)] |
---|
| 137 | except KeyError: |
---|
| 138 | return _(u'Session configuration object is not available.'), None |
---|
| 139 | if student.is_foreigner: |
---|
| 140 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
| 141 | else: |
---|
| 142 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
[9152] | 143 | elif student.state == RETURNING: |
---|
| 144 | # In case of returning school fee payment the payment session |
---|
| 145 | # and level contain the values of the session the student |
---|
| 146 | # has paid for. |
---|
| 147 | p_session, p_level = self.getReturningData(student) |
---|
| 148 | try: |
---|
| 149 | academic_session = grok.getSite()[ |
---|
| 150 | 'configuration'][str(p_session)] |
---|
| 151 | except KeyError: |
---|
| 152 | return _(u'Session configuration object is not available.'), None |
---|
| 153 | if student.is_foreigner: |
---|
| 154 | amount = getattr(certificate, 'school_fee_4', 0.0) |
---|
| 155 | else: |
---|
| 156 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
[9006] | 157 | # Give 50% school fee discount to staff members. |
---|
| 158 | if student.is_staff: |
---|
| 159 | amount /= 2 |
---|
[8598] | 160 | if amount in (0.0, None): |
---|
[9152] | 161 | return _('Amount could not be determined.' + |
---|
| 162 | ' Would you like to pay for a previous session?'), None |
---|
[8676] | 163 | # Add session specific penalty fee. |
---|
| 164 | if category == 'schoolfee' and student.is_postgrad: |
---|
| 165 | amount += academic_session.penalty_pg |
---|
| 166 | elif category == 'schoolfee': |
---|
| 167 | amount += academic_session.penalty_ug |
---|
| 168 | # Create ticket. |
---|
[8598] | 169 | for key in student['payments'].keys(): |
---|
| 170 | ticket = student['payments'][key] |
---|
| 171 | if ticket.p_state == 'paid' and\ |
---|
| 172 | ticket.p_category == category and \ |
---|
| 173 | ticket.p_item == p_item and \ |
---|
| 174 | ticket.p_session == p_session: |
---|
[9152] | 175 | return _('This type of payment has already been made.' + |
---|
| 176 | ' Would you like to pay for a previous session?'), None |
---|
[8715] | 177 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
[8950] | 178 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
[8598] | 179 | payment.p_id = "p%s" % timestamp |
---|
| 180 | payment.p_category = category |
---|
| 181 | payment.p_item = p_item |
---|
| 182 | payment.p_session = p_session |
---|
| 183 | payment.p_level = p_level |
---|
[9152] | 184 | payment.p_current = p_current |
---|
[8598] | 185 | payment.amount_auth = amount |
---|
| 186 | return None, payment |
---|
[7621] | 187 | |
---|
[9456] | 188 | def renderPDF(self, view, filename='slip.pdf', student=None, |
---|
| 189 | studentview=None, tableheader=None, tabledata=None, |
---|
| 190 | note=None, signatures=None): |
---|
| 191 | """Render pdf slips for various pages. |
---|
| 192 | """ |
---|
| 193 | # XXX: we have to fix the import problems here. |
---|
| 194 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
| 195 | from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE |
---|
| 196 | style = getSampleStyleSheet() |
---|
| 197 | creator = getUtility(IPDFCreator) |
---|
| 198 | data = [] |
---|
| 199 | doc_title = view.label |
---|
| 200 | author = '%s (%s)' % (view.request.principal.title, |
---|
| 201 | view.request.principal.id) |
---|
| 202 | footer_text = view.label |
---|
| 203 | if getattr(student, 'student_id', None) is not None: |
---|
| 204 | footer_text = "%s - %s - " % (student.student_id, footer_text) |
---|
| 205 | |
---|
| 206 | # Insert student data table |
---|
| 207 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 208 | if student is not None: |
---|
| 209 | bd_translation = trans(_('Base Data'), portal_language) |
---|
| 210 | data.append(Paragraph(bd_translation, style["Heading3"])) |
---|
| 211 | data.append(render_student_data(studentview)) |
---|
| 212 | |
---|
| 213 | # Insert widgets |
---|
| 214 | if view.form_fields: |
---|
| 215 | data.append(Paragraph(view.title, style["Heading3"])) |
---|
| 216 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 217 | separators = getattr(self, 'SEPARATORS_DICT', {}) |
---|
| 218 | table = creator.getWidgetsTable( |
---|
| 219 | view.form_fields, view.context, None, lang=portal_language, |
---|
| 220 | separators=separators) |
---|
| 221 | data.append(table) |
---|
| 222 | |
---|
| 223 | # Insert scanned docs |
---|
[9459] | 224 | #data.extend(docs_as_flowables(view, portal_language)) |
---|
[9456] | 225 | |
---|
| 226 | # Insert content table (optionally on second page) |
---|
| 227 | if tabledata and tableheader: |
---|
| 228 | #data.append(PageBreak()) |
---|
| 229 | data.append(Spacer(1, 20)) |
---|
| 230 | data.append(Paragraph(view.content_title, style["Heading3"])) |
---|
| 231 | contenttable = render_table_data(tableheader,tabledata) |
---|
| 232 | data.append(contenttable) |
---|
| 233 | |
---|
| 234 | # Insert signatures |
---|
| 235 | if signatures: |
---|
| 236 | data.append(Spacer(1, 20)) |
---|
| 237 | signaturetable = get_signature_table(signatures) |
---|
| 238 | data.append(signaturetable) |
---|
| 239 | |
---|
| 240 | # Insert history |
---|
| 241 | if filename.startswith('clearance') or filename.startswith('course'): |
---|
[9459] | 242 | data.append(Spacer(1, 20)) |
---|
[9456] | 243 | hist_translation = trans(_('Workflow History'), portal_language) |
---|
| 244 | data.append(Paragraph(hist_translation, style["Heading3"])) |
---|
| 245 | data.extend(creator.fromStringList(student.history.messages)) |
---|
| 246 | |
---|
| 247 | view.response.setHeader( |
---|
| 248 | 'Content-Type', 'application/pdf') |
---|
| 249 | try: |
---|
| 250 | pdf_stream = creator.create_pdf( |
---|
| 251 | data, None, doc_title, author=author, footer=footer_text, |
---|
| 252 | note=note) |
---|
| 253 | except IOError: |
---|
| 254 | view.flash('Error in image file.') |
---|
| 255 | return view.redirect(view.url(view.context)) |
---|
| 256 | return pdf_stream |
---|
| 257 | |
---|
[8441] | 258 | # Uniben prefix |
---|
[8413] | 259 | STUDENT_ID_PREFIX = u'B' |
---|