/*
	Window Name : w_payables_opening
*/

package ibase.webitm.ejb.fin;

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

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

public class PayableOpenings extends ValidatorEJB implements PayableOpeningsLocal,PayableOpeningsRemote //SessionBean
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	
	/*public void ejbCreate() throws RemoteException, CreateException 
	{   
		System.out.println("Entering into PayableOpeningsEJB.............");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String wfValData() throws RemoteException,ITMException
	{
		return "";
	}

	public String itemChanged() throws RemoteException,ITMException
	{
		return "";
	}
	

	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String valueXmlString = "";
		try
		{
			dom = parseString(xmlString); //returns the DOM Object for the passed XML Stirng
			System.out.println("xmlString :" + xmlString);
			dom1 = parseString(xmlString1); //returns the DOM Object for the passed XML Stirng
			System.out.println("xmlString1 :" + xmlString1);
			
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [PayablesOpeningEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
		}
        return valueXmlString; 
	}
	
	public String itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSet rs1 = null;
		StringBuffer valueXmlString = new StringBuffer();
		StringBuffer retString = new StringBuffer();
		String errCode = "";
		String sql = "";
		String sundrySql= "";
		String columnValue = "";
		String loginSite = "";
		String format = "";
		String accPrd = "";
		String childNodeName = "";
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr = 0;
		int n = 0;
		int currentFormNo = 0;
				 
		String userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
		System.out.println("userId ::-"+userId);
		loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
		System.out.println("loginSiteCode ::-"+loginSite);

		try
		{
			format= getApplDateFormat();
		}
		catch(Exception e)
		{
			System.out.println("Exception :[PayablesOpeningEJB]While Getting Date Format"+e.getMessage());		
		}
		java.util.Date DateX = new java.util.Date();
		java.text.SimpleDateFormat dtf= new SimpleDateFormat(format);
		
		try
		{
			conn = getConnection(); 
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}

			System.out.println("[PayablesOpeningEJB] [itemChanged] :currentFormNo ....." +currentFormNo);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			valueXmlString.append("<Detail>");	
			System.out.println("FORM NO ::-"+currentFormNo);								
			switch (currentFormNo)
			{
				case 1:
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				ctr = 0;
				int childNodeListLength = childNodeList.getLength();
				do
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();
					if(childNodeName.equals(currentColumn))
					{
						if (childNode.getFirstChild() != null)
						{
							columnValue=childNode.getFirstChild().getNodeValue().trim();
						}
					}
					ctr++;
				}
				while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
					
				if (currentColumn.trim().equals("itm_default"))
				{		
					String errString = "";
					String tranId = "";
					String siteDescr = "";
					String finEnt = "";
					String currDescr = "";
					String currCode = "";
					
					sql = "SELECT ACCT_PRD FROM PARAMETER";
					
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sql);
					if (rs.next())
					{
						accPrd = rs.getString(1);
					}
					try
					{	
						sql = "SELECT A.DESCR,A.FIN_ENTITY,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 = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,loginSite.trim());
						rs = pstmt.executeQuery();
						while (rs.next())
						{
							siteDescr = rs.getString(1);
							finEnt  = rs.getString(2);
							currCode = rs.getString(3);
							currDescr = rs.getString(4);
						}
						
						System.out.println("SiteCode ::- "+loginSite);
						System.out.println("SiteCode Desc ::-"+siteDescr);
						System.out.println("FinEntity ::- "+finEnt);
						System.out.println("CurrCode ::- "+currCode);
						System.out.println("CurrDescr ::- "+currDescr);
						System.out.println("AaccountPeriod ::- "+accPrd);
										
						valueXmlString.append("<acct_prd>").append(accPrd).append("</acct_prd>");
						valueXmlString.append("<site_code>").append(loginSite).append("</site_code>");
						//valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>");// Gulzar - 30/12/06
						valueXmlString.append("<site_descr>").append("<![CDATA["+siteDescr+"]]>").append("</site_descr>");// Gulzar - 30/12/06
						valueXmlString.append("<fin_entity>").append(finEnt).append("</fin_entity>");
						valueXmlString.append("<curr_code protect = '1'>").append(currCode).append("</curr_code>");
						//valueXmlString.append("<currency_descr>").append(currDescr).append("</currency_descr>");// Gulzar - 30/12/06
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");// Gulzar - 30/12/06
						valueXmlString.append("<tran_type protect = '1'>").append("OPN").append("</tran_type>"); //Gulzar 04-12-06
					}
					catch(Exception e)
					{
						System.out.println("Exception :[PayablesOpeningEJB][itemChanged::] :==>\n"+sql+e.getMessage());
						throw new ITMException(e);
					}
				}//itm_default				
				else if (currentColumn.trim().equals("site_code"))
				{
					String siteDescr = "";
					String finEnt = "";
					String currCode = "";
					String currDescr = "";
					if(columnValue != null)
					{
						try
						{  
							sql = "SELECT A.DESCR,A.FIN_ENTITY,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 = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,columnValue.trim());
							rs = pstmt.executeQuery();
							while (rs.next())
							{
								siteDescr = rs.getString(1);
								finEnt  = rs.getString(2);
								currCode = rs.getString(3);
								currDescr = rs.getString(4);
							}
							
							System.out.println("SiteCode ::- "+columnValue);
							System.out.println("SiteCode Desc ::- "+siteDescr);
							System.out.println("FinEntity ::- "+finEnt);
							System.out.println("CurrCode ::- "+currCode);
							System.out.println("CurrDescr ::- "+currDescr);
										
							//valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>");// Gulzar - 30/12/06
							valueXmlString.append("<site_descr>").append("<![CDATA["+siteDescr+"]]>").append("</site_descr>");// Gulzar - 30/12/06
							valueXmlString.append("<fin_entity>").append(finEnt).append("</fin_entity>");
							valueXmlString.append("<curr_code protect = '1'>").append(currCode).append("</curr_code>");
							//valueXmlString.append("<currency_descr>").append(currDescr).append("</currency_descr>");// Gulzar - 30/12/06
							valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");// Gulzar - 30/12/06
						}//try
						catch(Exception e)
						{
							System.out.println("Exception :[PayablesOpeningEJB][itemChanged::case 1::order_type] :==>\n"+e.getMessage());
							throw new ITMException(e);
						}
					}//if
				}//else if
				valueXmlString.append("</Detail>");	
			
				break;
				case 2:
				String currCode = "";
				String currDecr = "";
				String sundryType= "";
				String finEnt = "";
				String tranId = "";
				parentNodeList = dom.getElementsByTagName("Detail2");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				ctr = 0;
				int childListLength = childNodeList.getLength();
				do
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();
					if(childNodeName.equals(currentColumn))
					{
						if (childNode.getFirstChild() != null)
						{
							columnValue=childNode.getFirstChild().getNodeValue();
						}
					}
					ctr++;
				}
				while(ctr < childListLength && !childNodeName.equals(currentColumn));
				if (currentColumn.trim().equals("itm_default"))
				{
					String sqlEx = "";
					String dbCurrCode = "";
					currCode = getColumnValue("curr_code",dom1);
					tranId = getColumnValue("tran_id",dom1);
					finEnt = getColumnValue("fin_entity",dom1);
					System.out.println("tranId....................."+tranId);
					System.out.println("currCode................."+currCode);
					System.out.println("finEnt....................."+finEnt);
					
					valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
					valueXmlString.append("<tran_id>").append(tranId).append("</tran_id>");
				
					sqlEx = "SELECT CURR_CODE FROM FINENT WHERE FIN_ENTITY = '"+finEnt.trim()+"'";
					System.out.println("Exchange sql ::- " + sqlEx);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sqlEx);
					if (rs.next())
					{
						dbCurrCode = rs.getString(1);
						System.out.println("Curr Code from data base ::-"+dbCurrCode);
					}
					rs.close();	
					stmt.close();
					if (currCode.trim().equalsIgnoreCase(dbCurrCode.trim()))
					{
						valueXmlString.append("<exch_rate protect = '1'>").append("1").append("</exch_rate>");								
					}
					else
					{
						valueXmlString.append("<exch_rate protect= '0'>").append("</exch_rate>");
					}	
					valueXmlString.append("<sundry_type protect = '0'>").append("</sundry_type>");
					valueXmlString.append("<sundry_code protect = '0'>").append("</sundry_code>");
					valueXmlString.append("<ref_type protect = '1'>").append("OPN").append("</ref_type>"); //Gulzar 05-12-06
				}
				else if (currentColumn.trim().equals("tran_date")) //Case Added by Gulzar 05-12-06
				{
					String sundryCode = "", crTerm = "";
					int crDays = 0;
					sundryCode	= genericUtility.getColumnValue("sundry_code",dom); 
					if (sundryCode == null)
					{
						sundryCode = " ";
					}
					sql = "SELECT CR_TERM FROM SUPPLIER WHERE SUPP_CODE = '"+sundryCode+"'";
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sql);
					if (rs.next())
					{
						crTerm = rs.getString("CR_TERM");
					}
					rs.close();
					sql = "SELECT CR_DAYS FROM CRTERM WHERE CR_TERM ='" +crTerm+ "'";
					rs = stmt.executeQuery(sql);
					if (rs.next())
					{
						crDays = rs.getInt("CR_DAYS");
					}
					rs.close();
					stmt.close();
					System.out.println("columnValue : " + columnValue);
					SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
					Calendar  cal = Calendar.getInstance();
					java.util.Date tranDate = sdf.parse(columnValue);
					cal.setTime(tranDate);
					cal.add(cal.DATE,crDays);
					tranDate = cal.getTime();
					String dueDate = sdf.format(tranDate);
					valueXmlString.append("<due_date>").append(dueDate).append("</due_date>");
				}
				else if (currentColumn.trim().equals("ref_type"))
				{
					String refType = "";					
					if (columnValue != null)
					{
						System.out.println("Ref Type is........"+columnValue);
						if(columnValue.trim().equalsIgnoreCase("OPN"))
						{
							valueXmlString.append("<tot_amt protect = '0'>").append("</tot_amt>");	
						}
						else if(!columnValue.trim().equalsIgnoreCase("OPN"))
						{
							valueXmlString.append("<tot_amt protect = '1'>").append("0.0").append("</tot_amt>");
						}
					}//outer if
				}//else if
					
				else if (currentColumn.trim().equals("ref_no"))
				{
					java.sql.Date refDate = null;
					java.sql.Date dueDate = null;
					java.sql.Date billDate = null;
					java.sql.Date tranDate = null;
					String tranSer = "";
					String refSql = "";
					String sundryCode = "";
					String refType = "";
					String acctCode = "";
					String cctrCode = "";
					String billNo = "";
					String payMode = "";
					String purcOrder = "";
					String entryBatchNo = "";
					String sqlEx = "";
					String dbCurrCode = "";
					String tranDate1 = "";
					double exchRate = 0.0;
					double totAmount = 0.0;
					double adjAmount = 0.0;
					double supBillAmt = 0.0;
					double advAmount = 0.0;
					
					if (columnValue != null)
					{
						try					 
						{
							tranSer = getColumnValue("tran_ser",dom,"2");
							if(tranSer == null)
							{
								tranSer = "";
							}
							System.out.println("TRAN_SER .............."+tranSer);
							System.out.println("columnValue .............."+columnValue);
							refSql = "SELECT SUNDRY_TYPE,SUNDRY_CODE,REF_DATE,REF_TYPE,ACCT_CODE,CCTR_CODE,"
								   + "EXCH_RATE,DUE_DATE,TOT_AMT,ADJ_AMT,BILL_NO,BILL_DATE,SUPP_BILL_AMT,"
								   + "PAY_MODE,ADV_AMT,PURC_ORDER,ENTRY_BATCH_NO "
								   + "FROM MISC_PAYABLES WHERE TRAN_SER = '"+tranSer.trim()+"' "
								   + "AND  REF_NO = '"+columnValue.trim()+"' AND REF_TYPE ='OPN'"; //REF_TYPE ADDED GULZAR 04-12-06
							System.out.println("REF SQL................."+refSql);
							stmt = conn.createStatement();
							rs = stmt.executeQuery(refSql);
							if (rs.next())
							{
								System.out.println("Inside result set.................");
								sundryType = rs.getString(1);
								sundryCode = rs.getString(2);
								refDate = rs.getDate(3);
								refType = rs.getString(4);
								acctCode = rs.getString(5);
								cctrCode = rs.getString(6);
								exchRate = rs.getDouble(7);
								dueDate = rs.getDate(8);
								totAmount = rs.getDouble(9);
								adjAmount = rs.getDouble(10);
								billNo = rs.getString(11);
								billDate = rs.getDate(12);
								supBillAmt = rs.getDouble(13);
								payMode = rs.getString(14);
								advAmount = rs.getDouble(15);
								purcOrder = rs.getString(16);
								entryBatchNo = rs.getString(17);
								
								if (cctrCode == null)
								{
									cctrCode = "    ";
								}
								rs.close();	
								stmt.close();
								
								ArrayList arrList = new ArrayList();
								
								arrList = sundryDesc(sundryType,sundryCode,conn);
								String sundryDescr = (String)arrList.get(0);
								System.out.println("sundryDescr........"+sundryDescr);
								
								valueXmlString.append("<sundry_type protect = '1'>").append(sundryType).append("</sundry_type>");
								valueXmlString.append("<sundry_code protect = '1'>").append(sundryCode).append("</sundry_code>");
								//valueXmlString.append("<sundry_descr>").append(sundryDescr).append("</sundry_descr>");// Gulzar - 30/12/06
								valueXmlString.append("<sundry_descr>").append("<![CDATA["+sundryDescr+"]]>").append("</sundry_descr>");// Gulzar - 30/12/06
								valueXmlString.append("<ref_date>").append(dtf.format(refDate)).append("</ref_date>");
								valueXmlString.append("<ref_type>").append(refType).append("</ref_type>");
								valueXmlString.append("<acct_code protect = '1'>").append(acctCode).append("</acct_code>");
								valueXmlString.append("<cctr_code protect = '1'>").append(cctrCode).append("</cctr_code>");
								valueXmlString.append("<exch_rate protect = '1'>").append(exchRate).append("</exch_rate>");	
								valueXmlString.append("<due_date>").append(dtf.format(dueDate)).append("</due_date>");
								valueXmlString.append("<tot_amt_old>").append(totAmount).append("</tot_amt_old>");
								valueXmlString.append("<adj_amt>").append(adjAmount).append("</adj_amt>");
								valueXmlString.append("<bill_no>").append(billNo).append("</bill_no>");
								valueXmlString.append("<bill_date>").append(dtf.format(billDate)).append("</bill_date>");
								valueXmlString.append("<supp_bill_amt>").append(supBillAmt).append("</supp_bill_amt>");
								valueXmlString.append("<pay_mode>").append(payMode).append("</pay_mode>");
								valueXmlString.append("<adv_amt>").append(advAmount).append("</adv_amt>");
								valueXmlString.append("<purc_order>").append(purcOrder).append("</purc_order>");
								valueXmlString.append("<entry_batch_no>").append(entryBatchNo).append("</entry_batch_no>");
							}//if
							else 
							{
								valueXmlString.append("<sundry_type protect = '0'>").append("</sundry_type>");
								valueXmlString.append("<sundry_code protect = '0'>").append("</sundry_code>");
								valueXmlString.append("<ref_date>").append("</ref_date>");
								valueXmlString.append("<ref_type>").append("OPN").append("</ref_type>");
								valueXmlString.append("<acct_code protect = '0'>").append("</acct_code>");
								valueXmlString.append("<cctr_code protect = '0'>").append("</cctr_code>");
								
								sqlEx = "SELECT CURR_CODE FROM FINENT WHERE FIN_ENTITY ='" +finEnt+ "'";
								System.out.println("EXCHANGE SQL 2 ::- "+sqlEx);
								stmt = conn.createStatement();
								rs1 = stmt.executeQuery(sqlEx);
								if(rs1.next())
								{
									dbCurrCode = rs1.getString(1);
									System.out.println("Curr Code from DATA BASE ::- "+dbCurrCode);
								}
								rs1.close();	
								stmt.close();
								if (currCode.trim().equalsIgnoreCase(dbCurrCode))
								{
									valueXmlString.append("<exch_rate protect = '1'>").append("1").append("</exch_rate>");								
								}
								else
								{
									valueXmlString.append("<exch_rate protect= '0'>").append("</exch_rate>");
								}	
								valueXmlString.append("<due_date>").append("</due_date>");
								valueXmlString.append("<tot_amt_old protect = '1'>").append(totAmount).append("</tot_amt_old>");
								valueXmlString.append("<tot_amt>").append(totAmount).append("</tot_amt>");
								valueXmlString.append("<adj_amt>").append(adjAmount).append("</adj_amt>");
								valueXmlString.append("<bill_no>").append("</bill_no>");
								valueXmlString.append("<bill_date>").append("</bill_date>");
								valueXmlString.append("<supp_bill_amt>").append("</supp_bill_amt>");
								valueXmlString.append("<pay_mode>").append("</pay_mode>");
								valueXmlString.append("<adv_amt>").append("</adv_amt>");
								valueXmlString.append("<purc_order>").append("</purc_order>");
								valueXmlString.append("<entry_batch_no>").append("</entry_batch_no>");
							}
						}//try
						catch(Exception e)
						{
							System.out.println("Exception :[PayablesOpeningEJB][itemChanged::case 2:] :==>\n"+e.getMessage());
							throw new ITMException(e);
						}
					}//outer if
				}//else if
				else if(currentColumn.trim().equals("tot_amt"))
				{
					double totAmt = 0.0;
					double totAmtOld = 0.0;
					double diffAmt = 0.0;
					if (columnValue != null)
					{
						String stotAmtOld = getColumnValue("tot_amt_old",dom,"2");
						if(stotAmtOld == null)
						{
							totAmtOld = 0.0;
						}
						else
						{
							totAmtOld = Double.parseDouble(stotAmtOld);
						}
						System.out.println("Old total amount is "+totAmtOld);
						if((columnValue == null) || (columnValue.trim().length() == 0))
						{
							totAmt = 0.0;
						}
						else
						{
							totAmt = Double.parseDouble(columnValue);
						}
						System.out.println("Total  amount is "+totAmt);
						
						diffAmt = (totAmt - totAmtOld);
						
						System.out.println("Amount difference is "+diffAmt);
					}
					valueXmlString.append("<diff_amount>").append(diffAmt).append("</diff_amount>");
				}
				else if(currentColumn.trim().equals("curr_code"))
				{
					String tranDate = "";
					double exchRate = 0.0;
					
					if (columnValue != null)
					{
						currCode = getColumnValue("curr_code",dom1);
						tranDate = getColumnValue("tran_date",dom1);
						System.out.println("currCode *-::- "+ currCode);
						System.out.println("columnValue *-::- "+ columnValue);
						if(columnValue.trim().equals(currCode.trim()))
						{
							valueXmlString.append("<exch_rate protect = '1'>").append("1").append("</exch_rate>");
						}
						else
						{
							FinCommon finCommon = new FinCommon();
							exchRate = finCommon.getDailyExchRateSellBuy(columnValue.trim(),"",loginSite.trim(),tranDate.trim(),"S",conn);
							System.out.println("Daily Exch Rate ::- "+ exchRate);
							valueXmlString.append("<exch_rate protect = '0'>").append(exchRate).append("</exch_rate>");
						}
					}
				}
				else if (currentColumn.trim().equals("sundry_code"))
				{
					String sundryCode = "", crTerm = "", tranDate = "";
					int crDays = 0;
					String sundryDescr = "";
					String acctCode = "";
					String cctrCode = "";
					ArrayList arrList = new ArrayList();
					sundryType = getColumnValue("sundry_type",dom,"2");
					System.out.println("sundry_type .............."+sundryType);
					arrList = sundryDesc(sundryType,columnValue,conn);
					sundryDescr = (String)arrList.get(0);
					acctCode = (String)arrList.get(1);
					cctrCode = (String)arrList.get(2);
					//Added By Gulzar 05-12-06
					System.out.println("columnValue : " + columnValue);
					//sundryCode	= genericUtility.getColumnValue("cr_term",dom); 
					tranDate	= genericUtility.getColumnValue("tran_date",dom);
					if (columnValue == null)
					{
						columnValue = " ";
					}
					sql = "SELECT CR_TERM FROM SUPPLIER WHERE SUPP_CODE = '"+columnValue+"'";
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sql);
					if (rs.next())
					{
						crTerm = rs.getString("CR_TERM");
					}
					rs.close();
					sql = "SELECT CR_DAYS FROM CRTERM WHERE CR_TERM ='" +crTerm+ "'";
					rs = stmt.executeQuery(sql);
					if (rs.next())
					{
						crDays = rs.getInt("CR_DAYS");
					}
					rs.close();
					stmt.close();

					SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
					Calendar  cal = Calendar.getInstance();
					java.util.Date currDate = sdf.parse(tranDate);
					cal.setTime(currDate);
					cal.add(cal.DATE,crDays);
					currDate = cal.getTime();
					String dueDate = sdf.format(currDate);
					valueXmlString.append("<due_date>").append(dueDate).append("</due_date>");
					//End Added Gulzar 05-12-06
					//valueXmlString.append("<sundry_descr>").append(sundryDescr).append("</sundry_descr>");// Gulzar - 30/12/06
					valueXmlString.append("<sundry_descr>").append("<![CDATA["+sundryDescr+"]]>").append("</sundry_descr>");// Gulzar - 30/12/06
					valueXmlString.append("<acct_code>").append(acctCode).append("</acct_code>");
					valueXmlString.append("<cctr_code>").append(cctrCode).append("</cctr_code>");
				}//outer else if
				valueXmlString.append("</Detail>");
				break;
			}//Switch		
			valueXmlString.append("</Root>");				
		}//try
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
		}	
		finally
		{
			try
			{
				conn.close();
				conn = null;
			}
			catch(Exception s){}
		}
		return valueXmlString.toString();
	}//itemChanged()
	
	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String  errString = null;
		System.out.println("Entering into  validations ...................");
		try
		{
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : PayableOpeningsEJB : wfValData(String xmlString) : ==>\n"+e.getMessage());
		}
		return (errString);
	}

	public String wfValData(Document dom, Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String errString = "";
		String errCode = "";
		String sql = "";
		String userId = ""; 
		String loginSite = "";
		String siteCode = "";
		String acctCode = "";
		String cctrCode = "";
		String itemSer = "";
		String currCode = "";
		String childNodeName = ""; 
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr,currentFormNo=0;
		int childNodeListLength;
		int cnt;
		SimpleDateFormat simpleDateFormat = null;
		
		try
		{
			conn = getConnection(); 
			stmt = conn.createStatement();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
			System.out.println("userId = "+userId);
			loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
			System.out.println("xtraParams = "+xtraParams);
			simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
		
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			switch (currentFormNo)
			{
				case 1:
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						System.out.println("childNodeName : " + childNodeName);
						if (childNodeName.equals("site_code"))
						{
							System.out.println("childNodeName 1. : " + childNodeName);
							siteCode = genericUtility.getColumnValue("site_code",dom);
							System.out.println("childNode.getFirstChild() 1. : " + childNode.getFirstChild());
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMSITECD1";
								errString = getErrorString("site_code",errCode,userId);
								break;
							}
							errCode = itmDBAccessEJB.isSiteCode(siteCode, "", conn);
							if(errCode != "")
							{
								errString = getErrorString("site_code",errCode,userId);
								break;
							}
						}//if
					}
				break;
				case 2:
				
					String tranSer = "", refNo = "", refType = "";
					int  refTypeCnt = 0;
					System.out.println("In case 2 :");
					String childNodeValue = null;
					String tranSer1 = null,refNo1=null;
					ArrayList arList = new ArrayList();
					int lineNo=0,lineNo1=0;
					boolean isFound = false;
					
					//----Code added by nisar on 05/02/08----------------
					
					parentNodeList = dom2.getElementsByTagName("Detail2");
					
					System.out.println("[PayableOpeningsEJB] no of details2=>"+parentNodeList.getLength());
					
					for(int i=0;i<parentNodeList.getLength();i++)
					{
						parentNode = parentNodeList.item(i);
						childNodeList = parentNode.getChildNodes();
						HashMap hm = new HashMap();	
						for(int j=0;j<childNodeList.getLength();j++)
						{
							childNode= childNodeList.item(j);
							childNodeName = childNode.getNodeName();
						//	System.out.println("Child node=>"+childNode);
						//	System.out.println("Node Name=>"+childNodeName);
							
							if(childNodeName.equals("line_no"))				
							{
								childNodeValue= childNode.getFirstChild().getNodeValue();
								hm.put("line_no",childNodeValue);
							}				
							if(childNodeName.equals("tran_ser"))				
							{
								childNodeValue= childNode.getFirstChild().getNodeValue();								
								hm.put("tran_ser",childNodeValue);
							}
							if(childNodeName.equals("ref_no"))				
							{
								childNodeValue= childNode.getFirstChild().getNodeValue();
								hm.put("ref_no",childNodeValue);
							}						
						}
						arList.add(hm);							
					}
					//----Code ended by nisar on 05/02/08----------------
					
					parentNodeList = dom.getElementsByTagName("Detail2");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
				
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						System.out.println("childNodeName case 2..." + childNodeName);
						if(childNodeName.equals("sundry_code"))
						{
							if(childNode.getFirstChild() == null)
							{
								errCode = "VTSUNCD";
								errString = getErrorString("sundry_code",errCode,userId);
								break;
							}
						}
						else if(childNodeName.equals("tran_ser"))
						{
							if(childNode.getFirstChild() == null)
							{
								errCode = "VETRN2";
								errString = getErrorString("tran_ser",errCode,userId);
								break;
							}
						}
						else if(childNodeName.equals("ref_no"))
						{
							System.out.println("childNode.getFirstChild() :: ref_no :: " + childNode.getFirstChild());
							if(childNode.getFirstChild() == null)
							{
								errCode = "VTREFLINE1";
								errString = getErrorString("ref_no",errCode,userId);
								break;
							}
							else // Added By Gulzar 02-12-06
							{
								tranSer = genericUtility.getColumnValue("tran_ser",dom); 
								refNo   = genericUtility.getColumnValue("ref_no",dom); 
								System.out.println("tranSer : " + tranSer);
								System.out.println("refNo : " + refNo);
								sql = "SELECT COUNT(*) AS COUNT FROM MISC_PAYABLES WHERE TRAN_SER = '"+tranSer+"' AND REF_NO = '"+refNo+"'"; 
								System.out.println("sql : " + sql);
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									refTypeCnt = rs.getInt("COUNT");
								}
								rs.close();
								System.out.println("refTypeCnt : " + refTypeCnt);
								if (refTypeCnt > 0)
								{
									sql = "SELECT (CASE WHEN REF_TYPE IS NULL THEN ' ' ELSE REF_TYPE END) AS REF_TYPE FROM MISC_PAYABLES WHERE TRAN_SER = '"+tranSer+"' AND REF_NO = '"+refNo+"'"; 
									System.out.println("sql : " + sql);
									rs = stmt.executeQuery(sql);
									if (rs.next())
									{
										refType = rs.getString("REF_TYPE");
									}
									rs.close();
									System.out.println("refType : " + refType);
									if (!refType.equals("OPN"))
									{
										errCode = "VTOPNREF1";
										errString = getErrorString("ref_no",errCode,userId);
										break;
									}
								}
								//-------------Code added by nisar on 05/02/08---------------------
								tranSer = genericUtility.getColumnValue("tran_ser",dom2,"2"); 
								refNo   = genericUtility.getColumnValue("ref_no",dom2,"2"); 
								lineNo  = Integer.parseInt(genericUtility.getColumnValue("line_no",dom2,"2")==null?"0":genericUtility.getColumnValue("line_no",dom2,"2").trim()); 
								
								for(int i=0;i<arList.size();i++)
								{
									HashMap hm = (HashMap)arList.get(i);
									lineNo1 = Integer.parseInt(hm.get("line_no").toString());
									tranSer1 = hm.get("tran_ser").toString().trim();
									refNo1 = hm.get("ref_no").toString().trim();								
									
									System.out.println("lineNo1=>"+lineNo1+"-tranSER1=>"+tranSer1+"-refno1=>"+refNo1+"-");
									System.out.println("lineNo=>"+lineNo+"-"+"tranSER=>"+tranSer+"-refNo=>"+refNo+"-");
									System.out.println("condition=>"+(lineNo-lineNo1));
									
									if((lineNo-lineNo1)!=0 && tranSer1.equals(tranSer.trim()) && refNo1.equals(refNo.trim()))
									{
										isFound = true;
										break;
									}
								}//end of for
								if(isFound)
								{
									errCode = "VETRNEXT";
									System.out.println("Please do not duplicate transer and ref no combination"+errCode);
									errString = getErrorString("tran_ser",errCode,userId);
									break;
								}	
							}//End Added Gulzar 02-12-06
						}
						else if(childNodeName.equals("ref_type"))
						{	
							//String refType = ""; //Gulzar 04-12-06
							if(childNode.getFirstChild() == null)
							{
								errCode = "VTREF2";
								errString = getErrorString("ref_type",errCode,userId);
								break;
							}
							refType=genericUtility.getColumnValue("ref_type",dom,"2");
							if(!refType.trim().equals("OPN"))
							{
								errCode = "VTREF2";
								errString = getErrorString("ref_type",errCode,userId);
								break;
							}
						}
						else if (childNodeName.equals("acct_code"))
						{
							acctCode = getColumnValue("acct_code",dom,"2");
							if (childNode.getFirstChild() == null)
							{
								errCode = "VTACCTBD1";
								errString = getErrorString("acct_code",errCode,userId);
								break;
							}
							System.out.println("acct_code ................."+acctCode);
							FinCommon finCommon = new FinCommon();
							errCode = finCommon.isAcctCode(getColumnValue("site_code",dom1),acctCode,"fin",conn);
							if(errCode != "")
							{
								errString = getErrorString("acct_code",errCode,userId);
								break;
							}
						}//outer if
					/*	else if (childNodeName.equals("cctr_code"))
						{
							cctrCode = getColumnValue("cctr_code",dom,"2");
							if (childNode.getFirstChild() == null)
							{
								errCode = "VTINCCCODE";
								errString = getErrorString("cctr_code",errCode,userId);
								break;
							}
							System.out.println("cctr_code ......................."+cctrCode);
							errCode = itmDBAccessEJB.isCctrCode(getColumnValue("acct_code",dom,"2"),cctrCode,"fin",conn);
							System.out.println(getColumnValue("cctr_code",dom,"2"));
							System.out.println("errcode..." + errCode);
							if(errCode != "")
							{
								errString = getErrorString("cctr_code",errCode,userId);
								break;
							}
						}//outer if */
						else if (childNodeName.equals("curr_code"))
						{
							currCode = genericUtility.getColumnValue("curr_code",dom);
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMCURCD";
								errString = getErrorString("curr_code",errCode,userId);
								break;
							}
							currCode = childNode.getFirstChild().getNodeValue().trim();
							sql = "SELECT COUNT(1) AS COUNT FROM CURRENCY WHERE CURR_CODE = '"+currCode.trim()+"'";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								cnt = rs.getInt("COUNT");
								if (cnt == 0)
								{
									errCode = "VTCURRCD1";
									errString = getErrorString("curr_code",errCode,userId);
									break;
								}
							}
						}//if
						else if(childNodeName.equals("exch_rate"))
						{
							String exchRate = genericUtility.getColumnValue("exch_rate",dom);
							if(childNode.getFirstChild() == null || exchRate.trim().equals("0"))
							{
								errCode = "VTEXRAT1";
								errString = getErrorString("exch_rate",errCode,userId);
								break;
							}
							currCode = genericUtility.getColumnValue("curr_code",dom);
							String currCode1 = genericUtility.getColumnValue("curr_code",dom1);
							if(currCode.trim().equals(currCode1))
							{
								if(exchRate.trim().equals("1.0")) exchRate = "1";
								if(!exchRate.trim().equals("1")) 
								{
									errCode = "VTEXCH1";
									errString = getErrorString("exch_rate",errCode,userId);
									break;
								}
							}
						}
						else if(childNodeName.equals("tot_amt"))
						{
							double adjAmt = 0.0;
							double totAmt = 0.0;
							
							String sadjAmt = getColumnValue("adj_amt",dom,"2");
							adjAmt = Double.parseDouble(sadjAmt);
							System.out.println("Adj amount is "+adjAmt);
							
							String stotAmt = getColumnValue("tot_amt",dom,"2");
							if((stotAmt == null) || (stotAmt.trim().length() ==0))
							{
								stotAmt = "";
							}
							else
							{
								totAmt = Double.parseDouble(stotAmt);
							}
							System.out.println("Tot amount is "+totAmt);
							
							if((stotAmt.trim() == null) || (stotAmt.trim().length() == 0) || (totAmt<adjAmt ) && adjAmt != 0)
							{
								System.out.println("Invalid amount...........");
								System.out.println("Total amount "+totAmt+" is less than adj amount "+adjAmt);
								errCode = "INVALAMT";
								errString = getErrorString("tot_amt",errCode,userId);
								break;
							}
						}
						else if(childNodeName.equalsIgnoreCase("ref_date"))
						{
							System.out.println("Inside Reference date ...");
							if(childNode.getFirstChild() == null)
							{
								System.out.println("Reference date is null...");
								errCode = "VRDATE0";
								errString = getErrorString("ref_date",errCode,userId);
								break; 
							}
						}
						else if(childNodeName.equals("due_date"))
						{
							java.util.Date tranDate = new Date(simpleDateFormat.parse(genericUtility.getColumnValue("tran_date",dom)).getTime());
							
							if(childNode.getFirstChild() == null)
							{
								System.out.println("Error................");
								errCode = "VDUDATE0";
								errString = getErrorString("due_date",errCode,userId);
								break;
							}
							else
							{
								java.util.Date dueDate = new Date(simpleDateFormat.parse(genericUtility.getColumnValue("due_date",dom)).getTime());								
								String srefDate = getColumnValue("ref_date",dom);
								if(srefDate != null)
								{
									java.util.Date refDate = new Date(simpleDateFormat.parse(genericUtility.getColumnValue("ref_date",dom)).getTime());
									System.out.println("TranDate : " + tranDate + " Ref Date : " + refDate);
									if(tranDate.after(dueDate) || refDate.after(dueDate))
									{
										errCode = "VTDATERT";								
										errString = getErrorString("due_date",errCode,userId);
										break;
									}
								}
								
							}
						}
						else if(childNodeName.equalsIgnoreCase("bill_no"))
						{
							if(childNode.getFirstChild() == null)
							{
								System.out.println("Bill No. is null...");
								errCode = "VTRIBCA2";
								errString = getErrorString("bill_no",errCode,userId);
								break;
							}
						}
						else if(childNodeName.equals("bill_date"))
						{
							if(childNode.getFirstChild() == null)
							{
								errCode = "VBDATE0";
								errString = getErrorString("bill_date",errCode,userId);
								break;
							}
						}
					}
				break;
			}//swich
		}//try
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		System.out.println("ErrString ::"+errString);
		return errString;
	}//wfValData
	
	private ArrayList sundryDesc(String sundryType,String columnValue,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 (columnValue != null)
		{	
			try					 
			{
				//sundryType = getColumnValue("sundry_type",dom,"2");
				System.out.println("sundry_type .............."+sundryType);
				if (sundryType.equals("B"))						
				{
					System.out.println("SUNDRYTYPE ...................: B");
					sundrySql= " SELECT BANK_NAME,ACCT_CODE__BAL, CCTR_CODE__BAL FROM BANK WHERE BANK_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
						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= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						fName = rs.getString(1);
						mName = rs.getString(2);
						lName = rs.getString(3);
						acctCode = rs.getString(4);
						cctrCode = rs.getString(5);
						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("C"))
				{
					System.out.println("SUNDRYTYPE ......................: C");
					sundrySql= " SELECT CUST_NAME,ACCT_CODE__AR,CCTR_CODE__AR FROM CUSTOMER WHERE CUST_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
					}
					rs.close();	
					stmt.close();
				}
				else if (sundryType.equals("T"))
				{
					System.out.println("SUNDRYTYPE ......................: T");
					sundrySql= " SELECT TRAN_NAME,ACCT_CODE__FP,CCTR_CODE__FP FROM TRANSPORTER WHERE TRAN_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
					}
					rs.close();	
					stmt.close();								
				}
				else if (sundryType.equals("S"))
				{
					System.out.println("SUNDRYTYPE ..................... : S");
					sundrySql= " SELECT SUPP_NAME,ACCT_CODE__AP,CCTR_CODE__AP FROM SUPPLIER WHERE SUPP_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
					}
					rs.close();	
					stmt.close();
				}
				else if (sundryType.equals("X"))
				{
					System.out.println("SUNDRYTYPE ......................: X");
					sundrySql= " SELECT TAUTH_NAME,ACCT_CODE__AP,CCTR_CODE__AP FROM TAX_AUTHORITY WHERE TAUTH_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
					}
					rs.close();	
					stmt.close();
				}
				else if (sundryType.equals("L"))
				{
					System.out.println("SUNDRYTYPE ..................... : L");
					sundrySql= " SELECT PARTY_NAME,ACCT_CODE__AP,CCTR_CODE__AP FROM LOANPARTY WHERE PARTY_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
					}
					rs.close();	
					stmt.close();
				}
				else if (sundryType.equals("P"))
				{
					System.out.println("SUNDRYTYPE ..................... : P");
					sundrySql= " SELECT SP_NAME,ACCT_CODE__AP,CCTR_CODE__AP FROM SALES_PERS WHERE SALES_PERS= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
						acctCode = rs.getString(2);
						cctrCode = rs.getString(3);
					}
					rs.close();	
					stmt.close();								
				}
				else if (sundryType.equals("O"))
				{
					System.out.println("SUNDRYTYPE ..................... : O");
					sundrySql= " SELECT DESCR FROM ACCOUNTS WHERE ACCT_CODE= '"+columnValue+ "'";
					System.out.println("SundrySql ::-"+sundrySql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sundrySql);
					if (rs.next())
					{
						sundryDescr = rs.getString(1);
					}
					rs.close();	
					stmt.close();
				}								
			}//try
			catch(Exception e)
			{
				System.out.println("Exception :[PayablesOpeningEJB][sundryDesc] :==>\n"+e.getMessage());
				
			}
			arrList.add(sundryDescr);
			arrList.add(acctCode);
			arrList.add(cctrCode);
		}//outer if
		return arrList;
	}//sundryDesc
}//class