Package com.suarte.reports

Source Code of com.suarte.reports.ReceiptFormatToPdf

package com.suarte.reports;

import java.io.IOException;
import java.io.OutputStream;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.BadElementException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfPTable;
import com.suarte.core.Payment;
import com.suarte.utils.MathUtils;
import com.suarte.utils.NumberToLetters;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @date   Mar 12, 2011
* @author Ggutierrez
*/
public class ReceiptFormatToPdf {

    Image logo;
    Format formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    Format simpleFormat = new SimpleDateFormat("dd/MM/yyyy");
    private static final NumberToLetters numberToLetters = new NumberToLetters();
    private static final MathUtils mathUtils = new MathUtils();

    public ReceiptFormatToPdf() {
        try {
            logo = Image.getInstance(getClass().getResource("/suarte_logo.png"));
        } catch (BadElementException ex) {
            Logger.getLogger(ReceiptFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ReceiptFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ReceiptFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write(Payment payment, OutputStream out) {
        Document document = new Document();
        try {
            // step 2:
            // we create a writer that listens to the document and directs a PDF-stream to System.out (and a txt file)
            PdfWriter.getInstance(document, out);
            // step 3: we open the document
            document.open();

            document.add(blankSpace());
            document.add(blankSpace());
            document.add(blankSpace());
            document.add(blankSpace());
            document.add(blankSpace());
            document.add(blankSpace());

            addHeader(document, payment);

            document.close();

        } catch (DocumentException ex) {
            Logger.getLogger(ReceiptFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addHeader(Document document, Payment payment) throws DocumentException {
        //Table size 2 columns
        float[] widths = {0.8f, 0.3f};
        PdfPTable header = new PdfPTable(widths);
        String paymentAmount = "";
        String currencyDescription = "";

        if (payment.getCurrency().getSymbol().equals("C$")) {
            paymentAmount = String.valueOf(MathUtils.round(payment.getLocalAmount()));
        } else {
            paymentAmount = String.valueOf(MathUtils.round(payment.getForeignAmount()));
            // One more line
            header.getDefaultCell().setBorder(0);
            header.addCell(blankSpace());
            header.addCell(blankSpace());
        }

        currencyDescription = payment.getCurrency().getDescription();
        header.setWidthPercentage(78);
        header.getDefaultCell().setBorder(0);

        header.addCell(blankSpace());

        // Amount
        Paragraph p = new Paragraph("          " + paymentAmount, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_RIGHT);
        header.addCell(p);

        // Date
        header.getDefaultCell().setColspan(2);
        String paymentDate = simpleFormat.format(payment.getDate());
        p = new Paragraph(paymentDate, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_LEFT);
        header.addCell(p);
       
        // Blank Line
        header.addCell(blankSpace());
       
        // Recibimos de line: Company
        String companyName = payment.getCompany().getDescription();
        p = new Paragraph("                  " + companyName, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_LEFT);
        header.addCell(p);
       
        // Blank Line
        header.addCell(blankSpace());

        // La cantidad de: Amount
        Long entireAmount = Long.valueOf(paymentAmount.substring(0, paymentAmount.indexOf(".")));
        String cents = paymentAmount.substring(paymentAmount.indexOf(".") + 1, paymentAmount.length());
        Long centsAmount = cents.length() == 1 ? Long.valueOf(cents) * 10 : Long.valueOf(cents);
        String amountDescription = numberToLetters.toLetras(entireAmount) + " " + currencyDescription;
        String centsDescription = centsAmount != 0 ? numberToLetters.toLetras(centsAmount) : null;

        // First line
        header.getDefaultCell().setColspan(2);
        p = new Paragraph("                  " + amountDescription, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_LEFT);
        header.getDefaultCell().setColspan(2);
        header.addCell(p);
       
        // Blank Line
        header.addCell(blankSpace());

        // Second line
        if (centsDescription != null) {
            header.getDefaultCell().setColspan(2);
            p = new Paragraph("con " + centsDescription, new Font(Font.HELVETICA, 12, Font.NORMAL));
            p.setAlignment(Element.ALIGN_LEFT);
            header.getDefaultCell().setColspan(2);
            header.addCell(p);
        }
       
        // Blank Line
        header.addCell(blankSpace());

        // Concepto
        header.getDefaultCell().setColspan(2);
        String description = payment.getDescription();
        p = new Paragraph("                  " + description, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_LEFT);
        header.getDefaultCell().setColspan(2);
        header.addCell(p);
       
        // Blank Line (8)
        header.addCell(blankSpace());
        header.addCell(blankSpace());
        header.addCell(blankSpace());
        header.addCell(blankSpace());
        header.addCell(blankSpace());
        header.addCell(blankSpace());
        header.addCell(blankSpace());

        // Check or efective
        header.getDefaultCell().setColspan(2);
        String type = payment.getType().toString();
        if (type.equals("Efectivo")) {
            p = new Paragraph("          ex", new Font(Font.HELVETICA, 12, Font.NORMAL));
            p.setAlignment(Element.ALIGN_LEFT);
            header.addCell(p);
        } else if (type.equals("Cheque")) {
            // Additional line
            header.addCell(blankSpace());
            header.addCell(blankSpace());
            p = new Paragraph("          cx", new Font(Font.HELVETICA, 12, Font.NORMAL));
            p.setAlignment(Element.ALIGN_LEFT);
            header.addCell(p);
        }
        document.add(header);
    }

    public Paragraph blankSpace() {
        //White Line
        String text = " ";
        Paragraph s = new Paragraph(text);
        s.setAlignment(Element.ALIGN_CENTER);
        return s;
    }
}
TOP

Related Classes of com.suarte.reports.ReceiptFormatToPdf

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.