
package ibase.webitm.ejb.adm;

import ibase.utility.CommonConstants;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.TransIDGenerator;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

@javax.ejb.Stateless
@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
public class PerkClaimProcessPostSave extends ValidatorEJB //implements SessionBean
{	
	public String postSave()throws RemoteException,ITMException
	{
		return "";
	}

	public String postSave( String domString, String editFlag, String xtraParams, Connection conn ) throws RemoteException,ITMException
	{
		String retString = "";
		String errString = "";
		String objName = "";
		
		String sql = "";
		String sql1 = "";
		ResultSet rs = null; 
		ResultSet rs2 = null; 
		PreparedStatement pstmt = null;
		PreparedStatement pstmt1 = null;
		PreparedStatement pstmt2 = null;
		PreparedStatement adclaimhdrPstmt = null;
		PreparedStatement adclaimPstmt = null;
		PreparedStatement allowdednbalPstmt = null;
		
		Document dom = null;		
		
		boolean isError = false;
		boolean isLocalConn = false;
		
		int count = 0;
		int updateCnt = 0;
		
		String payrollVoucher = "";
		String payrollPrd = "";
		String adCode = "";
		String adCode1 = "";
		String contVouch = "";
		String pyrlAdclaim = "";
		String fromDate = "";
		String  toDate = "";
		String keyString = "";
		String keyStr = "";
		String bankCode = "";
		String payModeParm = "";
		String bankEnv ="";
		String empCode = "";
		String tranDate = "";
		String refNo = "";
		String balAmt = "";
		String claimAmt = "";
		String loanAdjamt = "";
		String effDate = "";
		String endDate = "";
		Double balAmount = 0.0d;
		Double claimAount  = 0.0d;
		Double loanAdjamount = 0.0d;
		Double clpaid = 0.0d;  
		String lsSite = "";
		String lsFinent = "";
		String lsDept = "";
		String lsPaymode = "";
		String lsCurr = "";
		String lsBank = "";
		String tranId = "";
		String lsAcctpay = "";
		String lsCctrpay = "";
		java.util.Date ldStart = null;
		java.util.Date ldEnd = null;
		String claimPrd = "";
		String startDate = "";
		String endDate1 = "";
		String effDate1 = "";
		String lsRefNo = "";   
		String lsSchemeno = "";
		String lcInterest = "";
		String lcMonInst = "";
		String lcLoanAmt = "";
		String lsRemarks = "";
		String recoNo = "";
		String lsLoanAd = "";
		String loanAmt = "";
		String adjAmt = "";
		String ld_end_date = "";
		String ld_start_date = "";
		String lc_interest = "";
		String ll_line = "";
		String tranIdDom = "";
		String ediOption = "";
		String columnValue = "";
		String acctSite = "";
		String siteCode = "";
		String empFName = "";
		String empMName = "";
		String empLName = "";
		String empName = "";
		String userXtraParms = "";
		String month =  "";
		
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		
		ArrayList<String> errList = new ArrayList<String>();
		ArrayList<String> errFields = new ArrayList<String>();
		HashMap payrHdr =  new HashMap();
		HashMap payrvDet =  new HashMap();
		ArrayList payDetList = new ArrayList();
		HashMap<String, HashMap<String , String>> detail3HashMap = new HashMap<String, HashMap<String, String>>();
		HashMap<String, HashMap<String , String>> detail2HashMap = new HashMap<String, HashMap<String, String>>();
		
		HashMap parentHashMap = new HashMap();
		System.out.println("domString PerkClaimProcessPostSave ["+domString+"]");		

		// Modified by Piyush on 02/06/2016[To add data in emp_adclaimdet table].Start
		String ldLoanStartDate = null;
		PreparedStatement adclaimdetInsStmt = null;
		int noRowInst = 0;
		// Modified by Piyush on 02/06/2016[To add data in emp_adclaimdet table].End

		try
		{
			conn.setAutoCommit( false );

			GenericUtility genericUtility = GenericUtility.getInstance();
			ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();		
			AdmCommon admCommon = new AdmCommon();
			
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			
			System.out.println("domString PerkClaimProcessPostSave ["+domString+"]");			
			
			if(domString != null && domString.length() > 0)
			{
				dom = genericUtility.parseString(domString);
			}
			
			System.out.println("xtraParams..["+xtraParams+"]");
			
			if(xtraParams == null || xtraParams.length() == 0)
			{
				xtraParams = checkNull(genericUtility.getColumnValue("user_xtra_parms", dom, "1"));
				System.out.println("userXtraParms["+xtraParams+"]");
			} 
			System.out.println("xtraParams.new .["+xtraParams+"]");
			
			String userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
		//	userId = "SYSTEM";
			String chgTerm = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "termId");
		//	chgTerm = "SYSTEM";
			
			objName  = getObjNameFromDom( dom, "objName");
			System.out.println("objName["+objName+"]");
			
			payrollVoucher = checkNull(genericUtility.getColumnValue("payroll_voucher", dom, "1"));
			payrollPrd = checkNull(genericUtility.getColumnValue("payroll_prd", dom, "1"));
			adCode = checkNull(genericUtility.getColumnValue("ad_code", dom, "1"));
			contVouch = checkNull(genericUtility.getColumnValue("cont_vouch", dom, "1"));
			
			System.out.println("payrollVoucher [" + payrollVoucher + "]");   
			System.out.println("payrollPrd [" + payrollPrd + "]");
			System.out.println("adCode [" + adCode + "]");
			System.out.println("contVouch [" + contVouch + "]");

			if(contVouch.equalsIgnoreCase("Y"))
			{
				errString = gbfWebserviceCall("w_claim_proc", domString, domString, xtraParams, conn);
			}
			else
			{
				if(payrollVoucher.equalsIgnoreCase("P"))
				{
					pyrlAdclaim = checkNull(admCommon.getEnv("999999",adCode+"_CLAIMAD", conn));
					System.out.println("pyrlAdclaim ["+pyrlAdclaim+"]");
					
					if(pyrlAdclaim == "NULLFOUND" || pyrlAdclaim == null || pyrlAdclaim.length() == 0)
					{
						//errString = "VSENVAR1";
						errString = itmDBAccess.getErrorString("","VSENVAR1","","",conn);
						isError = true;
						return errString;
					}
						
						sql  = "select fr_date , to_date from period where code = ? ";
						
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,payrollPrd);
						
						rs = pstmt.executeQuery();
						
						if(rs.next())
						{
							fromDate = checkNull(rs.getString("fr_date"));
							toDate = checkNull(rs.getString("to_date"));
						}
						else
						{
							//errString = "VTPED1";
							errString = itmDBAccess.getErrorString("","VTPED1","","",conn);
						}						
						if( rs != null)
						{
							rs.close();
							rs = null;
						}
						if( pstmt != null)
						{
							pstmt.close();
							pstmt = null;
						}
						System.out.println("fromDate::::::"+fromDate);
						System.out.println("toDate::::::"+toDate);
						
						if(errString != null && errString.trim().length() > 0)
						{
							isError = true;
							return errString ;
						}
					//}
				}
				
			//	String month = payrollPrd.substring( payrollPrd.length(), payrollPrd.length() - 2 ) ;
				if(payrollPrd != null && payrollPrd.length() > 0)
				{
					 month = payrollPrd.substring( payrollPrd.length() - 2 ,payrollPrd.length()) ;
				}
				
				sql = "select key_string as keystr from transetup where upper(tran_window) = 'W_LOAN_RECO'";
				
				pstmt = conn.prepareStatement(sql);
				
				rs = pstmt.executeQuery();
				
				if(rs.next())
				{
					keyStr = checkNull(rs.getString("keystr"));
				}
				else
				{
					//errString = "VTSEQ1";
					errString = itmDBAccess.getErrorString("","VTSEQ1","","",conn);
				}
				System.out.println("keyStr"+keyStr);
				
				if( rs != null)
				{
					rs.close();
					rs = null;
				}
				if( pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(errString != null && errString.trim().length() > 0)
				{
					isError = true;
					return errString ;
				}
				
				bankCode = checkNull(genericUtility.getColumnValue("bank_code", dom, "1"));
				payModeParm = checkNull(genericUtility.getColumnValue("pay_mode", dom, "1"));
				
				bankEnv =  checkNull(admCommon.getEnv("999999","BANKCODE", conn));
				System.out.println("bankEnv ["+bankEnv+"]");
				
				if(bankEnv.equalsIgnoreCase("NULLFOUND"))
				{
					//errString = "VSENVAR1";
					errString = itmDBAccess.getErrorString("","VSENVAR1","","",conn);
					isError = true;
					return errString;
				}
				
				sql = "select key_string as keystr from transetup where upper(tran_window) = 'T_EMP_ADCLAIM'";
				
				pstmt = conn.prepareStatement(sql);
				
				rs = pstmt.executeQuery();
				
				if(rs.next())
				{
					keyString = checkNull(rs.getString("keystr"));
				}
				else
				{
					//errString = "VTSEQ";
					errString = itmDBAccess.getErrorString("","VTSEQ","","",conn);
				}
				System.out.println("keyString"+keyString);
				
				if( rs != null)
				{
					rs.close();
					rs = null;
				}
				if( pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(errString != null && errString.trim().length() > 0)
				{
					isError = true;
					return errString ;
				}
				
				sql = "INSERT INTO EMP_ADCLAIMHDR (TRAN_ID, TRAN_DATE, EMP_CODE, CLAIM_AMT, LOAN_ADJAMT, PAYROLL_VOUCHER, PRD_CODE__PAYROLL, CHG_DATE, CHG_USER, CHG_TERM, AD_CODE ,CONFIRMED) " +
						"VALUES ( ?  ,  ?  ,  ?  ,  ? ,  ?  ,  ? ,  ?  ,  ? ,  ? ,  ? ,  ?  ,'Y') ";
				adclaimhdrPstmt = conn.prepareStatement(sql);

				sql = "";
				// Modified by Piyush on 02/06/2016 [To update chg_date,chg_user,chg_term in emp_adclaim table].Start
				/*
				sql = "INSERT INTO EMP_ADCLAIM (TRAN_ID, EMP_CODE, AD_CODE, EFF_DATE, BAL_AMT, CLAIM_AMT)" +
						" VALUES ( ? ,  ?  , ? ,  ? ,  ? ,  ?)";
				*/
				sql = "INSERT INTO EMP_ADCLAIM (TRAN_ID, EMP_CODE, AD_CODE, EFF_DATE, BAL_AMT, CLAIM_AMT, CHG_DATE, CHG_USER, CHG_TERM)" +
						" VALUES ( ? ,  ?  , ? ,  ? ,  ? ,  ? , ? , ? , ? )";
				// Modified by Piyush on 02/06/2016 [To update chg_date,chg_user,chg_term in emp_adclaim table].End

				adclaimPstmt = conn.prepareStatement(sql);
				
				sql = "UPDATE ALLOWDEDN_BAL SET 	AMT_BAL	= AMT_BAL - ? ,AMT_ADJ  = AMT_ADJ + ? "
						+"	WHERE EMP_CODE = ?   AND AD_CODE    = ?  AND START_DATE = ?  AND END_DATE   = ?";
				allowdednbalPstmt = conn.prepareStatement(sql);

				// Modified by Piyush on 02/06/2016[To insert data in emp_adclaimdet].Start
				String adclaimdetInsSql = "INSERT INTO EMP_ADCLAIMDET(TRAN_ID, LINE_NO, AD_CODE, EFF_DATE, BAL_AMT, ADJ_AMT, REF_TYPE, REF_NO) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
				adclaimdetInsStmt = conn.prepareStatement(adclaimdetInsSql);
				// Modified by Piyush on 02/06/2016[To insert data in emp_adclaimdet].End

				sql = "";
				sql = "select edi_option as ls_edi_option from transetup where tran_window = ?  ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,"w_perk_claim");
				rs = pstmt.executeQuery();
				
				if(rs.next())
				{
					ediOption = checkNull(rs.getString("ls_edi_option"));
				}
				System.out.println("ediOption::"+ediOption);
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}

				detail2HashMap = getDetail2Data(dom);
				System.out.println("detail2HashMap:::::"+detail2HashMap);
				
				Iterator it1 = detail2HashMap.keySet().iterator();
				while (it1.hasNext()) 
				{
					String key1 = (String) it1.next();
					
					empCode = detail2HashMap.get(key1).get("empCode");
					adCode1 = detail2HashMap.get(key1).get("adCode1");
					tranDate = detail2HashMap.get(key1).get("tranDate");
					balAmt = detail2HashMap.get(key1).get("balAmt");
					claimAmt = detail2HashMap.get(key1).get("claimAmt");
					loanAdjamt = detail2HashMap.get(key1).get("loanAdjamt");
					effDate = detail2HashMap.get(key1).get("effDate");
					endDate = detail2HashMap.get(key1).get("endDate");
					
					System.out.println("empCode::::::::["+empCode+"]");
					System.out.println("adCode1 ..["+adCode1+"]");
					System.out.println("tranDate::::::::["+tranDate+"]");
					System.out.println("adCode1 ..["+balAmt+"]");
					System.out.println("claimAmt ..["+claimAmt+"]");
					System.out.println("loanAdjamt ..["+loanAdjamt+"]");
					System.out.println("effDate ..["+effDate+"]");
					System.out.println("endDate ..["+endDate+"]");
					
					if (balAmt.length() > 0)
					{
						balAmount = Double.parseDouble(balAmt);
					}
					
					if (claimAmt.length() > 0)
					{
						claimAount  = Double.parseDouble(claimAmt);
					}
				
					if (loanAdjamt.length() > 0)
					{
						loanAdjamount = Double.parseDouble(loanAdjamt);
						
					}
					
					clpaid = (claimAount - loanAdjamount);
					
					System.out.println("balAmount["+balAmount);
					System.out.println("claimAount["+claimAount);
					System.out.println("loanAdjamount["+loanAdjamount);
					System.out.println("clpaid["+clpaid);
					
					if(balAmount < 0)
					{
						loanAdjamount = 0.0;
						if(claimAount > 0)
						{
							//errString = "VTCLAIMAM1";
							errString = itmDBAccess.getErrorString("","VTCLAIMAM1","","",conn);
							isError = true;
							return errString;
						}
						else 
						{
							if(claimAount < balAmount)
							{
								//errString = "VTCLAIMAM2";
								errString = itmDBAccess.getErrorString("","VTCLAIMAM2","","",conn);
								isError = true;
								return errString;
							}
						}
					}
					else if(claimAount < 0 || claimAount > balAmount)
					{
						//errString = "VTCLAIMAMT";
						errString = itmDBAccess.getErrorString("","VTCLAIMAMT","","",conn);
						isError = true;
						return errString;
					}
					
					sql = "select pay_site as ls_site, site.fin_entity as ls_finent, dept_code as ls_dept, pay_mode as ls_paymode," +
							" curr_code as ls_curr, employee.bank_code as ls_bank from employee, site, finent " +
							"where employee.pay_site = site.site_code and site.fin_entity = finent.fin_entity and emp_code = ? ";
					
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,empCode);
					
					rs = pstmt.executeQuery();
					
					if(rs.next())
					{
						lsSite = checkNull(rs.getString("ls_site"));
						lsFinent = checkNull(rs.getString("ls_finent"));
						lsDept = checkNull(rs.getString("ls_dept"));
						lsPaymode = checkNull(rs.getString("ls_paymode"));
						lsCurr = checkNull(rs.getString("ls_curr"));
						lsBank = checkNull(rs.getString("ls_bank"));
					}
					else
					{
						//errString = "VMEMPCD";
						errString = itmDBAccess.getErrorString("","VMEMPCD","","",conn);
					}
					System.out.println("lsSite"+lsSite);
					System.out.println("lsFinent"+lsFinent);
					System.out.println("lsDept"+lsDept);
					System.out.println("lsPaymode"+lsPaymode);
					System.out.println("lsCurr"+lsCurr);
					System.out.println("lsBank"+lsBank);
					
					if( rs != null)
					{
						rs.close();
						rs = null;
					}
					if( pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					if(errString != null && errString.trim().length() > 0)
					{
						isError = true;
						return errString ;
					}
					
					/*tranDate = checkNull(genericUtility.getColumnValue("claim_date", dom, "1"));
					System.out.println("tranDate:::::"+tranDate);*/
					
					String xmlValues = "";
					xmlValues ="<?xml version=\"1.0\" encoding=\"utf-8\"?><Root>";
					xmlValues = xmlValues + "<Header></Header>";
					xmlValues = xmlValues + "<Detail1>";
					xmlValues = xmlValues +		"<tran_id></tran_id>";
					xmlValues = xmlValues +		"<tran_date>" + tranDate + "</tran_date>";
					xmlValues = xmlValues +		"<site_code>" + lsSite + "</site_code>"; 
					xmlValues = xmlValues + "</Detail1></Root>";
					
					System.out.println("xmlValues  :["+xmlValues+"]");
					
					System.out.println( "userId.. ["+userId+"]" );
					
				//	TransIDGenerator tg = new TransIDGenerator(xmlValues, userId, CommonConstants.DB_NAME);
					TransIDGenerator tg = new TransIDGenerator(xmlValues, "SYSTEM", CommonConstants.DB_NAME);
					tranId = tg.generateTranSeqID("ECLAIM", "tran_id", keyString, conn);
					
					System.out.println("tranId:::::::"+tranId);
					
					if(bankCode != null && bankCode.length() > 0)
					{
						lsBank = bankCode ;
					}
					if(payModeParm != null && payModeParm.length() > 0)
					{
						lsPaymode = payModeParm ;
					}
					
					if(lsBank == null || lsBank.length() == 0 )
					{
						lsBank = bankEnv ;
					}
					
					tranDate = genericUtility.getValidDateTimeString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
					System.out.println("tranDate..of detail2..["+tranDate+"]");
					
				// fromDate = genericUtility.getValidDateTimeString(fromDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
					System.out.println("fromDate....["+fromDate+"]");
					
				//	toDate = genericUtility.getValidDateTimeString(toDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
					System.out.println("toDate....["+toDate+"]");
					
					Date currentDate = new Date();
					
					Date currentDate1 = new Date();
					String curDt = sdf.format(currentDate1);
					java.util.Date curDate1 = sdf.parse(curDt);
					
					String curDate = sdf.format(currentDate);
					curDate = genericUtility.getValidDateTimeString(curDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
					System.out.println("curDate::::::"+curDate);
					
					System.out.println("effDate:::"+effDate);
					if(effDate != null && effDate.length() > 0)
					{
						ldStart = sdf.parse(effDate);
						System.out.println("ldStart::::::"+ldStart);
					}
					System.out.println("endDate:::"+endDate);
					if(endDate != null && endDate.length() > 0)
					{
						ldEnd = sdf.parse(endDate);	
						System.out.println("ldEnd::::::"+ldEnd);
					}
					if(payrollVoucher.equalsIgnoreCase("P"))
					{
						if(clpaid > 0)
						{
							sql = "select count(*) as cnt from employee_mthad where emp_code = ? and ad_code = ? and eff_date = ? and exp_date = ? ";
							
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,empCode);
							pstmt.setString(2,pyrlAdclaim);
							pstmt.setTimestamp(3,java.sql.Timestamp.valueOf(fromDate));
							pstmt.setTimestamp(4,java.sql.Timestamp.valueOf(toDate));
							
							rs = pstmt.executeQuery();
							
							if(rs.next())
							{
								count = rs.getInt("cnt");
							}
							System.out.println("count"+count);
							
							if(count > 0)
							{
								sql = "update employee_mthad set type = 'F', amount =amount + ?, ref_type  = 'C', ref_no = ? , " +
										"prop_yn = 'N' where emp_code = ? and ad_code = ? and eff_date = ? and exp_date = ? ";
								
								pstmt = conn.prepareStatement(sql);
								
								pstmt.setDouble(1,claimAount);
								pstmt.setString(2,tranId);
								pstmt.setString(3,empCode);
								pstmt.setString(4,pyrlAdclaim);
								pstmt.setTimestamp(5,java.sql.Timestamp.valueOf(fromDate));
								pstmt.setTimestamp(6,java.sql.Timestamp.valueOf(toDate));
								
								updateCnt = pstmt.executeUpdate();
								
							}
							else
							{
								sql = "insert into employee_mthad (emp_code, month_code, ad_code, eff_date, exp_date, type, amount, chg_date, chg_user, chg_term, ref_type, ref_no, prop_yn) " +
										"Values(? , ? , ? , ? , ? ,'F',?, ? , ? , ? ,'C', ? ,'N')";
								
								pstmt= conn.prepareStatement(sql);
								
								pstmt.setString(1,empCode);
								pstmt.setString(2,month);
								pstmt.setString(3,pyrlAdclaim);
								pstmt.setTimestamp(4,java.sql.Timestamp.valueOf(fromDate));
								pstmt.setTimestamp(5,java.sql.Timestamp.valueOf(toDate));
								pstmt.setDouble(6,claimAount);
								pstmt.setTimestamp(7,java.sql.Timestamp.valueOf(curDate));
								pstmt.setString(8,userId); 
							//	pstmt.setString(8,"SYSTEM");
								pstmt.setString(9,chgTerm);
							//	pstmt.setString(9,"SYSTEM");
								pstmt.setString(10,tranId);					
								
								pstmt.executeQuery();
							
							}
							}
						}
						else 
						{
							//Create record in payr_voucher
							
							sql = "select pay_site as ls_site, site.fin_entity as ls_finent, dept_code as ls_dept," +
									" curr_code as ls_curr  from employee, site, finent " +
									"where employee.pay_site = site.site_code and site.fin_entity = finent.fin_entity and emp_code = ? ";
					
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,empCode);
							
							rs = pstmt.executeQuery();
							
							if(rs.next())
							{
								lsSite = checkNull(rs.getString("ls_site"));
								lsFinent = checkNull(rs.getString("ls_finent"));
								lsDept = checkNull(rs.getString("ls_dept"));
								lsCurr = checkNull(rs.getString("ls_curr"));
							}
							System.out.println("lsSite"+lsSite);
							System.out.println("lsFinent"+lsFinent);
							System.out.println("lsDept"+lsDept);
							System.out.println("lsCurr"+lsCurr);
							if( rs != null)
							{
								rs.close();
								rs = null;
							}
							if( pstmt != null)
							{
								pstmt.close();
								pstmt = null;
							}
							
							sql = "select acct_code as ls_acctpay, cctr_code  as ls_cctrpay  from department where dept_code = ? ";
			
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,lsDept);
							
							rs = pstmt.executeQuery();
							
							if(rs.next())
							{
								lsAcctpay = checkNull(rs.getString("ls_acctpay"));
								lsCctrpay = checkNull(rs.getString("ls_cctrpay"));
							}		
							System.out.println("lsAcctpay"+lsAcctpay);
							System.out.println("lsCctrpay"+lsCctrpay);
							
							if (lsCctrpay.length() == 0)
							{
								lsCctrpay = " ";
							}
							
							if( rs != null)
							{
								rs.close();
								rs = null;
							}
							if( pstmt != null)
							{
								pstmt.close();
								pstmt = null;
							}
							
							Calendar cal1 = Calendar.getInstance();
							Calendar cal2 = Calendar.getInstance();
							cal1.setTime(ldStart);
							cal2.setTime(ldEnd);
							/*
							startDate = sdf.format(cal1.get(Calendar.YEAR));
							endDate1 = sdf.format(cal2.get(Calendar.YEAR));
							System.out.println("startDate"+startDate);
							System.out.println("endDate1"+endDate1);
							*/
							//boolean sameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
							if(cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR))
							{
								System.out.println("cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)::::");
								//claimPrd = sdf.format(cal2.get(Calendar.YEAR));
								claimPrd = cal2.get(Calendar.YEAR) + "";
							}
							else
							{
								System.out.println("cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)::::");
								String claimPrd1 = cal1.get(Calendar.YEAR) + "";
								if (claimPrd1 == null)
								{
									claimPrd1 = "";
								}
								else
								{
									if (claimPrd1.length() == 4)
									{
										claimPrd1 = claimPrd1.substring(2 , 4);
									}
								}
								String claimPrd2 = cal2.get(Calendar.YEAR) + "";
								if (claimPrd2 == null)
								{
									claimPrd2 = "";
								}
								else
								{
									if (claimPrd2.length() == 4)
									{
										claimPrd2 = claimPrd2.substring(2 , 4);
									}
								}

								claimPrd = claimPrd1 + claimPrd2 ;
							}
							System.out.println("claimPrd:::"+claimPrd);
							
							String totalAmt = clpaid.toString();
							System.out.println("totalAmt:::::::["+totalAmt+"]");
							
							Double totAmt = Double.parseDouble(totalAmt);
							System.out.println("totAmt:::::"+totAmt);
							//yyyy-mm-dd
					//		SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-mm-dd");
							java.util.Date tranDate1= new java.util.Date();
							System.out.println("tranDate1:::::"+tranDate1);
					// remaining to convert into application date format
							
						//	sdf.parse(tranDate);
					//		String tempDt = sdfnew.format(sdf.parse(tranDate));
					//		System.out.print( "tempDt..["+tempDt+"]" );
						//	java.util.Date newDt = sdfnew.parse(tempDt);
					//		System.out.println("newDt..["+newDt+"]");
							
							//Modified on [05/04/2015]
							//formatter.parse(dateInString)
							 // sdf.parse( tranDate )
							
							//payrHdr.put("tran_date", tranDate1);
							payrHdr.put("tran_date", new Timestamp(tranDate1 == null ? null : tranDate1.getTime()));
							
					//		payrHdr.put("tran_date", newDt);
							payrHdr.put("tran_type", "PRK");
							payrHdr.put("eff_date", tranDate1);
						//	payrHdr.put("eff_date",  newDt);
							payrHdr.put("emp_code", empCode);
							payrHdr.put("ref_no", tranId);
							payrHdr.put("ref_date", tranDate1);
							//payrHdr.put("ref_date",  newDt);
							payrHdr.put("bank_code", lsBank);
							payrHdr.put("auto_pay", "Y");
							payrHdr.put("site_code", lsSite);
							payrHdr.put("fin_entity", lsFinent);
							payrHdr.put("tot_amt", totAmt);
							payrHdr.put("anal_code"," ");
							payrHdr.put("confirmed", "N");
							payrHdr.put("paid", "N");
							payrHdr.put("acct_code__pay", lsAcctpay);
							payrHdr.put("cctr_code__pay", lsCctrpay);
							payrHdr.put("pay_mode", lsPaymode);
							//String batchNo = lsSite.concat(" ").concat(adCode).concat(" ").concat(claimPrd);
							String batchNo = lsSite+adCode+claimPrd;
							payrHdr.put("batch_no", batchNo);
							payrHdr.put("vouch_type", "O");
							payrHdr.put("curr_code", lsCurr);
							payrHdr.put("exch_rate", 1.0);
							payrHdr.put("cr_term","0");
			
							String remark = adCode + " claim for " + effDate + " to "+ endDate;
							System.out.println("remark:::"+remark);
							
							payrHdr.put("remarks", remark);
							payrHdr.put("chg_date",curDate1);
							//payrHdr.put("chg_date",curDate);
							payrHdr.put("chg_user", userId);  
						//	payrHdr.put("chg_user", "SYSTEM");
							payrHdr.put("chg_term", chgTerm);
						//	payrHdr.put("chg_term", "SYSTEM");
						
							
				
							payrvDet = new HashMap();
							payrvDet.put("ad_code",adCode);
							//	payrvDet.put("amount", claimAmt);
							payrvDet.put("amount", claimAount);
							
						//	payDetList = new ArrayList();
							payDetList.add(payrvDet);
							//payrvDet.clear();
							System.out.println("payDetList:::::::::::["+payDetList+"]");
						}
						

						System.out.println("tranId"+tranId);
						
						adclaimhdrPstmt.setString(1,tranId);
						adclaimhdrPstmt.setTimestamp(2,java.sql.Timestamp.valueOf(tranDate));
						adclaimhdrPstmt.setString(3,empCode);
						adclaimhdrPstmt.setDouble(4,claimAount);
						adclaimhdrPstmt.setDouble(5,loanAdjamount);
						adclaimhdrPstmt.setString(6,payrollVoucher);
						adclaimhdrPstmt.setString(7,payrollPrd);
						// Modified by Piyush on 03/06/2016 [To  update time in change date]
						// adclaimhdrPstmt.setTimestamp(8,java.sql.Timestamp.valueOf(curDate));
						adclaimhdrPstmt.setTimestamp(8, new java.sql.Timestamp(System.currentTimeMillis()));
						adclaimhdrPstmt.setString(9,userId);  
					//	adclaimhdrPstmt.setString(9,"SYSTEM");
						adclaimhdrPstmt.setString(10,chgTerm);
					//	adclaimhdrPstmt.setString(10,"SYSTEM");
						adclaimhdrPstmt.setString(11,adCode1);
						
						adclaimhdrPstmt.executeQuery();
						adclaimhdrPstmt.clearParameters();
						/*
						if ( adclaimhdrPstmt != null )
						{
							adclaimhdrPstmt.close();
							adclaimhdrPstmt = null;
						}
						*/
						
						effDate1 = genericUtility.getValidDateTimeString(effDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
						System.out.println("effDate1....["+effDate1+"]");
						
						endDate1 = genericUtility.getValidDateTimeString(endDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
						System.out.println("effDate1....["+endDate1+"]");
						
						System.out.println("tranId"+tranId);
						adclaimPstmt.setString(1,tranId);
						adclaimPstmt.setString(2,empCode);
						adclaimPstmt.setString(3,adCode1);
						adclaimPstmt.setTimestamp(4,java.sql.Timestamp.valueOf(effDate1));
						adclaimPstmt.setDouble(5,balAmount);
						adclaimPstmt.setDouble(6,claimAount);
						// Modified by Piyush on 02/06/2016 [To update chg_date,chg_user,chg_term in emp_adclaim table].Start
						adclaimPstmt.setTimestamp(7,new java.sql.Timestamp(System.currentTimeMillis()));
						adclaimPstmt.setString(8,userId);  
						adclaimPstmt.setString(9,chgTerm);
						// Modified by Piyush on 02/06/2016 [To update chg_date,chg_user,chg_term in emp_adclaim table].End
						
						adclaimPstmt.executeQuery();
						adclaimPstmt.clearParameters();

						detail3HashMap =getDetail3Data(dom,empCode);
						System.out.println("detail3HashMap:::::"+detail3HashMap);

						int adClaimDetLineNo = 0;

						Iterator it = detail3HashMap.keySet().iterator();
						while (it.hasNext()) 
						{
							String key = (String) it.next();
							HashMap<String, String> detail = detail3HashMap.get(key);
							System.out.println("detail:::::::"+detail);
							
						/* remaining
						 * errcode = nvo_claimproc.gbf_claim_proc(ls_ad ,ls_emp ,ld_start,ld_end ,lc_claimamt)
								if not isnull(errcode) and len(trim(errcode)) >  0 then
									exit
								end if
						 */
						
						/* tranIdDom = checkNull(genericUtility.getColumnValue("ref_no", dom, "3"));
							System.out.println("tranIdDom::::"+tranIdDom);
							if(tranIdDom.equalsIgnoreCase(empCode))
							{
								return errString; 
							}
						 */
						
							lsRefNo = detail3HashMap.get(key).get("refNo");
							lsLoanAd = detail3HashMap.get(key).get("lsLoanAd");
							loanAmt = detail3HashMap.get(key).get("loanAmt");
							lcMonInst = detail3HashMap.get(key).get("lcMonInst");
							adjAmt = detail3HashMap.get(key).get("adjAmt");
							ld_end_date = detail3HashMap.get(key).get("ld_end_date");
							ld_start_date = detail3HashMap.get(key).get("ld_start_date");
							lc_interest = detail3HashMap.get(key).get("lc_interest");
							lsSchemeno = detail3HashMap.get(key).get("lsSchemeno");
							ll_line = detail3HashMap.get(key).get("ll_line");
							//Modified by Ahmed on 18/JUL/2016[To get balance amount of loan from detail3 screen][START]
							String balanceAmtFromLoan = detail3HashMap.get(key).get("bal_amt");
							//Modified by Ahmed on 18/JUL/2016[To get balance amount of loan from detail3 screen][END]
							System.out.println("lsRefNo::::"+lsRefNo);
							System.out.println("lsLoanAd::::"+lsLoanAd);
							System.out.println("loanAmt::::"+loanAmt);
							System.out.println("lcMonInst::::"+lcMonInst);
							System.out.println("adjAmt::::"+adjAmt);
							System.out.println("ld_end_date::::"+ld_end_date);
							System.out.println("ld_start_date::::"+ld_start_date);
							System.out.println("lc_interest::::"+lc_interest);
							System.out.println("lsSchemeno::::"+lsSchemeno);
							System.out.println("ll_line::::"+ll_line);
							//Modified by Ahmed on 18/JUL/2016[To get balance amount of loan from detail3 screen][START]
							System.out.println("balanceAmtFromLoan::::"+balanceAmtFromLoan);
							
							double balAmtFromLoan = 0.0d;
							if (balanceAmtFromLoan != null && balanceAmtFromLoan.trim().length() > 0)
							{
								balAmtFromLoan = Double.parseDouble(balanceAmtFromLoan);
							}
							//Modified by Ahmed on 18/JUL/2016[To get balance amount of loan from detail3 screen][END]
							//code remaining
							/*
							 ll_start = dw_2.Find("tran_id='" + gf_nulltostr(ls_emp)+ "'",1,ll_end )//13/03/01 changed ls_tranid to ls_emp
						if ll_start > 0  then //6
							for ll_yy = ll_start to ll_end
								ls_tranId1 = dw_2.getitemstring(ll_yy,"tran_id")
								if ls_tranId1 <> ls_emp then//13/03/01 changed ls_tranid to ls_emp
									exit
								end if	
								ls_ref_no     = dw_2.getitemstring(ll_yy,"ref_no")
								ls_loan_ad    = dw_2.getitemstring(ll_yy,"ad_code")
								lc_loan_amt   = dw_2.getitemnumber(ll_yy,"loan_amt")
								lc_mon_inst   = dw_2.getitemnumber(ll_yy,"mon_inst")
								lc_adjamt     = dw_2.getitemnumber(ll_yy,"adj_amt")
								ld_end_date   = dw_2.getitemdatetime(ll_yy,"end_date")
								ld_start_date = dw_2.getitemdatetime(ll_yy,"start_date")
								lc_interest   = dw_2.getitemnumber(ll_yy,"interest")
								ls_schemeno   = dw_2.getitemstring(ll_yy,"scheme_no")
								ll_line       = dw_2.getitemnumber(ll_yy,"line_no")
								ll_crow = dw_3.insertrow(0)
								dw_3.setitem(ll_crow, "tran_id", ls_tranid)
								dw_3.setitem(ll_crow, "line_no",ll_line )
								dw_3.setitem(ll_crow, "ad_code", ls_loan_ad)
								dw_3.setitem(ll_crow, "eff_date", dw_2.getitemdatetime(ll_yy,"eff_date"))
								dw_3.setitem(ll_crow, "bal_amt", dw_2.getitemnumber(ll_yy,"bal_amt"))
								dw_3.setitem(ll_crow, "adj_amt", lc_adjamt)
								dw_3.setitem(ll_crow, "ref_type", "L")
								dw_3.setitem(ll_crow, "ref_no", ls_ref_no)
								
								// update dw_payrv for voucher creation with loan amount adjusted in voucher detail
								ll_crow = dw_payrv_det.insertrow(0)
								dw_payrv_det.setitem(ll_crow, "ad_code", ls_loan_ad)
								dw_payrv_det.setitem(ll_crow, "amount", 0 - lc_adjamt)
							 */
							
							double adjAmount = Double.parseDouble(adjAmt);
							//TODO
								if(adjAmount > 0) {
								payrvDet = new HashMap();
								payrvDet.put("ad_code",lsLoanAd);
								payrvDet.put("amount",0 - adjAmount);
								payDetList.add(payrvDet);
								
								sql = "select scheme_no as ls_schemeno, interest as lc_interest, mon_inst as lc_mon_inst, loan_amt as lc_loan_amt from loans where loan_no = ? ";
								
								pstmt = conn.prepareStatement(sql);
								
								pstmt.setString(1,lsRefNo);
								
								rs = pstmt.executeQuery();
								
								if(rs.next())
								{
									 lsSchemeno = checkNull(rs.getString("ls_schemeno"));
									 lcInterest = checkNull(rs.getString("lc_interest"));
									 lcMonInst = checkNull(rs.getString("lc_mon_inst"));
									 lcLoanAmt = checkNull(rs.getString("lc_loan_amt"));
								}
								System.out.println("lsSchemeno::::"+lsSchemeno);
								System.out.println("lcInterest::::"+lcInterest);
								System.out.println("lcMonInst::::"+lcMonInst);
								System.out.println("lcLoanAmt::::"+lcLoanAmt);
								
								if( rs != null)
								{
									rs.close();
									rs = null;
								}
								if( pstmt != null)
								{
									pstmt.close();
									pstmt = null;
								}
	
								// Modified by Piyush on 02/06/2016[To insert data in emp_adclaimdet].Start
								ldLoanStartDate = ld_start_date == null ? null :genericUtility.getValidDateTimeString(ld_start_date,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
	
								adClaimDetLineNo = adClaimDetLineNo + 1;
								adclaimdetInsStmt.setString(1 , tranId);
								adclaimdetInsStmt.setInt(2 , adClaimDetLineNo);
								adclaimdetInsStmt.setString(3 , lsLoanAd);
								adclaimdetInsStmt.setTimestamp(4,ldLoanStartDate == null ? null: java.sql.Timestamp.valueOf(ldLoanStartDate));
								//Modified by Ahmed on 15/JUL/2016[To insert balance amount into emp_adclaimdet instead of loan amount][START]
								//adclaimdetInsStmt.setString(5 , loanAmt);
								adclaimdetInsStmt.setDouble(5, balAmtFromLoan);
								//Modified by Ahmed on 15/JUL/2016[To insert balance amount into emp_adclaimdet instead of loan amount][END]
								adclaimdetInsStmt.setString(6 , adjAmt);
								adclaimdetInsStmt.setString(7 , "L");
								adclaimdetInsStmt.setString(8 , lsRefNo);
								noRowInst = adclaimdetInsStmt.executeUpdate();
								adclaimdetInsStmt.clearParameters();
								if (noRowInst > 0)
								{
									System.out.println("1 row inserted emp_adclaimdet for Loan No = " + refNo);
								}
							/*	}
							//TODO
							// Modified by Piyush on 02/06/2016[To insert data in emp_adclaimdet].End
							
							double dblLoanAdjAmount = Double.parseDouble(adjAmt);
								System.out.println("PerkClaimProcessPostSave.postSave() Loan Adjust Amount::: [" + dblLoanAdjAmount + "]");
								if(dblLoanAdjAmount > 0){*/
								// Modified by Piyush on 02/06/2016[To Update remarks same as ITM]
								//lsRemarks = "Through claim,"+ adCode + " , for "+effDate ;
								lsRemarks = "Through claim, "+ adCode.trim() + " , on for "+effDate ;
								
								tranDate = detail2HashMap.get(key1).get("tranDate");
								System.out.println("tranDate::::::"+tranDate);
								
								xmlValues = "";
								xmlValues ="<?xml version=\"1.0\" encoding=\"utf-8\"?><Root>";
								xmlValues = xmlValues + "<Header></Header>";
								xmlValues = xmlValues + "<Detail1>";
								xmlValues = xmlValues +"<tran_id></tran_id>";
								xmlValues = xmlValues +"<pay_site>" + lsSite + "</pay_site>";
								xmlValues = xmlValues +"<reco_date>" + tranDate + "</reco_date>"; 
								xmlValues = xmlValues + "</Detail1></Root>";
								
								System.out.println("xmlValues  :["+xmlValues+"]");
								
								TransIDGenerator tranGen = new TransIDGenerator(xmlValues, userId, CommonConstants.DB_NAME); 
							//	TransIDGenerator tranGen = new TransIDGenerator(xmlValues, "SYSTEM", CommonConstants.DB_NAME);
								recoNo = tranGen.generateTranSeqID("L-RECO", "reco_no", keyStr, conn);
								
								System.out.println("recoNo::::::::"+recoNo);
								
								if(recoNo.equalsIgnoreCase("ERROR"))
								{
									System.out.println("Failed to generate loan recovery id:::::");
									
									//errString = "VTTRANID" ;
									errString = itmDBAccess.getErrorString("","VTTRANID","","",conn);
									isError = true;
									return errString;
								}
								
								ld_start_date = genericUtility.getValidDateTimeString(ld_start_date,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
								System.out.println("ld_start_date....["+ld_start_date+"]");
									
								ld_end_date = genericUtility.getValidDateTimeString(ld_end_date,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
								System.out.println("ld_end_date....["+ld_end_date+"]");
								
								tranDate = genericUtility.getValidDateTimeString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat() );
								System.out.println("tranDate....["+tranDate+"]");
							//TODO
								sql = "insert into loan_reco(reco_no, reco_date, loan_no, scheme_no, emp_code,start_date, loan_amt, loan_interest, mon_inst, ad_code,reco_amt, chg_date, chg_user, chg_term, remarks, confirmed) " +
										"values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,'Y')";
								
								pstmt = conn.prepareStatement(sql);
								
								pstmt.setString(1,recoNo);
								pstmt.setTimestamp(2,java.sql.Timestamp.valueOf(tranDate));
								pstmt.setString(3,lsRefNo);
								pstmt.setString(4,lsSchemeno);
								pstmt.setString(5,empCode);
								pstmt.setTimestamp(6,java.sql.Timestamp.valueOf(ld_start_date));
								pstmt.setString(7,lcLoanAmt);
								pstmt.setString(8,lcInterest);
								pstmt.setString(9,lcMonInst);
								pstmt.setString(10,lsLoanAd);
								pstmt.setString(11,adjAmt);
								// Modified by Piyush on 03/06/2016 [To  update time in change date]
								// pstmt.setTimestamp(12,java.sql.Timestamp.valueOf(curDate));
								pstmt.setTimestamp(12, new java.sql.Timestamp(System.currentTimeMillis()));
								pstmt.setString(13,userId);
							//	pstmt.setString(13,"SYSTEM");
								pstmt.setString(14,chgTerm);
							//	pstmt.setString(14,"SYSTEM");
								pstmt.setString(15,lsRemarks);
								
								pstmt.executeQuery();
								
								if(payrollVoucher.equalsIgnoreCase("V"))
								{
									PerkClaimProcessComm pclaim = new PerkClaimProcessComm();
									errString = pclaim.gbfLoanRecoRetrieve( recoNo, recoNo, xtraParams,conn);
									System.out.println("errString 555"+errString);
									
									if(errString != null && errString.trim().length() > 0)
									{
										System.out.println("errString:::::::::"+errString);
								    	//	errString = itmDBAccess.getErrorString("","VMDLOANRECO","","",conn);
										isError = true;
										return errString;	
									}
								}
								else
								{
									System.out.println("Inside else part :::::::::920::::::::");
									
									sql = "insert into loans_schedule_det(loan_no,prd_code,process_type,ad_code,amount,line_no,chg_date,chg_user,chg_term) " +
											"values ( ? , ? , 'PK',  ? ,  ? , '1' , ? ,  ? ,  ? )";
									
									pstmt = conn.prepareStatement(sql);
									
									pstmt.setString(1,lsRefNo);
									pstmt.setString(2,payrollPrd);
									pstmt.setString(3,lsLoanAd);
									pstmt.setString(4,adjAmt);
									// Modified by Piyush on 03/06/2016 [To  update time in change date]
									// pstmt.setTimestamp(5,java.sql.Timestamp.valueOf(curDate));
									pstmt.setTimestamp(5, new java.sql.Timestamp(System.currentTimeMillis()));
									pstmt.setString(6,userId); 
								//	pstmt.setString(6,"SYSTEM");
									pstmt.setString(7,chgTerm);
								//	pstmt.setString(7,"SYSTEM");
									
									pstmt.executeQuery();
								}
							}
						//}		if ll_start > 0  then //6 completed							
						
						}// To be done outside 
						
						if(payrollVoucher.equalsIgnoreCase("V"))
						{
							//errString = gf_payroll_voucher(payrHdr, payrvDet, "dw_claim");
							PerkClaimProcessComm pclaim = new PerkClaimProcessComm();
							errString = pclaim.payrollVoucher(payrHdr,payDetList, "dw_claim",xtraParams,conn);
							System.out.println("errString::::::"+errString);
							
							if(errString != null && errString.trim().length() > 0)
							{
								isError = true;
								return errString;
							}
						}
						else
						{
							if(loanAdjamount > 0) 
							{
								count = 0;
								sql = "select count(*) as cnt from employee_mthad where emp_code = ? and ad_code = ? and eff_date = ? and exp_date = ?";
								
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,empCode);
								pstmt.setString(2,lsLoanAd);
								pstmt.setTimestamp(3,java.sql.Timestamp.valueOf(fromDate));
								pstmt.setTimestamp(4,java.sql.Timestamp.valueOf(toDate));
								
								rs = pstmt.executeQuery();
								
								if(rs.next())
								{
									count = rs.getInt("cnt");
								}
								System.out.println("count:::"+count);
								
								if(rs != null)
								{
									rs.close();
									rs = null;
								}
								if(pstmt != null)
								{
									pstmt.close();
									pstmt = null;
								}
								
								if(count > 0)
								{
									sql1= "update employee_mthad set type = 'F', amount =amount + ?, ref_type  = 'C', ref_no =  ? , prop_yn = 'N' " +
											"where emp_code = ? and ad_code = ? and eff_date = ?  and exp_date =  ? ";
									
									pstmt1  = conn.prepareStatement(sql1);
									
									pstmt1.setDouble(1,loanAdjamount);
									pstmt1.setString(2,tranId);
									pstmt1.setString(3,empCode);
									pstmt1.setString(4,lsLoanAd);
									pstmt1.setTimestamp(5,java.sql.Timestamp.valueOf(fromDate));
									pstmt1.setTimestamp(6,java.sql.Timestamp.valueOf(toDate));
									
									pstmt1.executeUpdate();
									
									pstmt1.clearParameters();
									
									if ( pstmt1 != null )
									{
										pstmt1.close();
										pstmt1 = null;
									}
									
								}
								else
								{
									System.out.println("loanAdjamount:::::"+loanAdjamount);
									
									sql1 = "Insert into employee_mthad (emp_code, month_code, ad_code, eff_date, exp_date, type, amount, chg_date, chg_user, chg_term, ref_type, ref_no, prop_yn) " +
											"Values ( ? , ? , ? , ? ,  ? , 'F' , ? ,  ? , ? , ? ,'C', ? , 'N')";
									
									pstmt1  = conn.prepareStatement(sql1);
									
									pstmt1.setString(1,empCode);
									pstmt1.setString(2,month);
									pstmt1.setString(3,lsLoanAd);
									pstmt1.setTimestamp(4,java.sql.Timestamp.valueOf(fromDate));
									pstmt1.setTimestamp(5,java.sql.Timestamp.valueOf(toDate));
									pstmt1.setDouble(6,loanAdjamount);
									//	pstmt1.setString(6,loanAdjamt);
									// Modified by Piyush on 03/06/2016 [To  update time in change date]
									// pstmt1.setTimestamp(7,java.sql.Timestamp.valueOf(curDate));
									pstmt1.setTimestamp(7, new java.sql.Timestamp(System.currentTimeMillis()));
									pstmt1.setString(8,userId); 
								//	pstmt1.setString(8,"SYSTEM");
									pstmt1.setString(9,chgTerm);
								//	pstmt1.setString(9,"SYSTEM");
									pstmt1.setString(10,tranId);
									
									pstmt1.executeQuery();
									
									if ( pstmt1 != null )
									{
										pstmt1.close();
										pstmt1 = null;
									}
								}
							}
						}
						
						/*
						if(pstmt != null)
						{
							pstmt.close();
							pstmt = null;
						}
						*/

						sql = "UPDATE ALLOWDEDN_BAL SET AMT_BAL	= AMT_BAL - ?, AMT_ADJ = AMT_ADJ + ? WHERE EMP_CODE = ? AND AD_CODE = ? AND START_DATE = ? AND END_DATE = ?";
						allowdednbalPstmt = conn.prepareStatement(sql);
						allowdednbalPstmt.setDouble(1 , claimAount);
						allowdednbalPstmt.setDouble(2 , claimAount);
						allowdednbalPstmt.setString(3 , empCode);
						allowdednbalPstmt.setString(4 , adCode1);
						allowdednbalPstmt.setTimestamp(5,java.sql.Timestamp.valueOf(effDate1));
						allowdednbalPstmt.setTimestamp(6,java.sql.Timestamp.valueOf(endDate1));
						allowdednbalPstmt.executeQuery();
						allowdednbalPstmt.clearParameters();

						
						if(rs != null)
						{
							rs.close();
							rs = null;
						}
						if(pstmt != null)
						{
							pstmt.close();
							pstmt = null;
						}
						payDetList.clear();
					}
			}	
		}
		catch(Exception e)
		{
			isError =true;
			System.out.println("Exception :PerkClaimProcessPostSave : :==>\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("isError ["+isError+"]");
				System.out.println("isLocalConn ["+isLocalConn+"]");
				if( conn != null )
				{

					if( isError )
					{
						conn.rollback();
					}
					if (rs != null)
					{
						rs.close();
						rs = null;
					}
					if(pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					if(adclaimhdrPstmt != null)
					{
						adclaimhdrPstmt.close();
						adclaimhdrPstmt = null;
					}
					if(adclaimPstmt != null)
					{
						adclaimPstmt.close();
						adclaimPstmt = null;
					}
					if(allowdednbalPstmt != null)
					{
						allowdednbalPstmt.close();
						allowdednbalPstmt = null;
					}
					// Modified by Piyush on 02/06/2016[To add data in emp_adclaimdet table].Start
					if(adclaimdetInsStmt != null)
					{
						adclaimdetInsStmt.close();
						adclaimdetInsStmt = null;
					}
					// Modified by Piyush on 02/06/2016[To add data in emp_adclaimdet table].End
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception :PerkClaimProcessPostSave : :==>\n"+e.getMessage());
				
				try
				{
					System.out.println("Before rollback");
					conn.rollback();
				}
			
				catch(SQLException sqle)
				{
                    System.out.println(sqle);
				}
				
				throw new ITMException(e);
			
			}
		}
		System.out.println("Return string :"+errString);
		return errString;		
	}// Post Save Completed
	
	private HashMap<String, HashMap<String , String>> getDetail2Data(Document dom)
	{
		System.out.println("Inside getDetail2Data");
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String columnValue = "";
		String empCode = "";
		String adCode1  = "";
		String effDate=  "";
		String endDate = "";
		String tranDate = "";
		String balAmt = "";
		String claimAmt = "";
		String loanAdjamt = "";
		
		HashMap<String, HashMap<String , String>> parentHashMap1 = new HashMap<String, HashMap<String , String>>();
		
		parentList = dom.getElementsByTagName("Detail2");
		int parentNodeListLength = parentList.getLength();
		for (int prntCtr = 0; prntCtr < parentNodeListLength; prntCtr++ )
		{
			parentNode = parentList.item(prntCtr);
			childList = parentNode.getChildNodes();
			int noOfChilds = childList.getLength();		
			System.out.println("noOfChilds..["+noOfChilds+"]");
			
			HashMap<String , String> tempHmap1 =  new HashMap<String , String>();
			
			for (int ctr = 0; ctr < childList.getLength(); ctr++)
			{
				columnValue = "";
				
				childNode = childList.item(ctr);
				System.out.println("childNode..["+childNode+"]");
				
				if( childNode.getNodeType() != Node.ELEMENT_NODE )
				{
					continue;
				}
				String childNodeName = childNode.getNodeName();
				System.out.println("childNodeName..["+childNodeName+"]");
				
				if (childNodeName == null || childNodeName.trim().length() == 0)
				{
					continue;
				}
				if( childNodeName.equalsIgnoreCase("attribute") )
				{
					continue;
				}
				if ( childNode != null && childNode.getFirstChild() != null )
				{
					columnValue = childNode.getFirstChild().getNodeValue();
				}
				System.out.println(" columnValue..["+columnValue+"]");
				
				 if (childNodeName.equalsIgnoreCase("emp_code"))
				{
					empCode = "";
					empCode = columnValue;
					System.out.println("empCode:::::"+empCode);
					tempHmap1.put("empCode", empCode);
				}
				 if (childNodeName.equalsIgnoreCase("ad_code"))
				{
					adCode1 = "";
					adCode1 = columnValue;
					System.out.println("adCode1:::::"+adCode1);
					tempHmap1.put("adCode1", adCode1);
				}
				if (childNodeName.equalsIgnoreCase("tran_date"))
				{
					tranDate = columnValue;
					System.out.println("tranDate:::::"+tranDate);
					tempHmap1.put("tranDate", tranDate);
				}
				if (childNodeName.equalsIgnoreCase("bal_amt"))
				{
					balAmt = columnValue;
					System.out.println("balAmt:::::"+balAmt);
					tempHmap1.put("balAmt", balAmt);
				}
				if (childNodeName.equalsIgnoreCase("claim_amt"))
				{
					claimAmt = columnValue;
					System.out.println("claimAmt:::::"+claimAmt);
					tempHmap1.put("claimAmt", claimAmt);
				}
				if (childNodeName.equalsIgnoreCase("loan_adjamt"))
				{
					loanAdjamt = columnValue;
					// Modified by Piyush on 27/06/2016 [To make load adjustment 0 in case of no loan adjusted].Start
					if (loanAdjamt == null || loanAdjamt.trim().length() == 0)
					{
						loanAdjamt = "0.00";
					}
					// Modified by Piyush on 27/06/2016 [To make load adjustment 0 in case of no loan adjusted].End
					System.out.println("loanAdjamt:::::"+loanAdjamt);
					tempHmap1.put("loanAdjamt", loanAdjamt);
				}
				if (childNodeName.equalsIgnoreCase("eff_date"))
				{
					effDate = columnValue;
					System.out.println("effDate:::::"+effDate);
					tempHmap1.put("effDate", effDate);
				}
				if (childNodeName.equalsIgnoreCase("end_date"))
				{
					endDate = columnValue;
					System.out.println("endDate:::::"+endDate);
					tempHmap1.put("endDate", endDate);
				}
				if((empCode != null && empCode.length() > 0) && (adCode1 != null && adCode1.length() > 0) && (tranDate != null && tranDate.length() > 0) && (balAmt != null && balAmt.length() > 0) && (claimAmt != null && claimAmt.length() > 0) &&  (loanAdjamt != null && loanAdjamt.length() > 0) && (effDate != null && effDate.length() > 0) && (endDate != null && endDate.length() > 0) )
				{
					System.out.println("empCode...["+empCode+"]adCode1...["+adCode1+"]tranDate...["+tranDate+"]");
					System.out.println("balAmt...["+balAmt+"]claimAmt...["+claimAmt+"]loanAdjamt...["+loanAdjamt+"]");
					System.out.println("effDate...["+effDate+"]endDate...["+endDate+"]");
					System.out.println("tempHmap1...["+tempHmap1+"]");
					
					parentHashMap1.put(empCode,tempHmap1);
					
					empCode = "";
					adCode1 = "";
					effDate=  "";
					endDate = "";
					tranDate = "";
					balAmt = "";
					claimAmt = "";
					loanAdjamt = "";
					
					System.out.println("parentHashMap1::::"+parentHashMap1);
				}
				else
				{
					continue;
				}
				
			}
		}
		System.out.println(" RETURN parentHashMap1::::"+parentHashMap1);
		return parentHashMap1 ;
	}
	
	private HashMap<String, HashMap<String , String>> getDetail3Data(Document dom , String empCode)
	{
		System.out.println("Inside getDetail3Data");
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String columnValue = "";
		String tranId = "";
		String balAmt  = "";
		String claimAmt = "";
		String loanAdjamt = "";
		String effDate = "";
		String endDate = "";
		String refNo ="";
		String 	lsRefNo = "";
		String lsLoanAd = "";
		String loanAmt = "";
		String lcMonInst = "";
		String adjAmt = "";
		String ld_end_date = "";
		String ld_start_date = "";
		String lc_interest = "";
		String lsSchemeno = "";
		String ll_line = "";
		
		HashMap<String, HashMap<String , String>> parentHashMap = new HashMap<String, HashMap<String , String>>();
		
		parentList = dom.getElementsByTagName("Detail3");
		int parentNodeListLength = parentList.getLength();
		for (int prntCtr = 0; prntCtr < parentNodeListLength; prntCtr++ )
		{
			parentNode = parentList.item(prntCtr);
			childList = parentNode.getChildNodes();
			int noOfChilds = childList.getLength();		
			HashMap<String , String> tempHmap =  new HashMap<String , String>();
			for (int ctr = 0; ctr < childList.getLength(); ctr++)
			{
				columnValue = "";
				
				childNode = childList.item(ctr);
				System.out.println("childNode..["+childNode+"]");
				if( childNode.getNodeType() != Node.ELEMENT_NODE )
				{
					continue;
				}
				String childNodeName = childNode.getNodeName();
				System.out.println("childNodeName..["+childNodeName+"]");
				if (childNodeName == null || childNodeName.trim().length() == 0)
				{
					continue;
				}
				if ( childNode != null && childNode.getFirstChild() != null )
				{
					columnValue = childNode.getFirstChild().getNodeValue();
				}
				System.out.println("Second columnValue..["+columnValue+"]");
				System.out.println("empCode..["+empCode+"]");
				
				 if (childNodeName.equalsIgnoreCase("tran_id"))
				{
					tranId = columnValue;
					System.out.println("tranId:::::"+tranId);
					
					if(!(tranId.equalsIgnoreCase(empCode)))
					{
						//continue;
						tempHmap.clear();
						break;
					}
					else
					{
						tempHmap.put("tranId", tranId);
					}
				}
				else if (childNodeName.equalsIgnoreCase("ref_no"))
				{
					refNo = columnValue;
					tempHmap.put("refNo", refNo);
				}
				else if (childNodeName.equalsIgnoreCase("ad_code"))
				{
					lsLoanAd = columnValue;
					tempHmap.put("lsLoanAd", lsLoanAd);
				}
				else if (childNodeName.equalsIgnoreCase("loan_amt"))
				{
					loanAmt = columnValue;
					tempHmap.put("loanAmt", loanAmt);
				}
				else if (childNodeName.equalsIgnoreCase("mon_inst"))
				{
					lcMonInst = columnValue;
					tempHmap.put("lcMonInst", lcMonInst);
				}
				//else if (childNodeName.equalsIgnoreCase("adj_amt"))
				else if (childNodeName.equalsIgnoreCase("loan_adjamt"))
				{
					adjAmt = columnValue;
					tempHmap.put("adjAmt", adjAmt);
				}
				else if (childNodeName.equalsIgnoreCase("end_date"))
				{
					ld_end_date = columnValue;
					tempHmap.put("ld_end_date", ld_end_date);
				}
				else if (childNodeName.equalsIgnoreCase("start_date"))
				{
					ld_start_date = columnValue;
					tempHmap.put("ld_start_date", ld_start_date);
				}
				else if (childNodeName.equalsIgnoreCase("interest"))
				{
					lc_interest = columnValue;
					tempHmap.put("lc_interest", lc_interest);
				}
				else if (childNodeName.equalsIgnoreCase("scheme_no"))
				{
					lsSchemeno = columnValue;
					tempHmap.put("lsSchemeno", lsSchemeno);
				}
				else if (childNodeName.equalsIgnoreCase("line_no"))
				{
					ll_line = columnValue;
					tempHmap.put("ll_line", ll_line);
				}
				//Modified by Ahmed on 18/JUL/2016[To fetch balance amount of loan][START]
				else if (childNodeName.equalsIgnoreCase("bal_amt"))
				{
					balAmt = columnValue;
					tempHmap.put("bal_amt", balAmt);
				}
				//Modified by Ahmed on 18/JUL/2016[To fetch balance amount of loan][END]
			//	System.out.println("refNo...["+refNo+"]tempHmap..["+tempHmap+"]");
				//parentHashMap.put(refNo,tempHmap);
			}
			System.out.println("refNo...["+refNo+"]tempHmap..["+tempHmap+"]");
			if (tempHmap != null && !(tempHmap.isEmpty()))
			{
				System.out.println("refNo...["+refNo+"] ..before return ...tempHmap..["+tempHmap+"]");
				parentHashMap.put(refNo,tempHmap);
			}
			
		}
		System.out.println("parentHashMap.["+parentHashMap+"]");
		return parentHashMap ;
	}
	// 03/12/2015 To call nvo [Start]
	//private String gbfWebserviceCall(String businessObj, String xmlString1, String xmlString2, String xtraParams, String forcedFlag , Connection conn) throws ITMException
	private String gbfWebserviceCall(String businessObj, String xmlString1, String xmlString2, String xtraParams, Connection conn) throws ITMException
	{
		String methodName = "gbf_process";
		String compName = "";
		String retString = "";
		String serviceCode = "";
		String serviceURI = "";
		String actionURI = "";
		String sql = "";
		//Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;		
		try
		{
			
			actionURI = "http://NvoServiceurl.org/" + methodName;
                           System.out.println(businessObj+"---"+xmlString1+"---"+xmlString2+"---"+xtraParams);

			sql = "SELECT SERVICE_CODE,COMP_NAME FROM SYSTEM_EVENTS WHERE OBJ_NAME = ? AND EVENT_CODE = 'process' ";
			pStmt = conn.prepareStatement(sql);
			pStmt.setString(1,businessObj);
			rs = pStmt.executeQuery();
			if ( rs.next() )
			{
				serviceCode = rs.getString("SERVICE_CODE");
				compName = rs.getString("COMP_NAME");
			}
			System.out.println("serviceCode = "+serviceCode+" compName "+compName);
			sql = "SELECT SERVICE_URI,METHOD_NAME FROM SYSTEM_EVENT_SERVICES WHERE SERVICE_CODE = ? ";
			pStmt = conn.prepareStatement(sql);
			pStmt.setString(1,serviceCode);
			rs = pStmt.executeQuery();
			if ( rs.next() )//serviceURI = http://192.168.0.124/BaseE12DotNetService6-3-0-003/E12BusinessLogic6-3-0-003.asmx compName = nvo_business_object_dist_despatch   
			{
				serviceURI = rs.getString("SERVICE_URI");
                methodName = rs.getString("METHOD_NAME");
			}
			System.out.println("serviceURI = "+serviceURI+" compName = "+compName);
			Service service = new Service();
			Call call = (Call)service.createCall();
			call.setTargetEndpointAddress(new java.net.URL(serviceURI));
			call.setOperationName( new javax.xml.namespace.QName("http://NvoServiceurl.org", methodName ) );
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(actionURI);
			Object[] aobj = new Object[4];

			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "component_name"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "tab_xml_data_1"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "tab_xml_data_all"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "xtra_params"), XMLType.XSD_STRING, ParameterMode.IN);
			//call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "forced_flag"), XMLType.XSD_STRING, ParameterMode.IN);

			aobj[0] = new String(compName.trim());
			aobj[1] = new String(xmlString1.trim());
			aobj[2] = new String(xmlString2.trim());
			aobj[3] = new String(xtraParams);
		//	aobj[4] = new String(forcedFlag);

			call.setReturnType(XMLType.XSD_STRING);

			retString = (String)call.invoke(aobj);

			System.out.println("Return string from NVO is:==>["+retString+"]");

		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if (pStmt != null )
				{
					pStmt.close();
					pStmt = null;
				}
				 /*if( conn != null ){
					conn.close();
					conn = null;
				}*/
			}
			catch(Exception e)
			{
                System.out.println("Exception inCalling confirmed");
                e.printStackTrace();
                try{
                conn.rollback();

                }catch (Exception s)
                {
                    System.out.println("Unable to rollback");
                    s.printStackTrace();
                }
				throw new ITMException(e);
            }
		}
		return retString;
	}
	// 03/12/2015 To call nvo [End]

	private String getObjNameFromDom( Document dom, String attribute ) throws RemoteException,ITMException
	{
		String objName = "";
		try
		{
			NodeList detailList = null;
			Node currDetail = null,reqDetail = null;			
			int	detailListLength = 0;

			detailList = dom.getElementsByTagName("Detail1");
			detailListLength = detailList.getLength();
			for (int ctr = 0;ctr < detailListLength;ctr++)
			{
				currDetail = detailList.item(ctr);
				objName = currDetail.getAttributes().getNamedItem(attribute).getNodeValue();
						
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return objName;
	}

	public String checkNull(String input)
	{
		if (input == null || "null".equalsIgnoreCase(input))
		{
			input= "";
		}
		return input.trim();
	}

	/* String gbfLoanRecoRetrieve(String recoNoFrom,String recoNoTo,String xtraParams ,Connection conn) throws ITMException
	{
		String errString = "",sql = "" , userId = "";
		String loadMode = "A", editOption = "0";	
		String recNo = "";
		PreparedStatement pstmt = null,pstmtUpd = null;
		ResultSet rs = null;
		Timestamp currDate = null;
		GenericUtility genericUtility = null;
		try 
		{
			System.out.println("gbfLoanRecoRetrieve called........... ");
			currDate = new java.sql.Timestamp(System.currentTimeMillis());
			genericUtility = GenericUtility.getInstance();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			//sql = "select nvl(edi_option , '0') , nvl(load_mode , 'A')  from transetup where tran_window  = 'w_loan_reco' ";
			sql = "select edi_option ,load_mode  from transetup where tran_window  = 'w_loan_reco' ";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				editOption = rs.getString("edi_option") == null?"0":rs.getString("edi_option") ;
				loadMode = rs.getString("load_mode") == null?"A":rs.getString("load_mode") ;
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			sql = "select	reco_no from loan_reco where reco_no >= ?  and reco_no <= ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, recoNoFrom);
			pstmt.setString(2, recoNoTo);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				recNo = rs.getString("recNo") == null?"":rs.getString("recNo") ;

				sql = " update loan_reco set confirmed = 'Y', conf_date = ? ,emp_code__aprv = ? where reco_no = ? ";
				pstmtUpd = conn.prepareStatement(sql);
				pstmtUpd.setTimestamp(1, currDate);
				pstmtUpd.setString(2,userId);
				pstmtUpd.setString(3, recNo);
				pstmtUpd.executeUpdate();
				pstmtUpd.close();
				pstmtUpd = null;
				//63 ls_errcode = gbf_post_loan_reco(ls_recono , 1)
				errString = gbfPostLoanReco(recNo , xtraParams,conn);
				if(errString != null && errString.trim().length() > 0)
				{
					return errString;
				}

				if(editOption != null && editOption.trim().equalsIgnoreCase("0"))
				{

					String dataStr = "";
					if ( "1".equals(editOption.trim()) )
					{
						CreateRCPXML createRCPXML = new CreateRCPXML("w_loan_reco","reco_no");
						dataStr = createRCPXML.getTranXML( recNo, conn );
						System.out.println( "dataStr =[ "+ dataStr + "]" );
						Document ediDataDom = genericUtility.parseString(dataStr);
						E12CreateBatchLoadEjb e12CreateBatchLoad = new E12CreateBatchLoadEjb();
						String retString = e12CreateBatchLoad.createBatchLoad(ediDataDom, "w_loan_reco", "0", xtraParams, conn );
						createRCPXML = null;
						e12CreateBatchLoad = null;
						if( retString != null && "SUCCESS".equals(retString) )
						{
							System.out.println("retString from batchload = 	["+retString+"]");
						}
					}

				}

			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

		} catch (SQLException e) 
		{
			System.out.println("Exception :PerkClaimPos :gbfLoanRecoRetrieve :SQLException :==>\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}catch (Exception e) 
		{
			System.out.println("Exception :PerkClaimPos :gbfLoanRecoRetrieve :Exception :==>\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		return errString;
	}//gbfLoanRecoRetrieve end
	*/
}
