package ibase.webitm.ejb.wms;

import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import org.w3c.dom.*;

import javax.ejb.*;
import javax.naming.InitialContext;

import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.dis.*;

import javax.ejb.Stateless; // added for ejb3


//public class DespatchPosEJB extends ValidatorEJB implements SessionBean
@Stateless // added for ejb3
public class PickOrderPos extends ValidatorEJB implements PickOrderPosLocal, PickOrderPosRemote
{
	
	public String postSaveRec()throws RemoteException,ITMException
	{
		return "";
	}

    public String postSaveRec(String xmlString1,String domId,String objContext,String editFlag, String xtraParams, Connection conn)throws RemoteException,ITMException
	{
		Document dom = null;
		String retString = "";
		System.out.println("Pick Order called*********************************************");
		try
		{
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom = GenericUtility.getInstance().parseString(xmlString1);
				executepostSaveRec(dom,domId,objContext,editFlag,xtraParams,conn);
			}			
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :ReplenishmentPos ::" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		return "";
	}
		
	private String executepostSaveRec(Document dom, String domID,String objContext,String editFlag, String xtraParams, Connection conn)throws RemoteException,ITMException
	{
		Statement stmt = null, stmt1 = null;
		ResultSet rs = null, rs1 = null, rs2 = null, rs3 = null, rs4 = null, rs5 = null;
		PreparedStatement pstmt = null;
		String siteCode = "", itemCode = "", lineNo = "",locCode = "", lotNo = "", lotSl = "";
		String expLev = "", itemCodeOrd = "", unit = "", sordNo = "", custCode  = "", locGroup = "";
		String grade = "",siteCodeMfg = "", lineNoSord = "", unitStd = "", sql = "";
		HashMap strAllocate = new HashMap();
		String sqlUpdate = "", sqlInsert = "";
		String sqlRepl = "";
		String replOrd = "";
		String lineNorepl = "";
		String sqlPick = "";
		String sqlReplUpt = "";
		String invStat = "";
		String casePickLoc = "";
		String sqlReplCase = "";
		String resrvLoc = "";
		double pickQty = 0.0;
		double rate = 0d, qtyAlloc = 0d, convQtyStdUom = 0d, qtyStduom = 0d;
		int updateCnt = 0, retVal = 0;
		java.sql.Date expDate = null,mfgDate = null, currDate = null, tranDate = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		DistStkUpdLocal distStkUpd = null;
		NodeList hdrDom	= null,detailDom = null,currDetailList = null;
		Node currDetail = null;
		
		DistCommon discommon = new DistCommon();
		
		int detailListLength = 0,currDetailListLength = 0;
		String qtyStduomStr = "",updateStatus = "",nodeName = "",tranId = "";
		Connection conn1 = null;
		try
		{
			stmt = conn.createStatement();
			stmt1 = conn.createStatement();
			distStkUpd = getComponent("DistStkUpd");
			casePickLoc  = discommon.getDisparams("999999","CASE_PICK_INVSTAT",conn);
			resrvLoc  = discommon.getDisparams("999999","RESERV_LOCATION",conn);

			currDate = new java.sql.Date(System.currentTimeMillis());
			tranDate = new java.sql.Date(System.currentTimeMillis());
			System.out.println("\n CurrDate :"+currDate+"\n TranDate :"+tranDate);
						
			hdrDom = dom.getElementsByTagName("Detail1");
			siteCode = GenericUtility.getInstance().getColumnValueFromNode("site_code",hdrDom.item(0));
			//tranId = GenericUtility.getInstance().getColumnValueFromNode("desp_id",hdrDom.item(0));
			tranId = GenericUtility.getInstance().getColumnValueFromNode("pick_order",hdrDom.item(0));
			
			System.out.println("Site Code :: "+siteCode);
			System.out.println("Tran ID ::::::::::: "+tranId);
			
			currDetail  = getCurrentDetailFromDom(dom,domID);
			updateStatus = getCurrentUpdateFlag(currDetail);
			System.out.println("DomID :: "+domID+"\n Update Status ::"+updateStatus);
			
			if (currDetail != null && !updateStatus.equalsIgnoreCase("D"))
			{
				itemCode = GenericUtility.getInstance().getColumnValueFromNode("item_code",currDetail);
				lineNo = GenericUtility.getInstance().getColumnValueFromNode("line_no",currDetail);
				locCode = GenericUtility.getInstance().getColumnValueFromNode("loc_code",currDetail);
				lotNo = GenericUtility.getInstance().getColumnValueFromNode("lot_no",currDetail);
				lotSl = GenericUtility.getInstance().getColumnValueFromNode("lot_sl",currDetail);
				//qtyStduomStr = GenericUtility.getInstance().getColumnValueFromNode("quantity__stduom",currDetail);
				qtyStduomStr = GenericUtility.getInstance().getColumnValueFromNode("quantity",currDetail);
				expLev = GenericUtility.getInstance().getColumnValueFromNode("exp_lev",currDetail);
				//itemCodeOrd = GenericUtility.getInstance().getColumnValueFromNode("item_code__ord",currDetail);
				itemCodeOrd =itemCode;// GenericUtility.getInstance().getColumnValueFromNode("item_code__ord",currDetail);
				unit = GenericUtility.getInstance().getColumnValueFromNode("unit",currDetail);
				custCode = GenericUtility.getInstance().getColumnValueFromNode("cust_code",currDetail);
				//sordNo = GenericUtility.getInstance().getColumnValueFromNode("sord_no",currDetail);
				sordNo = GenericUtility.getInstance().getColumnValueFromNode("sale_order",currDetail);
				lineNoSord = GenericUtility.getInstance().getColumnValueFromNode("line_no__sord",currDetail);
				lineNoSord = "   " + lineNoSord; 
				lineNoSord = lineNoSord.substring(lineNoSord.length()-3);
				qtyStduom = Double.parseDouble(qtyStduomStr);
				
				
				/*sqlPick = "SELECT REPL_ORDER,LINE_NO__REPL,QUANTITY  " 
						 +"FROM PICK_ORD_DET WHERE PICK_ORDER = ? AND LINE_NO = ?";
			
				pstmt = conn.prepareStatement(sqlPick);
				
				pstmt.setString(1,tranId);
				pstmt.setString(2,lineNo);
				
				rs = pstmt.executeQuery();
				
				
				if (rs.next())
				{
					replOrd = rs.getString("REPL_ORDER");
					lineNorepl = rs.getString("LINE_NO__REPL");
					pickQty = rs.getDouble("QUANTITY");
				}
				
				if(pstmt != null)
				{	
					pstmt.close();
					pstmt = null;
				}
				if(rs != null)
				{	
					rs.close();
					rs = null;
				}
				//sqlRepl = "SELECT LOC_CODE FROM REPL_ORD_DET WHERE REPL_ORDER = ? AND LINE_NO = ?";
				sqlRepl = "SELECT R.LOC_CODE LOC_CODE,L.INV_STAT INV_STAT FROM REPL_ORD_DET R,LOCATION L "
						 +" WHERE R.LOC_CODE = L.LOC_CODE AND REPL_ORDER = ? AND LINE_NO = ?";
				
				pstmt = conn.prepareStatement(sqlRepl);
				
				pstmt.setString(1,replOrd);
				pstmt.setString(2,lineNorepl);
			
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					locCode = rs.getString("LOC_CODE");
					invStat = rs.getString("INV_STAT");
				}
				pstmt.close();
				pstmt = null;
				
				rs.close();
				rs = null;
				
				System.out.println("Enter in active Repalinshement^^^^^^^^^^^^^66");
				sqlReplCase = "SELECT R.LOC_CODE LOC_CODE,L.INV_STAT INV_STAT FROM REPL_ORD_DET R,LOCATION L" 
						 +" WHERE R.LOC_CODE = L.LOC_CODE AND  R.ITEM_CODE = ? AND R.SITE_CODE = ?"
					      +" AND R.LOT_NO = ? AND R.LOT_SL = ? AND R.LOC_CODE__TO = ?";
				pstmt = conn.prepareStatement(sqlReplCase);
				
				if(casePickLoc.equalsIgnoreCase(invStat))
				{
					
				
					
					pstmt.setString(1,itemCode);
					pstmt.setString(2,siteCode );
					pstmt.setString(3,lotNo );
					pstmt.setString(4,lotSl );
					pstmt.setString(5,locCode );
					
				
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						locCode = rs.getString("LOC_CODE");
						invStat = rs.getString("INV_STAT");
						System.out.println("Enter in active Repalinshement LOCCDOE"+locCode);
						
					}
					if(rs != null)
					{	
						rs.close();
						rs = null;
					}
						
					
				}
				if(invStat.startsWith("PND"))
				{
					
					System.out.println("Enter in active Repalinshement CASE PICK DSO");
					
					pstmt.setString(1,itemCode);
					pstmt.setString(2,siteCode );
					pstmt.setString(3,lotNo );
					pstmt.setString(4,lotSl );
					pstmt.setString(5,locCode );
					
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						locCode = rs.getString("LOC_CODE");
						System.out.println("Enter in active Repalinshement LOCCDOE IN PSO"+locCode);
						
					}
					if(pstmt != null)
					{	
						pstmt.close();
						pstmt = null;
					}
					if(rs != null)
					{	
						rs.close();
						rs = null;
					}	
				}
				if(pstmt != null)
				{	
					pstmt.close();
					pstmt = null;
				}*/
				/*
				sqlReplUpt = "UPDATE REPL_ORD_DET SET ACTUAL_QTY = ACTUAL_QTY + ?" 
						 	+" WHERE REPL_ORDER = ? AND LINE_NO = ?";
			
				pstmt = conn.prepareStatement(sqlReplUpt);
				
				System.out.println("pickQty"+pickQty+"qtyStduom"+qtyStduom);
				pstmt.setDouble(1,(pickQty - qtyStduom));
				pstmt.setString(2,replOrd);
				pstmt.setString(3,lineNorepl);
				
				int cnt = pstmt.executeUpdate();
				
				
				if (cnt > 0)
				{
					System.out.println("update actual qty succesfully");
				}
				if(pstmt != null)
				{	
					pstmt.close();
					pstmt = null;
				}
				*/	
				/*System.out.println("pick loc code%%%%%%%%%%%%%%%%%%%%%%%5"+locCode);
				System.out.println("pick QTYe%%%%%%%%%%%%%%%%%%%%%%%5"+pickQty);*/
				//Chnged by Rohna on to change loc Code
				//Changed as Rate updation is required.start
				/*sql = "SELECT CASE WHEN RATE IS NULL THEN 0 ELSE RATE END "
						  +"FROM STOCK WHERE ITEM_CODE = '"+itemCode+"' "
						  +"AND SITE_CODE = '"+siteCode+"' "
						  +"AND LOC_CODE = '"+locCode+"' "
						  +"AND LOT_NO = '"+lotNo+"' "
						  +"AND LOT_SL = '"+lotSl+"'";
				System.out.println("sql :"+sql);
				rs1 = stmt1.executeQuery(sql);
				if (rs1.next())
				{
					rate = rs1.getDouble(1);
					System.out.println("rate :"+rate);
				}
				stmt1.close();
				 -- Commented and Changes by gulzar - 12/11/07
				sql = "UPDATE DESPATCHDET SET COST_RATE = ? "
							+"WHERE DESP_ID = ? "
							+"AND ITEM_CODE = ? "
							+"AND LINE_NO   = ? "
							+"AND LOC_CODE  = ? "
							+"AND LOT_NO    = ? "
							+"AND LOT_SL	= ? ";
				System.out.println("update sql :"+sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setDouble(1,rate);
				pstmt.setString(2,tranId);
				pstmt.setString(3,itemCode);
				pstmt.setString(4,lineNo);
				pstmt.setString(5,locCode);
				pstmt.setString(6,lotNo);
				pstmt.setString(7,lotSl);
				updateCnt = pstmt.executeUpdate();
				System.out.println("updateCnt in despatchDet :"+updateCnt);
				pstmt.clearParameters();
				//End Commented Gulzar - 12/11/07
				//Added By Gulzar - 12/11/07
				sql = "UPDATE DESPATCHDET SET COST_RATE = ? "
							+"WHERE DESP_ID = '"+tranId+"' "
							+"AND ITEM_CODE = '"+itemCode+"' "
							+"AND LINE_NO   = '"+lineNo+"' "
							+"AND LOC_CODE  = '"+locCode+"' "
							+"AND LOT_NO    = '"+lotNo+"' "
							+"AND LOT_SL	= '"+lotSl+"' ";
				System.out.println("update sql is :"+sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setDouble(1,rate);
				updateCnt = pstmt.executeUpdate();
				pstmt.close(); pstmt = null;
				System.out.println("updateCnt in despatchDet :"+updateCnt);*/
				//End Added - Gulzar - 12/11/07
				//Changed as Rate updation is required.end
				sql = "SELECT QTY_ALLOC FROM SORDALLOC WHERE SALE_ORDER = '"+sordNo+"' "
						+"AND LINE_NO =	'"+lineNoSord+"' "
						+"AND EXP_LEV =	'"+expLev+"' "
						+"AND ITEM_CODE__ORD = '"+itemCodeOrd+"' "
						+"AND ITEM_CODE	= '"+itemCode+"' "
						+"AND LOC_CODE = '"+locCode+"' "
						+"AND LOT_NO = '"+lotNo+"' "
						+"AND LOT_SL = '"+lotSl+"' ";
						//+"AND ALLOC_MODE = 'M' ";//Manual allocation.	//Commented Jiten 10/10/06, It should check for both 'A' and 'M'. 
				System.out.println("Select sql :"+sql);
				stmt1 = conn.createStatement();
				rs2 = stmt1.executeQuery(sql);
				/*
				if (rs2.next())
				{
					qtyAlloc = rs2.getDouble(1);
					System.out.println("qtyAlloc :"+qtyAlloc);
				}
				*/
				if (!rs2.next())
				{
					sql = "SELECT CUST_CODE,LOC_GROUP FROM SORDER WHERE SALE_ORDER ='"+sordNo+"' ";
					System.out.println("sql  :"+sql);
					rs3 = stmt1.executeQuery(sql);
					if (rs3.next())
					{
						custCode = rs3.getString(1);
						System.out.println("custCode :"+custCode);
						locGroup = rs3.getString(2);
						System.out.println("locGroup :"+locGroup);
					}
					if (locGroup == null || locGroup.trim().length() == 0)
					{
						sql = "SELECT A.EXP_DATE, A.GRADE, A.MFG_DATE, A.SITE_CODE__MFG "
									+"FROM STOCK A WHERE A.ITEM_CODE = '"+itemCode+"' "
									+"AND A.SITE_CODE = '"+siteCode+"' "
									+"AND A.LOC_CODE = '"+locCode+"' "
									+"AND A.LOT_NO = '"+lotNo+"' "
									+"AND A.LOT_SL = '"+lotSl+"'" ;
						System.out.println("sql :"+sql);				
						rs4 = stmt1.executeQuery(sql);
						if (rs4.next())
						{
							expDate = rs4.getDate(1);
							System.out.println("expDate :"+expDate);
							grade = rs4.getString(2);
							System.out.println("grade :"+grade);
							mfgDate = rs4.getDate(3);
							System.out.println("mfgDate :"+mfgDate);
							siteCodeMfg = rs4.getString(4);
							System.out.println("siteCodeMfg :"+siteCodeMfg);
						}
					}
					else
					{
						sql = "SELECT A.EXP_DATE, A.GRADE, A.MFG_DATE, A.SITE_CODE__MFG "	
									+"FROM STOCK A, LOCATION B "
									+"WHERE A.LOC_CODE = B.LOC_CODE "
									+"AND B.LOC_GROUP = '"+locGroup+"' "
									+"AND A.ITEM_CODE = '"+itemCode+"' "
									+"AND A.SITE_CODE = '"+siteCode+"' "
									+"AND A.LOC_CODE = '"+locCode+"' "
									+"AND A.LOT_NO = '"+lotNo+"' "
									+"AND A.LOT_SL = '"+lotSl+"'";
						System.out.println("sql :"+sql);
						rs4 = stmt1.executeQuery(sql);
						if (rs4.next())
						{
							expDate = rs4.getDate(1);
							System.out.println("expDate :"+expDate);
							grade = rs4.getString(2);
							System.out.println("grade :"+grade);
							mfgDate = rs4.getDate(3);
							System.out.println("mfgDate :"+mfgDate);
							siteCodeMfg = rs4.getString(4);
							System.out.println("siteCodeMfg :"+siteCodeMfg);
						}
					}//end else
					sql = "SELECT UNIT__STD, CONV__QTY_STDUOM FROM SORDDET "
							 +"WHERE SALE_ORDER = '"+sordNo+"' "
							 +"AND LINE_NO = '"+lineNoSord+"'";
					System.out.println("sql :"+sql);
					rs5 = stmt1.executeQuery(sql);
					if (rs5.next())
					{
						unitStd = rs5.getString(1);
						System.out.println("unitStd :"+unitStd);
						convQtyStdUom = rs5.getDouble(2);
						System.out.println("convQtyStdUom :"+convQtyStdUom);
					}
					sqlInsert = "INSERT INTO SORDALLOC (SALE_ORDER, LINE_NO, EXP_LEV, ITEM_CODE__ORD ,ITEM_CODE, "
							   +"LOT_NO,	LOT_SL, LOC_CODE, ITEM_REF,	QUANTITY, UNIT, QTY_ALLOC, DATE_ALLOC, STATUS, "
							   +"ALLOC_MODE,	SITE_CODE ,ITEM_GRADE, EXP_DATE, MFG_DATE, SITE_CODE__MFG, UNIT__STD, "
							   //changed by sankara on 15/10/13 for insert wave_flag start.  
							   /*  +"CONV__QTY_STDUOM, QUANTITY__STDUOM )"
							   +"values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";   */
							   +"CONV__QTY_STDUOM, QUANTITY__STDUOM, WAVE_FLAG )"
							   +"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
					          //changed by sankara on 15/10/13 for insert wave_flag end.  
					System.out.println("Insert sql from ReplanishmentPos :"+sqlInsert);
					pstmt = conn.prepareStatement(sqlInsert);
					pstmt.setString(1,sordNo);
					pstmt.setString(2,lineNoSord);
					pstmt.setString(3,expLev);
					pstmt.setString(4,itemCodeOrd);
					pstmt.setString(5,itemCode);
					pstmt.setString(6,lotNo);
					pstmt.setString(7,lotSl);
					pstmt.setString(8,locCode);
					pstmt.setNull(9,java.sql.Types.VARCHAR);
					pstmt.setDouble(10,qtyStduom);
					pstmt.setString(11,unit);
					pstmt.setDouble(12,qtyStduom);
					pstmt.setDate(13,currDate);
					pstmt.setString(14,"P");
					pstmt.setString(15,"A");
					pstmt.setString(16,siteCode);
					pstmt.setString(17,grade);
					pstmt.setDate(18,expDate);
					pstmt.setDate(19,mfgDate);
					pstmt.setString(20,siteCodeMfg);
					pstmt.setString(21,unitStd);
					pstmt.setDouble(22,convQtyStdUom);
					pstmt.setDouble(23,qtyStduom);
					//changed by sankara on 15/10/13 insert wave_flag
					pstmt.setString(24,"Y");
					updateCnt = pstmt.executeUpdate();
					System.out.println("Inserted the no of records : updateCnt :"+updateCnt);
					pstmt.clearParameters();
														
					/*strAllocate.put("tran_date",tranDate);
					strAllocate.put("ref_ser","S-DSP");
					strAllocate.put("ref_id",tranId);
					strAllocate.put("ref_line",line.substring(line.length()-3));
					strAllocate.put("site_code",siteCode);
					strAllocate.put("item_code",itemCode);
					strAllocate.put("loc_code",locCode);
					strAllocate.put("lot_no",lotNo);
					strAllocate.put("lot_sl",lotSl);
					strAllocate.put("alloc_qty",new Double(qtyStduom));
					strAllocate.put("chg_user",genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode"));
					strAllocate.put("chg_term",genericUtility.getValueFromXTRA_PARAMS(xtraParams,"termId"));
					strAllocate.put("chg_win","W_DESPATCH");
					//Calling DistStkUpdEJB
					System.out.println("Calling DistStkUpdEJB.....");
					if (distStkUpd.updAllocTrace(strAllocate, conn) > 0)
					{
						System.out.println("distStkUpd.UpdAllocTrace(hashMap) : Sucessuful!");
					}
					sqlUpdate = "UPDATE SORDITEM SET QTY_ALLOC = QTY_ALLOC + ? "
							   +"WHERE SALE_ORDER = ? "
							   +"AND LINE_NO = ? "
							   +"AND EXP_LEV = ? ";
					System.out.println("Update sql :"+sqlUpdate);
					pstmt = conn.prepareStatement(sqlUpdate);
					pstmt.setDouble(1,qtyStduom);
					pstmt.setString(2,sordNo);
					pstmt.setString(3,lineNoSord);
					pstmt.setString(4,expLev);
					updateCnt = pstmt.executeUpdate();
					System.out.println("Updated the no of records : updateCnt :"+updateCnt);
					pstmt.clearParameters();*/
				}
				else{
					sqlUpdate = "UPDATE SORDALLOC "+
				            //changed by sankara on 17/10/13 update wave_flag
							//"SET QTY_ALLOC = QTY_ALLOC + ? "+
							"SET QTY_ALLOC = QTY_ALLOC + ?, WAVE_FLAG = 'Y' "+
							"WHERE SALE_ORDER = ? "+
							"AND LINE_NO = ? "+
							"AND EXP_LEV = ? "+
							"AND ITEM_CODE__ORD = ? "+
							"AND ITEM_CODE = ? "+
							"AND LOT_NO = ? "+
							"AND LOT_SL = ? "+
							"AND LOC_CODE = ? ";
					pstmt = conn.prepareStatement(sqlUpdate);
					pstmt.setDouble(1,qtyStduom);
					pstmt.setString(2,sordNo);
					pstmt.setString(3,lineNoSord);
					pstmt.setString(4,expLev);
					pstmt.setString(5,itemCodeOrd);
					pstmt.setString(6,itemCode);
					pstmt.setString(7,lotNo);
					pstmt.setString(8,lotSl);
					pstmt.setString(9,locCode);
					updateCnt = pstmt.executeUpdate();
					pstmt.clearParameters();
					pstmt.close();
					pstmt = null;
				}
				/////Customer wise manual allocation/deallocatuin.
				/////If edit mode then only diff qty is re-allocated.
				String tranIdSOAlloc = "";
				long lineNoSOAlloc = 0;
				double pendingDeallocQty = 0,allocQty = 0,deAllocQty = 0;
				sql = "SELECT B.TRAN_ID, B.LINE_NO, (B.QUANTITY - B.DEALLOC_QTY), B.QUANTITY, B.DEALLOC_QTY "+
					"FROM SORD_ALLOC A, SORD_ALLOC_DET B "+
					"WHERE A.TRAN_ID   	= B.TRAN_ID	"+
					"AND A.CUST_CODE 	= ? "+
					"AND A.SITE_CODE   = ? "+
					"AND A.SALE_ORDER  IS NULL "+
					"AND B.ITEM_CODE 	= ? "+
					"AND B.LOC_CODE	 	= ? "+
					"AND B.LOT_NO		= ? "+
					"AND B.LOT_SL		= ? "+
					"AND B.QUANTITY - B.DEALLOC_QTY > 0 ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,custCode);
				pstmt.setString(2,siteCode);
				pstmt.setString(3,itemCodeOrd);
				pstmt.setString(4,locCode);	
				pstmt.setString(5,lotNo);
				pstmt.setString(6,lotSl);
				rs = pstmt.executeQuery();
				if(rs.next()){
					tranIdSOAlloc = rs.getString(1);
					lineNoSOAlloc = rs.getLong(2);
					pendingDeallocQty = rs.getDouble(3);
					allocQty = rs.getDouble(4);
					deAllocQty = rs.getDouble(5);					
				}
				rs = null;
				////////
				conn1 = getConnection();
				Statement stmt2 = conn1.createStatement();
				double oldQty = 0,qtyUpdate = 0;
				if(tranIdSOAlloc != null && tranIdSOAlloc.trim().length() > 0){
					sql = "SELECT QUANTITY FROM PICK_ORD_DET WHERE PICK_ORDER = '"+tranId+"' AND LINE_NO = '"+lineNo+"'";
					rs = stmt2.executeQuery(sql);
					if(rs.next()){
						oldQty = rs.getDouble(1);
					}
					stmt2.close();
					stmt2 = null;
					conn1.close();
					conn1 = null;
					rs = null;
					//////////////
					if(deAllocQty + qtyStduom - oldQty > allocQty){
						qtyUpdate = allocQty - deAllocQty;
					}
					else{
						qtyUpdate = qtyStduom - oldQty;
					}
					sql = "UPDATE SORD_ALLOC_DET "+
						"SET DEALLOC_QTY = DEALLOC_QTY + ? "+
						"WHERE TRAN_ID = ? "+
						"AND LINE_NO = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1,qtyUpdate);
					pstmt.setString(2,tranIdSOAlloc);
					pstmt.setLong(3,lineNoSOAlloc);
					pstmt.executeUpdate();
					pstmt = null;
				} 
				String line = "   " + lineNo;
				System.out.println("line :"+line);
				strAllocate.put("tran_date",tranDate);
				strAllocate.put("ref_ser","P-TASK");
				System.out.println("REF ID888888888********"+tranId);
				strAllocate.put("ref_id",tranId);
				strAllocate.put("ref_line",line.substring(line.length()-3));
				System.out.println("Reference line noooo"+line.substring(line.length()-3));
				strAllocate.put("site_code",siteCode);
				strAllocate.put("item_code",itemCode);
				strAllocate.put("loc_code",locCode);
				strAllocate.put("lot_no",lotNo);
				strAllocate.put("lot_sl",lotSl);
				strAllocate.put("alloc_qty",new Double(qtyStduom));
				strAllocate.put("chg_user",genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode"));
				strAllocate.put("chg_term",genericUtility.getValueFromXTRA_PARAMS(xtraParams,"termId"));
				strAllocate.put("chg_win","W_PICK_ORD");
				//Calling DistStkUpdEJB
				System.out.println("Calling DistStkUpdEJB.....");
				if (distStkUpd.updAllocTrace(strAllocate, conn) > 0)
				{
					System.out.println("distStkUpd.UpdAllocTrace(hashMap) : Sucessuful!");
				}
				sqlUpdate = "UPDATE SORDITEM SET QTY_ALLOC = QTY_ALLOC + ? "
						   +"WHERE SALE_ORDER = ? "
						   +"AND LINE_NO = ? "
						   +"AND EXP_LEV = ? ";
				System.out.println("Update sql :"+sqlUpdate);
				pstmt = conn.prepareStatement(sqlUpdate);
				pstmt.setDouble(1,qtyStduom);
				pstmt.setString(2,sordNo);
				pstmt.setString(3,lineNoSord);
				pstmt.setString(4,expLev);
				updateCnt = pstmt.executeUpdate();
				System.out.println("Updated the no of records : updateCnt :"+updateCnt);
				pstmt.clearParameters();
			}
		}//try end
		catch (SQLException sqx)
		{
			System.out.println("The SQLException occurs in ReplanishmentPos :"+sqx);
			sqx.printStackTrace();
			throw new ITMException(sqx);
		}
		catch(Exception e)
		{
			System.out.println("The SQLException occurs in ReplanishmentPos :"+e);			
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(conn1 != null){
					conn1.close();
					conn1 = null;
				}
			}
			catch(Exception e)
			{
				System.err.println("Exception :DBAccessEJB :getITMVersion :\n"+e.getMessage());
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return "";
	}

	private Node getCurrentDetailFromDom(Document dom,String domId)
	{
		NodeList detailList = null;
		Node currDetail = null,reqDetail = null;
		String currDomId = "";
		int	detailListLength = 0;

		detailList = dom.getElementsByTagName("Detail2");
		detailListLength = detailList.getLength();
		for (int ctr = 0;ctr < detailListLength;ctr++)
		{
			currDetail = detailList.item(ctr);
			currDomId = currDetail.getAttributes().getNamedItem("domID").getNodeValue();
			if (currDomId.equals(domId))
			{
				reqDetail = currDetail;
				break;
			}			
		}
		return reqDetail;
	}

	private String getCurrentUpdateFlag(Node currDetail)
	{
		NodeList currDetailList = null;
		String updateStatus = "",nodeName = "";
		int currDetailListLength = 0;

		currDetailList = currDetail.getChildNodes();
		currDetailListLength = currDetailList.getLength();
		for (int i=0;i< currDetailListLength;i++)
		{
			nodeName = currDetailList.item(i).getNodeName();
			if (nodeName.equalsIgnoreCase("Attribute"))
			{
				updateStatus = currDetailList.item(i).getAttributes().getNamedItem("updateFlag").getNodeValue();
				break;
			}
		}
		return updateStatus;		
	}	
	/*	
	private DistStkUpd getComponent(String componentName)throws Exception
    {
		DistStkUpd comp = null;
        try
        {
			System.out.println("Looking For Component.... :: " + componentName);
			AppConnectParm appConnect = new AppConnectParm();
			java.util.Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			DistStkUpdHome distStkUpdHome = (DistStkUpdHome)ctx.lookup(componentName);
			comp = distStkUpdHome.create();
			System.out.println(componentName+" Found.....");			
        }
        catch(Exception e)
        {
			throw e;
        }
		return comp;
	}*/
	private DistStkUpdLocal getComponent(String componentName)throws Exception
    {
		DistStkUpdLocal comp = null;
        try
        {
			System.out.println("Looking For Component.... :: " + componentName);
			AppConnectParm appConnect = new AppConnectParm();
			java.util.Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			 comp = (DistStkUpdLocal)ctx.lookup("ibase/DistStkUpd/local"); // for ejb3
			//comp = distStkUpdHome.create();
			System.out.println(componentName+" Found.....");			
        }
        catch(Exception e)
        {
			throw e;
        }
		return comp;
	}
	
}