package gastostarjetaServlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
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.ServletOutputStream;
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.m05bFileLog;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.xml.sax.InputSource;



public class m05bImpGastosAnualesServlet extends javax.servlet.http.HttpServlet
{
		java.sql.Connection conn=null;
	private static final int REDONDEO  = 	BigDecimal.ROUND_HALF_UP;
	private static final int DECIMALES = 	2;
	m05bFileLog miLog = new m05bFileLog();
	
   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);
//        }

   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;
	   m05bComun miComun = new m05bComun();
	   
      try
      {  HttpSession session = request.getSession(true);
			m05bConexion miConex = new m05bConexion();
			conn = miConex.getConnection();
		
			/*Iker. idioma*/
			Locale idiomaLocal = (Locale)session.getAttribute("Local");
	
			ResourceBundle res =  (ResourceBundle)session.getAttribute("resTit");
			String idiomaSes = ((idiomaLocal.getLanguage()==null)?"es":idiomaLocal.getLanguage());	
			String campoTexto="C";
			if(!idiomaSes.equals("es")){
				campoTexto="E";
				
			}
      	 // Obtención de información de BD
      	 	 	// Vehículo
      	 double [] Meses =  new double[13];
         BigDecimal [] BdMeses = new BigDecimal[13];

         BdMeses[0] = new BigDecimal(0);          	 
         BdMeses[1] = new BigDecimal(0);          	 
         BdMeses[2] = new BigDecimal(0);          	 
         BdMeses[3] = new BigDecimal(0);          	 
         BdMeses[4] = new BigDecimal(0);          	 
         BdMeses[5] = new BigDecimal(0);          	 
         BdMeses[6] = new BigDecimal(0);          	 
         BdMeses[7] = new BigDecimal(0);          	 
         BdMeses[8] = new BigDecimal(0);          	 
         BdMeses[9] = new BigDecimal(0);          	 
         BdMeses[10] = new BigDecimal(0);          	 
         BdMeses[11] = new BigDecimal(0);          	 
         BdMeses[12] = new BigDecimal(0);          	 
         
      	 double [] Totales =  new double [13];
      	 BigDecimal [] BdTotales =  new BigDecimal [13];
         BdTotales[0] = new BigDecimal(0);          	 
         BdTotales[1] = new BigDecimal(0);          	 
         BdTotales[2] = new BigDecimal(0);          	 
         BdTotales[3] = new BigDecimal(0);          	 
         BdTotales[4] = new BigDecimal(0);          	 
         BdTotales[5] = new BigDecimal(0);          	 
         BdTotales[6] = new BigDecimal(0);          	 
         BdTotales[7] = new BigDecimal(0);          	 
         BdTotales[8] = new BigDecimal(0);          	 
         BdTotales[9] = new BigDecimal(0);          	 
         BdTotales[10] = new BigDecimal(0);          	 
         BdTotales[11] = new BigDecimal(0);          	 
         BdTotales[12] = new BigDecimal(0);          	 
      	 
      	 
         String sEjercicio=request.getParameter("Ejercicio");
            	if (sEjercicio==null){
					sEjercicio="";}
	     String sTarjeta = "";
	     String sFechaUltRev= "";
	     String sKmsUltRev= "";
	     String	sFechaPrevisto = "";
	     String sKmsPrevisto= "";
	     String sKmsActuales= "";
	     String sKmsHastaProx= "";
	     String	sDescripcion = "";
	     String	sDiasHastaProx= "";
	     String sMes="";
	     String sTarjetaAnt="";
	     String sImporte="";
	     double dImporte=0;
	     
         BigDecimal	BdImporte  ;
         BdImporte = new BigDecimal(0);
         
	     String sImporteTotal="";
	     double dImporteTotal=0;
	     
         BigDecimal	BdImporteTotal  ;
         BdImporteTotal = new BigDecimal(0);
	     
	     String sMesAnt="";
	     String sImporteMes="";
	     double dImporteMes=0;
	     
         BigDecimal	BdImporteMes  ;
         BdImporteMes = new BigDecimal(0);
	     
	     int iMes=0;
      	 
String sSentencia=" SELECT " +
m05bConstantes.COL_46_TARJETA+","+ 
" DECODE( LENGTH("+
m05bConstantes.COL_46_MES+"),1,'0'||"+
m05bConstantes.COL_46_MES+","+m05bConstantes.COL_46_MES+"),"+
" SUM("+ m05bConstantes.COL_46_IMPORTE+")"+   
" FROM " + 
m05bConstantes.TAB_HIST_OPEBANCOS+
" WHERE " + 
m05bConstantes.COL_46_EJER+"="+ sEjercicio  +
" GROUP BY " + 
m05bConstantes.COL_46_TARJETA+","+
m05bConstantes.COL_46_MES+
" UNION " +
" SELECT " +
 m05bConstantes.COL_18_TARJETA+","+ 
 "TO_CHAR("+m05bConstantes.COL_18_FECHA+",'mm'), SUM("+m05bConstantes.COL_18_IMPORTE+")"+ 
" FROM "+ m05bConstantes.TAB_OPEBANCOS+
" WHERE TO_CHAR("+m05bConstantes.COL_18_FECHA+",'YYYY')="+ sEjercicio+
" GROUP BY "+
m05bConstantes.COL_18_TARJETA+", TO_CHAR("+m05bConstantes.COL_18_FECHA+",'mm')"+
" ORDER BY 1,2,3 ";
         
miLog.addTraza("PO" , "sqlKilometrosServletVehiculos" , "sSentencia: " + sSentencia, m05bConstantes.LOG_NIVEL_BAJO);
		     
		 pstmt = conn.prepareStatement(sSentencia);
		 rs = pstmt.executeQuery();
			 
         //String foParam = request.getParameter(FO_REQUEST_PARAM);
         String sFoFileDir = "/datos/m05b/tmp/";
         File foFileDir = new File(sFoFileDir);
         File tmpFoFile = File.createTempFile("m05b", "Fo", foFileDir);


         FileOutputStream tmpFoFileStream = new FileOutputStream(tmpFoFile);

         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=\"2.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());
		 // 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 = "      </fo:block>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
                    sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " +  res.getString("infGastoAnual.titulo") + sEjercicio+"</fo:block>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        
        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());

            //PIE DE PAGINA Pie
        	sLinea = "		<fo:static-content flow-name=\"xsl-region-after\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
                        //PIE DE PAGINA Pie
					sLinea = "      <fo:block >\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"127mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"125mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"6pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">" + miComun.getDetFechaActual(campoTexto)+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    sLinea = "      <fo:block text-align=\"right\">" ; 
                       		            tmpFoFileStream.write(sLinea.getBytes());
		                                sLinea = res.getString("pagina")+" <fo:page-number/> \n";
                        	            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea=  "</fo:block>";
							            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-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());
//BODY
		         sLinea = "	<fo:flow flow-name=\"xsl-region-body\">\n";
		         tmpFoFileStream.write(sLinea.getBytes());
      	         // --------------    DETALLE DEL LISTADO    --------------
		           		sLinea= "        <fo:table table-layout=\"fixed\" space-after.optimum=\"12pt\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"28mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			                sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"17mm\"/>\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=\"17mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		                    sLinea = "	    <fo:table-header font-family=\"Helvetica\" font-size=\"7pt\" >\n";
			                tmpFoFileStream.write(sLinea.getBytes());
	       	    			    	sLinea = "	    <fo:table-row>\n";
			            		     tmpFoFileStream.write(sLinea.getBytes());
			            			// Tarjeta.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"center\">\n "+res.getString("infGastoAnual.col1") + "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Enero.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col2") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			   // Febrero.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col3") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Marzo
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col4") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Abril
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col5") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Mayo
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col6") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Junio
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col7") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Julio
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col8") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Agosto
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col9") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Septiembre
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col10") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Octubre
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col11") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Noviembre
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col12") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Diciembre
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col12") + "</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\"  text-align=\"center\">\n "+res.getString("infGastoAnual.col14") + "</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());
			            		///DETALLE
			            	sLinea = "	    </fo:table-header>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"8pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	int cont=0;
		   		            while (rs.next()){

	       	    			sTarjeta = rs.getString(1)==null ? "":rs.getString(1);
			   		       //miLog.addTraza("PO" , "sqlKilometrosServletVehiculos" , "sTarjeta: " + sTarjeta, m05bConstantes.LOG_NIVEL_BAJO);		   		             	       	    			
	       	    			sMes= rs.getString(2)==null ? "":rs.getString(2);
	                        iMes = Integer.valueOf(sMes).intValue();
	       	    			iMes=iMes-1;
	       	    			sImporte= rs.getString(3)==null ? "0":rs.getString(3);
				   		       //miLog.addTraza("PO" , "sqlKilometrosServletVehiculos" , "sImporte: " + sImporte, m05bConstantes.LOG_NIVEL_BAJO);		   		             	       	    				       	    			
                            dImporte = Double.valueOf(sImporte).doubleValue();
                            BdImporte = new BigDecimal(sImporte);
	       	    			if (sTarjeta.equals(sTarjetaAnt)||sTarjetaAnt.equals("")){
	       	    				sTarjetaAnt=sTarjeta;
	       	    				dImporteTotal=dImporteTotal+dImporte;
	       	    				BdImporteTotal=BdImporteTotal.add(BdImporte);
	       	    			    Meses[12]=dImporteTotal;
	       	    			    BdMeses[12] = BdImporteTotal;
	       	    				if ((sMes.equals(sMesAnt))||sMesAnt.equals("")){
	       	    					dImporteMes=dImporteMes+dImporte;
	       	    					BdImporteMes=BdImporteMes.add(BdImporte);
	       	    				    Meses[iMes]=dImporteMes;
	       	    				    BdMeses[iMes]=BdImporteMes;
	       	    				    
	       	    					sMesAnt=sMes;
	       	    					}
	       	    				else {
	       	    					dImporteMes=dImporte;
	       	    					BdImporteMes=BdImporte;
	       	    					Meses[iMes]=dImporteMes;
	       	    				    BdMeses[iMes]=BdImporteMes;
	       	    				    
	       	    					sMesAnt=sMes;}
	       	    			}
	       	    		    else {
	                        	
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Tarjeta. 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n " + sTarjetaAnt +"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		
			            			// 1. 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[0]=Totales[0]+Meses[0];
			            			    BdTotales[0]=BdTotales[0].add(BdMeses[0]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n " + miComun.formatearDblMilesDec(BdMeses[0].doubleValue()) +"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// 2.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[1]=Totales[1]+Meses[1];
			            			    BdTotales[1]=BdTotales[1].add(BdMeses[1]);
							            sLinea = "      <fo:block  text-align=\"center\">\n "+ miComun.formatearDblMilesDec(BdMeses[1].doubleValue()) +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// 3.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[2]=Totales[2]+Meses[2];
			            			    BdTotales[2]=BdTotales[2].add(BdMeses[2]);
			            			    sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[2].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
            			            // 4.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[3]=Totales[3]+Meses[3];
			            			    BdTotales[3]=BdTotales[3].add(BdMeses[3]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[3].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
            			            // 5
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[4]=Totales[4]+Meses[4];
			            			    BdTotales[4]=BdTotales[4].add(BdMeses[4]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[4].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 6
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[5]=Totales[5]+Meses[5];
			            			    BdTotales[5]=BdTotales[5].add(BdMeses[5]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[5].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 7
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[6]=Totales[6]+Meses[6];
			            			    BdTotales[6]=BdTotales[6].add(BdMeses[6]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[6].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 8.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[7]=Totales[7]+Meses[7];
			            			    BdTotales[7]=BdTotales[7].add(BdMeses[7]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[7].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 9.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[8]=Totales[8]+Meses[8];
			            			    BdTotales[8]=BdTotales[8].add(BdMeses[8]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[8].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			                        // 10.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[9]=Totales[9]+Meses[9];
			            			    BdTotales[9]=BdTotales[9].add(BdMeses[9]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[9].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
                                    // 11.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[10]=Totales[10]+Meses[10];
			            			    BdTotales[10]=BdTotales[10].add(BdMeses[10]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[10].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//12.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[11]=Totales[11]+Meses[11];
			            			    BdTotales[11]=BdTotales[11].add(BdMeses[11]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[11].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//13.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[12]=Totales[12]+Meses[12];
			            			    BdTotales[12]=BdTotales[12].add(BdMeses[12]);
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[12].doubleValue())+"</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());
                                   Meses[0] = 0;          	 
                                   Meses[1] = 0;          	 
                                   Meses[2] = 0;          	 
                                   Meses[3] = 0;          	 
                                   Meses[4] = 0;          	 
                                   Meses[5] = 0;          	 
                                   Meses[6] = 0;          	 
                                   Meses[7] = 0;          	 
                                   Meses[8] = 0;          	 
                                   Meses[9] = 0;          	 
                                   Meses[10] = 0;          	 
                                   Meses[11] = 0;          	 
                                   Meses[12] = 0;          	 

                                   BdMeses[0] = new BigDecimal(0);          	 
                                   BdMeses[1] = new BigDecimal(0);          	 
                                   BdMeses[2] = new BigDecimal(0);          	 
                                   BdMeses[3] = new BigDecimal(0);          	 
                                   BdMeses[4] = new BigDecimal(0);          	 
                                   BdMeses[5] = new BigDecimal(0);          	 
                                   BdMeses[6] = new BigDecimal(0);          	 
                                   BdMeses[7] = new BigDecimal(0);          	 
                                   BdMeses[8] = new BigDecimal(0);          	 
                                   BdMeses[9] = new BigDecimal(0);          	 
                                   BdMeses[10] = new BigDecimal(0);          	 
                                   BdMeses[11] = new BigDecimal(0);          	 
                                   BdMeses[12] = new BigDecimal(0);          	 
                                   
			        	       	   Meses[12]=dImporte;
			        	       	   BdMeses[12]=BdImporte;
			        	       	   
	       	    				   Meses[iMes]=dImporte;
	       	    				   BdMeses[iMes]=BdImporte;
	       	    				   
	       	    				   dImporteTotal=dImporte;
	       	    				   BdImporteTotal=BdImporte;
	       	    				   
	       	    				   dImporteMes=dImporte;
	       	    				   BdImporteMes=BdImporte;
	       	    				   
	       	    				   sMesAnt=sMes;
	       	    				   
	       	    				   sTarjetaAnt=sTarjeta;

			       		} }
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Tarjeta. 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n " + sTarjetaAnt +"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		
			            			// 1. 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[0]=Totales[0]+Meses[0];
			            			    BdTotales[0]=BdTotales[0].add(BdMeses[0]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n " + miComun.formatearDblMilesDec(BdMeses[0].doubleValue()) +"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// 2.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[1]=Totales[1]+Meses[1];
			            			    BdTotales[1]=BdTotales[1].add(BdMeses[1]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n "+ miComun.formatearDblMilesDec(BdMeses[1].doubleValue()) +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// 3.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[2]=Totales[2]+Meses[2];
			            			    BdTotales[2]=BdTotales[2].add(BdMeses[2]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[2].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
            			            // 4.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[3]=Totales[3]+Meses[3];
			            			    BdTotales[3]=BdTotales[3].add(BdMeses[3]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[3].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
            			            // 5
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[4]=Totales[4]+Meses[4];
			            			    BdTotales[4]=BdTotales[4].add(BdMeses[4]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[4].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 6
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[5]=Totales[5]+Meses[5];
			            			    BdTotales[5]=BdTotales[5].add(BdMeses[5]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[5].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 7
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[6]=Totales[6]+Meses[6];
			            			    BdTotales[6]=BdTotales[6].add(BdMeses[6]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[6].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 8.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[7]=Totales[7]+Meses[7];
			            			    BdTotales[7]=BdTotales[7].add(BdMeses[7]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[7].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 9.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[8]=Totales[8]+Meses[8];
			            			    BdTotales[8]=BdTotales[8].add(BdMeses[8]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[8].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			                        // 10.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[9]=Totales[9]+Meses[9];
			            			    BdTotales[9]=BdTotales[9].add(BdMeses[9]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[9].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
                                    // 11.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[10]=Totales[10]+Meses[10];
			            			    BdTotales[10]=BdTotales[10].add(BdMeses[10]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[10].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//12.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[11]=Totales[11]+Meses[11];
			            			    BdTotales[11]=BdTotales[11].add(BdMeses[11]);
			            			    
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[11].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//13.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    Totales[12]=Totales[12]+Meses[12];
			            			    BdTotales[12]=BdTotales[12].add(BdMeses[12]);
			            			    
			            			    sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdMeses[12].doubleValue())+"</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());
			            			// Tarjeta. 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n " + "TOTAL" +"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		
			            			// 1. 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n " + miComun.formatearDblMilesDec(BdTotales[0].doubleValue()) +"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// 2.
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"2mm\" text-align=\"center\">\n "+ miComun.formatearDblMilesDec(BdTotales[1].doubleValue()) +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// 3.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[2].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
            			            // 4.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[3].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
            			            // 5
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[4].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 6
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[5].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 7
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[6].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 8.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[7].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
            			            // 9.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[8].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			                        // 10.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[9].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
                                    // 11.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[10].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//12.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[11].doubleValue())+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//13.
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			    sLinea = "      <fo:block  text-align=\"center\">\n"+miComun.formatearDblMilesDec(BdTotales[12].doubleValue())+"</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());
			                   		rs.close();
			 	      	    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:page-sequence>\n";
             tmpFoFileStream.write(sLinea.getBytes());
             
         sLinea = "</fo:root>\n";
         tmpFoFileStream.write(sLinea.getBytes());
         

         tmpFoFileStream.close();

         //FileInputStream file = new FileInputStream(sFoFileDir + tmpFoFile.getName());

         //renderFO(new InputSource(file), response);
          generarPDFdesdeFO(sFoFileDir + tmpFoFile.getName(),sFoFileDir,response);
         //tmpFoFile.delete();

         //response.flushBuffer();


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

    /**
     * renders an FO inputsource into a PDF file which is rendered
     * directly to the response object's OutputStream
     */
    public void renderFO(InputSource foFile, HttpServletResponse response) throws ServletException
    {
        try
        {
            ByteArrayOutputStream out = new ByteArrayOutputStream();

	    	response.reset();
            OutputStream oOut=response.getOutputStream();
            BufferedOutputStream bOut=new BufferedOutputStream(oOut);
            Driver driver = new Driver(foFile, out);
            driver.setRenderer(Driver.RENDER_PDF);
            driver.run();

            response.setContentLength(out.size());
	    response.setContentType("application/pdf");


            bOut.write(out.toByteArray());
	    out.close();
	    bOut.flush();
            bOut.close();
//	    response.flushBuffer();
        }
        catch (Exception ex)
        {
            throw new ServletException(ex);
        }
    }
public void generarPDFdesdeFO(String ficheroFO, String rutaPDF, HttpServletResponse response) throws FOPException {
        try {

            Driver driver = new Driver();

            driver.setRenderer(Driver.RENDER_PDF);

            //Si no existe el directrio lo crea
            File f = new File(rutaPDF);

            if (!f.exists()) {

                f.mkdir();


            }
            //Cargar los datos del .FO
            FileInputStream file = new FileInputStream(ficheroFO);

            BufferedInputStream bfile=new BufferedInputStream(file);

            driver.setInputSource(new InputSource(bfile));


            //Crear un temporal que va a ser el .pdf
            String sPdfFileDir = rutaPDF;
            File   pdfFileDir = new File(sPdfFileDir);
            File   tmpPdfFile = File.createTempFile("m05b", ".pdf", pdfFileDir);


            FileOutputStream fileout = new FileOutputStream(tmpPdfFile);

            BufferedOutputStream bfileout=new BufferedOutputStream(fileout);

            driver.setOutputStream(bfileout);

            driver.run();


 

            bfile.close();

            bfileout.close();
            //Longitus del Fichero PDF.
            Long longitud = new Long(tmpPdfFile.length());            
            String sLongitud=""+ longitud;
            int iLongitud=Integer.parseInt(sLongitud);

            response.setContentLength((int)iLongitud);
	        response.setContentType("application/pdf");
	        
            byte[] fileBytes = new byte[longitud.intValue()];
            FileInputStream fisficheroPdf = new FileInputStream(tmpPdfFile);
            fisficheroPdf.read(fileBytes);
            ServletOutputStream os = response.getOutputStream();
            ((OutputStream)os).write(fileBytes);
            ((OutputStream)os).flush();
            ((OutputStream)os).close();

            // BORRAR EL FICHERO FO

            File fichero = new File(ficheroFO);
            

            boolean borrado = fichero.delete();
            boolean borrado2 = tmpPdfFile.delete();

        } catch (Exception e) {
            System.err.println("EXCEPCION !!! : Error pasando de FO a PDF: " + e);
        }


    }    
}