/* Description:
The ejb has 2 part one for getData and other for process tha getDate coading derive from nvo nvo_bo_auto_leave_encash
(gbf_calc_enchamt,gbf_get_data_logic:getData) (gbf_process_logic:process)
 */





//nvo_business_object_leave_enc
//gbf_calc_enchamt(viewdata)
//gbf_leave_approve(processs)
package ibase.webitm.ejb.adm;

import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import ibase.utility.CommonConstants;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import java.util.*;
import java.sql.*;
import java.io.*;
import org.w3c.dom.*; 
import javax.ejb.*;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.*;
import ibase.system.config.*;
import java.util.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class LeaveEncasePrc extends ProcessEJB implements LeaveEncasePrcLocal,LeaveEncasePrcRemote	//SessionBean
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	Connection conn = null;
	String processErrString = null;
	String messagePrStr = null;
	AdmCommon admCommon = new AdmCommon();
	//Globle variable Declaration
	//process
	String adCodeG =null;
	java.sql.Timestamp lapsOnDateTs = null;
	ArrayList contList = new ArrayList();
	//view Data
	String chgUser = null;  
	String chgTerm  = null;
	String loginSite = null;
	String userId = null;
	
	String errString =  null;
	double amountG = 0;

	/*public void ejbCreate() throws RemoteException, CreateException	{}
	public void ejbRemove(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}*/
	public String process() throws RemoteException,ITMException
	{return "";}
	
	
	
	
	
//	******************************************* G E T D A T A****************************************************************************
	public String getData(String xmlString, String xmlString2, String windowName, String xtraParams) throws RemoteException,ITMException
	{
		//ConnDriver connDriver = new ConnDriver();
		String rtrStr = "";
		Document headerDom = null;
		Document detailDom = null;
		System.out.println("xmlString:::::::::"+xmlString);
		System.out.println("xmlString2:::::::::"+xmlString2);
		System.out.println("windowName:::::::::"+windowName);
		System.out.println("xtraParams:::::::::"+xtraParams);
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				headerDom = genericUtility.parseString(xmlString); 				
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{	
				detailDom = genericUtility.parseString(xmlString2); 				
			}
			try
			{
				if(conn==null)
				{
					ConnDriver connDriver = new ConnDriver();
					conn = connDriver.getConnectDB("DriverITM");
					conn.setAutoCommit(false);
					DatabaseMetaData dbmd = conn.getMetaData();
					System.out.println("DriverName["+dbmd.getDriverName() );
					System.out.println("DriverURI["+dbmd.getURL()  );
					System.out.println("DriverUSER["+dbmd.getUserName() );
					System.out.println("ApplDateFormat["+genericUtility.getApplDateFormat());
					System.out.println("DBDateFormat["+genericUtility.getDBDateFormat() );
					connDriver = null;
				}
				
			}
			catch (Exception e)
			{
				System.out.println("Exception :LeaveEncasePrcEJB getData:==>"+e.getMessage());
			} 
			
			rtrStr = getData(headerDom, detailDom, windowName, xtraParams);
		}
		catch (Exception e)
		{
			
			rtrStr = e.getMessage();
			
		
		}
		return rtrStr; 
	}//END OF GETDATA(1)
	public String getData(Document headerDom, Document detailDom, String windowName, String xtraParams) throws RemoteException,ITMException
	{
	
		String errCode = "";
		String errString = null;
		String getDataSql = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		StringBuffer retTabSepStrBuff = new StringBuffer();
		String resultString=null;
		
		String period =null;
		
		String siteCodeTo = null; 
		String siteCodeFrom = null; 
		String deptCodeTo = null; 
		String deptCodeFrom = null; 
		String lveCodeTo= null;
		String lveCodeFrom = null;
		String procGroupTo = null;
		String procGroupFrom = null;
		String lapsOnDate = null;
		String empCodeFrom = null;
		String empCodeTo = null;
		String lapsDate = null;
		try
		{
			if(conn==null)
			{
					ConnDriver connDriver = new ConnDriver();
					conn = connDriver.getConnectDB("DriverITM");
					conn.setAutoCommit(false);
					DatabaseMetaData dbmd = conn.getMetaData();
					System.out.println("DriverName["+dbmd.getDriverName() );
					System.out.println("DriverURI["+dbmd.getURL()  );
					System.out.println("DriverUSER["+dbmd.getUserName() );
					System.out.println("ApplDateFormat["+genericUtility.getApplDateFormat());
					System.out.println("DBDateFormat["+genericUtility.getDBDateFormat() );
					connDriver = null;
				}
	
		siteCodeTo = genericUtility.getColumnValue("site_code__to",headerDom);; 
		siteCodeFrom = genericUtility.getColumnValue("site_code__fr",headerDom);
		deptCodeTo = genericUtility.getColumnValue("dept_code__to",headerDom); 
		deptCodeFrom = genericUtility.getColumnValue("dept_code__fr",headerDom); 
		lveCodeTo= genericUtility.getColumnValue("lve_code__to",headerDom);
		lveCodeFrom = genericUtility.getColumnValue("lve_code__fr",headerDom);
		procGroupTo = genericUtility.getColumnValue("proc_group__to",headerDom);
		procGroupFrom = genericUtility.getColumnValue("proc_group__fr",headerDom);
		lapsOnDate = genericUtility.getColumnValue("laps_on_date",headerDom);
		empCodeFrom = genericUtility.getColumnValue("emp_code__fr",headerDom);
		empCodeTo =genericUtility.getColumnValue("emp_code__to",headerDom);
		period = genericUtility.getColumnValue("prd_code",headerDom);
		lapsOnDate = genericUtility.getColumnValue("laps_on_date",headerDom);
		
		if ( siteCodeTo == null || siteCodeTo.trim().length() == 0 )
		{
			siteCodeTo = "";
			System.out.println("Site Code To is Null............");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( siteCodeFrom == null || siteCodeFrom.trim().length() == 0 )
		{
			siteCodeFrom = "";
			System.out.println("Site Code From is Null................");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( empCodeFrom == null || empCodeFrom.trim().length() == 0 )
		{
			empCodeFrom = "";
			System.out.println("Employee Code From is Null................");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( empCodeTo == null || empCodeTo.trim().length() == 0 )
		{
			empCodeTo = "";
			System.out.println("Employee Code To is Null..........");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		
		
		if ( deptCodeTo == null || deptCodeTo.trim().length() == 0 )
		{
			deptCodeTo = "";
			System.out.println("DepartMent Code To is Null............");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( deptCodeFrom == null || deptCodeFrom.trim().length() == 0 )
		{
			deptCodeFrom = "";
			System.out.println("DepartMent Code From is Null.......");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( lveCodeTo == null || lveCodeTo.trim().length() == 0 )
		{
			lveCodeTo = "";
			System.out.println("Lve Code To  is Null...........");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( lveCodeFrom == null || lveCodeFrom.trim().length() == 0 )
		{
			lveCodeFrom = "";
			System.out.println("Lve Code From  is Null is Null............");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( procGroupTo == null || procGroupTo.trim().length() == 0 )
		{
			procGroupTo = "";
			System.out.println("Process Group To Code To is Null.............");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( procGroupFrom == null || procGroupFrom.trim().length() == 0 )
		{
			procGroupFrom = "";
			System.out.println("Process Group From Code From is Null............");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		if ( lapsOnDate == null || lapsOnDate.trim().length() == 0 )
		{
			lapsOnDate = "";
			System.out.println("Laps On Date is Null......................");
			errString = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
			return errString;
		}
		else
		{
			lapsOnDate = getValidDateTimeString(lapsOnDate, genericUtility.getApplDateFormat(), genericUtility.getDBDateFormat());
			lapsOnDateTs = java.sql.Timestamp.valueOf(lapsOnDate + " 00:00:00");
		}
		//
		//+" "
		//+"employee.pay_site "
		 
		getDataSql = "SELECT "
						+" LEAVEBAL.EMP_CODE, "
						+" LEAVEBAL.LVE_CODE, " 
						+" EMPLOYEE.EMP_SITE,"
						+" EMPLOYEE.GRADE,"
						+" EMPLOYEE.CADRE,"
						+" SUM(LEAVEBAL.BAL_DAYS) AS BAL_DAYS,"
						+" 0 AS ENCASH_DAYS,"
						+" 0.00 AS AMT , "
						+"'     ' AS AD_CODE ,"
						+"EMPLOYEE.WORK_SITE,"
						+"EMPLOYEE.PAY_SITE,"
						+"EMPLOYEE.SUPP_CODE__CON "
						+"FROM LEAVEBAL, EMPLOYEE "
						+"WHERE ( EMPLOYEE.EMP_CODE = LEAVEBAL.EMP_CODE ) AND "
						+"( ( LEAVEBAL.LVE_CODE BETWEEN '"+lveCodeFrom+"' AND '"+lveCodeTo+"' ) AND "
						+"( LEAVEBAL.EMP_CODE BETWEEN '"+empCodeFrom+"' AND '"+empCodeTo+"' ) AND "
						+"( EMPLOYEE.WORK_SITE BETWEEN '"+siteCodeFrom+"' AND '"+siteCodeTo+"' ) AND "
						+"( EMPLOYEE.DEPT_CODE >= '"+deptCodeFrom+"') AND  "
						+"( EMPLOYEE.DEPT_CODE <= '"+deptCodeTo+"' ) AND  "
						+"( EMPLOYEE.PROC_GROUP >= '"+procGroupFrom+"') AND  "
						+"( EMPLOYEE.PROC_GROUP <= '"+procGroupTo+"') AND "
						+"( BAL_DAYS <> 0 )) AND  "
						+ "EMPLOYEE.SUPP_CODE__CON IS NOT NULL AND "
						+"(EMPLOYEE.RELIEVE_DATE IS NULL) "
						+"GROUP BY LEAVEBAL.EMP_CODE, "
		 				+"LEAVEBAL.LVE_CODE, "
						+"EMPLOYEE.EMP_SITE, "
						+"EMPLOYEE.GRADE,"
						+"EMPLOYEE.CADRE,"
						+"EMPLOYEE.WORK_SITE,"
						+"EMPLOYEE.PAY_SITE , "
						+" EMPLOYEE.SUPP_CODE__CON";  
					System.out.println("Get Data Sql:::::::::::["+getDataSql+"]");
						pstmt = conn.prepareStatement(getDataSql);
					//	pstmt.setTimestamp(1,lapsOnDateTs);
						rs = pstmt.executeQuery();
						String empCode = null;
						double balDay =0;
						String lveCode = null;
						
			while(rs.next()) 
			{
					errString = null;
					//retTabSepStrBuff.append(" ").append("\t");
					//lve_code
					lveCode = (rs.getString(2)==null?" " :rs.getString(2));
			        retTabSepStrBuff.append(lveCode.trim()).append("\t");
					//emp_code
					empCode = (rs.getString(1)==null)?" " :rs.getString(1);
 					retTabSepStrBuff.append(empCode.trim()).append("\t");
					//emp_site
					retTabSepStrBuff.append((rs.getString(3)==null?" " :rs.getString(3).trim())).append("\t");
					//grade
					retTabSepStrBuff.append((rs.getString(4)==null?" " :rs.getString(4).trim())).append("\t");
					//crade
					retTabSepStrBuff.append((rs.getString(5)==null?" " :rs.getString(5).trim())).append("\t");
					//bal_days//got to fx
					balDay = (rs.getDouble(6));
					retTabSepStrBuff.append((new Double(balDay).toString())).append("\t");
					errString  = CalcEnchamt(empCode,lveCode,period,balDay);
					if(errString!=null)break;
					//encash_days
					retTabSepStrBuff.append(balDay).append("\t");
					//amt
			 		retTabSepStrBuff.append(amountG).append("\t");
					//ad_code
					retTabSepStrBuff.append(adCodeG.trim()).append("\t");
					retTabSepStrBuff.append((rs.getString(8)==null?" " :rs.getString(8).trim())).append("\t");//ws
					retTabSepStrBuff.append((rs.getString(9)==null?" " :rs.getString(9).trim())).append("\t");//ps
					retTabSepStrBuff.append(" ").append("\t");
					//Supplier code contract
					retTabSepStrBuff.append((rs.getString(12)==null?" ":rs.getString(12).trim())).append("\n");
			}
			System.out.println("ResultString....." + resultString);
			if(errString!=null)
			{
				String errString1 = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
				String begPart = errString1.substring(0,errString1.indexOf("<trace>")+7);
				String endPart = errString1.substring(errString1.indexOf("</trace>"));
				System.out.println("begPart:::"+begPart);
				System.out.println("endPart:::"+endPart);
				String mainStr = begPart+errString+endPart;
				errString1 = mainStr;
				System.out.println("errString1="+errString1);
				return errString1;
			 
			}
			resultString = retTabSepStrBuff.toString();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			System.out.println("SQLException :LeaveEncasePrcEJB :getData(Document headerDom, Document detailDom, String windowName, String xtraParams):" + e.getMessage() + ":");
		}
		catch (Exception e)
		{
				e.printStackTrace();
			System.out.println("Exception :LeaveEncasePrcEJB :getData(Document headerDom, Document detailDom, String windowName, String xtraParams):" + e.getMessage() + ":");
		}
		finally
		{
			try
			{
				retTabSepStrBuff = null;
				if(pstmt != null)
				{
					pstmt.close();
				}
				if(rs != null)
				{
					rs.close();
				}
				
				if(conn != null)
				{conn.close();
				conn=null;}
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		System.out.println("Return String From Application Server --->>>");
		System.out.println("["+resultString+"]");
		return resultString;	
	}//END OF GETDATA(2)
	//gbf_calc_enchamt
	private String  CalcEnchamt(String empCode,String lveCode,String period,double balDays)
	{
		System.out.println("CalcEnchamt is in progress..........");
		System.out.println("EmpCode:::["+empCode+"]");
		System.out.println("lveCode:::["+lveCode+"]");
		System.out.println("period:::["+period+"]");
		System.out.println("balDays:::["+balDays+"]");
		
		String errStr = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		String sql =null;
		String workSite = null;
		String grade =null;
		String cadre =null;
		String lveTblno  =null;
		String adCode=null;
		String encashable = null;
		int cnt = 0;
		String propsDayStr = null;
		int propdDay =0;
		double amount = 0.0;
		double finalAmt =0.0;
		
		try
		{
			sql = "SELECT WORK_SITE , GRADE , CADRE	FROM EMPLOYEE WHERE EMP_CODE = '"+empCode.trim()+"'";
			System.out.println("Employee Information :::["+sql+"]");
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				workSite = rs.getString(1);
				grade = rs.getString(2);
				cadre = rs.getString(3);
				System.out.println("workSite:::["+workSite+"]");
				System.out.println("Grade:::["+grade+"]");
				System.out.println("Cadre:::["+cadre+"]");
			}
			
			rs.close();
			pstmt.close();
			sql = "SELECT LVE_TBLNO  FROM GRADE_SITE_LVETBL WHERE SITE_CODE = '"+workSite+"'  AND GRADE_CODE = '"+grade+"'";
			System.out.println("FINDING LEAVE TABLE NUMBER FROM GRADE_SITE_LVETBL.....");
			System.out.println("GRADE_SITE_LVETBL:::["+sql+"]");
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				lveTblno = rs.getString(1);
				System.out.println("lveTblno:::["+lveTblno+"]");
			}
		 	rs.close();
			pstmt.close();
			if(lveTblno == null)
			{
				
				System.out.println("LVE_TBLNO NOT FOUND IN THE GRADE_SITE_LVETBL");
				sql =" SELECT LVE_TBLNO FROM CADRE_SITE_LVETBL 	WHERE SITE_CODE = '"+workSite+"' AND CADRE_CODE = '"+cadre+"'";
				System.out.println("FINDING LEAVE TABLE NUMBER FROM CADRE_SITE_LVETBL.....");
				System.out.println("CADRE_SITE_LVETBL:::["+sql+"]");
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					lveTblno = rs.getString(1);
					System.out.println("lveTblno---:::["+lveTblno+"]");
				}
				rs.close();
				pstmt.close();
				if (lveTblno == null)
				{
					errStr = "Leave Table Not Defined For Grade [ "+ grade + " ]or Cadre [ "+cadre+"]";
					 return errStr;
				}
			}
			sql = " SELECT ENCASHABLE FROM LVETABLE WHERE LVE_TBLNO = '"+lveTblno+"' AND LVE_CODE = '"+lveCode+"'";
			System.out.println("LVETABLE:::["+sql+"]");
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				encashable = rs.getString(1);
				System.out.println("ENCASHABLE---:::["+encashable+"]");
			}
			rs.close();
			pstmt.close();
			if(encashable != null && (!encashable.equalsIgnoreCase("Y")))
			{
				errStr = " Leave is not encashable" ;
				return errStr;
			}
			String lveCon = lveCode.trim()+"_ENCON";
			adCode = admCommon.getAdmEnv(workSite , cadre , grade ,lveCon  ,new java.sql.Timestamp(System.currentTimeMillis()) ,conn);
			if(adCode != null)System.out.println("Ad code From getAdmEnv(workSite , cadre , grade ,lveCon  ,new java.sql.Timestamp(System.currentTimeMillis()) ,conn)["+adCode+"]");
			if(	adCode.equalsIgnoreCase("NULLFOUND"))
			{
				adCode = admCommon.getEnv("999999" , lveCon,conn);
				if(adCode != null)System.out.println("Ad code From admCommon.getEnv(999999 , lveCon,conn)["+adCode+"]");
				if(adCode.equalsIgnoreCase("NULLFOUND"))
				{
					adCode = null;
					adCode = admCommon.getAdmEnv(workSite , cadre , grade , "PL_ENCON" ,new java.sql.Timestamp(System.currentTimeMillis()),conn);
					if(adCode != null)System.out.println("Ad code From getAdmEnv(workSite , cadre , grade , PL_ENCON ,new java.sql.Timestamp(System.currentTimeMillis()),conn);["+adCode+"]");
				
					if(adCode==null)
					{
						adCode = admCommon.getEnv( "999999" , "PL_ENCON",conn  );
						if(adCode != null)System.out.println("Ad code From getEnv( 999999 , PL_ENCON,conn  );["+adCode+"]");
				
					}
				}
			}
			if(adCode.equalsIgnoreCase("NULLFOUND"))
			{
				errStr = "Variable name : "+lveCode+"_ENCON"+"NOT FOUND IN THE SITE_ENV OR PAYPARM";
			}
			sql = " SELECT COUNT(*)  FROM ALLWDEDN WHERE AD_CODE = '"+adCode+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				cnt = rs.getInt(1);
			}
			System.out.println("Ad code in Allodedn["+cnt+"]");
			rs.close();
			pstmt.close();
			
			if(cnt==0)
			{
				errStr = "Wrong ad defined in payroll variable: PL_ENC/"+lveCode +"_ENC";
				return errStr;
			}
			//*********************
			lveCon = lveCode.trim()+"_PROP";
			propsDayStr = admCommon.getAdmEnv(workSite , cadre , grade ,lveCon  ,new java.sql.Timestamp(System.currentTimeMillis()),conn );
			if(	propsDayStr.equalsIgnoreCase("NULLFOUND"))
			{
				propsDayStr = admCommon.getEnv("999999" , lveCon,conn);
				if(propsDayStr.equalsIgnoreCase("NULLFOUND"))
				{
					propsDayStr = admCommon.getAdmEnv(workSite , cadre , grade , "PL_PROP" ,new java.sql.Timestamp(System.currentTimeMillis()),conn);
					if(propsDayStr.equalsIgnoreCase("NULLFOUND"))
					{
						propsDayStr = admCommon.getEnv( "999999" , "PL_PROP",conn  );
					}
				}
			}
			if(adCode.equalsIgnoreCase("NULLFOUND"))
			{
				errStr = "Variable name : PL_ENCON/"+lveCode +"_ENCON"+"NOT FOUND IN THE SITE_ENV OR PAYPARM";
				return errStr;
			}
			System.out.println("propdDay::::::["+propsDayStr.trim()+"]");
		
			propdDay = Integer.parseInt(propsDayStr.trim());
			
			//***********************
			
			sql = "SELECT CODE FROM PERIOD WHERE  ? BETWEEN  FR_DATE AND TO_DATE " ;
			System.out.println("Payroll_projdet:::["+sql+"]");
			pstmt = conn.prepareStatement(sql);
			System.out.println("Laps Date:{"+lapsOnDateTs+"]");
			pstmt.setTimestamp(1,lapsOnDateTs);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				period = rs.getString(1);
				System.out.println("Period is ["+period+"]");
			}
			rs.close();
			pstmt.close();
			
			
			sql = " SELECT AMOUNT FROM PAYROLL_PROJDET	WHERE PRD_CODE = '"+period+"' and emp_code = '"+empCode+"' and ad_code  = '"+adCode+"'";
			System.out.println("Payroll_projdet:::["+sql+"]");
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				amount = rs.getDouble(1);
			}
			rs.close();
			pstmt.close();
			if(amount <=   0)
			{
				errStr = "Payroll Projection has not been done the emp["+empCode+"] prdCode ["+period+"] and adCode ["+adCode+"]";
				return errStr;
			}
		/*	sql = "select rnd_off , rnd_to from allwdedn where ad_code = '"+adCode+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				amount = rs.getDouble(1);
			}*/
			finalAmt = ( amount * balDays ) / propdDay;
			System.out.println("amount=["+amount+"]");
			System.out.println("balDays=["+balDays+"]");
			System.out.println("propdDay=["+propdDay+"]");
			System.out.println("finalAmt=["+finalAmt+"]");
			adCodeG = adCode;
			amountG = finalAmt;
			
			
			
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
		return errStr;
		
	}
	/*Decimal rnd_amt
	Decimal lc_multiply = 1
	if unr_amt < 0 then 
		lc_multiply = -1
		unr_amt = abs(unr_amt)
	elseif unr_amt = 0 then 
		return unr_amt
//	 17-01-04	no round return the unround amount
	elseif rnd_off = 'N' then
		return unr_amt
//	 17-01-04 of rnd_to is 0 divide by zero	
	elseif isnull(rnd_to) or rnd_to = 0 then
		return unr_amt
//	 end 17-01-04
	end if	
	Choose case rnd_off
	Case 'X'
	   if mod(unr_amt, rnd_to) > 0 then 
	      rnd_amt = unr_amt - mod(unr_amt, rnd_to) + rnd_to
	   else
	      rnd_amt = unr_amt
	   end if
	Case 'P'
	   rnd_amt = unr_amt - mod(unr_amt, rnd_to)
	Case 'R'
	   if mod(unr_amt, rnd_to) < rnd_to/2 then
	      rnd_amt = unr_amt - mod(unr_amt, rnd_to)
	   else 
	      rnd_amt = unr_amt - mod(unr_amt, rnd_to) + rnd_to
	   end if 
	Case else
	   rnd_amt = unr_amt
	End Choose
	rnd_amt = rnd_amt * lc_multiply
	Return rnd_am*/

	
	public String process(String xmlString, String xmlString2, String windowName, String xtraParams) throws RemoteException,ITMException
	{
		Document detailDom = null;
		Document headerDom = null;
		String retStr = "";
		try
		{	
			System.out.println("xmlString[process]::::::::::;;;"+xmlString);
			System.out.println("xmlString2[process]::::::::::;;;"+xmlString2);
			System.out.println("windowName[process]::::::::::;;;"+windowName);
			System.out.println("xtraParams[process]:::::::::;;;"+xtraParams);
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				headerDom = genericUtility.parseString(xmlString); 
				System.out.println("headerDom" + headerDom);
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				detailDom = genericUtility.parseString(xmlString2); 
				System.out.println("detailDom" + detailDom);
			}
		    retStr = process(headerDom, detailDom, windowName, xtraParams);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return retStr;
}//END OF PROCESS (1)
	
	public String process(Document headerDom, Document detailDom, String windowName, String xtraParams) throws RemoteException,ITMException
	{
		
		messagePrStr = null;
		processErrString = null;
		
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null; 
		
		int parentNodeListLength = 0;
		int childNodeListLength = 0;
		String childNodeName =  null;
		//filter field
		String encaseDate = null;
		String prdCode = null;
		String lapsOnDate = null;
		
		//brow field
		
		String lveCode = null;
		String camt = null;
		String balDay = null;
		String empCode =null;
		String adCode=null;
		String workSite = null;
		String encaseDay = null;
		String contCode = null;
		
		
		try
		{
			if(conn==null)
			{
				ConnDriver connDriver = new ConnDriver();
				conn = connDriver.getConnectDB("DriverITM");
				conn.setAutoCommit(false);
				DatabaseMetaData dbmd = conn.getMetaData();
				System.out.println("DriverName["+dbmd.getDriverName()+"]");
				System.out.println("DriverURI["+dbmd.getURL()+"]"  );
				System.out.println("DriverUSER["+dbmd.getUserName()+"]" );
				System.out.println("ApplDateFormat["+genericUtility.getApplDateFormat()+"]");
				System.out.println("DBDateFormat["+genericUtility.getDBDateFormat()+"]");
				connDriver = null;
			}

			encaseDate = genericUtility.getColumnValue("dt_encash",headerDom);
			prdCode = genericUtility.getColumnValue("prd_code",headerDom);
			System.out.println("Date Of encase::::::["+encaseDate+"]");
			System.out.println("Period Code::::::["+prdCode+"]");
			
			chgUser = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginCode");  
			chgTerm = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "termId");
			loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
			
			parentNodeList = detailDom.getElementsByTagName("Detail2");
			parentNodeListLength = parentNodeList.getLength(); 
			System.out.println("parentNodeListLength:::::::::"+parentNodeListLength);
			for (int selectedRow = 0; selectedRow < parentNodeListLength; selectedRow++)
			{
				lapsOnDate= null;
				lveCode = null; 
				camt = null;
				balDay = null;
				empCode =null;
				adCode=null;
				workSite = null;
				encaseDay = null;
				parentNode = parentNodeList.item(selectedRow);
				childNodeList = parentNode.getChildNodes();
				childNodeListLength = childNodeList.getLength();
				System.out.println("childNodeListLength::: "+ childNodeListLength+"\n");
				for (int childRow = 0; childRow < childNodeListLength; childRow++)
				{
					
					
					childNode = childNodeList.item(childRow);
					childNodeName = childNode.getNodeName();
					if (childNodeName.equalsIgnoreCase("supp_code__con"))
					{
						if(childNode.getFirstChild()!=null)
						{
							contCode = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("lve_code"))
					{
						if(childNode.getFirstChild()!=null)
						{
							lveCode = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("camt"))
					{
						if(childNode.getFirstChild()!=null)
						{
							camt = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("bal_days"))
					{
						if(childNode.getFirstChild()!=null)
						{
							balDay = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("emp_code"))
					{
						if(childNode.getFirstChild()!=null)
						{
							empCode = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("ad_code"))
					{
						if(childNode.getFirstChild()!=null)
						{
							adCode = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("work_site"))
					{
						if(childNode.getFirstChild()!=null)
						{
							workSite = childNode.getFirstChild().getNodeValue();
						}
					}
					if (childNodeName.equalsIgnoreCase("encash_days"))
					{
						if(childNode.getFirstChild()!=null)
						{
							encaseDay = childNode.getFirstChild().getNodeValue();
						}
					}
				}//inner loop
				//********************
				
				
				EncashCust encashCust= new EncashCust();
				
				encashCust.setCustCode(empCode);
				encashCust.setLeaveCode(lveCode);
				encashCust.setAmount(Double.parseDouble(camt));
				encashCust.setBalDay(Double.parseDouble(balDay));
				encashCust.setAdCode(adCode);
				encashCust.setEnchDay(Double.parseDouble(encaseDay));
				encashCust.setWorkSite(workSite);
				encashCust.setLapsOnDate(lapsOnDateTs);
				
				int ele = getExistanceOfContCode(contList, contCode);
				System.out.println("ContractorCode exist/not["+ele+","+contCode+"]");
				EncashSupp encashSupp ;
				if (ele == -1)
				{
					encashSupp = new EncashSupp();
					encashSupp.setContCode(contCode);
					//supplier.setGenInfo(conn);
					//supplier.setPrdCode(prdCode);
					contList.add(encashSupp);
					ele = contList.size() -1;
					System.out.println("The size of the element is "+ele);
				}
				else 
				{
					encashSupp = (EncashSupp)contList.get(ele);
				}
			
				encashSupp.setCustomerList(encashCust);
				contList.set(ele,encashSupp);	
			//**********************
			}// OUT FOR LOOP 
			//VOUCHER CREATION LOGIC CALL
				createVoucher(contList);
			
			if(messagePrStr!=null)
			{
					String errString1 = itmDBAccessEJB.getErrorString("","VTMRPPARM","","",conn);
					String begPart = errString1.substring(0,errString1.indexOf("<trace>")+7);
					String endPart = errString1.substring(errString1.indexOf("</trace>"));
					System.out.println("begPart:::"+begPart);
					System.out.println("endPart:::"+endPart);
					String mainStr = begPart+messagePrStr+endPart;
					errString1 = mainStr;
					System.out.println("errString1="+errString1);
					processErrString  = errString1;
				 
				}
		}//try end	
		catch(Exception e)
		{
			processErrString = "Exception";
			e.printStackTrace();
		}
		finally
		{
			System.out.println("Closing Connection....");
			try
			{ if(conn != null){conn.close();conn = null;}}
			catch(Exception e)
			{e.printStackTrace();}
		}
		return processErrString;
	}//END OF PROCESS(2)
	
	
	

	public String  createVoucher(ArrayList contList)
	{
		
		EncashSupp encashSupp = null;
		EncashCust encashCust = null;
		ArrayList customerList = null;
		String contCode = null;

		for(int cont=0;cont<contList.size();cont++)
		{
			
			encashSupp = (EncashSupp)contList.get(cont);
			customerList = (ArrayList)encashSupp.getCustomerList();
			contCode = encashSupp.getContCode();
			Suppliers supp = new Suppliers();
			supp.setSuppCode(contCode);
			supp.setGenInfo(conn);
			System.out.println("Total No of employee under Cont.["+contCode+"] is ["+customerList.size()+"]");
			for(int cust=0;cust<customerList.size();cust++)
			{
				
				encashCust = (EncashCust)customerList.get(cust);
				String str = 	makeTran(encashCust,supp,conn);
				if(messagePrStr!=null)
				{
					break;//return messagePrStr;
				}
						
			}
			if(processErrString==null && messagePrStr == null)
			{
				applyMaster(supp);
			
			}
			if(processErrString == null && messagePrStr == null)
			{
				try{conn.commit();}catch(Exception d){}
				System.out.println("Transaction commit for contract Code["+contCode+"]");
			}
			else
			{
				try{conn.rollback();}catch(Exception d){}
				System.out.println("Transaction RollBack for contract Code["+contCode+"]");
			}
		}
		return messagePrStr;
	}
	public void applyMaster(Suppliers supp)
	{
		//Header
		String suppCode = null;
		String taxClass=null;
		String 	taxChap=null;
		
		String taxEnv =null;
		String currCode=null;
		String siteCode=null;
		String bankCode=null;
		String finEntity=null;
		double exchRate=0;
		String acctCodeAp=null;
		String cctrCodeAp=null;
		String prdCode =  null;
		ArrayList custList = null;
		//Detail
		String empCode = null;
		double Amount =0;
		String acctCode = null;
		String cctrCode = null;
		double amt=0;
		String entryBatchNo =null;
		String acctPay = null;
		String cctrPay = null;
		
		StringBuffer masterXmlString = new StringBuffer();
		masterXmlString.append("<DocumentRoot>");
		masterXmlString.append("<description>").append("Datawindow Root").append("</description>");
		masterXmlString.append("<group0>");
		masterXmlString.append("<description>").append("Group0 description").append("</description>");
		masterXmlString.append("<Header0>");
		masterXmlString.append("<objName><![CDATA[").append("misc_voucher").append("]]></objName>");
		masterXmlString.append("<pageContext><![CDATA[").append("2").append("]]></pageContext>");
		masterXmlString.append("<objContext><![CDATA[").append("1").append("]]></objContext>");
		masterXmlString.append("<editFlag><![CDATA[").append("A").append("]]></editFlag>");
		masterXmlString.append("<focusedColumn><![CDATA[").append("").append("]]></focusedColumn>");
		masterXmlString.append("<action><![CDATA[").append("SAVE").append("]]></action>");
		masterXmlString.append("<elementName><![CDATA[").append("").append("]]></elementName>");
		masterXmlString.append("<keyValue><![CDATA[").append("1").append("]]></keyValue>");
		masterXmlString.append("<taxKeyValue><![CDATA[").append("").append("]]></taxKeyValue>");
		masterXmlString.append("<saveLevel><![CDATA[").append("1").append("]]></saveLevel>");
		masterXmlString.append("<forcedSave><![CDATA[").append(true).append("]]></forcedSave>");
		masterXmlString.append("<taxInFocus><![CDATA[").append(false).append("]]></taxInFocus>");
		masterXmlString.append("<description>").append("Header0 members").append("</description>");
		
		
		suppCode=supp.getSuppCode();
		taxClass=supp.getTaxClass();
		taxChap=supp.getTaxChap();
		taxEnv=supp.getTaxEnv();
		currCode=supp.getCurrCode();
		siteCode=supp.getSiteCode();
		bankCode=supp.getBankCode();
		finEntity=supp.getFinEntity();
		exchRate=supp.getExchRate();
		acctCodeAp=supp.getAcctCodeAp();
		cctrCodeAp=supp.getCctrCodeAp();
		prdCode =  supp.getPrdCode();
		acctPay =supp.getAcctCodeAp();
		cctrPay = supp.getCctrCodeAp();
		System.out.println("Period Code@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@["+prdCode+"]");
		entryBatchNo = prdCode+supp.getSiteCode();
		
//		Header xml String 
		masterXmlString.append("<Detail1 dbID=\"\" domID=\"1\" objName=\"misc_voucher\" objContext=\"1\">");
		masterXmlString.append("<attribute pkNames=\"\" status=\"N\" updateFlag=\"A\" selected=\"N\"/>");
		masterXmlString.append("<tran_id/>");
		masterXmlString.append("<vouch_type><![CDATA[").append("E").append("]]></vouch_type>");
		masterXmlString.append("<tran_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></tran_date>");
		masterXmlString.append("<tran_type><![CDATA[").append("LEN").append("]]></tran_type>"); 
    	masterXmlString.append("<eff_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></eff_date>");	
		masterXmlString.append("<sundry_type><![CDATA[").append("S").append("]]></sundry_type>");
		masterXmlString.append("<sundry_code ><![CDATA[").append((suppCode==null?" ":suppCode.trim())).append("]]></sundry_code>");
		masterXmlString.append("<acct_code__ap><![CDATA[").append((acctCodeAp==null?"":acctCodeAp.trim())).append("]]></acct_code__ap>");
		masterXmlString.append("<cctr_code__ap><![CDATA[").append((cctrCodeAp==null?"":cctrCodeAp.trim())).append("]]></cctr_code__ap>");
		masterXmlString.append("<bill_no><![CDATA[").append("").append("]]></bill_no>");
		masterXmlString.append("<bill_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></bill_date>");
		masterXmlString.append("<order_ref><![CDATA[").append("").append("]]></order_ref>");
		masterXmlString.append("<site_code><![CDATA[").append((siteCode==null?" ":siteCode.trim())).append("]]></site_code>");
		masterXmlString.append("<fin_entity><![CDATA[").append((finEntity==null?"":finEntity.trim())).append("]]></fin_entity>");
		masterXmlString.append("<curr_code><![CDATA[").append((currCode==null?"":currCode.trim())).append("]]></curr_code>");
		masterXmlString.append("<proj_code><![CDATA[").append("").append("]]></proj_code>");
		masterXmlString.append("<cr_term><![CDATA[").append("").append("]]></cr_term>");
		masterXmlString.append("<due_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></due_date>");
		masterXmlString.append("<bill_amt><![CDATA[").append("0").append("]]></bill_amt>");
		masterXmlString.append("<tax_class><![CDATA[").append((taxClass==null?"":taxClass.trim())).append("]]></tax_class>");
		masterXmlString.append("<tax_chap><![CDATA[").append((taxChap==null?"":taxChap.trim())).append("]]></tax_chap>");
		masterXmlString.append("<tax_env><![CDATA[").append((taxEnv==null?"":taxEnv.trim())).append("]]></tax_env>");
		masterXmlString.append("<tax_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></tax_date>");
		masterXmlString.append("<tax_amt><![CDATA[").append("0").append("]]></tax_amt>");  //cross update
		masterXmlString.append("<tot_amt><![CDATA[").append("0").append("]]></tot_amt>");  //cross update
		masterXmlString.append("<adv_amt><![CDATA[").append("0").append("]]></adv_amt>");
		masterXmlString.append("<acct_code__adv><![CDATA[").append("").append("]]></acct_code__adv>");
		masterXmlString.append("<cctr_code__adv><![CDATA[").append("").append("]]></cctr_code__adv>");
		masterXmlString.append("<net_amt><![CDATA[").append("0").append("]]></net_amt>");  //cross update
		masterXmlString.append("<net_amt__bc><![CDATA[").append("0").append("]]></net_amt__bc>");  //cross update
		masterXmlString.append("<auto_pay><![CDATA[").append("Y").append("]]></auto_pay>");
		masterXmlString.append("<pay_mode><![CDATA[").append("").append("]]></pay_mode>");
		masterXmlString.append("<bank_code><![CDATA[").append((bankCode==null?"":bankCode.trim())).append("]]></bank_code>");
		masterXmlString.append("<entry_batch_no><![CDATA[").append((entryBatchNo==null?" ":entryBatchNo.trim())).append("]]></entry_batch_no>");
		masterXmlString.append("<anal_code><![CDATA[").append("").append("]]></anal_code>");
		masterXmlString.append("<emp_code><![CDATA[").append("").append("]]></emp_code>");
		masterXmlString.append("<diff_amt__exch><![CDATA[").append("").append("]]></diff_amt__exch>");
		masterXmlString.append("<acct_code__cf><![CDATA[").append("").append("]]></acct_code__cf>");
		masterXmlString.append("<cctr_code__cf><![CDATA[").append("").append("]]></cctr_code__cf>");
		masterXmlString.append("<confirmed><![CDATA[").append("N").append("]]></confirmed>");
		masterXmlString.append("<conf_date><![CDATA[").append("").append("]]></conf_date>");
		masterXmlString.append("<emp_code__aprv><![CDATA[").append("").append("]]></emp_code__aprv>");
		masterXmlString.append("<site_descr><![CDATA[").append("").append("]]></site_descr>");
		masterXmlString.append("<chg_date/>");
		masterXmlString.append("<chg_user/>");
		masterXmlString.append("<chg_term/>");
		masterXmlString.append("<remarks><![CDATA[").append("").append("]]></remarks>");
		masterXmlString.append("<supp_bill_amt><![CDATA[").append("00").append("]]></supp_bill_amt>");
		masterXmlString.append("<exch_rate ><![CDATA[").append("1").append("]]></exch_rate>");
		masterXmlString.append("<rnd_off><![CDATA[").append("R").append("]]></rnd_off>");
		masterXmlString.append("<rnd_to><![CDATA[").append("1").append("]]></rnd_to>");
		masterXmlString.append("<rnd_amt><![CDATA[").append("00").append("]]></rnd_amt>");
		masterXmlString.append("<sundry_type__pay><![CDATA[").append("S").append("]]></sundry_type__pay>");	
		masterXmlString.append("<sundry_code__pay><![CDATA[").append((suppCode==null?" ":suppCode.trim())).append("]]></sundry_code__pay>"); 
		masterXmlString.append("<acct_code__pay><![CDATA[").append((acctPay==null?"":acctPay.trim())).append("]]></acct_code__pay>");
		masterXmlString.append("<cctr_code__pay><![CDATA[").append((cctrPay==null?"":cctrPay.trim())).append("]]></cctr_code__pay>");
		masterXmlString.append("<chq_name><![CDATA[").append("").append("]]></chq_name>");
		masterXmlString.append("</Detail1>");	
		custList = supp.getCustomerList();
		for(int i=0;i<custList.size();i++)
		{
			Customer custObj = (Customer)custList.get(i);
			empCode = custObj.getEmpCode();
			amt =   custObj.getAmount();
			acctCode =  custObj.getAcctCode();
			cctrCode =  custObj.getCctrCode();
			String lineNo = new Integer(++i).toString(); 
		
			masterXmlString.append("<Detail3 dbID=\"\" domID='"+lineNo+"'  objName=\"misc_voucher\"  objContext=\"3\">");
			masterXmlString.append("<attribute  pkNames=\"tran_id:line_no:\" status=\"N\" updateFlag=\"A\" selected=\"N\"/>");
			masterXmlString.append("<tran_id/>");
			masterXmlString.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
			masterXmlString.append("<tran_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></tran_date>");
			masterXmlString.append("<acct_code><![CDATA[").append((acctCode==null?" ":acctCode.trim())).append("]]></acct_code>");
			masterXmlString.append("<cctr_code><![CDATA[").append((cctrCode==null?" ":cctrCode.trim())).append("]]></cctr_code>");
			masterXmlString.append("<amount><![CDATA[").append(new Double(amt).toString()).append("]]></amount>");
			masterXmlString.append("<anal_code><![CDATA[").append("").append("]]></anal_code>");  //9/8/2006
			masterXmlString.append("<emp_code><![CDATA[").append(empCode).append("]]></emp_code>");
			masterXmlString.append("<apply_tax><![CDATA[").append("Y").append("]]></apply_tax>");
			masterXmlString.append("<tax_amt><![CDATA[").append("").append("]]></tax_amt>");
			masterXmlString.append("<remarks/>");
			masterXmlString.append("<sundry_type__for><![CDATA[").append("").append("]]></sundry_type__for>");
			masterXmlString.append("<sundry_code__for><![CDATA[").append("").append("]]></sundry_code__for>");
			masterXmlString.append("<acct_code__dr><![CDATA[").append("").append("]]></acct_code__dr>");
			masterXmlString.append("<cctr_code__dr><![CDATA[").append("").append("]]></cctr_code__dr>");
			masterXmlString.append("<acct_code__cr><![CDATA[").append("").append("]]></acct_code__cr>");
			masterXmlString.append("<cctr_code__cr><![CDATA[").append("").append("]]></cctr_code__cr>");
			masterXmlString.append("<prcp_id><![CDATA[").append("").append("]]></prcp_id>");
			masterXmlString.append("<line_no__rcp><![CDATA[").append("").append("]]></line_no__rcp>");
			masterXmlString.append("<item_code><![CDATA[").append("").append("]]></item_code>");
			masterXmlString.append("<quantity><![CDATA[").append("").append("]]></quantity>");
			masterXmlString.append("<rate><![CDATA[").append("").append("]]></rate>");
			masterXmlString.append("<rate__clg><![CDATA[").append("").append("]]></rate__clg>");
			masterXmlString.append("<bill_no><![CDATA[").append("").append("]]></bill_no>");
			masterXmlString.append("<bill_date><![CDATA[").append(getCurrdateInAppFormat()).append("]]></bill_date>");
			masterXmlString.append("<bill_amt><![CDATA[").append("").append("]]></bill_amt>");
			masterXmlString.append("<tax_class><![CDATA[").append((taxClass==null?" ":taxClass.trim())).append("]]></tax_class>");
			masterXmlString.append("<tax_chap><![CDATA[").append((taxChap==null?" ":taxChap.trim())).append("]]></tax_chap>");
			masterXmlString.append("<tax_env><![CDATA[").append((taxEnv==null?"":taxEnv.trim())).append("]]></tax_env>");
			masterXmlString.append("</Detail3>");	
		}
		masterXmlString.append("</Header0>");
		masterXmlString.append("</group0>");
		masterXmlString.append("</DocumentRoot>");
		String retString = null;
		try
		{
			PayRollVoucPrc payRollVoucPrcEJB = null;
			payRollVoucPrcEJB = new PayRollVoucPrc();//added for ejb3
		
			String convertXmlString = masterXmlString.toString();
			//writting going xmlstring to masterstatefull
			payRollVoucPrcEJB.writeXmlInFile(convertXmlString,"LEN",suppCode);
			//end
			String returnArrFromSave [] = payRollVoucPrcEJB.saveXmlData(convertXmlString,userId,loginSite);	
			for (int a =0 ;a<returnArrFromSave.length; a++)
			{
				retString = retString +returnArrFromSave[a];
				retString = retString +"@";
			}
			System.out.println("retString:::::::::::::"+retString);
			String tranIdG = retString.substring(retString.indexOf("@")+1,retString.length()-1);
			System.out.println("Return Transaction Id:::::::["+tranIdG+"]");
		//	payRollVoucPrcEJB.updateMiscVoucherDet(tranIdG,conn);
		//	payRollVoucPrcEJB.updateMiscVoucher(tranIdG,conn);  
		}
		catch(Exception e)
		{
			
			processErrString = "EXCEPTION";
			e.printStackTrace();
			
		}
		
	}
	
	private String makeTran(EncashCust encashCust,Suppliers supp,Connection conn)
	{
		String tranId =null;
		String custCode=null;
		Timestamp lapsOnDate = null;
		String  leaveCode=null;
		double amount=0;
		double balDay = 0;
		String adCode=null;
		String workSite = null;
		double enchDay=0;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String period = null;
		String sql =null;
	//	String leaveCode =null;
	//	String adcode =null;
	//	String tranId =null;
		
		try
		{
			sql = "SELECT CODE FROM PERIOD WHERE  ? BETWEEN  FR_DATE AND TO_DATE " ;
			System.out.println("Payroll_projdet:::["+sql+"]");
			pstmt = conn.prepareStatement(sql);
			System.out.println("Laps Date:{"+lapsOnDateTs+"]");
			pstmt.setTimestamp(1,lapsOnDateTs);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				period = rs.getString(1);
				System.out.println("Period is ["+period+"]");
			}
			rs.close();
			pstmt.close();
			//********setting period code
			supp.setPrdCode(period);
			
			custCode = encashCust.getCustCode();
			leaveCode = encashCust.getLeaveCode();
			amount = encashCust.getAmount();
			balDay =encashCust.getBalDay();
			adCode = encashCust.getAdCode();
			adCodeG = adCode;
			enchDay = encashCust.getEnchDay();
			workSite = encashCust.getWorkSite();
			lapsOnDate = encashCust.getLapsOnDate();
			tranId = generateTranId("W_EMPLEAVE",workSite,getCurrdateInAppFormat(),conn); 
			System.out.println("Generated Tarn Id ::["+tranId+"]");
			sql = "INSERT INTO EMPLEAVE "
				+"( TRAN_ID, EMP_CODE, LVE_DATE_FR, LVE_DATE_TO, LVE_CODE,"
				+"  ENCH_AMT, NO_DAYS, EMP_CODE__APRV, APRV_DATE,STATUS, "
				+" STATUS_DATE,  CHG_DATE, CHG_USER, CHG_TERM,USE_FLAG,"
				+"LEAVE_STARTS,LEAVE_ENDS, PRD_CODE, START_TIME, END_TIME)"
				+"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			pstmt.setString(2,custCode);
			pstmt.setTimestamp(3,lapsOnDate);
			pstmt.setTimestamp(4,lapsOnDate);
			pstmt.setString(5,leaveCode);
			pstmt.setDouble(6,amount);
			pstmt.setDouble(7,balDay);
			pstmt.setString(8,userId);
			pstmt.setTimestamp(9,new java.sql.Timestamp(System.currentTimeMillis()));
			pstmt.setString(10,"C");
			pstmt.setTimestamp(11,new java.sql.Timestamp(System.currentTimeMillis()));
			pstmt.setTimestamp(12,new java.sql.Timestamp(System.currentTimeMillis()));
			pstmt.setString(13,userId);
			pstmt.setString(14,chgTerm);
			pstmt.setString(15,"E");
			pstmt.setString(16,"B");
			pstmt.setString(17,"E");
			pstmt.setString(18,period);
			pstmt.setTimestamp(19,lapsOnDate);
			pstmt.setTimestamp(20,lapsOnDate);
			pstmt.executeUpdate();
			pstmt.close();
			//errcode = nvo_leave_encashment.gbf_leaves_approve(ls_mtranid,2,ls_payroll_or_voucher,ls_payroll_prd, 1)
			LeaveApprove(tranId,supp,conn);
			if(messagePrStr!=null)
			{
				return messagePrStr;
			}
			
		}
		catch(Exception e)
		{
			processErrString = "EXCEPTION";
			e.printStackTrace();
		}
		return messagePrStr;
		
	}
	public void LeaveApprove(String tranid,Suppliers supp,Connection conn)
	{
		
		
		PreparedStatement pstmt = null;
		ResultSet rs= null;
		PreparedStatement pstmtUpd = null;
		ResultSet rsUpd= null;
		String empCode = null;
		String leaveCode =null;
		Timestamp leaveDateFrom  = null;
		Timestamp leaveDateTo  = null;
		String status = null;
		double noDay = 0;
		String useFlag = null;
		double encashAmt =0;
		String prdCode =null;
		Timestamp aprvDate  = null;
		Timestamp leaveDate = null;
		String sql =null;
		String balOpt = null;
		String paybles =null;
		double nextBal = 0;
		double updateBal =0;
		double menchdays=0;
		String updSql =null;
		int count = 0;
		String acctCode = null;
		String cctrCode = null;
		double mcondays =0;
		String adCode = adCodeG;
		
		
		
		
		try
		{
			sql = "	SELECT EMP_CODE,LVE_CODE,LVE_DATE_FR,LVE_DATE_TO,STATUS, NO_DAYS, USE_FLAG, ENCH_AMT, PRD_CODE, APRV_DATE  "
				+	" FROM EMPLEAVE WHERE TRAN_ID = '"+tranid+"'";
				System.out.println("EMPLEAVE ::::-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				System.out.println("Record found for the transation in empleave.......................["+tranid+"]");
				empCode = rs.getString(1);
				leaveCode = rs.getString(2);
				leaveDateFrom  =rs.getTimestamp(3);
				leaveDateTo  = rs.getTimestamp(4);
				status = rs.getString(5);
				noDay = rs.getDouble(6);
				useFlag = rs.getString(7);;
				encashAmt =rs.getDouble(8);
				prdCode =rs.getString(9);
				aprvDate  = rs.getTimestamp(4);;
				
				
			}
			rs.close();
			pstmt.close();
			sql = "	SELECT PAYABLE, BAL_OPT FROM LEAVES WHERE LVE_CODE = '"+leaveCode+"'";
			System.out.println("LEAVES ::::-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				paybles = rs.getString(1);
				balOpt = rs.getString(2);
				System.out.println("LEAVES BALANCE OPTION:::["+balOpt+"]");
				
				
			}
			rs.close();
			pstmt.close();
			if(balOpt.equalsIgnoreCase("Y"))
			{
				Timestamp effDate  = null;
				Timestamp upDate  = null;
				Timestamp expDate  = null;
				double balDay  = 0;
	
				nextBal = noDay;
				
				sql = "SELECT EMP_CODE, LVE_CODE,EFF_DATE, UPTO_DATE,EXP_DATE, BAL_DAYS FROM LEAVEBAL "
					+"WHERE EMP_CODE = '"+empCode+"'AND LVE_CODE = '"+leaveCode+"' AND EXP_DATE >= ?"
					+"order by exp_date";
				System.out.println("LEAVEBAL QUERY ::::-"+sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setTimestamp(1,leaveDateFrom);
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					effDate = rs.getTimestamp(3);
					upDate = rs.getTimestamp(4);
					expDate =  rs.getTimestamp(5);
					balDay = rs.getDouble(6);
					
					if(balDay==0 && effDate.compareTo(leaveDateFrom)< 0 )continue;
					if(effDate.compareTo(leaveDateFrom)>0)break;
					if(balDay<nextBal)
					{
						updateBal =balDay;
					}
					else
					{
						updateBal = nextBal;	
					}
					if(useFlag.equalsIgnoreCase("E"))
					{
						mcondays = 0;
						menchdays = updateBal;
					}
					if(useFlag.equalsIgnoreCase("C"))
					{
						mcondays = 0;
					}
					updSql = " UPDATE LEAVEBAL 	SET BAL_DAYS   = BAL_DAYS - ?,CONS_DAYS  = CONS_DAYS + ?, ENCH_DAYS  = ENCH_DAYS + ?"
						+" WHERE EMP_CODE ='"+empCode+"'AND LVE_CODE ='"+leaveCode+"'AND EFF_DATE = ? AND UPTO_DATE = ?";
					System.out.println("Update leaveBalanceLeave sql:::"+updSql);
					pstmtUpd = conn.prepareStatement(updSql);
					pstmtUpd.setDouble(1,updateBal);
					pstmtUpd.setDouble(2,menchdays);
					pstmtUpd.setDouble(3,menchdays);
					pstmtUpd.setTimestamp(4,effDate);
					pstmtUpd.setTimestamp(5,upDate);
					System.out.println("Update The leaveBal:::["+pstmtUpd.executeUpdate()+"]");
					pstmtUpd.close();
					// update leave balances trace table.
					updSql = "SELECT COUNT(*) FROM EMPLVE_TRACE	WHERE TRAN_ID      = '"+tranid+"'   AND EFF_DATE    = ?	AND  UPTO_DATE   = ? ";
					pstmtUpd = conn.prepareStatement(updSql);
					pstmtUpd.setTimestamp(1,effDate);
					pstmtUpd.setTimestamp(2,upDate);
					rsUpd = pstmtUpd.executeQuery();
					if(rsUpd.next())
					{
						count  = rsUpd.getInt(1);
					}
					System.out.println("Count :::::::["+count+"]");
					rsUpd.close();
					pstmtUpd.close();
					if(count == 0)
					{
						updSql = "	INSERT INTO EMPLVE_TRACE  (TRAN_ID , EMP_CODE, LVE_DATE_FR, LVE_DATE_TO, LVE_CODE,"
							+"  EFF_DATE, UPTO_DATE, LVES_ALLOC)VALUES (?, ?,?,?,?,?,?,?)" ;
						
						pstmtUpd = conn.prepareStatement(updSql);
						pstmtUpd.setString(1,tranid);
						pstmtUpd.setString(2,empCode);
						pstmtUpd.setTimestamp(3,leaveDateFrom);
						pstmtUpd.setTimestamp(4,leaveDateTo);
						pstmtUpd.setString(5,leaveCode);
						pstmtUpd.setTimestamp(6,effDate);
						pstmtUpd.setTimestamp(7,upDate);
						pstmtUpd.setDouble(8,updateBal);
						System.out.println("Insert in to emplve_trace["+pstmtUpd.executeUpdate()+"]");
						pstmtUpd.close();
					}
					else
					{
						sql = "UPDATE EMPLVE_TRACE 	SET LVES_ALLOC = ? WHERE TRAN_ID = '"+tranid+"'   AND EFF_DATE  = ? "
						+"	AND UPTO_DATE   = ?" ;
						pstmtUpd = conn.prepareStatement(updSql);
						pstmtUpd.setDouble(1,updateBal);
						pstmtUpd.setString(2,tranid);
						pstmtUpd.setTimestamp(3,effDate);
						pstmtUpd.setTimestamp(4,upDate);
						System.out.println("Update in to emplve_trace["+pstmtUpd.executeUpdate()+"]");
						pstmtUpd.close();
							
					}
					
					nextBal = nextBal - updateBal;
					
					if(nextBal == 0)break;
				
				}//end while
				rs.close();
				pstmt.close(); 
				
			}//balopy =y
			leaveDate = leaveDateFrom;
			
		//	String prdCode = null;
			String prdSql = null;
			System.out.println("leaveDate:::["+leaveDate+"]");
			System.out.println("leaveDateTo:::["+leaveDateTo+"]");
	 		System.out.println("leaveDateFrom:::["+leaveDateFrom+"]");
			
			while(leaveDate.compareTo(leaveDateTo)<=0)
			{
			 	java.sql.Timestamp toDate = null;
				java.sql.Timestamp frDate  = null;
				prdSql = "SELECT CODE ,fr_date, to_date FROM PERIOD WHERE  ? BETWEEN  FR_DATE AND TO_DATE " ;
				System.out.println("Period Query:::::["+prdSql+"]");
				pstmtUpd = conn.prepareStatement(prdSql);
				System.out.println("Laps Date:{"+leaveDate+"]");
				pstmtUpd.setTimestamp(1,leaveDate);
				rsUpd= pstmtUpd.executeQuery();
				if(rsUpd.next())
				{
					prdCode = rsUpd.getString(1);
					frDate = rsUpd.getTimestamp(2);
					toDate = rsUpd.getTimestamp(3);
					
					System.out.println("Period is ["+prdCode+"]");
					 
				}
				rsUpd.close();
				pstmtUpd.close();
				leaveDateTo = leaveDateFrom;
				
				//lc_no_days = noDay;
				prdSql ="INSERT INTO EMPLEAVE_SUMMARY  (TRAN_ID , EMP_CODE, LVE_DATE_FR, LVE_DATE_TO, LVE_CODE, NO_DAYS, "
					+" PRD_CODE,USE_FLAG ) 	VALUES(?,?,?,?,?,?,?,?)";
				System.out.println("EMPLEAVE_SUMMARY Query:::::["+prdSql+"]");	
				
				System.out.println("TRAN_ID ["+tranid+"]");
				System.out.println("PRD_CODE ["+prdCode+"]");
				System.out.println("USE_FLAG ["+useFlag+"]");
				System.out.println("leaveDateFrom ["+leaveDateFrom+"]");
				System.out.println("noDay ["+noDay+"]");
				System.out.println("leaveCode ["+leaveCode+"]");
				System.out.println("empCode ["+empCode+"]");
				
			
				pstmtUpd = conn.prepareStatement(prdSql);
				pstmtUpd.setString(1,tranid);
				pstmtUpd.setString(2,empCode.trim());
				pstmtUpd.setTimestamp(3,leaveDateFrom);
				pstmtUpd.setTimestamp(4,leaveDateTo);
				pstmtUpd.setString(5,leaveCode);
				pstmtUpd.setDouble(6,noDay);
				pstmtUpd.setString(7,prdCode.trim());
				pstmtUpd.setString(8,useFlag.trim());
				System.out.println("INSERT in to empleave_summary["+pstmtUpd.executeUpdate()+"]");
				pstmtUpd.close(); 
				leaveDate = getAddDateInTimestamp(toDate,1);
				System.out.println("After Adding 1 (leaveDate):::["+leaveDate+"]");
			}
		 	sql = "SELECT ACCT_CODE__SAL,CCTR_CODE__SAL FROM EMPLOYEE WHERE EMP_CODE='"+empCode+"'" ;
			System.out.println("empQuery="+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{ 
				acctCode = rs.getString(1);
				cctrCode = rs.getString(2);
				if(cctrCode!=null)System.out.println("acctCode::["+acctCode+"]");	
				if(cctrCode!=null)System.out.println("cctrCode:::["+cctrCode+"]");	
				System.out.println("empCode :::::["+empCode+"]");	
			}
			rs.close();
			pstmt.close();
			if(acctCode == null)
			{
				sql = "SELECT ACCT_CODE__SAL,CCTR_CODE__SAL FROM ALLWDEDN 	WHERE AD_CODE = '"+adCode+"'";
				System.out.println("allwdedn sql ::::::::::["+sql+"'");
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					acctCode = rs.getString(1);
					cctrCode = rs.getString(2);
						if(cctrCode!=null)System.out.println("acctCode(ALLWDEDN)::["+acctCode+"]");	
						if(cctrCode!=null)System.out.println("cctrCode(ALLWDEDN):::["+cctrCode+"]");	
				}
				rs.close();
				pstmt.close();
			}
			Customer cust = new Customer();
			cust.setAmount(encashAmt);
			cust.setEmpCode(empCode);
			if(acctCode == null)
			{
				messagePrStr = "No Acctcode In Employee["+empCode+"] In Employee and Adcode in ALLWDEDN for adCode["+adCode+"]";
				return;
			}
			cust.setAcctCode(acctCode);
			cust.setCctrCode(cctrCode);
			supp.setCustomerList(cust);
		}//end of try
		catch(Exception e)
		{
			processErrString = "EXCEPTION";
			e.printStackTrace();
		}
	
	}
	//TO FIND WHEATHER A CONTRACTOR CODE PRESENT IN LIST OR NOT
	private int getExistanceOfContCode(ArrayList contList,String contCode)
	{
			int ctr = 0;
			int retVal = -1;
			EncashSupp encashSupp;
			for (ctr = 0;ctr < contList.size();ctr++)
			{
				encashSupp = (EncashSupp)contList.get(ctr);
				if (encashSupp.getContCode().trim().equalsIgnoreCase(contCode.trim()))
				{
					retVal = ctr;
					break;
				}
			}
			return retVal;
	}

//GENERATING TRAN ID 
private String generateTranId(String windowName,String tranDate,String siteCode,Connection conn)
{
	
		Statement stmt = null;
		ResultSet rs = null;
		String sql = null;
		String tranId = null;
		String xmlValues = null;
		 try
	     {
	    	sql = "SELECT KEY_STRING, TRAN_ID_COL, REF_SER FROM TRANSETUP WHERE UPPER(TRAN_WINDOW)= '"+windowName+"'";
	    	System.out.println("Tran generator Sql"+sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			System.out.println("keyString :"+rs.toString());
			String tranSer1 = "";
			String keyString = "";
			String keyCol = "";
			if (rs.next())
			{
				keyString = rs.getString(1);
				keyCol = rs.getString(2);
				tranSer1 = rs.getString(3);				
			}
			System.out.println("keyString :"+keyString);
			System.out.println("keyCol :"+keyCol);
			System.out.println("tranSer1 :"+tranSer1);
			xmlValues ="<?xml version=\"1.0\" encoding=\"utf-8\"?><Root>";
			xmlValues = xmlValues + "<Header></Header>";
			xmlValues = xmlValues + "<Detail1>";
			xmlValues = xmlValues +	"<tran_id></tran_id>";
			xmlValues = xmlValues + "<site_code>" + siteCode + "</site_code>";
			xmlValues = xmlValues + "<tran_date>"+ tranDate + "</tran_date>";
			xmlValues = xmlValues + "</Detail1></Root>";
			System.out.println("xmlValues  :["+xmlValues+"]");
			TransIDGenerator tg = new TransIDGenerator(xmlValues, "BASE", CommonConstants.DB_NAME);
			tranId = tg.generateTranSeqID(tranSer1, keyCol, keyString, conn);
			System.out.println("tranId :"+tranId);
		}
		catch (SQLException ex)
		{
			processErrString = "Exception";
			ex.printStackTrace();
		}
		catch (Exception e)
		{
			processErrString = "Exception";
			e.printStackTrace();
		}
		return tranId;
	}
//CONVERTING APPLICATION DATE TO DB DATE AND VICE VERSA
	public String getValidDateTimeString(String dateTimeStr, String sourceDateTimeFormat, String targetDateTimeFormat) throws ITMException
	{
		System.out.println("Getting the valid datetime string for dateTimeStr :"+dateTimeStr+": which is in format :"+sourceDateTimeFormat);
		Object date = null;
		String retDateStr = "";
		try
		{
			if (!(sourceDateTimeFormat.equalsIgnoreCase(targetDateTimeFormat)))
			{
				if (sourceDateTimeFormat.indexOf("/") != -1)
				{
					dateTimeStr.replace('/', '-');
				}
				else if (sourceDateTimeFormat.indexOf(".") != -1)
				{
					dateTimeStr.replace('.', '-');
				}
				date = new SimpleDateFormat(sourceDateTimeFormat).parse(dateTimeStr);
				SimpleDateFormat sdfOutput = new SimpleDateFormat(targetDateTimeFormat);
				retDateStr = sdfOutput.format(date);
			}
			else
			{
				retDateStr = dateTimeStr;
			}
		}
		catch (Exception e)
		{
			System.out.println("Exception :GenericUtility :getValidDateString :==>"+e.getMessage());
			throw new ITMException(e);
		}
		System.out.println("retDateStr :"+retDateStr);
		return retDateStr;
	}
	private Timestamp getAddDateInTimestamp(Timestamp date, int days)
	{
		java.sql.Timestamp retTimestamp = null;
		try
		{
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getDispDateFormat());
			Calendar  cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE,days);
			java.util.Date date1 = cal.getTime();
			long time = date1.getTime();
			retTimestamp= new java.sql.Timestamp(time);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		return retTimestamp;
	}//end of getAddDateInTimestamp

//GETTING CURRENT DATE IN APPLICATION DATE FORMAT
private String getCurrdateInAppFormat()
{
	String currAppdate =null;
	java.sql.Timestamp currDate = null;
	Object date = null;
	SimpleDateFormat DBDate=null;
	try
	{
			
			currDate =new java.sql.Timestamp(System.currentTimeMillis()) ;
			System.out.println(genericUtility.getDBDateFormat());
		 	DBDate= new SimpleDateFormat(genericUtility.getDBDateFormat());
			date = DBDate.parse(currDate.toString());
			currDate =	java.sql.Timestamp.valueOf(DBDate.format(date).toString() + " 00:00:00.0");
			currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate).toString();
	}
	catch(Exception e)
	{
		System.out.println("Exception in  getCurrdateInAppFormat:::"+e.getMessage());
	}
	return (currAppdate);
}

}//END OF EJB