<%@page import="java.util.*,javax.sql.*,java.sql.*,javax.naming.*,m05bClasses.*" %>
<%@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"%>   
	<% } %>
	<%@ include file="/m05bIdioma.inc"%>
	
<%		
	String finalizada=request.getParameter("Finalizada")==null ? "N":request.getParameter("Finalizada");
%>	
	
<head>
		<title><%= resTit.getString("titAsigGastos")%></title>
		<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
		<link rel="stylesheet" href="<%=m05bConstantesPaginas.PATH_ESTILOS%>/m05bestilos.css" type="text/css">
</head>
<BODY style="cursor: wait" onload="document.body.style.cursor='default';InitMenu();" onkeydown="cancelaEnter();" onunload="document.body.style.cursor='wait';">
 <%//@ include file="/mantenimientosJSP/includes/m05Cabecera_c.jsp"%>
<FORM NAME="FormRelGastos" METHOD="POST">
		<%
			int contTotal = 0;
			String sql = "", sql2="";
			String asig=(String) session.getAttribute("GastosAsig");
			String desde=(String) session.getAttribute("GastosDesde");
			String hasta=(String) session.getAttribute("GastosHasta");
			String ntarjeta="", fecha ="", persona;
		    String matricula=request.getParameter("Matricula");
		    String importe="", codigo="";
            java.sql.Connection conn=null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;

        	m05bFileLog miLog = new m05bFileLog();

		try{
					//session.removeAttribute("Gastos");
				
					persona = (String) session.getAttribute("persona");
					
					m05bCamposInformes miTarjeta = new m05bCamposInformes();
					ntarjeta = (String) miTarjeta.getTarjeta(matricula);
					
					sql = "select "+m05bConstantes.COL_09_CODIGO +
				    ","+m05bConstantes.COL_09_FECHA+","+m05bConstantes.COL_09_IMPORTE+
				    " from "+m05bConstantes.TAB_GASTOS+","+m05bConstantes.TAB_ASIGNACION;
				    /*sql=sql+" where "+m05bConstantes.COL_01_CODASIG+"="+asig+
				    		" AND "+m05bConstantes.COL_01_MATRICULA +"="+m05bConstantes.COL_09_MATRICULA+
				    		" AND to_char(" + m05bConstantes.COL_09_FECHA + ",'DD/MM/YYYY') BETWEEN to_char(" +
				    		m05bConstantes.COL_01_FDESDE + ",'DD/MM/YYYY') and to_char(" + m05bConstantes.COL_01_FHASTA + ",'DD/MM/YYYY')";*/
					sql=sql+" where "+m05bConstantes.COL_01_CODASIG+"="+asig+
				    		" AND "+m05bConstantes.COL_01_MATRICULA +"="+m05bConstantes.COL_09_MATRICULA+
				    		" AND " + m05bConstantes.COL_09_FECHA + " BETWEEN " + m05bConstantes.COL_01_FDESDE + " AND DECODE(" + m05bConstantes.COL_01_FDEVOL+",'',"+ m05bConstantes.COL_01_FHASTA+","+m05bConstantes.COL_01_FDEVOL+")";				    	
				    sql = sql + " ORDER BY " + m05bConstantes.COL_09_FECHA + " DESC, " + m05bConstantes.COL_09_CODIGO + " DESC";
				    miLog.addTraza(persona, "RelGastos","SQL: " + sql, m05bConstantes.LOG_NIVEL_BAJO);
					//m05bbBeginSelect(sql);
					//m05bRS = m05bPStmt.executeQuery();
		           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();					
				%>
				    <input type="hidden" name="CodAsig" value="<%=asig%>">
				    <TABLE class="buscar">
						<tr>
						    <!--td width="90%" align="right"><INPUT TYPE="IMAGE" style="cursor:hand" VALUE="Retornar" ONCLICK="JavaScript:window.close();return false;" onMouseOver="top.status='Retornar'; return true" onMouseOut="top.status=''; return true" src="<%=m05bConstantesPaginas.PATH_IMAGENES%>/retornar.gif"></td-->
                            <td width="90%" align="right"><input type="button" class="boton" name="<%= res.getString("etBOT_RETORNAR")%>" value="<%= res.getString("etBOT_RETORNAR")%>" ONCLICK="JavaScript:window.close();return false;" onMouseOver="top.status='<%= res.getString("etBOT_STATUSRETORNAR")%>'; return false" onMouseOut="top.status=''; return false" /></td>				      					  						    
							<% //if (finalizada.equals("N")) { %>
						    	<!--td width="10%" align="right"> <INPUT TYPE="IMAGE" style="cursor:hand" VALUE="Nuevo" ONCLICK="JavaScript:fncNuevo('<%=matricula%>');return false;" onMouseOver="top.status='Dar de alta un nuevo gasto'; return false" onMouseOut="top.status=''; return false" src="<%=m05bConstantesPaginas.PATH_IMAGENES%>/nuevo.gif"/> </td-->
                                <td width="10%" align="right"><input type="button" class="boton" name="<%= res.getString("etBOT_NUEVO")%>" value="<%= res.getString("etBOT_NUEVO")%>" ONCLICK="JavaScript:fncNuevo('<%=matricula%>');return false;" onMouseOver="top.status='<%= res.getString("etBOT_STATUSNUEVO")%>'; return false" onMouseOut="top.status=''; return false" /></td>				      					  						    	
						    <% //} %>
						</tr>
					</TABLE>
				    
					<TABLE class="buscar" >
					    <tr>
					      <td valign="top" align="left" class="searchTitle"><%= res.getString("etTarjeta")%></td>
					      <td align="left" valign="top" class="searchTitle"><%= res.getString("etMatricula")%></td>
					    </tr>
					    <tr>
					      <td align="left"><INPUT class="inputField" TYPE="TEXT" NAME="Tarjeta" VALUE="<%=ntarjeta%>" disabled size="30" maxlength="30"/></td>
					      <td align="left"><INPUT class="inputField" TYPE="TEXT" NAME="Matricula" VALUE="<%=matricula%>" disabled size="30" maxlength="30"/></td>
					    </tr>
					</TABLE>
					<TABLE class="buscar" >
					    <tr>
					       <td class="searchTitle" width="15%"><%= res.getString("etCodigo")%></td>
					       <td class="searchTitle" width="20%"><%= res.getString("etFecha")%></td>
					       <td class="searchTitle" width="15%"><%= res.getString("etImporte")%></td>
					    </tr>
					</TABLE>
							
					<TABLE class="buscar" >
						<% 
					while (rs.next()==true) {
							codigo = rs.getString(m05bConstantes.COL_09_CODIGO);
							fecha= rs.getString(m05bConstantes.COL_09_FECHA);
					        m05bComun comun = new m05bComun();
					        fecha = comun.formatearFechaHora(fecha);
					        importe=rs.getString(m05bConstantes.COL_09_IMPORTE);
				%>
						<tr >
						   <td class="inputFieldTitle" width="15%"><FONT size="2"><A href="#" onclick="javascript:fncSeleccionar('<%=codigo%>','<%=matricula%>');return false;"><%=codigo%></A></FONT></td>
				   		   <td class="inputFieldTitle" width="20%"><FONT size="2"><%=fecha%></FONT></td>
				           <td class="inputFieldTitle" width="15%"><FONT size="2"><%=importe%></FONT></td>
						</tr>
				<%		++contTotal;
					}%>
						<tr> </tr>
					    <tr align="center">
						      <td class="inputField" colspan="3"><%= res.getString("etTotal")%> <%=contTotal%></td>
					    </tr>
					</TABLE>

				<%
		} catch (Exception ex1) {
			miLog.addTraza(ex1);
		} finally {
			//m05bEndSelect();
		       if (rs != null)
			   rs.close();
		       if (pstmt != null)
			   pstmt.close();
		       if (conn != null)
			   conn.close();
			
		}
		%>
</FORM>
<%//@ include file="/mantenimientosJSP/includes/m05Pie_c.jsp"%>
</BODY>
		<SCRIPT language=JavaScript>
			function fncSeleccionar (codigo, matricula){
				var docu = document.FormRelGastos;
			    docu.action="m05bGenerarGastosServlet?Codigo="+codigo+"&Matricula="+matricula+"&Finalizada="+'<%=finalizada%>';
			    docu.submit();
			}

			function fncNuevo(matricula){
				var nuevo="NUEVO";
				document.FormRelGastos.action="m05bGastosJsp.jsp?Codigo="+nuevo+"&Matricula="+matricula+"&Cabecera=true"+"&Finalizada="+'<%=finalizada%>';
				document.FormRelGastos.submit();
			}
		</SCRIPT>	
</HTML>