/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ddns.grupotsergio;

import com.itextpdf.text.BaseColor;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.ByteArrayOutputStream;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.Font.FontFamily;
import java.text.DecimalFormat;

/**
 *
 * @author Edson
 */
public class relatExtrato extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        String acao = req.getParameter("acao");
        if(acao.equals("Imprimir")){
            PdfWriter wri = null;
            Document doc = null;

            try{
                doc = new Document(PageSize.A4);
                wri = PdfWriter.getInstance(doc, baos);
                doc.open();

                PdfPTable table = new PdfPTable(2);
                table.setWidthPercentage(100f);
                table.getDefaultCell().setUseAscender(true);
                table.getDefaultCell().setUseDescender(true);

                Font font = new Font(FontFamily.HELVETICA, 8, Font.NORMAL, BaseColor.BLACK);
                Font fontb = new Font(FontFamily.HELVETICA, 8, Font.BOLD, BaseColor.BLACK);

                lancaccGS extr = new lancaccGS();
                extr.ImprimeRelat();
                ResultSet lista = extr.getResultado();
                if(lista.next()){
//                    lista.last();
                    PdfPCell cel = new PdfPCell(new Phrase("Extrato Bancário",font));
                    cel.setColspan(2);
                    cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel);
                    PdfPCell cel1 = new PdfPCell(new Phrase("Banco",font));
                    cel1.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel1);
                    PdfPCell cel2 = new PdfPCell(new Phrase(lista.getString("banco") + " - " +
                        lista.getString("nrobanco"),font));
                    cel2.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel2);

                    PdfPCell cel3 = new PdfPCell(new Phrase("Agência",font));
                    cel3.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel3);
                    PdfPCell cel4 = new PdfPCell(new Phrase(lista.getString("agencia") + " - " +
                        lista.getString("nroagencia"),font));
                    cel4.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel4);

                    PdfPCell cel5 = new PdfPCell(new Phrase("Conta Corremte",font));
                    cel5.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel5);
                    PdfPCell cel6 = new PdfPCell(new Phrase(lista.getString("cc"),font));
                    cel6.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel6);

                    DataMy_Bra dt = new DataMy_Bra();
                    dt.DataMy_Bra(lista.getString("dtin1"));
                    DataMy_Bra dtf = new DataMy_Bra();
                    dtf.DataMy_Bra(lista.getString("dtfin"));

                    PdfPCell cel7 = new PdfPCell(new Phrase("Intervalo do Extrato de: " +
                        dt.bras + " á " + dtf.bras,font));
                    cel7.setColspan(2);
                    cel7.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel7);
                    
                    PdfPCell cel8 = new PdfPCell(new Phrase("Saldo Inicial " +
                        lista.getString("salfin"),font));
                    cel8.setColspan(2);
                    cel8.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel8);
                    
                }
                doc.add(table);

                PdfPTable tab = new PdfPTable(new float[]  {1, 1, 4, 1, 1, 1});
                tab.setWidthPercentage(100f);
                tab.getDefaultCell().setUseAscender(true);
                tab.getDefaultCell().setUseDescender(true);

                PdfPCell cel = new PdfPCell(new Phrase("Data",font));
                cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel);
                PdfPCell cel1 = new PdfPCell(new Phrase("Doc.",font));
                cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel1);
                PdfPCell cel2 = new PdfPCell(new Phrase("Histórico",font));
                cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel2);
                PdfPCell cel3 = new PdfPCell(new Phrase("Crédito",font));
                cel3.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel3);
                PdfPCell cel4 = new PdfPCell(new Phrase("Débito",font));
                cel4.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel4);
                PdfPCell cel5 = new PdfPCell(new Phrase("Saldo",font));
                cel5.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel5);

                lancaccGS extr1 = new lancaccGS();
                extr1.ImprimeRelat();
                ResultSet list = extr1.getResultado();
                list.first();
                while(list.next()){
                    DataMy_Bra dtl = new DataMy_Bra();
                    dtl.DataMy_Bra(list.getString("dtlanca"));
                    PdfPCell cel6 = new PdfPCell(new Phrase(dtl.bras ,font));
                    cel6.setHorizontalAlignment(Element.ALIGN_LEFT);
                    tab.addCell(cel6);

                    PdfPCell cel7 = new PdfPCell(new Phrase(list.getString("doc"),font));
                    cel7.setHorizontalAlignment(Element.ALIGN_LEFT);
                    tab.addCell(cel7);

                    PdfPCell cel8 = new PdfPCell(new Phrase(list.getString("historico"),font));
                    cel8.setHorizontalAlignment(Element.ALIGN_LEFT);
                    tab.addCell(cel8);

                    DecimalFormat dfc = new DecimalFormat();
                    dfc.applyPattern("###,##0.00");

                    if(list.getDouble("credito") != 0){
                        PdfPCell cel9 = new PdfPCell(new Phrase(dfc.format(list.getDouble("credito")),font));
                        cel9.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel9);
                    }else{
                        tab.addCell("");
                    }

                    if(list.getDouble("debito") != 0){
                        PdfPCell cel10 = new PdfPCell(new Phrase(dfc.format(list.getDouble("debito")),font));
                        cel10.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel10);
                    }else{
                        tab.addCell("");
                    }

                    PdfPCell cel11 = new PdfPCell(new Phrase(dfc.format(list.getDouble("salfin")),font));
                    cel11.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    tab.addCell(cel11);
                }

                doc.add(tab);

                doc.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            res.setContentType("application/pdf");
            res.setContentLength(baos.size());
            ServletOutputStream out = res.getOutputStream();
            baos.writeTo(out);
            out.flush();
        }
    }

}
