Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.support.services;

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 java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

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.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import in.shop2020.thrift.clients.WarehouseClient;
import in.shop2020.warehouse.LineItem;
import in.shop2020.warehouse.PurchaseOrder;
import in.shop2020.warehouse.Supplier;
import in.shop2020.warehouse.WarehouseService.Client;

public class PdfPoSheetGenerator {
    
    private static Logger logger = LoggerFactory.getLogger(PdfPoSheetGenerator.class);
    
    //private static final Properties properties = readProperties();
    private static final String ourAddress = "Spice Online Retail Pvt. Ltd.\nKhasra No. 819, Block-K\nMahipalpur, New Delhi-110037\n";
    private static final String tinNo = "07250399732";
    
    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());
        poTable.addCell(poSummaryTable);
        poTable.addCell(getSalutationTable(supplier));
        poTable.addCell(getPoDetailsTable(purchaseOrder));
        poTable.addCell(getBillToTable());
        
        return poTable;
    }
    
    private static PdfPCell getAddressCell() {
        Paragraph addressParagraph = new Paragraph(ourAddress + "\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 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(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(){
        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(ourAddress + "\nTIN NO. " + tinNo, helvetica8)));
        
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
        billToTable.addCell(new Phrase("Mr. Pramod Kumar, Contact No. +91 9971573026", helvetica8));
        
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
        billToTable.addCell(new Phrase("Prices inclusive of all taxes", helvetica8));
        
        return billToTable;
    }
    
    private static PdfPCell getProductNameCell(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(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;
    }
    
    /*public static void main(String[] args) throws Exception {
        long purchaseOrderId = 82;

        if (args != null && args.length != 0) {
            purchaseOrderId = Long.parseLong(args[0]);
        }

        WarehouseClient warehouseServiceClient = new WarehouseClient();
        Client client = warehouseServiceClient.getClient();
        in.shop2020.warehouse.PurchaseOrder purchaseOrder = client.getPurchaseOrder(purchaseOrderId);
        Supplier supplier = client.getSupplier(purchaseOrder.getSupplierId());
        String filename = generatePdfSheet(purchaseOrder, supplier);
        System.out.println("PO generated in: " + filename);
    }*/
}