/*
 * 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 com.itextpdf.text.Chunk;
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;
import java.lang.String;

/**
 *
 * @author Edson
 */
public class relatBalancete 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(new float[]  {2, 4, 1, 1, 1, 1});
                table.setWidthPercentage(100f);
                table.getDefaultCell().setUseAscender(true);
                table.getDefaultCell().setUseDescender(true);

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

                balanceteGS bal = new balanceteGS();
                bal.listaBalancete();
                ResultSet lista = bal.getResultado();
                if(lista.next()){
                    lista.last();
                    PdfPCell cel = new PdfPCell(new Phrase(lista.getString("empresa"),fontb));
                    cel.setColspan(10);
                    cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel);

                    PdfPCell cel1 = new PdfPCell(new Phrase("Balancete de Verificação",fontb));
                    cel1.setColspan(10);
                    cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel1);

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

                    PdfPCell cel2 = new PdfPCell(new Phrase("Intervalo Entre Datas: de " +
                            dt.getBras() + " até " + dtf.getBras() ,font));
                    cel2.setColspan(10);
                    cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel2);

                    PdfPCell cel3 = new PdfPCell(new Phrase("Conta",fontb));
                    cel3.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel3);

                    PdfPCell cel4 = new PdfPCell(new Phrase("Descrição",font));
                    cel4.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel4);

                    PdfPCell cel5 = new PdfPCell(new Phrase("Saldo Inicial",font));
                    cel5.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel5);

                    PdfPCell cel6 = new PdfPCell(new Phrase("Debito",font));
                    cel6.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel6);

                    PdfPCell cel7 = new PdfPCell(new Phrase("Credito",font));
                    cel7.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel7);

                    PdfPCell cel8 = new PdfPCell(new Phrase("Saldo Final",font));
                    cel8.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel8);

                }
                doc.add(table);

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

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

                balanceteGS cts = new balanceteGS();
                cts.listaBalancete();
                ResultSet liscon = bal.getResultado();
                while(liscon.next()){
                    int niv = liscon.getString("conta").length();
                    if(niv < 6){
                        PdfPCell cel3 = new PdfPCell(new Phrase(liscon.getString("conta"),fontb));
                        cel3.setHorizontalAlignment(Element.ALIGN_LEFT);
                        tab.addCell(cel3);

                        PdfPCell cel4 = new PdfPCell(new Phrase(liscon.getString("descricao"),fontb));
                        cel4.setHorizontalAlignment(Element.ALIGN_LEFT);
                        tab.addCell(cel4);

                        PdfPCell cel5 = new PdfPCell(new Phrase(df.format(liscon.getDouble("salini")),fontb));
                        cel5.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel5);

                        PdfPCell cel6 = new PdfPCell(new Phrase(df.format(liscon.getDouble("debito")),fontb));
                        cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel6);

                        PdfPCell cel7 = new PdfPCell(new Phrase(df.format(liscon.getDouble("credito")),fontb));
                        cel7.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel7);

                        PdfPCell cel8 = new PdfPCell(new Phrase(df.format(liscon.getDouble("salfin")),fontb));
                        cel8.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel8);
                    }else{
                        PdfPCell cel3 = new PdfPCell(new Phrase(liscon.getString("conta"),font));
                        cel3.setHorizontalAlignment(Element.ALIGN_LEFT);
                        tab.addCell(cel3);

                        PdfPCell cel4 = new PdfPCell(new Phrase(liscon.getString("descricao"),font));
                        cel4.setHorizontalAlignment(Element.ALIGN_LEFT);
                        tab.addCell(cel4);

                        PdfPCell cel5 = new PdfPCell(new Phrase(df.format(liscon.getDouble("salini")),font));
                        cel5.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel5);

                        PdfPCell cel6 = new PdfPCell(new Phrase(df.format(liscon.getDouble("debito")),font));
                        cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel6);

                        PdfPCell cel7 = new PdfPCell(new Phrase(df.format(liscon.getDouble("credito")),font));
                        cel7.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel7);

                        PdfPCell cel8 = new PdfPCell(new Phrase(df.format(liscon.getDouble("salfin")),font));
                        cel8.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        tab.addCell(cel8);
                    }
                    
                }
                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();
        }
    }

}
