/*
 * 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.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import af.AceFilial;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font.FontFamily;

import java.io.ByteArrayOutputStream;
import java.text.DecimalFormat;
import javax.servlet.http.HttpServlet;

/**
 *
 * @author Edson
 */
public class relatPP 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;
        Paragraph para = null;

        try{
            doc = new Document(PageSize.A4);
            wri = PdfWriter.getInstance(doc, baos);
            doc.open();
            PdfPTable table = new PdfPTable(2);

            Font f = new Font();
            f.setFamily("Courrier");
            f.setSize(8);

            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, 10, Font.BOLD, BaseColor.BLACK);

            parcelascomprasGS par = new parcelascomprasGS();
            par.impPP();
            ResultSet pp = par.getResultado();
            if(pp.next()){
                PdfPCell cel1 = new PdfPCell(new Phrase("Empresa: "+pp.getString("rs")));
                cel1.setColspan(2);
                cel1.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel1);
                
                PdfPCell cel = new PdfPCell(new Phrase("Processo de Pagamento Nº    "+pp.getString("pp")));
                cel.setColspan(2);
                cel.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel);

                table.addCell("Fornecedor: ");
                PdfPCell cel10 = new PdfPCell(new Phrase(pp.getString("forne"),fontb));
                cel10.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel10);
                
                table.addCell("Nota Fiscal/Documento: ");
                PdfPCell cel2 = new PdfPCell(new Phrase(pp.getString("nf"),fontb));
                cel2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cel2);

                table.addCell("Valor:");
                DecimalFormat dfc = new DecimalFormat();
                dfc.applyPattern("###,##0.00");
                PdfPCell cel3 = new PdfPCell(new Phrase("R$"+dfc.format(pp.getDouble("valor")),fontb));
                cel3.setHorizontalAlignment(Element.ALIGN_RIGHT);

                table.addCell(cel3);

                table.addCell("Emissão: ");
                DataMy_Bra emi = new DataMy_Bra();
                emi.DataMy_Bra(pp.getString("emissao"));
                table.addCell(emi.bras);
                
                table.addCell("Vencimento: ");
                DataMy_Bra vcto = new DataMy_Bra();
                vcto.DataMy_Bra(pp.getString("vcto"));
                PdfPCell cel11 = new PdfPCell(new Phrase(vcto.bras,fontb));
                cel11.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cel11);

                table.addCell("Previsão de Pgto.: ");
                DataMy_Bra prev = new DataMy_Bra();
                prev.DataMy_Bra(pp.getString("prev"));
                PdfPCell cel12 = new PdfPCell(new Phrase(prev.bras,fontb));
                cel12.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(cel12);

                PdfPCell cel4 = new PdfPCell(new Phrase("Observações: " + pp.getString("obs")));
                cel4.setColspan(2);
                cel4.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel4);
                
                table.addCell("Centro de Custo: ");
                PdfPCell cel13 = new PdfPCell(new Phrase(pp.getString("ccusto"),fontb));
                cel13.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel13);

                table.addCell("Banco para Pagamento: ");
                table.addCell(pp.getString("banco"));
            }
            PdfPCell cel2 = new PdfPCell(new Phrase("Aprovação Financeira:"));
            cel2.setColspan(1);
            cel2.setFixedHeight(46f);
            table.addCell(cel2);
            PdfPCell cel3 = new PdfPCell(new Phrase("Setor Fiscal: Observações/Visto"));
            cel3.setColspan(1);
            cel3.setFixedHeight(46f);
            table.addCell(cel3);
            PdfPCell cel4 = new PdfPCell(new Phrase("Contas a Pagar: Observações/Visto"));
            cel4.setColspan(1);
            cel4.setFixedHeight(46f);
            table.addCell(cel4);
            PdfPCell cel5 = new PdfPCell(new Phrase("Pagamento: Observações/Visto"));
            cel5.setColspan(1);
            cel5.setFixedHeight(46f);
            table.addCell(cel5);
            PdfPCell cel6 = new PdfPCell(new Phrase("Diretoria: Observações/Visto"));
            cel6.setColspan(2);
            cel6.setFixedHeight(56f);
            table.addCell(cel6);

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