es.clinica.veterinaria.albaranes.AlbaranPdf.java Source code

Java tutorial

Introduction

Here is the source code for es.clinica.veterinaria.albaranes.AlbaranPdf.java

Source

package es.clinica.veterinaria.albaranes;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import es.clinica.veterinaria.ventas.Venta;
import es.clinica.veterinaria.ventas.VentaDAO;
import es.clinica.veterinaria.ventas_linea.VentaLinea;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;

/**
 * @author SaRCo
 */

public class AlbaranPdf {
    private Document document;
    private Venta venta;
    private String FILE = "c:/temp/albaran-";
    private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    private Font small = new Font(Font.FontFamily.TIMES_ROMAN, 10);

    public AlbaranPdf() {
    }

    public void createPdf() {
        try {
            VentaDAO ventaDao = new VentaDAO();
            document = new Document(PageSize.A4);
            String fecha = new SimpleDateFormat("yyyy-MM-dd").format(venta.getFecha());
            String salbaran = getDirectorio().toString() + "/albaran-" + fecha + "-" + venta.getId() + ".pdf";
            PdfWriter.getInstance(document, new FileOutputStream(salbaran));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            //                addContent(document);
            document.add(createTable());
            document.close();
            venta.setAlbaran("../albaranes/albaran-" + fecha + "-" + venta.getId() + ".pdf");
            ventaDao.updateAlbaran(venta);
            //                System.out.println("Documento cerrado");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    public Venta getVenta() {
        return venta;
    }

    public void setVenta(Venta venta) {
        this.venta = venta;
    }

    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
    private void addMetaData(Document document) {
        document.addTitle("My first PDF");
        document.addSubject("Using iText");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private void addTitlePage(Document document) throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        String fecha = new SimpleDateFormat("dd / MM / yyyy").format(venta.getFecha());
        preface.add(new Paragraph("ALBAR?N N " + venta.getId()
                + "                                                            " + fecha, catFont));

        addEmptyLine(preface, 2);
        document.add(preface);
        // Will create: Report generated by: _name, _date
        //preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        //    smallBold));
        //addEmptyLine(preface, 3);
        //preface.add(new Paragraph("This document describes something which is very important ", smallBold));
        preface = new Paragraph("SERVICIOS MDICOS-VETERINARIOS");
        preface.setAlignment(Element.ALIGN_CENTER);
        addEmptyLine(preface, 2);
        document.add(preface);

        preface = new Paragraph("Cliente: " + venta.getCliente().getFullname() + "\nTelfono: "
                + venta.getCliente().getTelefono());
        preface.setAlignment(Element.ALIGN_LEFT);
        addEmptyLine(preface, 2);
        document.add(preface);
        //preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
        //    redFont));

        //document.add(preface);
        // Start a new page
        // document.newPage();
    }

    //    private  void addContent(Document document) throws DocumentException {
    //        Anchor anchor = new Anchor("First Chapter", catFont);
    //        anchor.setName("First Chapter");
    //
    //        // Second parameter is the number of the chapter
    //        Chapter catPart = new Chapter(new Paragraph(anchor), 1);
    //
    //        Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    //        Section subCatPart = catPart.addSection(subPara);
    //        subCatPart.add(new Paragraph("Hello"));
    //
    //        subPara = new Paragraph("Subcategory 2", subFont);
    //        subCatPart = catPart.addSection(subPara);
    //        subCatPart.add(new Paragraph("Paragraph 1"));
    //        subCatPart.add(new Paragraph("Paragraph 2"));
    //        subCatPart.add(new Paragraph("Paragraph 3"));
    //
    //        // Add a list
    //        createList(subCatPart);
    //        Paragraph paragraph = new Paragraph();
    //        addEmptyLine(paragraph, 5);
    //        subCatPart.add(paragraph);
    //
    //        // Add a table
    //        createTable(subCatPart);
    //
    //        // Now add all this to the document
    //        document.add(catPart);
    //
    //        // Next section
    //        anchor = new Anchor("Second Chapter", catFont);
    //        anchor.setName("Second Chapter");
    //
    //        // Second parameter is the number of the chapter
    //        catPart = new Chapter(new Paragraph(anchor), 1);
    //
    //        subPara = new Paragraph("Subcategory", subFont);
    //        subCatPart = catPart.addSection(subPara);
    //        subCatPart.add(new Paragraph("This is a very important message"));
    //
    //        // Now add all this to the document
    //        document.add(catPart);
    //
    //    }

    //    private  void createTable(Section subCatPart)   throws BadElementException {
    //        PdfPTable table = new PdfPTable(3);
    //
    //        // t.setBorderColor(BaseColor.GRAY);
    //        // t.setPadding(4);
    //        // t.setSpacing(4);
    //        // t.setBorderWidth(1);
    //
    //        PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
    //        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    //        table.addCell(c1);
    //
    //        c1 = new PdfPCell(new Phrase("Table Header 2"));
    //        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    //        table.addCell(c1);
    //
    //        c1 = new PdfPCell(new Phrase("Table Header 3"));
    //        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    //        table.addCell(c1);
    //        table.setHeaderRows(1);
    //
    //        table.addCell("1.0");
    //        table.addCell("1.1");
    //        table.addCell("1.2");
    //        table.addCell("2.1");
    //        table.addCell("2.2");
    //        table.addCell("2.3");
    //
    //        subCatPart.add(table);
    //
    //    }

    //    private  void createList(Section subCatPart) {
    //        List list = new List(true, false, 10);
    //        list.add(new ListItem("First point"));
    //        list.add(new ListItem("Second point"));
    //        list.add(new ListItem("Third point"));
    //        subCatPart.add(list);
    //    }

    private void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private File getDirectorio() {
        Desktop desktop = Executions.getCurrent().getDesktop();
        String realpath = desktop.getWebApp().getRealPath("/albaranes");

        File baseDir = new File(realpath + "/");

        if (!baseDir.exists()) {
            baseDir.mkdirs();
        }

        return baseDir;
    }

    public PdfPTable createTable() throws DocumentException {
        // a table with three columns
        int iva = 0, iva2 = 0;
        DecimalFormat df = new DecimalFormat("0.00");
        PdfPTable table = new PdfPTable(5);
        table.setTotalWidth(new float[] { 55, 150, 200, 70, 70 });
        table.setLockedWidth(true);

        // the cell object
        // we add a cell with colspan 3
        PdfPCell cell = new PdfPCell(new Phrase("CANT."));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingTop(5);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("CONCEPTO"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingTop(5);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("DESCRIPCIN"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingTop(5);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("PRECIO"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingTop(5);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("IMPORTE"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingTop(5);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        HashSet<VentaLinea> listVenta = getVenta().getVenta_lineas();

        for (VentaLinea vlinea : listVenta) {
            if (vlinea.getTipo() == 1) {
                if (vlinea.getProducto().getIva() != null) {
                    iva2 = vlinea.getProducto().getIva().getValor();
                    //                    System.out.println("IVA: " +iva2);
                }
            } else if (vlinea.getTipo() == 2) {
                if (vlinea.getServicio().getIva() != null) {
                    iva2 = vlinea.getServicio().getIva().getValor();
                    //                    System.out.println("IVA: " +iva2);
                }
            }

            //Para hacer el calculo nos vamos a quedar con el IVA mayor
            if (iva < iva2) {
                iva = iva2;
            }

            cell = new PdfPCell(new Phrase(vlinea.getCantidad() + "", small));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setPaddingBottom(5);
            table.addCell(cell);

            table.addCell(new PdfPCell(new Phrase(vlinea.getNombre(), small)));

            String descripcion = vlinea.getDescripcion();
            if (descripcion == null || "null".equals(descripcion)) {
                table.addCell(new PdfPCell(new Phrase(" ", small)));
                //                System.out.println("null:" + descripcion);
            } else {
                table.addCell(new PdfPCell(new Phrase(descripcion, small)));
                //                System.out.println("!null:" + descripcion);
            }

            cell = new PdfPCell(new Phrase(df.format(vlinea.getPvp()) + " ", small));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setPaddingBottom(5);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(df.format(vlinea.getPreciototalNoIVA()) + " ", small));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setPaddingBottom(5);
            table.addCell(cell);
        }

        cell = new PdfPCell(new Phrase(" "));
        cell.setColspan(1);
        cell.setBorderWidthBottom(0);
        cell.setBorderWidthLeft(0);
        cell.setBorder(0);
        cell.setBorderColorLeft(BaseColor.WHITE);
        cell.setBorderColorBottom(BaseColor.WHITE);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        table.addCell(cell);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("SUMA"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(df.format(venta.getCostesinIva()) + " "));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        /* IVA */
        cell = new PdfPCell(new Phrase(" "));
        cell.setColspan(1);
        cell.setBorderWidthBottom(0);
        cell.setBorderWidthLeft(0);
        cell.setBorder(0);
        cell.setBorderColorLeft(BaseColor.WHITE);
        cell.setBorderColorBottom(BaseColor.WHITE);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        table.addCell(cell);
        table.addCell(cell);

        //        float costetotal = (float) (venta.getCoste() * (1+(iva*0.01)));

        cell = new PdfPCell(new Phrase("IVA " + iva + "%"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(df.format(venta.getIvas()) + " "));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        /* COSTE TOTAL */
        cell = new PdfPCell(new Phrase(" "));
        cell.setColspan(1);
        cell.setBorderWidthBottom(0);
        cell.setBorderWidthLeft(0);
        cell.setBorder(0);
        cell.setBorderColorLeft(BaseColor.WHITE);
        cell.setBorderColorBottom(BaseColor.WHITE);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        table.addCell(cell);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("TOTAL"));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(df.format(venta.getCoste()) + " "));
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setPaddingBottom(5);
        table.addCell(cell);

        return table;
    }
}