/**
 * @author : Akhilesh Sikarwar 
 * @Version : 1.0
 * Date : 05/02/13
 */
package ibase.webitm.ejb.fin.adv;

import ibase.system.config.ConnDriver;
import ibase.utility.CommonConstants;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.ejb.fin.FinCommon;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
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.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.ejb.Stateless;
@Stateless
public class ReceiptAdvConf extends ActionHandlerEJB implements ReceiptAdvConfLocal,ReceiptAdvConfRemote 
{
	public String confirm(String tranId,String xtraParams,String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("confirm called........");
		String sql = "";
		String confirm = "";
		String siteCode = "";
		String errString = "" ;
		String loginEmpCode = "";
		Connection conn = null;
		ConnDriver connDriver = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		FinCommon finCommon = null;
		GenericUtility genericUtility = null;
		ITMDBAccessEJB itmDBAccessEJB = null;
		ValidatorEJB validatorEJB = null;
		System.out.println("tran id = "+tranId);
		try
		{
			finCommon = new FinCommon();
			genericUtility = new GenericUtility();
			itmDBAccessEJB = new ITMDBAccessEJB();
			validatorEJB = new ValidatorEJB();
			connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			conn.setAutoCommit(false);
			loginEmpCode = validatorEJB.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");

			sql = "select confirmed ,site_code  from receipt where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				confirm = rs.getString("confirmed") == null ?"N":rs.getString("confirmed");
				siteCode = rs.getString("site_code") == null ?" ":rs.getString("site_code");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			if(confirm != null  && confirm.equalsIgnoreCase("N"))
			{
				errString = gbfRetrieveReceipt(tranId,siteCode,xtraParams,conn);
				System.out.println("err String from gdfRetrieveMiscRcp ="+errString);
				if(errString == null || errString.trim().length() == 0)
				{
					sql = "update receipt set confirmed = 'Y', conf_date = ?,emp_code__aprv = ? where tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDate(1, new java.sql.Date(new java.util.Date().getTime()));
					pstmt.setString(2, loginEmpCode);
					pstmt.setString(3, tranId);
					int updateCoount = pstmt.executeUpdate();
					System.out.println("no of row update = "+updateCoount);
					pstmt.close();
					pstmt = null;
					if(updateCoount > 0)
					{
						errString = itmDBAccessEJB.getErrorString("","VTCICONF3 ","","",conn);
					}
				}
				else 
				{
					//errString = itmDBAccessEJB.getErrorString("","VTDESNCONF ","","",conn);
					return errString;
				}
			}
			else
			{
				System.out.println("The Selected transaction is already confirmed");
				errString = itmDBAccessEJB.getErrorString("","VTMCONF1","","",conn);
				return errString;
			}
			System.out.println("115 err String from confirm method.....");
		} 
		catch( Exception e)
		{
			if(conn!=null)
			{
				try {
					conn.rollback();
				} catch (SQLException ex) {

					System.out.println("Exception : "+e);
					e.printStackTrace();
					throw new ITMException(e);

				}
			}
			System.out.println("Exception : "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			if(conn!=null)
			{
				try
				{
					if(errString != null && errString.trim().length() > 0)
					{
						if(errString.indexOf("VTCICONF3") > -1)
						{
							conn.commit();
						}
						else
						{
							conn.rollback();
						}
					}
					if(rs != null)
					{
						rs.close();
						rs = null;
					}
					if(pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					conn.close();
				}
				catch(Exception e)
				{
					System.out.println("Exception : "+e);e.printStackTrace();
					throw new ITMException(e);
				}
			}
		}
		return errString;
	} //end of confirm method

	private String gbfRetrieveReceipt(String tranId,String siteCode,String xtraParams,Connection conn)throws RemoteException,ITMException
	{
		System.out.println("gdf Retrieve Receipt Called...........");
		String errString = "";
		String sundryCode = "";
		String sql = "";
		String cctrFin = "";
		String cctrFins = "";
		String rcpType = "";
		String bdFluctuationCf = "";
		String ledgPostConf = "";
		String acctFin = "";
		String acctFins = "";
		String refser = "";
		String refNo ="";
		String acctCodeAr  = "";
		String cctrCodeAr  = "";	
		String billDisc ="";
		String custCode  = "";	
		String rcpClubPost = "";
		String partyDocRef = "";
		String lineNoRef = ""; 
		String acct = "";
		String cctr = "";
		String stype = "";
		String scode = "";
		String acctCode = "";
		String cctrCode = "";
		String currCode = "";
		String sundryType = "";
		String hcurrCode = "";
		String finEntity = "";
		String hcustCode = "";
		String refSer = "";
		String acctCodeDr = "";
		String cctrCodeDr = "";
		String acctCodeCr = "";
		String cctrCodeCr = "";
		String rcpepcAdj = "";

		int count = 0;
		double netAmount = 0.0;
		double exchRateRcv  = 0.0;	
		double rcpAmt  = 0.0;
		double finChg  = 0.0;
		double totalAmt = 0.0;
		double advAmt = 0.0 ;
		double finTotal = 0.0;
		double exchRate = 0.0;
		double exch = 0.0;
		double totbdDiff = 0.0;
		double total = 0.0;
		double tFin = 0.0;
		double hExchRate = 0.0 ;
		double amount = 0.0;
		double totEpc = 0.0;
		double totEpcBc = 0.0 ;
		double hexchRate = 0.0;
		double diffAmtExch = 0.0;
		double badDebtAmt =  0.0;
		double amtProv = 0.0;
		double amtRevrsed = 0.0;
		double amtRev = 0.0;
		double badAmt = 0.0;
		double totBc = 0.0;
		double chq = 0.0;
		int i = 0;
		int cnt = 0 ;
		Date tranDate = null;
		HashMap RcpHdrMap = null;
		HashMap DetMap = null;
		HashMap RcpDetMap = null;
		HashMap ADetMap = null;
		HashMap EpcAdjMap = null;
		ArrayList EpcAdjList = new ArrayList();
		boolean isEpcCurrSame = false;
		ArrayList RcpDetList = new ArrayList();
		ArrayList DetList = new ArrayList();
		ArrayList ADetList = new ArrayList();
		ArrayList miscRcpDetList = new ArrayList();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ITMDBAccessEJB itmDBAccessEJB = null;
		FinCommon finCommon = null;
		DistCommon disCommon = null;
		try
		{
			finCommon = new FinCommon();
			disCommon = new DistCommon();


			cctrFin = finCommon.getFinparams("999999", "ACCT_FINCHG", conn);
			if(cctrFin != null && ! "NULLFOUND".equalsIgnoreCase(cctrFin))
			{
				acctFin = disCommon.getToken(cctrFin, ";");

			}else
			{
				errString = itmDBAccessEJB.getErrorString("","VTFINPARM ","","",conn);
			}

			cctrFins = finCommon.getFinparams("999999", "ACCT_FINCHG_AR", conn);
			if(cctrFins != null && ! "NULLFOUND".equalsIgnoreCase(cctrFins))
			{
				acctFins = disCommon.getToken(cctrFins, ";");
			}else
			{
				errString = itmDBAccessEJB.getErrorString("","VTFINPARM ","","",conn);
			}


			bdFluctuationCf = finCommon.getFinparams("999999", "BD_FLUCTUATION_CF", conn);
			if( "NULLFOUND".equalsIgnoreCase(bdFluctuationCf))
			{
				errString = itmDBAccessEJB.getErrorString("","VTFINPARM ","","",conn);
			}


			rcpClubPost = finCommon.getFinparams("999999", "RCP_CLUB_POST", conn);
			if( "NULLFOUND".equalsIgnoreCase(rcpClubPost))
			{
				errString = itmDBAccessEJB.getErrorString("","VTFINPARM ","","",conn);
			}


			itmDBAccessEJB = new ITMDBAccessEJB();

			if ("db2".equalsIgnoreCase(CommonConstants.DB_NAME ))
			{
				sql = "SELECT tran_id, adv_amt, rcp_type FROM receipt  WHERE tran_id = ? for update ";
			}

			else if ( "mssql".equalsIgnoreCase(CommonConstants.DB_NAME ))
			{
				sql = "SELECT tran_id, adv_amt, rcp_type FROM receipt (updlock) WHERE tran_id = ? " ;
			}
			else
			{
				sql =" SELECT tran_id, adv_amt, rcp_type FROM receipt WHERE tran_id =  ? for update nowait" ;
			}
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				tranId = rs.getString("tran_id") == null ? " ":rs.getString("tran_id");
				advAmt = rs.getDouble("adv_amt");
				rcpType = rs.getString("rcp_type") == null ? " ":rs.getString("rcp_type");
			}
			pstmt.close();
			pstmt = null;
			rs.close();
			rs = null;

			if(advAmt < 0)
			{
				errString = itmDBAccessEJB.getErrorString("","VTRCPADV2","","",conn);
				return errString ;
			}

			java.util.Date today=	new java.sql.Date(new java.util.Date().getTime());

			sql = "select tran_id, tran_date, cust_code, tran_type, rcp_mode, bank_code, ref_no, ref_date, remarks, net_amt, curr_code," +
					" exch_rate, net_amt__bc, fin_entity, acct_code__ar, cctr_code__ar, acct_code__bal, cctr_code__bal, site_code," +
					" confirmed, adv_amt ,acct_code__cf, cctr_code__cf,eff_date, chq_amt,remarks, proj_code, entry_batch_no,bill_amt ," +
					" diff_amt__exch, net_amt__bc,  anal_code, bill_disc_amt, oth_amt, efundadj_amt, acct_code__adv, cctr_code__adv " +
					" from  receipt  where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				RcpHdrMap = new HashMap();
				RcpHdrMap.put("tran_date",rs.getDate("tran_date"));
				RcpHdrMap.put("tran_id",rs.getString("tran_id"));
				RcpHdrMap.put("cust_code",rs.getString("cust_code"));
				RcpHdrMap.put("tran_type",rs.getString("tran_type"));
				RcpHdrMap.put("rcp_mode",rs.getString("rcp_mode"));
				RcpHdrMap.put("bank_code",rs.getString("bank_code"));
				RcpHdrMap.put("ref_no",rs.getString("ref_no"));
				RcpHdrMap.put("ref_date",rs.getDate("ref_date"));
				RcpHdrMap.put("remarks",rs.getString("remarks"));
				RcpHdrMap.put("net_amt",rs.getDouble("net_amt"));
				RcpHdrMap.put("exch_rate",rs.getDouble("exch_rate"));
				RcpHdrMap.put("curr_code",rs.getString("curr_code"));
				RcpHdrMap.put("net_amt__bc",rs.getDouble("net_amt__bc"));
				RcpHdrMap.put("fin_entity",rs.getString("fin_entity"));
				RcpHdrMap.put("acct_code__ar",rs.getString("acct_code__ar"));
				RcpHdrMap.put("cctr_code__ar",rs.getString("cctr_code__ar")==null?"":rs.getString("cctr_code__ar"));
				RcpHdrMap.put("acct_code__bal",rs.getString("acct_code__bal"));
				RcpHdrMap.put("cctr_code__bal",rs.getString("cctr_code__bal")==null?"":rs.getString("cctr_code__bal"));
				RcpHdrMap.put("site_code",rs.getString("site_code"));
				RcpHdrMap.put("confirmed",rs.getString("confirmed"));
				RcpHdrMap.put("adv_amt",rs.getDouble("adv_amt"));
				RcpHdrMap.put("acct_code__cf",rs.getString("acct_code__cf"));
				RcpHdrMap.put("cctr_code__cf",rs.getString("cctr_code__cf")==null?"":rs.getString("cctr_code__cf"));
				RcpHdrMap.put("eff_date",rs.getDate("eff_date"));
				RcpHdrMap.put("chq_amt",rs.getDouble("chq_amt"));
				RcpHdrMap.put("remarks",rs.getString("remarks"));
				RcpHdrMap.put("proj_code",rs.getString("proj_code"));
				RcpHdrMap.put("entry_batch_no",rs.getString("entry_batch_no"));
				RcpHdrMap.put("bill_amt",rs.getDouble("bill_amt"));
				RcpHdrMap.put("diff_amt__exch",rs.getDouble("diff_amt__exch"));
				RcpHdrMap.put("net_amt__bc",rs.getDouble("net_amt__bc"));
				RcpHdrMap.put("anal_code",rs.getString("anal_code"));
				RcpHdrMap.put("bill_disc_amt",rs.getDouble("bill_disc_amt"));
				RcpHdrMap.put("oth_amt",rs.getDouble("oth_amt"));
				RcpHdrMap.put("efundadj_amt",rs.getDouble("efundadj_amt"));
				RcpHdrMap.put("acct_code__adv",rs.getString("acct_code__adv"));
				RcpHdrMap.put("cctr_code__adv",rs.getString("cctr_code__adv")==null?"":rs.getString("cctr_code__adv"));

				System.out.println(" Header Map..........");
				System.out.println(RcpHdrMap.toString());
			}
			pstmt.close();
			pstmt = null;
			rs.close();
			rs = null;

			sql =" select ledg_post_conf from transetup where upper(tran_window) = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,"W_RECEIPT_ADV");  // issue
			rs = pstmt.executeQuery();
			if(rs.next())
			{     
				ledgPostConf = rs.getString("ledg_post_conf");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			if(ledgPostConf != null  && "Y".equalsIgnoreCase(ledgPostConf))
			{
				if(((Date)RcpHdrMap.get("tran_date")).compareTo(today)!= 0){tranDate = today;}
				sql = "update receipt set tran_date  =? where tran_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setDate(1, new java.sql.Date(new java.util.Date().getTime()));
				pstmt.setString(2, tranId);
				int updateCoount = pstmt.executeUpdate();

				pstmt.close();
				pstmt = null;

			}

			if(((Date)RcpHdrMap.get("eff_date")).compareTo(today) > 0)
			{

				errString = itmDBAccessEJB.getErrorString("","VTRCPDT1","","",conn);
			}


			// populate rcpacct

			DetMap = new HashMap();
			sql = "select sundry_type,sundry_code,acct_code,cctr_code,emp_code, anal_code,curr_code ,exch_rate,amount,party_doc_ref from rcpacct where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				DetMap.put("sundry_type", rs.getString("sundry_type"));
				DetMap.put("sundry_code", rs.getString("sundry_code"));
				DetMap.put("acct_code", rs.getString("acct_code"));
				DetMap.put("cctr_code", rs.getString("cctr_code"));
				DetMap.put("emp_code", rs.getString("emp_code"));
				DetMap.put("anal_code", rs.getString("anal_code"));
				DetMap.put("curr_code", rs.getString("curr_code"));
				DetMap.put("exch_rate", rs.getDouble("exch_rate"));
				DetMap.put("amount", rs.getDouble("amount"));
				DetMap.put("party_doc_ref", rs.getString("party_doc_ref"));
				DetList.add(DetMap);
				System.out.println(" receipt Acct Map..........");
				System.out.println(DetList.toString());
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			sql = "select ref_ser, ref_no,adj_amt,acct_code,cctr_code,exch_rate__vouch from rcpadv where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{	
				ADetMap = new HashMap();
				ADetMap.put("ref_ser", rs.getString("ref_ser"));
				ADetMap.put("ref_no", rs.getString("ref_no"));
				ADetMap.put("adj_amt", rs.getDouble("adj_amt"));

				ADetMap.put("acct_code", rs.getString("acct_code"));
				ADetMap.put("cctr_code", rs.getString("cctr_code"));

				ADetMap.put("exch_rate__vouch", rs.getDouble("exch_rate__vouch"));
				ADetList.add(ADetMap);
				System.out.println(" receipt Adv Map..........");
				System.out.println(ADetMap.toString());
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			sql = "select ref_ser, ref_no,curr_code,exch_rate,amount,acct_code,cctr_code,sundry_type,sundry_code from rcpepc_adj where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{	
				EpcAdjMap = new HashMap();
				EpcAdjMap.put("ref_ser", rs.getString("ref_ser"));
				EpcAdjMap.put("ref_no", rs.getString("ref_no"));

				EpcAdjMap.put("curr_code", rs.getString("curr_code"));
				EpcAdjMap.put("exch_rate", rs.getString("exch_rate"));
				EpcAdjMap.put("amount", rs.getString("amount"));

				EpcAdjMap.put("acct_code", rs.getString("acct_code"));
				EpcAdjMap.put("cctr_code", rs.getString("cctr_code"));
				EpcAdjMap.put("sundry_type", rs.getString("sundry_type"));
				EpcAdjMap.put("sundry_code", rs.getString("sundry_code"));
				EpcAdjList.add(EpcAdjMap);
				System.out.println(" Rcpepc Adj Map..........");
				System.out.println(EpcAdjList.toString());

			}

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

			//RcpDetMap = new HashMap();
			sql = "select line_no__ref,ref_ser,ref_no,acct_code__ar,cctr_code__ar,exch_rate__rcv,rcp_amt,fin_chg,cust_code,bad_debt_amt,bill_disc_amt from rcpdet where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				RcpDetMap = new HashMap();
				RcpDetMap.put("line_no__ref", rs.getString("line_no__ref"));
				RcpDetMap.put("ref_ser", rs.getString("ref_ser"));
				RcpDetMap.put("ref_no", rs.getString("ref_no"));
				RcpDetMap.put("acct_code__ar", rs.getString("acct_code__ar"));
				RcpDetMap.put("cctr_code__ar", rs.getString("cctr_code__ar")==null?"":rs.getString("cctr_code__ar"));
				RcpDetMap.put("exch_rate__rcv", rs.getDouble("exch_rate__rcv"));
				RcpDetMap.put("rcp_amt", rs.getDouble("rcp_amt"));
				RcpDetMap.put("fin_chg", rs.getDouble("fin_chg"));
				RcpDetMap.put("cust_code", rs.getString("cust_code"));
				RcpDetMap.put("bad_debt_amt", rs.getDouble("bad_debt_amt"));
				RcpDetMap.put("bill_disc_amt", rs.getDouble("bill_disc_amt")); 				//ADDED BY RITESH ON 24/05/13
				RcpDetList.add(RcpDetMap);

				System.out.println(" DetMap  Map..........");
				System.out.println(RcpDetMap.toString());

			}

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

			for(i=0 ; i<RcpDetList.size() ; i++)
			{
				RcpDetMap = new HashMap();
				RcpDetMap = (HashMap) RcpDetList.get(i);


				if(!((String)RcpDetMap.get("ref_ser")).equals("M-INV"))
				{
					total = total + (Double)RcpDetMap.get("rcp_amt");
					finTotal = finTotal + (Double)RcpDetMap.get("fin_chg");

					System.out.println("LINE NO 541"+total);


					if(rcpClubPost.equals("Y"))
					{

						sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,(String)RcpDetMap.get("acct_code__ar"));
						pstmt.setString(2,(String)RcpDetMap.get("cctr_code__ar")==null?"":(String)RcpDetMap.get("cctr_code__ar"));
						pstmt.setDouble(3,(Double)RcpDetMap.get("exch_rate__rcv"));
						pstmt.setString(4,tranId);
						rs = pstmt.executeQuery();
						while(rs.next())
						{
							cnt = rs.getInt(1);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						if(cnt==0)
						{

/*
							sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,tranId);//issue
							pstmt.setString(2,String.valueOf(cnt+1));
							pstmt.setString(3,(String)RcpDetMap.get("acct_code__ar"));
							pstmt.setString(4,(String)RcpDetMap.get("cctr_code__ar")==null?"":(String)RcpDetMap.get("cctr_code__ar"));
							pstmt.setDouble(5,(Double)RcpDetMap.get("exch_rate__rcv"));
							pstmt.setDouble(6,(Double)RcpDetMap.get("rcp_amt"));
							pstmt.setString(7,"");
							pstmt.setString(8,"");
							pstmt.setString(9,"Y");
							pstmt.setString(10,"C");
							pstmt.setString(11,(String)RcpDetMap.get("cust_code"));
							cnt = pstmt.executeUpdate();

							pstmt.close();
							pstmt = null;*///open
						}

						else
						{

							sql = "update rcpacct set amount  =? where tran_id = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1, ((Double)RcpDetMap.get("rcp_amt")+(Double)(DetMap.get("amount")==null?0.0:DetMap.get("amount"))));
							pstmt.setString(2, tranId);
							int updateCoount = pstmt.executeUpdate();

							pstmt.close();
							pstmt = null;
						}
						partyDocRef = finCommon.gfGetPartyDocRef((String)RcpDetMap.get("ref_ser"), (String)RcpDetMap.get("ref_no"),conn);
						DetMap.put("party_doc_ref",partyDocRef);
					}

				}
			}
			// populate bill if bill is discounted
			// then credit the amount to the BD

			for(i=0 ; i<RcpDetList.size() ; i++)
			{
				RcpDetMap = new HashMap();
				RcpDetMap = (HashMap)RcpDetList.get(i);


				refSer 			= (String)RcpDetMap.get( "ref_ser");
				refNo   		= (String)RcpDetMap.get("ref_no");
				hExchRate 		= (Double)RcpHdrMap.get("exch_rate");
				lineNoRef       = (String)RcpDetMap.get("line_no__ref"); 


				if(refSer.trim().equalsIgnoreCase("S-INV") || refSer.substring(0, 4).equalsIgnoreCase("R-IBC")) 
				{
					sql = "select receivables.bill_disc from receivables where tran_ser = ? and ref_no = ? and line_no__ref = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,refSer);
					pstmt.setString(2,refNo);
					pstmt.setString(3,lineNoRef);
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						billDisc = rs.getString(1)==null?"N":rs.getString(1);
					}

					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;
				}
				if(billDisc.equalsIgnoreCase("Y"))
				{
					sql = "SELECT export_fund.acct_code__ar, export_fund.cctr_code__ar, export_fund.exch_rate, sundry_type,sundry_code  FROM 	export_fund,    export_fundet   WHERE ( export_fund.tran_id = export_fundet.tran_id ) and  	( export_fund.confirmed 	= 'Y') and( ( export_fundet.ref_ser 	= ? ) and   ( export_fundet.ref_no 		= ? ) )"   ;
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,refSer);				//modify by ritesh on 24/05/13
					pstmt.setString(2,refNo);				//modify by ritesh on 24/05/13
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						acct = rs.getString(1);
						cctr = rs.getString(2)==null?"":rs.getString(2);
						exch = rs.getDouble(3);
						stype = rs.getString(4);
						scode = rs.getString(5);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

			//		rcpAmt = (Double)RcpDetMap.get( "rcp_amt");
					rcpAmt = (Double)RcpDetMap.get( "bill_disc_amt");               //added by ritesh on 15/05/13 as per discussion with pravin
					totbdDiff 	= totbdDiff + (rcpAmt * hExchRate ) - (rcpAmt * exch );						
			//		rcpAmt  		= 0 - (Double)RcpDetMap.get( "rcp_amt");
					rcpAmt  		= 0 - (Double)RcpDetMap.get( "bill_disc_amt");  // //added by ritesh on 15/05/13 as per discussion with pravin
					finChg 	        = 0 - (Double)RcpDetMap.get( "fin_chg");

					total = total + rcpAmt;

					System.out.println("total in 666"+total);


					tFin = tFin + finChg;

					if(rcpClubPost.equals("Y"))
					{

						sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,acct);
						pstmt.setString(2,cctr==null?"":cctr);
						pstmt.setDouble(3,exch);
						pstmt.setString(4,tranId);
						rs = pstmt.executeQuery();
						while(rs.next())
						{
							cnt = rs.getInt(1);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						if(cnt==0)
						{

							sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,tranId);
							pstmt.setString(2,String.valueOf(cnt+1));
							pstmt.setString(3,acct);
							pstmt.setString(4,cctr==null?"":cctr);
							pstmt.setDouble(5,exch);
							pstmt.setDouble(6,rcpAmt);
							pstmt.setString(7,"");
							pstmt.setString(8,"");
							pstmt.setString(9,"N");
							pstmt.setString(10,stype);
							pstmt.setString(11,scode);
							cnt = pstmt.executeUpdate();


							pstmt.close();
							pstmt = null;
						}
						else
						{

							sql = "update rcpacct set amount  =? where tran_id = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1, (rcpAmt+(Double)(DetMap.get("amount")==null?0:DetMap.get("amount"))));
							pstmt.setString(2, tranId);
							int updateCoount = pstmt.executeUpdate();


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

				}
			}

			//adjust for advances adjusted

			for(i=0 ; i<ADetList.size() ; i++)
			{
				ADetMap = new HashMap();
				ADetMap = (HashMap) ADetList.get(i);


				acct = (String)ADetMap.get("acct_code");
				cctr = (String)ADetMap.get("cctr_code");
				exch = (Double)ADetMap.get("exch_rate__vouch");
				amount = (Double)ADetMap.get("adj_amt");

				total  = total + amount;
				System.out.println("LINE NO 743"+total);
				if(rcpClubPost.equals("Y"))
				{
					sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,acct);
					pstmt.setString(2,cctr==null?"":cctr);
					pstmt.setDouble(3,exch);
					pstmt.setString(4,tranId);//issue
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						cnt = rs.getInt(1);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;
				}


				if(cnt==0)
				{
					sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,tranId);//issue
					pstmt.setString(2,String.valueOf(cnt+2));
					pstmt.setString(3,acct);
					pstmt.setString(4,cctr==null?"":cctr);
					pstmt.setDouble(5,exch);
					pstmt.setDouble(6,amount);
					pstmt.setString(7,"");
					pstmt.setString(8,"");
					pstmt.setString(9,"Y");
					pstmt.setString(10,"C");
					pstmt.setString(11,(String)RcpHdrMap.get("cust_code"));
					cnt = pstmt.executeUpdate();


					pstmt.close();
					pstmt = null;
				}
				else
				{

					sql = "update rcpacct set amount  =? where tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1, (amount+(Double)(DetMap.get("amount")==null?0:DetMap.get("amount"))));
					pstmt.setString(2, tranId);
					int updateCoount = pstmt.executeUpdate();


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





			// insert data for finchg			
			//take but det and insert 

			if(tFin != 0)
			{
				total = total + tFin;
				System.out.println("LINE NO 812"+total);


				acct = acctFin==null?"":acctFin;
				cctr = cctrFin;
				exch = (Double)RcpHdrMap.get("exch_rate");

				sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,tranId);//issue
				pstmt.setString(2,String.valueOf(cnt+1));////issue
				pstmt.setString(3,acct);
				pstmt.setString(4,cctr==null?"":cctr);
				pstmt.setDouble(5,exch);
				pstmt.setDouble(6,tFin);
				pstmt.setString(7,"");
				pstmt.setString(8,"");
				pstmt.setString(9,"N");
				pstmt.setString(10,"C");
				pstmt.setString(11,(String)RcpHdrMap.get("cust_code"));
				cnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;

				sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,tranId);//issue
				pstmt.setString(2,String.valueOf(cnt+1));//issue
				pstmt.setString(3,acctFins);
				pstmt.setString(4,cctrFins==null?"":cctrFins);
				pstmt.setDouble(5,exch);
				pstmt.setDouble(6,tFin);
				pstmt.setString(7,"");
				pstmt.setString(8,"");
				pstmt.setString(9,"Y");
				pstmt.setString(10,"C");
				pstmt.setString(11,(String)RcpHdrMap.get("cust_code"));
				cnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;

				sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,tranId);//issue
				pstmt.setString(2,String.valueOf(cnt+1));//issue
				pstmt.setString(3,acct);
				pstmt.setString(4,cctr==null?"":cctr);
				pstmt.setDouble(5,exch);
				pstmt.setDouble(6,0-tFin);
				pstmt.setString(7,"");
				pstmt.setString(8,"");
				pstmt.setString(9,"Y");
				pstmt.setString(10,"C");
				pstmt.setString(11,(String)RcpHdrMap.get("cust_code"));
				cnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;

			}

			// insert data for advance
			advAmt = 	(Double)RcpHdrMap.get("adv_amt");
			if(advAmt!= 0)
			{
				total = total + advAmt;
				System.out.println("LINE NO 874"+total);
				//System.out.println("total in adv"+total);
				acct = (String)RcpHdrMap.get("acct_code__adv");
				cctr = (String)RcpHdrMap.get("cctr_code__adv");
				exch = (Double)RcpHdrMap.get("exch_rate");


				if(rcpClubPost.equals("Y"))
				{

					sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,acct);
					pstmt.setString(2,cctr==null?"":cctr);
					pstmt.setDouble(3,exch);
					pstmt.setString(4,tranId);//issue
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						cnt = rs.getInt(1);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					if(cnt==0)
					{
						sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,tranId);//issue
						pstmt.setString(2,String.valueOf(cnt+1));//issue
						pstmt.setString(3,acct);
						pstmt.setString(4,cctr==null?"":cctr);
						pstmt.setDouble(5,exch);
						pstmt.setDouble(6,advAmt);
						pstmt.setString(7,"");
						pstmt.setString(8,"");
						pstmt.setString(9,"Y");
						pstmt.setString(10,"C");
						pstmt.setString(11,((String)RcpHdrMap.get("cust_code")).trim());
						cnt = pstmt.executeUpdate();
						pstmt.close();
						pstmt = null;
					}
					else
					{
						sql = "update rcpacct set amount  =? where tran_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setDouble(1, ((Double)(RcpHdrMap.get("adv_amt")==null?0.0:RcpHdrMap.get("adv_amt"))+(Double)(DetMap.get("amount")==null?0.0:DetMap.get("amount"))));
						pstmt.setString(2, tranId);
						int updateCoount = pstmt.executeUpdate();
						pstmt.close();
						pstmt = null;
					}
				}

			}

			// insert data for EPC ADJUSTED 
			///// insert rows for epc adjustment
			for(i=0 ; i<EpcAdjList.size() ; i++)
			{
				EpcAdjMap = new HashMap();
				EpcAdjMap = (HashMap) DetList.get(i);

				currCode = (String)EpcAdjMap.get("curr_code");
				acctCode = (String)EpcAdjMap.get("acct_code");
				cctrCode = (String)EpcAdjMap.get("cctr_code")==null?"":(String)EpcAdjMap.get("cctr_code");
				exchRate = (Double)EpcAdjMap.get("exch_rate");
				amount = (Double)EpcAdjMap.get("amount");
				sundryType = (String)EpcAdjMap.get("sundry_type");
				sundryCode = (String)EpcAdjMap.get("sundry_code");

				hcurrCode = (String)RcpHdrMap.get("curr_code");
				//hexchRate = (Double)RcpHdrMap.get("exch_rate");
				totEpc = totEpc + amount;
				if(!currCode.equalsIgnoreCase(hcurrCode))
				{
					isEpcCurrSame = false;
					totEpcBc = totEpcBc + (exchRate * amount);
				}
				else
				{
					totEpcBc = totEpcBc + (hexchRate * amount);
				}


				if(rcpClubPost.equals("Y"))
				{

					sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,acctCode);
					pstmt.setString(2,cctrCode==null?"":cctrCode);
					pstmt.setDouble(3,exchRate);
					pstmt.setString(4,tranId);//issue
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						cnt = rs.getInt(1);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;
					if(cnt==0)
					{

						sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,tranId);//issue
						pstmt.setString(2,String.valueOf(cnt+1));////issue
						pstmt.setString(3,acctCode);
						pstmt.setString(4,cctrCode==null?"":cctrCode);
						pstmt.setDouble(5,exchRate);
						pstmt.setDouble(6,advAmt);
						pstmt.setString(7,"");
						pstmt.setString(8,"");
						pstmt.setString(9,"Y");
						pstmt.setString(10,"C");
						pstmt.setString(11,(String)RcpHdrMap.get("cust_code"));
						cnt = pstmt.executeUpdate();
						pstmt.close();
						pstmt = null;
					}

					else
					{
						sql = "update rcpacct set amount  =? where tran_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setDouble(1, ((Double)EpcAdjMap.get("amount")+(Double)DetMap.get("amount")));
						pstmt.setString(2, tranId);
						int updateCoount = pstmt.executeUpdate();
						pstmt.close();
						pstmt = null;
					}
				}
			}
			// insert rows for  ADVANCE BOOKING adjustment

			finEntity = (String)RcpHdrMap.get("fin_entity");
			sql = "select curr_code from finent  where fin_entity = ? " ;
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, finEntity);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				currCode = rs.getString("curr_code");
			}

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


			/*	sql = "SELECT diff_amt__exch FROM ADVBKING_ADJ where tran_id  = ? " ;
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranId);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				diffAmtExch = rs.getDouble("diff_amt__exch");
			}

			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;*/ //---OPEN



			acct = (String)RcpHdrMap.get("acct_code__ar");
			cctr = (String)RcpHdrMap.get("cctr_code__ar");
			exch = (Double)RcpHdrMap.get("exch_rate");
			stype = "C";

			exchRate = 1;
			amount = diffAmtExch;

			if(rcpClubPost.equals("Y"))
			{

				sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,acct);
				pstmt.setString(2,cctr==null?"":cctr);
				pstmt.setDouble(3,exch);
				pstmt.setString(4,tranId);//issue
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					cnt = rs.getInt(1);
				}

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

				/*if(cnt==0)
				{


					sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY,SUNDRY_TYPE,SUNDRY_CODE) values (?,?,?,?,?,?,?,?,?,?,?)";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,tranId);//issue
					pstmt.setString(2,String.valueOf(cnt+1));////issue
					pstmt.setString(3,acct);
					pstmt.setString(4,cctr==null?"":cctr);
					pstmt.setDouble(5,exch);
					pstmt.setDouble(6,amount);
					pstmt.setString(7,"");
					pstmt.setString(8,"");
					pstmt.setString(9,"N");
					pstmt.setString(10,"C");
					pstmt.setString(11,(String)RcpHdrMap.get("cust_code"));
					cnt = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}

				else
				{

					sql = "update rcpacct set amount  =? where tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1, (diffAmtExch+diffAmtExch));
					pstmt.setString(3, tranId);
					int updateCoount = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}*/ //OPEN
			}

			for(i=0 ; i<RcpDetList.size() ; i++)
			{
				RcpDetMap = new HashMap();
				RcpDetMap = (HashMap) RcpDetList.get(i);

				refSer	= (String)RcpDetMap.get("ref_ser");
				refNo	= (String)RcpDetMap.get("ref_no");
				rcpAmt	= (Double)RcpDetMap.get("rcp_amt");
				badDebtAmt	=(Double) RcpDetMap.get("bad_debt_amt");
				lineNoRef	= (String)RcpDetMap.get("line_no__ref");

				sql = "select case when bad_debt_amt is null then 0 else bad_debt_amt end, exch_rate from receivables where tran_ser = ? and ref_no = ? and line_no__ref = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,refSer);
				pstmt.setString(2,refNo);
				pstmt.setString(3,lineNoRef);
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					badDebtAmt = rs.getDouble(1);
					exchRate = rs.getDouble("exch_rate");

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

				if(badDebtAmt > 0)
				{
					sql = "select baddebt_amt,tran_id, acct_code__dr,cctr_code__dr, acct_code__cr, cctr_code__cr, amt_prov, case when amt_revrsed is null then 0 else amt_revrsed end from bad_debts_prov where tran_ser = ? and ref_no = ? and amt_prov <> 0 order by tran_date";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,refSer);
					pstmt.setString(2,refNo);
					pstmt.setString(3,lineNoRef);
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						badDebtAmt = rs.getDouble("baddebt_amt");
						tranId = rs.getString("tran_id");
						acctCodeDr = rs.getString("acct_code__dr");
						cctrCodeDr = rs.getString("cctr_code__dr")==null?"":rs.getString("cctr_code__dr");
						acctCodeCr = rs.getString("acct_code__cr");
						cctrCodeCr = rs.getString("cctr_code__cr")==null?"":rs.getString("cctr_code__cr");
						amtProv = rs.getDouble("amt_prov");
						amtRevrsed = rs.getDouble("amt_revrsed");
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					amtRev = Math.min(badDebtAmt,amtProv);
					total = total + amtRev;
					System.out.println("LINE NO 1164"+total);

					if(rcpClubPost.equals("Y"))
					{

						sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,acctCodeDr);
						pstmt.setString(2,cctrCodeDr==null?"":cctrCodeDr);
						pstmt.setDouble(3,exch);
						pstmt.setString(4,tranId);//issue
						rs = pstmt.executeQuery();
						while(rs.next())
						{
							cnt = rs.getInt(1);
						}

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


						if(cnt==0)
						{

							sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY) values (?,?,?,?,?,?,?,?,?)";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,tranId);//issue
							pstmt.setString(2,(String)RcpDetMap.get("line_no__ref"));////issue
							pstmt.setString(3,acctCodeDr);
							pstmt.setString(4,cctrCodeDr==null?"":cctrCodeDr);
							pstmt.setDouble(5,exchRate);
							pstmt.setDouble(6,amtRev);
							pstmt.setString(7,"");
							pstmt.setString(8,"");
							pstmt.setString(9,"N");
							cnt = pstmt.executeUpdate();

							pstmt.close();
							pstmt = null;

						}
						else
						{
							sql = "update rcpacct set amount  =? where tran_id = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1, ((badDebtAmt*-1)+(Double)DetMap.get(amount)));
							pstmt.setString(2, tranId);
							int updateCoount = pstmt.executeUpdate();

							pstmt.close();
							pstmt = null;
						}

						if(rcpClubPost.equals("Y"))
						{

							sql = "select count(*) from rcpacct where acct_code = ? and cctr_code = ? and exch_rate = ? and tran_id = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,acctCodeCr);
							pstmt.setString(2,cctrCodeCr==null?"":cctrCodeCr);
							pstmt.setDouble(3,exch);
							pstmt.setString(4,tranId);//issue
							rs = pstmt.executeQuery();
							while(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;

							if(cnt==0)
							{

								// for debit account
								sql = "Insert into rcpacct (TRAN_ID,LINE_NO,ACCT_CODE,CCTR_CODE,EXCH_RATE,AMOUNT,EMP_CODE,ANAL_CODE,EFFECT_PARTY) values (?,?,?,?,?,?,?,?,?)";
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,tranId);//issue
								pstmt.setString(2,(String)RcpDetMap.get("line_no__ref"));////issue
								pstmt.setString(3,acctCodeCr);
								pstmt.setString(4,cctrCodeCr==null?"":cctrCodeCr);
								pstmt.setDouble(5,exchRate);
								pstmt.setDouble(6,amtRev);
								pstmt.setString(7,"");
								pstmt.setString(8,"");
								pstmt.setString(9,"N");
								cnt = pstmt.executeUpdate();
								pstmt.close();
								pstmt = null;
							}
						}
						else
						{

							sql = "update rcpacct set amount  =? where tran_id = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setDouble(1, (badDebtAmt+(Double)DetMap.get(amount)));
							pstmt.setString(2, tranId);
							int updateCoount = pstmt.executeUpdate();
							pstmt.close();
							pstmt = null;
						}


						sql = "update bad_debts_prov set amt_prov = ?, amt_revrsed = ?  where tran_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setDouble(1,amtRev);
						pstmt.setDouble(2,amtRevrsed);
						pstmt.setString(3,tranId);
						pstmt.executeUpdate();
						pstmt.close();
						pstmt = null;

						badAmt = badAmt - amtRev;

						sql = " update receivables set bad_debt_amt = ? , ref_no = ?  , tran_ser = ? and line_no__ref = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setDouble(1,badDebtAmt-total);
						pstmt.setString(2,refNo);
						pstmt.setString(3,refser);
						pstmt.setString(4,lineNoRef);
						pstmt.executeUpdate();
						pstmt.close();
						pstmt = null;

						System.out.println("aaaaaaaaaaaa"+total);
					}
					System.out.println("BBBBBBBBBBB"+total);
				}
				System.out.println("CCCCCCCCCCCC"+total);
			}

			System.out.println("DDDDDDDDDDDDDDDDDDd"+total);
			//total = 0;

			totBc = total * (Double)RcpHdrMap.get("exch_rate");
			System.out.println("DDDDDDDDDDDDDDDDDDd"+total);
			/// the upper block is transfer here.
			total = total + (Double)RcpHdrMap.get("net_amt");	
			totBc = totBc + (Double)RcpHdrMap.get("net_amt__bc");	

			System.out.println("DDDDDDDDDDDDDDDDDDd"+total);
			if(bdFluctuationCf.equalsIgnoreCase("N"))
			{
				totBc = totBc +totbdDiff;
				System.out.println("DDDDDDDDDDDDDDDDDDd"+total);
			}

			// if EPC is adjusted in diff currency then update the net amount bc.
	
			if(isEpcCurrSame == true)
			{
				System.out.println("total totEpc totEpcBc"+total+""+totEpc+""+totEpcBc);
				total = total + totEpc;	
				totBc = totBc +totEpcBc;
			}
			else	
			{
				totBc = total + totEpcBc;	
			}


			//if EPC can be adjusted in base currency if it is so then check in base currency		
			sql = "SELECT count(*)  FROM  rcpepc_adj  WHERE rcpepc_adj.tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				rcpepcAdj = rs.getString(1);
			}

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

			chq = (Double)RcpHdrMap.get("chq_amt")- (Double)RcpHdrMap.get("bill_disc_amt")+(Double)RcpHdrMap.get("oth_amt");
	
			// if EPC adjusted in same currency then
			if(isEpcCurrSame == true)
			{

				chq = chq + (Double)RcpHdrMap.get("efundadj_amt");	

			}

			
			System.out.println("chq and total "+chq+""+total);
			if(chq != total)
			{
				errString = itmDBAccessEJB.getErrorString("","VTRCPT8","","",conn);
				conn.rollback();
			}

			if(errString == null || errString.trim().length() == 0)
			{
				errString = gbfPostReceipt(tranId,xtraParams, RcpHdrMap, DetList,RcpDetList,ADetList,EpcAdjList, conn);
			}
			else
			{
				//conn.rollback();
			}
			
			

			sql = "update receipt set net_amt = ?, net_amt__bc = ? where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setDouble(1,total);
			pstmt.setDouble(2,totBc);
			pstmt.setString(3,tranId);//issue
			pstmt.executeUpdate();
			pstmt.close();
			pstmt = null;

			RcpHdrMap.put("net_amt", total);
			RcpHdrMap.put("net_amt__bc", totBc);
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			System.out.println("Exception : "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		return errString;
	}



	private String gbfPostReceipt(String tranId,String xtraParams,HashMap RcpHdrMap,ArrayList DDetList, ArrayList RcpDetList,ArrayList ADetList,ArrayList EpcAdjList, Connection conn)throws RemoteException,ITMException
	{
		System.out.println("gbfPostReceipt Called..........");
		String errString = "";
		String ledgPostConf = "";
		String sql = "";
		String refNo = "";
		String commCalcOnOff = "";
		String confDrcrOpt = "";
		String confJvOpt   = "";
		HashMap DetMap = null;
		ArrayList DetList = new ArrayList();
		double netAmount = 0.0;
		double amount = 0.0;
		double debit = 0.0;
		double credit = 0.0;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ITMDBAccessEJB itmDBAccessEJB = null;
		FinCommon finCommon = null;
		DistCommon disCommon = null;
		try
		{
			itmDBAccessEJB = new ITMDBAccessEJB();
			finCommon = new FinCommon();


			// populate rcpacct

			//DetMap = new HashMap();
			sql = "select sundry_type,sundry_code,acct_code,cctr_code,emp_code, anal_code,curr_code ,exch_rate,amount,party_doc_ref from rcpacct where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				DetMap = new HashMap();
				DetMap.put("sundry_type", rs.getString("sundry_type"));
				DetMap.put("sundry_code", rs.getString("sundry_code"));
				DetMap.put("acct_code", rs.getString("acct_code"));
				DetMap.put("cctr_code", rs.getString("cctr_code"));
				DetMap.put("emp_code", rs.getString("emp_code"));
				DetMap.put("anal_code", rs.getString("anal_code"));
				DetMap.put("curr_code", rs.getString("curr_code"));
				DetMap.put("exch_rate", rs.getDouble("exch_rate"));
				DetMap.put("amount", rs.getDouble("amount"));
				DetMap.put("party_doc_ref", rs.getString("party_doc_ref"));
				DetList.add(DetMap);
			}			
			
			System.out.println(" receipt Acct Map 1445..........");
			System.out.println(DetList.toString());



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


			// header posting
			errString = gbfPostRcphdr(tranId, xtraParams,RcpHdrMap, DetList,conn);
			if(errString != null && errString.trim().length() > 0)
			{
				return errString;
			}
			else
			{
				gbfPostRcpdet(tranId,xtraParams,RcpHdrMap,DetList,RcpDetList,ADetList,EpcAdjList,conn);
			}
			if(errString != null && errString.trim().length() > 0)
			{
				return errString;
			}

			sql = "SELECT ref_no , amount FROM  rcpepc_adj  WHERE rcpepc_adj.tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				refNo = rs.getString(1);
				amount = rs.getDouble(2);
				if(amount < 0 ){amount = -amount;}
				if(amount > 0)
				{
					sql = "update advbking set bal_amt = bal_amt - ? where tran_id  = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1,amount);
					pstmt.setString(2,refNo);
					pstmt.executeUpdate();

					pstmt.close();
					pstmt = null;

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

			sql = "SELECT ref_no , amount FROM  rcpepc_adj  WHERE rcpepc_adj.tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				refNo = rs.getString(1);
				amount = rs.getDouble(2);
				if(amount < 0 ){amount = -amount;}
				if(amount > 0)
				{
					sql = "update advbking set bal_amt = bal_amt - ? where tran_id  = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1,amount);
					pstmt.setString(2,refNo);
					pstmt.executeUpdate();

					pstmt.close();
					pstmt = null;

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

			if(errString == null && errString.trim().length() == 0)
			{
				if(Math.abs(debit - credit) >= 1)
				{
					errString = itmDBAccessEJB.getErrorString("","VTAMTMIS","","",conn);
				}

			}

			if(errString == null || errString.trim().length() == 0)
			{


			}


			commCalcOnOff = finCommon.getFinparams("999999", "COMM_CALC_ON_OFF", conn);
			if(commCalcOnOff.equalsIgnoreCase("Y"))
			{


				//nvo_commission_calculation lnvo_commission
				///lnvo_commission = create nvo_commission_calculation

				confDrcrOpt = finCommon.getFinparams("999999", "COMM_DRCR_CONF", conn);
				confJvOpt = finCommon.getFinparams("999999", "COMM_JV_CONF", conn);


				//ls_errcode = lnvo_commission.gbf_calc_commission('','R',ls_tranid,ls_conf_drcr_opt,ls_conf_jv_opt)			
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			System.out.println(e.getMessage());
			System.out.println("Exception : "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		return errString;

	}
	private String gbfPostRcphdr(String tranId,String xtraParams,HashMap RcpHdrMap,ArrayList RcpDetList, Connection conn)throws RemoteException,ITMException
	{
		System.out.println("gbfPostRcphdr called........");
		String errString = "";
		String currCode = "";
		String siteCode = "";
		String siteCodeFor = "";
		String finEntity = "";
		String acctCode = "";
		String cctrCode = "";
		String acctCodePay = "";
		String cctrCodePay = "";
		String acctCodeAr = "";
		String cctrCodeAr = "";
		String projectCode = "";
		String analCode = "";
		String bankCode = "";
		String remarks = "";
		String sundryType = "";
		String sundryCode = "";
		String sundryTypeFor = "";
		String sundryCodeFor = "";
		String contactCode = "";
		String empCode = "";
		String refNO = "";
		String rcpMode = "";
		String batchNo = "";
		String tranType = "";
		String confirm = "";
		String loginEmpCode = "";
		String winName = "";
		String editOption = "";
		String confirmed = "";
		String sql = "";
		String errorType = "";
		String bdFluctuationCf = "";
		String acctCodeBal = "";
		String cctrCodeBal = "";
		String basecurrCode = "";
		String projCode = "";
		String custCode = "";
		String salesPers = "";
		String refSer = "";
		String refNo = "";
		String acctCodeCf = "";
		String payslip = "";
		boolean isInBaseCurr =false;
		int count = 0;
		int cnt1 = 0 ;
		int cnt = 0;
		double exchRate = 0.0;
		double netAmount = 0.0;
		double amount = 0.0;
		double advAmt = 0.0;
		double netAmt = 0.0;
		double chqAmt = 0.0;
		double debit  = 0.0;
		double credit = 0.0;
		double totAmt = 0.0;
		double ediOption = 0.0;
		double netAmountBc = 0.0;
		Date effDate = null;
		Date tranDate = null;
		Date refDate = null;
		HashMap detListMap = null;
		HashMap glTraceMap = null;
		HashMap sundryBalMap = null;
		HashMap bankTranLogMap = null;
		HashMap receivablesMap = null;
		HashMap epcadjMap = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		FinCommon finCommon = null;
		ValidatorEJB validatorEJB = new ValidatorEJB();
		GenericUtility genericUtility = new GenericUtility();
		ITMDBAccessEJB itmDBAccessEJB = null;
		try
		{
			finCommon =new  FinCommon();
			loginEmpCode = validatorEJB.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
			java.util.Date today=	new java.sql.Date(new java.util.Date().getTime());
			itmDBAccessEJB = new ITMDBAccessEJB();
			bdFluctuationCf = finCommon.getFinparams("999999", "BD_FLUCTUATION_CF", conn);
			if(bdFluctuationCf.equalsIgnoreCase("NULLFOUND"))
			{
				errString = itmDBAccessEJB.getErrorString("","VTFINPARM","","",conn);
			}

			tranId = (String)RcpHdrMap.get("tran_id");
			tranType = (String)RcpHdrMap.get("tran_type");
			if(tranType.equalsIgnoreCase("J"))
			{
				 
				if((Double)RcpHdrMap.get("adv_amt") != 0 && (Double)RcpHdrMap.get("chq_amt") != 0)
				{
					errString = itmDBAccessEJB.getErrorString("","VTRCPJVAMT","","",conn);
				}
			}

			if((Double)RcpHdrMap.get("bill_amt") == 0 && (Double)RcpHdrMap.get("adv_amt") == 0 && (Double)RcpHdrMap.get("net_amt") != 0)
			{
				errString = itmDBAccessEJB.getErrorString("","VTRCPT11","","",conn);
			}

			tranId = (String)RcpHdrMap.get("tran_id");

			/// count for EPC. in diffrent currency.
			sql =" SELECT count(*) FROM rcpepc_adj,receipt  WHERE ( rcpepc_adj.tran_id = receipt.tran_id ) and   ( rcpepc_adj.curr_code <> receipt.curr_code ) and   ( ( receipt.tran_id = ? ) ) ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				cnt1 = rs.getInt(1);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			if(cnt1 == 0)
			{
				errString = itmDBAccessEJB.getErrorString("","VTIBCA5","","",conn);
				//return errString;
			}

			sql =" SELECT count(*) FROM rcpacct,receipt  WHERE ( rcpacct.tran_id = receipt.tran_id ) and  ( rcpacct.curr_code <> receipt.curr_code ) and  ( ( receipt.tran_id = ? ) )";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				cnt1 = rs.getInt(1);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			if(cnt1 == 0)
			{
				errString = itmDBAccessEJB.getErrorString("","VTIBCA5","","",conn);
				//return errString;
			}


			/// if EPC in diff currency.
			if((Double)RcpHdrMap.get("efundadj_amt")!= 0 && cnt1 > 0)
			{
				isInBaseCurr = true;
			}
			// for other charges in diff currency.
			if((Double)RcpHdrMap.get("oth_amt")!= 0 && cnt1 > 0)
			{
				isInBaseCurr = true;
			}

			if((Double)RcpHdrMap.get("bill_disc_amt")!= 0 && bdFluctuationCf.equalsIgnoreCase("N"))
			{
				isInBaseCurr = true;
			}
			finEntity = (String)RcpHdrMap.get("fin_entity");
			sql = "SELECT finent.curr_code  FROM finent WHERE finent.fin_entity = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,finEntity);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				basecurrCode = rs.getString(1);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			analCode = (String)RcpHdrMap.get("anal_code");
			tranDate = (Date)RcpHdrMap.get("tran_date");
			refDate = (Date)RcpHdrMap.get("ref_date");
			effDate = (Date)RcpHdrMap.get("eff_date");
			remarks = (String)RcpHdrMap.get("remarks");
			finEntity = (String)RcpHdrMap.get("fin_entity");
			siteCode = (String)RcpHdrMap.get("site_code");
			bankCode = (String)RcpHdrMap.get("bank_code");
			acctCodeBal = (String)RcpHdrMap.get("acct_code__bal");
			cctrCodeBal = (String)RcpHdrMap.get("cctr_code__bal");
			netAmt = (Double)RcpHdrMap.get("net_amt");
			currCode = (String)RcpHdrMap.get("curr_code");
			exchRate = (Double)RcpHdrMap.get("exch_rate");
			projCode = (String)RcpHdrMap.get("proj_code");
			projCode = (String)RcpHdrMap.get("proj_code");
			refNO = (String)RcpHdrMap.get("ref_no");
			chqAmt = (Double)RcpHdrMap.get("chq_amt");
			custCode = (String)RcpHdrMap.get("cust_code");
			netAmountBc = (Double)RcpHdrMap.get("net_amt__bc");
			acctCodeCf = (String)RcpHdrMap.get("acct_code__cf");
			rcpMode = (String)RcpHdrMap.get("rcp_mode");

			for (int ctr =0; ctr < RcpDetList.size(); ctr++)
			{
				detListMap = new HashMap();
				detListMap = (HashMap) RcpDetList.get(ctr);


				cctrCodeAr = (String)detListMap.get("cctr_code__ar");

			}
			// insert into gltrace for bank

			glTraceMap = new HashMap();
			glTraceMap.put("tran_date", new Timestamp(tranDate.getTime()));
			glTraceMap.put("eff_date",new Timestamp(effDate.getTime()));
			glTraceMap.put("fin_entity", finEntity);
			glTraceMap.put("site_code", siteCode);
			glTraceMap.put("sundry_type", "B");
			glTraceMap.put("sundry_code", bankCode);
			glTraceMap.put("acct_code", acctCodeBal);
			glTraceMap.put("cctr_code", cctrCodeBal);
			glTraceMap.put("emp_code", "");
			glTraceMap.put("anal_code", "");


			System.out.println("AMOUTTTTTTTTTTTTTTT"+(Double)RcpHdrMap.get("net_amt")+""+netAmountBc);
			if(isInBaseCurr == true)
			{

				System.out.println("IN TRUE!!!!!!!!!!!!!!!!!");
				glTraceMap.put("curr_code", basecurrCode);

				glTraceMap.put("exch_rate", 1);
				if((Double)RcpHdrMap.get("net_amt__bc") < 0 )
				{
					glTraceMap.put("dr_amt", 0.0);
					glTraceMap.put("cr_amt", 0 - netAmountBc);
				}

				else{
					glTraceMap.put("dr_amt", netAmountBc);
					glTraceMap.put("cr_amt", 0.0);
				}
			}
			else 
			{
				System.out.println("IN FALSE!!!!!!!!!!!!!!!!!");
				glTraceMap.put("curr_code", currCode);

				glTraceMap.put("exch_rate", exchRate);
				if((Double)RcpHdrMap.get("net_amt") < 0 )
				{
					glTraceMap.put("dr_amt", 0.0);
					glTraceMap.put("cr_amt", 0 - netAmt);
				}

				else{
					glTraceMap.put("dr_amt", netAmt);
					glTraceMap.put("cr_amt", 0.0);
				}
			}
			glTraceMap.put("ref_type","F");
			glTraceMap.put("ref_ser", "RCP");
			glTraceMap.put("ref_id", tranId);
			glTraceMap.put("remarks", remarks);
			glTraceMap.put("proj_code", projectCode);
			//ls_errcode = gbf_gltrace_upd(lstr_gltrace)
			System.out.println("1766 glTraceUpdate Called..............");
			errString = finCommon.glTraceUpdate(glTraceMap,conn); 
			if(errString != null && errString.trim().length() > 0)
			{
				return errString;
			}

			debit  = debit  + (Double)glTraceMap.get("dr_amt")* (Double)glTraceMap.get("exch_rate");
			credit = credit + (Double)glTraceMap.get("cr_amt") * (Double)glTraceMap.get("exch_rate");

			// update sundrybal for bank

			sundryBalMap = new HashMap();
			sundryBalMap.put("tran_date",new Timestamp(tranDate.getTime()));
			sundryBalMap.put("eff_date",new Timestamp(effDate.getTime()));
			sundryBalMap.put("fin_entity", finEntity);
			sundryBalMap.put("site_code", siteCode);
			sundryBalMap.put("sundry_type", "B");
			sundryBalMap.put("sundry_code", bankCode);
			sundryBalMap.put("acct_code", acctCodeBal);
			sundryBalMap.put("cctr_code", cctrCodeBal);
			if(isInBaseCurr == true)
			{
				sundryBalMap.put("curr_code", basecurrCode);

				sundryBalMap.put("exch_rate", 1);
				if((Double)RcpHdrMap.get("net_amt__bc") < 0 )
				{
					sundryBalMap.put("dr_amt", 0.0);
					sundryBalMap.put("cr_amt", 0 - netAmountBc );
				}

				else{
					sundryBalMap.put("dr_amt", netAmountBc);
					sundryBalMap.put("cr_amt", 0.0);
				}
			}
			else 
			{
				sundryBalMap.put("curr_code", currCode);

				sundryBalMap.put("exch_rate", exchRate);
				if((Double)RcpHdrMap.get("net_amt") < 0 )
				{
					sundryBalMap.put("dr_amt", 0.0);
					sundryBalMap.put("cr_amt", 0 - netAmt);
				}

				else{
					sundryBalMap.put("dr_amt", netAmt);
					sundryBalMap.put("cr_amt", 0.0);
				}
			}

			sundryBalMap.put("adv_amt", 0.0);
			sundryBalMap.put("contact_code", "");
			//Added by Sharon as instructed by Shiri madam on 29-July-2004
			if (chqAmt != 0 || advAmt > 0 )
			{
				System.out.println("In 1826 SUNDRY-BAL !!!!!!!!!!!!!!!!!!!!!!!!!!");
				errString = finCommon.gbf_sundrybal_upd(sundryBalMap,conn);
			}
			if(errString != null && errString.trim().length() > 0)
			{
				return errString;
			}

			//if chq amt is zero then should not be entered into banktran_log
			if (chqAmt != 0 )
			{
				// update banktran_log
				bankTranLogMap = new HashMap();
				bankTranLogMap.put("tran_date", new Timestamp(tranDate.getTime()));
				bankTranLogMap.put("tran_ser", "RCP");
				bankTranLogMap.put("tran_no", tranId);
				bankTranLogMap.put("bank_code", bankCode);
				bankTranLogMap.put("ref_no", refNO);
				bankTranLogMap.put("ref_date", new Timestamp(refDate.getTime()));
				bankTranLogMap.put("ref_mode", rcpMode);
				if(isInBaseCurr == true)
				{
					//bankTranLogMap.put("curr_code", basecurrCode);
					bankTranLogMap.put("exch_rate", 1);
					if((Double)RcpHdrMap.get("net_amt__bc") < 0 )
					{
						bankTranLogMap.put("amount",0 - (Double)RcpHdrMap.get("net_amt__bc"));
						bankTranLogMap.put("tran_type", "P");
					}

					else{
						bankTranLogMap.put("amount",(Double)RcpHdrMap.get("net_amt__bc"));
						bankTranLogMap.put("tran_type", "R");
					}
				}
				else 
				{
					//bankTranLogMap.put("curr_code", currCode);

					bankTranLogMap.put("exch_rate", exchRate);
					if((Double)RcpHdrMap.get("net_amt") < 0 )
					{
						bankTranLogMap.put("amount",0 - netAmt);
						bankTranLogMap.put("tran_type", "P");
					}

					else{
						bankTranLogMap.put("amount",netAmt);
						bankTranLogMap.put("tran_type", "R");
					}
				}
				bankTranLogMap.put("site_code", siteCode);
				bankTranLogMap.put("acct_code", acctCodeBal);
				bankTranLogMap.put("cctr_code",cctrCodeBal);
				bankTranLogMap.put("sundry_type", "C");
				bankTranLogMap.put("sundry_code", custCode);
				bankTranLogMap.put("remarks", remarks);
				bankTranLogMap.put("due_date", new Timestamp(tranDate.getTime()));
				bankTranLogMap.put("exch_rate", exchRate);
				bankTranLogMap.put("entry_batch_no",(String)RcpHdrMap.get("entry_batch_no") );
				//ls_errcode = gbf_banktranlog_upd(lstr_banktranlog)
				System.out.println("545 bankTranLogUpdateeUpdate Called..............");
				errString = finCommon.bankTranLogUpdate(bankTranLogMap, xtraParams, conn);
				if(errString != null && errString.trim().length() > 0)
				{
					return errString;
				}

			}

			// insert into gltrace for currency fluctuation
			if((Double)RcpHdrMap.get("diff_amt__exch") != 0)
			{
				glTraceMap = new HashMap();

				glTraceMap.put("tran_date", new Timestamp(tranDate.getTime()));
				glTraceMap.put("eff_date",new Timestamp(effDate.getTime()));
				glTraceMap.put("fin_entity", finEntity);
				glTraceMap.put("site_code", siteCode);
				glTraceMap.put("sundry_type", "O");
				glTraceMap.put("sundry_code", bankCode);
				glTraceMap.put("acct_code", (String)RcpHdrMap.get("acct_code__cf"));
				if(acctCodeCf ==null || acctCodeCf.trim().length() == 0)
				{
					if(RcpDetList.size() >= 1)
					{
						glTraceMap.put("cctr_code", cctrCodeAr);
					}
				}
				else
				{
					glTraceMap.put("cctr_code", (String)RcpHdrMap.get("cctr_code__cf"));
				}
				glTraceMap.put("emp_code", "");
				glTraceMap.put("anal_code", "");
				glTraceMap.put("curr_code", basecurrCode);
				glTraceMap.put("exch_rate", 1);
				if((Double)RcpHdrMap.get("diff_amt__exch") > 0)
				{
					glTraceMap.put("dr_amt",(Double)RcpHdrMap.get("diff_amt__exch"));
					glTraceMap.put("cr_amt", 0);
					cnt1=1;
				}else
				{
					glTraceMap.put("dr_amt", 0.0);
					glTraceMap.put("cr_amt", 0 - (Double)RcpHdrMap.get("diff_amt__exch"));
					cnt1=2;
				}

				glTraceMap.put("ref_type","F");
				glTraceMap.put("ref_ser", "RCP");
				glTraceMap.put("ref_id", tranId);
				glTraceMap.put("remarks", remarks);
				glTraceMap.put("proj_code", projectCode);
				//ls_errcode = gbf_gltrace_upd(lstr_gltrace)
				System.out.println("1938 glTraceUpdate Called..............");
				errString = finCommon.glTraceUpdate(glTraceMap,conn);
				if(errString != null && errString.trim().length() > 0)
				{
					return errString;
				}


			}
			debit  = debit  + ((Double)glTraceMap.get("dr_amt") *(Double)glTraceMap.get("exch_rate"));
			credit = credit +((Double)glTraceMap.get("cr_amt") * (Double)glTraceMap.get("exch_rate"));



			// insert into receivables for advance amount

			if((Double)RcpHdrMap.get("adv_amt") > 0)
			{
				sql = "select sales_pers  from customer where cust_code  = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,custCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					salesPers = rs.getString(1);
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;

				receivablesMap = new HashMap();
				receivablesMap.put("tran_ser", "R-ADV");
				receivablesMap.put("tran_date", new Timestamp(tranDate.getTime()));
				receivablesMap.put("fin_entity", finEntity);
				receivablesMap.put("site_code", siteCode);
				receivablesMap.put("ref_no", tranId);
				receivablesMap.put("sale_order", (String)RcpHdrMap.get("sale_order"));
				receivablesMap.put("ref_date", new Timestamp(tranDate.getTime()));
				receivablesMap.put("due_date", new Timestamp(tranDate.getTime()));
				receivablesMap.put("curr_code", currCode);
				receivablesMap.put("exch_rate", exchRate);
				receivablesMap.put("cust_code", custCode);
				receivablesMap.put("acct_code", acctCode);
				receivablesMap.put("cctr_code", cctrCode);
				receivablesMap.put("eff_date", new Timestamp(effDate.getTime()));
				receivablesMap.put("tax_amt", 0);
				receivablesMap.put("discount", 0);
				receivablesMap.put("tot_amt", 0- advAmt);
				receivablesMap.put("auto_rcp", "N");
				receivablesMap.put("bank_code","");
				receivablesMap.put("ref_type", "F");
				receivablesMap.put("cr_term", "");
				receivablesMap.put("sales_pers", salesPers);
				receivablesMap.put("item_ser", (String)RcpHdrMap.get("item_ser")==null?"":(String)RcpHdrMap.get("item_ser"));
				receivablesMap.put("fin_scheme",(String)RcpHdrMap.get("fin_scheme"));
				//added extra column for gbfReceivablesUpd method 
				receivablesMap.put("sales_pers__1","");
				receivablesMap.put("sales_pers__2","");
				receivablesMap.put("line_no__ref",null);
				receivablesMap.put("mrp_value","0");
				receivablesMap.put("gp_no","");
				receivablesMap.put("gp_date","");
				receivablesMap.put("cust_ref_no","");
				receivablesMap.put("cust_ref_date","");
				receivablesMap.put("cust_ref_amt","");
				
				System.out.println("In 1997 receivablesMap !!!!!!!!!!!!!!!!!!!!!!!!!!");			
				errString = finCommon.gbfReceivablesUpd(receivablesMap,conn);
				if(errString != null && errString.trim().length() > 0)
				{
					return errString;
				}

				// update sundrybal for adv amt > 0
				sundryBalMap = new HashMap();
				sundryBalMap.put("tran_date",new Timestamp(tranDate.getTime()));
				sundryBalMap.put("eff_date",new Timestamp(effDate.getTime()));
				sundryBalMap.put("fin_entity", finEntity);
				sundryBalMap.put("site_code", siteCode);
				sundryBalMap.put("sundry_type", "B");
				sundryBalMap.put("sundry_code", bankCode);
				sundryBalMap.put("acct_code", (String)RcpHdrMap.get("acct_code__adv"));
				sundryBalMap.put("cctr_code", (String)RcpHdrMap.get("cctr_code__adv"));
				sundryBalMap.put("curr_code", currCode);
				sundryBalMap.put("exch_rate", exchRate);
				sundryBalMap.put("dr_amt", 0.0);
				sundryBalMap.put("cr_amt", 0.0);
				sundryBalMap.put("adv_amt", 0.0);
				contactCode = finCommon.getContactCode(sundryType, sundryCode, conn);
				sundryBalMap.put("contact_code",contactCode );
				System.out.println("In 2021 sundryBalMap !!!!!!!!!!!!!!!!!!!!!!!!!!");
				errString = finCommon.gbf_sundrybal_upd(sundryBalMap,conn);
				if(errString != null && errString.trim().length() > 0)
				{
					return errString;
				}

				// Update pay-in-slip 

				payslip = (String)RcpHdrMap.get("entry_batch_no")==null?"":(String)RcpHdrMap.get("entry_batch_no");

				if(payslip.trim().length() > 0)
				{
					sql =" select count(1) from receipt_pay_in_slip where tran_id = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,tranId);
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						cnt = rs.getInt("tot_amt");
					}
					if(cnt <= 0)
					{
						sql = "insert into receipt_pay_in_slip ( tran_id, entry_batch_no ) values (?, ?)";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,tranId);
						pstmt.setString(2,payslip);
						rs = pstmt.executeQuery();

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

				/*rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;*/


				confirmed = (String)RcpHdrMap.get("confirmed");
				if(!confirmed.equalsIgnoreCase("Y"))
				{
					sql = "update receipt set confirmed = ?, conf_date = ?, emp_code__aprv = ? where tran_id = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,"Y");
					pstmt.setDate(2,new java.sql.Date(new java.util.Date().getTime()));
					pstmt.setString(3,loginEmpCode);
					pstmt.setString(4,tranId);
					pstmt.executeUpdate();

					pstmt.close();
					pstmt = null;

				}
				//lds_deta.retrieve(lstr_gltrace.ref_id)

				// Selecting from transetup
				sql = "select edi_option from transetup where tran_window = ?";	
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,"w_receipt_adv");	
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					ediOption = rs.getInt(1);
				}

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

				if(ediOption > 0)
				{
					RcpHdrMap.put("confirmed", "Y");
					RcpHdrMap.put("conf_date", today);
					RcpHdrMap.put("emp_code__aprv", loginEmpCode);
				}



				/*
				sql = "select edi_option from transetup where tran_window = ?";
				pstmt =  conn.prepareStatement(sql);
				pstmt.setString(1,"w_misc_rcp_oth");
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					editOption = rs.getString("edi_option") == null ?" ":rs.getString("edi_option");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				if(editOption != null && editOption.trim().equalsIgnoreCase("0"))
				{

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

				}*/

			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			System.out.println(e.getMessage());
			System.out.println("Exception : "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		return errString;
	}

	private String gbfPostRcpdet(String tranId,String xtraParams,HashMap RcpHdrMap,ArrayList DetList , ArrayList RcpDetList,ArrayList ADetList,ArrayList EpcAdjList,Connection conn)throws RemoteException,ITMException
	{
		System.out.println("gbfPostRcpdet called..............");
		String errString = "";
		String currCode = "";
		String siteCode = "";
		String siteCodeFor = "";
		String finEntity = "";
		String acctCode = "";
		String cctrCode = ""; 
		String recoAcctCode = "";
		String recoCctrCode = "";
		String taxAcctCode = "";
		String taxCctrCode = "";
		String acctCode1 = "";
		String cctrCode1 = "";
		String acctCodeAr = "";
		String cctrCodeAr = "";
		String projectCode = "";
		String analCode = "";
		String bankCode = "";
		String remarks = "";
		String sundryType = "";
		String contactCode = "";
		String sundryCode = "";
		String sundryTypeFor = "";
		String sundryCodeFor = "";
		String tranType = "";
		String empCode = "";
		String refNO = "";
		String rcpMode = "";
		String batchNo = "";
		String confirm = "";
		String loginEmpCode = "";
		String winName = "";
		String editOption = "";
		String sql = "";
		String hcurrCode = "";
		String errorType = "";
		String payslip = "";
		String partyDocRef = "";
		String refSer = "";
		String refNo = "";
		String salesPers = "";
		String status = "";
		String lineNoPef = "";
		int count = 0;
		int lineNo = 0;
		double exchRate = 0.0;
		double hexchRate = 0.0;
		double netAmount = 0.0;
		double amount = 0.0;
		double taxAmount = 0.0;
		double recoAmount = 0.0;
		double debit  = 0.0;
		double credit = 0.0;
		double totAmt = 0.0;
		double advAmt = 0.0;
		double adjAmt = 0.0;
		double advAdj = 0.0;
		double recoverAmt = 0.0;
		Date effDate = null;
		Date tranDate = null;
		Date refDate = null;
		HashMap DetMap = null;
		HashMap RcpDetMap = null;
		HashMap ADetMap = null;
		HashMap EpcAdjMap = null;
		HashMap glTraceMap = null;
		HashMap sundryBalMap = null;
		HashMap bankTranLogMap = null;
		HashMap detListMap = null;
		HashMap recofiltMap = null;
		HashMap recodataMap = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		FinCommon finCommon = null;
		DistCommon distCommon = null;
		ITMDBAccessEJB itmDBAccessEJB = null;
		ValidatorEJB validatorEJB = new ValidatorEJB();
		GenericUtility genericUtility = new GenericUtility();
		try
		{
			finCommon = new  FinCommon();
			distCommon = new DistCommon();
			itmDBAccessEJB = new ITMDBAccessEJB();
			loginEmpCode = validatorEJB.getValueFromXTRA_PARAMS(xtraParams, "loginCode");

			tranDate = (Date)RcpHdrMap.get("tran_date");
			effDate = (Date)RcpHdrMap.get("eff_date");
			finEntity = (String)RcpHdrMap.get("fin_entity");
			siteCode = (String)RcpHdrMap.get("site_code");
			siteCodeFor = (String)RcpHdrMap.get("site_code__for");
			projectCode = (String)RcpHdrMap.get("proj_code");
			hcurrCode = (String)RcpHdrMap.get("curr_code");
			hexchRate = (Double)RcpHdrMap.get("exch_rate");
			remarks = (String)RcpHdrMap.get("remarks");

			System.out.println("DetList SIZE "+DetList.size());
			System.out.println("DetList SIZE "+DetList);

			for (int ctr =0; ctr < DetList.size(); ctr++)
			{

				System.out.println("no of times "+ctr);
				DetMap = new HashMap();
				DetMap = (HashMap) DetList.get(ctr);

				System.out.println("DetMap SIZE "+DetMap);

				/* sundryType = (String)detListMap.get("sundry_type");
				sundryCode = (String)detListMap.get("sundry_code");					
				acctCode = (String)detListMap.get("acct_code");
				cctrCode = (String)detListMap.get("cctr_code");
				analCode = (String)detListMap.get("anal_code");
				empCode = (String)detListMap.get("emp_code");
				//projectCode = (String)detListMap.get("proj_code");
				currCode = (String)detListMap.get("curr_code");
				exchRate = (Double)detListMap.get("exch_rate");
				partyDocRef = (String)detListMap.get("party_doc_ref");*/


				glTraceMap = new HashMap();
				glTraceMap.put("tran_date",new Timestamp(tranDate.getTime()));
				glTraceMap.put("eff_date",new Timestamp(effDate.getTime()));
				glTraceMap.put("fin_entity", finEntity);
				glTraceMap.put("site_code", siteCode);
				glTraceMap.put("sundry_type",(String)DetMap.get("sundry_type")==null?"O":(String)DetMap.get("sundry_type"));
				glTraceMap.put("sundry_code",(String)DetMap.get("sundry_code")== null?"":(String)DetMap.get("sundry_code"));
				glTraceMap.put("acct_code", (String)DetMap.get("acct_code"));
				System.out.println("818 account chek ="+acctCode+"  "+sundryType+"   "+sundryCode);
				glTraceMap.put("cctr_code", (String)DetMap.get("cctr_code")==null?"":(String)DetMap.get("cctr_code"));
				glTraceMap.put("emp_code", (String)DetMap.get("emp_code"));
				glTraceMap.put("anal_code", (String)DetMap.get("anal_code"));
				glTraceMap.put("curr_code", (String)DetMap.get("curr_code")==null?hcurrCode:(String)DetMap.get("curr_code"));
				glTraceMap.put("proj_code", projectCode);
				glTraceMap.put("exch_rate", (Double)DetMap.get("exch_rate"));

				amount = (Double)DetMap.get("amount");
				if(amount > 0)
				{
					glTraceMap.put("dr_amt",0.0);
					glTraceMap.put("cr_amt",amount);
				}
				else
				{
					glTraceMap.put("dr_amt",0 - amount);
					glTraceMap.put("cr_amt",0.0);
				}
				glTraceMap.put("ref_type","F");
				glTraceMap.put("ref_ser", "RCP");
				glTraceMap.put("ref_id", tranId);
				glTraceMap.put("remarks",remarks);
				glTraceMap.put("party_doc_ref",partyDocRef);
				System.out.println("848 glTraceUpdate Called..............");
				errString = finCommon.glTraceUpdate(glTraceMap,conn);
				if(errString != null && errString.trim().length() > 0)
				{
					System.out.println("ERROR IN GLTRACE ...........");
					
					return errString;
				}

				debit  = debit  + ((Double)glTraceMap.get("dr_amt") * (Double)glTraceMap.get("exch_rate"));
				credit = credit + ((Double)glTraceMap.get("cr_amt") * (Double)glTraceMap.get("exch_rate"));

				/// update sundrybal for customer code
				if((String)glTraceMap.get("sundry_type") != null && !((String)glTraceMap.get("sundry_type")).trim().equalsIgnoreCase("O"))
				{
					System.out.println("COMES INTO CONDITION ...........");
					
					
					sundryBalMap = new HashMap();
					sundryBalMap.put("tran_date",new Timestamp(tranDate.getTime()));
					sundryBalMap.put("eff_date",new Timestamp(effDate.getTime()));
					sundryBalMap.put("fin_entity", finEntity);
					sundryBalMap.put("site_code", siteCode);
					sundryBalMap.put("sundry_type", sundryType);
					sundryBalMap.put("sundry_code", sundryCode);
					sundryBalMap.put("acct_code", (String)DetMap.get("acct_code"));
					sundryBalMap.put("cctr_code", (String)DetMap.get("cctr_code")==null?"":(String)DetMap.get("cctr_code"));
					sundryBalMap.put("curr_code", (String)DetMap.get("curr_code")==null?hcurrCode:(String)DetMap.get("curr_code"));
					sundryBalMap.put("exch_rate", (Double)DetMap.get("exch_rate"));

					amount = (Double)DetMap.get("amount");
					if(amount > 0)
					{
						sundryBalMap.put("dr_amt", 0.0);
						sundryBalMap.put("cr_amt", amount);
					}
					else
					{
						sundryBalMap.put("dr_amt", 0 - amount);
						sundryBalMap.put("cr_amt", 0.0);
					}
					sundryBalMap.put("adv_amt", 0.0);
					contactCode = finCommon.getContactCode(sundryType, sundryCode, conn);
					sundryBalMap.put("contact_code",contactCode );
					System.out.println("2366 sundryBaleUpdate Called..............");
					errString = finCommon.gbf_sundrybal_upd(sundryBalMap, conn);
					if(errString != null && errString.trim().length() > 0)
					{
						System.out.println("error is sundryBaleUpdate ..........."+errString);
						
						return errString;
					}
				}
				System.out.println("last ...........");
			}

			for (int ctr =0; ctr < RcpDetList.size(); ctr++)
			{
				RcpDetMap = new HashMap();
				RcpDetMap = (HashMap) RcpDetList.get(ctr);


				refSer = (String)RcpDetMap.get("ref_ser");
				refNo = (String)RcpDetMap.get("ref_no");
				amount = (Double)RcpDetMap.get("rcp_amt");
				lineNoPef = (String)RcpDetMap.get("line_no__ref");


				itmDBAccessEJB = new ITMDBAccessEJB();

				if ("db2".equalsIgnoreCase(CommonConstants.DB_NAME ))
				{
					sql = "SELECT tot_amt, adj_amt, recover_amt, sales_pers FROM receivables  WHERE tran_ser = ?  and ref_no = ? and line_no__ref  = ? for update ";
				}

				else if ( "mssql".equalsIgnoreCase(CommonConstants.DB_NAME ))
				{
					sql = "SELECT tot_amt, adj_amt, recover_amt, sales_pers FROM receivables (updlock) where tran_ser = ?  and ref_no = ? and line_no__ref  = ? " ;
				}
				else
				{
					sql =" SELECT tot_amt, adj_amt, recover_amt, sales_pers FROM receivables WHERE tran_ser = ?  and ref_no = ? and line_no__ref  = ? for update nowait" ;
				}
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,refSer);
				pstmt.setString(2,refNo);
				pstmt.setString(3,lineNoPef);
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					totAmt = rs.getDouble("tot_amt");
					adjAmt = rs.getDouble("adj_amt");
					recoverAmt = rs.getDouble("recover_amt");
					salesPers = rs.getString("sales_pers");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;


				if(Math.abs(amount - adjAmt) < Math.abs(amount))
				{
					errString = itmDBAccessEJB.getErrorString("","VTBAL1","","",conn);
				}


				adjAmt = adjAmt + totAmt;
				if(totAmt == adjAmt)
				{
					status = "A";
				}
				else
				{
					status = "P";
				}


				sql = "update receivables set adj_amt = ?, status = ?,stat_date = ? where tran_ser = ? and ref_no = ? and line_no__ref = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setDouble(1,adjAmt);
				pstmt.setString(2,status);
				pstmt.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
				pstmt.setString(4,refSer);
				pstmt.setString(5,refNo);
				pstmt.setString(6,lineNoPef);
				pstmt.executeUpdate();

				pstmt.close();
				pstmt = null;

				/*if(recoverAmt > 0)
				{
					/// filter data	
					recofiltMap.put("crnote_date", new Timestamp(tranDate.getTime()));//issue
					recofiltMap.put("site_code",siteCode);
				}

				/// actual data for generating debit/credit note.

				if(recoverAmt <= amount)
				{
					recodataMap.put("os_reco_amt", recoverAmt);//issue
				}
				else
				{
					recodataMap.put("os_reco_amt", amount);
				}

				recodataMap.put("tran_ser", refSer);
				recodataMap.put("ref_no", refNo);
				recodataMap.put("sales_pers", salesPers);
				recodataMap.put("curr_code", currCode);
				recodataMap.put("exch_rate", exchRate);
				recodataMap.put("site_code", siteCode);
				recodataMap.put("fin_entity", finEntity);

				recodataMap.put("tot_amt", totAmt);
				recodataMap.put("adj_amt", adjAmt);*/ //open

				/*ls_errcode = nvo_drcr_rcp.gbf_misc_credit_note_os_reco_dealer(lds_recofilt, lds_recodata, 'C', 'RCP', ls_tranid) */ //issue



			}


			// do the advance adjustment

			for (int ctr =0; ctr < ADetList.size(); ctr++)
			{
				ADetMap = new HashMap();
				ADetMap = (HashMap) ADetList.get(ctr);


				refSer = (String)ADetMap.get("ref_ser");
				refNo = (String)ADetMap.get("ref_no");
				advAdj = (Double)ADetMap.get("adj_amt");


				sql ="select tot_amt, adj_amt from receivables where tran_ser = ? and ref_no = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,refSer);
				pstmt.setString(2,refNo);
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					amount = rs.getDouble("tot_amt");
					adjAmt = rs.getDouble("adj_amt");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;


				if(Math.abs(amount - adjAmt) < Math.abs(adjAmt))
				{
					errString = itmDBAccessEJB.getErrorString("","VTBAL1","","",conn);
				}


				if (Math.abs(amount) == Math.abs(adjAmt + advAdj))
				{
					status = "A";
				}
				else
				{
					status = "P";
				}
				adjAmt = adjAmt + advAdj;
				sql = "update receivables set adj_amt = adj_amt + ?,status = ?, stat_date = ? where tran_ser = ? and ref_no = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setDouble(1,advAdj);
				pstmt.setString(2,status);
				pstmt.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
				pstmt.setString(4,refSer);
				pstmt.setString(5,refNo);
				pstmt.executeUpdate();

				pstmt.close();
				pstmt = null;
				// adjusted in sunbdrybal

				sundryBalMap = new HashMap();
				sundryBalMap.put("tran_date",new Timestamp(tranDate.getTime()));
				sundryBalMap.put("eff_date",new Timestamp(effDate.getTime()));
				sundryBalMap.put("fin_entity", finEntity);
				sundryBalMap.put("site_code", siteCode);
				sundryBalMap.put("sundry_type", "C");
				sundryBalMap.put("sundry_code", (String)RcpHdrMap.get("cust_code"));
				sundryBalMap.put("acct_code", (String)ADetMap.get("acct_code"));
				sundryBalMap.put("cctr_code", (String)ADetMap.get("cctr_code"));
				sundryBalMap.put("curr_code", hcurrCode);
				sundryBalMap.put("exch_rate", (Double)ADetMap.get("exch_rate__vouch"));
				sundryBalMap.put("dr_amt", 0.0);
				sundryBalMap.put("cr_amt", 0.0);
				sundryBalMap.put("adv_amt", (Double)ADetMap.get("adj_amt"));
				contactCode = finCommon.getContactCode((String)sundryBalMap.get("sundry_type"),(String) sundryBalMap.get("sundry_code"), conn);
				sundryBalMap.put("contact_code",contactCode );
				System.out.println("884 sundryBaleUpdate Called..............");
				errString = finCommon.gbf_sundrybal_upd(sundryBalMap, conn);
				if(errString != null && errString.trim().length() > 0)
				{
					return errString;
				}

			}

			for (int ctr =0; ctr < EpcAdjList.size(); ctr++)
			{
				EpcAdjMap = new HashMap();
				EpcAdjMap = (HashMap) EpcAdjList.get(ctr);


				refSer = (String)EpcAdjMap.get("ref_ser");
				refNo = (String)EpcAdjMap.get("ref_no");
				amount = (Double)EpcAdjMap.get("amount");;


				itmDBAccessEJB = new ITMDBAccessEJB();

				if ("db2".equalsIgnoreCase(CommonConstants.DB_NAME ))
				{
					sql = "SELECT  tot_amt, adj_amt FROM receivables  WHERE tran_ser = ?  and ref_no = ?  for update ";
				}

				else if ( "mssql".equalsIgnoreCase(CommonConstants.DB_NAME ))
				{
					sql = "SELECT  tot_amt, adj_amt FROM receivables (updlock) where tran_ser = ? and  ref_no = ?  " ;
				}
				else
				{
					sql =" SELECT  tot_amt, adj_amt FROM receivables WHERE tran_ser = ?  and ref_no = ?  for update nowait" ;
				}
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,"E-FUND");
				pstmt.setString(2,refNo);
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					totAmt = rs.getDouble("tot_amt");
					adjAmt = rs.getDouble("adj_amt");

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


				if(Math.abs(amount - adjAmt) < Math.abs(amount))
				{
					errString = itmDBAccessEJB.getErrorString("","VTBAL1","","",conn);
				}


				adjAmt = adjAmt + totAmt;
				if(totAmt == adjAmt)
				{
					status = "A";
				}
				else
				{
					status = "P";
				}


				sql = "update misc_payables set adj_amt = ?  where tran_ser = 'E-FUND' and ref_no = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setDouble(1,adjAmt);
				pstmt.setString(2,"E-FUND");
				pstmt.setString(3,refNo);
				pstmt.executeUpdate();

				pstmt.close();
				pstmt = null;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			System.out.println(e.getMessage());
			System.out.println("Exception : "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		return errString;
	}

}
