package com.suarte.reports;
import com.suarte.core.Quotation;
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.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.suarte.core.QuotationDetail;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.net.MalformedURLException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @date Mar 12, 2011
* @author Ggutierrez
*/
public class QuotationToPdf {
Image logo;
Format formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Format simpleFormat = new SimpleDateFormat("dd/MM/yyyy");
DecimalFormat numberFormat = new DecimalFormat( "#,###,###,##0.00" );
public QuotationToPdf() {
try {
logo = Image.getInstance(getClass().getResource("/suarte_small.png"));
} catch (BadElementException ex) {
Logger.getLogger(QuotationToPdf.class.getName()).log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
Logger.getLogger(QuotationToPdf.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuotationToPdf.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void write(Quotation quotation, 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();
addLogo(document);
addCompanyInfo(document);
document.add(blankSpace());
//TITTLE
Paragraph p = new Paragraph("COTIZACION", new Font(Font.HELVETICA, 16, Font.BOLD));
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);
document.add(blankSpace());
document.add(blankSpace());
addHeader(document, quotation);
document.add(blankSpace());
document.add(blankSpace());
addDetailHeader(document);
addDetail(document, quotation);
addFooter(document, quotation);
document.close();
} catch (DocumentException ex) {
Logger.getLogger(QuotationToPdf.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void addLogo(Document document) throws DocumentException {
logo.setAlignment(Image.ALIGN_LEFT);
logo.setAbsolutePosition(20f, 750f);
document.add(logo);
}
public void addCompanyInfo(Document document) throws DocumentException {
Paragraph p = new Paragraph("Su Arte", new Font(Font.HELVETICA, 9, Font.BOLD));
p.setAlignment(Element.ALIGN_RIGHT);
document.add(p);
// Print date
String myDate = formatter.format(new Date());
p = new Paragraph("Impreso: " + myDate, new Font(Font.HELVETICA, 9, Font.NORMAL));
p.setAlignment(Element.ALIGN_RIGHT);
document.add(p);
// Print Fax & phone
String pbx = "PBX: 2250-7360, Telefax: 2250-7360";
p = new Paragraph(pbx, new Font(Font.HELVETICA, 9, Font.NORMAL));
p.setAlignment(Element.ALIGN_RIGHT);
document.add(p);
// Print Fax & phone
String email = "E-mail: info@suarte.com";
p = new Paragraph(email, new Font(Font.HELVETICA, 9, Font.NORMAL));
p.setAlignment(Element.ALIGN_RIGHT);
document.add(p);
}
public void addHeader(Document document, Quotation quotation) throws DocumentException {
//Table size 4 columns
float[] widths = {0.2f, 0.4f, 0.3f, 0.2f};
PdfPTable header = new PdfPTable(widths);
header.setWidthPercentage(100);
header.getDefaultCell().setBorder(0);
// Line 1
header.addCell(new Paragraph(""));
header.addCell(new Paragraph(""));
Paragraph p = new Paragraph("Cotizacion no.: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
header.addCell(p);
p = new Paragraph(quotation.getId().toString(), new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
// Line 2
header.addCell(new Paragraph(""));
header.addCell(new Paragraph(""));
p = new Paragraph("Fecha: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
header.addCell(p);
String quotationDate = simpleFormat.format(quotation.getTransDate());
p = new Paragraph(quotationDate, new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
// Line 3
p = new Paragraph("Empresa: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
String companyName = quotation.getCompany().getDescription();
p = new Paragraph(companyName, new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
p = new Paragraph("Moneda: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
header.addCell(p);
p = new Paragraph(quotation.getCurrency().getDescription(), new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
// Line 4
p = new Paragraph("Contacto: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
String firstName = quotation.getContact().getFirstName();
String lastName = quotation.getContact().getLastName();
String contactName = quotation.getContact().toString();
p = new Paragraph(contactName, new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
header.addCell(new Paragraph(""));
header.addCell(new Paragraph(""));
// Line 5
p = new Paragraph("Telefono: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
String phone = quotation.getCompany().getPhone();
p = new Paragraph(phone, new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
header.addCell(new Paragraph(""));
header.addCell(new Paragraph(""));
// Line 6
p = new Paragraph("Fax: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
String fax = quotation.getCompany().getFax();
p = new Paragraph(fax, new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
header.addCell(new Paragraph(""));
header.addCell(new Paragraph(""));
// Line 7
p = new Paragraph("Descripcion: ", new Font(Font.HELVETICA, 12, Font.NORMAL));
p.setAlignment(Element.ALIGN_LEFT);
header.addCell(p);
String description = quotation.getDescription();
p = new Paragraph(description, new Font(Font.HELVETICA, 12, Font.NORMAL));
p.setAlignment(Element.ALIGN_LEFT);
header.getDefaultCell().setColspan(3);
header.addCell(p);
document.add(header);
}
public void addDetailHeader(Document document) throws DocumentException {
//Table size 6 columns: Product, resolution, workType, price, quantity, total
float[] widths = {0.4f, 0.3f, 0.3f, 0.15f, 0.15f, 0.15f};
PdfPTable header = new PdfPTable(widths);
header.setWidthPercentage(100);
header.getDefaultCell().setBorder(PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.BOTTOM | PdfPCell.TOP);
header.getDefaultCell().setGrayFill(0.8f);
Paragraph p = new Paragraph("Producto", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph("Resolucion", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph("Tipo Trabajo", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph("Precio", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph("Cant.", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph("Total", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
document.add(header);
}
public void addDetail(Document document, Quotation quotation) throws DocumentException {
//Table size 6 columns: Product, resolution, workType, price, quantity, total
float[] widths = {0.4f, 0.3f, 0.3f, 0.15f, 0.15f, 0.15f};
PdfPTable header = new PdfPTable(widths);
header.setWidthPercentage(100);
header.getDefaultCell().setBorder(PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.BOTTOM | PdfPCell.TOP);
header.getDefaultCell().setGrayFill(0f);
for (QuotationDetail detail : quotation.getDetails()) {
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
String description = detail.getProduct() != null ? detail.getProduct().getDescription() : detail.getItemDescription();
Paragraph p = new Paragraph(description, new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
String resolution = detail.getProduct() != null ? detail.getProduct().getResolution().getDescription() : null;
p = new Paragraph(resolution, new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
String workType = detail.getProduct() != null ? detail.getProduct().getWorkType().getDescription() : null;
p = new Paragraph(workType, new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
p = new Paragraph(numberFormat.format(detail.getPrice()), new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph(numberFormat.format(detail.getQuantity()), new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
p = new Paragraph(numberFormat.format(detail.getSubTotal()), new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(p);
}
header.getDefaultCell().setBorder(0);
header.getDefaultCell().setColspan(6);
header.addCell(blankSpace());
//Footer
header.getDefaultCell().setColspan(0);
String currencySymbol = quotation.getCurrency().getSymbol();
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
Paragraph t = new Paragraph("SubTotal: ", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.getDefaultCell().setColspan(4);
header.addCell(t);
t = new Paragraph(currencySymbol, new Font(Font.HELVETICA, 11, Font.NORMAL));
header.getDefaultCell().setColspan(0);
header.addCell(t);
t = new Paragraph(numberFormat.format(quotation.getSubtotal()), new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(t);
t = new Paragraph("Impuesto: ", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.getDefaultCell().setColspan(4);
header.addCell(t);
t = new Paragraph(currencySymbol, new Font(Font.HELVETICA, 11, Font.NORMAL));
header.getDefaultCell().setColspan(0);
header.addCell(t);
t = new Paragraph(numberFormat.format(quotation.getTax()), new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(t);
t = new Paragraph("Total: ", new Font(Font.HELVETICA, 11, Font.NORMAL));
header.getDefaultCell().setColspan(4);
header.addCell(t);
t = new Paragraph(currencySymbol, new Font(Font.HELVETICA, 11, Font.NORMAL));
header.getDefaultCell().setColspan(0);
header.addCell(t);
t = new Paragraph(numberFormat.format(quotation.getTotalCost()), new Font(Font.HELVETICA, 11, Font.NORMAL));
header.addCell(t);
document.add(header);
}
public void addFooter(Document document, Quotation quotation) throws DocumentException {
//Table size 3 columns
float[] widths = {0.2f, 0.4f, 0.5f};
PdfPTable header = new PdfPTable(widths);
header.setWidthPercentage(100);
header.getDefaultCell().setBorder(0);
// Line 1
Paragraph p = new Paragraph("Forma de pago: ", new Font(Font.HELVETICA, 10, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
p = new Paragraph(quotation.getPaymentMethod().getDescription(), new Font(Font.HELVETICA, 10, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
header.addCell(new Paragraph(""));
// Line 2
p = new Paragraph("Garantia: ", new Font(Font.HELVETICA, 10, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
String guarantee = quotation.getGuarantee() != null ? quotation.getGuarantee().getDescription() : null;
p = new Paragraph(guarantee, new Font(Font.HELVETICA, 10, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.addCell(p);
header.addCell(new Paragraph(""));
// Line 3
header.getDefaultCell().setColspan(3);
header.addCell(new Paragraph(" "));
header.getDefaultCell().setColspan(3);
header.addCell(new Paragraph(" "));
// Line 4
p = new Paragraph("Agradeciendo su atencion y esperando poder servirle como usted se merece; ", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.getDefaultCell().setColspan(3);
header.addCell(p);
// Line 5
header.getDefaultCell().setColspan(3);
header.addCell(new Paragraph(" "));
header.getDefaultCell().setColspan(3);
header.addCell(new Paragraph(" "));
// Line 6
header.getDefaultCell().setColspan(0);
header.getDefaultCell().setBorder(PdfPCell.TOP);
p = new Paragraph("Ing. Xochilt T. Rocha", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.getDefaultCell().setColspan(2);
header.addCell(p);
header.getDefaultCell().setBorder(PdfPCell.TOP);
p = new Paragraph("Aceptado por el cliente", new Font(Font.HELVETICA, 12, Font.NORMAL));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
header.getDefaultCell().setColspan(2);
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;
}
}