/*
 * 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.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import af.AceFilial;
import java.text.DecimalFormat;

/**
 *
 * @author Edson
 */
public class relatSolPgto extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        String acao = req.getParameter("acao");
        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);

            solpgtoGS solp = new solpgtoGS();
            solp.impSolpgto(acao);
            
            ResultSet lista = solp.getResultado();
            if(lista.next()){
                PdfPCell cel = new PdfPCell(new Phrase("Solicitação de Pagamentos Nº"+lista.getString("s.idsolpgto")));
                cel.setColspan(2);
                cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel);
                PdfPCell cel1 = new PdfPCell(new Phrase(lista.getString("f.razao_social")));
                cel1.setColspan(2);
                cel1.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel1);
                table.addCell("Contrato(Centro de Custo):");

                PdfPCell cel2 = new PdfPCell(new Phrase(lista.getString("c.cttonr")));
                cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel2);

                table.addCell("Favorecido:");
                table.addCell(lista.getString("s.favorecido"));
                table.addCell("Froma de Pagamento:");
                table.addCell(lista.getString("s.formapgto"));
                table.addCell("Data da Solicitação:");
                DataMy_Bra dt = new DataMy_Bra();
                dt.DataMy_Bra(lista.getString("s.dtsolicitacao"));
                table.addCell(dt.bras);
                table.addCell("Data do Pagamento:");
                dt.DataMy_Bra(lista.getString("s.dtpgto"));
                table.addCell(dt.bras);

                table.addCell("Total de Solcitação:");

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

                PdfPCell cel12 = new PdfPCell(new Phrase(df9.format(lista.getDouble("s.total"))));
                cel12.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cel12);

                PdfPCell cel3 = new PdfPCell(new Phrase("Itens da Solicitação"));
                cel3.setColspan(2);
                cel3.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel2);
                doc.add(table);
            }

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

            solpgtoGS solpi = new solpgtoGS();

            solpi.impSolpgto(acao);
            ResultSet listai = solpi.getResultado();
            tab.addCell("Descrição");
            tab.addCell("Valor");
            while(listai.next()){
                tab.addCell(listai.getString("i.descrcao"));

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

                PdfPCell cel2 = new PdfPCell(new Phrase(df9.format(listai.getDouble("i.valor"))));
                cel2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel2);
            }
            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();
    }
}
