package tarjetasServlet;

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

/**
 * Inserte aquí la descripción del tipo.
 * Fecha de creación: (21/09/05)
 */
    
public class m05bImpDuplicCajasServlet extends javax.servlet.http.HttpServlet
{
	     public static final String FO_REQUEST_PARAM = "fo";
			
	     String sLinea;
	     File foFileDir ;
	     File tmpFoFile ;
		 FileOutputStream tmpFoFileStream ;	     

		 java.sql.Connection conn=null;
		 m05bConexion miConex = new m05bConexion();
		 m05bComun miComun = new m05bComun();
		 m05bFileLog miLog = new m05bFileLog();
		 m05bInformes miInforme = new m05bInformes();

   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);
   }

   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;
      String sTarjeta="", sTarjetaAnt ="", sFecha="", sFechaAnt="", sHora="", sHoraAnt="", 
	  		sUEconomica = "", sCentro= "", sSentencia;
      String sMEGE="", sMatricula="", sTipo = "", sConcepto= "", sCIF="";
      String sNumComercio="", sDescripcion="", sImporte = "", sImporteAnt = "", sCargabon= "";
	  String persona = "";
	  double dNumReg=0.0 ;

      try {  
      	  
      	 HttpSession session = request.getSession(true);
	     persona = (String) session.getAttribute("persona");
		 miLog.addTraza(persona, "ImpDuplicCajas Servlet", "INICIO", m05bConstantes.LOG_NIVEL_BAJO);

		 conn = miConex.getConnection();
      	 
		 sSentencia=" SELECT " + m05bConstantes.COL_16_TARJETA + " ," +
		 	 			m05bConstantes.COL_16_FECHA + ", " + m05bConstantes.COL_16_HORA + ", " +
						m05bConstantes.COL_16_UCONOMICA + ", " + m05bConstantes.COL_16_CENTRO + "," +
						m05bConstantes.COL_16_MEGE + ", " + m05bConstantes.COL_16_MATRICULA + "," +
						m05bConstantes.COL_16_CONCEPTO + ", " + m05bConstantes.COL_16_CIF + "," +
						m05bConstantes.COL_16_TIPO  + "," +
						m05bConstantes.COL_16_COMERCIO + ", " + m05bConstantes.COL_16_DESCRIPCION + "," +
						m05bConstantes.COL_16_IMPORTE + ", " + m05bConstantes.COL_16_CARG_ABON + "" +
						" FROM "+ m05bConstantes.TAB_KUTXAK +
						" WHERE " + m05bConstantes.COL_16_MATRICULA + "||" + m05bConstantes.COL_16_FECHA + "||" +
									m05bConstantes.COL_16_HORA + "||" + m05bConstantes.COL_16_IMPORTE + " IN (" +
							" SELECT " + m05bConstantes.COL_16_MATRICULA + "||" + m05bConstantes.COL_16_FECHA + "||" +
									m05bConstantes.COL_16_HORA + "||" + m05bConstantes.COL_16_IMPORTE +
									" FROM "+ m05bConstantes.TAB_KUTXAK +
									" GROUP BY  " + m05bConstantes.COL_16_MATRICULA + ", " +
										m05bConstantes.COL_16_FECHA + ", " + m05bConstantes.COL_16_HORA + ", " + 
										m05bConstantes.COL_16_IMPORTE +
									" HAVING COUNT(" + m05bConstantes.COL_16_MATRICULA+ ")>1)" +
						" ORDER BY 1,2 ";
         miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Duplicados SQL: " + sSentencia, m05bConstantes.LOG_NIVEL_BAJO);
		 pstmt = conn.prepareStatement(sSentencia);
		 rs = pstmt.executeQuery();
			 
         tmpFoFileStream=miInforme.fncAbrirFicheroTemporal();
         miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Abrir fichero temporal", m05bConstantes.LOG_NIVEL_BAJO);
         String sLinea;
         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=\"21cm\" page-width=\"29.5cm\" margin-top=\"2cm\" margin-bottom=\"2cm\" margin-left=\"2cm\" margin-right=\"2cm\">\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=\"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());

		 // PAGE-SEQUENCE DEL LISTADO 
	         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 = "p. <fo:page-number/>\n";
			            tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block>\n";
		            tmpFoFileStream.write(sLinea.getBytes());*/
                    sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " + "INFORME DUPLICADOS KUTXAK" + "</fo:block>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());

            //PIE DE PAGINA
		    miInforme.fncGenerarPiePagina(tmpFoFileStream, m05bConstantesInformes.DIR_HORIZONTAL); 
		    miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Generar pie pagina", m05bConstantes.LOG_NIVEL_BAJO);
			//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=\"27mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"18mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            	
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"12mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            	
		            		sLinea = "	    <fo:table-column column-width=\"12mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"12mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"12mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"22mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"40mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"19mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());

		                    /*sLinea = "	    <fo:table-header font-family=\"Helvetica\" font-size=\"7pt\" >\n";
			                tmpFoFileStream.write(sLinea.getBytes());
			                sLinea = "	    </fo:table-header>\n";
			                tmpFoFileStream.write(sLinea.getBytes());*/
			            		///DETALLE
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"8pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Tarjeta 
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Tarjeta </fo:block>";
				 			            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Matricula
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Matrícula </fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			            			// Fecha Mvto.
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Fecha </fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Hora Mvto.
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Hora </fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// U. Economica
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Importe </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Centro
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Centro </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Codigo MEGE 
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Código MEGE </fo:block>";
				 			            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tipo Mvto
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Tipo Mvto. </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Concepto
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\" font-weight=\"bold\">\n Concepto </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// CIF 
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\" font-weight=\"bold\">\n CIF </fo:block>";
				 			            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Num. Comercio
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\" font-weight=\"bold\">\n Núm.\nComercio </fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Descripcion
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\" font-weight=\"bold\">\n Descripción </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Importe
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\" font-weight=\"bold\">\n U. Económica </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Cargo/Abono
			            			sLinea = "	    <fo:table-cell padding-before=\"1mm\" padding-after=\"0.6mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\" font-weight=\"bold\">\n Cargo/Abono </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());
			            	int cont=0;
			            	miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Antes while", m05bConstantes.LOG_NIVEL_BAJO);
		   		            while (rs.next()) {
			   		    	    sTarjeta = rs.getString(1)==null ? "":rs.getString(1);
			   		    	    sFecha = rs.getString(2)==null ? "":rs.getString(2);
			   		    	    sFecha=sFecha.substring(6,8)+"/"+sFecha.substring(4,6)+"/"+sFecha.substring(0,4);
			   		    	    sHora = rs.getString(3)==null ? "":rs.getString(3);
			   		    	    sHora=sHora.substring(0,2)+":"+sHora.substring(2,4);
		       	    			sUEconomica = rs.getString(4)==null ? "":rs.getString(4);
		       	    			sCentro = rs.getString(5)==null ? "":rs.getString(5);
			   		    	    sMEGE = rs.getString(6)==null ? "":rs.getString(6);
			   		    	    sMatricula = rs.getString(7)==null ? "":rs.getString(7);
		       	    			sConcepto = rs.getString(8)==null ? "":rs.getString(8);
		       	    			sCIF = rs.getString(9)==null ? "":rs.getString(9);
		       	    			sTipo = rs.getString(10)==null ? "":rs.getString(10);
		       	    			sNumComercio = rs.getString(11)==null ? "":rs.getString(11);
		       	    			sDescripcion = rs.getString(12)==null ? "":rs.getString(12);
		       	    			sImporte = rs.getString(13)==null ? "":rs.getString(13);
		       	    			// Poner separador de decimales

		       	    			if (sImporte.substring(0,1).equals("."))  {
	       	    					sImporte="0"+sImporte;
		       	                			       	                
		       	                }
		       	    			if (((sImporte.indexOf(","))==-1) && ((sImporte.indexOf("."))==-1)){
		       	    				sImporte=sImporte+".00";
		       	    			}
		       	    			if ((sImporte.length()==3) && ((sImporte.substring(sImporte.indexOf("."),sImporte.length())).length()==2)) {
		       	    				sImporte=sImporte+"0";
		       	    			}
		       	    			sImporte=sImporte.substring(0,sImporte.length()-3) + "."+ sImporte.substring(sImporte.length()-2,sImporte.length());
		       	    			while (sImporte.length() > 0)  {
		       	    				//Quitar ceros a la izda
		       	    				if (sImporte.substring(0,1).equals("0")) {
		       	    					sImporte=sImporte.substring(1,sImporte.length());
		       	    					break;		       	    					
		       	    				}  else if (sImporte.substring(0,1).equals(","))  {
		       	    					sImporte="0"+sImporte;
		       	    					break;
	       	    					}  else {
	       	    						break;
	       	    					}
		       	    		    }		       	    			
		       	    			sCargabon = rs.getString(14)==null ? "":rs.getString(14);
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		if (   (sTarjeta.equals(sTarjetaAnt)) && (sFecha.equals(sFechaAnt)) && 
			            				(sHora.equals(sHoraAnt)) && (sImporte.equals(sImporteAnt))  )	{
			            			// Tarjeta 			            		 	
				            		sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n </fo:block>";
				 			            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());
				            		// Matricula				            			
				            		sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
				            		tmpFoFileStream.write(sLinea.getBytes());
									        sLinea = "      <fo:block text-align=\"center\">\n </fo:block>";
					 			            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());
			            			// Fecha 			            		 	
				            		sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n </fo:block>";
				 			            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());
				            		// Hora				            			
				            		sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
				            		tmpFoFileStream.write(sLinea.getBytes());
									        sLinea = "      <fo:block text-align=\"center\">\n </fo:block>";
					 			            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());			
				            		// Importe				            			
				            		sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
				            		tmpFoFileStream.write(sLinea.getBytes());
									        sLinea = "      <fo:block text-align=\"center\">\n </fo:block>";
					 			            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());						            	
								} else {
			            			// Tarjeta 									
				            		sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n" + sTarjeta + "</fo:block>";
				 			            tmpFoFileStream.write(sLinea.getBytes());
				            		sLinea = "	    </fo:table-cell>\n";
				            		tmpFoFileStream.write(sLinea.getBytes());	
			            			// Matricula
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n "+ sMatricula +"</fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
			            			// Fecha Mvto.
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n "+ sFecha +"</fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
								    sLinea = "	    </fo:table-cell>\n";
								    tmpFoFileStream.write(sLinea.getBytes());
								    // Hora Mvto.
								    sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
								        tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n "+ sHora +"</fo:block>\n";
			            				tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
			            			// Importe
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">\n"+ sImporte +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
								}
			            			// Centro
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">\n"+sCentro +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Codigo MEGE 
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n " + sMEGE + "</fo:block>";
				 			            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tipo Mvto.
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">\n"+ sTipo +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Concepto
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">\n"+sConcepto +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// CIF 
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n " + sCIF + "</fo:block>";
				 			            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Num Comercio
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
								        sLinea = "      <fo:block text-align=\"center\">\n "+ sNumComercio +"</fo:block>\n";
								        tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Descripcion
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">\n"+ sDescripcion +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// U. Economica
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">\n"+ sUEconomica +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			            			// Cargo/Abono
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">\n"+ sCargabon +"</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());
								sTarjetaAnt=sTarjeta;
								sFechaAnt=sFecha;	
								sHoraAnt=sHora;
								sImporteAnt=sImporte;									
								} // Fin bucle while
			            	miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Despues while", m05bConstantes.LOG_NIVEL_BAJO);		   		            
			 	      	    sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		          //  sLinea = "      </fo:block-container>\n";
		           // tmpFoFileStream.write(sLinea.getBytes());

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

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

         tmpFoFileStream.close();
         miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Antes render", m05bConstantes.LOG_NIVEL_BAJO);
         miInforme.generarPDFdesdeFO(response);
         miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Despues render", m05bConstantes.LOG_NIVEL_BAJO);

      } catch (Exception ex) {
      		miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "EXCEPTION: "+ex, m05bConstantes.LOG_NIVEL_BAJO);      	
      		try {
				response.sendRedirect("m05bError.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				miLog.addTraza(e);
			}
      } finally{
      		miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "FINALLY", m05bConstantes.LOG_NIVEL_BAJO);      	
	   	    try {
				if (rs != null)
        			rs.close();
	   	    	if (pstmt != null)
					pstmt.close();
	   	    	if (conn != null)
					conn.close();
			} catch (SQLException e2) {
				miLog.addTraza(m05bConstantes.LOG_NO_USER, m05bConstantes.LOG_NO_SRC, "Exception: OutletMaintenanceS.service: " + e2, m05bConstantes.LOG_NIVEL_BAJO);
				try {
					response.sendRedirect("m05bError.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					miLog.addTraza(e);
				}
			}
	   }
    }  // Fin funcion performTask

}