package vehiculosServlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import m05bClasses.m05bComun;
import m05bClasses.m05bConexion;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bConstantesInformes;
import m05bClasses.m05bDescrip;
import m05bClasses.m05bFileLog;
import m05bClasses.m05bInformes;

public class m05bImpVehiculosKilometrajesServlet extends
		javax.servlet.http.HttpServlet {
	java.sql.Connection conn = null;
	String sLinea;
	File foFileDir;
	File tmpFoFile;
	FileOutputStream tmpFoFileStream;

	m05bComun miComun = new m05bComun();
	m05bFileLog miLog = new m05bFileLog();
	m05bInformes miInforme = new m05bInformes();
	m05bConexion miConex = new m05bConexion();
	m05bDescrip miDescrip = new m05bDescrip();

	String sSituacion = "", sGRango = "", sRango = "", sDescripcion = "",
			sMatricula = "", sFecDesde = "", sFecHasta = "",
			sSituacionDesc = "", sMarca = "", sModelo = "",
			sDescripcionMM = "", sModeloDesc = "", sSentenciaTotal = "",
			sOrden = "";

	public static final String FO_REQUEST_PARAM = "fo";

	// Logger log = null;

	public void doPost(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {
		try {
			doGet(request, response);
		} catch (Exception e) {
			miLog.addTraza(e);
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		performTask(request, response);
	}

	// if(log == null)
	// {
	// Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
	// log = hierarchy.getLoggerFor("fop");
	// log.setPriority(Priority.WARN);
	// }

	private void fncImprimirComienzoPagina(ResourceBundle res) throws java.io.IOException,
			javax.naming.NamingException {
		sLinea = "<fo:page-sequence master-reference=\"general\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// REGION BEFORE
		sLinea = "		<fo:static-content flow-name=\"xsl-region-before\" >\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// -------------- CABECERA DEL LISTADO --------------
		// Cabecera 1
		sLinea = "      <fo:block text-align=\"end\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      </fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n "
				+ res.getString("infKilRec.titulo") + "</fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"5pt\" font-size=\"9pt\">\n <![CDATA["
				+ res.getString("infKilRec.texto1") 
				+ sFecDesde
				+ res.getString("infKilRec.texto2")
				+ sFecHasta
				+ "]]></fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		if (!(sSituacion.equals(""))) {
			sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"5pt\" font-size=\"9pt\">\n <![CDATA["
					+ res.getString("infKilRec.texto3") + sSituacionDesc + "]]></fo:block>\n";
			tmpFoFileStream.write(sLinea.getBytes());
		}
		if (((sMarca == null) || (sMarca.equals("")))
				&& ((sModelo == null) || (sModelo.equals("")))) {
			sLinea = "      <fo:block  text-align=\"left\" font-size=\"9pt\">\n <![CDATA["
					+ res.getString("infKilRec.texto4") + sRango + " " + sDescripcion + "]]></fo:block>";
			tmpFoFileStream.write(sLinea.getBytes());
		}
		// if (!(sDescripcionMM.equals(""))){
		// sLinea =
		// "      <fo:block font-weight=\"bold\" padding-top=\"5pt\" font-size=\"9pt\">\n "
		// + "Marca/Modelo: " +sDescripcionMM +"</fo:block>\n";
		// tmpFoFileStream.write(sLinea.getBytes());
		// }
		if (!(sModelo.equals(""))) {
			sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"5pt\" font-size=\"9pt\">\n <![CDATA["
					+ res.getString("infKilRec.texto5") + sModeloDesc + "]]></fo:block>\n";
			tmpFoFileStream.write(sLinea.getBytes());
		}

		sLinea = " 		</fo:static-content>\n";
		tmpFoFileStream.write(sLinea.getBytes());

		// PIE DE PAGINA
		miInforme.fncGenerarPiePagina(tmpFoFileStream,
				m05bConstantesInformes.DIR_VERTICAL);

		// BODY
		sLinea = "	<fo:flow flow-name=\"xsl-region-body\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		Context ctx = new InitialContext(System.getProperties());
		// -------------- DETALLE DEL LISTADO --------------
		sLinea = "        <fo:table space-after.optimum=\"12pt\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"8pt\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-row>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// Matricula
		sLinea = "	    <fo:table-cell padding-top=\"2mm\" >\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\" text-align=\"center\">\n "+res.getString("infKilRec.col1")
				+ "</fo:block>";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-cell>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// Marca
		sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infKilRec.col2")+"</fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-cell>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// Modelo
		sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infKilRec.col3")+"</fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-cell>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// F.Matriculacion
		sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infKilRec.col4")+"</fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-cell>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// Km
		sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infKilRec.col5")+"</fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-cell>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		// situacion
		sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infKilRec.col6")+"</fo:block>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-cell>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    </fo:table-row>\n";
		tmpFoFileStream.write(sLinea.getBytes());
		sLinea = "	    <fo:table-row >\n";
		tmpFoFileStream.write(sLinea.getBytes());

	}

	public void performTask(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response)
			throws ServletException {

		java.sql.Connection conn = null;
		PreparedStatement pstmt = null, pstmt2 = null;
		ResultSet rs = null, rs2 = null;
		sSituacion = "";
		sGRango = "";
		sRango = "";
		sDescripcion = "";
		sMatricula = "";
		sFecDesde = "";
		sFecHasta = "";
		sSituacionDesc = "";
		sMarca = "";
		sModelo = "";
		sDescripcionMM = "";
		sModeloDesc = "";
		sSentenciaTotal = "";
		sOrden = "";
		;

		try {
			HttpSession session = request.getSession(true);
			ResourceBundle res = (ResourceBundle) session
					.getAttribute("resTit");

			Locale idiomaLocal = (Locale) session.getAttribute("Local");
			String idiomaSes = ((idiomaLocal.getLanguage() == null) ? "es"
					: idiomaLocal.getLanguage());
			String campoTexto = "", campoTexto2 = "", campoTexto3 = "C";
			String formatoFecha="dd/MM/yyyy";
			if (!idiomaSes.equals("es")) {
				campoTexto = "E";
				campoTexto2 = "E_";
				campoTexto3 = "E";
				formatoFecha = "yyyy/MM/dd";
			}
			conn = miConex.getConnection();

			// Obtención de información de BD
			// Vehículo
			// Recogemos la situacion
			sDescripcionMM = "";
			sSituacion = request.getParameter("Situacion");
			if ((sSituacion == null) || (sSituacion.equals(""))) {
				sSituacion = "";
			} else {
				sSituacion = miComun.ListaCaracteres(sSituacion);
			}
			// Recogemos el Grupo Rango
			sGRango = request.getParameter("GRango");
			if ((sGRango == null) || (sGRango.equals(""))
					|| (sGRango.equals("***"))) {
				sGRango = "";
			}

			// Recogemos el Rango
			sRango = request.getParameter("Rango");
			if ((sRango == null) || (sRango.equals(""))) {
				sRango = "";
			}
			// Recogemos la Matricula
			sMarca = request.getParameter("CodMarca");
			if ((sMarca == null) || (sMarca.equals(""))
					|| (sMarca.equals("***"))) {
				sMarca = "";
			}
			// Recogemos el Modelo
			sModelo = request.getParameter("CodModelo");
			if ((sModelo == null) || (sModelo.equals(""))) {
				sModelo = "";
			} else {
				sModelo = miComun.ListaCaracteres(sModelo);
			}
			// Recogemos la Matricula
			sMatricula = request.getParameter("Matricula");
			if (sMatricula == null) {
				sMatricula = "";
			}
			// Recogemos la fecha Desde
			sFecDesde = request.getParameter("FecDesde");
			if (sFecDesde == null) {
				sFecDesde = "";
			}
			// Recogemos la fecha Hasta:
			sFecHasta = request.getParameter("FecHasta");
			if (sFecHasta == null) {
				sFecHasta = "";
			}

			String sUsuario = (String) session.getAttribute("persona");

			double dNumero = 0.0;

			String sFIniKm = "", sFFinKm = "", sIniKm = "", sFinKm = "", sDifKm = "", sTotDifKm = "", sDifDia = "", sTotDifDia = "", sKilometraje = "", sTotKilometraje = "", sRangoAnt = "", sSituacionD = "";
			String sModeloD = "";

			if (!(sSituacion.equals(""))) {
				String sSentenciaSituacion = " SELECT NVL("+campoTexto
						+ m05bConstantes.COL_99_DESCRIPCION +","+m05bConstantes.COL_99_DESCRIPCION + ") FROM "
						+ m05bConstantes.TAB_DESCRIPCIONES + "  S " + " WHERE "
						+ m05bConstantes.COL_99_CODDESC + " in (" + sSituacion
						+ ") AND " + m05bConstantes.COL_99_CODTABLA + "='"
						+ m05bConstantes.DESC_SITUACIONES + "'";
				pstmt = conn.prepareStatement(sSentenciaSituacion);
				rs = pstmt.executeQuery();
				while (rs.next()) {
					sSituacionD = rs.getString(1);
					if (!(sSituacionDesc.equals(""))) {
						sSituacionDesc = sSituacionDesc + "," + sSituacionD;
					} else {
						sSituacionDesc = sSituacionD;
					}
				}
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
			}
			if (!(sModelo.equals(""))) {
				String sSentenciaModelo = " SELECT "
						+ m05bConstantes.COL_17_DESCRIP + " FROM "
						+ m05bConstantes.TAB_MODELOS + "  S " + " WHERE "
						+ m05bConstantes.COL_17_CODMOD + " in (" + sModelo
						+ ")";
				pstmt = conn.prepareStatement(sSentenciaModelo);
				rs = pstmt.executeQuery();
				while (rs.next()) {
					sModeloD = rs.getString(1);
					if (!(sModeloDesc.equals(""))) {
						sModeloDesc = sModeloDesc + "," + sModeloD;
					} else {
						sModeloDesc = sModeloD;
					}
				}
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
			}
			String sSentencia = " SELECT " + m05bConstantes.COL_34_MATRICULA
					+ "," + m05bConstantes.COL_90_RANGO + ", NVL(R."+campoTexto
					+ m05bConstantes.COL_99_DESCRIPCION+",R."+ m05bConstantes.COL_99_DESCRIPCION+ "),TO_CHAR("
					+ m05bConstantes.COL_90_FINIKM + ",'"+formatoFecha+"'),"
					+ m05bConstantes.COL_90_INIKM + ",TO_CHAR("
					+ m05bConstantes.COL_90_FFINKM + ",'"+formatoFecha+"'),"
					+ m05bConstantes.COL_90_FINKM + ","
					+ m05bConstantes.COL_34_SITUACION + ","
					+ m05bConstantes.COL_90_DIFKM + " FROM "
					+ m05bConstantes.TAB_DESCRIPCIONES + "  R,"
					+ m05bConstantes.TAB_VEHICULOS + ","
					+ m05bConstantes.TAB_TMPVEHICULOS + " WHERE "
					+ m05bConstantes.COL_34_MATRICULA + "="
					+ m05bConstantes.COL_90_MATRICULA + " AND "
					+ m05bConstantes.COL_99_CODTABLA + "='"
					+ m05bConstantes.DESC_RANGOS + "' AND "
					+ m05bConstantes.COL_90_RANGO + "= R."
					+ m05bConstantes.COL_99_CODDESC + " AND "
					+ m05bConstantes.COL_90_USUARIO + "='" + sUsuario + "'"
					+ " AND ( ((" + m05bConstantes.COL_90_FINIKM
					+ "<= TO_DATE('" + sFecDesde + "','"+formatoFecha+"'))"
					+ " AND (" + m05bConstantes.COL_90_FFINKM + ">= TO_DATE('"
					+ sFecDesde + "','"+formatoFecha+"'))) OR (("
					+ m05bConstantes.COL_90_FINIKM + ">= TO_DATE('" + sFecDesde
					+ "','"+formatoFecha+"')) AND (" + m05bConstantes.COL_90_FFINKM
					+ "<= TO_DATE('" + sFecHasta + "','"+formatoFecha+"')))OR (("
					+ m05bConstantes.COL_90_FFINKM + ">= TO_DATE('" + sFecHasta
					+ "','"+formatoFecha+"')) AND (" + m05bConstantes.COL_90_FINIKM
					+ "<= TO_DATE('" + sFecHasta + "','"+formatoFecha+"'))))";

			if (((sMarca == null) || (sMarca.equals("")))
					&& ((sModelo == null) || (sModelo.equals("")))) {
				sSentenciaTotal = " SELECT " + m05bConstantes.COL_90_RANGO
						+ "," + " SUM(" + m05bConstantes.COL_90_DIFKM + ")"
						+ " FROM " + m05bConstantes.TAB_VEHICULOS + ","
						+ m05bConstantes.TAB_TMPVEHICULOS + " WHERE "
						+ m05bConstantes.COL_34_MATRICULA + "="
						+ m05bConstantes.COL_90_MATRICULA + " AND "
						+ m05bConstantes.COL_90_USUARIO + "='" + sUsuario + "'"
						+ " AND ( ((" + m05bConstantes.COL_90_FINIKM
						+ "<= TO_DATE('" + sFecDesde + "','"+formatoFecha+"'))"
						+ " AND (" + m05bConstantes.COL_90_FFINKM
						+ ">= TO_DATE('" + sFecDesde
						+ "','"+formatoFecha+"'))) OR (("
						+ m05bConstantes.COL_90_FINIKM + ">= TO_DATE('"
						+ sFecDesde + "','"+formatoFecha+"')) AND ("
						+ m05bConstantes.COL_90_FFINKM + "<= TO_DATE('"
						+ sFecHasta + "','"+formatoFecha+"')))OR (("
						+ m05bConstantes.COL_90_FFINKM + ">= TO_DATE('"
						+ sFecHasta + "','"+formatoFecha+"')) AND ("
						+ m05bConstantes.COL_90_FINIKM + "<= TO_DATE('"
						+ sFecHasta + "','"+formatoFecha+"'))))";
			} else {
				sSentenciaTotal = " SELECT " + " SUM("
						+ m05bConstantes.COL_90_DIFKM + ")" + " FROM "
						+ m05bConstantes.TAB_VEHICULOS + ","
						+ m05bConstantes.TAB_TMPVEHICULOS + " WHERE "
						+ m05bConstantes.COL_34_MATRICULA + "="
						+ m05bConstantes.COL_90_MATRICULA + " AND "
						+ m05bConstantes.COL_90_USUARIO + "='" + sUsuario + "'"
						+ " AND ( ((" + m05bConstantes.COL_90_FINIKM
						+ "<= TO_DATE('" + sFecDesde + "','"+formatoFecha+"'))"
						+ " AND (" + m05bConstantes.COL_90_FFINKM
						+ ">= TO_DATE('" + sFecDesde
						+ "','"+formatoFecha+"'))) OR (("
						+ m05bConstantes.COL_90_FINIKM + ">= TO_DATE('"
						+ sFecDesde + "','"+formatoFecha+"')) AND ("
						+ m05bConstantes.COL_90_FFINKM + "<= TO_DATE('"
						+ sFecHasta + "','"+formatoFecha+"')))OR (("
						+ m05bConstantes.COL_90_FFINKM + ">= TO_DATE('"
						+ sFecHasta + "','"+formatoFecha+"')) AND ("
						+ m05bConstantes.COL_90_FINIKM + "<= TO_DATE('"
						+ sFecHasta + "','"+formatoFecha+"'))))";

			}

			if (!(sSituacion.equals(""))) {
				// sSituacion=miComun.ListaCaracteres(sSituacion);
				sSentencia = sSentencia + " AND "
						+ m05bConstantes.COL_34_SITUACION + " in ("
						+ sSituacion + ")";
				sSentenciaTotal = sSentenciaTotal + " AND "
						+ m05bConstantes.COL_34_SITUACION + " in ("
						+ sSituacion + ")";
			}
			if (!(sRango.equals(""))) {
				sSentencia = sSentencia + " AND " + m05bConstantes.COL_90_RANGO
						+ " = '" + sRango + "'";
				sSentenciaTotal = sSentenciaTotal + " AND "
						+ m05bConstantes.COL_90_RANGO + " = '" + sRango + "'";
			}

			else if (!(sGRango.equals(""))) {
				String sSentenciaGrupo = " SELECT "
						+ m05bConstantes.COL_36_RANGOS + " FROM "
						+ m05bConstantes.TAB_GRUPOS + " WHERE "
						+ m05bConstantes.COL_36_CODIGO + " = '" + sGRango + "'";
				pstmt = conn.prepareStatement(sSentenciaGrupo);
				rs = pstmt.executeQuery();
				if (rs.next()) {
					sGRango = rs.getString(1) == null ? "" : rs.getString(1);
					sGRango = miComun.ListaCaracteres(sGRango);
					sSentencia = sSentencia + " AND "
							+ m05bConstantes.COL_90_RANGO + " in ( " + sGRango
							+ ")";
					sSentenciaTotal = sSentenciaTotal + " AND "
							+ m05bConstantes.COL_90_RANGO + " in ( " + sGRango
							+ ")";
					// miLog.addTraza(sUsuario , "sqlKilometrosServletVehiculos"
					// , "sSentencia: " + sSentencia,
					// m05bConstantes.LOG_NIVEL_BAJO);

				}
			}

			if (!(sModelo.equals(""))) {
				// sSituacion=miComun.ListaCaracteres(sSituacion);
				sSentencia = sSentencia + " AND "
						+ m05bConstantes.COL_34_MODELO + " in (" + sModelo
						+ ")";
				sSentenciaTotal = sSentenciaTotal + " AND "
						+ m05bConstantes.COL_34_MODELO + " in (" + sModelo
						+ ")";
			}

			if (!(sMarca.equals(""))) {
				// sMarca=miComun.ListaCaracteres(sMarca);
				sSentencia = sSentencia + " AND " + m05bConstantes.COL_34_MARCA
						+ " =  '" + sMarca + "'";
				sSentenciaTotal = sSentenciaTotal + " AND "
						+ m05bConstantes.COL_34_MARCA + "= '" + sMarca + "'";
				// miLog.addTraza(sUsuario , "sqlKilometrosServletVehiculos" ,
				// "sSentencia: " + sSentencia, m05bConstantes.LOG_NIVEL_BAJO);
				sDescripcionMM = miDescrip.getDescripcion(
						m05bConstantes.DESC_MARCAS, sMarca)
						+ " "
						+ sDescripcionMM;
			}
			// if (!(sDescripcionMM.equals(""))){

			// }

			if (!(sMatricula.equals(""))) {
				sSentencia = sSentencia + " AND "
						+ m05bConstantes.COL_34_MATRICULA + " = '" + sMatricula
						+ "'";
				sSentenciaTotal = sSentenciaTotal + " AND "
						+ m05bConstantes.COL_34_MATRICULA + " = '" + sMatricula
						+ "'";
			}

			if (((sMarca == null) || (sMarca.equals("")))
					&& ((sModelo == null) || (sModelo.equals("")))) {
				sOrden = " ORDER BY 2 asc,1 asc";
			} else {
				sOrden = " ORDER BY 1 asc";
			}

			sSentencia = sSentencia + sOrden;

			// miLog.addTraza(sUsuario ,
			// "m05b - ImpVehiculosKilometrajesServlet:  " , "sSentenciaTotal: "
			// + sSentenciaTotal, m05bConstantes.LOG_NIVEL_BAJO);
			// miLog.addTraza(sUsuario ,
			// "m05b - ImpVehiculosKilometrajesServlet:  " , "sSentencia: " +
			// sSentencia, m05bConstantes.LOG_NIVEL_BAJO);
		//	System.out.println(sSentencia);
			pstmt = conn.prepareStatement(sSentencia);
		
			rs = pstmt.executeQuery();

			tmpFoFileStream = miInforme.fncAbrirFicheroTemporal();

			sLinea = "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n";
			tmpFoFileStream.write(sLinea.getBytes());

			sLinea = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
			tmpFoFileStream.write(sLinea.getBytes());
			// LAYOUT DEL LISTADO
			sLinea = "<fo:layout-master-set>\n";
			tmpFoFileStream.write(sLinea.getBytes());
			sLinea = "	<fo:simple-page-master master-name=\"general\"  page-height=\"29.5cm\" page-width=\"21cm\" margin-top=\"2cm\" margin-bottom=\"2cm\" margin-left=\"2.5cm\" margin-right=\"2.5cm\">\n";
			tmpFoFileStream.write(sLinea.getBytes());
			sLinea = "		<fo:region-before extent=\"3cm\"/>\n";
			tmpFoFileStream.write(sLinea.getBytes());
			sLinea = "		<fo:region-body  margin-top=\"3.8cm\" margin-bottom=\"1.5cm\" />\n";
			tmpFoFileStream.write(sLinea.getBytes());
			sLinea = "		<fo:region-after  extent=\"1cm\"/>\n";
			tmpFoFileStream.write(sLinea.getBytes());
			sLinea = "		<fo:region-start extent=\"0cm\"/>\n";
			tmpFoFileStream.write(sLinea.getBytes());
			sLinea = "		<fo:region-end extent=\"0cm\"/>\n";
			tmpFoFileStream.write(sLinea.getBytes());

			sLinea = "	</fo:simple-page-master>\n";
			tmpFoFileStream.write(sLinea.getBytes());

			sLinea = "</fo:layout-master-set>\n";
			tmpFoFileStream.write(sLinea.getBytes());
			int cont = 0;
			String sSentenciaTotal1 = "";
			String dNumeroS = "";
			while (rs.next()) {
				sMatricula = rs.getString(1) == null ? "" : rs.getString(1);
				sRango = rs.getString(2) == null ? "" : rs.getString(2);
				sDescripcion = rs.getString(3) == null ? "" : rs.getString(3);
				sFIniKm = rs.getString(4) == null ? "" : rs.getString(4);

				dNumeroS = (rs.getString(5) == null) ? "0" : rs.getString(5);
				sIniKm = miComun.formatearDblMiles(new Double(dNumeroS)
						.doubleValue());
				sIniKm = sIniKm.replaceAll(",", ".");
				sFFinKm = rs.getString(6) == null ? "" : rs.getString(6);

				dNumeroS = (rs.getString(7) == null) ? "0" : rs.getString(7);
				sFinKm = miComun.formatearDblMiles(new Double(dNumeroS)
						.doubleValue());
				sFinKm = sFinKm.replaceAll(",", ".");
				dNumeroS = (rs.getString(9) == null) ? "0" : rs.getString(9);
				sDifKm = miComun.formatearDblMiles(new Double(dNumeroS)
						.doubleValue());
				sDifKm = sDifKm.replaceAll(",", ".");
				if (((sMarca == null) || (sMarca.equals("")))
						&& ((sModelo == null) || (sModelo.equals("")))) {
					if (((!(sRango.equals(sRangoAnt)))) || (cont == 54)) {
						if ((sRangoAnt != "") && (!(sRango.equals(sRangoAnt)))) {
							// sRangoAnt=sRango;
							sSentenciaTotal1 = sSentenciaTotal + " AND "
									+ m05bConstantes.COL_90_RANGO + "='"
									+ sRangoAnt + "' GROUP BY "
									+ m05bConstantes.COL_90_RANGO;
							pstmt2 = conn.prepareStatement(sSentenciaTotal1);
							rs2 = pstmt2.executeQuery();
							if (rs2.next()) {
								dNumeroS = (rs2.getString(2) == null) ? "0"
										: rs2.getString(2);
								sTotDifKm = miComun
										.formatearDblMiles(new Double(dNumeroS)
												.doubleValue());
								sTotDifKm = sTotDifKm.replaceAll(",", ".");
								if (pstmt2 != null) {
									pstmt2.close();
								}
								sLinea = "	    <fo:table-row >\n";
								tmpFoFileStream.write(sLinea.getBytes());
								// Matricula
								sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "      <fo:block  text-align=\"center\">\n "
										+ "</fo:block>";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "	    </fo:table-cell>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								// Marca
								sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "      <fo:block  text-align=\"center\">\n "
										+ "</fo:block>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "	    </fo:table-cell>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								// Modelo
								sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "      <fo:block  text-align=\"center\">\n"
										+ "</fo:block>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "	    </fo:table-cell>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								// F.Matricula
								sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "      <fo:block  text-align=\"center\">\n"
										+ "</fo:block>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "	    </fo:table-cell>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								// Km
								sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "      <fo:block  text-align=\"center\">\n"
										+ "</fo:block>\n";
								sLinea = "	    </fo:table-cell>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								// situacion
								sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
										+ sTotDifKm + "]]></fo:block>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "	    </fo:table-cell>\n";
								tmpFoFileStream.write(sLinea.getBytes());
								sLinea = "	    </fo:table-row>\n";
								tmpFoFileStream.write(sLinea.getBytes());
							} // if (rs2.next())
							miInforme.fncImprimirFinPagina(tmpFoFileStream);
						} else if (cont == 54) {
							miInforme.fncImprimirFinPagina(tmpFoFileStream);
						} // Fin else if (cont==54)
						fncImprimirComienzoPagina(res);
						cont = 0;
					} else {
						cont = cont + 1;
						sLinea = "	    <fo:table-row >\n";
						tmpFoFileStream.write(sLinea.getBytes());
					} // Fin if ( (sRangoAnt!="") &&
						// (!(sRango.equals(sRangoAnt))) )
					sRangoAnt = sRango;
					// Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA[ "
							+ sMatricula + "]]></fo:block>";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Marca
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n <![CDATA["
							+ sFIniKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Modelo
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sIniKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// F.Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sFFinKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Km
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sFinKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// situacion
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sDifKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-row>\n";
					tmpFoFileStream.write(sLinea.getBytes());
				} else {
					if (cont == 54) {
						miInforme.fncImprimirFinPagina(tmpFoFileStream);
						cont = 0;
					}
					if (cont == 0) {
						fncImprimirComienzoPagina(res);
						cont = 0;
					}
					if ((cont != 0)) {

						sLinea = "	    <fo:table-row >\n";
						tmpFoFileStream.write(sLinea.getBytes());
					}
					cont = cont + 1;
					// Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA[ "
							+ sMatricula + "]]></fo:block>";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Marca
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n <![CDATA["
							+ sFIniKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Modelo
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sIniKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// F.Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sFFinKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Km
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sFinKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// situacion
					sLinea = "	    <fo:table-cell padding-top=\"0.2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sDifKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-row>\n";
					tmpFoFileStream.write(sLinea.getBytes());
				}

			} // Fin while

			if (((sMarca == null) || (sMarca.equals("")))
					&& ((sModelo == null) || (sModelo.equals("")))) {
				sSentenciaTotal1 = sSentenciaTotal + " AND "
						+ m05bConstantes.COL_90_RANGO + "='" + sRango
						+ "' GROUP BY " + m05bConstantes.COL_90_RANGO;
				pstmt2 = conn.prepareStatement(sSentenciaTotal1);
				// miLog.addTraza(sUsuario , "m05b - Grabar Vehiculos:  " ,
				// "sSentenciaTotal1: " + sSentenciaTotal1,
				// m05bConstantes.LOG_NIVEL_BAJO);

				rs2 = pstmt2.executeQuery();
				if (rs2.next()) {
					dNumeroS = (rs2.getString(2) == null) ? "0" : rs2
							.getString(2);
					sTotDifKm = miComun.formatearDblMiles(new Double(dNumeroS)
							.doubleValue());
					sTotDifKm = sTotDifKm.replaceAll(",", ".");
					sLinea = "	    <fo:table-row >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n "
							+ "</fo:block>";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Marca
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n "
							+ "</fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Modelo
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n"
							+ "</fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// F.Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n"
							+ "</fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Km
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n"
							+ "</fo:block>\n";
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// situacion
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sTotDifKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-row>\n";
					tmpFoFileStream.write(sLinea.getBytes());
				}
			} else {
				sSentenciaTotal1 = sSentenciaTotal;
				pstmt2 = conn.prepareStatement(sSentenciaTotal1);
				// miLog.addTraza(sUsuario , "m05b - Grabar Vehiculos:  " ,
				// "sSentenciaTotal1: " + sSentenciaTotal1,
				// m05bConstantes.LOG_NIVEL_BAJO);

				rs2 = pstmt2.executeQuery();
				if (rs2.next()) {
					dNumeroS = (rs2.getString(1) == null) ? "0" : rs2
							.getString(1);
					sTotDifKm = miComun.formatearDblMiles(new Double(dNumeroS)
							.doubleValue());
					sTotDifKm = sTotDifKm.replaceAll(",", ".");
					sLinea = "	    <fo:table-row >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n "
							+ "</fo:block>";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Marca
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n "
							+ "</fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Modelo
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n"
							+ "</fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// F.Matricula
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n"
							+ "</fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// Km
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" >\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n"
							+ "</fo:block>\n";
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					// situacion
					sLinea = "	    <fo:table-cell padding-top=\"0.4mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["
							+ sTotDifKm + "]]></fo:block>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-cell>\n";
					tmpFoFileStream.write(sLinea.getBytes());
					sLinea = "	    </fo:table-row>\n";
					tmpFoFileStream.write(sLinea.getBytes());
				}
			}
			miInforme.fncImprimirFinPagina(tmpFoFileStream);
			rs.close();

			sLinea = "</fo:root>\n";
			tmpFoFileStream.write(sLinea.getBytes());
			tmpFoFileStream.close();

			// miLog.addTraza(m05bConstantes.LOG_NO_USER,
			// m05bConstantes.LOG_NO_SRC, "Antes Generar PDF desde FO" ,
			// m05bConstantes.LOG_NIVEL_BAJO);
			miInforme.generarPDFdesdeFO(response);
			// miLog.addTraza(m05bConstantes.LOG_NO_USER,
			// m05bConstantes.LOG_NO_SRC, "Despues Generar PDF desde FO" ,
			// m05bConstantes.LOG_NIVEL_BAJO);

		} catch (Exception ex) {
			try {
				response.sendRedirect("m05bError.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				miLog.addTraza(e);
			}
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (rs2 != null)
					rs2.close();
				if (pstmt != null)
					pstmt.close();
				if (pstmt2 != null)
					pstmt2.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e2) {
				try {
					response.sendRedirect("m05bError.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					miLog.addTraza(e);
				}
			}
		} // fin finally

	} // Fin funcion performTask

}