/*
  Window Name : w_rcp_ibca
  Button Name : Default , All Site , For Site
  Action	  : Checks for Default,All site,For Site Button as per Conditions

*/

package ibase.webitm.ejb.fin.adv;

import java.rmi.RemoteException;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;

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

@Stateless // added for ejb3

public class RcpIbcaAct extends ActionHandlerEJB implements RcpIbcaActLocal,RcpIbcaActRemote // 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 actionType, String xmlString, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		String  resString = null;
		try
		{
//Checking for Default Button
			if (actionType.equalsIgnoreCase("Default"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance(); 
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString); 
				}
				resString = actionDefault(dom,objContext,xtraParams);
			}
//Checking for AllSite Button
			if (actionType.equalsIgnoreCase("AllSite"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance(); 
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString); 
				}
				resString = actionAllSite(dom,objContext,xtraParams);
			}
//Checking for ForSite Button
			if (actionType.equalsIgnoreCase("ForSite"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance(); 
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString); 
				}
				resString = actionForSite(dom,objContext,xtraParams);
			}

		}
	   	catch(Exception e)
		{
			System.out.println("Exception :RcpIbcaAction :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from RcpIbcaAction actionHandler"+resString);
	    return (resString);
	}

	private String actionDefault(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		int sundryCodeLen;
		String errCode = "";
		String errString = "";
		String siteCode = "";
		String sundryCode = "";
		String currCode = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		String sql = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String detCnt  = "0";
	    ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		try
		{
			//detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			System.out.println("******* detCnt ******:"+detCnt+":");
			if (detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");
				siteCode = genericUtility.getColumnValue("site_code__from",dom);
				System.out.println("siteCode :"+siteCode); 
				currCode = genericUtility.getColumnValue("curr_code",dom);
				System.out.println("currCode :"+currCode); 
				sundryCode = genericUtility.getColumnValue("sundry_code",dom);
				System.out.println("sundryCode :"+sundryCode); 
				

				if (sundryCode ==null || sundryCode.trim().length() == 0)
				{
					sql="SELECT TRAN_SER,REF_NO FROM RECEIVABLES WHERE SITE_CODE = '"+ siteCode +"'" + " AND CURR_CODE = '"+currCode +"'" + " AND TOT_AMT - ADJ_AMT <> 0 AND TRAN_SER <> 'R_ADV' AND TRAN_SER NOT LIKE 'R_IBC%' ORDER BY DUE_DATE";
				} 
				else
				{
					sql= "SELECT TRAN_SER, REF_NO FROM RECEIVABLES WHERE SITE_CODE = '"+ siteCode +"'" + " AND CURR_CODE = '"+ currCode + "'"+" AND CUST_CODE = '"+ sundryCode +"'" + " AND TOT_AMT - ADJ_AMT <> 0 AND TRAN_SER <> 'R_ADV' AND TRAN_SER NOT LIKE "+ "'R_IBC%' ORDER BY DUE_DATE ";
				}
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<ref_ser>").append("<![CDATA[").append(rs.getString(1).trim()).append("]]>").append("</ref_ser>\r\n")	;
					valueXmlString.append("<ref_no>").append("<![CDATA[").append(rs.getString(2).trim()).append("]]>").append("</ref_no>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}
				stmt.close();
				valueXmlString.append("</Root>\r\n");			
			}
			else
			{
				 errCode = "VTDETCNT";
			}
			if (!errCode.equals(""))
			{
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);
				conn.close();
				conn = null;
				return errString;
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception : RcpIbcaAction : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection......");
				conn.close();
				conn = null;
			}
			catch(Exception e){}
		}
		return valueXmlString.toString();
	}

	private String actionAllSite(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		String siteCode = "";
		String errCode = "";
		String errString = "";
		String currCode = "";
		String tranDate = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String detCnt  = "0";
	    ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		try
		{
			//detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			System.out.println("******* detCnt ******:"+detCnt+":");
			if (detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");
				siteCode = genericUtility.getColumnValue("site_code__from",dom);
				System.out.println("siteCode :"+siteCode); 
				currCode = genericUtility.getColumnValue("curr_code",dom);
				System.out.println("currCode :"+currCode); 
				tranDate = genericUtility.getColumnValue("tran_date",dom);
				System.out.println("tranDate :"+tranDate); 
						
				String sql="SELECT TRAN_SER, REF_NO, TOT_AMT, ADJ_AMT FROM RECEIVABLES WHERE SITE_CODE = '"+ siteCode +"'" +
						   " AND CURR_CODE = '"+ currCode +"'" + "AND TOT_AMT - ADJ_AMT <> 0 AND TRAN_SER <> 'R-ADV' AND TRAN_SER <> 'R-IBCA' AND  TRAN_DATE < = to_date('"+ tranDate +"','dd/MM/yy') ORDER BY DUE_DATE";  
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<ref_ser>").append("<![CDATA[").append(rs.getString(1).trim()).append("]]>").append("</ref_ser>\r\n")	;
					valueXmlString.append("<ref_no>").append("<![CDATA[").append(rs.getString(2).trim()).append("]]>").append("</ref_no>\r\n");
					valueXmlString.append("<tot_amt>").append("<![CDATA[").append(rs.getString(3)).append("]]>").append("</tot_amt>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}
				stmt.close();
				valueXmlString.append("</Root>\r\n");			
			}
			else
			{
				errCode = "VTDETCNT";
			}
			if (!errCode.equals(""))
			{
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);
				conn.close();
				conn = null;
				return errString;
			}
		}
		catch(SQLException sq)
		{
			System.out.println("Exception : RcpIbcaAction : actionHandler :(Document dom)" +sq.getMessage());
			throw new ITMException(sq);
		}
		catch(Exception e)
		{
			System.out.println("Exception : RcpIbcaAction : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection....");
				conn.close();
				conn = null;
			}
			catch(Exception e){}
		}
		return valueXmlString.toString();
	}

	private String actionForSite(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		String siteCode = "";
		String errCode = "";
		String errString = "";
		String currCode = "";
		String siteCodeTo = "";
		String tranDate = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String detCnt  = "0";
		ConnDriver connDriver = new ConnDriver();//Added later to get a Connection 13-10-05
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		try
		{
			//detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			System.out.println("******* detCnt ******:"+detCnt+":");
			if (detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");	// Jiten  - 13-10-05
				siteCode = genericUtility.getColumnValue("site_code__from",dom);
				System.out.println("siteCode "+siteCode); 
				currCode = genericUtility.getColumnValue("curr_code",dom);
				System.out.println("currCode "+currCode); 
				siteCodeTo = genericUtility.getColumnValue("site_code__to",dom);
				System.out.println("siteCodeTo "+siteCodeTo); 
				tranDate = genericUtility.getColumnValue("tran_date",dom);
				System.out.println("tranDate "+tranDate); 

				String sql="SELECT TRAN_SER, REF_NO, TOT_AMT, ADJ_AMT, DUE_DATE FROM RECEIVABLES WHERE SITE_CODE ='"+ siteCode +"'" + "AND CURR_CODE ='"+ currCode +"'" + "AND TOT_AMT - ADJ_AMT <> 0 AND TRAN_SER NOT IN ('R-ADV','R-IBCA','S-INV') AND TRAN_DATE < = TO_DATE('"+ tranDate +"', 'dd/MM/yy')" + " UNION ALL SELECT TRAN_SER, REF_NO, TOT_AMT, ADJ_AMT, DUE_DATE FROM RECEIVABLES WHERE CURR_CODE ='"+ currCode +"'" +" AND TOT_AMT - ADJ_AMT <> 0 AND TRAN_DATE < = TO_DATE('"+ tranDate +"','dd/MM/yy')" + "AND TRAN_SER = 'S-INV' AND REF_NO IN ( SELECT I.INVOICE_ID FROM INVOICE I , SORDER S WHERE I.SALE_ORDER = S.SALE_ORDER AND S.SITE_CODE = '"+ siteCodeTo +"'" + ") ORDER BY DUE_DATE"; 
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				
				while (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<res_ser>").append("<![CDATA[").append(rs.getString(1).trim()).append("]]>").append("</res_ser>\r\n")	;
					valueXmlString.append("<ref_no>").append("<![CDATA[").append(rs.getString(2).trim()).append("]]>").append("</ref_no>\r\n");
					valueXmlString.append("<tot_amt>").append("<![CDATA[").append(rs.getString(3)).append("]]>").append("</tot_amt>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}
				stmt.close();
				valueXmlString.append("</Root>\r\n");			
			}
			else
			{
				errCode = "VTDETCNT";
			}
			if (!errCode.equals(""))
			{
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);
				conn.close();
				conn = null;
				return errString;
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception : RcpIbcaAction : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}
			catch(Exception e){}
		}
		return valueXmlString.toString();
	}
}