## $Id: browser.py 7880 2012-03-14 15:39:49Z henrik $
##
## Copyright (C) 2012 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 zope.formlib.textwidgets import BytesDisplayWidget
from waeup.kofa.widgets.datewidget import (
    FriendlyDateWidget, FriendlyDateDisplayWidget
    )
from waeup.kofa.browser.layout import KofaPage
from waeup.kofa.students.interfaces import IStudentOnlinePayment
from waeup.kofa.students.browser import (
    StudentPersonalDisplayFormPage, StudentPersonalManageFormPage,
    StudentClearanceManageFormPage, StudentClearanceEditFormPage,
    StudentClearanceDisplayFormPage
    )
from waeup.kofa.students.viewlets import RequestCallbackActionButton
from waeup.custom.students.interfaces import (
    IStudent, IStudentPersonal, IStudentClearance, IStudentClearanceEdit
    )
from waeup.custom.interfaces import MessageFactory as _

class StudentPersonalDisplayFormPage(StudentPersonalDisplayFormPage):
    """ Page to display student personal data
    """
    grok.context(IStudent)
    form_fields = grok.AutoFields(IStudentPersonal)
    form_fields['perm_address'].custom_widget = BytesDisplayWidget

class StudentPersonalManageFormPage(StudentPersonalManageFormPage):
    """ Page to edit student clearance data
    """
    grok.context(IStudent)
    form_fields = grok.AutoFields(IStudentPersonal)

class StudentClearanceDisplayFormPage(StudentClearanceDisplayFormPage):
    """ Page to display student clearance data
    """
    grok.context(IStudent)
    form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked')
    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')

class StudentClearanceManageFormPage(StudentClearanceManageFormPage):
    """ Page to edit student clearance data
    """
    grok.context(IStudent)
    form_fields = grok.AutoFields(IStudentClearance)
    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')

class StudentClearanceEditFormPage(StudentClearanceEditFormPage):
    """ View to edit student clearance data by student
    """
    grok.context(IStudent)
    form_fields = grok.AutoFields(
        IStudentClearanceEdit).omit('clearance_locked')
    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')

class InterswitchActionButton(RequestCallbackActionButton):
    grok.order(2)
    icon = 'actionicon_pay.png'
    text = _('CollegePAY')
    target = 'goto_interswitch'

class RequestCallbackActionButton(RequestCallbackActionButton):
    grok.order(3)
    icon = 'actionicon_call.png'
    text = _('Request CollegePAY callback')
    target = 'callback'

class InterswitchPage(KofaPage):
    """ View which sends a POST request to the Interswitch
    CollegePAY payment gateway.
    """
    grok.context(IStudentOnlinePayment)
    grok.name('goto_interswitch')
    grok.template('goto_interswitch')
    grok.require('waeup.payStudent')

    label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
    submit_button = _('Submit')

    # Uniben product data (to be removed later)
    product_id = '57'
    currency = '566'
    pay_item_id = '5700'

    action = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
    site_redirect_url = 'http://localhost?echo='
    site_name = 'xyz.waeup.org'
    provider_acct = '2345'
    provider_bank_id = '8'
    provider_item_name = 'Kofa Provider Fee'
    institution_acct = '1234'
    institution_bank_id = '9'
    institution_name = 'Sample University'

    def update(self):
        if self.context.p_state != 'unpaid':
            self.flash(_("Payment ticket can't be re-send to CollegePAY."))
            self.redirect(self.url(self.context, '@@index'))
            return
        xmldict = {}
        self.student = self.context.getStudent()
        self.amount = (self.context.amount_auth + self.context.surcharge_1 +
            self.context.surcharge_2 + self.context.surcharge_3)
        self.amount_100 = 100 * self.amount
        self.local_date_time = str(self.context.creation_date)
        certificate = getattr(self.student['studycourse'],'certificate',None)
        if certificate is not None:
            xmldict['department'] = certificate.__parent__.__parent__.code
            xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
        else:
            xmldict['department'] = None
            xmldict['faculty'] = None
        xmldict['detail_ref'] = self.context.p_id
        xmldict['provider_amt'] = 100 * self.context.surcharge_1
        xmldict['provider_acct'] = self.provider_acct
        xmldict['provider_bank_id'] = self.provider_bank_id
        xmldict['provider_item_name'] = self.provider_item_name
        xmldict['institution_amt'] = 100 * self.context.amount_auth
        xmldict['institution_acct'] = self.institution_acct
        xmldict['institution_bank_id'] = self.institution_bank_id
        xmldict['institution_item_name'] = self.context.p_category
        xmldict['institution_name'] = self.institution_name
        # Interswitch amount is not part of the xml data
        xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
</item_details>
</payment_item_detail>""" % xmldict
        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
        return
