Rev 7466 | Rev 8713 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.inventory.service;import in.shop2020.purchase.PurchaseOrder;import in.shop2020.purchase.Supplier;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.DateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.itextpdf.text.Document;import com.itextpdf.text.Element;import com.itextpdf.text.Font;import com.itextpdf.text.Font.FontFamily;import com.itextpdf.text.FontFactory;import com.itextpdf.text.Paragraph;import com.itextpdf.text.Phrase;import com.itextpdf.text.Rectangle;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;public class PdfPoSheetGenerator {private static Logger logger = LoggerFactory.getLogger(PdfPoSheetGenerator.class);// private static final Properties properties = readProperties();private static final String ourAddressDelhi = "Spice Online Retail Pvt. Ltd.\nC/O,PIBCO LIMITED, Basement,Punjsons\n2,Kalkaji Industrial Area, New Delhi-110019\n";private static final String ourAddressBhiwandi = "Spice Online Retail Pvt. Ltd.\nC/O. FedEx Express Transportation and Supply Chain Services (India) Private Limited.\nC/O NDR WAREHOUSING, SURVEY NO.95, MUMBAI - NASIK HIGHWAY, WADAPE VILLAGE\nBHIWANDI (NR. SAI DHABA), Thane,Maharashtra -421302\n";private static final String ourAddressGoregaon = "Spice Online Retail Pvt. Ltd.\n93/743, Motilal Nagar-1, Goregaon(WEST),\nMotilal Nagar, Mumbai, Maharashtra-400062\n";private static final String amazonAddress = "Spice Online Retail Pvt. Ltd. \nC/O Amazon Seller Services Pvt. Ltd.,\nBuilding H Prathmesh Complex, Saravali Village,\nOpp Hotel Vatika Kalyan, Bhivandi Junction,\nBhiwandi, Maharashtra\n";private static final String tinNoDelhi = "07250399732";private static final String tinNoMum = "27450984008";private static final Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);private static final Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);private static final Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);public static String generatePdfSheet(PurchaseOrder purchaseOrder,Supplier supplier) throws IOException {ByteArrayOutputStream baosPDF = null;try {baosPDF = new ByteArrayOutputStream();Document document = new Document();PdfWriter.getInstance(document, baosPDF);document.addAuthor("shop2020");document.addTitle("Purchase Order No: "+ purchaseOrder.getPoNumber());document.open();PdfPTable poTable = getPoTable(purchaseOrder, supplier);poTable.setSpacingAfter(10.0f);poTable.setWidthPercentage(90.0f);document.add(poTable);document.close();baosPDF.close();} catch (Exception e) {logger.error("Error while generating Invoice: ", e);}String tmpDir = System.getProperty("java.io.tmpdir");String filename = tmpDir + "/po-" + purchaseOrder.getId() + ".pdf";File f = new File(filename);FileOutputStream fos = new FileOutputStream(f);baosPDF.writeTo(fos);return filename;}private static PdfPTable getPoTable(PurchaseOrder purchaseOrder,Supplier supplier) throws Exception {PdfPTable poTable = new PdfPTable(1);poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);PdfPCell poTitleCell = new PdfPCell(new Phrase("Purchase Order",helveticaBold12));poTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);poTitleCell.setBorder(Rectangle.NO_BORDER);Date poDate = new Date(purchaseOrder.getCreatedAt());PdfPTable poSummaryTable = new PdfPTable(new float[] { 0.5f, 0.5f });poSummaryTable.addCell(new PdfPCell(new Phrase("PO No: "+ purchaseOrder.getPoNumber())));poSummaryTable.addCell(new PdfPCell(new Phrase("Date: "+ DateFormat.getDateInstance(DateFormat.MEDIUM).format(poDate))));poSummaryTable.setSpacingBefore(10.0f);poTable.addCell(poTitleCell);poTable.addCell(getAddressCell(purchaseOrder.getWarehouseId()));poTable.addCell(poSummaryTable);poTable.addCell(getSalutationTable(supplier));poTable.addCell(getPoDetailsTable(purchaseOrder));poTable.addCell(getBillToTable(purchaseOrder.getWarehouseId()));return poTable;}private static PdfPCell getAddressCell(long warehouseId) {//TODO Write this code in a proper configurable wayString address = "";String tinNo = "";if(warehouseId ==7) {address = ourAddressDelhi;tinNo = tinNoDelhi;} else if(warehouseId == 12) {address = ourAddressGoregaon;tinNo = tinNoMum;} else if(warehouseId == 13) {address = ourAddressBhiwandi;tinNo = tinNoMum;} else if(warehouseId ==16) {address = amazonAddress;tinNo = tinNoMum;}Paragraph addressParagraph = new Paragraph(address + "\nTIN NO. "+ tinNo, new Font(FontFamily.TIMES_ROMAN, 8f));PdfPCell addressCell = new PdfPCell();addressCell.addElement(addressParagraph);addressCell.setHorizontalAlignment(Element.ALIGN_CENTER);addressCell.setBorder(Rectangle.NO_BORDER);return addressCell;}private static PdfPTable getSalutationTable(Supplier supplier)throws Exception {PdfPTable salutationTable = new PdfPTable(1);salutationTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);salutationTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);salutationTable.addCell(new Phrase("To", helvetica8));salutationTable.addCell(new Phrase(supplier.getName(), helvetica8));salutationTable.addCell(new Paragraph(supplier.getCommunicationAddress(), helvetica8));salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));salutationTable.addCell(new Phrase("Please supply the following stocks as per the details given below:",helvetica8));salutationTable.addCell(new Phrase(" "));return salutationTable;}private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,0.2f, 0.2f });detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));detailsTable.addCell(new Phrase("Description", helveticaBold8));detailsTable.addCell(new Phrase("Quantity", helveticaBold8));detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));int slNo = 0;double total = 0;for (in.shop2020.purchase.LineItem lineitem : purchaseOrder.getLineitems()) {slNo++;detailsTable.addCell(new Phrase(slNo + "", helvetica8));detailsTable.addCell(getProductNameCell(lineitem));detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",helvetica8));detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",helvetica8));double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();total += lineTotal;detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));}detailsTable.addCell(getTotalCell(4));detailsTable.addCell(new Phrase("" + total, helvetica8));return detailsTable;}private static PdfPTable getBillToTable(long warehouseId) {//TODO Write this code in a proper configurable wayString address = "";String tinNo = "";String contactPerson = "";if(warehouseId ==7) {address = ourAddressDelhi;tinNo = tinNoDelhi;contactPerson = "Mr. Shiv Kumar, Contact No. +91 9911232226";} else if(warehouseId == 12) {address = ourAddressGoregaon;tinNo = tinNoMum;contactPerson = "Mr. Avinash Sambhaji Lavange, Contact No. +91 9004049589";} else if(warehouseId == 13) {address = ourAddressBhiwandi;tinNo = tinNoMum;contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";} else if(warehouseId ==16) {address = amazonAddress;tinNo = tinNoMum;contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";}PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));billToTable.addCell(new PdfPCell(new Paragraph(address+ "\nTIN NO. " + tinNo, helvetica8)));billToTable.addCell(new Phrase("Contact Person:", helvetica8));billToTable.addCell(new Phrase(contactPerson, helvetica8));billToTable.addCell(new Phrase("Taxes:", helvetica8));billToTable.addCell(new Phrase("Prices inclusive of all taxes",helvetica8));return billToTable;}private static PdfPCell getProductNameCell(in.shop2020.purchase.LineItem lineitem) {String itemName = getItemDisplayName(lineitem);PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);return productNameCell;}private static String getItemDisplayName(in.shop2020.purchase.LineItem lineitem) {StringBuffer itemName = new StringBuffer();if (lineitem.getBrand() != null)itemName.append(lineitem.getBrand() + " ");if (lineitem.getModelName() != null)itemName.append(lineitem.getModelName() + " ");if (lineitem.getModelNumber() != null)itemName.append(lineitem.getModelNumber() + " ");if (lineitem.getColor() != null&& !lineitem.getColor().trim().equals("NA"))itemName.append("(" + lineitem.getColor() + ")");return itemName.toString();}private static PdfPCell getTotalCell(int colspan) {PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));totalCell.setColspan(colspan);totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return totalCell;}}