Rev 676 | Blame | Last modification | View Log | RSS feed
package in.shop2020.support.services;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.logistics.Provider;import in.shop2020.model.v1.catalog.InventoryService.Client;import in.shop2020.model.v1.catalog.InventoryServiceException;import in.shop2020.model.v1.catalog.Warehouse;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.thrift.clients.CatalogServiceClient;import in.shop2020.thrift.clients.LogisticsServiceClient;import in.shop2020.thrift.clients.TransactionServiceClient;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.DateFormat;import java.text.DecimalFormat;import java.util.Date;import java.util.List;import org.apache.thrift.TException;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.FontFactoryImp;import com.itextpdf.text.Image;import com.itextpdf.text.Paragraph;import com.itextpdf.text.Phrase;import com.itextpdf.text.Rectangle;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;public class InvoiceGenerationService {private TransactionServiceClient tsc = null;private CatalogServiceClient csc = null;private LogisticsServiceClient lsc = null;private DecimalFormat amountFormat = new DecimalFormat("#,##0.00");public InvoiceGenerationService() {try {tsc = new TransactionServiceClient();csc = new CatalogServiceClient();lsc = new LogisticsServiceClient();} catch (Exception e) {e.printStackTrace();}}public ByteArrayOutputStream generateInvoice(long orderId) {ByteArrayOutputStream baosPDF = null;in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();Client iclient = csc.getClient();in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();Order order = null;Warehouse warehouse = null;Provider provider = null;String destCode = null;try {order = tclient.getOrder(orderId);warehouse = iclient.getWarehouse(order.getWarehouse_id());long providerId = order.getLogistics_provider_id();provider = logisticsClient.getProvider(providerId);destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());} catch (TransactionServiceException e1) {e1.printStackTrace();return baosPDF;} catch (TException e1) {e1.printStackTrace();return baosPDF;} catch (InventoryServiceException e) {e.printStackTrace();return baosPDF;} catch (LogisticsServiceException e) {e.printStackTrace();return baosPDF;}try {baosPDF = new ByteArrayOutputStream();String fontPath = InvoiceGenerationService.class.getResource("/" + provider.getName().toLowerCase() + "/barcode.TTF").getPath();FontFactoryImp ttfFontFactory = new FontFactoryImp();ttfFontFactory.register(fontPath, "barcode");Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 32);Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);Font helvetica10 = FontFactory.getFont(FontFactory.HELVETICA, 10);Font helvetica12 = FontFactory.getFont(FontFactory.HELVETICA, 12);Font helvetica16 = FontFactory.getFont(FontFactory.HELVETICA, 16);Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);Document document = new Document();PdfWriter.getInstance(document, baosPDF);document.addAuthor("shop2020");document.addTitle("Invoice No: " + order.getInvoice_number());document.open();PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(Rectangle.NO_BORDER);String logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);logoCell.setBorder(Rectangle.NO_BORDER);String addressString = warehouse.getLocation() + "\nPIN " + warehouse.getPincode() + "\n\n";Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN, 8f));PdfPCell addressCell = new PdfPCell();addressCell.addElement(addressParagraph);addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);addressCell.setBorder(Rectangle.NO_BORDER);PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);titleCell.setBorder(Rectangle.NO_BORDER);PdfPTable providerInfoTable = new PdfPTable(2);providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);providerNameCell.setColspan(2);providerNameCell.setBorder(Rectangle.NO_BORDER);PdfPCell awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));awbNumberCell.setPaddingTop(20.0f);awbNumberCell.setPaddingBottom(10.0f);awbNumberCell.setRowspan(3);awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);awbNumberCell.setBorder(Rectangle.NO_BORDER);providerInfoTable.addCell(providerNameCell);providerInfoTable.addCell(awbNumberCell);providerInfoTable.addCell(new Phrase("Account No : " + provider.getAccountNo(), helvetica8));providerInfoTable.addCell(new Phrase("AWB Date : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));providerInfoTable.addCell(new Phrase("Weight : " + order.getTotal_weight()/1000.0 + " Kg", helvetica8));PdfPTable customerTable = new PdfPTable(1);customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);PdfPTable nameTable = new PdfPTable(2);nameTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);nameTable.addCell(new Phrase(order.getCustomer_name(), helvetica12));PdfPCell phoneCell = new PdfPCell(new Phrase("Phone :" + order.getCustomer_mobilenumber(), helvetica12));phoneCell.setHorizontalAlignment(Element.ALIGN_RIGHT);phoneCell.setBorder(Rectangle.NO_BORDER);nameTable.addCell(phoneCell);PdfPCell nameCell = new PdfPCell(nameTable);nameCell.setBorder(Rectangle.NO_BORDER);customerTable.addCell(nameCell);customerTable.addCell(new Phrase(order.getCustomer_address1(), helvetica12));customerTable.addCell(new Phrase(order.getCustomer_address2(), helvetica12));customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), helvetica12));customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));PdfPTable invoiceTable = new PdfPTable(6);invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);//invoiceTable.setSpacingAfter(20);//invoiceTable.setSpacingBefore(20);PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));invoiceTableHeader.setBorder(Rectangle.NO_BORDER);invoiceTableHeader.setColspan(6);invoiceTableHeader.setPaddingTop(10);invoiceTable.addCell(invoiceTableHeader);invoiceTable.addCell(new Phrase("Order No", helvetica8));invoiceTable.addCell(new Phrase("Paymode", helvetica8));invoiceTable.addCell(new Phrase("Product Name", helvetica8));invoiceTable.addCell(new Phrase("Quantity", helvetica8));invoiceTable.addCell(new Phrase("Rate", helvetica8));invoiceTable.addCell(new Phrase("Amount", helvetica8));List<LineItem> lineitems = order.getLineitems();double totalitems = 0;for (LineItem lineitem : lineitems) {invoiceTable.addCell(new Phrase(orderId + "", helvetica8));invoiceTable.addCell(new Phrase("Prepaid", helvetica8));PdfPCell productNameCell = new PdfPCell(new Phrase(lineitem.getBrand() + " "+ lineitem.getModel_number() + " "+ lineitem.getModel_name() + " " + lineitem.getColor(),helvetica8));productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);invoiceTable.addCell(productNameCell);invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));PdfPCell unitPriceCell = new PdfPCell(new Phrase(amountFormat.format(lineitem.getUnit_price()), helvetica8));unitPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);invoiceTable.addCell(unitPriceCell);PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(lineitem.getTotal_price()), helvetica8));totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);invoiceTable.addCell(totalPriceCell);totalitems = totalitems + lineitem.getQuantity();}PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));totalCell.setColspan(4);invoiceTable.addCell(totalCell);PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);invoiceTable.addCell(rupeesCell);PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(order.getTotal_amount()) + "", helveticaBold8));totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);invoiceTable.addCell(totalAmountCell);PdfPCell tinCell = new PdfPCell(new Phrase("TIN No: " + warehouse.getTinNumber(), helvetica8));tinCell.setColspan(6);tinCell.setPadding(2);invoiceTable.addCell(tinCell);table.addCell(logoCell);table.addCell(titleCell);table.addCell(providerInfoTable);table.addCell(customerTable);table.addCell(invoiceTable);table.addCell(new Phrase("If undelivered, return to:", helvetica10));table.addCell(addressCell);table.addCell(new Phrase("Do not pay any extra charges to the Courier."));table.setWidthPercentage(90.0f);document.add(table);document.close();baosPDF.close();} catch (Exception e) {e.printStackTrace();}return baosPDF;}public static void main(String[] args) throws IOException {InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(13);File f = new File("/home/ashish/Downloads/invoice-13.pdf");FileOutputStream fos = new FileOutputStream(f);baos.writeTo(fos);}}