/*
 * 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.Font.FontFamily;
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 java.text.DecimalFormat;

/**
 *
 * @author Edson
 */
public class relatCtsPagar 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;
            DataMy_Bra dd = new DataMy_Bra();
            DataMy_Bra ddf = new DataMy_Bra();

            doc = new Document(PageSize.A4);

            PdfPTable table = new PdfPTable(new float[]  {1, 1, 6, 1, 1});

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

            try{
                wri = PdfWriter.getInstance(doc, baos);

                doc.open();

                parcelascomprasGS compra = new parcelascomprasGS();
                compra.listaCtsPg();
                ResultSet lista = compra.getResultado();
                if(lista.next()){
                    PdfPCell cel = new PdfPCell(new Phrase(lista.getString("razao_social"),font));
                    cel.setColspan(5);
                    cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel);
                    PdfPCell cel2 = new PdfPCell(new Phrase("Relatório do Contas a Pagar",font));
                    cel2.setColspan(5);
                    cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel2);

                    dd.DataMy_Bra(lista.getString("dtini"));
                    ddf.DataMy_Bra(lista.getString("dtfin"));

                    PdfPCell cel1 = new PdfPCell(new Phrase("Período de "+ dd.bras +
                            "  até "+ ddf.bras +"  ordenado pelo Venicmento",font));
                    cel1.setColspan(5);
                    cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel1);
                }


                PdfPCell cel10 = new PdfPCell(new Phrase("Documento",font));
                cel10.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel10);

                PdfPCell cel11 = new PdfPCell(new Phrase("P.P.",font));
                cel11.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel11);

                PdfPCell cel12 = new PdfPCell(new Phrase("Fornecedor",font));
                cel12.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel12);

                PdfPCell cel13 = new PdfPCell(new Phrase("Vcto.",font));
                cel13.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel13);

                PdfPCell cel14 = new PdfPCell(new Phrase("Valor.",font));
                cel14.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cel14);

                PdfPCell c10 = new PdfPCell(new Phrase("Contrato",font));
                c10.setColspan(2);
                c10.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(c10);

                PdfPCell c11 = new PdfPCell(new Phrase("Hitórico",font));
                c11.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(c11);

                PdfPCell c12 = new PdfPCell(new Phrase("Bacno Pgto.",font));
                c12.setColspan(2);
                c12.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(c12);

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

                double sdia = 0;
                double tot = 0;
                String ultdata = "1900-01-01";

                parcelascomprasGS compra1 = new parcelascomprasGS();
                compra1.listaCtsPg();
                ResultSet lista1 = compra1.getResultado();
                while(lista1.next()){
                    if(!lista1.getString("prevpgto").equals(ultdata)){

                        dd.DataMy_Bra(lista1.getString("prevpgto"));
                        PdfPCell cel5 = new PdfPCell(new Phrase("Previsão para o dia : "+dd.bras,fontb));
                        cel5.setColspan(5);
                        cel5.setHorizontalAlignment(Element.ALIGN_CENTER);
                        table.addCell(cel5);
                        sdia = 0;

                        parcelascomprasGS compra2 = new parcelascomprasGS();
                        compra2.listaCtsPgDet(lista1.getString("prevpgto"));
                        ResultSet lista2 = compra2.getResultado();
                        while(lista2.next()){

                            PdfPCell cel2 = new PdfPCell(new Phrase (lista2.getString("nf"),fontb));
                            cel2.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cel2);

                            PdfPCell cel3 = new PdfPCell(new Phrase (lista2.getString("titulo"),fontb));
                            cel3.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cel3);

                            PdfPCell cel4 = new PdfPCell(new Phrase (lista2.getString("nome"),fontb));
                            cel4.setHorizontalAlignment(Element.ALIGN_CENTER);
                            table.addCell(cel4);

                            dd.DataMy_Bra(lista2.getString("vcto"));
                            PdfPCell cel15 = new PdfPCell(new Phrase (dd.bras,fontb));
                            cel15.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cel15);

                            PdfPCell cel6 = new PdfPCell(new Phrase
                            (df.format(lista2.getDouble("valor")),fontb));
                            cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            table.addCell(cel6);

                            PdfPCell cel7 = new PdfPCell(new Phrase (lista2.getString("custo"),font));
                            cel7.setColspan(2);
                            cel7.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cel7);

                            PdfPCell cel8 = new PdfPCell(new Phrase(lista2.getString("obs"),font));
                            cel8.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cel8);

                            PdfPCell cel9 = new PdfPCell(new Phrase(lista2.getString("banco"),font));
                            cel9.setColspan(2);
                            cel9.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cel9);

                            PdfPCell cel20 = new PdfPCell(new Phrase(""));
                            cel20.setColspan(5);
                            table.addCell(cel20);

                            sdia = sdia + lista2.getDouble("valor");
                            tot = tot + lista2.getDouble("valor");
                        }

                        String nbanco = "banco";

                        parcelascomprasGS compra3 = new parcelascomprasGS();
                        compra3.listaCtsPgBco(lista1.getString("prevpgto"));
                        ResultSet lista3 = compra3.getResultado();
                        while(lista3.next()){
                            if(!lista3.getString("banco").equals(nbanco)){
                                parcelascomprasGS compra4 = new parcelascomprasGS();
                                compra4.listaCtsPgBcoSoma(lista1.getString("prevpgto"), lista3.getString("banco"));
                                ResultSet lista4 = compra4.getResultado();
                                if(lista4.next()){

                                    PdfPCell cel17 = new PdfPCell(new Phrase
                                    ("Total do Banco: "+lista3.getString("banco") +"  R$"+
                                    df.format(lista4.getDouble("svlr")),font));
                                    cel17.setColspan(5);
                                    cel17.setHorizontalAlignment(Element.ALIGN_LEFT);
                                    table.addCell(cel17);
                                }
                            }
                            nbanco = lista3.getString("banco");
                        }

                        dd.DataMy_Bra(lista1.getString("prevpgto"));
                        PdfPCell cel16 = new PdfPCell(new Phrase
                        ("Total do Dia: "+dd.bras +"   R$"+
                        df.format(sdia),fontb));
                        cel16.setColspan(5);
                        cel16.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        table.addCell(cel16);

                    }

                    ultdata = lista1.getString("prevpgto");
                }
                PdfPCell cel17 = new PdfPCell(new Phrase
                ("Total Geral :  R$"+
                df.format(tot),fontb));
                cel17.setColspan(5);
                cel17.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel17);
            
                doc.add(table);
                doc.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            res.setContentType("application/pdf");
            res.setContentLength(baos.size());
            ServletOutputStream out = res.getOutputStream();
            baos.writeTo(out);
            out.flush();
        }
    }
}
