<%@page import="java.util.*,javax.sql.*,java.sql.*,javax.naming.*,m05bClasses.*" %>
<%@include file="/mantenimientosJSP/includes/m05b.inc"%>
<!-- Para poder mostrar caracteres especiales en los mensajes javascript -->
<%@page contentType="text/html; charset=UTF-8" %>	
<HTML>
<%@ include file="/m05bIdioma.inc"%>
<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
	<script language="javascript1.2" src="<%=m05bConstantesPaginas.PATH_SCRIPTS%>/m05Funciones.js"></script>
	<script language="javascript1.2" src="<%=m05bConstantesPaginas.PATH_SCRIPTS%>/formateador.js"></script>	
</HEAD>
	
<%
	  String sql="";
	  boolean bolExiste=false;
      m05bFileLog miLog = new m05bFileLog();
	  String strNorden=request.getParameter("NOrden")==null?"":request.getParameter("NOrden");
	  String strImporte=request.getParameter("FormatImporte");
	  if ((strImporte==null) || strImporte.equals("")){
	   strImporte="0";
	  }
	  String strFecha=request.getParameter("FRevision");
	  String strKm=request.getParameter("KmRevision");
	  String strDesc=request.getParameter("Desc");
	  strDesc = strDesc.replaceAll("\"","\'\'");
	  String strkmp="", strCodigo="", strfprev="";
	  String strMatricula=request.getParameter("Matricula").toUpperCase();
%>

<BODY style="cursor: wait" bgcolor="white" onkeydown="cancelaEnter();" onload="document.body.style.cursor='default';" onunload="document.body.style.cursor='wait';">
  <form name="formOrdRev" >
       <input type="hidden" name="Codigo" value="">
       <input type="hidden" name="Descripcion" value="<%=strDesc%>">
       <input type="hidden" name="Matricula" value="<%=strMatricula%>">
       <input type="hidden" name="Fecha" value="<%=strFecha%>">
       <input type="hidden" name="KmProxRevision" value="">
       <input type="hidden" name="KmRevision" value="<%=strKm%>">
       <input type="hidden" name="FechaProx" value="">
       <input type="hidden" name="NOrden" value="<%=strNorden%>">
       <input type="hidden" name="Importe" value="<%=strImporte%>">
       
  </form>
<%  

      sql="select " + m05bConstantes.COL_35_CODIGO +
          " FROM " + m05bConstantes.TAB_REVISIONES +
          " where " + m05bConstantes.COL_35_ORDEN + "='" + strNorden + "'";
    java.sql.Connection conn=null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;	          
  try {
	     if (!sql.equals("")){
           m05bConexion miConex = new m05bConexion();
           conn = miConex.getConnection();
           conn.setAutoCommit(false);  // Al final si todo ha ido bien se hace commit	 
		   pstmt = conn.prepareStatement(sql);
		   rs = pstmt.executeQuery();        	   
		   //m05bbBeginSelect(sql);
		   //m05bRS = m05bPStmt.executeQuery();
		   //if (m05bRS.next() == true){		            
		       //bolExiste=true; 
		   //}
		   if (rs.next() == true){		            
		       bolExiste=true; 
		   }		   
	     }
  } catch (Exception ex){
   		miLog.addTraza(ex);
  } finally {
      //m05bEndSelect();
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (conn != null)
					conn.close();      
  }
    if (bolExiste){
%>
           <script>
                 alert(dameAlerta(idioma, 227));
           </script>
 <%      	

 } else {
		   	 sql="select "+m05bConstantes.COL_35_CODIGO+" FROM "+m05bConstantes.TAB_REVISIONES +
		   	 		" ORDER BY  TO_NUMBER("+m05bConstantes.COL_35_CODIGO+") DESC";
			  try {
				     if (!sql.equals("")){
					       //m05bbBeginSelect(sql);
						   //m05bRS = m05bPStmt.executeQuery();					      
					       //if (m05bRS.next() == true){
					               //strCodigo=m05bRS.getString(m05bConstantes.COL_35_CODIGO);
					       //}
					       m05bConexion miConex = new m05bConexion();
                           conn = miConex.getConnection();
                           conn.setAutoCommit(false);  // Al final si todo ha ido bien se hace commit	 
		                   pstmt = conn.prepareStatement(sql);
		                   rs = pstmt.executeQuery();   
					       if (rs.next() == true){
					           strCodigo=rs.getString(m05bConstantes.COL_35_CODIGO);
					       }		                        	   
				     }
			  } catch (Exception SQLException){
			   		miLog.addTraza(SQLException);
			  } finally {
			      //m05bEndSelect();
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (conn != null)
					conn.close();			      
			  }
	          int cod=0;
	          cod=Integer.parseInt(strCodigo);
	          cod=cod+1;
	          strCodigo=""+cod;
	          sql="select " + m05bConstantes.COL_34_KMREVIS + "," + m05bConstantes.COL_34_MESREVIS +
	          		" FROM " + m05bConstantes.TAB_VEHICULOS +
	          		" where " + m05bConstantes.COL_34_MATRICULA + "='" + strMatricula + "'";
		  	  try {
			     if (!sql.equals("")){
				       //m05bbBeginSelect(sql);
					   //m05bRS = m05bPStmt.executeQuery();
				       //while (m05bRS.next() == true){				               
				               //strkmp=m05bRS.getString(m05bConstantes.COL_34_KMREVIS);
				               //strfprev=m05bRS.getString(m05bConstantes.COL_34_MESREVIS);
				      //}
					       m05bConexion miConex = new m05bConexion();
                           conn = miConex.getConnection();
                           conn.setAutoCommit(false);  // Al final si todo ha ido bien se hace commit	 
		                   pstmt = conn.prepareStatement(sql);
		                   rs = pstmt.executeQuery();   
				           while (rs.next() == true){				               
				               strkmp=rs.getString(m05bConstantes.COL_34_KMREVIS);
				               strfprev=rs.getString(m05bConstantes.COL_34_MESREVIS);
				           }		                   				      
			     }
			  } catch (Exception ex){
			   		miLog.addTraza(ex);
			  } finally {
			     // m05bEndSelect();
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (conn != null)
					conn.close();			     
			      
			  }
          %>
                <script>          
                var msge = dameAlerta(idioma, 70);
                msge = msge.replace("replace1", '<%=strMatricula%>');
				msge = msge.replace("replace2", '<%=strKm%>');
				msge = msge.replace("replace3", '<%=strFecha%>');
				msge = msge.replace("replace4", '<%=strDesc%>');
				msge = msge.replace("replace5", '<%=strImporte%>');
				if (confirm(msge)){
		            document.formOrdRev.Codigo.value="<%=strCodigo%>";
		            var i=0;
		            var KmRevision2=trimPointers(document.formOrdRev.KmRevision.value);
		            i=parseInt(KmRevision2,10);
		            i=i+parseInt('<%=strkmp%>',10);
		            document.formOrdRev.KmProxRevision.value=i;
		            
		            var fec=document.formOrdRev.Fecha.value;
		            var aux=fec.substring(fec.indexOf("/")+1,fec.lastIndexOf("/"));
		            var aux2=fec.substring(fec.lastIndexOf("/")+1,fec.length);
		            ano=parseInt(aux2,10);
		            c=parseInt(aux,10);
		            m=parseInt('<%=strfprev%>',10);
		            d=m/12;
		            aux1=Math.floor(d);
		            ano=ano+aux1;
		            if ((m-(aux1*12))==0){
			               aux3=fec.substring(0,fec.indexOf("/"))+"/"+aux+"/"+ano;
			               document.formOrdRev.FechaProx.value=aux3;
		            } else {
			             m=c+(m-(aux1*12));
			             if (m>12){
				               ano++;
				               m=m-12;
			             }
			              aux3=fec.substring(0,fec.indexOf("/"))+"/"+m+"/"+ano;
			              document.formOrdRev.FechaProx.value=aux3;
		            }
		            FechaValida(document.formOrdRev.FechaProx);
		            formatearFecha(document.formOrdRev.FechaProx);		            
		            top.formRevision.Codigo.value=document.formOrdRev.Codigo.value;
		            top.formRevision.Matricula.value=document.formOrdRev.Matricula.value;
		            top.formRevision.Descripcion.value=document.formOrdRev.Descripcion.value;
		            top.formRevision.Fecha.value=document.formOrdRev.Fecha.value;
		            top.formRevision.KmProxRevision.value=document.formOrdRev.KmProxRevision.value;
		            top.formRevision.KmRevision.value=document.formOrdRev.KmRevision.value;
		            top.formRevision.FechaProx.value=document.formOrdRev.FechaProx.value;
		            top.formRevision.NOrden.value=document.formOrdRev.NOrden.value;
		            top.formRevision.Importe.value=document.formOrdRev.Importe.value;
		            top.formRevision.RevMes.value="<%=strfprev%>";
		            top.formRevision.Realizada.value="S";
		            top.formRevision.Accion.value="I";
		            top.fncRev();
		     }    
		     </script>  
		    
		    <%
    }
%>
</BODY>
</HTML>