/*
Window Name : w_payment_exp
Button Name : Paid
Action      : The voucher will be paid.It will update CONFIRMED field of PAYMENT_EXP Table.
*/
package ibase.webitm.ejb.fin.adv;

import java.rmi.RemoteException;
import java.sql.*;
import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class PaymentExpPaid extends ActionHandlerEJB implements PaymentExpPaidLocal,PaymentExpPaidRemote // SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}

	public String actionHandler(String tranID, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("Calling actionHandler from PaymentExpEJB");
		String varValue = "";
		Connection conn = null;
		String retString = "";
		ConnDriver connDriver = new ConnDriver();
		try
		{
			ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
			conn = connDriver.getConnectDB("DriverITM");
			varValue = itmDBAccessEJB.getEnvFin("999999","DIRECT_PAY_PAID",conn);
			if (varValue.equalsIgnoreCase("Y"))
			{
					retString = actionPaid(tranID,xtraParams);			
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :PaymentExp :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
	    return retString;
	}
	//Default Button
	private String actionPaid(String tranID, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn = null;
		String confirm = "";
		String sql = "";
		String sql1 = "";
		String errCode = "";
		ResultSet rs = null;
		Statement stmt = null;
		String retString = ""; 
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			sql = "SELECT CASE WHEN CONFIRMED IS NULL THEN 'N' ELSE CONFIRMED END FROM PAYMENT_EXP WHERE TRAN_ID = '"+ tranID +"'";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				confirm = rs.getString(1);
				System.out.println("confirm :"+confirm); 			
			}
			if (confirm.equalsIgnoreCase("N"))
			{
				sql = "UPDATE PAYMENT_EXP SET CONFIRMED = 'P', NET_AMT__PAID = NET_AMT WHERE TRAN_ID ='"+ tranID +"'";   
				System.out.println("Updating SQL :: "+sql);
				int resultUp = conn.createStatement().executeUpdate(sql);
				if (resultUp  > 0 )
				{
					conn.commit();
					errCode = "VTPAYMRK";
				}				
			}
			else if (confirm.equalsIgnoreCase("P"))
			{
			   errCode = "VTPAID2";
			}
			else
			{
			   errCode = "VTMCONF1";
			}
			if (errCode != null  && errCode.trim().length() > 0)
			{
				retString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);
			}
			/*else
			{
				retString ="";
			} */
		}
		catch(Exception e)
		{
			System.out.println("Exception :: PaymentExpEJB ::"+e.getMessage());
			e.printStackTrace();
			errCode = e.getMessage();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection......");
				conn.close();
			}
			catch(Exception e){}
		}		
		return retString;
	} 
}