package ibase.webitm.ejb.adm.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 PayrVoucherRls extends ActionHandlerEJB implements PayrVoucherRlsLocal,PayrVoucherRlsRemote // 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("PayrVoucherRlsEJB is called");
		String  retString = null;
		try
		{
			retString = actionRelease(tranID,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :PayrVoucherRlsEJB :actionHandler:" + e.getMessage() + ":");
			throw new ITMException(e);
		}
	    return retString;
	}

	private String actionRelease(String tranId ,String xtraParams)throws ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;	
		ResultSet rs = null;
		String sql = "", updSql = "", errCode = "", errString = "", confirm = "", hold = "";
		double totalAmt = 0d, adjAmt = 0d, holdAmt = 0d;
		int updateCnt = 0;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		try
		{	
			conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			System.out.println("TranId ::"+tranId);
			sql = "SELECT CONFIRMED,HOLD FROM PAYR_VOUCHER WHERE TRAN_ID = '"+tranId+"'";
			System.out.println("sql ::"+sql);
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				confirm = rs.getString(1);
				System.out.println("confirm  ::"+confirm);
				hold = rs.getString(2);
				System.out.println("hold ::"+hold);
			}
			if (hold.equals(""))
			{
				errCode = "VTHLDNLL";
			}
			if (confirm.equals("Y"))
			{
				sql = "SELECT TOT_AMT, ADJ_AMT, HOLD_AMT FROM MISC_PAYABLES "
					 +"WHERE TRAN_SER = 'P-VOUC' AND REF_NO = '"+tranId+"'";
				System.out.println("sql ::"+sql);
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					totalAmt = rs.getDouble(1);
					System.out.println("totalAmt ::"+totalAmt);
					adjAmt = rs.getDouble(2);
					System.out.println("adjAmt ::"+adjAmt);
					holdAmt = rs.getDouble(3);
					System.out.println("holdAmt ::"+holdAmt);
				}
				if ((totalAmt - adjAmt) == 0)
				{
					errCode = "VTBAL2";  // already paid off
				}
				else if(adjAmt != 0 && adjAmt != totalAmt)
				{
					errCode = "VTBAL2"; // partly paid off
				}
				else if (hold.equals("Y"))
				{
					updSql = "UPDATE MISC_PAYABLES SET HOLD_AMT = 0 WHERE TRAN_SER = ? AND REF_NO = ?";
					System.out.println("updSql ::"+updSql);
					pstmt = conn.prepareStatement(updSql);
					pstmt.setString(1,"P-VOUC");
					pstmt.setString(2,tranId);
					updateCnt = pstmt.executeUpdate();
					System.out.println("updateCnt  ::"+updateCnt);

					updSql ="UPDATE PAYR_VOUCHER SET HOLD = 'N' WHERE TRAN_ID = ? ";
					System.out.println("updSql ::"+updSql);
					pstmt = conn.prepareStatement(updSql);
					pstmt.setString(1,tranId);
					updateCnt = pstmt.executeUpdate();
					System.out.println("updateCnt  ::"+updateCnt);
					if (updateCnt > 0)
					{
						errCode = "VTHOLD1";
					}
				}// end hold else
			}//end confirm if
			else
			{
				if (hold.equals("Y"))
				{
					updSql = "UPDATE PAYR_VOUCHER SET HOLD = 'N' WHERE TRAN_ID = ? ";
					System.out.println("updSql ::"+updSql);
					pstmt = conn.prepareStatement(updSql);
					pstmt.setString(1,tranId);
					updateCnt = pstmt.executeUpdate();
					System.out.println("updateCnt  ::"+updateCnt);
					if (updateCnt > 0)
					{
						errCode = "VTHOLD1";
					}
				}//end hold if
			}//end confirm else
			if (errCode != null  && errCode.trim().length() > 0)
			{
				System.out.println("PayrVoucherRlsEJB:errCode:"+errCode);
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);				
			}
		}// try end
		catch(SQLException sqx)
		{
			System.out.println("Exception :PayrVoucherRlsEJB :" + sqx.getMessage() + ":");
			throw new ITMException(sqx);
		}
		catch(Exception e)
		{
			System.out.println("Exception :PayrVoucherRlsEJB :" + e.getMessage() + ":");
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}catch(Exception e){}
		}
		return errString;
	}
}