package ordenreparacionServlet;

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.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.m05bFileLog;
import m05bClasses.m05bInformes;


public class m05bVehReparacionesServlet 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();	
	    
	    String sMatricula="", sMatriculaAnt="", sFEntrada="", sFSalida="", 
			   sOrden="", sCodRepa="", sOrdenAnt="", 
			   sConcepto="", sEmpleado="", sHoras="", sKmEntrada="",sKmSalida="";
	    double dImporteOT=0.0, dImportePie=0.0, dImporteTotal=0.0, dKm=0.0;
	    String sImporteOT="", sImporteRepa="", sImportePie="", sImporteTotal="";	    
	       
		
   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
   {
       performTask(request, response);
   }

   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 fncImprimirComienzoTabla()throws java.io.IOException, javax.naming.NamingException
{

					sLinea= "        <fo:table space-after.optimum=\"20pt\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "	    <fo:table-column column-width=\"40mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "	    <fo:table-column column-width=\"25mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        sLinea = "   <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        sLinea = "   <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        sLinea = "   <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());			        
			        sLinea = "   <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        sLinea = "      <fo:table-body font-family=\"Helvetica\" font-size=\"6pt\">";
				    tmpFoFileStream.write(sLinea.getBytes());
	       	    	  sLinea = "	    <fo:table-row>\n";
			          tmpFoFileStream.write(sLinea.getBytes());
}  // Fin funcion fncImprimirComienzoTabla

private void fncImprimirComienzoBloque(ResourceBundle res )throws java.io.IOException, javax.naming.NamingException
{


		     // --------------    CABECERA DEL LISTADO    --------------
             //Cabecera 1
		       sLinea = "      <fo:block font-weight=\"bold\" font-size=\"10pt\" padding-top=\"10pt\">\n "+res.getString("infReparaciones.texto1")+": " + sOrden +"</fo:block>\n";
			   tmpFoFileStream.write(sLinea.getBytes());               
			   sLinea = "     <fo:block font-weight=\"bold\" font-size=\"10pt\">\n "+res.getString("infReparaciones.texto2")+": " + sFEntrada + "     -     "+res.getString("infReparaciones.texto3")+": " + sFSalida + "</fo:block> \n";
			   tmpFoFileStream.write(sLinea.getBytes());
			   sLinea = "     <fo:block font-weight=\"bold\" font-size=\"10pt\">\n "+res.getString("infReparaciones.texto4")+": " + sImporteOT + "</fo:block> \n";
			   tmpFoFileStream.write(sLinea.getBytes());
			   
  				miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC,"	IMPRIMIR COMIENZO TABLA FUNCION " , m05bConstantes.LOG_NIVEL_BAJO);	         
			   fncImprimirComienzoTabla();
		              // Cod. Reparacion
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col1")+"</fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());	
			          // Concepto
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col2")+"</fo:block>";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());
			          // Empleado
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\"  font-size=\"7pt\"  text-align=\"center\">\n"+res.getString("infReparaciones.col3")+"</fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());
     			      // Horas
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col4")+"</fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());	
     			      // KmEntrada
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col5")+"</fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());	
     			      // KmSalida
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col6")+"</fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());				          
                      // Imp. Reparacion
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col7")+" </fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());
     			      // Imp. Pieza
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col8")+"</fo:block>\n";
					  tmpFoFileStream.write(sLinea.getBytes());
			          sLinea = "	    </fo:table-cell>\n";
			          tmpFoFileStream.write(sLinea.getBytes());	
     			      // Total
			          sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			          tmpFoFileStream.write(sLinea.getBytes());
					  sLinea = "      <fo:block font-weight=\"bold\" font-size=\"7pt\" text-align=\"center\">\n"+res.getString("infReparaciones.col9")+"</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());
	
}  // Fin funcion fncImprimirComienzoBloque

/**
 * Initializes the servlet.
 */
   public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException
   {
	   java.sql.Connection conn=null;
	   PreparedStatement pstmt = null;
	   ResultSet rs = null;
	   
      try  {  
      		HttpSession session = request.getSession(true);
      		
      	  /*Iker. idioma*/
    		Locale idiomaLocal = (Locale)session.getAttribute("Local");

    		ResourceBundle res =  (ResourceBundle)session.getAttribute("resTit");
    		String idiomaSes = ((idiomaLocal.getLanguage()==null)?"es":idiomaLocal.getLanguage());	
    		String campoTexto3="C",campoTexto="";
    		String formatoFecha="dd-mm-yyyy hh24:mi";
    		String formatoFecha2="dd-mm-yyyy";
    		if(!idiomaSes.equals("es")){
    			campoTexto3="E";	campoTexto="E";
    			formatoFecha="yyyy-mm-dd hh24:mi";
    			formatoFecha2="yyyy-mm-dd";
    			
    		}
			m05bConexion miConex = new m05bConexion();
			conn = miConex.getConnection();

			sMatricula=request.getParameter("Matricula")==null?"":request.getParameter("Matricula");
			sFEntrada=request.getParameter("FEntrada")==null?"":request.getParameter("FEntrada");
	     
			String sSQL="select min(" + m05bConstantes.COL_20_MATRICULA + "), min(" + m05bConstantes.COL_20_CODIGO + 
     					"), min(to_char(" + m05bConstantes.COL_20_FENTRADA +	 ",'"+formatoFecha+"')), min(to_char(" + m05bConstantes.COL_20_FSALIDA + ",'"+formatoFecha+"'))" +
						", min(" + m05bConstantes.COL_20_IMPORTE + "), min(" + m05bConstantes.COL_19_CODIGO + 
						"), min(NVL(" +campoTexto+ m05bConstantes.COL_25_DESCRIP + ","+m05bConstantes.COL_25_DESCRIP +")), min(" + m05bConstantes.COL_07_NOMBRE + 
						"), min(" + m05bConstantes.COL_19_TIEMPO + "), min(" + m05bConstantes.COL_19_IMPORTE  +
						"), min(" + m05bConstantes.COL_20_KM_ENTRADA+ "), min(" + m05bConstantes.COL_20_KM_SALIDA +"), sum( "+ m05bConstantes.COL_23_TOTAL  + ")" +
					" from " + m05bConstantes.TAB_ORDENESTRAB + ", " + m05bConstantes.TAB_ORDEREPAR + 
						", " + m05bConstantes.TAB_PIEZAREP + ", " + m05bConstantes.TAB_EMPTALLER  +
						", "+m05bConstantes.TAB_REPARACIONES +
					" where  " + m05bConstantes.COL_20_MATRICULA + " = '" +  sMatricula + "'" +
						" and " + m05bConstantes.COL_25_CODIGO + " = " + m05bConstantes.COL_19_CODIGO  +
						" and " + m05bConstantes.COL_19_COD_ORD + " = " + m05bConstantes.COL_20_CODIGO  +
						" and " + m05bConstantes.COL_23_ORDEN + " (+) = " + m05bConstantes.COL_19_COD_ORD  +
						" and " + m05bConstantes.COL_23_CODIGO + " (+) = " + m05bConstantes.COL_19_CODIGO  +						
						" and " + m05bConstantes.COL_07_CODIGO + " = " + m05bConstantes.COL_19_EMPLEADO;
		/*	String sSQL="select " + m05bConstantes.COL_20_MATRICULA + ", " + m05bConstantes.COL_20_CODIGO + 
				", to_char(" + m05bConstantes.COL_20_FENTRADA +	 ",'dd-mm-yyyy hh24:mi'), to_char(" + m05bConstantes.COL_20_FSALIDA + ",'dd-mm-yyyy hh24:mi')" +
			", " + m05bConstantes.COL_20_IMPORTE + ", " + m05bConstantes.COL_19_CODIGO + 
			", " + m05bConstantes.COL_19_CONCEPTO + ", " + m05bConstantes.COL_07_NOMBRE + 
			", " + m05bConstantes.COL_19_TIEMPO + ", " + m05bConstantes.COL_19_IMPORTE  +
			", " + m05bConstantes.COL_23_TOTAL  +
		" from " + m05bConstantes.TAB_ORDENESTRAB + ", " + m05bConstantes.TAB_ORDEREPAR + 
			", " + m05bConstantes.TAB_PIEZAREP + ", " + m05bConstantes.TAB_EMPTALLER  +
		" where  " + m05bConstantes.COL_20_MATRICULA + " = '" +  sMatricula + "'" +
			" and " + m05bConstantes.COL_19_COD_ORD + " = " + m05bConstantes.COL_20_CODIGO  +
			" and " + m05bConstantes.COL_23_ORDEN + " (+) = " + m05bConstantes.COL_19_COD_ORD  +
			" and " + m05bConstantes.COL_23_CODIGO + " (+) = " + m05bConstantes.COL_19_CODIGO  +						
			" and " + m05bConstantes.COL_07_CODIGO + " = " + m05bConstantes.COL_19_EMPLEADO;		*/	
			if (!(sFEntrada.equals(""))) {
				sSQL+=" and " + m05bConstantes.COL_20_FENTRADA + " >= to_date('" + sFEntrada + "','"+formatoFecha2+"')";
			}
			sSQL+=" group by " + m05bConstantes.COL_20_MATRICULA + ", " + m05bConstantes.COL_20_CODIGO + 
					", " + m05bConstantes.COL_19_CODIGO;
			sSQL+=" order by " + m05bConstantes.COL_20_CODIGO + " desc, " + m05bConstantes.COL_19_CODIGO ;			
            miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC,"	SQL: "+ sSQL, m05bConstantes.LOG_NIVEL_BAJO);
            pstmt = conn.prepareStatement(sSQL);
            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=\"2cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-body  margin-top=\"2cm\" margin-bottom=\"1cm\" />\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());
	         
	         sLinea = "<fo:page-sequence master-reference=\"general\">\n";
	         tmpFoFileStream.write(sLinea.getBytes());	         
        	 // TITULO
	         miInforme.fncGenerarTitulo(tmpFoFileStream, res.getString("infReparaciones.titulo")+": ", sMatricula);
        	  
        	 // PIE DE PAGINA
    		 miInforme.fncGenerarPiePagina(tmpFoFileStream, m05bConstantesInformes.DIR_VERTICAL,campoTexto3,res.getString("pagina")); 	 
    		 
    		 //BODY
	         sLinea = "	<fo:flow flow-name=\"xsl-region-body\">\n";
	         tmpFoFileStream.write(sLinea.getBytes());
	     	  // PAGE-SEQUENCE DEL LISTADO 
		      //int cont=0;
		      boolean primeraVez=true;
		      String dNumeroS="";
	   		  while (rs.next()){
	   	    			   sMatricula = rs.getString(1)==null ? "":rs.getString(1);
	   	    			   sOrden = rs.getString(2);
	   	    			   sFEntrada = rs.getString(3);
	   	    			   sFSalida = rs.getString(4)==null ? "":rs.getString(4);	       	    			   
	   	    			   dImporteOT = rs.getDouble(5);	
	   	    			   sImporteOT=miComun.formatearDblMilesDec(dImporteOT); 	       	    			   
	   	    			   sCodRepa = rs.getString(6);		   
	   	    			   sConcepto = rs.getString(7);		
	   	    			   sEmpleado = rs.getString(8);		
	   	    			   sHoras = rs.getString(9)==null ? "":rs.getString(9);	
	   	    			   dImporteTotal = rs.getDouble(10);
	   	    			   sImporteTotal=miComun.formatearDblMilesDec(dImporteTotal);
	   	    			   if (sImporteTotal.equals("0,00")) {
	   	    			   		sImporteTotal="";
	   	    			   }
	   	    			   dImportePie = rs.getDouble(13);
	   	    			   sImportePie=miComun.formatearDblMilesDec(dImportePie);	    	    			   
	   	    			   if (sImportePie.equals("0,00")) {
	   	    			   		sImportePie="";
	   	    			   }	    
	   	    			   sImporteRepa=miComun.formatearDblMilesDec(dImporteTotal-dImportePie);
	   	    			   if (sImporteRepa.equals("0,00")) {
	   	    			   		sImporteRepa="";
	   	    			   }
	   	    			   dNumeroS = (rs.getString(11)==null)?"0":rs.getString(11);
	   	    			   sKmEntrada=miComun.formatearDblMiles(new Double(dNumeroS).doubleValue());
	   	    			   if (sKmEntrada.equals("0")) {
	   	    			          sKmEntrada="";
	   	    			   }
	   	    			   sKmEntrada = sKmEntrada.replaceAll(",","."); 
	   	    			   dNumeroS = (rs.getString(12)==null)?"0":rs.getString(12);
	   	    			   sKmSalida=miComun.formatearDblMiles(new Double(dNumeroS).doubleValue());
	   	    			   if (sKmSalida.equals("0")) {
	   	    			       sKmSalida="";
	   	    			   }	
	   	    			   sKmSalida = sKmSalida.replaceAll(",","."); 
	   	    			   if(primeraVez){
	   	    			   		primeraVez=false;
	   	    			   		fncImprimirComienzoBloque(res); 
	   	    			   }else{
	       	    			   if ( ((!(sMatricula.equals(sMatriculaAnt)))) 
	       	    			   		|| ((!(sOrden.equals(sOrdenAnt))))) {	       	    			 
	    			   					sLinea = "	    </fo:table-body>\n";
	       	    			          	tmpFoFileStream.write(sLinea.getBytes());
	       	    			            sLinea= "       </fo:table>\n";
	       	    			           	tmpFoFileStream.write(sLinea.getBytes()); 	
		    			   				miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC,"	IMPRIMIR COMIENZO BLOQUE ", m05bConstantes.LOG_NIVEL_BAJO);	       	    			   		
		       	    			   		fncImprimirComienzoBloque(res); 				       	    			           	
	       	    			   } else {
	       	    			   		sLinea = "	    <fo:table-row >\n";
	       	    			   		tmpFoFileStream.write(sLinea.getBytes());
			            	   }
	   	    			   }
		   
		    			    	// Cod Reparacion
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sCodRepa+"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Concepto 
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"left\">\n <![CDATA[" + sConcepto +"]]></fo:block>";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Empleado
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n <![CDATA["+ sEmpleado +"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Horas
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sHoras +"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
		            			// KmEntrada
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sKmEntrada +"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
		            			// KmSalida
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sKmSalida +"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());		            			
	        			       // Imp. Reparacion
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sImporteRepa +"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());			            			
		            			// Imp. Piezas
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sImportePie+"]]></fo:block>\n";
						            tmpFoFileStream.write(sLinea.getBytes());
						            
		            			sLinea = "	    </fo:table-cell>\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Total
		            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
						            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sImporteTotal+"]]></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());
		            		
	    			   		//cont=cont+1;  
		            		sMatriculaAnt = sMatricula;
		            		sOrdenAnt = sOrden;				            
		        }
	   		  	if(primeraVez){
		   		  	sLinea= "        <fo:table space-after.optimum=\"20pt\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
	   		  	    sLinea = "      <fo:table-body font-family=\"Helvetica\" font-size=\"6pt\">";
			        tmpFoFileStream.write(sLinea.getBytes());
	   		  		sLinea = "	    <fo:table-row>\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();

         miInforme.generarPDFdesdeFO(response);
      }
      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 (pstmt != null)
					pstmt.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);
				}
	  		}
	   }
    } 
	
}