Rev 3044 | Blame | Last modification | View Log | RSS feed
package in.shop2020.support.services;import in.shop2020.config.ConfigException;import in.shop2020.logistics.DeliveryType;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.CatalogClient;import in.shop2020.thrift.clients.LogisticsClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.thrift.clients.config.ConfigClient;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.text.DateFormat;import java.text.DecimalFormat;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.Properties;import org.apache.commons.lang.WordUtils;import org.apache.thrift.TException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.ibm.icu.text.RuleBasedNumberFormat;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.DottedLineSeparator;public class InvoiceGenerationService {private static Logger logger = LoggerFactory.getLogger(InvoiceGenerationService.class);private TransactionClient tsc = null;private CatalogClient csc = null;private LogisticsClient lsc = null;private static Locale indianLocale = new Locale("en", "IN");private DecimalFormat amountFormat = new DecimalFormat("#,##0.00");private static final Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);private static final Font helvetica10 = FontFactory.getFont(FontFactory.HELVETICA, 10);private static final Font helvetica12 = FontFactory.getFont(FontFactory.HELVETICA, 12);private static final Font helvetica16 = FontFactory.getFont(FontFactory.HELVETICA, 16);private static final Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);private static final Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);private static final Properties properties = readProperties();private static final String ourAddress = properties.getProperty("sales_tax_address","Spice Online Retail Pvt. Ltd.\nKhasra No. 819, Block-K\nMahipalpur, New Delhi-110037\n");private static final String tinNo = properties.getProperty("sales_tax_tin", "07250399732");private static final String delhiPincodePrefix = "11";private static final double salesTaxLowRate = Double.parseDouble(properties.getProperty("sales_tax_low_rate", "5.0"));private static final double salesTaxHighRate = Double.parseDouble(properties.getProperty("sales_tax_high_rate", "12.5"));private static final double salesTaxCutOff = (Double.parseDouble(properties.getProperty("sales_tax_cutoff", "10000")) * (100 + salesTaxLowRate))/100;private static Properties readProperties(){Properties props = new Properties();try {InputStream is = InvoiceGenerationService.class.getResourceAsStream("/in/shop2020/support/services/InvoiceGenerationService.properties");props.load(is);} catch (IOException e) {logger.error("Error while reading properties file.", e);}return props;}public InvoiceGenerationService() {try {tsc = new TransactionClient();csc = new CatalogClient();lsc = new LogisticsClient();} catch (Exception e) {logger.error("Error while instantiating thrift clients.", e);}}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;int barcodeFontSize = 0;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());barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));} catch (TransactionServiceException tse) {logger.error("Error while getting order information", tse);return baosPDF;} catch (InventoryServiceException ise) {logger.error("Error while getting the warehouse information.", ise);return baosPDF;} catch (LogisticsServiceException lse) {logger.error("Error while getting the provider information.", lse);return baosPDF;} catch (ConfigException ce) {logger.error("Error while getting the fontsize for the given provider", ce);return baosPDF;} catch (TException te) {logger.error("Error while getting some essential information from the services", te);return baosPDF;}try {baosPDF = new ByteArrayOutputStream();Document document = new Document();PdfWriter.getInstance(document, baosPDF);document.addAuthor("shop2020");document.addTitle("Invoice No: " + order.getInvoice_number());document.open();PdfPTable dispatchAdviceTable = getDispatchAdviceTable(order, warehouse, provider, barcodeFontSize, destCode);dispatchAdviceTable.setSpacingAfter(10.0f);dispatchAdviceTable.setWidthPercentage(90.0f);PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider);taxTable.setSpacingBefore(5.0f);taxTable.setWidthPercentage(90.0f);document.add(dispatchAdviceTable);document.add(new DottedLineSeparator());document.add(taxTable);document.close();baosPDF.close();} catch (Exception e) {logger.error("Error while generating Invoice: ", e);}return baosPDF;}private PdfPTable getDispatchAdviceTable(Order order, Warehouse warehouse, Provider provider, float barcodeFontSize, String destCode){Font barCodeFont = getBarCodeFont(provider, barcodeFontSize);PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(Rectangle.NO_BORDER);PdfPCell logoCell = getLogoCell();PdfPCell titleCell = getTitleCell();PdfPTable customerTable = getCustomerAddressTable(order, destCode, false, helvetica12);PdfPTable providerInfoTable = getProviderTable(order, provider, barCodeFont);PdfPTable dispatchTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});dispatchTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);dispatchTable.addCell(customerTable);dispatchTable.addCell(new Phrase(" "));dispatchTable.addCell(providerInfoTable);PdfPTable invoiceTable = getTopInvoiceTable(order);PdfPCell addressCell = getAddressCell();table.addCell(logoCell);table.addCell(titleCell);table.addCell(dispatchTable);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."));return table;}private Font getBarCodeFont(Provider provider, float barcodeFontSize) {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, barcodeFontSize);return barCodeFont;}private PdfPCell getLogoCell() {String logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();PdfPCell logoCell;try {logoCell = new PdfPCell(Image.getInstance(logoPath), false);} catch (Exception e) {//Too Many exceptions to catch here: BadElementException, MalformedURLException and IOExceptionlogger.warn("Couldn't load the Saholic logo: ", e);logoCell = new PdfPCell(new Phrase("Saholic Logo"));}logoCell.setBorder(Rectangle.NO_BORDER);return logoCell;}private PdfPCell getTitleCell() {PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);titleCell.setBorder(Rectangle.NO_BORDER);return titleCell;}private PdfPTable getProviderTable(Order order, Provider provider, Font barCodeFont) {PdfPTable providerInfoTable = new PdfPTable(1);providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);providerNameCell.setBorder(Rectangle.NO_BORDER);PdfPCell awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));awbNumberCell.setPaddingTop(20.0f);awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);awbNumberCell.setBorder(Rectangle.NO_BORDER);providerInfoTable.addCell(providerNameCell);providerInfoTable.addCell(awbNumberCell);providerInfoTable.addCell(new Phrase("Account No : " + provider.getDetails().get(DeliveryType.PREPAID).getAccountNo(), helvetica8));providerInfoTable.addCell(new Phrase("AWB Date : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));providerInfoTable.addCell(new Phrase("Weight : " + order.getTotal_weight() + " Kg", helvetica8));return providerInfoTable;}private PdfPTable getTopInvoiceTable(Order order){PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);invoiceTable.addCell(getInvoiceTableHeader(6));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));populateTopInvoiceTable(order, invoiceTable);invoiceTable.addCell(getTotalCell(4));invoiceTable.addCell(getRupeesCell());invoiceTable.addCell(getTotalAmountCell(order.getTotal_amount()));PdfPCell tinCell = new PdfPCell(new Phrase("TIN NO. " + tinNo, helvetica8));tinCell.setColspan(6);tinCell.setPadding(2);invoiceTable.addCell(tinCell);return invoiceTable;}private void populateTopInvoiceTable(Order order, PdfPTable invoiceTable) {List<LineItem> lineitems = order.getLineitems();for (LineItem lineitem : lineitems) {invoiceTable.addCell(new Phrase(order.getId() + "", helvetica8));invoiceTable.addCell(new Phrase("Prepaid", helvetica8));invoiceTable.addCell(getProductNameCell(lineitem, false));invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));invoiceTable.addCell(getPriceCell(lineitem.getUnit_price()));invoiceTable.addCell(getPriceCell(lineitem.getTotal_price()));}}private PdfPCell getAddressCell() {Paragraph addressParagraph = new Paragraph(ourAddress, new Font(FontFamily.TIMES_ROMAN, 8f));PdfPCell addressCell = new PdfPCell();addressCell.addElement(addressParagraph);addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);addressCell.setBorder(Rectangle.NO_BORDER);return addressCell;}private PdfPTable getTaxCumRetailInvoiceTable(Order order, Provider provider){PdfPTable taxTable = new PdfPTable(1);taxTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);taxTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);PdfPCell retailInvoiceTitleCell = new PdfPCell(new Phrase("TAX CUM RETAIL INVOICE", helveticaBold12));retailInvoiceTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);retailInvoiceTitleCell.setBorder(Rectangle.NO_BORDER);Paragraph sorlAddress = new Paragraph(ourAddress + "TIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f, Element.ALIGN_CENTER));PdfPCell sorlAddressCell = new PdfPCell(sorlAddress);sorlAddressCell.addElement(sorlAddress);sorlAddressCell.setHorizontalAlignment(Element.ALIGN_CENTER);PdfPTable customerAddress = getCustomerAddressTable(order, null, true, helvetica8);PdfPTable orderDetails = getOrderDetails(order, provider);PdfPTable addrAndOrderDetailsTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});addrAndOrderDetailsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);addrAndOrderDetailsTable.addCell(customerAddress);addrAndOrderDetailsTable.addCell(new Phrase(" "));addrAndOrderDetailsTable.addCell(orderDetails);boolean isVAT = order.getCustomer_pincode().startsWith(delhiPincodePrefix);PdfPTable invoiceTable = getBottomInvoiceTable(order, isVAT);PdfPCell disclaimerCell = new PdfPCell(new Phrase("Goods once sold will not be taken back.\nAll disputes subject to Delhi Jurisdiction.", helvetica8));disclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);disclaimerCell.setBorder(Rectangle.NO_BORDER);taxTable.addCell(retailInvoiceTitleCell);taxTable.addCell(sorlAddress);taxTable.addCell(addrAndOrderDetailsTable);taxTable.addCell(invoiceTable);taxTable.addCell(disclaimerCell);return taxTable;}private PdfPTable getCustomerAddressTable(Order order, String destCode, boolean showPaymentMode, Font font){PdfPTable customerTable = new PdfPTable(1);customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);customerTable.addCell(new Phrase(order.getCustomer_name(), font));customerTable.addCell(new Phrase(order.getCustomer_address1(), font));customerTable.addCell(new Phrase(order.getCustomer_address2(), font));customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), font));if(destCode != null)customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));elsecustomerTable.addCell(new Phrase(order.getCustomer_pincode(), font));customerTable.addCell(new Phrase("Phone :" + order.getCustomer_mobilenumber(), font));if(showPaymentMode){customerTable.addCell(new Phrase(" ", font));customerTable.addCell(new Phrase("Payment Mode: Prepaid", font));}return customerTable;}private PdfPTable getOrderDetails(Order order, Provider provider){PdfPTable orderTable = new PdfPTable(new float[]{0.4f, 0.6f});orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);orderTable.addCell(new Phrase("Invoice No:", helvetica8));orderTable.addCell(new Phrase(order.getInvoice_number(), helvetica8));orderTable.addCell(new Phrase("Date:", helvetica8));orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));orderTable.addCell(new Phrase(" "));orderTable.addCell(new Phrase(" "));orderTable.addCell(new Phrase("Order ID:", helvetica8));orderTable.addCell(new Phrase("" + order.getId(), helvetica8));orderTable.addCell(new Phrase("Order Date:", helvetica8));orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getCreated_timestamp())), helvetica8));orderTable.addCell(new Phrase("Courier:", helvetica8));orderTable.addCell(new Phrase(provider.getName(), helvetica8));orderTable.addCell(new Phrase("AWB No:", helvetica8));orderTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));orderTable.addCell(new Phrase("AWB Date:", helvetica8));orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));return orderTable;}private PdfPTable getBottomInvoiceTable(Order order, boolean isVAT){PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.5f, 0.1f, 0.1f, 0.1f});invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);invoiceTable.addCell(getInvoiceTableHeader(5));invoiceTable.addCell(new Phrase("Sl. No.", helveticaBold8));invoiceTable.addCell(new Phrase("Description", helveticaBold8));invoiceTable.addCell(new Phrase("Quantity", helveticaBold8));invoiceTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));invoiceTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));double orderAmount = order.getTotal_amount();double rate = getTaxRate(orderAmount);double salesTax = (rate * orderAmount)/(100 + rate);populateBottomInvoiceTable(order, invoiceTable, rate);PdfPCell salesTaxCell = getPriceCell(salesTax);invoiceTable.addCell(getVATLabelCell(isVAT));invoiceTable.addCell(new Phrase(rate + "%", helvetica8));invoiceTable.addCell(salesTaxCell);invoiceTable.addCell(getEmptyCell(5));invoiceTable.addCell(getTotalCell(3));invoiceTable.addCell(getRupeesCell());invoiceTable.addCell(getTotalAmountCell(orderAmount));invoiceTable.addCell(new Phrase("Amount in Words:", helvetica8));invoiceTable.addCell(getAmountInWordsCell(orderAmount));invoiceTable.addCell(getEOECell(5));return invoiceTable;}private PdfPCell getInvoiceTableHeader(int colspan) {PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));invoiceTableHeader.setBorder(Rectangle.NO_BORDER);invoiceTableHeader.setColspan(colspan);invoiceTableHeader.setPaddingTop(10);return invoiceTableHeader;}private double getTaxRate(double orderAmount) {double rate;if(orderAmount <= salesTaxCutOff){rate = salesTaxLowRate;} else {rate = salesTaxHighRate;}return rate;}private void populateBottomInvoiceTable(Order order, PdfPTable invoiceTable, double rate) {for (LineItem lineitem : order.getLineitems()) {invoiceTable.addCell(new Phrase("" + order.getId() , helvetica8));invoiceTable.addCell(getProductNameCell(lineitem, true));invoiceTable.addCell(new Phrase("" + lineitem.getQuantity(), helvetica8));double itemPrice = lineitem.getUnit_price();double showPrice = (100 * itemPrice)/(100 + rate);invoiceTable.addCell(getPriceCell(showPrice)); //Unit Price Celldouble totalPrice = lineitem.getTotal_price();showPrice = (100 * totalPrice)/(100 + rate);invoiceTable.addCell(getPriceCell(showPrice)); //Total Price Cell}}private PdfPCell getProductNameCell(LineItem lineitem, boolean appendIMEI) {String itemName = getItemDisplayName(lineitem, appendIMEI);PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);return productNameCell;}private PdfPCell getPriceCell(double price) {PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(price), helvetica8));totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return totalPriceCell;}private PdfPCell getVATLabelCell(boolean isVAT) {PdfPCell vatCell = null;if(isVAT){vatCell = new PdfPCell(new Phrase("VAT", helveticaBold8));} else {vatCell = new PdfPCell(new Phrase("CST", helveticaBold8));}vatCell.setColspan(3);vatCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return vatCell;}private PdfPCell getEmptyCell(int colspan) {PdfPCell emptyCell = new PdfPCell(new Phrase(" ", helvetica8));emptyCell.setColspan(colspan);return emptyCell;}private PdfPCell getTotalCell(int colspan) {PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));totalCell.setColspan(colspan);totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return totalCell;}private PdfPCell getRupeesCell() {PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return rupeesCell;}private PdfPCell getTotalAmountCell(double orderAmount) {PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(orderAmount), helveticaBold8));totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return totalAmountCell;}/*** This method uses ICU4J libraries to convert the given amount into words* of Indian locale.** @param orderAmount* The amount to convert.* @return the string representation of the given amount.*/private PdfPCell getAmountInWordsCell(double orderAmount) {RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);StringBuilder amountInWords = new StringBuilder("Rs. ");amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)orderAmount)));amountInWords.append(" and ");amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(orderAmount*100)%100)));amountInWords.append(" paise");PdfPCell amountInWordsCell= new PdfPCell(new Phrase(amountInWords.toString(), helveticaBold8));amountInWordsCell.setColspan(4);return amountInWordsCell;}/*** Returns the item name to be displayed in the invoice table.** @param lineitem* The line item whose name has to be displayed* @param appendIMEI* Whether to attach the IMEI No. to the item name* @return The name to be displayed for the given line item.*/private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){StringBuffer itemName = new StringBuffer();if(lineitem.getBrand()!= null)itemName.append(lineitem.getBrand() + " ");if(lineitem.getModel_name() != null)itemName.append(lineitem.getModel_name() + " ");if(lineitem.getModel_number() != null )itemName.append(lineitem.getModel_number() + " ");if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))itemName.append("("+lineitem.getColor()+")");if(appendIMEI && lineitem.isSetImei_number()){itemName.append("\nIMEI No. " + lineitem.getImei_number());}return itemName.toString();}/**** @param colspan* @return a PdfPCell containing the E&OE text and spanning the given* no. of columns*/private PdfPCell getEOECell(int colspan) {PdfPCell eoeCell = new PdfPCell(new Phrase("E & O.E", helvetica8));eoeCell.setColspan(colspan);eoeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);return eoeCell;}public static void main(String[] args) throws IOException {InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();long orderId = 434;ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId);String userHome = System.getProperty("user.home");File f = new File(userHome + "/invoice-" + orderId + ".pdf");FileOutputStream fos = new FileOutputStream(f);baos.writeTo(fos);System.out.println("Invoice generated.");}}