package ibase.webitm.ejb.fin;

import java.rmi.RemoteException;

import java.util.*;
import java.sql.*;
import javax.ejb.*;

import ibase.utility.CommonConstants;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.TransIDGenerator;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import ibase.webitm.ejb.ITMDBAccessEJB;
import javax.ejb.Stateless; // added for ejb3
@Stateless // added for ejb3
public class ReceivablesOpeningConf extends ActionHandlerEJB implements ReceivablesOpeningConfLocal,ReceivablesOpeningConfRemote //SessionBean
{  	
	GenericUtility genericUtility = null;
	Connection conn = null;
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	//ITMDBAccess  itmDBAccess = null;

  /*	public void ejbCreate() throws RemoteException, CreateException
	{
		try
		{
			System.out.println("ReceivablesOpeningConfEJB ejbCreate called...");
		}
		catch (Exception e)
		{
			System.out.println("Exception [01]ReceivablesOpeningConfEJB :ejbCreate :==>"+e);
			throw new CreateException();
		}
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/

	public String confirm(String tranID,String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		String result = "";
		try
		{
			System.out.println("Confirm ReceivablesOpeningConfEJB Called....");
			result =  confirmReceivables(tranID, xtraParams); 
		}
		catch(Exception e)
		{
			System.out.println("Exception [02]" + e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("Returning Result :: " + result);
		return result;
	}
	private String confirmReceivables(String tranID, String xtraParams) throws RemoteException,ITMException
	{
		String retString = "";
		Statement stmt = null, stmt1 = null, stmt2 = null, stmt3 = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSet rsDtl = null;
		ResultSet rsMst = null;

		String confirmed = "";
		String siteCode = null;
		String finEntity = "";
		String currCode = "";
		String acctCode = "";
		String acctPrd = "";
		String cctrCode = "";
		String prdCode = "";
		String chgUser = "";
		String chgTerm = "";
		String refNo = "";
		String custCode = "";
		String crTerm = null;
		String reasCode = "";
		String itemSer = null;
		String salesPers = "";
		String salesPers1 = "";
		String salesPers2 = "";
		String contactCode = "";
		String finScheme = "";
		String saleOrder = "";
		String reasonDtl = "";
		String status = "";
		String refSerOrg ="";
		String billDisc ="";
		String tranSer = "";
		String auotRcp = "";
		String bankCode = null;
		String recd = "";
		String refType = "";
		String reasonCode = "";
		String autoRcp = "";
		String userId = "";
		
		String sqlHdr = "";
		String sqlDtl = "";
		String sqlMst = "";
		String sqlIns = "";
		String sql = "";
		
		int cnt = 0;
		int reminderCnt = 0;
		int recvDetCnt = 0;
		double totAmt = 0;
		double diffAmt = 0;
		double adjAmt = 0;
		double taxAmt = 0;
		double drAmt = 0;
		double crAmt = 0;
		double exchRate = 0;
		double discount = 0;
		double recoverAmt = 0;
		double allocAmt = 0;
		double disputeAmt = 0;
		double badDebtAmt = 0;
		double diffAmount = 0;
		double totAmtOld = 0d;
		double crAmtOld = 0d, drAmtOld = 0d, crAmtNew = 0d, drAmtNew = 0d;

		java.sql.Timestamp chgDate = null;
		java.sql.Timestamp dueDate = null;
		java.sql.Timestamp tranDate = null;
		java.sql.Timestamp refDate = null;
		java.sql.Timestamp statDate = null;
		java.sql.Timestamp effDate = null;
		
		GenericUtility genericUtility = GenericUtility.getInstance();
		ConnDriver connDriver = new ConnDriver();
		
		String empCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginEmpCode");
		userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
		try
		{
			try
			{
				conn = connDriver.getConnectDB("DriverITM");
				conn.setAutoCommit(false);
			}
			catch(Exception e)
			{
				System.out.println("Exception :03: connecting Database [confirmReceivables] : " + e);
			}	
			try
			{
				sqlHdr = "SELECT TRAN_DATE, SITE_CODE, FIN_ENTITY, CURR_CODE, TOT_AMT, ADJ_AMT, EMP_CODE__APRV, CONFIRMED, ACCT_PRD, TRAN_TYPE FROM RECEIVABLES_HDR WHERE TRAN_ID ='"+ tranID +"'";	 //ACCT_PRD and TRAN_TYPE ADDED GULZAR 02-12-06
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sqlHdr);
				if (rs.next())
				{
					tranDate	= rs.getTimestamp("TRAN_DATE");
					siteCode 	= rs.getString("SITE_CODE");
					finEntity 	= rs.getString("FIN_ENTITY");
					currCode 	= rs.getString("CURR_CODE"); 
					totAmt 		= rs.getDouble("TOT_AMT");
					adjAmt 		= rs.getDouble("ADJ_AMT");
					empCode 	= rs.getString("EMP_CODE__APRV");
					confirmed 	= rs.getString("CONFIRMED");
					acctPrd 	= rs.getString("ACCT_PRD"); //Gulzar 30/11/6
					refType 	= rs.getString("TRAN_TYPE"); //Gulzar 02-12-06
				}
			}
			catch(SQLException se)
			{
				System.out.println("SQLException [04][confirmReceivables][Excuting Query Failed]" + sqlHdr + se.getMessage());
				se.printStackTrace();
				retString = se.getMessage();
				return retString;
			}
			catch(Exception e)
			{
				System.out.println("Exception [05][confirmReceivables][Excuting Query Failed]" + sqlHdr + e.getMessage());
				e.printStackTrace();
				retString = e.getMessage();
				return retString;
			}
			finally
			{
				try
				{
					stmt.close();
					stmt = null;
					rs.close();
				}
				catch(Exception e)
				{
					System.out.println("Exception [06][confirmReceivables]" + e.getMessage());
					e.printStackTrace();
					retString = e.getMessage();
					System.out.println("Exception In Closing Statement and ResultSet" + retString);
					//return retString; //Gulzar 04-12-06
				}
			}
			if(confirmed == null)
			{
				confirmed = "";
			}
			if(confirmed.trim().equalsIgnoreCase("Y"))
			{
				retString =	itmDBAccessEJB.getErrorString("","VTWORCONF","","",conn);
			}		
			else
			{
				try
				{
					sqlDtl = "SELECT TRAN_SER, REF_NO, REF_DATE, TRAN_DATE, CUST_CODE, ACCT_CODE, CCTR_CODE, CURR_CODE, EXCH_RATE, TOT_AMT, ADJ_AMT, CR_TERM, DUE_DATE, REAS_CODE, ITEM_SER, SALES_PERS, SALES_PERS__1, SALES_PERS__2, CONTACT_CODE, FIN_SCHEME, SALE_ORDER, DIFF_AMT FROM RECEIVABLES_DTL WHERE TRAN_ID ='"+tranID+"'";	 
					stmt = conn.createStatement();
					
					rsDtl = stmt.executeQuery(sqlDtl);
					
	
					while (rsDtl.next())
					{
						totAmtOld = 0d;
						totAmt = 0d;
	
						tranSer		= rsDtl.getString("TRAN_SER");
						refNo 		= rsDtl.getString("REF_NO");
						refDate		= rsDtl.getTimestamp("REF_DATE");
						tranDate 	= rsDtl.getTimestamp("TRAN_DATE");
						custCode 	= rsDtl.getString("CUST_CODE");
						acctCode 	= rsDtl.getString("ACCT_CODE");
						cctrCode 	= rsDtl.getString("CCTR_CODE");
						currCode 	= rsDtl.getString("CURR_CODE");
						exchRate 	= rsDtl.getDouble("EXCH_RATE");
						totAmt 		= rsDtl.getDouble("TOT_AMT");
						adjAmt 		= rsDtl.getDouble("ADJ_AMT");
						crTerm 		= rsDtl.getString("CR_TERM");
						dueDate 	= rsDtl.getTimestamp("DUE_DATE");
						reasCode 	= rsDtl.getString("REAS_CODE");
						itemSer 	= rsDtl.getString("ITEM_SER");
						salesPers 	= rsDtl.getString("SALES_PERS");
						salesPers1 	= rsDtl.getString("SALES_PERS__1");
						salesPers2 	= rsDtl.getString("SALES_PERS__2");
						contactCode = rsDtl.getString("CONTACT_CODE");
						finScheme 	= rsDtl.getString("FIN_SCHEME");             
						saleOrder 	= rsDtl.getString("SALE_ORDER");
						diffAmt 	= rsDtl.getDouble("DIFF_AMT");
	
						if(crTerm == null)
						{
							crTerm = "";
						}
						try
						{
							sql = "SELECT COUNT(1) AS COUNT  FROM RECEIVABLES "
								+ "WHERE TRAN_SER = '"+ tranSer +"' AND REF_NO = '"+ refNo +"' AND SITE_CODE = '"+ siteCode +"'";
							stmt1 = conn.createStatement();
							rsMst = stmt1.executeQuery(sql);
							if (rsMst.next())
							{
								cnt = rsMst.getInt("COUNT");
							}
						}
						catch(SQLException se)
						{
							System.out.println("SQLException [07][confirmReceivables][Excuting Query Failed]" + sql + se.getMessage());
							se.printStackTrace();
							retString = se.getMessage();
							return retString;
						}
						catch(Exception e)
						{
							System.out.println("Exception [08][confirmReceivables]" + e.getMessage());
							e.printStackTrace();
							retString = e.getMessage();
							return retString;
						}
						finally
						{
							try
							{
								rsMst.close();
								rsMst = null;
								stmt1.close();
								stmt1 = null;
							}
							catch(Exception e)
							{
								System.out.println("Exception [09][confirmReceivables]" + e.getMessage());
								e.printStackTrace();
								retString = e.getMessage();						
							}
						}
						System.out.println("CNT : " + cnt);
						totAmtOld = 0;//added by nisar on 06/02/08
						//Added by Gulzar 01-12-06
						sql = "SELECT TOT_AMT FROM RECEIVABLES "
								+ "WHERE TRAN_SER = '"+ tranSer +"' AND REF_NO = '"+ refNo +"' AND SITE_CODE = '"+ siteCode +"'";
						
						stmt2 = conn.createStatement();
						rs = stmt2.executeQuery(sql);
						
						if (rs.next())
						{
							totAmtOld = rs.getDouble("TOT_AMT");
						}
						rs.close();
						diffAmt = totAmt - totAmtOld;
						System.out.println("diffAmt = totAmt - totAmtOld : " + diffAmt);
						if (totAmtOld < 0)
						{
							crAmtOld = totAmtOld *(-1);
							drAmtOld = 0d;
						}
						else
						{
							crAmtOld = 0d;
							drAmtOld = totAmtOld;
						}
						if (totAmt < 0)
						{
							crAmtNew = totAmt *(-1);
							drAmtNew = 0d;
						}
						else
						{
							crAmtNew = 0d;
							drAmtNew = totAmt;
						}
						stmt2.close();
						drAmt = drAmtNew - drAmtOld;
						crAmt = crAmtNew - crAmtOld;
						System.out.println("drAmt = drAmtNew - drAmtOld : " + drAmt);
						System.out.println("crAmt = crAmtNew - crAmtOld : " + crAmt);
						//End Added Gulzar 01-12-06
						if (cnt == 0)
						{
							try
							{
								//////Jiten 27/07/06 new Tran id generated for receivables/////////
								String keyString = "",recvTranID = "";
								//sql = "SELECT KEY_STRING FROM TRANSETUP WHERE TRAN_WINDOW = 'T_RCVABLES'"; //Gulzar 29/08/06
								sql = "SELECT KEY_STRING FROM TRANSETUP WHERE TRAN_WINDOW = 't_rcvables'"; //Gulzar 29/08/06
								
								stmt3 = conn.createStatement();
								rs = stmt3.executeQuery(sql);
								if(rs.next())
								{
									keyString = rs.getString("KEY_STRING");
								}
								rs.close();
								String XMLString = "<?xml version=\"1.0\"?>\r\n<Root>\r\n<header>"+
								"\r\n</header><Detail1><item_ser>"+itemSer+"</item_ser>"+
								"\r\n<site_code>"+siteCode+"</site_code>\r\n<tran_date>"+tranDate+"</tran_date></Detail1></Root>";
								
								CommonConstants.setIBASEHOME();
								TransIDGenerator tg = new TransIDGenerator(XMLString, userId, CommonConstants.DB_NAME); //FOR DB2
							
								recvTranID = tg.generateTranSeqID("RCVL", "tran_id", keyString, conn);
	
								System.out.println("Transaction Id Generated for Receivables :"+recvTranID);
								////////End////////
								sqlIns = "INSERT INTO RECEIVABLES (" +
										 "TRAN_ID, TRAN_SER, TRAN_DATE, REF_NO, REF_DATE, CURR_CODE, EXCH_RATE, CUST_CODE, " + 
										 "ACCT_CODE, CCTR_CODE, DUE_DATE, FIN_ENTITY, SITE_CODE, TOT_AMT, ADJ_AMT, STATUS, " + 
										 "STAT_DATE, DISCOUNT, TAX_AMT, AUTO_RCP, BANK_CODE, RECD, REF_TYPE, CR_TERM, SALES_PERS," +
										 "ITEM_SER, ALLOC_AMT, REMINDER_CNT, REAS_CODE, REAS_DETAIL, SALES_PERS__1, SALES_PERS__2,"+
										 "RECOVER_AMT, BILL_DISC, CONTACT_CODE, DISPUTE_AMT, BAD_DEBT_AMT, EFF_DATE, FIN_SCHEME," +
										 "REF_SER__ORG, SALE_ORDER, LINE_NO__REF) " +
										 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
								
								System.out.println("Insert Sql :"+sqlIns);
								pstmt = conn.prepareStatement(sqlIns);
	
								//pstmt.setString(1,tranID);
								pstmt.setString(1,recvTranID); // Jiten  - 27/07/06 
								pstmt.setString(2,tranSer);
								pstmt.setTimestamp(3,tranDate);
								pstmt.setString(4,refNo);
								pstmt.setTimestamp(5,refDate);
								pstmt.setString(6,currCode);
								pstmt.setDouble(7,exchRate);
								pstmt.setString(8,custCode);
								pstmt.setString(9,acctCode);
								pstmt.setString(10,cctrCode);
								pstmt.setTimestamp(11,dueDate);
								pstmt.setString(12,finEntity);
								pstmt.setString(13,siteCode);
								pstmt.setDouble(14,totAmt);
								pstmt.setDouble(15,adjAmt);
								pstmt.setString(16,status);
								pstmt.setTimestamp(17,statDate);
								pstmt.setDouble(18,discount);
								pstmt.setDouble(19,taxAmt);
								pstmt.setString(20,autoRcp);
								pstmt.setString(21,bankCode);
								pstmt.setString(22,recd);
								pstmt.setString(23,refType);
								pstmt.setString(24,crTerm);
								pstmt.setString(25,salesPers);
								pstmt.setString(26,itemSer);
								pstmt.setDouble(27,allocAmt);
								pstmt.setDouble(28,reminderCnt);
								pstmt.setString(29,reasonCode);
								pstmt.setString(30,reasonDtl);
								pstmt.setString(31,salesPers1);
								pstmt.setString(32,salesPers2);
								pstmt.setDouble(33,recoverAmt);
								pstmt.setString(34,billDisc);
								pstmt.setString(35,contactCode);
								pstmt.setDouble(36,disputeAmt);
								pstmt.setDouble(37,badDebtAmt);
								pstmt.setTimestamp(38,effDate);
								pstmt.setString(39,finScheme);
								pstmt.setString(40,refSerOrg);
								pstmt.setString(41,saleOrder);
								pstmt.setString(42,"1");
								pstmt.executeUpdate();
								System.out.println("Insert Query Executed : FOR 000000 IN RECEIVABLES MASTER");
							}
							catch(SQLException se)
							{
								System.out.println("SQLException [10][confirmReceivables][Excuting Query Failed]" + sqlIns + se.getMessage());
								try
								{
									conn.rollback();	
								}
								catch(Exception e)
								{
									System.out.println("Exception In Rollback Transaction");
								}
								se.printStackTrace();
								retString = se.getMessage();
								return retString;
							}
							catch(Exception e)
							{
								System.out.println("Exception [11][confirmReceivables][Excuting Query Failed]" + sqlIns + e.getMessage());
								e.printStackTrace();
								retString = e.getMessage();
								return retString;
							}
							finally
							{
								try
								{
									pstmt.close();
									pstmt = null;
									stmt3.close();
									
								}
								catch(Exception e)
								{
									System.out.println("Exception [12][confirmReceivables]" + sqlIns + e.getMessage());
									e.printStackTrace();
									retString = e.getMessage();	
									//return retString;	//Gulzar 04-12-06						
								}
							}
						}
						else
						{
							if (totAmt < adjAmt && adjAmt != 0)
							{
								System.out.println("TotAmt < AdjAmt");
								retString =	itmDBAccessEJB.getErrorString("","VTCREDIT01","","",conn);
							}
							else
							{
								try
								{		
									System.out.println("Diff Amt :" + diffAmt);															  
									sql = "UPDATE RECEIVABLES SET TRAN_DATE = ?, REF_DATE = ?, CURR_CODE = ?, " +
										  "EXCH_RATE = ?, CUST_CODE = ?, ACCT_CODE = ?, CCTR_CODE = ?, DUE_DATE = ?, FIN_ENTITY = ?, " +
										  "TOT_AMT = TOT_AMT + ( ? ), ADJ_AMT = ?, STATUS = ?, STAT_DATE = ?, DISCOUNT = ?, TAX_AMT = ?, AUTO_RCP = ?, BANK_CODE = ?, " + 
										  "RECD = ?, REF_TYPE = ?, CR_TERM = ?, SALES_PERS = ?, ITEM_SER = ?, ALLOC_AMT = ?, REMINDER_CNT = ?," +
										  "REAS_CODE = ?, REAS_DETAIL = ?, SALES_PERS__1 = ?, SALES_PERS__2 = ?, RECOVER_AMT = ?, BILL_DISC = ?, CONTACT_CODE = ?, " +
										  "DISPUTE_AMT = ?, BAD_DEBT_AMT = ?, EFF_DATE = ?, FIN_SCHEME = ?, REF_SER__ORG = ?, SALE_ORDER = ? " +
										  "WHERE TRAN_SER = '"+ tranSer +"' AND REF_NO = '"+ refNo +"' AND SITE_CODE = '"+ siteCode +"'";
									
									System.out.println("Update Sql :"+sql);
									pstmt = conn.prepareStatement(sql);
				
									pstmt.setTimestamp(1,tranDate);
									pstmt.setTimestamp(2,refDate);
									pstmt.setString(3,currCode);
									pstmt.setDouble(4,exchRate);
									pstmt.setString(5,custCode);
									pstmt.setString(6,acctCode);
									pstmt.setString(7,cctrCode);
									pstmt.setTimestamp(8,dueDate);
									pstmt.setString(9,finEntity);
									pstmt.setDouble(10,diffAmt);
									pstmt.setDouble(11,adjAmt);
									pstmt.setString(12,status);
									pstmt.setTimestamp(13,statDate);
									pstmt.setDouble(14,discount);
									pstmt.setDouble(15,taxAmt);
									pstmt.setString(16,autoRcp);
									pstmt.setString(17,bankCode);
									pstmt.setString(18,recd);
									pstmt.setString(19,refType);
									pstmt.setString(20,crTerm);
									pstmt.setString(21,salesPers);
									pstmt.setString(22,itemSer);
									pstmt.setDouble(23,allocAmt);
									pstmt.setDouble(24,reminderCnt);
									pstmt.setString(25,reasonCode);
									pstmt.setString(26,reasonDtl);
									pstmt.setString(27,salesPers1);
									pstmt.setString(28,salesPers2);
									pstmt.setDouble(29,recoverAmt);
									pstmt.setString(30,billDisc);
									pstmt.setString(31,contactCode);
									pstmt.setDouble(32,disputeAmt);
									pstmt.setDouble(33,badDebtAmt);
									pstmt.setTimestamp(34,effDate);
									pstmt.setString(35,finScheme);
									pstmt.setString(36,refSerOrg);
									pstmt.setString(37,saleOrder);
									pstmt.executeUpdate();
									System.out.println("Upadte Query Executed : 000000 IN RECEIVABLES MASTER");
									pstmt.close();
								}
								catch(SQLException se)
								{
									System.out.println("SQLException [13][confirmReceivables][Excuting Query Fail]" + sql+ se.getMessage());
									try
									{
										conn.rollback();	
									}
									catch(Exception e)
									{
										System.out.println("Exception In Rollback Transaction");
									}
									se.printStackTrace();
									retString = se.getMessage();
									return retString;
								}
								catch(Exception e)
								{
									System.out.println("Exception [14][confirmReceivables]" + e.getMessage());
									e.printStackTrace();
									try
									{
										conn.rollback();	
									}
									catch(Exception e1)
									{
										e = e1;
									}
									retString = e.getMessage();
								}	
								finally
								{
									try
									{
										if (pstmt != null)
										{
											pstmt.close();
											pstmt = null;
										}
										/*if(conn != null)//Jiten 27/07/06 -  not required
										{
											conn.commit(); 
										} */
									}
									catch(Exception e)
									{
										System.out.println("Exception [15][confirmReceivables]" + e.getMessage());
										e.printStackTrace();
										retString = e.getMessage();
										//return retString; //Gulzar 04-12-06
									}
								}					
							} // else of  if(totAmt < adjAmt)
						} //else of if(cnt == 0)
						HashMap sundrybalMap = new HashMap();
						/*Commented and Changes below - Gulzar 24/11/06
						String sundryCode = " ";
						String sundryType = " ";
						*/
						//Added - Gulzar 24/11/06
						String sundryCode = custCode;
						String sundryType = "C";
						//End Addition Gulzar 24/11/06
						double advAmt = 0;
						
						sundrybalMap.put("eff_date",tranDate);
						sundrybalMap.put("tran_date",tranDate);
						sundrybalMap.put("fin_entity",finEntity);
						sundrybalMap.put("site_code",siteCode);
						sundrybalMap.put("sundry_type",sundryType);
						sundrybalMap.put("sundry_code",sundryCode);
						sundrybalMap.put("acct_code",acctCode);
						sundrybalMap.put("cctr_code",cctrCode);
						sundrybalMap.put("curr_code",currCode);
						sundrybalMap.put("contact_code",contactCode);
						sundrybalMap.put("exch_rate",new Double(exchRate));
						sundrybalMap.put("acct_prd",acctPrd); //Gulzar 30/11/06
						sundrybalMap.put("user_id", userId);//Gulzar 30/11/06
						
						/* -- Commented By Gulzar 01-12-06
						if (diffAmt>=0)
						{
							drAmt = diffAmt;
						}
						else
						{
							crAmt = diffAmt;
						}
						*///EndComment Gulzar 01-12-06
	
						sundrybalMap.put("dr_amt",new Double(drAmt));
						sundrybalMap.put("cr_amt",new Double(crAmt));
						sundrybalMap.put("adv_amt",new Double(advAmt));
						
						System.out.println("sundrybalMap : " + sundrybalMap);
						
						FinCommon finCommon = new FinCommon();
						//retString = finCommon.gbf_sundrybal_upd(sundrybalMap,conn);//Gulzar 30/11/06
						retString = finCommon.sundrybalOpnUpd(sundrybalMap,conn); //Gulzar 30/11/06
	
						sundrybalMap.clear();//Gulzar 02-12-06
						System.out.println("Error Code........"+ retString);
						if(retString.trim().length()>0)
						{
							retString = itmDBAccessEJB.getErrorString("", retString, userId);
							break;
						}
						
						HashMap acctbalMap = new HashMap();
						
						System.out.println(" ContactCode : " + contactCode + " TranDate : " + tranDate + " FinEntity : " + finEntity + " siteCode : " + siteCode + " AcctCode : " + acctCode + " CctrCode : " + cctrCode + " CurrCode : " + currCode + " EmpCode : " + empCode + " ExchRate : " + exchRate + " DrAmt : " + drAmt + " CrAmt : " + crAmt);
						
						acctbalMap.put("contact_code", contactCode);
						acctbalMap.put("tran_date", tranDate);
						acctbalMap.put("eff_date", tranDate);
						acctbalMap.put("fin_entity", finEntity);
						acctbalMap.put("site_code", siteCode);
						acctbalMap.put("acct_code", acctCode);  
						acctbalMap.put("cctr_code", cctrCode);
						acctbalMap.put("curr_code", currCode);
						acctbalMap.put("emp_code", empCode);
						acctbalMap.put("user_id", userId);
						acctbalMap.put("exch_rate", new Double(exchRate));
						acctbalMap.put("dr_amt", new Double(drAmt));
						acctbalMap.put("cr_amt", new Double(crAmt));
						acctbalMap.put("acct_prd",acctPrd); //Gulzar 30/11/06
						
						System.out.println("acctbalMap : " + acctbalMap);
						
						//retString = finCommon.gbf_acctbal_upd(acctbalMap,conn);//Gulzar 30/11/06
						retString = finCommon.acctbalOpnUpd(acctbalMap,conn);//Gulzar 30/11/06
						acctbalMap.clear();
						System.out.println("Error Code........"+retString);
						if(retString.trim().length()>0)
						{
							retString = itmDBAccessEJB.getErrorString("", retString, userId);
							break;
						}
						
						//conn.commit(); //Jiten 27/07/06 - not required
					}
					try
					{
						String sqlUpdateHdr = "UPDATE RECEIVABLES_HDR SET CONFIRMED = 'Y', CONF_DATE = ? ,EMP_CODE__APRV = ? WHERE TRAN_ID = ?";
						System.out.println("Update Sql :"+sqlUpdateHdr);
						pstmt = conn.prepareStatement(sqlUpdateHdr);
						pstmt.setTimestamp(1,new java.sql.Timestamp(System.currentTimeMillis()));
						pstmt.setString(2,empCode);
						pstmt.setString(3,tranID);
						int conf = pstmt.executeUpdate();	
						if(conf == 1)
						{
							System.out.println("RECEIVABLES_HDR Updated...(Confirmed)" + userId);
							conn.commit();						
							retString = itmDBAccessEJB.getErrorString("","VTMCONF2",userId);
							System.out.println("Ret String: " + retString);
						}
						else
						{
							System.out.println("RECEIVABLES_HDR Not Updated...(Not Confirmed)");
						}
					}
					catch(SQLException se)
					{
						System.out.println("SQLException[16] :[ReceivablesOpeningConfEJB][Excuting Query Failed]" + sql + se.getMessage());
						se.printStackTrace();
						conn.rollback();//Added by Jiten 27/07/06	
						retString = se.getMessage();
						return retString;
					}
					catch(Exception e)
					{
						retString = "Not Confirmed";
						conn.rollback();//Added by Jiten 27/07/06	
						System.out.println("Exception[17] ::"+e.getMessage());
						e.printStackTrace();
						retString = "Not Confirmed :" + e.getMessage();
					}
					finally
					{
						try
						{
							if(pstmt != null)
							{
								pstmt.close();
								pstmt = null;
							}
							if(conn != null)
							{
								System.out.println("Closing Connection......");
								//conn.close();
								conn = null;
							}
						}
						catch(SQLException se)
						{
							System.out.println("SQLException [18] Connection Error Closed :" + se.getMessage());
							se.printStackTrace();
							retString = "Connection Not Closed :" + se.getMessage();
							return retString;
						}
					}//finally
				} // try
				catch(Exception e)
				{
					System.out.println("Exception [19][confirmReceivables]:" + e.getMessage());
					e.printStackTrace();
					try
					{
						conn.rollback(); //Added by Jiten 27/07/06
					}
					catch(Exception e1){}
					retString = e.getMessage();
					return retString;
				}
				finally
				{
					try
					{
						if (stmt != null)
						{
							stmt.close();
							stmt = null;
						}
						if (rsDtl != null)
						{
							rsDtl.close();
							rsDtl = null;
						}
						if (conn != null)//Added by Jiten 27/07/06
						{
							conn.close();
							conn = null;
						}
					}
					catch(Exception e)
					{
						System.out.println("Exception [20][confirmReceivables]" + sqlHdr + e.getMessage());
						e.printStackTrace();
						retString = e.getMessage();
						return retString;
					}
				}
			} // else
		}
		
		catch(Exception e)
		{
			System.out.println("Exception [20][confirmReceivables]" + sqlHdr + e.getMessage());
			e.printStackTrace();
			retString = e.getMessage();
			return retString;
		}
		finally
		{
			try
			{
				if (stmt1 != null)
				{
					stmt1.close();
					stmt1 = null;
				}
				if (stmt2 != null)
				{
					stmt2.close();
					stmt2 = null;
				}
				if (stmt3 != null)
				{
					stmt3.close();
					stmt3 = null;
				}
				if (stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if (rsDtl != null)
				{
					rsDtl.close();
					rsDtl = null;
				}
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (rsMst != null)
				{
					rsMst.close();
					rsMst = null;
				}
				if (conn != null)//Added by Jiten 27/07/06
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception [20][confirmReceivables]" + sqlHdr + e.getMessage());
				e.printStackTrace();
				retString = e.getMessage();
				return retString;
			}
		}
		System.out.println("Ret String1: " + retString);
		return retString;
	}
}