<%@page import="java.rmi.RemoteException"%>
<%@page import="ibase.webitm.utility.ITMException"%>
<%@page contentType="text/html"%>
<%@page	import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"%>
<%@page import="ibase.utility.CommonConstants"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="org.w3c.dom.Node"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="org.w3c.dom.NodeList"%>
<%@page import="ibase.webitm.utility.GenericUtility"%>

<jsp:useBean id="scmdasBean" scope="request" class="ibase.webitm.bean.wms.LocationStockOccuBean" />
<html>
<head>
<link rel="stylesheet" type="text/css" 	href="/ibase/wms/css/scmdashboard.css" />
<link rel="stylesheet" type="text/css" 	href="/ibase/wms/css/ui.dropdownchecklist.standalone.css" />
<link rel="stylesheet" type="text/css"	href="/ibase/wms/css/jquery-ui.css" />
<script type="text/javascript"	src="/ibase/wms/jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript"	src="/ibase/wms/jquery/jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript"	src="/ibase/wms/jquery/ui.dropdownchecklist-1.4-min.js"></script>

<%!GenericUtility genericUtility = GenericUtility.getInstance();
	String xmlString = "";
	String objName = "";
	Document dom = null;
	NodeList dtlList = null;
	Node parentNode = null;
	NodeList childNodeList = null;
	Node currDetail = null;	
	String childNodeName = "", locPhyArea = "", areaDtlString = "", selectedArea = "",imgSrc ="";
	String siteCode = "";
	String locationRange="";
	NodeList rowList = null, stackList = null, columnList = null;
	boolean stockNotPresent;
	boolean stockPresent;
	Node childNode = null;
	Node rowNode = null, stackNode = null, columnNode = null;
	String areaStr = "";
	NodeList locationCodeList = null;
	Node locationNode = null;
	NodeList stockList = null;
	int frmTop;
	int frmLeft;
%>
<%
	response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
	response.setHeader("Cache-Control", "no-store, no-cache,must-revalidate");
	response.addHeader("Cache-Control", "post-check=0, pre-check=0");
	response.setHeader("Pragma", "no-cache");
	int ctr = 0;
	
%>
<script type="text/javascript">   
 $(document).ready(function()
		 {
 	$("#area").dropdownchecklist({emptyText: "Select ", width:100, height:25 });
 
 	$( "#opendiv" ).dialog({
 		
 		autoOpen: false,
        modal: false,
        draggable: false,
        height: "auto",
        width: "auto",
        resizable: false,
        position: [630,30],
        closeOnEscape: true,
 	
 	});
 	 $("#opendiv").dialog({width:400,height:150}); 
   	$( "#img" ).click(function() 
 			{
 	$( "#opendiv" ).dialog( "open" );

 	});
 	$( "#cancel" ).click(function() 
 	   {
 		$( "#opendiv" ).dialog( "close" );
 	   });
 	
 	$( "#ok" ).click(function() 
 	 	   {
	 	   var area= $("#area").val();
	 	   var locationRange = $("#locationRange").val();
 		$('#LocationStockOccupancy').attr('action', 'LocationStockOccupancy.jsp?area='+area+'&locationRange='+locationRange);
 		$('#LocationStockOccupancy').submit();
 			 	 
 	     //document.getElementById("LocationStockOccupancy").submit();
 	 	   });
		 });
 
 </script>
<script type="text/javascript">	
	  
	 function getStockDtl(location,row,stack,column)
     {
	        var newTable,startTag,endTag;		  
	             	 startTag="<TABLE id='parentStkTbl' class='parentstktbl' ><TBODY>"
	        	 endTag="</TBODY></TABLE>"
                 newTable=startTag;	   
                               		  
	      data = document.getElementById("stock").value;      		  
	     
	    if (window.DOMParser) 
	      {
	  		parser = new DOMParser();
	  		xmlDoc = parser.parseFromString(data, "text/xml");
	  	} else
	  	{
	  		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  		xmlDoc.async = false;
	  		xmlDoc.loadXML(data);
	  	} 
	      if (xmlDoc != null && xmlDoc != "")
	    		{
	    	  x=xmlDoc.getElementsByTagName("loc_phy_area")[0]
            	  locPhyRowList=x.childNodes;
	    	  if(locPhyRowList !=null)
	    		   {
	    		    for (i = 0; i < locPhyRowList.length; i++) 
 				  {
	    			      locPhyRow = locPhyRowList[i];
	    			      if(locPhyRowList[i].attributes !=null)
	    			    	 { 
	    			    	    rowValue=locPhyRowList[i].attributes.getNamedItem("row").nodeValue;
	    			         
	    			    	    if(rowValue==row)
		    			    		 {
		    			    	      
			    			    	     locPhyStackList=locPhyRow.childNodes;
				    			      
			    			    	      for (j = 0; j < locPhyStackList.length; j++) 
						    				{
					    			    	  locPhyStack=locPhyStackList[j];
	    			    	 
						    			    	  if(locPhyStackList[j].attributes !=null)
						    			    	  { 
						    			                    stackValue=locPhyStackList[j].attributes.getNamedItem("stack").nodeValue;
							    			               	if(stackValue ==stack)
							    			               		{
								    			                  
									    			               	locPhyColList=locPhyStack.childNodes;
										    			    	       for (k = 0; k < locPhyColList.length; k++) 
													    				{
										    			    	          locPhyCol=locPhyColList[k];
										    			    	          colValue=locPhyCol.getAttribute("col");
													    			    	          
													    			    	          if(colValue==column)
													    			    	          {
										    			    	                     
													    			    	          locationCodeList=locPhyCol.childNodes;
													    			    	                
													    			    	                 for (l = 0; l <locationCodeList.length; l++) 
																 					    				{
															    			    	                	     locationCode=locationCodeList[l];
															    			    	                	     locationCodeValue=locationCode.getAttribute("lcode");
															    			    	                	     if(locationCodeValue==location)
																							    			    	      {
																							    			    	            stockList= locationCode.childNodes;
																				    			    	                	     if(stockList !=null)
																				    			    	                	    {	 
																						    			    	                	      for (m = 0; m<stockList.length; m++) 
																									 					    				{
																						    			    	                	    	  stock=stockList[m];
																						    			    	                	    	  stockDtl=stock.childNodes;
																												    			    	          
																						    			    	                	    	  newTable+="<TR>";
																						    			    	                	    	  
																						    			    	                	    	  newTable+="<TD>";
																						    			    	                	    	  newTable+="<TABLE id='stockTbl' class='stocktbl'>";
																						    			    	                	    	  
																						    			    	                	    	  for (n = 0; n<stockDtl.length; n++) 
																																 					 {
																						    			    	                	    		  if(n==0)
																											    			    	        		{ 
																						    			    	                	    	  		  newTable+="<TR>";
																						    			    	                	    		      newTable+="<TD>Item Code :"+stockDtl[n].childNodes[0].nodeValue +"</TD>";
																											    			    	        		
																											    			    	        		}
																						    			    	                	    		     
																						    			    	                	    		     
																						    			    	                	    		   if(n==2)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Lot No :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  } 

																						    			    	                	    		   if(n==1)
																											    			    	        		 { 
																						    			    	                	    		     newTable+="<TD>Description :"+stockDtl[n].childNodes[0].nodeValue+"</TD>"; 
																						    			    	                	    		     newTable+="</TR>";
																											    			    	        		 }
																						    			    	                	    		   
																						    			    	                	    		   if(n==3)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Lot Serial :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		   
																						    			    	                	    		   if(n==4)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD > Quantity :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			 
																						    			    	                	    			  }
																						    			    	                	    		   if(n==5)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TD >Hold Quantity :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		  
																						    			    	                	    		   if(n==6)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Allocated Qty :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		  
																						    			    	                	    		   if(n==7)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Mfg. Date :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			  }
																						    			    	                	    		   if(n==8)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TD>Expiry Date :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		   if(n==9)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			   newTable+="<TD>Retest Date :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																												    			    	        		}
																						    			    	                	    	  
																						    			    	                	    	          newTable+="</TABLE>";
																						    			    	                	    	          newTable+="</TD>";
																												    			    	        	  newTable+="</TR>";


																													    			    	        	  if(m< stockList.length-1)
																													    			    	        	  { 
																												    			    	        	       newTable+="<TR> <TD> &nbsp;</TD></TR>";
																													    			    	        	  }
																																 					 }
										 					    				                                                             }
							    			    	                	                                                          }
		 					    				                                                                         }
																 					    				}
													    			    	                 }
													    			    	          
													    			    	}
										    			    	       
				    				                             }	
							    			               	
							    			               	}  
						    			    	  
		    				                    }	
				    			        } 
 				  
	    			    	 }
 				}
	    		}  
	      newTable+=endTag;
	    document.getElementById('stocktableDiv').innerHTML=newTable;
	    $("#stocktableDiv").dialog({width:425,height:350}); 
	    $("#FieldOnFocus").attr('value',this.id); 
	    $('#stocktableDiv').css({
	    	  width: '425px',
	    	});
     	  
		   if(location==null)
  	      {
			 $('#stocktableDiv').dialog('close');
		   }

var span=document.getElementById("ui-dialog-title-stocktableDiv");

while(span.firstChild)
{
span.removeChild(span.firstChild);
}
span.appendChild(document.createTextNode(location));
             
     }	

  </script>
</head>
<%
	try {
		
			ibase.utility.UserInfoBean userInfo = (ibase.utility.UserInfoBean) session.getAttribute("USER_INFO");
			//String user     = userInfo.getLoginCode();           
            siteCode          = userInfo.getSiteCode();      
            System.out.println("siteCodebeforeif::::"+siteCode);			
           // siteCode        =request.getParameter("siteCode");
		    if (siteCode == null || siteCode.trim().length() == 0 ) 
			{
			  siteCode  = "TA387";
			  System.out.println("siteCodeinsideif::::"+siteCode);
			 }                   
		    if(xmlString == "")
			 {
				xmlString = scmdasBean.getLocPhyArea();
			 }
			//add by Birendra Pandey on dated 21 Aug 2014			
             selectedArea         = request.getParameter("area");
             locationRange        = request.getParameter("locationRange");	             
             if(locationRange==null){
             locationRange ="";
             }
             if(selectedArea!=null && selectedArea.trim().length() != 0 )            
              { 
           	  areaStr = scmdasBean.getLocDtl(selectedArea, siteCode,locationRange);
           	  
              }
          //end code on dated 21Aug2014   
%>
<body>



<table class="pagetitle">
<tr>
        <td class="pagetitlename"> Location Stock Occupancy</td>	
	<td align="right"> <img id='img' class="img" alt="" src="/ibase/wms/images/analysis_scope.png"  onclick="divopen()" />  
</tr> 
</table>

<form method="post" id='LocationStockOccupancy' action="LocationStockOccupancy.jsp" name="LocationStockOccupancy">
<div id='opendiv' >
 <table  class='popUpFiltrTbl' >
 <tr>
						
	
					<td> Area <select  name="area" id="area"  >
						<%
						if (xmlString != null && xmlString.trim().length() > 0) 
						{
								dom = genericUtility.parseString(xmlString);
								if (dom != null)
								{
									dtlList = dom.getElementsByTagName("Detail");
									for (int cntr = 0; cntr < dtlList.getLength(); cntr++) 
									{
										parentNode = dtlList.item(cntr);
										childNodeList = parentNode.getChildNodes();

										for (int nodCtr = 0; nodCtr < childNodeList .getLength(); nodCtr++) 
										{
											currDetail = childNodeList.item(nodCtr);
											childNodeName = currDetail.getNodeName();
											if (childNodeName.trim().equals("loc_phy_area"))
											{
												//locPhyArea = currDetail.getFirstChild().getNodeValue();
												locPhyArea = currDetail.getTextContent();

												if (locPhyArea != "")
												{
													%>
						<option><%=locPhyArea%>
						</option>
						<%
												}
											}
										}

									}
								}
							}
					%>
				</select>
				
				</td> 
			 <td> Location Range Like <input type text name="locationRange"	id="locationRange" size="10" value="<%=locationRange%>">
			 </td>
						
	</tr>
		
	</table>
	 <input type hidden name="stock" id="stock"  value="<%=areaStr.replaceAll("\"", "'")%>" >
	<div style="position: absolute;left:300;top:94; ">
	<table>
	<tr>	
		    <td>
			<input type="button" id='cancel' name='cancel' value="Cancel">
			<input type="button" id='ok'  name="ok" value="Ok" /></td>
	
	</tr>
	
</table>
</div>


</div>



		<table align="center">
			
			<tr>
				<td>
					<div id="wrapper">
						<ul id="index_cards">
							<%
							         frmTop = 50;
							         frmLeft =50;
								
							 if(areaStr !=null && areaStr.length() >0)
							        {
							        dom = genericUtility.parseString(areaStr);
								    rowList = dom.getElementsByTagName("loc_phy_row");
								for (int rowCtr = 0; rowCtr < rowList.getLength(); rowCtr++) 
								{
									rowNode   = rowList.item(rowCtr);
									stackList = rowNode.getChildNodes();
									int cardID = rowCtr + 1;
									List<String>  colImg = new ArrayList<String>();
									List<String>  columnArray = new ArrayList<String>();
									List<String>	 columnDataList=new ArrayList();
									
									
									boolean isStkPresnt=false;
									boolean  isStkNotPresnt=false;
									String imagePath="";
									if(stackList.item(0).getChildNodes()!=null)
									{
							
									   for(int totlCol=0; totlCol<stackList.item(0).getChildNodes().getLength(); totlCol++ )
											         	{
										                   stockNotPresent=false;														
									                       stockPresent=false;
												for(int stkCtr = 0; stkCtr < stackList.getLength(); stkCtr++) 
																	{
																	    	stackNode = stackList.item(stkCtr);
																		    columnList = stackNode.getChildNodes();
																		    
																		if (columnList.getLength() > 0) 
																		{
																				columnNode = columnList.item(totlCol);
																				
																				if(columnNode!=null)	
																				{
																					if(columnNode.getChildNodes() !=null)
																						{		
																							locationCodeList = columnNode.getChildNodes();
																					
																							if(locationCodeList.getLength() >0)
																								{	
																									locationNode = locationCodeList.item(0);
																									stockList    = locationNode.getChildNodes();
																									columnArray.add(columnNode.getAttributes().item(0).getNodeValue());
																									
																										if (stockList.getLength() > 0)
																											{   
																												stockPresent=true;
																												isStkPresnt=true;
columnArray.add(columnNode.getAttributes().item(0).getNodeValue());
																											} 
																										else
																									    {
																										stockNotPresent=true;
																										isStkNotPresnt=true;
columnArray.add(columnNode.getAttributes().item(0).getNodeValue());
																									    }
																								}
											                          					 }	
																					
																				}	
																		
																	}	
																						}
																		
												                        if(stockPresent && stockNotPresent)
																				{
																				 imgSrc ="/ibase/wms/images/yellow.png";	
																				
																				}else if(stockPresent)
																				{
																				   imgSrc ="/ibase/wms/images/green.png";	
																				}else
																				{	
																					imgSrc ="/ibase/wms/images/white.png";
																				}
								                                        	colImg.add(imgSrc);
												}	
								 }
									
									
									
									//System.out.println("isStkPresnt ="+isStkPresnt +" and isStkNotPresnt="+isStkNotPresnt);
									 if(isStkPresnt && isStkNotPresnt)
									{
										imagePath ="/ibase/wms/images/yellow.png";	
									
									}else if(isStkPresnt)
									{
										imagePath ="/ibase/wms/images/green.png";	
									}else if(isStkNotPresnt)
									{	
										imagePath ="/ibase/wms/images/white.png";
									}
							
						%>

							<li id="<%="card-"+cardID%>"
								style="left:<%=frmLeft%>px;top:<%=frmTop%>px;">
								<table id='celltable'>
									<tr>
										<td background="<%=imagePath%>"><%=rowNode.getAttributes().item(0).getNodeValue()%></td>
										<%
  						   	 columnDataList=new ArrayList();
    	  						   	 for (int stkCtr = 0; stkCtr < stackList.getLength(); stkCtr++) 
    									{
    											    	stackNode = stackList.item(stkCtr);
    												    columnList = stackNode.getChildNodes();
    												     stockNotPresent=false;														
    												     stockPresent=false;
    												if (columnList.getLength() > 0) 
    												{
    													for (int colCtr = 0; colCtr < columnList.getLength(); colCtr++) 
    													   {
    														columnNode = columnList.item(colCtr);
    														
    														if(columnNode!=null)	
    														{
    															if(stkCtr==0)
    															{	
    															columnDataList.add(columnNode.getAttributes().item(0).getNodeValue());		
    														     }
    														}
    													}
    												}
    									}    
    						   	      for( int i=0;i<colImg.size(); i++)
  						   	    	   {
  						   	    		 %>
										<td background="<%=colImg.get(i)%>" align="right"><%= columnDataList.get(i)%></td>
										<%
  						   	    	    } %>
									</tr>
									<%
										for (int stkCtr = 0; stkCtr < stackList.getLength(); stkCtr++) 
										{
												    	stackNode = stackList.item(stkCtr);
													    columnList = stackNode.getChildNodes();
													     stockNotPresent=false;														
													     stockPresent=false;
													if (columnList.getLength() > 0) 
													{

														
														for (int colCtr = 0; colCtr < columnList.getLength(); colCtr++) 
														   {
															columnNode = columnList.item(colCtr);
															
															if(columnNode!=null)	
															{
																if(columnNode.getChildNodes() !=null)
																	{		
																		locationCodeList = columnNode.getChildNodes();
																
																		if(locationCodeList.getLength() >0)
																			{	
																				locationNode = locationCodeList.item(0);
																				stockList = locationNode.getChildNodes();
																				 
																					if (stockList.getLength() > 0)
																						{   
																																
																						stockPresent=true;
										
																						} 
																					else
																				    {
																						stockNotPresent=true;
																					}
																			}
																		}
															 }						
														   }
														
														 if(stockPresent && stockNotPresent)
														  {
															 imgSrc ="/ibase/wms/images/yellow.png";
														  }
														 else if(stockPresent)
														  {
															   imgSrc ="/ibase/wms/images/green.png";	
														  }
														 else if(stockNotPresent)
														  {	
																 imgSrc ="/ibase/wms/images/white.png";
														  }
														
														 %>
									<tr id="info">
										<td background="<%=imgSrc%>" align="right"><%=stackNode.getAttributes().item(0).getNodeValue()%></td>
										<%
								
														for (int colCtr = 0; colCtr < columnList.getLength(); colCtr++) 
														{
															columnNode = columnList.item(colCtr);
															
															if(columnNode!=null)	
															{
																if(columnNode.getChildNodes() !=null)
																	{		
																		locationCodeList = columnNode.getChildNodes();
																
																		if(locationCodeList.getLength() >0)
																			{	
																				locationNode = locationCodeList.item(0);
																				stockList = locationNode.getChildNodes();
																			//	 System.out.println("stockList.getLength() in real ="+stockList.getLength());
																					if (stockList.getLength() > 0)
																						{   
										%>
										<!--<!--%=columnNode.getAttributes().item(0).getNodeValue()%-->
										<td class="itemcell" onclick="getStockDtl('<%=locationNode.getAttributes().item(0) .getNodeValue()%>','<%=rowNode.getAttributes().item(0).getNodeValue()%>','<%=stackNode.getAttributes().item(0).getNodeValue()%>','<%=columnNode.getAttributes().item(0).getNodeValue()%>');"> <%=locationNode.getAttributes().item(0) .getNodeValue()%>
										</td>
										<%
											} 
											else
										    {
										%>
										<td class="cellempty" onclick="getStockDtl();"><%=locationNode.getAttributes().item(0) .getNodeValue()%>
										</td>
										<%
											}
										}
																	}}
													}
										%>
									</tr>
									<%
										}}
									%>
								</table>
							</li>
							<%
							        frmLeft+=25;
									frmTop+= 25;
								}
								}
						%>
						</ul>
					</div>
				</td>
				<td>
					<div id="stocktableDiv" width="400px"></div>
				</td>
			</tr>
		</table>

		<%
		} catch (RemoteException ex) {
			System.out .println("Exception : LocationStockOccupancey  sMessageArguments(String,wizardBean) :" + ex); //$NON-NLS-1$

		} catch (ITMException ex) {
			System.out .println("Exception : LocationStockOccupancey  sMessageArguments(String,wizardBean) :" + ex); //$NON-NLS-1$

		} catch (Exception ex) {
			System.out .println("Exception : LocationStockOccupancey  sMessageArguments(String,wizardBean) :" + ex); //$NON-NLS-1$
		}
	%>
	</form>
	<script>
  areaCode           ="<%=this.selectedArea%>";
  locRange           ="<%=this.locationRange%>";  
  document.getElementById("locationRange").value=locRange;
   window.onload      = new function() { 

	    var currList   = document.getElementById("area");         
        for (var m = 0; m < currList.options.length; m++) 
            	 {
        	     var checkval = currList.options[m].value;
        		   if(checkval==areaCode)
    				  {
                        
        			   currList.options[m].selected = true;
    				  }
    		       }
         
    
           
      
};

</script>
</body>
</html>

