/*
 * 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 relatRazao 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);

                double saldo = 0;
                double totdeb = 0;
                double totcre = 0;

                lancacontabGS raz = new lancacontabGS();
                raz.listaRazao();
                ResultSet lista = raz.getResultado();
                if(lista.next()){
                    lista.last();
                    PdfPCell cel = new PdfPCell(new Phrase(lista.getString("empresa"),fontb));
                    cel.setColspan(2);
                    cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel);

                    PdfPCell cel1 = new PdfPCell(new Phrase("Razão Analítico",fontb));
                    cel1.setColspan(2);
                    cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel1);

                    DataMy_Bra dt = new DataMy_Bra();
                    dt.DataMy_Bra(lista.getString("datain"));
                    DataMy_Bra dtf = new DataMy_Bra();
                    dtf.DataMy_Bra(lista.getString("datafin"));

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

                    PdfPCell cel3 = new PdfPCell(new Phrase("Conta: "+
                            lista.getString("conta") + " Descrição: "+
                            lista.getString("descricao"),fontb));
                    cel3.setColspan(2);
                    cel3.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cel3);

                    lista.first();

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

                    PdfPCell cel4 = new PdfPCell(new Phrase("Saldo Anterior -----> "+
                            dfi.format(lista.getDouble("debito")),font));
                    cel4.setColspan(2);
                    cel4.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cel4);

                    saldo = lista.getDouble("debito");

                }
                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 cel11 = new PdfPCell(new Phrase("Data",font));
                cel11.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel11);

                PdfPCell cel51 = new PdfPCell(new Phrase("Docum.",font));
                cel51.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel51);

                PdfPCell cel12 = new PdfPCell(new Phrase("Histórico",font));
                cel12.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel12);

                PdfPCell cel13 = new PdfPCell(new Phrase("Débito",font));
                cel13.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel13);

                PdfPCell cel14 = new PdfPCell(new Phrase("Crédito",font));
                cel14.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel14);

                PdfPCell cel15 = new PdfPCell(new Phrase("Saldo",font));
                cel15.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel15);

                lancacontabGS cts = new lancacontabGS();
                cts.listaRazaoConta();
                ResultSet liscon = raz.getResultado();
                while(liscon.next()){
                    lancacontabGS detl = new lancacontabGS();
                    detl.listaRazaoDet(liscon.getString("conta"));
                    ResultSet detconl = detl.getResultado();
                    while(detconl.next()){
                        if(detconl.getInt("idlanca") == 0){
                            PdfPCell cel1 = new PdfPCell(new Phrase(detconl.getString("descricao"),font));
                            cel1.setColspan(4);
                            cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                            tab.addCell(cel1);

                            PdfPCell cel2 = new PdfPCell(new Phrase("Saldo Anterior: "+detconl.getString("debito"),font));
                            cel2.setColspan(2);
                            cel2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            tab.addCell(cel2);
                        }else{
                            DataMy_Bra dt = new DataMy_Bra();
                            dt.DataMy_Bra(detconl.getString("dtlan"));

                            PdfPCell cel7 = new PdfPCell(new Phrase(dt.bras,font));
                            cel7.setHorizontalAlignment(Element.ALIGN_LEFT);
                            tab.addCell(cel7);

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

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

                            if(detconl.getDouble("debito") != 0){
                                DecimalFormat dfd = new DecimalFormat();
                                dfd.applyPattern("###,##0.00");

                                PdfPCell cel5 = new PdfPCell(new Phrase(dfd.format(detconl.getDouble("debito")),font));
                                cel5.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                tab.addCell(cel5);
                            }else{
                                tab.addCell("");
                            }

                            if(detconl.getDouble("credito") != 0){
                                DecimalFormat dfc = new DecimalFormat();
                                dfc.applyPattern("###,##0.00");

                                PdfPCell cel4 = new PdfPCell(new Phrase(dfc.format(detconl.getDouble("credito")),font));
                                cel4.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                tab.addCell(cel4);
                            }else{
                                tab.addCell("");
                            }

                            saldo = saldo + detconl.getDouble("debito") -
                                    detconl.getDouble("credito");

                            totdeb = totdeb + detconl.getDouble("debito");
                            totcre = totcre + detconl.getDouble("credito");

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

                            PdfPCell cel6 = new PdfPCell(new Phrase(dfs.format(saldo),font));
                            cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            tab.addCell(cel6);
                        }
                    }
                }

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

                PdfPCell cel22 = new PdfPCell(new Phrase("Totais",font));
                cel22.setColspan(3);
                cel22.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel22);

                PdfPCell cel23 = new PdfPCell(new Phrase(dfs.format(totdeb),font));
                cel23.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel23);

                PdfPCell cel24 = new PdfPCell(new Phrase(dfs.format(totcre),font));
                cel24.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel24);

                PdfPCell cel25 = new PdfPCell(new Phrase(""));
                cel25.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel25);

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

}
