Rev 21149 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.services;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.logistics.PickUpType;import in.shop2020.logistics.PickupStore;import in.shop2020.logistics.Provider;import in.shop2020.model.v1.inventory.InventoryServiceException;import in.shop2020.model.v1.inventory.Warehouse;import in.shop2020.model.v1.order.BuyerInfo;import in.shop2020.model.v1.order.EbayOrder;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.OrderStatus;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.model.v1.order.WarehouseAddress;import in.shop2020.thrift.clients.InventoryClient;import in.shop2020.thrift.clients.LogisticsClient;import in.shop2020.thrift.clients.TransactionClient;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.MalformedURLException;import java.text.DateFormat;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.thrift.TException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;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 ManifestGenerator {private static Logger logger = LoggerFactory.getLogger(ManifestGenerator.class);private TransactionClient tsc = null;private InventoryClient csc = null;private LogisticsClient lsc = null;private DecimalFormat weightFormat = new DecimalFormat("0.000");private static DateFormat df = new SimpleDateFormat("MMddyy");public ManifestGenerator() {try {tsc = new TransactionClient();csc = new InventoryClient();lsc = new LogisticsClient();} catch (Exception e) {logger.error("Error while initializing one of the thrift clients", e);}}public ByteArrayOutputStream generateManifestFile(long warehouseId, long providerId, boolean isCod, List<Long> orderIds, String runner) {logger.info("Generating manifest file for warehouse "+warehouseId+" providerId "+providerId+" cod= "+isCod);ByteArrayOutputStream baosPDF = null;in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();List<OrderStatus> statuses = new ArrayList<OrderStatus>();statuses.add(OrderStatus.BILLED);List<Order> orders = null;Provider provider = null;List<PickupStore> stores = null;BuyerInfo buyerInfo = null;try {orders = txnClient.getOrdersInBatch(statuses, 0, 0, warehouseId, 0);buyerInfo = txnClient.getBuyerByWarehouse(warehouseId);provider = logisticsClient.getProvider(providerId);stores = logisticsClient.getAllPickupStores();} catch (TException e) {logger.error("Error getting information from one of the Thrift Services: ", e);return baosPDF;} catch (LogisticsServiceException e) {logger.error("Error getting provider info from the logistics service: ", e);return baosPDF;} catch (TransactionServiceException e) {logger.error("Error getting orders from the transaction service: ", e);return baosPDF;}try {WarehouseAddress wha = buyerInfo.getBuyerAddress();baosPDF = new ByteArrayOutputStream();Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);Font helvetica9 = FontFactory.getFont(FontFactory.HELVETICA, 9);Document document = new Document();PdfWriter.getInstance(document, baosPDF);document.addAuthor("shop2020");document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);document.open();PdfPTable table = new PdfPTable(3);table.getDefaultCell().setBorder(Rectangle.NO_BORDER);table.getDefaultCell().setPaddingBottom(10.0f);String addressString = wha.getAddress() + " PIN - " + wha.getPin()+ "\n\n";PdfPTable ordersTable = null;ordersTable = new PdfPTable(8);if (providerId > 7 && providerId <12) {ordersTable = new PdfPTable(10);} else {ordersTable.setWidths(new float[]{0.8f, 2, 2, 1.1f, 2, 2, 1.1f, 2});}ordersTable.addCell(new Phrase("Sl No", helvetica8));ordersTable.addCell(new Phrase("Master Order Id", helvetica8));ordersTable.addCell(new Phrase("AWB No", helvetica8));ordersTable.addCell(new Phrase("Packet Wt.(Kg)", helvetica8));//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayIdif(providerId > 7 && providerId <12) {ordersTable.addCell(new Phrase("PaisaPayId", helvetica8));ordersTable.addCell(new Phrase("Sales Rec No.", helvetica8));}if(runner == null){ordersTable.addCell(new Phrase("Name", helvetica8));ordersTable.addCell(new Phrase("Destination City", helvetica8));ordersTable.addCell(new Phrase("Pincode", helvetica8));ordersTable.addCell(new Phrase("State", helvetica8));}else{ordersTable.addCell(new Phrase("Store Code", helvetica8));ordersTable.addCell(new Phrase("Store Address", helvetica8));ordersTable.addCell(new Phrase("Store City", helvetica8));ordersTable.addCell(new Phrase("Phone", helvetica8));}Map<String, Double> totalWeightMap = new HashMap<String, Double>();Map<String, String> airwayBillNoMap = new HashMap<String, String>();Map<String, Order> orderDetailsMap = new HashMap<String, Order>();List<Order> selectedOrders = new ArrayList<Order>();for(int i=0; i < orders.size();i++){Order order = orders.get(i);if(!orderIds.contains(order.getId()))continue;if(order.getLogistics_provider_id()!=providerId)continue;if(order.isLogisticsCod() != isCod) {if (order.getNet_payable_amount() > 0) continue;}selectedOrders.add(order);}for(Order order: selectedOrders){if(orderDetailsMap.containsKey(order.getLogisticsTransactionId())){continue;} else {if(order.getLogisticsTransactionId()!=null){orderDetailsMap.put(order.getLogisticsTransactionId(), order);}else {orderDetailsMap.put(order.getId()+"", order);}}}for(Order order: selectedOrders){if(order.getLogisticsTransactionId()!=null){if(totalWeightMap.containsKey(order.getLogisticsTransactionId())){double totalWeight = totalWeightMap.get(order.getLogisticsTransactionId())+ order.getTotal_weight();totalWeightMap.put(order.getLogisticsTransactionId(), totalWeight);}else{double totalWeight = order.getTotal_weight();totalWeightMap.put(order.getLogisticsTransactionId(), totalWeight);}if(airwayBillNoMap.containsKey(order.getLogisticsTransactionId())){continue;}else{airwayBillNoMap.put(order.getLogisticsTransactionId(), order.getAirwaybill_no());}}else{airwayBillNoMap.put(order.getId()+"", order.getAirwaybill_no());totalWeightMap.put(order.getId()+"", order.getTotal_weight());}}int serialNo = 0;for(String logisticsTxnId : airwayBillNoMap.keySet()){Order singleOrder = orderDetailsMap.get(logisticsTxnId);serialNo++;ordersTable.addCell(new Phrase(serialNo + "", helvetica8));ordersTable.addCell(new Phrase(logisticsTxnId, helvetica8));ordersTable.addCell(new Phrase(airwayBillNoMap.get(logisticsTxnId), helvetica8));ordersTable.addCell(new Phrase(weightFormat.format(totalWeightMap.get(logisticsTxnId)), helvetica8));if(providerId > 7 && providerId <12) {try {EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(singleOrder.getId());ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));} catch (Exception e) {logger.error("Error getting Ebay Order for OrderId : " + singleOrder.getId(), e);return baosPDF;}}if(runner == null){ordersTable.addCell(new Phrase(singleOrder.getCustomer_name(), helvetica8));ordersTable.addCell(new Phrase(singleOrder.getCustomer_city(), helvetica8));ordersTable.addCell(new Phrase(singleOrder.getCustomer_pincode(), helvetica8));ordersTable.addCell(new Phrase(singleOrder.getCustomer_state(), helvetica8));}else{PickupStore store = getStoreFromId(stores, singleOrder.getPickupStoreId());ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));ordersTable.addCell(new Phrase(store.getCity(), helvetica8));ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));}}if(isCod) {table.addCell(new Phrase("Manifest ID: C" + df.format(new Date()) + String.format("%04d", warehouseId) + String.format("%02d", providerId), helvetica9));table.addCell(new Phrase("PAYMODE: COD", helvetica9));}else {table.addCell(new Phrase("Manifest ID: P" + df.format(new Date()) + String.format("%04d", buyerInfo.getAddressId()) + String.format("%02d", providerId), helvetica8));table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));}if(provider.getPickup() == PickUpType.RUNNER){table.addCell(new Phrase("Runner Name: " + runner, helvetica8));table.addCell(new Phrase("Manifest Type: Runner Manifest", helvetica8));table.addCell(new Phrase("", helvetica8));}else{table.addCell(new Phrase("Carrier Name: " + provider.getName(), helvetica8));table.addCell(new Phrase("Carriere Account: " + provider.getDetails().get(0).getAccountNo(), helvetica8));table.addCell(new Phrase("Manifest Type: Courier Manifest", helvetica8));}table.addCell(new Phrase("Dispatch Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));table.addCell(new Phrase("No. of Shipments: " + airwayBillNoMap.size(), helvetica8));table.addCell(new Phrase(" ", helvetica8));table.addCell(new Phrase(" ", helvetica8));document.add(new Paragraph(buyerInfo.getOrganisationName() + " Manifest Report", new Font(FontFamily.TIMES_ROMAN,12f)));document.add(new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f)));document.add(table);document.add(ordersTable);document.add(new Paragraph("\nPick Employee Name: Signature: ", helvetica9));document.add(new Paragraph("\nPickup By Location: ", helvetica9));document.add(new Paragraph("\nPickup Vehicle No: ", helvetica9));document.close();baosPDF.close();} catch (DocumentException e) {logger.error("Error while creating the manifest file", e);} catch (MalformedURLException e) {logger.error("Error while creating the manifest file", e);} catch (IOException e) {logger.error("Error while creating the manifest file", e);}String cod_str = isCod?"COD":"PREPAID";String filename_manifest = "/manifest-reports/manifest-"+warehouseId+"-"+providerId+"-"+cod_str+"-"+Calendar.getInstance().getTimeInMillis()+".pdf";try{FileOutputStream fos = new FileOutputStream(filename_manifest);baosPDF.writeTo(fos);}catch(Exception manifestWriteException){manifestWriteException.printStackTrace();}return baosPDF;}private static PickupStore getStoreFromId(List<PickupStore> stores, long storeId){for(PickupStore store: stores){if(store.getId() == storeId)return store;}return null;}public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds, List<String> awbs) {ByteArrayOutputStream baosPDF = null;in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();List<OrderStatus> statuses = new ArrayList<OrderStatus>();statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);List<Order> orders = null;PickupStore store = null;Provider provider = null;try {orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0, 0);store = logisticsClient.getPickupStore(storeId);provider = logisticsClient.getProvider(providerId);} catch (TException e) {logger.error("Error getting information from one of the Thrift Services: ", e);return baosPDF;} catch (LogisticsServiceException e) {logger.error("Error getting provider info from the logistics service: ", e);return baosPDF;} catch (TransactionServiceException e) {logger.error("Error getting orders from the transaction service: ", e);return baosPDF;}try {baosPDF = new ByteArrayOutputStream();Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);Document document = new Document();PdfWriter.getInstance(document, baosPDF);document.addAuthor("shop2020");document.addTitle("Manifest for store " + storeId + " provider " + providerId);document.open();PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(Rectangle.NO_BORDER);table.getDefaultCell().setPaddingBottom(10.0f);String addressString = store.getName() + "\n" + store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2())+ "\n" + store.getCity() + "\n" + store.getState() + "\nPIN " + store.getPin()+ "\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);PdfPTable ordersTable = new PdfPTable(4);ordersTable.addCell(new Phrase("Sl No", helvetica8));ordersTable.addCell(new Phrase("Order No", helvetica8));ordersTable.addCell(new Phrase("AWB No", helvetica8));ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));int serialNo = 0;for(int i=0; i < orders.size();i++){Order order = orders.get(i);if(!orderIds.contains(order.getId()))continue;if(order.getLogistics_provider_id()!=providerId)continue;if(order.getPickupStoreId()!=storeId)continue;//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?serialNo++;List<LineItem> lineItems = order.getLineitems();double weight = 0;for(LineItem lineItem: lineItems)weight += lineItem.getTotal_weight();ordersTable.addCell(new Phrase(serialNo + "", helvetica8));ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));ordersTable.addCell(new Phrase(awbs.get(orderIds.indexOf(order.getId())), helvetica8));ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));}table.addCell(addressCell);table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));table.addCell(ordersTable);table.setWidthPercentage(90.0f);document.add(table);document.close();baosPDF.close();} catch (DocumentException e) {logger.error("Error while creating the manifest file", e);} catch (MalformedURLException e) {logger.error("Error while creating the manifest file", e);} catch (IOException e) {logger.error("Error while creating the manifest file", e);}return baosPDF;}public static void main(String[] args) throws IOException {ManifestGenerator manifestGenerator = new ManifestGenerator();ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null, null);File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");FileOutputStream fos = new FileOutputStream(f);baos.writeTo(fos);}}