 /* 
	Window Name : w_misc_voucher
    Button Name : Hold
	Action      : Put Voucher on hold i.e it will update MISC_PAYABLES 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 MiscVoucherHld extends ActionHandlerEJB implements MiscVoucherHldLocal,MiscVoucherHldRemote //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("MiscVoucherHldEJB:actionHandler called");
		String  retString = null;
		try
		{
			System.out.println("Transaction ID To Hold :: "+tranID);
			retString = actionHold(tranID,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :MiscVoucherHldEJB :actionHandler:" + e.getMessage() + ":");
			throw new ITMException(e);
		}
		System.out.println("returning from MiscVoucherHldEJB actionHandler"+retString);
		return retString;
	}

	private String actionHold(String tranID,  String xtraParams)throws ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String errCode="";
		String errString="";
		String sql = "";
		String updSql = "";
		String confirmed = null;
		int rows=0;
		float totAmt=0;
		float adjAmt=0;
		float holdAmt=0;
		float balAmt=0;
		ConnDriver connDriver = new ConnDriver();
		try
		{	
			conn = connDriver.getConnectDB("DriverITM");
			ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
			stmt = conn.createStatement();
			sql="SELECT CASE WHEN CONFIRMED IS NULL THEN 'N' ELSE CONFIRMED END CONF FROM MISC_VOUCHER WHERE TRAN_ID = '"+tranID+"'";
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				confirmed = rs.getString("CONF");
			}
			if (confirmed.equalsIgnoreCase("Y"))
			{								   
				sql="SELECT TOT_AMT, ADJ_AMT,CASE WHEN HOLD_AMT IS NULL THEN 0 ELSE HOLD_AMT END HOLD_AMT FROM MISC_PAYABLES WHERE TRAN_SER = 'M-VOUC' AND REF_NO = '"+tranID+"'";
				rs = stmt.executeQuery(sql);
				if(rs.next())
				{
					totAmt=rs.getFloat("TOT_AMT");
					adjAmt=rs.getFloat("ADJ_AMT");
					holdAmt=rs.getFloat("HOLD_AMT");
				}
				balAmt = totAmt-adjAmt;
				if(balAmt == 0)
				{
					errCode = "VTBAL2";  // already paid off
				}
				else if(adjAmt != 0 && adjAmt != totAmt)
				{
					errCode = "VTBAL2"; // partly paid off
				}
				else if (holdAmt == 0) 
				{
					//Put Voucher on hold
					updSql = "UPDATE MISC_PAYABLES SET HOLD_AMT = TOT_AMT WHERE TRAN_SER = 'M-VOUC' AND REF_NO = '"+tranID+"'";
					System.out.println("Updating SQL :: "+updSql);
					rows=stmt.executeUpdate(updSql);					
				}
				else
				{
					errCode="VTHOLD3";
				}
			}//end if confirmed "Y"
			else
			{
				errCode="UNCONTRAN";	//if Transaction Not confirmed
			}
			if(errCode.trim().length() == 0 && rows > 0)
			{
				errCode = "VTHOLD2";
				conn.commit();
				System.out.println("\n <==== Transaction Updated Successfully ====>");

			} 
			if (errCode != null  && errCode.trim().length() > 0)
			{
				System.out.println("MiscVoucherHldEJB:errCode:"+errCode);
				//errString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);//Gulzar 30-12-06		
				errString = itmDBAccessEJB.getErrorString("", errCode, "");//Gulzar 30-12-06					
			}
			
		}
		catch(Exception e)
		{
			System.out.println("Exception :MiscVoucherHldEJB :" + e.getMessage() + ":");
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Connection Closing......");
				conn.close();
				conn = null;
			}
			catch(Exception e){}
		}
		return errString;
	}
}