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

package br.com.ddns.grupotsergio;

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

                reldespesaGS reld = new reldespesaGS();
                reld.exiReldesp();
                ResultSet lista = reld.getResultado();
                if(lista.next()){
                    PdfPCell cel = new PdfPCell(new Phrase("Relatório de Despesas Nº    "+lista.getString("nro")));
                    cel.setColspan(2);
                    cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel);

                    PdfPCell cel1 = new PdfPCell(new Phrase("Empresa: "+lista.getString("empresa")));
                    cel1.setColspan(2);
                    cel1.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cel1);
                
                    table.addCell("Usuário:");
                    PdfPCell cel2 = new PdfPCell(new Phrase(lista.getString("usuario")));
                    cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel2);
                    
                    table.addCell("Data da Emissão");
                    DataMy_Bra emi = new DataMy_Bra();
                    emi.DataMy_Bra(lista.getString("data"));
                    PdfPCell cel3 = new PdfPCell(new Phrase(emi.bras));
                    cel3.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel3);

                    table.addCell("Reembolsar");
                    table.addCell(lista.getString("reembolsar"));
                    table.addCell("Forma de Reembolso");
                    table.addCell(lista.getString("formareemb"));

                    table.addCell("Data do Último Relatório");

                    DataMy_Bra dur = new DataMy_Bra();
                    dur.DataMy_Bra(lista.getString("dtsalant"));
                    PdfPCell cel4 = new PdfPCell(new Phrase(dur.bras));
                    cel4.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel4);

                    table.addCell("Saldo Anterior");

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

                    PdfPCell cel5 = new PdfPCell(new Phrase("R$"+dfc.format(lista.getDouble("salant"))));
                    cel5.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel5);

                    table.addCell("Total dos Débitos");

                    PdfPCell cel6 = new PdfPCell(new Phrase("R$"+dfc.format(lista.getDouble("debito"))));
                    cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel6);

                    table.addCell("Total dos Créditos");

                    PdfPCell cel7 = new PdfPCell(new Phrase("R$"+dfc.format(lista.getDouble("credito"))));
                    cel7.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel7);

                    table.addCell("Saldo");

                    PdfPCell cel8 = new PdfPCell(new Phrase("R$"+dfc.format(lista.getDouble("saldo"))));
                    cel8.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel8);

                    table.addCell("Valor do Reembolso");

                    PdfPCell cel9 = new PdfPCell(new Phrase("R$"+dfc.format(lista.getDouble("reembolso"))));
                    cel9.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel9);

                    table.addCell("Fundo Fixo");

                    PdfPCell cel10 = new PdfPCell(new Phrase("R$"+dfc.format(lista.getDouble("ffixo"))));
                    cel10.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel10);
                }
                PdfPCell cel1 = new PdfPCell(new Phrase("Itens de Relatório"));
                cel1.setColspan(2);
                cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel1);
                doc.add(table);


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

                table1.setWidthPercentage(100f);
                table1.getDefaultCell().setUseAscender(true);
                table1.getDefaultCell().setUseDescender(true);

                reldespesaGS reldi = new reldespesaGS();
                reldi.exiReldesp();
                ResultSet listai = reldi.getResultado();

                table1.addCell("Item");
                table1.addCell("Data");
                table1.addCell("Descrição");
                table1.addCell("Crédito");
                table1.addCell("Débito");
                table1.addCell("Saldo");
                int item = 0;
                double saldo = 0;

                DataMy_Bra dd = new DataMy_Bra();
            
                saldo = lista.getDouble("salant");

                reldespesaGS reld1 = new reldespesaGS();
                reld1.exiReldesp();
                ResultSet lista1 = reld1.getResultado();
               while(lista1.next()){
                    DecimalFormat dfc = new DecimalFormat();
                    dfc.applyPattern("###,##0.00");

                    item = item + 1;
                    table1.addCell(String.format("%d",item));
                    dd.DataMy_Bra(lista1.getString("dtdespesa"));
                    table1.addCell(dd.bras);
                    table1.addCell(lista1.getString("descricao"));
                   if(lista1.getDouble("icredito") > 0){
                        PdfPCell cel12 = new PdfPCell(new Phrase("R$"+dfc.format(lista1.getDouble("icredito"))));
                        cel12.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        table1.addCell(cel12);
                    }else{
                        table1.addCell("");
                    }
                    if(lista1.getDouble("idebito") > 0){
                        PdfPCell cel13 = new PdfPCell(new Phrase("R$"+dfc.format(lista1.getDouble("idebito"))));
                        cel13.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        table1.addCell(cel13);
                    }else{
                        table1.addCell("");
                    }
                    saldo = saldo + lista1.getDouble("icredito")-
                    lista1.getDouble("idebito");

                    PdfPCell cel13 = new PdfPCell(new Phrase("R$"+dfc.format(saldo)));
                    cel13.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table1.addCell(cel13);
 
                }

                doc.add(table1);

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

}
