Subversion Repositories SmartDukaan

Rev

Rev 670 | 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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import org.apache.thrift.TException;

import com.itextpdf.text.BaseColor;
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;
import com.itextpdf.text.pdf.draw.LineSeparator;


public class InvoiceGenerationService {
        private static TransactionServiceClient tsc = null;
        private static CatalogServiceClient csc = null;
        private static LogisticsServiceClient lsc = null;
        
        static {
                try {
                        tsc = new TransactionServiceClient();
                        csc = new CatalogServiceClient();
                        lsc = new LogisticsServiceClient();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static 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;
                try {
                        order = tclient.getOrder(orderId);
                        warehouse = iclient.getWarehouse(order.getWarehouse_id());
                        provider = logisticsClient.getProvider(order.getLogistics_provider_id());
                } 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 {
        //             OutputStream file = new FileOutputStream(new File("/home/rajveer/Test.pdf"));
             baosPDF = new ByteArrayOutputStream();
             
             String fontPath = InvoiceGenerationService.class.getResource("/C39TXME.TTF").getPath();
             FontFactoryImp ttfFontFactory = new FontFactoryImp();
             ttfFontFactory.register(fontPath, "barcode");
             Font barCode = ttfFontFactory.getFont("barcode", 30);
             Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
             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.png").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", FontFactory.getFont(FontFactory.HELVETICA, 20)));
             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("Courier Name:"  + provider.getName(), helvetica8));
             providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
             providerNameCell.setColspan(2);
             providerNameCell.setBorder(Rectangle.NO_BORDER);
             
             PdfPCell awbNumberCell = new PdfPCell(new Paragraph(order.getAirwaybill_no()+"", barCode));
             awbNumberCell.setPaddingBottom(10.0f);
             awbNumberCell.setRowspan(2);
             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.SHORT).format(new Date()), helvetica8));
             
             
             PdfPTable customerTable = new PdfPTable(new float[]{1f, 0.2f, 3f});
             customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
             customerTable.addCell(new Phrase("Name", helvetica8));
             customerTable.addCell(new Phrase(":", helvetica8));
             customerTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
             
             customerTable.addCell(new Phrase("Address", helvetica8));
             customerTable.addCell(new Phrase(":", helvetica8));
             customerTable.addCell(new Phrase(order.getCustomer_address(), helvetica8));
             
             customerTable.addCell(new Phrase("City", helvetica8));
             customerTable.addCell(new Phrase(":", helvetica8));
             customerTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
             
             customerTable.addCell(new Phrase("State", helvetica8));
             customerTable.addCell(new Phrase(":", helvetica8));
             customerTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));
             
             customerTable.addCell(new Phrase("PIN", helvetica8));
             customerTable.addCell(new Phrase(":", helvetica8));
             customerTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
             
             customerTable.addCell(new Phrase("Phone", helvetica8));
             customerTable.addCell(new Phrase(":", helvetica8));
             customerTable.addCell(new Phrase(order.getCustomer_mobilenumber()+"", helvetica8));
             
             PdfPTable invoiceTable = new PdfPTable(6);
             invoiceTable.setSpacingAfter(20);
             invoiceTable.setSpacingBefore(20);
             
             PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8)));
             invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
             invoiceTableHeader.setColspan(6);
             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("EXP", helvetica8));
                 invoiceTable.addCell(new Phrase(lineitem.getBrand() + " " + lineitem.getModel_number() + " " + lineitem.getModel_name() + " " + lineitem.getColor(), helvetica8));
                     invoiceTable.addCell(new Phrase(lineitem.getQuantity()+"", helvetica8));
                     invoiceTable.addCell(new Phrase(lineitem.getUnit_price()+"", helvetica8));
                     invoiceTable.addCell(new Phrase(lineitem.getTotal_price()+"", helvetica8));
                     totalitems = totalitems + lineitem.getQuantity();
             }
             PdfPCell totalCell = new PdfPCell(new Phrase("Total", helvetica8));
             totalCell.setColspan(4);
         invoiceTable.addCell(totalCell);
             invoiceTable.addCell(new Phrase(totalitems+"", helvetica8));
             invoiceTable.addCell(new Phrase(order.getTotal_amount()+"", helvetica8));
             
             PdfPCell cstCell = new PdfPCell(new Phrase("CST No: " , helvetica8));
             PdfPCell tinCell = new PdfPCell(new Phrase("TIN No: " + warehouse.getTinNumber(), helvetica8));
             cstCell.setColspan(3);
             tinCell.setColspan(3);
             cstCell.setBorderWidthRight(0);
             tinCell.setBorderWidthLeft(0);
             invoiceTable.addCell(cstCell);
             invoiceTable.addCell(tinCell);
             
             table.addCell(logoCell);
             table.addCell(addressCell);
             table.addCell(titleCell);
             table.addCell(providerInfoTable);
             table.addCell(customerTable);
             table.addCell(invoiceTable);
             table.addCell(new Phrase("If Undelivered:", helvetica8));
             table.addCell(new Phrase("Return to:", helvetica8));
             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 {
                ByteArrayOutputStream baos = InvoiceGenerationService.generateInvoice(1);
                File f = new File("/home/rajveer/Desktop/one.pdf");
                FileOutputStream fos = new FileOutputStream(f);
                baos.writeTo(fos);
        }
}