<%@page import="java.util.*,javax.sql.*,java.sql.*,javax.naming.*,m05bClasses.*, java.lang.*" %>
<%@include file="/mantenimientosJSP/includes/m05b.inc"%>

<HTML>
	
<%	// _______________________________XLNETs______________________________________
	// Evaluar variable de session que indica si esta conectado a XLNets
	if (session.getAttribute("objetoSeguridad") == null)  { 
		// Verificar que no ha transcurrido el tiempo de sesion (web.xml) %>
			<%@ include file="/m05bControlAcceso.inc"%> 
<%	} 
	String persona = (String) session.getAttribute("persona");
%>	
	
<%
    String Km=request.getParameter("Km")==null?m05bConstantes.REV_PRX_KM:request.getParameter("Km");
    String Dias=request.getParameter("Dias")==null?m05bConstantes.REV_PRX_DIAS:request.getParameter("Dias");
    String Vencidas=request.getParameter("hddVencidas");
	String orden="";
    orden = request.getParameter("Orden")==null?"":request.getParameter("Orden");			
    
    if (Vencidas == null) {
    	Vencidas = "Proximas";
    }
    m05bFileLog miLog = new m05bFileLog();
    miLog.addTraza(persona , "Prox Revision", "Vencidas: " + Vencidas, m05bConstantes.LOG_NIVEL_BAJO);
%>
<%@ include file="/m05bIdioma.inc"%>
	
<head>
		<% if (Vencidas.equals("Vencidas")) {  %>
				    <title><%= resTit.getString("titRevVencidas")%></title>
		<% } else { %>
				    <title><%= resTit.getString("titRevProximas")%></title>
		<% } %>
		<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
		<link rel="stylesheet" href="<%=m05bConstantesPaginas.PATH_ESTILOS%>/m05bestilos.css" type="text/css">
		<script language="javascript1.2" src="<%=m05bConstantesPaginas.PATH_SCRIPTS%>/m05Funciones.js"></script>
        <script language="javascript1.2" src="<%=m05bConstantesPaginas.PATH_SCRIPTS%>/formateador.js"></script>
</head>
<BODY style="cursor: wait" onkeydown="cancelaEnter();" onload="document.body.style.cursor='default'; InitMenu(); return false;" onunload="document.body.style.cursor='wait';">
		<SCRIPT language=JavaScript>
			var urlIdioma;		//URL recarga en funcion del idioma
			var rutaCabecera;	//Camino de migas
			var detActual;		//Ventana actual
			urlIdioma = "<%=m05bConstantesPaginas.VEH_REV_PROX%>";
			<% if (Vencidas.equals("Vencidas")) {  %>
					detActual = '<%=resCM.getString("cmRevVencidas")%>';
			<% } else { %>
					detActual = '<%=resCM.getString("cmRevProximas")%>';
			<% } %>
			//rutaCabecera = "<%=m05bConstantesPaginas.DATOS_CABECERA%>" + urlIdioma + "'>\\"+detActual+"</a>";
			rutaCabecera="";
		</SCRIPT>
		<%@ include file="/mantenimientosJSP/includes/m05Cabecera_c.jsp"%>
<p>
<table  width="100%" height="100%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr><td colspan=3></td></tr>
	<tr valign="top">
		<td width="2%">
			<DIV ID="thessm" style="visibility:hidden;Position : Absolute ;Left : 0 ;Top : 80 ;Z-Index : 20;">
	      		<DIV ID="thessm2" style="Position : Absolute ;Left : (-140) ;Top : 0 ;Z-Index : 20;">
	                 <%@ include file="/m05bMenuInicial.inc"%>
	       		</DIV>
			</DIV>
		</td>
		<td>&nbsp</td>
		<td width="98%">
			<FORM NAME="FormProxRevision" METHOD="POST" ACTION="m05bProxRevisionJsp.jsp">
					<input type="hidden" name="hddVencidas" value="">
								<!--   BOTONERA     -->
					 <TABLE class="general">
					    <tr >
					      <td width="70%" valign="top" class="searchTitle"><%= res.getString("etOrdenarPor")%></td>
					    </tr>
					    <tr> 
					 	  <td class="grupoAsignacion" align="left">
					      	  <% if (orden.equals("MATRICULA")) { %>
						          <input type="RADIO" NAME="Orden" CHECKED VALUE="MATRICULA"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etMatricula")%></FONT>
						          <input type="RADIO" NAME="Orden" VALUE="FECHA"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etFechaProxRevision")%></FONT>
						          <input TYPE="RADIO" NAME="Orden" VALUE="KM"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etKmProxRevision")%></FONT>
					      	  <% } else if (orden.equals("FECHA")) { %>
						  	      <input type="RADIO" NAME="Orden" VALUE="MATRICULA"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etMatricula")%></FONT>
							      <input type="RADIO" NAME="Orden" CHECKED VALUE="FECHA"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etFechaProxRevision")%></FONT>
							      <input TYPE="RADIO" NAME="Orden" VALUE="KM" onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etKmProxRevision")%></FONT>
					      	  <% } else if (orden.equals("KM")) { %>
						  	      <input type="RADIO" NAME="Orden" VALUE="MATRICULA"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etMatricula")%></FONT>
							      <input type="RADIO" NAME="Orden" VALUE="FECHA"  onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etFechaProxRevision")%></FONT>
							      <input TYPE="RADIO" NAME="Orden" CHECKED VALUE="KM" onclick="JavaScript:fncCambiarOrden()" border='0'/><FONT size="2"><%= res.getString("etKmProxRevision")%></FONT>
							  <% }  %>
					      </td>
						  <!--td width="80%" align="right"><INPUT TYPE="IMAGE" style="cursor:hand" VALUE="Retornar" ONCLICK="JavaScript:history.back();return false" onMouseOver="top.status='Retornar'; return true" onMouseOut="top.status=''; return true" src="<%=m05bConstantesPaginas.PATH_IMAGENES%>/retornar.gif"></td-->
                          <td width="80%" align="right"><input type="button" class="boton" name="<%= res.getString("etBOT_RETORNAR")%>" value="<%= res.getString("etBOT_RETORNAR")%>" ONCLICK="JavaScript:history.back();return false;" onMouseOver="top.status='<%= res.getString("etBOT_STATUSRETORNAR")%>'; return false" onMouseOut="top.status=''; return false" /></td>				      					  						  
					      <!--td width="10%" align="right"><INPUT TYPE="IMAGE" style="cursor:hand" VALUE="Aceptar" ONCLICK="JavaScript:fncProxRev();return false;" onMouseOver="top.status='Aceptar'; return false" onMouseOut="top.status=''; return false;" src="<%=m05bConstantesPaginas.PATH_IMAGENES%>/aceptar.gif"/></td-->
					      <td width="10%" align="right"><input type="button" class="boton" name="<%= res.getString("etBOT_ACEPTAR")%>" value="<%= res.getString("etBOT_ACEPTAR")%>" ONCLICK="JavaScript:fncProxRev();return false;" onMouseOver="top.status='<%= res.getString("etBOT_STATUSACEPTAR")%>'; return false" onMouseOut="top.status=''; return false" /></td>				      					  						  
					      <!--td width="10%" align="right"><INPUT 	 style="cursor:hand" VALUE="Revisiones Vencidas" ONCLICK="JavaScript:fncRevVencidas ();return false;" onMouseOver="top.status='Revisiones Vencidas'; return false" onMouseOut="top.status=''; return false;" src="<%=m05bConstantesPaginas.PATH_IMAGENES%>/revvencidas.gif"/></td-->
                          <td width="10%" align="right"><input type="button" class="botonL" name="<%= res.getString("etBOT_REVIVENCIDA")%>" value="<%= res.getString("etBOT_REVIVENCIDA")%>" ONCLICK="JavaScript:fncRevVencidas();return false;" onMouseOver="top.status='<%= res.getString("etBOT_STATUSREVIVENCIDA")%>'; return false" onMouseOut="top.status=''; return false" /></td>				      					  						  					      
					     </tr>
					 </TABLE>
					 <TABLE class="general">
					   <%  	if (Vencidas.equals("Vencidas")) { %>
							     <tr>
							         <td colspan="4">&nbsp;</td>
							     </tr>
					   <% } else { %>
							     <tr>
							         <td class="searchTitle"><%= res.getString("etDias")%></td>
							         <td class="searchTitle"><%= res.getString("etKm")%></td>
							       	 <td></td>
							       	 <td></td>
							     </tr>
							     <tr>
							         <td align="left"><INPUT class="inputField" TYPE="TEXT" NAME="Dias" VALUE="<%=Dias%>" size="5" maxlength="5" onblur="javascript:validaNum(this);"/></td>
							         <td align="left"><INPUT class="inputField" TYPE="TEXT" NAME="Km" VALUE="<%=Km%>" size="5" maxlength="5" onblur="javascript:validaNum(this);"/></td>
							       	 <td></td>
							       	 <td></td>
							     </tr>
					  <%}%>
					  </TABLE>
					  <TABLE class="general">
					    <tr>
					      <td width="15%" class="searchTitle"><%= res.getString("etMatricula")%></td>
					      <td width="15%" class="searchTitle"><%= res.getString("etKmActuales")%></td>
					      <td width="20%" class="searchTitle"><%= res.getString("etFechaProxRevision")%></td>
					      <td width="20%" class="searchTitle"><%= res.getString("etKmProxRevision")%></td>
					      <td width="15%" class="searchTitle"><%= res.getString("etDiasProxRevision")%></td>
					      <td width="15%" class="searchTitle"><%= res.getString("etKmRevision")%></td>
					    </tr>
					<%
							java.sql.Connection m05bConn = null;
							PreparedStatement m05bPstmt = null;
							ResultSet m05bRS = null;
							String sql="";
							int contTotal=0;

					try{
						m05bConexion miConex = new m05bConexion();
						m05bConn = miConex.getConnection();	
						String matricula="", fecha="", kmpr="", kmre="", dia="", codigo="";
					    String campos="", campos1="", campos2="", campos3="";
					    String tablas="", con2="", con3="", con4="", group="";
						String sWhereVencidas = " REALIZA_35 = 'N'";
						String kmAct="";

					    StringBuffer strBuf1= new StringBuffer();
					  if (!(Vencidas.equals("Vencidas"))) {
					    sql=" SELECT " +
						m05bConstantes.COL_35_MATRICULA + " , " + 
						m05bConstantes.COL_34_RANGO + " , " + 
						m05bConstantes.COL_35_FECHA + " , " +
						m05bConstantes.COL_35_KMRE + " , " +
						m05bConstantes.COL_34_KMACTUALES + " , " +
						m05bConstantes.COL_35_KMPRE + " , "+ 
						m05bConstantes.COL_35_KMPRE + " - " + m05bConstantes.COL_34_KMACTUALES + " AS KmsHastaRevision,"+
						m05bConstantes.COL_35_FECPRE + " , ROUND(" + 
						m05bConstantes.COL_35_FECPRE + " - SYSDATE) AS DiasHastaRevision ,"+
						m05bConstantes.COL_35_REALIZA + ","+m05bConstantes.COL_35_CODIGO+
						" FROM " +
						m05bConstantes.TAB_VEHICULOS +" , " +m05bConstantes.TAB_REVISIONES +
						" where ( " +
						" ( " +  m05bConstantes.COL_35_MATRICULA +" = "+ m05bConstantes.COL_34_MATRICULA +" ) "+
						" and "+  
						" ( " + m05bConstantes.COL_35_MATRICULA+"||"+m05bConstantes.COL_35_FECHA + " in (SELECT "+ 
						m05bConstantes.COL_35_MATRICULA+"||Max("+m05bConstantes.COL_35_FECHA+")  FROM "+m05bConstantes.TAB_REVISIONES +
						" GROUP BY "+ m05bConstantes.COL_35_MATRICULA+")) "+ 
						" and "+ 
						"((((" + m05bConstantes.COL_35_KMPRE+ ") Is Not Null)"+ 
						" AND "+
						" (("+ m05bConstantes.COL_35_KMPRE+") <> 0) " +
						" AND "+ 
						" ((" + m05bConstantes.COL_35_KMPRE + "-"+ m05bConstantes.COL_34_KMACTUALES+") Between 0 " +" And " + Km +" )"  +
						" AND ((" + m05bConstantes.COL_34_SITUACION +")='A') "+
						")"+ 
						" OR "+ 
						"("+
						"(("+m05bConstantes.COL_35_FECPRE+") Is Not Null)"+ 
						" AND ((ROUND("+m05bConstantes.COL_35_FECPRE+" - SYSDATE)) Between 0 "+" And " + Dias +")"+
						" AND (("+m05bConstantes.COL_34_SITUACION+")='A')"+
						")))";
                        }else{
                        sql=" SELECT "+
						m05bConstantes.COL_35_MATRICULA + " , " + 
						m05bConstantes.COL_34_RANGO + " , " + 
						m05bConstantes.COL_35_FECHA + " , " +
						m05bConstantes.COL_35_KMRE + " , " +
						m05bConstantes.COL_34_KMACTUALES + " , " +
						m05bConstantes.COL_35_KMPRE + " , "+ 
						m05bConstantes.COL_35_KMPRE + " - " + m05bConstantes.COL_34_KMACTUALES + " AS KmsHastaRevision,"+
						m05bConstantes.COL_35_FECPRE + " , ROUND(" + 
						m05bConstantes.COL_35_FECPRE + " - SYSDATE) AS DiasHastaRevision ,"+
						m05bConstantes.COL_35_REALIZA +
						" FROM " +
						m05bConstantes.TAB_VEHICULOS +" , " +m05bConstantes.TAB_REVISIONES +
						" where ( " +
						" ( " +  m05bConstantes.COL_35_MATRICULA +" = "+ m05bConstantes.COL_34_MATRICULA +" ) "+
						" and "+  
						" ( " + m05bConstantes.COL_35_MATRICULA+"||"+m05bConstantes.COL_35_FECHA + " in (SELECT "+ 
						m05bConstantes.COL_35_MATRICULA+"||Max("+m05bConstantes.COL_35_FECHA+")  FROM "+m05bConstantes.TAB_REVISIONES +
						" GROUP BY "+ m05bConstantes.COL_35_MATRICULA+")) "+ 
						" and "+ 
						"((((" + m05bConstantes.COL_35_KMPRE+ ") Is Not Null)"+ 
						" AND "+
						" (("+m05bConstantes.COL_35_KMPRE+" - "+m05bConstantes.COL_34_KMACTUALES +") <0) "+ 
						" AND "+ 
						" (( "+m05bConstantes.COL_35_REALIZA+")='N') "+
						" AND "+ 
						" ((" +  m05bConstantes.COL_35_REALIZA +")='N')" +
						" AND (("+ m05bConstantes.COL_34_SITUACION+")='A')"+
						")"+
						" OR "+ 
						" ( "+
						"(("+m05bConstantes.COL_35_FECPRE+") Is Not Null)"+
						" AND ((ROUND ("+m05bConstantes.COL_35_FECPRE + " - SYSDATE))<0) " +
						" AND (("+m05bConstantes.COL_35_REALIZA + ")='N')"+ 
						" AND (("+m05bConstantes.COL_34_SITUACION+")='A')"+
						")))";
                        
                        }


						//strBuf1.append(con2).append(con4).append(con3);						    
						    //group=" GROUP BY MATRIC_35 , (KMPREVIS_35-KM_ACT_34) ,(FPREVIS_35 - SYSDATE ),FPREVIS_35  ";
					    //strBuf1.append(group);
			if (orden.equals("MATRICULA"))
			    sql=sql+ " order by "+m05bConstantes.COL_35_MATRICULA+","+m05bConstantes.COL_35_FECPRE+" desc ,"+m05bConstantes.COL_35_KMPRE;
				//strBuf1.append(" order by ").append(m05bConstantes.COL_35_MATRICULA).append(",").append(m05bConstantes.COL_35_FECPRE).append(" desc ,").append(m05bConstantes.COL_35_KMPRE);
			else if (orden.equals("FECHA"))
			    sql=sql+ " order by "+m05bConstantes.COL_35_FECPRE+" desc ,"+m05bConstantes.COL_35_MATRICULA+" , "+m05bConstantes.COL_35_KMPRE;
				//strBuf1.append(" order by ").append(m05bConstantes.COL_35_FECPRE).append(" desc ,").append(m05bConstantes.COL_35_MATRICULA).append(",").append(m05bConstantes.COL_35_KMPRE);
			else if (orden.equals("KM"))
				//strBuf1.append(" order by ").append(m05bConstantes.COL_35_KMPRE).append(" , ").append(m05bConstantes.COL_35_MATRICULA).append(",").append(m05bConstantes.COL_35_FECPRE).append(" desc ");
				sql=sql+ " order by "+m05bConstantes.COL_35_KMPRE+"  ,"+m05bConstantes.COL_35_MATRICULA+" , "+m05bConstantes.COL_35_FECPRE + " desc ";
miLog.addTraza(persona , "Prox Revision", "sql: " + sql, m05bConstantes.LOG_NIVEL_BAJO);					    
					    m05bComun comun = new m05bComun();
    					miLog.addTraza(m05bConstantes.LOG_NO_USER , "Prox. Revisiones",  "SQL:  " + strBuf1.toString(), m05bConstantes.LOG_NIVEL_BAJO);
					    m05bPstmt = m05bConn.prepareStatement(sql);
						m05bRS = m05bPstmt.executeQuery();
						
						while (m05bRS.next() == true){
							    matricula= m05bRS.getString(m05bConstantes.COL_35_MATRICULA);
					            kmre= m05bRS.getString("KmsHastaRevision")==null?"0":m05bRS.getString("KmsHastaRevision");
					            kmpr= m05bRS.getString(m05bConstantes.COL_35_KMPRE)==null?"0":m05bRS.getString(m05bConstantes.COL_35_KMPRE);
					            fecha=m05bRS.getString(m05bConstantes.COL_35_FECPRE);
					            dia=m05bRS.getString("DiasHastaRevision")==null?"0":m05bRS.getString("DiasHastaRevision");
					            fecha=comun.formatearFecha(fecha,campoTexto3);
					            codigo=m05bRS.getString(m05bConstantes.COL_35_CODIGO);
					            kmAct=m05bRS.getString(m05bConstantes.COL_34_KMACTUALES)==null?"0":m05bRS.getString(m05bConstantes.COL_34_KMACTUALES);
					    %>
							<tr>
					   	       <td class="inputFieldTitle"><FONT size="1"><A href="#" onclick="javascript:seleccionar('<%=codigo%>');return false;"><%=matricula%></A></FONT></td>
			                   <input type="hidden" name=<%="kmAct"+contTotal%> value="<%=kmAct%>"> 					   	    
					   		   <script>
			                        formatNumero(document.FormProxRevision.<%="kmAct" + contTotal%>);
					   		   </script>
			                   <td class="inputFieldTitle" style="text-align: right"><FONT size="1"><script> document.write(document.FormProxRevision.<%="kmAct"+contTotal%>.value);</script></FONT></td>
					           <td class="inputFieldTitle" style="text-align: center"><FONT size="1"><%=fecha%></FONT></td>
			                   <input type="hidden" name=<%="kmpr"+contTotal%> value="<%=kmpr%>"> 
					   		   <script>
			                        formatNumero(document.FormProxRevision.<%="kmpr" + contTotal%>);
					   		   </script>
			                   <td class="inputFieldTitle" style="text-align: right"><FONT size="1"><script> document.write(document.FormProxRevision.<%="kmpr"+contTotal%>.value);</script></FONT></td>
			                   <input type="hidden" name=<%="dia"+contTotal%> value="<%=dia%>"> 
					   		   <script>
			                        formatNumero(document.FormProxRevision.<%="dia" + contTotal%>);
					   		   </script>
			                   <td class="inputFieldTitle" style="text-align: right"><FONT size="1"><script> document.write(document.FormProxRevision.<%="dia"+contTotal%>.value);</script></FONT></td>
			                   <input type="hidden" name=<%="kmre"+contTotal%> value="<%=kmre%>"> 
					   		   <script>
			                        formatNumero(document.FormProxRevision.<%="kmre" + contTotal%>);
					   		   </script>
			                   <td class="inputFieldTitle" style="text-align: right"><FONT size="1"><script> document.write(document.FormProxRevision.<%="kmre"+contTotal%>.value);</script></FONT></td>
							</tr>
						<% 	++contTotal;
							}
								 %>
							<tr> </tr>
							   <tr align="center">
							      <td class="inputField" colspan="5"><%= res.getString("etTotal")%> <%=contTotal%></td>
							</tr>
						</TABLE>
			</FORM>
		</td>
	</tr>
	<tr>
			<td colspan="3"><%@ include file="/mantenimientosJSP/includes/m05Pie_c.jsp"%>	</td>
	</tr>
</table>
<SCRIPT language='javascript'>
		 function seleccionar(codigo) {
		    docu=document.FormProxRevision;
		    docu.action="m05bGenerarRevisionServlet?Codigo=" + codigo;
		    docu.submit();
		  }

          function fncRevVencidas() {
		    docu=document.FormProxRevision;
            docu.hddVencidas.value="Vencidas";
            docu.Dias.value="-1";
            docu.Km.value="-1";
            docu.submit();
          }

          function fncProxRev(){
               document.FormProxRevision.submit();
          }
	function fncCambiarOrden() {
		var orden;
		if (document.FormProxRevision.Orden[0].checked) {
			orden="MATRICULA";
		}else if (document.FormProxRevision.Orden[1].checked) {
			orden="FECHA";
		}else {
			orden="KM";
		}
			document.FormProxRevision.action="m05bProxRevisionJsp.jsp?Orden=" + orden;
		    document.FormProxRevision.submit();
	}
	function validaNum(elem){
		if(elem.value == "" || !esEnteroSig(elem.value)){
			alert(dameAlerta(idioma, 14));
			elem.focus();
		}
	}
    setTimeout('initSlide();', 1);
</SCRIPT>
<%
}  catch (Exception ex1) {
		miLog.addTraza(ex1);
}  finally {
		try{
			if (m05bRS != null)
				m05bRS.close();
			if (m05bPstmt != null)
				m05bPstmt.close();
			if (m05bConn != null)
				m05bConn.close();
		} catch (Exception ex2) {
				miLog.addTraza(ex2);
		}
}%>
</BODY>
</HTML>