/*
 * 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 relatCtsReceb 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, 5, 1, 1, 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();

                faturaGS compra = new faturaGS();
                compra.listaCtsReg();
                ResultSet lista = compra.getResultado();
                if(lista.next()){
                    PdfPCell cel = new PdfPCell(new Phrase(lista.getString("empresa"),font));
                    cel.setColspan(6);
                    cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel);
                    PdfPCell cel2 = new PdfPCell(new Phrase("Relatório do Contas a Receber",font));
                    cel2.setColspan(6);
                    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 pelas Faturas",font));
                    cel1.setColspan(6);
                    cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel1);
                }


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

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

                PdfPCell cel13 = new PdfPCell(new Phrase("Emissão",font));
                cel13.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel13);

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

                PdfPCell cel10 = new PdfPCell(new Phrase("Valor Total",font));
                cel10.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel10);

                PdfPCell c10 = new PdfPCell(new Phrase("Valor Líquido",font));
                c10.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(c10);

                double tot = 0;
                double tg = 0;
                
                faturaGS compra1 = new faturaGS();
                compra1.listaCtsReg();
                ResultSet lista1 = compra1.getResultado();
                while(lista1.next()){

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

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

                    dd.DataMy_Bra(lista1.getString("emissao"));
                    PdfPCell cel15 = new PdfPCell(new Phrase (dd.bras,font));
                    cel15.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel15);

                    dd.DataMy_Bra(lista1.getString("vcto"));
                    PdfPCell cel16 = new PdfPCell(new Phrase (dd.bras,font));
                    cel16.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel16);
                    
                    DecimalFormat df = new DecimalFormat();
                    df.applyPattern("###,###,##0.00");

                    PdfPCell cel2 = new PdfPCell(new Phrase
                    (df.format(lista1.getDouble("total")),font));
                    cel2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel2);

                    PdfPCell cel6 = new PdfPCell(new Phrase
                    (df.format(lista1.getDouble("vlrliq")),font));
                    cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel6);
                    
                    PdfPCell cel = new PdfPCell(new Phrase(lista1.getString("ctto")+" - "+lista1.getString("obs"),fontb));
                    cel.setColspan(6);
                    cel.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel);

                    PdfPCell cel7 = new PdfPCell(new Phrase("PIS: "+df.format(lista1.getDouble("pis"))+
                            " - COFINS: "+df.format(lista1.getDouble("cofins"))+
                            " - IR: "+df.format(lista1.getDouble("cofins"))+
                            " - CSLL: "+df.format(lista1.getDouble("csll"))+
                            " - ISS: "+df.format(lista1.getDouble("iss"))+
                            " - INSS: "+df.format(lista1.getDouble("inss")),font));
                    cel7.setColspan(6);
                    cel7.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel7);
                    
                    PdfPCell cel8 = new PdfPCell(new Phrase("",font));
                    cel8.setColspan(6);
                    cel8.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel8);
                    
                    tot = tot + lista1.getDouble("vlrliq");
                    tg = tg + lista1.getDouble("total");
                }

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

                PdfPCell cel17 = new PdfPCell(new Phrase
                ("Total Geral :  R$"+
                dft.format(tg) +
                "             Total Líquido :  R$"+
                dft.format(tot),fontb));
                cel17.setColspan(6);
                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();
        }
    }
}
