## $Id: payments.py 15471 2019-06-21 08:28:09Z 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
##
"""
Student payment components.
"""
import grok
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from zope.schema.interfaces import ConstraintNotSatisfied
from hurry.workflow.interfaces import IWorkflowInfo
from waeup.kofa.students.interfaces import IStudentNavigation
from waeup.kofa.students.workflow import CLEARED, RETURNING, PAID
from waeup.kofa.utils.helpers import attrs_to_fields
from waeup.kofa.accesscodes import create_accesscode
from kofacustom.nigeria.students.payments import (
    NigeriaStudentOnlinePayment, NigeriaStudentOnlinePaymentFactory)
from waeup.aaue.students.interfaces import ICustomStudentOnlinePayment

class CustomStudentOnlinePayment(NigeriaStudentOnlinePayment):
    """This is a custom online payment for students.

    Since this class inherits from StudentOnlinePayment, it automatically
    implements the interfaces of the parent class which means
    all viewlets and views meant for StudentOnlinePayment
    can also be accessed in the customized system.
    """
    grok.implements(ICustomStudentOnlinePayment, IStudentNavigation)
    grok.provides(ICustomStudentOnlinePayment)

    def __init__(self):
        super(CustomStudentOnlinePayment, self).__init__()
        return

    def redeemTicket(self):
        student = self.student
        if self.p_category in ('schoolfee', 'schoolfee_incl', 'schoolfee_1'):
            # Bypass activation code creation if next session
            # can be started directly.
            if student['studycourse'].next_session_allowed:
                try:
                    if student.state == CLEARED:
                        IWorkflowInfo(student).fireTransition(
                            'pay_first_school_fee')
                        return None
                    elif student.state == RETURNING:
                        IWorkflowInfo(student).fireTransition(
                            'pay_school_fee')
                        return None
                    elif student.state == PAID:
                        IWorkflowInfo(student).fireTransition(
                            'pay_pg_fee')
                        return None
                except ConstraintNotSatisfied:
                    pass
            # Create SFE access code
            pin, error = create_accesscode(
                'SFE',0,self.amount_auth,student.student_id)
            if error:
                return error
            self.ac = pin
        elif self.p_category == 'bed_allocation':
            # Create HOS access code
            pin, error = create_accesscode(
                'HOS',0,self.amount_auth,student.student_id)
            if error:
                return error
            self.ac = pin
        elif self.p_category.startswith('transcript'):
            # Create TSC access code
            pin, error = create_accesscode(
                'TSC',0,self.amount_auth,student.student_id)
            if error:
                return error
            self.ac = pin
        return None

CustomStudentOnlinePayment = attrs_to_fields(
    CustomStudentOnlinePayment, omit=['display_item'])

class CustomStudentOnlinePaymentFactory(NigeriaStudentOnlinePaymentFactory):
    """A factory for student online payments.
    """

    def __call__(self, *args, **kw):
        return CustomStudentOnlinePayment()

    def getInterfaces(self):
        return implementedBy(CustomStudentOnlinePayment)
