package ibase.webitm.ejb.adm;
import java.util.Calendar ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;  
import java.sql.Timestamp ;
import java.sql.Connection;
import java.util.HashMap;
import java.io.ByteArrayOutputStream ;
import java.io.PrintStream ;
class LoanCalculation
{
	/*	
	if(infoMap.get("fr_date")!=null)
	{
		frDate = 	(Timestamp)infoMap.get("fr_date");//form period
	}
	if(infoMap.get("tran_id")!=null)
	{
			tranId = 	(Timestamp)infoMap.get("tran_id");
	}
		if(infoMap.get("data_object")!=null)
	{
			dataObject = 	infoMap.get("data_object").toString();
	}
		if (type.eqalsIgnoreCase("PP"))
	{
		//payrollDetRs = retrieve(prdCode,mprdcode,mempcode,mempcode,msitecode,msitecode,mprocgroup_fr,mprocgroup_to)
	}
	else
	{
		payrollDetRs = retrieve(ls_prdcomp,mprdcode,mempcode,mempcode,msitecode,msitecode,mprocgroup_fr,mprocgroup_to)
	
	}*/
protected String gbfPayRollPost(HashMap infoMap ,Connection conn, String mtype)//ls_dataobject'N'
{
	ibase.webitm.ejb.adm.AdmCommon admCommon = new ibase.webitm.ejb.adm.AdmCommon();
	System.out.println(" in gbfPayRollPost()......");
	System.out.println(" Parameter Map ::::::"+infoMap);
	System.out.println(" Type:::-["+mtype+"]");
	
	//Decleration Part
	PreparedStatement pstmt=null;
	ResultSet rs=null;  
	Timestamp frDate = null;
	Timestamp toDate =null;
	Timestamp endDate = null;
	Timestamp startDate = null;
	Timestamp relive = null;
	Timestamp effDate = null;
	Timestamp payDate = null;

	String empCode = null;
	String retString = null;
	String sql =null;
	String adCode = null;
	String postYN = null;
	String prdCode =null;
	String trye=null;
	String siteCode =null;
	String grade = null;
	String cadre = null;
	String design =null;
	String finEntity =null;
	String payTable =null;
	String	siteEmp = null;
	String siteWork = null;
	String tranId = null;
	String 	dropdedInst = null;
	String 	prdComp = 	null;
	String empacSal = null;
	String empccSal =  null;
	String payMode =  null;
	String bankCode =  null;
	String	payble = null;
	String	balOpt = null;
	String acctCodeSal = null;
	String cctrCodeSal = null;
	String chgTerm =null;
	String procGroupFr = null;
	String procGroupTo = 	null;
	String type =null;
	String chgTrem = null;
	String userId =null;
	String refType =null;
	double  rndTo =0;
	String netAd = null;
	String seq =null;
	String adRefType =null;
	String refNo =null;
	String  rndOff =null;
	String adRefNo =null;
	String  withHeld =null;
	String deptCode =null;
	String errString = null;
	String  errcode =null;

	double netPaid =0;
	double amtBal =0;
	double amount = 0;
	double baseAmt =0;
	double resultAmt = 0;
	double slabAmt =0;
	double calcAmt =0; 
	double balAmt =0;
	
	int count =0;
	int	balFr = 0;
	int balTo = 0;
	int lineNo =0;

	//*********************************
	if(infoMap.get("ad_code")!=null)
	{	
  		adCode = 	infoMap.get("ad_code").toString();	//payroll det
	}
	if(infoMap.get("rnd_off")!=null)
	{	
  		rndOff = 	infoMap.get("rnd_off").toString();	//payroll det
	}
	if(infoMap.get("rnd_to")!=null)
	{	
  		rndTo = 	Double.parseDouble(infoMap.get("rnd_to").toString());	//payroll det
	}
	        
         
	if(infoMap.get("amount")!=null)
	{	
  		amount = 	Double.parseDouble(infoMap.get("amount").toString());//payrolldet
	}
	if(infoMap.get("post_yn")!=null)//payrolldet
	{	
  		postYN = 	infoMap.get("post_yn").toString();
	}
	if(infoMap.get("emp_code")!=null)//payrolldet
	{	
  		empCode = 	infoMap.get("emp_code").toString();
	}
	if(infoMap.get("prd_code")!=null)//payrolldet
	{
		prdCode = 	infoMap.get("prd_code").toString();
		prdComp = prdCode;
	}
	if(infoMap.get("type")!=null)//type from filter
	{
		type = 	infoMap.get("type").toString();
	}
	if(infoMap.get("site_code")!=null)//siteCodePay from payroll
	{
		siteCode = 	infoMap.get("site_code").toString();
	}
	if(infoMap.get("dept_code")!=null)//emoloyee or payroll
	{
		deptCode = 	infoMap.get("dept_code").toString();
	}
	if(infoMap.get("grade")!=null)//emoloyee or payroll
	{
		grade = 	infoMap.get("grade").toString();
	}
	if(infoMap.get("cadre")!=null)//emoloyee or payroll
	{
		cadre = 	infoMap.get("cadre").toString();
	}
	if(infoMap.get("design")!=null)//emoloyee or payroll
	{
		design = 	infoMap.get("design").toString();
	}
	if(infoMap.get("fin_entity")!=null)//sitemaster 
	{
		finEntity = 	infoMap.get("fin_entity").toString();
	}
	if(infoMap.get("pay_table")!=null)
	{
		payTable = 	infoMap.get("pay_table").toString();//payroll
	}
	if(infoMap.get("site_emp")!=null)//payroll site_code_emp
	{
		siteEmp = 	infoMap.get("site_emp").toString();
	}
	if(infoMap.get("site_work")!=null)//payroll
	{
		siteWork = 	infoMap.get("site_work").toString();
	}
	if(infoMap.get("eff_date")!=null)//filter
	{
		effDate = 	(Timestamp)infoMap.get("eff_date");//payrolldate
		payDate = effDate;
	}
	if(infoMap.get("proc_group_fr")!=null)//filter
	{
 		procGroupFr = 	infoMap.get("proc_group_fr").toString();
	}
	if(infoMap.get("proc_group_to")!=null)//filter
	{
 		procGroupTo = 	infoMap.get("proc_group_to").toString();
	}
	if(infoMap.get("ref_type")!=null)//filter
	{
 		refType = 	infoMap.get("ref_type").toString();
	}
	if(infoMap.get("ref_no")!=null)//filter
	{
 		refNo = 	infoMap.get("ref_no").toString();
	}
	if(infoMap.get("user_id")!=null)//filter
	{
 		userId = 	infoMap.get("user_id").toString();
	}
	if(infoMap.get("chg_term")!=null)//filter
	{
 		chgTerm = 	infoMap.get("chg_term").toString();
	}
		
	/*if(infoMap.get("pay_date")!=null)
	{
		payDate = 	(Timestamp)infoMap.get("pay_date");
	}
	if(infoMap.get("prd_comp")!=null)//periodCode
	{
 		prdComp = 	infoMap.get("prd_comp").toString();
	}*/
	//N
	try
	{
		sql ="SELECT FR_DATE,TO_DATE TODATE FROM PERIOD WHERE CODE = '"+prdCode+"'";
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next())
		{
			toDate = rs.getTimestamp(1);
			frDate= rs.getTimestamp(2);
			System.out.println("From Date :::-["+frDate+"]");
			System.out.println("To Date :::-["+toDate+"]");
			
		}
		else
		{
			retString = "Data Not Found For Code ["+prdCode+"]";
			return retString;
		}
		rs.close();
		pstmt.close();
		if(toDate!=null)
		{
			sql = "SELECT ACCT_CODE__SAL, CCTR_CODE__SAL, PAY_MODE, BANK_CODE, RELIEVE_DATE, WITH_HELD "
			+" FROM EMPLOYEE WHERE EMP_CODE = '"+empCode+"'";
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				empacSal = rs.getString(1);
				empccSal = rs.getString(2);
				payMode = rs.getString(3);
				bankCode = rs.getString(4);
				relive = rs.getTimestamp(5);
				withHeld =rs.getString(6);
			}
			rs.close();
			pstmt.close();
		}
		netAd = admCommon.getEnv("999999", "NET_AC",conn);	
		if(	netAd.equalsIgnoreCase("NULLFOUND"))
		{
			netAd = admCommon.getEnv("999999", "NET",conn);
			if(	netAd.equalsIgnoreCase("NULLFOUND"))
			{
				retString = "Error VSENVAR1'+'~t'+"+"Environmetal Variable : NET_AC, NET ";
				return retString;
			}
			
		}
		System.out.println("NetAd :-["+netAd+"]");
		dropdedInst = admCommon.getEnv("999999", "DROP_INST",conn);
		if(	dropdedInst.equalsIgnoreCase("NULLFOUND"))
		{
		
				retString = "Error VSENVAR1'+'~t'+"+"Environmetal Variable : DROP_INST";
				return retString;
		}
		System.out.println("dropdedInst :-["+dropdedInst+"]");
		if (type.equalsIgnoreCase("PP"))
		{
			sql = " SELECT AMOUNT  FROM PAYROLLDET WHERE PRD_CODE = '"+prdComp+"' AND EMP_CODE = '"+empCode+"'  AND AD_CODE = '"+netAd+"'";
			
		}
		else
		{
			sql	 = " SELECT DIFF_AMT FROM ARREARDET WHERE PRD_CODE ='"+prdComp+"' AND EMP_CODE =  '"+empCode+"'  AND "
			+" PRD_CODE__ACC = '"+prdCode+"' AND AD_CODE =  '"+netAd+"'";
		}
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next())
		{
			netPaid = rs.getDouble(1);
		}
		rs.close();
		pstmt.close();
		sql = "SELECT PAYABLE, BAL_OPT, ACCT_CODE__SAL, CCTR_CODE__SAL, BAL_FROM, BAL_TO  "
		+"FROM ALLWDEDN WHERE AD_CODE = '"+adCode+"'" ;
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next())
		{
			payble = rs.getString(1);
			balOpt = rs.getString(2);
			acctCodeSal = rs.getString(3);
			cctrCodeSal = rs.getString(4);
			balFr = rs.getInt(5);
			balTo =rs.getInt(6);
		}
		rs.close();
		pstmt.close();
	/*	sql = "SELECT COUNT(*)  FROM PAYROLL  	WHERE PRD_CODE = '"+prdCode+"' AND EMP_CODE = '"+empCode+"'";
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next())
		{
			count = rs.getInt(1);
		}
		rs.close();
		pstmt.close();
		if( count == 0 && relive.compareTo(frDate)<0)
		{
				sql = "INSERT INTO PAYROLL (PRD_CODE, EMP_CODE, SITE_CODE__PAY, DEPT_CODE,	WORK_DAYS, PAID_DAYS, PAY_TABLE, BASIC_AMT, ALLOW_AMT, DEDN_AMT,
				+"	NET_AMT, VOUCHER_NO, GRADE, CADRE, DESIGNATION, FIN_ENTITY, PAYROLL_DATE,CHG_DATE, CHG_USER, CHG_TERM, REMARKS, SITE_CODE__EMP, SITE_CODE__WORK)"
				+"	values (:mprdcode, :mempcode, :msitecode, :mdeptcode,
							0, 0, :mpaytable, 0, 0, 0,
							0, 'PAYROLL', :mgrade, :mcadre, :mdesign, :mfinentity, :mpaydate,
							:mchgdate, :userid, :termid, 'Due to arrear processing', :msiteemp, :msitework);
		}*/
	sql = "  SELECT COUNT(*)  FROM PAYARR_DET WHERE PRD_CODE = '"+prdCode+"' AND EMP_CODE = '"+empCode+"' and AD_CODE = '"+adCode+"'";
	System.out.println("Sql :-"+sql);
	pstmt = conn.prepareStatement(sql);
	rs = pstmt.executeQuery();
	if(rs.next())
	{
		count = rs.getInt(1);
	}
	System.out.println("Count ["+count+"]");
	rs.close();
	pstmt.close();
	if(count==0)
	{
		sql = "INSERT INTO PAYARR_DET "
		+"(PRD_CODE, EMP_CODE, AD_CODE, TYPE,PAYABLE, BASE_AMT, SLAB_AMT, RESULT_AMT, POST_YN, "
		+" CALC_AMT, RND_OFF, RND_TO, AMOUNT, SEQ_NO, REF_TYPE, REF_NO)"
		+" VALUES (?,?, ?, ?, ?, ?, ?, ?,?,?, ?, ?,?, ?, ?, ?) ";
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		System.out.println("PrdCode :-["+prdCode+"]");
		pstmt.setString(1,prdCode.trim());
		System.out.println("empCode :-["+empCode+"]");
		pstmt.setString(2,empCode.trim());
		System.out.println("adCode :-["+adCode+"]");
		pstmt.setString(3,adCode.trim());
		System.out.println("type :-["+type+"]");
		pstmt.setString(4,type);
		System.out.println("payble :-["+payble+"]");
		pstmt.setString(5,payble);

		System.out.println("baseAmt :-["+baseAmt+"]");
		pstmt.setDouble(6,baseAmt);
		System.out.println("slabAmt :-["+slabAmt+"]");
		pstmt.setDouble(7,slabAmt);
		System.out.println("resultAmt :-["+resultAmt+"]");
		pstmt.setDouble(8,resultAmt);
		System.out.println("postYN :-["+postYN+"]");
		pstmt.setString(9,(postYN==null?"":postYN));
		System.out.println("calcAmt :-["+calcAmt+"]");
		pstmt.setDouble(10,calcAmt);
		System.out.println("rndOff :-["+rndOff+"]");
		pstmt.setString(11,(rndOff==null?"":rndOff));
		System.out.println("rndTo :-["+rndTo+"]");
		pstmt.setDouble(12,rndTo);
		System.out.println("amount :-["+amount+"]");
		pstmt.setDouble(13,amount);
		System.out.println("seq :-["+seq+"]");
		pstmt.setString(14,(seq==null?"":seq));
		System.out.println("adRefType :-["+adRefType+"]");
		pstmt.setString(15,(adRefType==null?"":adRefType));
		System.out.println("adRefNo :-["+adRefNo+"]");
		pstmt.setString(16,(adRefNo==null?"":adRefNo));
		
		System.out.println("No of Record Insert in the payarr_det ["+pstmt.executeUpdate()+"]");
		pstmt.close();
	}
	else
	{
		sql = " UPDATE PAYARR_DET SET AMOUNT = AMOUNT + ?, BASE_AMT = BASE_AMT + ?, SLAB_AMT = SLAB_AMT + ?,"
		+" RESULT_AMT = RESULT_AMT + ? 		WHERE PRD_CODE = '"+prdCode+"' AND EMP_CODE = '"+empCode+"' AND AD_CODE = '"+adCode+"'";
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		pstmt.setDouble(1,amount);
		pstmt.setDouble(2,baseAmt);
		pstmt.setDouble(3,slabAmt);
		pstmt.setDouble(4,resultAmt);
		System.out.println("No of Record Update in the payarr_det ["+pstmt.executeUpdate()+"]");
		pstmt.close();

	}	
				System.out.println("Balance Option :-["+balOpt+"]");
				System.out.println("ReferenceType :-["+refType+"]");
				if (amount != 0)
				{
						if (("C".equalsIgnoreCase(balOpt)) || ("A".equalsIgnoreCase(balOpt)))
						{
						
								if("L".equalsIgnoreCase(refType))
								{
										sql="SELECT COUNT(*) FROM ALLOWDEDN_BAL 	WHERE EMP_CODE  ='"+empCode+"'"
										+"  AND AD_CODE   = '"+adCode+"'"
										+"	AND ? >= START_DATE "//mfrdate
										+"AND REF_TYPE  ='"+refType+"'"
										+"	AND REF_NO    = '"+refNo+"'";
										System.out.println("Sql ->"+sql);
										System.out.println("frDate ->["+frDate+"]");
										pstmt = conn.prepareStatement(sql);
										pstmt.setTimestamp(1,frDate);
										rs = pstmt.executeQuery();
										if(rs.next())
										{
											count = rs.getInt(1);
										}
										rs.close();
										pstmt.close();
								}
								else
								{
										sql = "SELECT COUNT(*) FROM ALLOWDEDN_BAL WHERE EMP_CODE  ='"+empCode+"'"
										+"AND AD_CODE  ='"+adCode+"'"
										+"AND  ? >= START_DATE " //:mfrdate
										+"AND ?<= END_DATE ";//:mfrdate 
										System.out.println("Sql ->"+sql);
										System.out.println("frDate ->["+frDate+"]");
										pstmt = conn.prepareStatement(sql);
										pstmt.setTimestamp(1,frDate);
										pstmt.setTimestamp(2,frDate);
										rs = pstmt.executeQuery();
										if(rs.next())
										{
											count = rs.getInt(1);
										}
										rs.close();
										pstmt.close();
								}
					
			
				if(count==0)
				{
					if(("A".equalsIgnoreCase(balOpt)) ||("N".equalsIgnoreCase(postYN)))
					{

								/****** commented 0n 19/01/01
								** ld_start = datetime(date(year(date(mfrdate)), li_balfr, 1))
								** if li_balto < li_balfr then
								** 		ld_end = datetime(date(year(date(mfrdate)) + 1, li_balto, 1))
								** else
								** 		ld_end = datetime(date(year(date(mfrdate)), li_balto, 1))
								** end if
								** added on 19/01/01							
								*******/
								
								if(balTo<balFr && getMonth(frDate) < balFr)
								{
									String dateStr = new Integer(getYear(frDate,-1)).toString()+ "-"+new Integer(balFr).toString()+"-"+new Integer(1).toString();
									startDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
									System.out.println("StartDate :-"+startDate);

								}
								else
								{	
									String dateStr = new Integer(getYear(frDate,0)).toString()+ "-"+new Integer(balFr).toString()+"-"+new Integer(1).toString();
									startDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
									System.out.println("StartDate :-"+startDate);
								}
								if ((balTo < balFr &&  getMonth(frDate)< balFr) ||	(balTo > balFr) )
								{
									String dateStr = new Integer(getYear(frDate,0)).toString()+ "-"+new Integer(balTo).toString()+"-"+new Integer(1).toString();
									endDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
									System.out.println("EndDate :-"+endDate);
								}
								else
								{
									String dateStr = new Integer(getYear(frDate,1)).toString()+ "-"+new Integer(balTo).toString()+"-"+new Integer(1).toString();
									endDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
									System.out.println("EndDate :-"+endDate);
									
								}
								
								//ld_end = gf_lastdate(ld_end)
								sql = " INSERT INTO ALLOWDEDN_BAL "
								+" 	(EMP_CODE, AD_CODE, START_DATE, END_DATE, AMOUNT, AMT_ADJ, AMT_BAL, CHG_DATE, CHG_USER, CHG_TERM, REF_TYPE)"
								+"VALUES (?, ?, ?, ?, ?,0, ?, ?, ?,?, 'K')";
								System.out.println("Sql :-"+sql);
								System.out.println("sql :-"+sql);
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,empCode);
								pstmt.setString(2,adCode);
								pstmt.setTimestamp(3,startDate);
								pstmt.setTimestamp(4,endDate);
								pstmt.setDouble(5,amount);
								pstmt.setDouble(6,amount);
								pstmt.setTimestamp(7,new java.sql.Timestamp(System.currentTimeMillis()));
								pstmt.setString(8,userId);
								pstmt.setString(9,chgTrem);
								System.out.println("No of record insert in the allowdedn_bal ["+pstmt.executeUpdate()+"]");
						}
						else
						{
							retString  = "VPNOBAL1";
						}
				}
				else
				{
					 if (("A".equalsIgnoreCase(balOpt) || ("N".equalsIgnoreCase(postYN))))
					 {
						if ("L".equalsIgnoreCase(refType))
						{
						
							sql = " UPDATE ALLOWDEDN_BAL   SET AMOUNT   = (AMOUNT + ?), "
							+"    AMT_BAL  = (AMT_BAL + ?),"
							+" CHG_USER = '"+userId+"'," 
							+"CHG_DATE = ?,"
							+" CHG_TERM ='"+chgTerm+"'"
							+" WHERE EMP_CODE = '"+empCode+"'"
							+" AND AD_CODE  = '"+adCode+"'"
							+"  AND ?>= START_DATE "
							+"  AND REF_TYPE = '"+refType+"'"
							+" AND REF_NO   = '"+refNo+"'";
							System.out.println("Sql :-"+sql);
							System.out.println("Sql ->"+sql);
							System.out.println("frDate ->["+frDate+"]");
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1,amount);
							pstmt.setDouble(2,amount);
							pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
							pstmt.setTimestamp(4,frDate);
							System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
							pstmt.close();
							
						}
						else
						{
									sql = 	"UPDATE ALLOWDEDN_BAL "
										+"   SET AMOUNT   = AMOUNT + ?, "
										+ "AMT_BAL  = AMT_BAL + ?,"
										+" CHG_USER = '"+userId+"'," 
										+" CHG_DATE = ? ,"
										+" CHG_TERM = '"+chgTerm+"'"
										+"WHERE EMP_CODE = '"+empCode+"'"
										+"AND AD_CODE  = '"+adCode+"'"
										+"AND ? >= START_DATE "
										+"AND ? <= END_DATE";
										System.out.println("Sql :-"+sql);
									System.out.println("Sql ->"+sql);
									System.out.println("Amount ->"+amount);
									System.out.println("frDate ->"+frDate);
									pstmt = conn.prepareStatement(sql);
									pstmt.setDouble(1,amount);
									pstmt.setDouble(2,amount);
									pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
									pstmt.setTimestamp(4,frDate);
									pstmt.setTimestamp(5,frDate);
									System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
						}
					}
        	      else 
        	      {
						if ("L".equalsIgnoreCase(refType)) 
						{					
							sql = "SELECT AMT_BAL "
							+"  FROM ALLOWDEDN_BAL "
							+"	 WHERE EMP_CODE ='"+empCode+"'"
							+"   AND AD_CODE  = '"+adCode+"'"
							+" AND ? >= START_DATE "
							+" AND REF_TYPE = '"+refType+"'"
							+"	AND REF_NO   = '"+refNo+"'"
							+"AND AMT_BAL  <> 0";
							System.out.println("Sql :-"+sql);
							pstmt = conn.prepareStatement(sql);
							pstmt.setTimestamp(1,frDate);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								amtBal = rs.getDouble(1);
							}
							rs.close();
							pstmt.close();
						}
						else
						{
							sql="SELECT AMT_BAL  FROM ALLOWDEDN_BAL "
							+" WHERE EMP_CODE = '"+empCode+"'" 
							+" AND AD_CODE  = '"+adCode+"'"
							+" AND ? >= START_DATE"
							+" AND ? <= END_DATE "
							+" AND AMT_BAL  <> 0 ";
							System.out.println("Sql :-"+sql);
							pstmt = conn.prepareStatement(sql);
							pstmt.setTimestamp(1,frDate);
							pstmt.setTimestamp(2,frDate);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								amtBal = rs.getDouble(1);
							}
							rs.close();
							pstmt.close();
						}
					}
					if ("L".equalsIgnoreCase(refType)) 
					{			
							sql = "UPDATE ALLOWDEDN_BAL   SET AMT_BAL  = (AMT_BAL - ?),"
							+"	 AMT_ADJ  = AMT_ADJ + ?, "
							+" CHG_USER = ?,"
							+"CHG_DATE = ?,"
							+" CHG_TERM = ?"
							+" WHERE EMP_CODE = '"+empCode+"'"
							+" AND AD_CODE  = '"+adCode+"'"	
							+" AND ? >= START_DATE"
							+"  AND REF_TYPE = '"+refType+"'"
							+"  AND REF_NO   = '"+refNo+"'"
							+"  AND AMT_BAL  <> 0";	
							System.out.println("Sql :-"+sql);
							System.out.println("Sql ->"+sql);
							System.out.println("Amount ->"+amount);
							System.out.println("frDate ->"+frDate);
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1,amount);
							pstmt.setDouble(2,amount);
							pstmt.setString(3,userId);
							pstmt.setTimestamp(4,new java.sql.Timestamp(System.currentTimeMillis()));
							pstmt.setString(5,chgTerm);
							pstmt.setTimestamp(6,frDate);
							System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
							pstmt.close();

						}							
						else
						{
							sql = "UPDATE ALLOWDEDN_BAL   SET AMT_BAL  = AMT_BAL - ? ,"
							+"	 AMT_ADJ  = AMT_ADJ + ?, "
							+" CHG_USER = ?,"
							+"CHG_DATE = ?,"
							+" CHG_TERM = ? "
							+" WHERE EMP_CODE = '"+empCode+"'"
							+" AND AD_CODE  = '"+adCode+"'"	
							+" AND ? >= START_DATE "
							+" AND ? >END_DATE "
							+"	AND AMT_BAL  <> 0";
							System.out.println("Sql :-"+sql);
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1,amount);
							pstmt.setDouble(2,amount);
							pstmt.setString(3,userId);
							pstmt.setTimestamp(4,new java.sql.Timestamp(System.currentTimeMillis()));
							pstmt.setString(5,chgTerm);
							pstmt.setTimestamp(6,frDate);
							pstmt.setTimestamp(7,frDate);
							System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
							pstmt.close();
						}
				}
			}//end of count allodede_bal
		}//end of amount <>0
				
			if ("L".equalsIgnoreCase(refType) && ("C".equalsIgnoreCase(balOpt)))
			{
						if (amount != 0)
						{
									if ("PP".equalsIgnoreCase(mtype))
									{
											sql = "INSERT INTO LOAN_REG(LOAN_NO , TRAN_NO , TRAN_DATE , TRAN_TYPE , AMOUNT ,INT_CALCULATED , CHG_DATE , CHG_USER , CHG_TERM) "
											+" VALUES(? ,? , ? , 'P' , ? ,'N' , ? , ? ,?)";
											System.out.println("Sql :-"+sql);
											pstmt = conn.prepareStatement(sql);
											pstmt.setString(1,refNo);
											pstmt.setString(2,prdCode);
											pstmt.setTimestamp(3,toDate);
											pstmt.setDouble(4,amount);
											pstmt.setTimestamp(5,new Timestamp(System.currentTimeMillis()));
											pstmt.setString(6,userId);
											pstmt.setString(7,chgTerm);
											System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
											pstmt.close();
									}
									else
									{
										sql ="INSERT INTO LOAN_REG(LOAN_NO , TRAN_NO , TRAN_DATE , TRAN_TYPE , AMOUNT , INT_CALCULATED , CHG_DATE , CHG_USER , CHG_TERM) "
										+"  VALUES(? ,?,? , 'A' , ? , 'N' , ?,? ,?)";
										System.out.println("Sql :-"+sql);
											pstmt = conn.prepareStatement(sql);
												pstmt = conn.prepareStatement(sql);
											pstmt.setString(1,refNo);
											pstmt.setString(2,prdCode+"-"+prdCode);//:ls_prdcomp||'-'||:mprdcode 
											pstmt.setTimestamp(3,toDate);
											pstmt.setDouble(4,amount);
											pstmt.setTimestamp(5,new Timestamp(System.currentTimeMillis()));
											pstmt.setString(6,userId);
											pstmt.setString(7,chgTerm);
											System.out.println("Record Update in LOAN_REG["+pstmt.executeUpdate()+"]");
											pstmt.close();
									}
								}
								
								sql = "SELECT BALANCE_AMT FROM LOANS WHERE LOAN_NO = '"+refNo+"'";
								System.out.println("Sql :-"+sql);
								pstmt = conn.prepareStatement(sql);
							//	pstmt.setTimestamp(1,frDate);
								
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									amtBal = rs.getDouble(1);
								}
								rs.close();
								pstmt.close();
								amtBal = amtBal-amount;
								if(amtBal < 0)
								{
								 	retString = "VPNOBAL1";
								 	return retString;
								
								}
								if (amtBal > 0)
								{
									sql = "UPDATE LOANS SET BALANCE_AMT = BALANCE_AMT - ?, "
									+" DATE__BALANCE = ? WHERE LOAN_NO = '"+refNo+"'" ;
									System.out.println("Sql :-"+sql);
									pstmt = conn.prepareStatement(sql);
									pstmt.setDouble(1,amount);
									pstmt.setTimestamp(2,new java.sql.Timestamp(System.currentTimeMillis()));
									System.out.println("Record Update in LOANS["+pstmt.executeUpdate()+"]");
									pstmt.close();
							
								}
								else	
								{	
									sql = "UPDATE LOANS SET BALANCE_AMT = BALANCE_AMT - ?, "
										+" DATE__BALANCE = ?,  STATUS = 'E' WHERE LOAN_NO = '"+refNo+"'" ;
										System.out.println("Sql :-"+sql);
										pstmt = conn.prepareStatement(sql);
										pstmt.setDouble(1,amount);
										pstmt.setTimestamp(2,new java.sql.Timestamp(System.currentTimeMillis()));
										System.out.println("Record Update in LOANS["+pstmt.executeUpdate()+"]");
										pstmt.close();
								}									
							
								if("PP".equalsIgnoreCase(mtype))
								{
										sql= "SELECT LINE_NO   FROM LOANS_SCHEDULE_DET "
										+" WHERE LOAN_NO  = '"+refNo+"'"
										+"AND PRD_CODE = '"+prdCode+"'"
										+"AND PROCESS_TYPE = 'PY'";
										System.out.println("Sql :-"+sql);
										pstmt = conn.prepareStatement(sql);
									
										rs = pstmt.executeQuery();
										if(rs.next())
										{
											lineNo = rs.getInt(1);
										}
										rs.close();
										pstmt.close();
										if(dropdedInst.equalsIgnoreCase("Y") || amount > 0)
										{
											sql = "UPDATE LOANS_SCHEDULE 	SET NO_INST_DED = NO_INST_DED + 1"
											+" WHERE LOAN_NO = '"+refNo+"'"
											+"AND LINE_NO = "+lineNo;
											System.out.println("Sql :-"+sql);
											pstmt = conn.prepareStatement(sql);
											System.out.println("Record Update in loans_schedule["+pstmt.executeUpdate()+"]");
											pstmt.close();
										}
											sql =" UPDATE LOANS_SCHEDULE_DET SET STATUS = 'U'"
											+" WHERE LOAN_NO  =  '"+refNo+"'"
											+" AND PRD_CODE = '"+prdCode+"'"
											+"AND PROCESS_TYPE = 'PY'";
											System.out.println("Sql :-"+sql);
											pstmt = conn.prepareStatement(sql);
											System.out.println("Record Update in loans_schedule["+pstmt.executeUpdate()+"]");
											pstmt.close();
							
							}
							sql ="SELECT BALANCE_AMT  FROM LOANS WHERE LOAN_NO =  '"+refNo+"'";
							System.out.println("Sql :-"+sql);
						 	pstmt = conn.prepareStatement(sql);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								balAmt = rs.getDouble(1);
							}
							rs.close();
							pstmt.close();
							sql = " UPDATE PAYARR_DET 	SET AMT_BAL  = ? WHERE PRD_CODE = '"+prdCode+"'"
							+"	AND EMP_CODE = '"+empCode+"' AND  AD_CODE  = '"+adCode+"'";
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1,balAmt);
							System.out.println("Record Update in payarr_det["+pstmt.executeUpdate()+"]");
							pstmt.close();
					
		}
	}
	catch(Exception e)
	{
		java.io.ByteArrayOutputStream byteBuff = new java.io.ByteArrayOutputStream();
		java.io.PrintStream printBuff= new PrintStream(byteBuff);
		e.printStackTrace(printBuff);
		retString = byteBuff.toString();//"Exception";
		e.printStackTrace();
	}
	return (retString);
	}//end of method
	public int getMonth(java.sql.Timestamp date)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(cal.MONTH)+1;
		cal = null;
		return month;
	}
	public int getYear(java.sql.Timestamp date,int yr)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(cal.YEAR);
		cal = null;
		return (year+yr);
	}
	
	//added by msalam on 01/10/07 for over loading function so as to proper updates of tables in case of contract voucher generation
	protected String gbfPayRollPost(HashMap infoMap ,Connection conn,String mtype, String vouchType)//ls_dataobject'N'
	{
		ibase.webitm.ejb.adm.AdmCommon admCommon = new ibase.webitm.ejb.adm.AdmCommon();
		System.out.println(" in gbfPayRollPost()......");
		System.out.println(" Parameter Map ::::::"+infoMap);
		System.out.println(" Type:::-["+mtype+"]");
		
		//Decleration Part
		PreparedStatement pstmt=null;
		ResultSet rs=null;  
		Timestamp frDate = null;
		Timestamp toDate =null;
		Timestamp endDate = null;
		Timestamp startDate = null;
		Timestamp relive = null;
		Timestamp effDate = null;
		Timestamp payDate = null;
	
		String empCode = null;
		String retString = null;
		String sql =null;
		String adCode = null;
		String postYN = null;
		String prdCode =null;
		String trye=null;
		String siteCode =null;
		String grade = null;
		String cadre = null;
		String design =null;
		String finEntity =null;
		String payTable =null;
		String	siteEmp = null;
		String siteWork = null;
		String tranId = null;
		String 	dropdedInst = null;
		String 	prdComp = 	null;
		String empacSal = null;
		String empccSal =  null;
		String payMode =  null;
		String bankCode =  null;
		String	payble = null;
		String	balOpt = null;
		String acctCodeSal = null;
		String cctrCodeSal = null;
		String chgTerm =null;
		String procGroupFr = null;
		String procGroupTo = 	null;
		String type =null;
		String chgTrem = null;
		String userId =null;
		String refType =null;
		double  rndTo =0;
		String netAd = null;
		String seq =null;
		String adRefType =null;
		String refNo =null;
		String  rndOff =null;
		String adRefNo =null;
		String  withHeld =null;
		String deptCode =null;
		String errString = null;
		String  errcode =null;
	
		double netPaid =0;
		double amtBal =0;
		double amount = 0;
		double baseAmt =0;
		double resultAmt = 0;
		double slabAmt =0;
		double calcAmt =0; 
		double balAmt =0;
		
		int count =0;
		int	balFr = 0;
		int balTo = 0;
		int lineNo =0;
	
		//*********************************
		if(infoMap.get("ad_code")!=null)
		{	
	  		adCode = 	infoMap.get("ad_code").toString();	//payroll det
		}
		if(infoMap.get("rnd_off")!=null)
		{	
	  		rndOff = 	infoMap.get("rnd_off").toString();	//payroll det
		}
		if(infoMap.get("rnd_to")!=null)
		{	
	  		rndTo = 	Double.parseDouble(infoMap.get("rnd_to").toString());	//payroll det
		}
		        
	         
		if(infoMap.get("amount")!=null)
		{	
	  		amount = 	Double.parseDouble(infoMap.get("amount").toString());//payrolldet
		}
		if(infoMap.get("post_yn")!=null)//payrolldet
		{	
	  		postYN = 	infoMap.get("post_yn").toString();
		}
		if(infoMap.get("emp_code")!=null)//payrolldet
		{	
	  		empCode = 	infoMap.get("emp_code").toString();
		}
		if(infoMap.get("prd_code")!=null)//payrolldet
		{
			prdCode = 	infoMap.get("prd_code").toString();
			prdComp = prdCode;
		}
		if(infoMap.get("type")!=null)//type from filter
		{
			type = 	infoMap.get("type").toString();
		}
		if(infoMap.get("site_code")!=null)//siteCodePay from payroll
		{
			siteCode = 	infoMap.get("site_code").toString();
		}
		if(infoMap.get("dept_code")!=null)//emoloyee or payroll
		{
			deptCode = 	infoMap.get("dept_code").toString();
		}
		if(infoMap.get("grade")!=null)//emoloyee or payroll
		{
			grade = 	infoMap.get("grade").toString();
		}
		if(infoMap.get("cadre")!=null)//emoloyee or payroll
		{
			cadre = 	infoMap.get("cadre").toString();
		}
		if(infoMap.get("design")!=null)//emoloyee or payroll
		{
			design = 	infoMap.get("design").toString();
		}
		if(infoMap.get("fin_entity")!=null)//sitemaster 
		{
			finEntity = 	infoMap.get("fin_entity").toString();
		}
		if(infoMap.get("pay_table")!=null)
		{
			payTable = 	infoMap.get("pay_table").toString();//payroll
		}
		if(infoMap.get("site_emp")!=null)//payroll site_code_emp
		{
			siteEmp = 	infoMap.get("site_emp").toString();
		}
		if(infoMap.get("site_work")!=null)//payroll
		{
			siteWork = 	infoMap.get("site_work").toString();
		}
		if(infoMap.get("eff_date")!=null)//filter
		{
			effDate = 	(Timestamp)infoMap.get("eff_date");//payrolldate
			payDate = effDate;
		}
		if(infoMap.get("proc_group_fr")!=null)//filter
		{
	 		procGroupFr = 	infoMap.get("proc_group_fr").toString();
		}
		if(infoMap.get("proc_group_to")!=null)//filter
		{
	 		procGroupTo = 	infoMap.get("proc_group_to").toString();
		}
		if(infoMap.get("ref_type")!=null)//filter
		{
	 		refType = 	infoMap.get("ref_type").toString();
		}
		if(infoMap.get("ref_no")!=null)//filter
		{
	 		refNo = 	infoMap.get("ref_no").toString();
		}
		if(infoMap.get("user_id")!=null)//filter
		{
	 		userId = 	infoMap.get("user_id").toString();
		}
		if(infoMap.get("chg_term")!=null)//filter
		{
	 		chgTerm = 	infoMap.get("chg_term").toString();
		}
		
		//added by msalam on 03/01/07 start
		//parameterMap.put( "base_amt", Double.toString(baseAmt) );
		//parameterMap.put( "slab_amt", Double.toString(slabAmt) );
		//parameterMap.put( "result_amt", Double.toString(resultAmt) );
		if(infoMap.get("base_amt")!=null)
		{	
	  		baseAmt = 	Double.parseDouble(infoMap.get("base_amt").toString());//payrolldet
		}
		if(infoMap.get("slab_amt")!=null)
		{	
	  		slabAmt = 	Double.parseDouble(infoMap.get("slab_amt").toString());//payrolldet
		}
		if(infoMap.get("result_amt")!=null)
		{	
	  		resultAmt = 	Double.parseDouble(infoMap.get("result_amt").toString());//payrolldet
		}
		if(infoMap.get("calc_amt")!=null)
		{	
	  		calcAmt = Double.parseDouble(infoMap.get("calc_amt").toString());//payrolldet
		}
		if(infoMap.get("seq_no")!=null)
		{	
	  		seq = infoMap.get("seq_no").toString();//payrolldet
		}
		adRefType = refType;
		//added by msalam on 03/01/07 start

		
		
		/*if(infoMap.get("pay_date")!=null)
		{
			payDate = 	(Timestamp)infoMap.get("pay_date");
		}
		if(infoMap.get("prd_comp")!=null)//periodCode
		{
	 		prdComp = 	infoMap.get("prd_comp").toString();
		}*/
		//N
		try
		{
			sql ="SELECT FR_DATE,TO_DATE TODATE FROM PERIOD WHERE CODE = '"+prdCode+"'";
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				//changed by msalam as not proper on 24/12/07 at sun
				//toDate = rs.getTimestamp(1);
				//frDate= rs.getTimestamp(2);
				toDate = rs.getTimestamp( "TODATE" );
				frDate= rs.getTimestamp( "FR_DATE" );

				System.out.println("From Date :::-["+frDate+"]");
				System.out.println("To Date :::-["+toDate+"]");
				
			}
			else
			{
				retString = "Data Not Found For Code ["+prdCode+"]";
				return retString;
			}
			rs.close();
			pstmt.close();
			if(toDate!=null)
			{
				sql = "SELECT ACCT_CODE__SAL, CCTR_CODE__SAL, PAY_MODE, BANK_CODE, RELIEVE_DATE, WITH_HELD "
				+" FROM EMPLOYEE WHERE EMP_CODE = '"+empCode+"'";
				System.out.println("Sql :-"+sql);
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					empacSal = rs.getString(1);
					empccSal = rs.getString(2);
					payMode = rs.getString(3);
					bankCode = rs.getString(4);
					relive = rs.getTimestamp(5);
					withHeld =rs.getString(6);
				}
				rs.close();
				pstmt.close();
			}
			netAd = admCommon.getEnv("999999", "NET_AC",conn);	
			if(	netAd.equalsIgnoreCase("NULLFOUND"))
			{
				netAd = admCommon.getEnv("999999", "NET",conn);
				if(	netAd.equalsIgnoreCase("NULLFOUND"))
				{
					retString = "Error VSENVAR1'+'~t'+"+"Environmetal Variable : NET_AC, NET ";
					return retString;
				}
				
			}
			System.out.println("NetAd :-["+netAd+"]");
			dropdedInst = admCommon.getEnv("999999", "DROP_INST",conn);
			if(	dropdedInst.equalsIgnoreCase("NULLFOUND"))
			{
			
					retString = "Error VSENVAR1'+'~t'+"+"Environmetal Variable : DROP_INST";
					return retString;
			}
			System.out.println("dropdedInst :-["+dropdedInst+"]");
			if (mtype.equalsIgnoreCase("PP"))
			{
				sql = " SELECT AMOUNT  FROM PAYROLLDET WHERE PRD_CODE = '"+prdComp+"' AND EMP_CODE = '"+empCode+"'  AND AD_CODE = '"+netAd+"'";
				
			}
			else
			{
				sql	 = " SELECT DIFF_AMT FROM ARREARDET WHERE PRD_CODE ='"+prdComp+"' AND EMP_CODE =  '"+empCode+"'  AND "
				+" PRD_CODE__ACC = '"+prdCode+"' AND AD_CODE =  '"+netAd+"'";
			}
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				netPaid = rs.getDouble(1);
			}
			rs.close();
			pstmt.close();
			sql = "SELECT PAYABLE, BAL_OPT, ACCT_CODE__SAL, CCTR_CODE__SAL, BAL_FROM, BAL_TO  "
			+"FROM ALLWDEDN WHERE AD_CODE = '"+adCode+"'" ;
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				payble = rs.getString(1);
				balOpt = rs.getString(2);
				acctCodeSal = rs.getString(3);
				cctrCodeSal = rs.getString(4);
				balFr = rs.getInt(5);
				balTo =rs.getInt(6);
			}
			rs.close();
			pstmt.close();
		/*	sql = "SELECT COUNT(*)  FROM PAYROLL  	WHERE PRD_CODE = '"+prdCode+"' AND EMP_CODE = '"+empCode+"'";
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				count = rs.getInt(1);
			}
			rs.close();
			pstmt.close();
			if( count == 0 && relive.compareTo(frDate)<0)
			{
					sql = "INSERT INTO PAYROLL (PRD_CODE, EMP_CODE, SITE_CODE__PAY, DEPT_CODE,	WORK_DAYS, PAID_DAYS, PAY_TABLE, BASIC_AMT, ALLOW_AMT, DEDN_AMT,
					+"	NET_AMT, VOUCHER_NO, GRADE, CADRE, DESIGNATION, FIN_ENTITY, PAYROLL_DATE,CHG_DATE, CHG_USER, CHG_TERM, REMARKS, SITE_CODE__EMP, SITE_CODE__WORK)"
					+"	values (:mprdcode, :mempcode, :msitecode, :mdeptcode,
								0, 0, :mpaytable, 0, 0, 0,
								0, 'PAYROLL', :mgrade, :mcadre, :mdesign, :mfinentity, :mpaydate,
								:mchgdate, :userid, :termid, 'Due to arrear processing', :msiteemp, :msitework);
			}*/
		sql = "  SELECT COUNT(*)  FROM PAYARR_DET WHERE PRD_CODE = '"+prdCode+"' AND EMP_CODE = '"+empCode+"' and AD_CODE = '"+adCode+"'";
		System.out.println("Sql :-"+sql);
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next())
		{
			count = rs.getInt(1);
		}
		System.out.println("Count ["+count+"]");
		rs.close();
		pstmt.close();
		if(count==0)
		{
			sql = "INSERT INTO PAYARR_DET "
			+"(PRD_CODE, EMP_CODE, AD_CODE, TYPE,PAYABLE, BASE_AMT, SLAB_AMT, RESULT_AMT, POST_YN, "
			+" CALC_AMT, RND_OFF, RND_TO, AMOUNT, SEQ_NO, REF_TYPE, REF_NO)"
			+" VALUES (?,?, ?, ?, ?, ?, ?, ?,?,?, ?, ?,?, ?, ?, ?) ";
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			System.out.println("PrdCode :-["+prdCode+"]");
			pstmt.setString(1,prdCode.trim());
			System.out.println("empCode :-["+empCode+"]");
			pstmt.setString(2,empCode.trim());
			System.out.println("adCode :-["+adCode+"]");
			pstmt.setString(3,adCode.trim());
			System.out.println("type :-["+type+"]");
			pstmt.setString(4,type);
			System.out.println("payble :-["+payble+"]");
			pstmt.setString(5,payble);
			System.out.println("baseAmt :-["+baseAmt+"]");
			pstmt.setDouble(6,baseAmt);
			System.out.println("slabAmt :-["+slabAmt+"]");
			pstmt.setDouble(7,slabAmt);
			System.out.println("resultAmt :-["+resultAmt+"]");
			pstmt.setDouble(8,resultAmt);
			System.out.println("postYN :-["+postYN+"]");
			pstmt.setString(9,(postYN==null?"":postYN));
			System.out.println("calcAmt :-["+calcAmt+"]");
			pstmt.setDouble(10,calcAmt);
			System.out.println("rndOff :-["+rndOff+"]");
			pstmt.setString(11,(rndOff==null?"":rndOff));
			System.out.println("rndTo :-["+rndTo+"]");
			pstmt.setDouble(12,rndTo);
			System.out.println("amount :-["+amount+"]");
			pstmt.setDouble(13,amount);
			System.out.println("seq :-["+seq+"]");
			pstmt.setString(14,(seq==null?"":seq));
			System.out.println("adRefType :-["+adRefType+"]");
			pstmt.setString(15,(adRefType==null?"":adRefType));
			System.out.println("adRefNo :-["+adRefNo+"]");
			pstmt.setString(16,(adRefNo==null?"":adRefNo));
			System.out.println("No of Record Insert in the payarr_det ["+pstmt.executeUpdate()+"]");
			pstmt.close();
		}
		else
		{
			sql = " UPDATE PAYARR_DET SET AMOUNT = AMOUNT + ?, BASE_AMT = BASE_AMT + ?, SLAB_AMT = SLAB_AMT + ?,"
			+" RESULT_AMT = RESULT_AMT + ? 		WHERE PRD_CODE = '"+prdCode+"' AND EMP_CODE = '"+empCode+"' AND AD_CODE = '"+adCode+"'";
			System.out.println("Sql :-"+sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setDouble(1,amount);
			pstmt.setDouble(2,baseAmt);
			pstmt.setDouble(3,slabAmt);
			pstmt.setDouble(4,resultAmt);
			System.out.println("No of Record Update in the payarr_det ["+pstmt.executeUpdate()+"]");
			pstmt.close();
	
		}	
					System.out.println("Balance Option :-["+balOpt+"]");
					System.out.println("ReferenceType :-["+refType+"]");
					if (amount != 0)
					{
							if (("C".equalsIgnoreCase(balOpt)) || ("A".equalsIgnoreCase(balOpt)))
							{
							
									if("L".equalsIgnoreCase(refType))
									{
											sql="SELECT COUNT(*) FROM ALLOWDEDN_BAL 	WHERE EMP_CODE  ='"+empCode+"'"
																						+"  AND AD_CODE   = '"+adCode+"'"
																						+"	AND ? >= START_DATE "//mfrdate
											+"AND REF_TYPE  ='"+refType+"'"
											+"	AND REF_NO    = '"+refNo+"'";
											System.out.println("Sql ->"+sql);
											System.out.println("frDate ->["+frDate+"]");
											pstmt = conn.prepareStatement(sql);
											pstmt.setTimestamp(1,frDate);
											rs = pstmt.executeQuery();
											if(rs.next())
											{
												count = rs.getInt(1);
											}
											rs.close();
											pstmt.close();
									}
									else
									{
											sql = "SELECT COUNT(*) FROM ALLOWDEDN_BAL WHERE EMP_CODE  ='"+empCode+"'"
											+"AND AD_CODE  ='"+adCode+"'"
											+"AND  ? >= START_DATE " //:mfrdate
											+"AND ? <= END_DATE ";//:mfrdate 
											System.out.println("Sql ->"+sql);
											System.out.println("frDate ->["+frDate+"]");
											System.out.println("toDate ->["+toDate+"]");
											pstmt = conn.prepareStatement(sql);
											pstmt.setTimestamp(1,frDate);
											pstmt.setTimestamp(2,frDate);
											//next line changed by msalam on 241207 to set todate in end_date
											//pstmt.setTimestamp( 2, toDate);
											rs = pstmt.executeQuery();
											if(rs.next())
											{
												count = rs.getInt(1);
											}
											rs.close();
											pstmt.close();
									}
						
				
					if(count==0)
					{
						if(("A".equalsIgnoreCase(balOpt)) ||("N".equalsIgnoreCase(postYN)))
						{
	
									/****** commented 0n 19/01/01
									** ld_start = datetime(date(year(date(mfrdate)), li_balfr, 1))
									** if li_balto < li_balfr then
									** 		ld_end = datetime(date(year(date(mfrdate)) + 1, li_balto, 1))
									** else
									** 		ld_end = datetime(date(year(date(mfrdate)), li_balto, 1))
									** end if
									** added on 19/01/01							
									*******/
									
									if(balTo<balFr && getMonth(frDate) < balFr)
									{
										String dateStr = new Integer(getYear(frDate,-1)).toString()+ "-"+new Integer(balFr).toString()+"-"+new Integer(1).toString();
										startDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
										System.out.println("StartDate :-"+startDate);
	
									}
									else
									{	
										String dateStr = new Integer(getYear(frDate,0)).toString()+ "-"+new Integer(balFr).toString()+"-"+new Integer(1).toString();
										startDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
										System.out.println("StartDate :-"+startDate);
									}
									if ((balTo < balFr &&  getMonth(frDate)< balFr) ||	(balTo > balFr) )
									{
										String dateStr = new Integer(getYear(frDate,0)).toString()+ "-"+new Integer(balTo).toString()+"-"+new Integer(1).toString();
										endDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
										System.out.println("EndDate :-"+endDate);
									}
									else
									{
										String dateStr = new Integer(getYear(frDate,1)).toString()+ "-"+new Integer(balTo).toString()+"-"+new Integer(1).toString();
										endDate =	java.sql.Timestamp.valueOf(dateStr + " 00:00:00.0");
										System.out.println("EndDate :-"+endDate);
										
									}
									
									//ld_end = gf_lastdate(ld_end)
									sql = " INSERT INTO ALLOWDEDN_BAL "
									+" 	(EMP_CODE, AD_CODE, START_DATE, END_DATE, AMOUNT, AMT_ADJ, AMT_BAL, CHG_DATE, CHG_USER, CHG_TERM, REF_TYPE)"
									+"VALUES (?, ?, ?, ?, ?,0, ?, ?, ?,?, 'K')";
									System.out.println("Sql :-"+sql);
									System.out.println("sql :-"+sql);
									pstmt = conn.prepareStatement(sql);
									pstmt.setString(1,empCode);
									pstmt.setString(2,adCode);
									pstmt.setTimestamp(3,startDate);
									pstmt.setTimestamp(4,endDate);
									pstmt.setDouble(5,amount);
									pstmt.setDouble(6,amount);
									pstmt.setTimestamp(7,new java.sql.Timestamp(System.currentTimeMillis()));
									pstmt.setString(8,userId);
									pstmt.setString(9,chgTrem);
									System.out.println("No of record insert in the allowdedn_bal ["+pstmt.executeUpdate()+"]");
							}
							else
							{
								retString  = "VPNOBAL1";
							}
					}
					else
					{
						 if (("A".equalsIgnoreCase(balOpt) || ("N".equalsIgnoreCase(postYN))))
						 {
							if ("L".equalsIgnoreCase(refType))
							{
							
								sql = " UPDATE ALLOWDEDN_BAL   SET AMOUNT   = (AMOUNT + ?), "
								+"    AMT_BAL  = (AMT_BAL + ?),"
								+" CHG_USER = '"+userId+"'," 
								+"CHG_DATE = ?,"
								+" CHG_TERM ='"+chgTerm+"'"
								+" WHERE EMP_CODE = '"+empCode+"'"
								+" AND AD_CODE  = '"+adCode+"'"
								+"  AND ?>= START_DATE "
								+"  AND REF_TYPE = '"+refType+"'"
								+" AND REF_NO   = '"+refNo+"'";
								System.out.println("Sql :-"+sql);
								System.out.println("Sql ->"+sql);
								System.out.println("frDate ->["+frDate+"]");
								pstmt = conn.prepareStatement(sql);
								pstmt.setDouble(1,amount);
								pstmt.setDouble(2,amount);
								pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
								pstmt.setTimestamp(4,frDate);
								System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
								pstmt.close();
								
							}
							else
							{
										sql = 	"UPDATE ALLOWDEDN_BAL "
											+"   SET AMOUNT   = AMOUNT + ?, "
											+ "AMT_BAL  = AMT_BAL + ?,"
											+" CHG_USER = '"+userId+"'," 
											+" CHG_DATE = ? ,"
											+" CHG_TERM = '"+chgTerm+"'"
											+"WHERE EMP_CODE = '"+empCode+"'"
											+"AND AD_CODE  = '"+adCode+"'"
											+"AND ? >= START_DATE "
											+"AND ? <= END_DATE";
											System.out.println("Sql :-"+sql);
										System.out.println("Sql ->"+sql);
										System.out.println("Amount ->"+amount);
										System.out.println("frDate ->"+frDate);
										pstmt = conn.prepareStatement(sql);
										pstmt.setDouble(1,amount);
										pstmt.setDouble(2,amount);
										pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
										pstmt.setTimestamp(4,frDate);
										pstmt.setTimestamp(5,frDate);
										System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
							}
						}
	        	      else 
	        	      {
							if ("L".equalsIgnoreCase(refType)) 
							{					
								sql = "SELECT AMT_BAL "
								+"  FROM ALLOWDEDN_BAL "
								+"	 WHERE EMP_CODE ='"+empCode+"'"
								+"   AND AD_CODE  = '"+adCode+"'"
								+" AND ? >= START_DATE "
								+" AND REF_TYPE = '"+refType+"'"
								+"	AND REF_NO   = '"+refNo+"'"
								+"AND AMT_BAL  <> 0";
								System.out.println("Sql :-"+sql);
								pstmt = conn.prepareStatement(sql);
								pstmt.setTimestamp(1,frDate);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									amtBal = rs.getDouble(1);
								}
								rs.close();
								pstmt.close();
							}
							else
							{
								sql="SELECT AMT_BAL  FROM ALLOWDEDN_BAL "
								+" WHERE EMP_CODE = '"+empCode+"'" 
								+" AND AD_CODE  = '"+adCode+"'"
								+" AND ? >= START_DATE"
								+" AND ? <= END_DATE "
								+" AND AMT_BAL  <> 0 ";
								System.out.println("Sql :-"+sql);
								pstmt = conn.prepareStatement(sql);
								pstmt.setTimestamp(1,frDate);
								pstmt.setTimestamp(2,frDate);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									amtBal = rs.getDouble(1);
								}
								rs.close();
								pstmt.close();
							}
						}
						if ("L".equalsIgnoreCase(refType)) 
						{			
								sql = "UPDATE ALLOWDEDN_BAL   SET AMT_BAL  = (AMT_BAL - ?),"
								+"	 AMT_ADJ  = AMT_ADJ + ?, "
								+" CHG_USER = ?,"
								+"CHG_DATE = ?,"
								+" CHG_TERM = ?"
								+" WHERE EMP_CODE = '"+empCode+"'"
								+" AND AD_CODE  = '"+adCode+"'"	
								+" AND ? >= START_DATE"
								+"  AND REF_TYPE = '"+refType+"'"
								+"  AND REF_NO   = '"+refNo+"'"
								+"  AND AMT_BAL  <> 0";	
								System.out.println("Sql :-"+sql);
								System.out.println("Sql ->"+sql);
								System.out.println("Amount ->"+amount);
								System.out.println("frDate ->"+frDate);
								pstmt = conn.prepareStatement(sql);
								pstmt.setDouble(1,amount);
								pstmt.setDouble(2,amount);
								pstmt.setString(3,userId);
								pstmt.setTimestamp(4,new java.sql.Timestamp(System.currentTimeMillis()));
								pstmt.setString(5,chgTerm);
								pstmt.setTimestamp(6,frDate);
								System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
								pstmt.close();
	
							}							
							else
							{
								sql = "UPDATE ALLOWDEDN_BAL   SET AMT_BAL  = AMT_BAL - ? ,"
								+"	 AMT_ADJ  = AMT_ADJ + ?, "
								+" CHG_USER = ?,"
								+"CHG_DATE = ?,"
								+" CHG_TERM = ? "
								+" WHERE EMP_CODE = '"+empCode+"'"
								+" AND AD_CODE  = '"+adCode+"'"	
								+" AND ? >= START_DATE "
								+" AND ? >END_DATE "
								+"	AND AMT_BAL  <> 0";
								System.out.println("Sql :-"+sql);
								pstmt = conn.prepareStatement(sql);
								pstmt.setDouble(1,amount);
								pstmt.setDouble(2,amount);
								pstmt.setString(3,userId);
								pstmt.setTimestamp(4,new java.sql.Timestamp(System.currentTimeMillis()));
								pstmt.setString(5,chgTerm);
								pstmt.setTimestamp(6,frDate);
								pstmt.setTimestamp(7,frDate);
								System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
								pstmt.close();
							}
					}
				}//end of count allodede_bal
			}//end of amount <>0
					
			if ("L".equalsIgnoreCase(refType) && ("C".equalsIgnoreCase(balOpt)))
			{
				if (amount != 0)
				{
							if ("PP".equalsIgnoreCase(mtype))
							{
									sql = "INSERT INTO LOAN_REG(LOAN_NO , TRAN_NO , TRAN_DATE , TRAN_TYPE , AMOUNT ,INT_CALCULATED , CHG_DATE , CHG_USER , CHG_TERM) "
									+" VALUES(? ,? , ? , 'P' , ? ,'N' , ? , ? ,?)";
									System.out.println("Sql :-"+sql);
									pstmt = conn.prepareStatement(sql);
									pstmt.setString(1,refNo);
									pstmt.setString(2,prdCode);
									pstmt.setTimestamp(3,toDate);
									pstmt.setDouble(4,amount);
									pstmt.setTimestamp(5,new Timestamp(System.currentTimeMillis()));
									pstmt.setString(6,userId);
									pstmt.setString(7,chgTerm);
									System.out.println("Record Update in allowdedn_bal["+pstmt.executeUpdate()+"]");
									pstmt.close();
							}
							else
							{
								sql ="INSERT INTO LOAN_REG(LOAN_NO , TRAN_NO , TRAN_DATE , TRAN_TYPE , AMOUNT , INT_CALCULATED , CHG_DATE , CHG_USER , CHG_TERM) "
								+"  VALUES(? ,?,? , 'A' , ? , 'N' , ?,? ,?)";
								System.out.println("Sql :-"+sql);
									pstmt = conn.prepareStatement(sql);
										pstmt = conn.prepareStatement(sql);
									pstmt.setString(1,refNo);
									pstmt.setString(2,prdCode+"-"+prdCode);//:ls_prdcomp||'-'||:mprdcode 
									pstmt.setTimestamp(3,toDate);
									pstmt.setDouble(4,amount);
									pstmt.setTimestamp(5,new Timestamp(System.currentTimeMillis()));
									pstmt.setString(6,userId);
									pstmt.setString(7,chgTerm);
									System.out.println("Record Update in LOAN_REG["+pstmt.executeUpdate()+"]");
									pstmt.close();
							}
				}
					
				sql = "SELECT BALANCE_AMT FROM LOANS WHERE LOAN_NO = '"+refNo+"'";
				System.out.println("Sql :-"+sql);
				pstmt = conn.prepareStatement(sql);
			//	pstmt.setTimestamp(1,frDate);
				
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					amtBal = rs.getDouble(1);
				}
				rs.close();
				pstmt.close();
				amtBal = amtBal-amount;
				if(amtBal < 0)
				{
				 	retString = "VPNOBAL1";
				 	return retString;
				
				}
				if (amtBal > 0)
				{
					sql = "UPDATE LOANS SET BALANCE_AMT = BALANCE_AMT - ?, "
					+" DATE__BALANCE = ? WHERE LOAN_NO = '"+refNo+"'" ;
					System.out.println("Sql :-"+sql);
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1,amount);
					pstmt.setTimestamp(2,new java.sql.Timestamp(System.currentTimeMillis()));
					System.out.println("Record Update in LOANS["+pstmt.executeUpdate()+"]");
					pstmt.close();
			
				}
				else	
				{	
					sql = "UPDATE LOANS SET BALANCE_AMT = BALANCE_AMT - ?, "
						+" DATE__BALANCE = ?,  STATUS = 'E' WHERE LOAN_NO = '"+refNo+"'" ;
						System.out.println("Sql :-"+sql);
						pstmt = conn.prepareStatement(sql);
						pstmt.setDouble(1,amount);
						pstmt.setTimestamp(2,new java.sql.Timestamp(System.currentTimeMillis()));
						System.out.println("Record Update in LOANS["+pstmt.executeUpdate()+"]");
						pstmt.close();
				}									
			
				if("PP".equalsIgnoreCase(mtype))
				{
						sql= "SELECT LINE_NO   FROM LOANS_SCHEDULE_DET "
						+" WHERE LOAN_NO  = '"+refNo+"'"
						+"AND PRD_CODE = '"+prdCode+"'"
						+"AND PROCESS_TYPE = 'PY'";
						System.out.println("Sql :-"+sql);
						pstmt = conn.prepareStatement(sql);
					
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							lineNo = rs.getInt(1);
						}
						rs.close();
						pstmt.close();
						if(dropdedInst.equalsIgnoreCase("Y") || amount > 0)
						{
							sql = "UPDATE LOANS_SCHEDULE 	SET NO_INST_DED = NO_INST_DED + 1"
							+" WHERE LOAN_NO = '"+refNo+"'"
							+"AND LINE_NO = "+lineNo;
							System.out.println("Sql :-"+sql);
							pstmt = conn.prepareStatement(sql);
							System.out.println("Record Update in loans_schedule["+pstmt.executeUpdate()+"]");
							pstmt.close();
						}
							sql =" UPDATE LOANS_SCHEDULE_DET SET STATUS = 'U'"
							+" WHERE LOAN_NO  =  '"+refNo+"'"
							+" AND PRD_CODE = '"+prdCode+"'"
							+"AND PROCESS_TYPE = 'PY'";
							System.out.println("Sql :-"+sql);
							pstmt = conn.prepareStatement(sql);
							System.out.println("Record Update in loans_schedule["+pstmt.executeUpdate()+"]");
							pstmt.close();
			
				}

				sql ="SELECT BALANCE_AMT  FROM LOANS WHERE LOAN_NO =  '"+refNo+"'";
				System.out.println("Sql :-"+sql);
			 	pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					balAmt = rs.getDouble(1);
				}
				rs.close();
				pstmt.close();
				sql = " UPDATE PAYARR_DET 	SET AMT_BAL  = ? WHERE PRD_CODE = '"+prdCode+"'"
				+"	AND EMP_CODE = '"+empCode+"' AND  AD_CODE  = '"+adCode+"'";
				pstmt = conn.prepareStatement(sql);
				pstmt.setDouble(1,balAmt);
				System.out.println("Record Update in payarr_det["+pstmt.executeUpdate()+"]");
				pstmt.close();		
			}
		}
		catch(Exception e)
		{
			java.io.ByteArrayOutputStream byteBuff = new java.io.ByteArrayOutputStream();
			java.io.PrintStream printBuff= new PrintStream(byteBuff);
			e.printStackTrace(printBuff);
			retString = byteBuff.toString();//"Exception";
			e.printStackTrace();
		}
		return (retString);
	}//end of method
	//end by msalam on 01/10/07
}//End Of bean



















