package ibase.webitm.ejb.fin;

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

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import javax.ejb.Stateless; // added for ejb3
@Stateless // added for ejb3
public class SundrybalOpen extends ValidatorEJB implements SundrybalOpenLocal,SundrybalOpenRemote //SessionBean
{

	GenericUtility genericUtility = GenericUtility.getInstance(); 
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	Connection conn = null;

	/*public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("Create Method Called...");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String wfValData() throws RemoteException,ITMException
	{
		System.out.println("wfValData()...");
		return "";
	}
	public String itemChanged() throws RemoteException,ITMException
	{
		System.out.println("itemChanged Called...");
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{

		Document currentDom = null;
		Document headerDom = null;
		Document detailDom = null;
		String valueXmlString = "";
		try
		{
			currentDom	= parseString(xmlString);
			headerDom	= parseString(xmlString1);
			if (xmlString2.trim().length() > 0 )
			{
				detailDom = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(currentDom,headerDom,detailDom,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [SundrybalOpenEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
			e.printStackTrace();
		}
        return valueXmlString; 
	}
	public String itemChanged(Document currentDom, Document headerDom,Document detailDom, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String errString = "", userId = "", empCode = "", loginSite = "";
		StringBuffer retString = new StringBuffer();
		String acctPrd = "", prdCode = "000000", sql = "";
		String siteDescr = "", finEntity = "", finentDescr = "", currCode = "", currDescr = ""; 
		String siteCode = "";
		int currentFormNo = 0;
		
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		userId = getValueFromXTRA_PARAMS(xtraParams,"userId");
		empCode = getValueFromXTRA_PARAMS(xtraParams,"empCode");
		loginSite = getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>");
		
		try
		{
			conn = getConnection();//Gets a Connection
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("[SundrybalOpenEJB] [itemChanged] :currentFormNo ....." +currentFormNo);
			valueXmlString.append("<Root>");
			valueXmlString.append("<header>");
			valueXmlString.append("<editFlag>").append(editFlag).append("</editFlag>");
			valueXmlString.append("</header>");
			valueXmlString.append("<Detail>");		
			switch (currentFormNo)
			{
				case 1:
					if (currentColumn.trim().equals("itm_default"))
					{
						try
						{
							sql = "SELECT A.DESCR,A.FIN_ENTITY, B.DESCR, B.CURR_CODE, C.DESCR "
								+" FROM SITE A, FINENT B, CURRENCY C "
								+ "WHERE A.FIN_ENTITY = B.FIN_ENTITY "
								+ "AND B.CURR_CODE = C.CURR_CODE "
								+ "AND A.SITE_CODE = '"+loginSite+"'";							
							stmt = conn.createStatement();
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								siteDescr	= rs.getString(1);
								finEntity	= rs.getString(2);
								finentDescr = rs.getString(3);
								currCode	= rs.getString(4);
								currDescr	= rs.getString(5);
							}
							rs.close();
							sql = "SELECT ACCT_PRD FROM PARAMETER";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								acctPrd = rs.getString("ACCT_PRD");
							}	
							rs.close();	
							stmt.close();
							System.out.println("AaccountPeriod.::: "+acctPrd);
							System.out.println("Period Code.::: "+prdCode);
							System.out.println("SiteCode::: "+loginSite);
							System.out.println("SiteCode Desc:::: "+siteDescr);
							System.out.println("FinEntity.::: "+finEntity);
							System.out.println("FinEntityDesc.::: "+finentDescr);
							System.out.println("CurrCode.::: "+currCode);
							System.out.println("CurrDescr.::: "+currDescr);

							valueXmlString.append("<acct_prd>").append(acctPrd).append("</acct_prd>");
							valueXmlString.append("<prd_code>").append(prdCode).append("</prd_code>");
							valueXmlString.append("<site_code>").append(loginSite).append("</site_code>");
							valueXmlString.append("<site_descr>").append("<![CDATA["+siteDescr+"]]>").append("</site_descr>");
							valueXmlString.append("<fin_entity>").append(finEntity).append("</fin_entity>");
							valueXmlString.append("<finent_descr>").append("<![CDATA["+finentDescr+"]]>").append("</finent_descr>");
							valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
							valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");
							valueXmlString.append("<tran_type protect = '1'>").append("OPN").append("</tran_type>");
						}
						catch(Exception e)
						{
							System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 1::itm_default] :==>\n"+e.getMessage());
							e.printStackTrace();
							throw new ITMException(e);
						}
					}
					if (currentColumn.trim().equals("site_code"))
					{
						siteCode = genericUtility.getColumnValue("site_code",currentDom);
						System.out.println("SiteCode:::: "+siteCode);
						if (siteCode != null)
						{
							try					 
							{
								sql = "SELECT A.DESCR,A.FIN_ENTITY, B.DESCR, B.CURR_CODE, C.DESCR "
									+" FROM SITE A, FINENT B, CURRENCY C "
									+ "WHERE A.FIN_ENTITY = B.FIN_ENTITY "
									+ "AND B.CURR_CODE = C.CURR_CODE "
									+ "AND A.SITE_CODE ='"+siteCode+"'";								
								stmt = conn.createStatement();
								rs = stmt.executeQuery(sql);							
								if (rs.next())
								{
									siteDescr	= rs.getString(1);
									finEntity	= rs.getString(2);
									finentDescr = rs.getString(3);
									currCode	= rs.getString(4);
									currDescr	= rs.getString(5);
								}
								rs.close();
								stmt.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 1::site_code] :==>\n"+e.getMessage());
								e.printStackTrace();
								throw new ITMException(e);
							}
						}
						valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>");
						valueXmlString.append("<fin_entity>").append(finEntity).append("</fin_entity>");
						valueXmlString.append("<finent_descr>").append("<![CDATA["+finentDescr+"]]>").append("</finent_descr>");
						valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");
					}
					if (currentColumn.trim().equals("curr_code"))
					{
						currCode = genericUtility.getColumnValue("curr_code",currentDom);
						System.out.println("CurrencyCode:::: "+currCode);
						if (currCode != null)
						{
							try					 
							{
								sql = "SELECT DESCR FROM CURRENCY WHERE CURR_CODE ='" +currCode+ "'";
								stmt = conn.createStatement();
								rs = stmt.executeQuery(sql);							
								if (rs.next())
								{
									currDescr = rs.getString(1);
								}
								rs.close();
								stmt.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 1::curr_code] :==>\n"+e.getMessage());
								e.printStackTrace();
								throw new ITMException(e);
							}
						}
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");
					}//
					valueXmlString.append("</Detail>");
				break;

				case 2:
					String dbCurrCode = "", dbCurrCodeDescr = "";
					siteCode	= getColumnValue("site_code",headerDom);
					finEntity	= getColumnValue("fin_entity",headerDom);
					currCode	= getColumnValue("curr_code",headerDom);
					currDescr	= getColumnValue("currency_descr",headerDom);
					acctPrd		= getColumnValue("acct_prd",headerDom);
					prdCode		= getColumnValue("prd_code",headerDom);
					if (currentColumn.trim().equals("itm_default"))
					{
						valueXmlString.append("<curr_code>").append(dbCurrCode).append("</curr_code>");
						valueXmlString.append("<currency_descr>").append("<![CDATA["+dbCurrCodeDescr+"]]>").append("</currency_descr>");
					}
					else if (currentColumn.trim().equals("sundry_code") || currentColumn.trim().equals("sundry_type"))
					{
						String sundryCode = "", sundryDescr = "", acctCode = "", cctrCode = "";
						String sundryType = "", acctDescr = "", cctrDescr = "", currCodeFin = "", finEnt = "";
						double crAmtOld = 0d, drAmtOld = 0d, crAmtBaseOld = 0d, drAmtBaseOld = 0d;
						ArrayList arrList = new ArrayList();
						
						sundryType = getColumnValue("sundry_type",currentDom,"2");
						sundryCode = getColumnValue("sundry_code",currentDom,"2");
						System.out.println("sundry_type ::"+sundryType);
						arrList = sundryDesc(sundryType,sundryCode,conn);
						System.out.println("arrList return From sundryDesc() :: " + arrList);
						sundryDescr = (String)arrList.get(0);
						acctCode	= (String)arrList.get(1);
						cctrCode	= (String)arrList.get(2);

						//---Added by nisar on 17/01/08---
						sql="SELECT TRIM(DDF_SUNDRY_NAME('"+sundryType+"','"+sundryCode+"','')) DESCR FROM DUAL "; 
						pstmt = conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							sundryDescr = rs.getString("DESCR");
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						//---Ended by nisar on 17/01/08---
						if (sundryDescr == null)
						{
							sundryDescr = " ";
						}
						if (acctCode == null)
						{
							acctCode = " ";
						}
						if (cctrCode == null)
						{
							cctrCode = " ";
						}
						try					 
						{
							sql = "SELECT DESCR FROM ACCOUNTS WHERE ACCT_CODE ='" +acctCode+ "'";
							stmt = conn.createStatement();
							rs = stmt.executeQuery(sql);
													
							if (rs.next())
							{
								acctDescr = rs.getString("DESCR");
							}
							rs.close();
							sql = "SELECT DESCR FROM COSTCTR WHERE CCTR_CODE ='"+cctrCode+"'";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								cctrDescr = rs.getString("DESCR");
							}
							rs.close();
							stmt.close();
						}
						catch(Exception e)
						{
							System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::sundry_code || sundry_type] :==>\n"+e.getMessage());
							e.printStackTrace();
							throw new ITMException(e);
						}
						try					 
						{
							stmt = conn.createStatement();
							if (sundryType.equals("B"))
							{
								sql = "SELECT CURR_CODE FROM BANK WHERE BANK_CODE ='" +sundryCode+ "'";
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									currCode = rs.getString("CURR_CODE");
								}
							}
							else if (sundryType.equals("E"))
							{
								sql = "SELECT CURR_CODE FROM FINENT WHERE FIN_ENTITY ='" +sundryCode+ "'";
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									currCode = rs.getString("CURR_CODE");
								}
							}
							else if (sundryType.equals("P"))
							{
								sql = "SELECT CURR_CODE FROM SALES_PERS WHERE SALES_PERS ='" +sundryCode+ "'";
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									currCode = rs.getString("CURR_CODE");
								}
							}
							//---------Added by nisar on 17/01/08--
							else if (sundryType.equals("T"))
							{
								sql="SELECT CURR_CODE FROM TRANSPORTER WHERE TRAN_CODE = '"+sundryCode+"'";
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									currCode = rs.getString("CURR_CODE");
								}
							}
							else if (sundryType.equals("L"))
							{//ADDED BY NISAR ON 17/01/08
								sql="SELECT CURR_CODE FROM LOANPARTY WHERE PARTY_CODE ='"+sundryCode+"'";
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									currCode = rs.getString("CURR_CODE");
								}
							}
							else if (sundryType.equals("X"))
							{//ADDED BY NISAR ON 17/01/08
								sql = "SELECT CURR_CODE FROM FINENT WHERE FIN_ENTITY ='" +sundryCode+ "'";
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									currCode = rs.getString("CURR_CODE");
								}
							}
						}
						catch(Exception e)
						{
							System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::sundry_code || sundry_type] :==>\n"+e.getMessage());
							e.printStackTrace();
							throw new ITMException(e);
						}
						finally
						{
							try
							{
								if (rs != null)
								{
									rs.close();
									rs = null;
								}
								if (stmt != null)
								{
									stmt.close();
									stmt = null;
								}
							}
							catch(Exception e){}
						}
						try
						{
							sql = " SELECT DESCR FROM CURRENCY WHERE CURR_CODE = '"+currCode+"'";
							stmt = conn.createStatement();
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								currDescr = rs.getString("DESCR");
							}
							rs.close();
							sql = "SELECT SITE.FIN_ENTITY, CURR_CODE FROM SITE, FINENT WHERE SITE.FIN_ENTITY = FINENT.FIN_ENTITY AND SITE_CODE = '"+siteCode+"'";
							rs = stmt.executeQuery(sql);							
							if (rs.next())
							{
								finEnt = rs.getString(1);
								currCodeFin = rs.getString(2);
							}
							rs.close();
							if (currCodeFin.equals(currCode))
							{
								valueXmlString.append("<exch_rate protect = '1'>").append("1").append("</exch_rate>");
							}
							else
							{
								valueXmlString.append("<exch_rate protect = '0'>").append("</exch_rate>");
							}
							rs.close();
							stmt.close();
						}
						catch(Exception e)
						{
							System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::sundry_code || sundry_type] :==>\n"+e.getMessage());
							e.printStackTrace();
							throw new ITMException(e);
						}
						/* -- Commented and Changes below - Gulzar - 16/07/08  Req ID:FI89PHE007
						sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM ACCTBAL "+
							  " WHERE ACCT_PRD = '"+acctPrd+"' AND PRD_CODE = '"+prdCode+"' AND FIN_ENTITY = '"+finEntity+"'"+
							  " and SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+acctCode+"' AND CCTR_CODE = '"+cctrCode+"'";*/	
						sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM SUNDRYBAL "+
							  "WHERE ACCT_PRD = '"+acctPrd+"' AND PRD_CODE = '"+prdCode+"' AND FIN_ENTITY = '"+finEntity+"' "+
							  "AND SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+acctCode+"' AND CCTR_CODE = '"+cctrCode+"' " +
							  "AND SUNDRY_TYPE = '"+sundryType+"' AND SUNDRY_CODE = '"+sundryCode+"'";
						System.out.println("sql ::"+ sql);
						//End Changes - Gulzar - 16/07/08 - Req ID:FI89PHE007
						stmt = conn.createStatement();
						rs = stmt.executeQuery(sql);
						try					 
						{
							if (rs.next())
							{
								crAmtOld	= rs.getDouble("CR_AMT");
								drAmtOld	= rs.getDouble("DR_AMT");
								crAmtBaseOld = rs.getDouble("CR_AMT__BASE");
								drAmtBaseOld = rs.getDouble("DR_AMT__BASE");
							}
							rs.close();
							stmt.close();
						}
						catch(Exception e)
						{
							System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::acct_code] :==>\n"+e.getMessage());
							e.printStackTrace();
							throw new ITMException(e);
						}
						valueXmlString.append("<sundry_descr>").append("<![CDATA["+sundryDescr+"]]>").append("</sundry_descr>");
						valueXmlString.append("<acct_code>").append(acctCode).append("</acct_code>");
						valueXmlString.append("<accounts_descr>").append("<![CDATA["+acctDescr+"]]>").append("</accounts_descr>");
						valueXmlString.append("<costctr_descr>").append("<![CDATA["+cctrDescr+"]]>").append("</costctr_descr>");
						valueXmlString.append("<cctr_code>").append(cctrCode).append("</cctr_code>");
						valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");
						valueXmlString.append("<cr_amt__old>").append(crAmtOld).append("</cr_amt__old>");
						valueXmlString.append("<dr_amt__old>").append(drAmtOld).append("</dr_amt__old>");
						valueXmlString.append("<cr_amt__base_old>").append(crAmtBaseOld).append("</cr_amt__base_old>");
						valueXmlString.append("<dr_amt__base_old>").append(drAmtBaseOld).append("</dr_amt__base_old>");
					}
					else if (currentColumn.trim().equals("acct_code"))
					{
						String acctDescr = "", acctCode = "", cctrCode = "";
						String sundryType = "";
						String sundryCode = "";
						double crAmtOld = 0;
						double drAmtOld = 0;
						double crAmtBaseOld = 0;
						double drAmtBaseOld = 0;
						acctCode = getColumnValue("acct_code",currentDom,"2");
						sundryType = getColumnValue("sundry_type",currentDom,"2");
						sundryCode = getColumnValue("sundry_code",currentDom,"2");
						System.out.println("acctCode= "+acctCode);
						if (acctCode != null)
						{
							try					 
							{
								sql = "SELECT DESCR FROM ACCOUNTS WHERE ACCT_CODE ='" +acctCode+ "'";
								stmt = conn.createStatement();
								rs = stmt.executeQuery(sql);
														
								if (rs.next())
								{
									acctDescr = rs.getString("DESCR");
								}
								rs.close();
								stmt.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::acct_code] :==>\n"+e.getMessage());
								e.printStackTrace();
								throw new ITMException(e);
							}
							cctrCode = getColumnValue("cctr_code",currentDom,"2"); 
							System.out.println("acctPrd= "+acctPrd);
							System.out.println("prdCode= "+prdCode);
							System.out.println("siteCode= "+siteCode);
							System.out.println("cctr_code= "+cctrCode);
							System.out.println("finEntity= "+finEntity);
							
							/* -- Commented and Changes below - Gulzar - 16/07/08 - Req ID:FI89PHE007
							sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM ACCTBAL "+
								  " WHERE ACCT_PRD = '"+acctPrd+"' AND PRD_CODE = '"+prdCode+"' AND FIN_ENTITY = '"+finEntity+"'"+
								  " and SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+acctCode+"' AND CCTR_CODE = '"+cctrCode+"'";*/
							sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM SUNDRYBAL "+
								  "WHERE ACCT_PRD = '"+acctPrd+"' AND PRD_CODE = '"+prdCode+"' AND FIN_ENTITY = '"+finEntity+"' "+
								  "AND SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+acctCode+"' AND CCTR_CODE = '"+cctrCode+"' " +
								  "AND SUNDRY_TYPE = '"+sundryType+"' AND SUNDRY_CODE = '"+sundryCode+"'";
							System.out.println("sql ::"+ sql);
							//End Changes - Gulzar - 16/07/08 - Req ID:FI89PHE007
							stmt = conn.createStatement();
							rs = stmt.executeQuery(sql);
							try					 
							{
								if (rs.next())
								{
									crAmtOld	= rs.getDouble("CR_AMT");
									drAmtOld	= rs.getDouble("DR_AMT");
									crAmtBaseOld = rs.getDouble("CR_AMT__BASE");
									drAmtBaseOld = rs.getDouble("DR_AMT__BASE");
								}
								rs.close();
								stmt.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::acct_code] :==>\n"+e.getMessage());
								e.printStackTrace();
								throw new ITMException(e);
							}
						}
						valueXmlString.append("<accounts_descr>").append("<![CDATA["+acctDescr+"]]>").append("</accounts_descr>");
						valueXmlString.append("<cr_amt__old>").append(crAmtOld).append("</cr_amt__old>");
						valueXmlString.append("<dr_amt__old>").append(drAmtOld).append("</dr_amt__old>");
						valueXmlString.append("<cr_amt__base_old>").append(crAmtBaseOld).append("</cr_amt__base_old>");
						valueXmlString.append("<dr_amt__base_old>").append(drAmtBaseOld).append("</dr_amt__base_old>");
					}
					else if (currentColumn.trim().equals("cctr_code"))
					{
						String cctrDescr = "", cctrCode = "", acctCode = "";
						String sundryType = "";
						String sundryCode = "";
						double crAmtOld = 0d;
						double drAmtOld = 0d;
						double crAmtBaseOld = 0d;
						double drAmtBaseOld = 0d;
						cctrCode = getColumnValue("cctr_code",currentDom,"2"); 
						sundryType = getColumnValue("sundry_type",currentDom,"2");
						sundryCode = getColumnValue("sundry_code",currentDom,"2");
						System.out.println("cctr_code="+cctrCode);
						if (cctrCode != null)
						{
							try					 
							{
								sql = "SELECT DESCR FROM COSTCTR WHERE cctr_code ='"+cctrCode+"'";
								stmt = conn.createStatement();
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									cctrDescr = rs.getString("DESCR");
								}
								rs.close();
								stmt.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 2::cctr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
							acctCode = getColumnValue("acct_code",currentDom,"2"); 
							System.out.println("acct_code="+acctCode);
							System.out.println("acctPrd="+acctPrd);
							System.out.println("prdCode="+prdCode);
							System.out.println("siteCode="+siteCode);
							System.out.println("cctr_code="+cctrCode);
							System.out.println("finEntity="+finEntity);
							
							/* -- Commented and Changes below - Gulzar - 16/07/08 - Req ID:FI89PHE007
							sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM ACCTBAL "+
								  " WHERE ACCT_PRD = '"+acctPrd+"' AND PRD_CODE = '"+prdCode+"' AND FIN_ENTITY = '"+finEntity+"'"+
								  " and SITE_CODE = '"+siteCode+"' AND ACCT_CODE = '"+acctCode+"' AND CCTR_CODE = '"+cctrCode+"'";*/
							sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM SUNDRYBAL "+
								  "WHERE ACCT_PRD = '"+acctPrd+"' AND PRD_CODE = '"+prdCode+"' AND FIN_ENTITY = '"+finEntity+"' "+
								  "AND SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+acctCode+"' AND CCTR_CODE = '"+cctrCode+"' " +
								  "AND SUNDRY_TYPE = '"+sundryType+"' AND SUNDRY_CODE = '"+sundryCode+"'";
							System.out.println("sql ::"+ sql);
							//End Changes - Gulzar - 16/07/08 - Req ID:FI89PHE007
							try					 
							{
								stmt = conn.createStatement();
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									crAmtOld	= rs.getDouble("CR_AMT");
									drAmtOld	= rs.getDouble("DR_AMT");
									crAmtBaseOld = rs.getDouble("CR_AMT__BASE");
									drAmtBaseOld = rs.getDouble("DR_AMT__BASE");
								}
								rs.close();
								stmt.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[SundrybalOpenEJB][itemChanged::case 1::cctr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
						}
						valueXmlString.append("<cr_amt__old>").append(crAmtOld).append("</cr_amt__old>");
						valueXmlString.append("<dr_amt__old>").append(drAmtOld).append("</dr_amt__old>");
						valueXmlString.append("<cr_amt__base_old>").append(crAmtBaseOld).append("</cr_amt__base_old>");
						valueXmlString.append("<dr_amt__base_old>").append(drAmtBaseOld).append("</dr_amt__base_old>");
						valueXmlString.append("<costctr_descr>").append("<![CDATA["+cctrDescr+"]]>").append("</costctr_descr>");
					}
					else if (currentColumn.trim().equals("cr_amt")) 
					{
						 double exchRate = Double.parseDouble(getColumnValue("exch_rate",currentDom,"2"));
						 double crAmt = Double.parseDouble(getColumnValue("cr_amt",currentDom,"2"));
						 double result =  exchRate * crAmt;
						 String sResult =  Double.toString(result);
						 valueXmlString.append("<cr_amt__base>").append(sResult).append("</cr_amt__base>");	
					}
					else if (currentColumn.trim().equals("dr_amt"))
					{
						 double exchRate = Double.parseDouble(getColumnValue("exch_rate",currentDom,"2"));
						 double crAmt = Double.parseDouble(getColumnValue("dr_amt",currentDom,"2"));
						 double result = exchRate * crAmt;
						 String sResult =  Double.toString(result);
						 valueXmlString.append("<dr_amt__base>").append(sResult).append("</dr_amt__base>");	
					}
					valueXmlString.append("</Detail>");
				break;
			}//End Of Switch Block
			valueXmlString.append("</Root>");
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection...........");
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception in Closing Connection :: " + e);
			}
		}
		return valueXmlString.toString();
	}
	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document currentDom = null;
		Document headerDom = null;
		Document detailDom = null;
		String  errString = null;
		System.out.println("Validation Start..........");
		try
		{
			System.out.println("xmlString:::"+xmlString);
			currentDom = parseString(xmlString);
			headerDom = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				detailDom = parseString("<Root>" + xmlString2+ "</Root>");
			}
			errString = wfValData(currentDom,headerDom,detailDom,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : SundrybalOpenEJB : wfValData(String xmlString) : ==>\n"+e.getMessage());
		}
		return errString;
	}

	public String wfValData(Document currentDom, Document headerDom,Document detailDom, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = "";
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		String userId = ""; 
		String loginSite = "", siteCode = "", errCode = "", currCode = "";
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = ""; 
		int ctr,currentFormNo=0;
		int childNodeListLength;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB(); 
		try
		{
			GenericUtility genericUtility = GenericUtility.getInstance(); 
			conn = getConnection(); 
			stmt = conn.createStatement();
			userId = getValueFromXTRA_PARAMS(xtraParams,"userId");
			loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
			System.out.println("userId :: " + userId);
			System.out.println("loginSite :: " + loginSite);
			
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			switch(currentFormNo)
			{
				case 1:
					parentNodeList = currentDom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						int cnt;
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if (childNodeName.equals("site_code"))
						{
							siteCode = getColumnValue("site_code",currentDom);
							System.out.println("Site Code :: " + siteCode);
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMSITECD1";
								errString = getErrorString("site_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
							System.out.println("call to site code function");
							errCode = itmDBAccessEJB.isSiteCode(siteCode,"FIN",conn); 
							System.out.println("errcode="+errCode);
							if(errCode != "")
							{
								errString = getErrorString("site_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
						}
						else if (childNodeName.equals("curr_code"))
						{
							currCode = getColumnValue("curr_code",currentDom);
							System.out.println("Curr Code :: "+ currCode);
							if (childNode.getFirstChild() == null)
							{
								errCode = "VTCURR";
								errString = getErrorString("curr_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
							sql = "SELECT COUNT(*) AS COUNT FROM CURRENCY WHERE CURR_CODE = '"+currCode+"'";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								cnt = rs.getInt("COUNT");
								if (cnt == 0)
								{
									errCode = "VTCURRCD1";
									errString = getErrorString("curr_code",errCode,userId);
									System.out.println("errString :: " + errString);
									break;
								}
							}
							rs.close();
						}//End Of Else if Block
					}//End Of For Loop
				break;
				case 2:
					String acctCode = "";
					String cctrCode = "";
					String crAmt = "",sundryCode="";
					String drAmt = "",sundryType="";
					double d_crAmt= 0;
					double d_drAmt= 0;
					FinCommon finCommon = new FinCommon();
					System.out.println("Validation for Detail..........");
					parentNodeList	= currentDom.getElementsByTagName("Detail2");
					parentNode		= parentNodeList.item(0);
					childNodeList	= parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						int cnt;
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if(childNodeName.equals("sundry_code"))
						{
							if(childNode.getFirstChild() == null)
							{
								errCode = "VTSUNCD";
								errString = getErrorString("sundry_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							} //added by rajendra on 2/22/2008 for checking sundry_code
							else
							{
								
								sundryType = getColumnValue("sundry_type",currentDom,"2");
								sundryCode = getColumnValue("sundry_code",currentDom,"2");
								
								System.out.println("sundryType .........:: "+ sundryType);
								System.out.println("sundryCode .........:: "+ sundryCode);
								System.out.println("site_code .........:: "+ getColumnValue("site_code",headerDom));
							    errCode = finCommon.isSundryCode(getColumnValue("site_code",headerDom), sundryType, sundryCode, "", conn);
								if(errCode != null && errCode.trim().length() >0)
								{
									errString = getErrorString("sundry_code",errCode,"BASE");
									System.out.println("errString :: " + errString);
									break;
								}

							}
							//ended by rajendra on 2/22/2008 for checking sundry_code
						}
						else if (childNodeName.equals("acct_code"))
						{
							acctCode = getColumnValue("acct_code",currentDom,"2");
							System.out.println("Acct Code :: "+ acctCode);
							if (childNode.getFirstChild() == null)	
							{
								errCode = "VTACCTCODE";
								errString = getErrorString("acct_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
							//comment by rajendra on 2/22/2008
							//FinCommon finCommon = new FinCommon();
							errCode = finCommon.isAcctCode(getColumnValue("site_code",headerDom),acctCode,"fin",conn);
							System.out.println("errCode From finCommon.isAcctCode() :: " + errCode);
							if(errCode != "")
							{
								errString = getErrorString("acct_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
						}
						else if (childNodeName.equals("cctr_code"))
						{
							cctrCode=getColumnValue("cctr_code",currentDom,"2");
							System.out.println("cctrCode :: "+ cctrCode);
							if (childNode.getFirstChild() == null)
							{
								errCode = "VTCCTRCD1";
								errString = getErrorString("cctr_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
							errCode = itmDBAccessEJB.isCctrCode(getColumnValue("acct_code",currentDom,"2"),cctrCode,"FIN",conn);
							System.out.println("errCode From itmDBAccessEJB.isCctrCode() :: " + errCode);
							if(errCode != "")
							{
								errString = getErrorString("cctr_code",errCode,userId);
								System.out.println("errString :: " + errString);
								break;
							}
						}
						else if (childNodeName.equals("cr_amt"))
						{
							
							drAmt = getColumnValue("dr_amt",currentDom,"2");
							crAmt = getColumnValue("cr_amt",currentDom,"2");
							drAmt = drAmt == null ? "0" :drAmt;
							crAmt = crAmt == null ? "0" :crAmt;
							d_drAmt = Double.parseDouble(drAmt);
							d_crAmt = Double.parseDouble(crAmt);
							if(d_drAmt > 0 && d_crAmt > 0)
							{
								errCode = "VTCRAMT";
								errString = getErrorString("cr_amt",errCode,userId);
								break;
							}
						}
						else if (childNodeName.equals("dr_amt"))
						{
							
							drAmt = getColumnValue("dr_amt",currentDom,"2");
							crAmt = getColumnValue("cr_amt",currentDom,"2");
							drAmt = drAmt == null ? "0" :drAmt;
							crAmt = crAmt == null ? "0" :crAmt;
							d_drAmt = Double.parseDouble(drAmt);
							d_crAmt = Double.parseDouble(crAmt);
							if(d_drAmt > 0 && d_crAmt>0)
							{
								errCode = "VTCRAMT";
								errString = getErrorString("cr_amt",errCode,userId);
								break;
							}
						}
					}
				break;
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection...........");
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception in Closing Connection :: " + e);
			}
		}
		System.out.println("ErrString ::"+errString);
		return errString;
	}
	private ArrayList sundryDesc(String sundryType,String sundryCode,Connection conn)
	{
		Statement stmt = null;
		ResultSet rs = null;
		String sundrySql = "";
		String sundryDescr = "";
		String acctCode = "";
		String cctrCode = "";
		String fName = "";
		String mName = "";
		String lName = "";
		ArrayList arrList = new ArrayList();
		if (sundryCode != null)
		{	
			try					 
			{
				System.out.println("sundry_type .............."+sundryType);
				if (sundryType.equals("B"))						
				{
					System.out.println("SUNDRY TYPE ...................: B");
					sundrySql= " SELECT BANK_NAME,ACCT_CODE__BAL, CCTR_CODE__BAL FROM BANK WHERE BANK_CODE= '"+sundryCode+"'";
					System.out.println("SundrySql :: "+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString("BANK_NAME");
						acctCode	= rs.getString("ACCT_CODE__BAL");
						cctrCode	= rs.getString("CCTR_CODE__BAL");
						System.out.println("SUNDRY DESCR........."+sundryDescr);
						System.out.println("ACCT CODE..............."+acctCode);
						System.out.println("CCTR CODE..............."+cctrCode);
					}
				}
				else if (sundryType.equals("E"))
				{
					System.out.println("SUNDRY TYPE ................... : E");
					sundrySql= " SELECT EMP_FNAME,EMP_MNAME,EMP_LNAME,ACCT_CODE__SAL, CCTR_CODE__SAL FROM EMPLOYEE WHERE EMP_CODE= '"+sundryCode+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						fName		= rs.getString("EMP_FNAME");
						mName		= rs.getString("EMP_MNAME");
						lName		= rs.getString("EMP_LNAME");
						acctCode	= rs.getString("ACCT_CODE__SAL");
						cctrCode	= rs.getString("CCTR_CODE__SAL");
						sundryDescr = fName.trim()+" "+mName.trim()+" "+lName.trim();
						System.out.println("SUNDRY DESCR........."+sundryDescr);
						System.out.println("ACCT CODE..............."+acctCode);
						System.out.println("CCTR CODE..............."+cctrCode);
					}
					rs.close();	
					stmt.close();
				}
				else if (sundryType.equals("P"))
				{
					System.out.println("SUNDRY TYPE ..................... : P");
					sundrySql= " SELECT SP_NAME,ACCT_CODE__AP,CCTR_CODE__AP FROM SALES_PERS WHERE SALES_PERS= '"+sundryCode+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString("SP_NAME");
						acctCode	= rs.getString("ACCT_CODE__AP");
						cctrCode	= rs.getString("CCTR_CODE__AP");
					}
					rs.close();	
					stmt.close();								
				}
			}//try
			catch(Exception e)
			{
				System.out.println("Exception :[SundrybalOpenEJB][sundryDesc] :==>\n"+e.getMessage());
			}
			arrList.add(sundryDescr);
			arrList.add(acctCode);
			arrList.add(cctrCode);
		}//outer if
		else
		{
			arrList.add(" ");
			arrList.add(" ");
			arrList.add(" ");
		}
		return arrList;
	}//sundryDesc	

	private String decFormat(double num)
	{
		NumberFormat nf = new DecimalFormat("##############0.000");
		nf.setMaximumFractionDigits(3);
		return nf.format(num);
	}

}