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 PayrVoucherHld extends ActionHandlerEJB implements PayrVoucherHldLocal,PayrVoucherHldRemote //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("PayrVoucherHldEJB is called");
		String  retString = null;
		try
		{
			retString = actionHold(tranID,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :PayrVoucherHldEJB :actionHandler:" + e.getMessage() + ":");
			throw new ITMException(e);
		}
	    return retString;
	}

	private String actionHold(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");
   			conn.setAutoCommit(false);
   			connDriver = null;
			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);
			}
			rs.close();
			rs = null;
			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);
				}
				rs.close();
				rs = null;
				if ((totalAmt - adjAmt) == 0)
				{
					errCode = "VTBAL2";  // already paid off
				}
				else if(adjAmt != 0 && adjAmt != totalAmt)
				{
					errCode = "VTBAL2"; // partly paid off
				}
				else if (hold.equals("N") || hold.equals(""))
				{
					updSql = "UPDATE MISC_PAYABLES SET HOLD_AMT = TOT_AMT "
							+"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();
					pstmt.close();
					pstmt = null;
					System.out.println("updateCnt  ::"+updateCnt);

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