/*
 * 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 com.itextpdf.text.Image;

/**
 *
 * @author Edson
 */
public class relatCotacao 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;
        double valor = 0;
        double icm = 0;
        double outro = 0;
        double valor1 = 0;
        double icm1 = 0;
        double outro1 = 0;
        double valor2 = 0;
        double icm2 = 0;
        double outro2 = 0;

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

            cotacaoGS cota = new cotacaoGS();
            cota.listaCotacaoRelat();
            ResultSet lista = cota.getResultado();
            if(lista.next()){
                String pat = "C:/CT/Java/Erp_Web/CTSoftWares/web/Imagens/Empresa"+lista.getString("idempresa")+".jpg";
                Image img = Image.getInstance(pat);
                doc.add(Image.getInstance(pat));
                PdfPCell cel = new PdfPCell(new Phrase("Cotação Nº"+lista.getString("idcotacao"),font));
                cel.setColspan(2);
                cel.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cel);
                PdfPCell cel1 = new PdfPCell(new Phrase(lista.getString("empresa"),font));
                cel1.setColspan(2);
                cel1.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel1);
                PdfPCell cel2 = new PdfPCell(new Phrase("Contrato(Centro de Custo): " +
                        lista.getString("cttonr"),font));
                cel2.setColspan(2);
                cel2.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel2);
                DataMy_Bra dt = new DataMy_Bra();
                dt.DataMy_Bra(lista.getString("datacota"));
                PdfPCell cel3 = new PdfPCell(new Phrase("Data da cotação :" +
                        dt.bras,font));
                cel3.setColspan(2);
                cel3.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cel3);
            }
            doc.add(table);

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

            cotacaoGS cotadet = new cotacaoGS();
            cotadet.listaCotacaoRelat();
            ResultSet listai = cotadet.getResultado();
            while(listai.next()){
                valor = valor + listai.getDouble("preco1");
                icm = icm + listai.getDouble("icms1");
                outro = outro + listai.getDouble("outros1");
                valor1 = valor1 + listai.getDouble("preco2");
                icm1 = icm1 + listai.getDouble("icms2");
                outro1 = outro1 + listai.getDouble("outros2");
                valor2 = valor2 + listai.getDouble("preco3");
                icm2 = icm2 + listai.getDouble("icms3");
                outro2 = outro2 + listai.getDouble("outros3");

                PdfPCell cel4 = new PdfPCell(new Phrase("Produto: "+listai.getString("produto"),font));
                cel4.setColspan(5);
                cel4.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel4);

                PdfPCell cel17 = new PdfPCell(new Phrase("Fornecedor",font));
                cel17.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel17);
                PdfPCell cel18 = new PdfPCell(new Phrase("Valor Unit",font));
                cel18.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel18);
                PdfPCell cel19 = new PdfPCell(new Phrase("Qtd.",font));
                cel19.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel19);
                PdfPCell cel20 = new PdfPCell(new Phrase("ICMS",font));
                cel20.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel20);
                PdfPCell cel21 = new PdfPCell(new Phrase("Outros",font));
                cel21.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab.addCell(cel21);
                
                PdfPCell cel5 = new PdfPCell(new Phrase(listai.getString("forne1"),font));
                cel5.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab.addCell(cel5);
                PdfPCell cel6 = new PdfPCell(new Phrase(listai.getString("preco1"),font));
                cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel6);
                PdfPCell cel7 = new PdfPCell(new Phrase(listai.getString("qtd1"),font));
                cel7.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel7);
                PdfPCell cel8 = new PdfPCell(new Phrase(listai.getString("icms1"),font));
                cel8.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel8);
                PdfPCell cel9 = new PdfPCell(new Phrase(listai.getString("outros1"),font));
                cel9.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel9);

                PdfPCell cel1 = new PdfPCell(new Phrase(listai.getString("forne2"),font));
                cel1.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab.addCell(cel1);
                PdfPCell cel2 = new PdfPCell(new Phrase(listai.getString("preco2"),font));
                cel2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel2);
                PdfPCell cel3 = new PdfPCell(new Phrase(listai.getString("qtd2"),font));
                cel3.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel3);
                PdfPCell cel10 = new PdfPCell(new Phrase(listai.getString("icms2"),font));
                cel10.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel10);
                PdfPCell cel11 = new PdfPCell(new Phrase(listai.getString("outros2"),font));
                cel11.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel11);

                PdfPCell cel12 = new PdfPCell(new Phrase(listai.getString("forne3"),font));
                cel12.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab.addCell(cel12);
                PdfPCell cel13 = new PdfPCell(new Phrase(listai.getString("preco3"),font));
                cel13.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel13);
                PdfPCell cel14 = new PdfPCell(new Phrase(listai.getString("qtd3"),font));
                cel14.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel14);
                PdfPCell cel15 = new PdfPCell(new Phrase(listai.getString("icms3"),font));
                cel15.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel15);
                PdfPCell cel16 = new PdfPCell(new Phrase(listai.getString("outros3"),font));
                cel16.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab.addCell(cel16);
            }
            doc.add(tab);

            PdfPTable table1 = new PdfPTable(2);
            table1.setWidthPercentage(100f);
            table1.getDefaultCell().setUseAscender(true);
            table1.getDefaultCell().setUseDescender(true);
            
            PdfPCell cel = new PdfPCell(new Phrase("Ressumo da Cotação",font));
            cel.setColspan(2);
            cel.setHorizontalAlignment(Element.ALIGN_CENTER);
            table1.addCell(cel);

            doc.add(table1);
            
            PdfPTable tab1 = new PdfPTable(new float[]  {4, 4, 1});
            tab1.setWidthPercentage(100f);
            tab1.getDefaultCell().setUseAscender(true);
            tab1.getDefaultCell().setUseDescender(true);

            cotacaoGS cotadet1 = new cotacaoGS();
            cotadet1.listaCotacaoRelat();
            ResultSet lista1 = cotadet1.getResultado();
            if(lista1.next()){
                PdfPCell cel17 = new PdfPCell(new Phrase("Fornecedor",font));
                cel17.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab1.addCell(cel17);
                PdfPCell cel18 = new PdfPCell(new Phrase("Condição Pgto.",font));
                cel18.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab1.addCell(cel18);
                PdfPCell cel19 = new PdfPCell(new Phrase("Total",font));
                cel19.setHorizontalAlignment(Element.ALIGN_CENTER);
                tab1.addCell(cel19);

                PdfPCell cel5 = new PdfPCell(new Phrase(lista1.getString("forne1"),font));
                cel5.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab1.addCell(cel5);
                PdfPCell cel7 = new PdfPCell(new Phrase(lista1.getString("condipg1"),font));
                cel7.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab1.addCell(cel7);
                PdfPCell cel6 = new PdfPCell(new Phrase(String.format("%10.2f",valor),font));
                cel6.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab1.addCell(cel6);

                PdfPCell cel1 = new PdfPCell(new Phrase(lista1.getString("forne2"),font));
                cel1.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab1.addCell(cel1);
                PdfPCell cel3 = new PdfPCell(new Phrase(lista1.getString("condipg2"),font));
                cel3.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab1.addCell(cel3);
                PdfPCell cel2 = new PdfPCell(new Phrase(String.format("%10.2f",valor1),font));
                cel2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab1.addCell(cel2);

                PdfPCell cel12 = new PdfPCell(new Phrase(lista1.getString("forne3"),font));
                cel12.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab1.addCell(cel12);
                PdfPCell cel14 = new PdfPCell(new Phrase(lista1.getString("condipg3"),font));
                cel14.setHorizontalAlignment(Element.ALIGN_LEFT);
                tab1.addCell(cel14);
                PdfPCell cel13 = new PdfPCell(new Phrase(String.format("%10.2f",valor2),font));
                cel13.setHorizontalAlignment(Element.ALIGN_RIGHT);
                tab1.addCell(cel13);

            }
            PdfPCell cel20 = new PdfPCell(new Phrase("",font));
            cel20.setColspan(3);
            tab1.addCell(cel20);
            PdfPCell cel21 = new PdfPCell(new Phrase("",font));
            cel21.setColspan(3);
            tab1.addCell(cel21);
            PdfPCell cel22 = new PdfPCell(new Phrase("Consultor:",font));
            cel22.setColspan(3);
            cel22.setRowspan(3);
            tab1.addCell(cel22);

            doc.add(tab1);


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

}
