source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/students/browser.py @ 15844

Last change on this file since 15844 was 15844, checked in by Henrik Bettermann, 5 years ago

Render payment slip in states waived and scholarship.

  • Property svn:keywords set to Id
File size: 15.9 KB
Line 
1## $Id: browser.py 15844 2019-11-25 11:09:43Z henrik $
2##
3## Copyright (C) 2012 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
19from zope.formlib.textwidgets import BytesDisplayWidget
20from zope.component import getUtility
21from zope.security import checkPermission
22from zope.i18n import translate
23from datetime import datetime
24from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget
25from waeup.kofa.interfaces import IExtFileStore, IObjectHistory, IKofaUtils
26from waeup.kofa.browser.layout import action, UtilityView
27from waeup.kofa.utils.helpers import get_current_principal, to_timezone
28from waeup.kofa.students.browser import (
29    StudentPersonalDisplayFormPage, StudentPersonalManageFormPage,
30    StudentClearanceManageFormPage, StudentClearanceEditFormPage,
31    StudentClearanceDisplayFormPage, OnlinePaymentFakeApproveView,
32    ExportPDFClearanceSlip, StudentBaseManageFormPage,
33    StudentBaseDisplayFormPage,
34    StudentBasePDFFormPage,
35    StudentBaseEditFormPage, StudentPersonalEditFormPage,
36    OnlinePaymentDisplayFormPage, OnlinePaymentAddFormPage,
37    OnlinePaymentBreadcrumb, ExportPDFPaymentSlip,
38    ExportPDFCourseRegistrationSlip,
39    ExportPDFBedTicketSlip,
40    StudentFilesUploadPage, emit_lock_message,
41    AccommodationManageFormPage,
42    BedTicketAddPage)
43from waeup.kofa.students.interfaces import IStudentsUtils
44from kofacustom.nigeria.students.interfaces import (
45    INigeriaStudentBase, INigeriaStudent, INigeriaStudentPersonal,
46    INigeriaStudentPersonalEdit,
47    INigeriaUGStudentClearance,INigeriaPGStudentClearance,
48    INigeriaStudentOnlinePayment
49    )
50from waeup.kofa.students.workflow import ADMITTED
51from kofacustom.nigeria.interfaces import MessageFactory as _
52
53class NigeriaOnlinePaymentBreadcrumb(OnlinePaymentBreadcrumb):
54    """A breadcrumb for payments.
55    """
56    grok.context(INigeriaStudentOnlinePayment)
57
58class NigeriaStudentBaseDisplayFormPage(StudentBaseDisplayFormPage):
59    """ Page to display student base data
60    """
61    form_fields = grok.AutoFields(INigeriaStudentBase).omit(
62        'password', 'suspended', 'suspended_comment',
63        'flash_notice', 'provisionally_cleared')
64    form_fields[
65        'financial_clearance_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
66
67class NigeriaStudentBaseManageFormPage(StudentBaseManageFormPage):
68    """ View to manage student base data
69    """
70    form_fields = grok.AutoFields(INigeriaStudentBase).omit(
71        'student_id', 'adm_code', 'suspended',
72        'financially_cleared_by', 'financial_clearance_date')
73
74class NigeriaStudentBaseEditFormPage(StudentBaseEditFormPage):
75    """ View to edit student base data
76    """
77    form_fields = grok.AutoFields(INigeriaStudentBase).select(
78        'email', 'phone')
79
80class NigeriaStudentPersonalDisplayFormPage(StudentPersonalDisplayFormPage):
81    """ Page to display student personal data
82    """
83    form_fields = grok.AutoFields(INigeriaStudentPersonal)
84    form_fields['perm_address'].custom_widget = BytesDisplayWidget
85    form_fields['next_kin_address'].custom_widget = BytesDisplayWidget
86    form_fields[
87        'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')
88
89class NigeriaStudentPersonalEditFormPage(StudentPersonalEditFormPage):
90    """ Page to edit personal data
91    """
92    form_fields = grok.AutoFields(INigeriaStudentPersonalEdit).omit('personal_updated')
93
94class NigeriaStudentPersonalManageFormPage(StudentPersonalManageFormPage):
95    """ Page to edit personal data
96    """
97    form_fields = grok.AutoFields(INigeriaStudentPersonal)
98    form_fields['personal_updated'].for_display = True
99    form_fields[
100        'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')
101
102class NigeriaStudentClearanceDisplayFormPage(StudentClearanceDisplayFormPage):
103    """ Page to display student clearance data
104    """
105
106    @property
107    def form_fields(self):
108        if self.context.is_postgrad:
109            form_fields = grok.AutoFields(
110                INigeriaPGStudentClearance).omit('clearance_locked')
111        else:
112            form_fields = grok.AutoFields(
113                INigeriaUGStudentClearance).omit('clearance_locked')
114        if not getattr(self.context, 'officer_comment'):
115            form_fields = form_fields.omit('officer_comment')
116        else:
117            form_fields['officer_comment'].custom_widget = BytesDisplayWidget
118        return form_fields
119
120class NigeriaExportPDFClearanceSlip(ExportPDFClearanceSlip):
121    """Deliver a PDF slip of the context.
122    """
123    omit_fields = ('password', 'suspended', 'suspended_comment',
124        'phone', 'adm_code', 'email', 'date_of_birth', 'current_level',
125        'flash_notice')
126
127    @property
128    def form_fields(self):
129        if self.context.is_postgrad:
130            form_fields = grok.AutoFields(
131                INigeriaPGStudentClearance).omit('clearance_locked')
132        else:
133            form_fields = grok.AutoFields(
134                INigeriaUGStudentClearance).omit('clearance_locked')
135        if not getattr(self.context, 'officer_comment'):
136            form_fields = form_fields.omit('officer_comment')
137        return form_fields
138
139class NigeriaStudentClearanceManageFormPage(StudentClearanceManageFormPage):
140    """ Page to edit student clearance data
141    """
142
143    @property
144    def form_fields(self):
145        if self.context.is_postgrad:
146            form_fields = grok.AutoFields(
147                INigeriaPGStudentClearance).omit('clr_code')
148        else:
149            form_fields = grok.AutoFields(
150                INigeriaUGStudentClearance).omit('clr_code')
151        return form_fields
152
153class NigeriaStudentClearanceEditFormPage(StudentClearanceEditFormPage):
154    """ View to edit student clearance data by student
155    """
156
157    @property
158    def form_fields(self):
159        if self.context.is_postgrad:
160            form_fields = grok.AutoFields(INigeriaPGStudentClearance).omit(
161            'clearance_locked', 'nysc_location', 'clr_code', 'officer_comment',
162            'physical_clearance_date')
163        else:
164            form_fields = grok.AutoFields(INigeriaUGStudentClearance).omit(
165            'clearance_locked', 'clr_code', 'officer_comment',
166            'physical_clearance_date')
167        return form_fields
168
169class NigeriaExportPDFCourseRegistrationSlip(ExportPDFCourseRegistrationSlip):
170    """Deliver a PDF slip of the context.
171    """
172    omit_fields = ('password', 'suspended', 'suspended_comment',
173        'phone', 'adm_code', 'sex', 'email', 'date_of_birth', 'current_level',
174        'flash_notice')
175
176class NigeriaOnlinePaymentDisplayFormPage(OnlinePaymentDisplayFormPage):
177    """ Page to view an online payment ticket
178    """
179    grok.context(INigeriaStudentOnlinePayment)
180    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).omit(
181        'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item','p_combi')
182    form_fields[
183        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
184    form_fields[
185        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
186
187class NigeriaOnlinePaymentAddFormPage(OnlinePaymentAddFormPage):
188    """ Page to add an online payment ticket
189    """
190    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).select(
191        'p_combi')
192
193class NigeriaOnlinePaymentFakeApproveView(OnlinePaymentFakeApproveView):
194    """ Disable payment approval view for students.
195
196    This view is used for browser tests only and
197    has to be neutralized here!
198    """
199    grok.name('fake_approve')
200    grok.require('waeup.managePortal')
201
202    def update(self):
203        return
204
205class NigeriaExportPDFPaymentSlip(ExportPDFPaymentSlip):
206    """Deliver a PDF slip of the context.
207    """
208    grok.context(INigeriaStudentOnlinePayment)
209    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).omit(
210        'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item',
211        'p_split_data','p_combi')
212    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
213    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
214    omit_fields = ('password', 'suspended', 'suspended_comment', 'phone',
215        'adm_code', 'sex', 'email', 'date_of_birth', 'current_level',
216        'flash_notice')
217
218    def update(self):
219        if not self.context.p_state in ('waived', 'scholarship') \
220            and not self.context.r_company:
221            self.redirect(self.url(self.context))
222            return
223        return
224
225class NigeriaAccommodationManageFormPage(AccommodationManageFormPage):
226    """ Page to manage bed tickets.
227    This manage form page is for both students and students officers.
228    """
229    with_hostel_selection = False
230
231class NigeriaBedTicketAddPage(BedTicketAddPage):
232    """ Page to add a bed ticket
233    """
234    with_ac = True
235    with_bedselection = False
236
237class NigeriaExportPDFBedTicketSlip(ExportPDFBedTicketSlip):
238    """Deliver a PDF slip of the context.
239    """
240    omit_fields = ('password', 'suspended', 'suspended_comment',
241        'phone', 'adm_code', 'email', 'date_of_birth', 'current_level',
242        'flash_notice')
243
244class NigeriaStudentFilesUploadPage(StudentFilesUploadPage):
245    """ View to upload passport picture.
246
247    Students are not allowed to change the picture if they
248    passed the regular Kofa application.
249    """
250
251    def update(self):
252        # Passport pictures must not be editable if application slip
253        # exists.
254        slip = getUtility(IExtFileStore).getFileByContext(
255            self.context, 'application_slip')
256        PORTRAIT_CHANGE_STATES = getUtility(IStudentsUtils).PORTRAIT_CHANGE_STATES
257        if self.context.state not in PORTRAIT_CHANGE_STATES or slip is not None:
258            emit_lock_message(self)
259            return
260        super(StudentFilesUploadPage, self).update()
261        return
262
263class ClearStudentFinancially(UtilityView, grok.View):
264    """ Clear student financially by financial clearance officer
265    """
266    grok.context(INigeriaStudent)
267    grok.name('clear_financially')
268    grok.require('waeup.clearStudentFinancially')
269
270    def update(self):
271        if self.context.financially_cleared_by:
272            self.flash(_('This student has already been financially cleared.'),
273                       type="danger")
274            self.redirect(self.url(self.context))
275            return
276        user = get_current_principal()
277        if user is None:
278            usertitle = 'system'
279        else:
280            usertitle = getattr(user, 'public_name', None)
281            if not usertitle:
282                usertitle = user.title
283        self.context.financially_cleared_by = usertitle
284        self.context.financial_clearance_date = datetime.utcnow()
285        self.context.writeLogMessage(self,'financially cleared')
286        history = IObjectHistory(self.context)
287        history.addMessage('Financially cleared')
288        self.flash(_('Student has been financially cleared.'))
289        self.redirect(self.url(self.context))
290        return
291
292    def render(self):
293        return
294
295class WithdrawFinancialClearance(UtilityView, grok.View):
296    """ Withdraw financial clearance by financial clearance officer
297    """
298    grok.context(INigeriaStudent)
299    grok.name('withdraw_financial_clearance')
300    grok.require('waeup.clearStudentFinancially')
301
302    def update(self):
303        if not self.context.financially_cleared_by:
304            self.flash(_('This student has not yet been financially cleared.'),
305                       type="danger")
306            self.redirect(self.url(self.context))
307            return
308        self.context.financially_cleared_by = None
309        self.context.financial_clearance_date = None
310        self.context.writeLogMessage(self,'financial clearance withdrawn')
311        history = IObjectHistory(self.context)
312        history.addMessage('Financial clearance withdrawn')
313        self.flash(_('Financial clearance withdrawn.'))
314        self.redirect(self.url(self.context))
315        return
316
317    def render(self):
318        return
319
320cleared_note = """
321<br /><br /><br />
322<strong>Financially cleared on %s by %s.</strong>
323
324"""
325
326class NigeriaExportPDFFinancialClearancePage(UtilityView, grok.View):
327    """Deliver a PDF financial clearance slip.
328    """
329    grok.context(INigeriaStudent)
330    grok.name('fee_payment_history.pdf')
331    grok.require('waeup.viewStudent')
332    prefix = 'form'
333
334    omit_fields = (
335        'suspended', 'phone',
336        'adm_code', 'suspended_comment',
337        'date_of_birth', 'current_level',
338        'flash_notice')
339
340    form_fields = None
341
342    @property
343    def label(self):
344        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
345        return translate(_('Fee Payment History for'),
346            'waeup.kofa', target_language=portal_language) \
347            + ' %s' % self.context.display_fullname
348
349    def _sigsInFooter(self):
350        if not checkPermission('waeup.clearStudentFinancially', self.context):
351            return ()
352        return (_('Date, Checking Officer Signature'),
353                _('Date, Approving Officer Signature'),
354                )
355
356    @property
357    def note(self):
358        if self.context.financially_cleared_by:
359            tz = getUtility(IKofaUtils).tzinfo
360            try:
361                timestamp = to_timezone(
362                    self.context.financial_clearance_date, tz).strftime(
363                        "%Y-%m-%d %H:%M:%S")
364            except ValueError:
365                return
366            return cleared_note % (
367                timestamp, self.context.financially_cleared_by)
368        return
369
370    @property
371    def tabletitle(self):
372        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
373        tabletitle = []
374        tabletitle.append(translate(_('Successful Payments'), 'waeup.kofa',
375            target_language=portal_language))
376        return tabletitle
377
378    def render(self):
379        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
380        P_ID = translate(_('Payment Id'), 'waeup.kofa', target_language=portal_language)
381        #CD = translate(_('Creation Date'), 'waeup.kofa', target_language=portal_language)
382        PD = translate(_('Payment Date'), 'waeup.kofa', target_language=portal_language)
383        CAT = translate(_('Payment Category'), 'waeup.kofa', target_language=portal_language)
384        ITEM = translate(_('Payment Item'), 'waeup.kofa', target_language=portal_language)
385        AMT = translate(_('Amount (Naira)'), 'waeup.kofa', target_language=portal_language)
386        SSS = translate(_('Payment Session'), 'waeup.kofa', target_language=portal_language)
387        studentview = StudentBasePDFFormPage(self.context.student,
388            self.request, self.omit_fields)
389        students_utils = getUtility(IStudentsUtils)
390
391        tabledata = []
392        tableheader = []
393        tabledata.append(sorted(
394            [value for value in self.context['payments'].values()
395             if value.p_state in ('paid', 'waived', 'scholarship')],
396             key=lambda value: value.p_session))
397        tableheader.append([(P_ID,'p_id', 4.2),
398                         #(CD,'creation_date', 3),
399                         (PD,'formatted_p_date', 3),
400                         (CAT,'category', 3),
401                         (ITEM, 'p_item', 3),
402                         (AMT, 'amount_auth', 2),
403                         (SSS, 'p_session', 2),
404                         ])
405        return students_utils.renderPDF(
406            self, 'financial_clearance_slip.pdf',
407            self.context.student, studentview,
408            tableheader=tableheader,
409            tabledata=tabledata,
410            signatures=None,
411            sigs_in_footer=self._sigsInFooter(),
412            omit_fields=self.omit_fields,
413            note=self.note
414            )
Note: See TracBrowser for help on using the repository browser.