Rev 13407 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.services;import in.shop2020.logistics.DeliveryType;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.logistics.PickupStore;import in.shop2020.logistics.Provider;import in.shop2020.logistics.ProviderDetails;import in.shop2020.model.v1.inventory.InventoryServiceException;import in.shop2020.model.v1.inventory.Warehouse;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.thrift.clients.InventoryClient;import in.shop2020.thrift.clients.LogisticsClient;import in.shop2020.thrift.clients.TransactionClient;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.thrift.TException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class CourierDetailsGenerator {private static Logger logger = LoggerFactory.getLogger(CourierDetailsGenerator.class);private TransactionClient tsc = null;private InventoryClient isc = null;private LogisticsClient lsc = null;public CourierDetailsGenerator(){try {tsc = new TransactionClient();isc = new InventoryClient();lsc = new LogisticsClient();} catch (Exception e) {logger.error("Error while initializing one of the thrift clients", e);}}public ByteArrayOutputStream generateCourierDetails(long warehouseId, long providerId, boolean isCod){ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = isc.getClient();in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();List<Order> orders = null;Warehouse warehouse = null;Provider provider = null;try {List<OrderStatus> statuses = new ArrayList<OrderStatus>();statuses.add(OrderStatus.BILLED);orders = txnClient.getAllOrders(statuses, 0L, new Date().getTime(), warehouseId);warehouse = inventoryClient.getWarehouse(warehouseId);provider = logisticsClient.getProvider(providerId);} catch (TException e) {logger.error("Error getting information from one of the Thrift Services: ", e);return baosXLS;} catch (InventoryServiceException e) {logger.error("Error getting warehouse info from the catalog service: ", e);return baosXLS;} catch (LogisticsServiceException e) {logger.error("Error getting provider info from the logistics service: ", e);return baosXLS;} catch (TransactionServiceException e) {logger.error("Error getting orders from the transaction service: ", e);return baosXLS;}Workbook wb = new HSSFWorkbook();CreationHelper createHelper = wb.getCreationHelper();Sheet sheet = wb.createSheet("new sheet");CellStyle dateCellStyle = wb.createCellStyle();dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));CellStyle weightStyle = wb.createCellStyle();weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));// Create the header row and put all the titles in it. Rows are 0 based.Row headerRow = sheet.createRow((short)0);headerRow.createCell(0).setCellValue("Sl No");headerRow.createCell(1).setCellValue("AWB No");headerRow.createCell(2).setCellValue("AWB Date");headerRow.createCell(3).setCellValue("Saholic Order Id");headerRow.createCell(4).setCellValue("Name");headerRow.createCell(5).setCellValue("Address 1");headerRow.createCell(6).setCellValue("Address 2");headerRow.createCell(7).setCellValue("City");headerRow.createCell(8).setCellValue("State");headerRow.createCell(9).setCellValue("Pin Code");headerRow.createCell(10).setCellValue("Telephone No 1");headerRow.createCell(11).setCellValue("Telephone No 2");headerRow.createCell(12).setCellValue("Paymode");headerRow.createCell(13).setCellValue("Amount to be Collected");headerRow.createCell(14).setCellValue("Shipment Value");headerRow.createCell(15).setCellValue("Packet Weight(in Kg)");headerRow.createCell(16).setCellValue("Product Name");headerRow.createCell(17).setCellValue("Pickup Location");headerRow.createCell(18).setCellValue("Customer A/C Code");String accountNo = "";DeliveryType dt = DeliveryType.PREPAID;if (isCod) {dt = DeliveryType.COD;}for (ProviderDetails detail : provider.getDetails()) {if(in.shop2020.model.v1.inventory.WarehouseLocation.findByValue((int) detail.getLogisticLocation()) == warehouse.getLogisticsLocation() && detail.getDeliveryType() == dt) {accountNo = detail.getAccountNo();}}Date awbDate = new Date();String location = removeNewLines(warehouse.getLocation());Map<String, List<Order>> logisticsTxnIdOrdersMap = new HashMap<String, List<Order>>();Map<Long, String> itemNamesMap = new HashMap<Long, String>();Map<String, Map<Long, Double>> logisticsTxnIdOrderQuantityMap = new HashMap<String, Map<Long, Double>>();for(Order order:orders){if(order.getLogistics_provider_id()!=providerId)continue;if(order.isLogisticsCod() != isCod)continue;LineItem lineItem = order.getLineitems().get(0);if(order.isSetLogisticsTransactionId()){if(logisticsTxnIdOrdersMap.containsKey(order.getLogisticsTransactionId())){List<Order> orderList = logisticsTxnIdOrdersMap.get(order.getLogisticsTransactionId());orderList.add(order);logisticsTxnIdOrdersMap.put(order.getLogisticsTransactionId(), orderList);} else{List<Order> orderList = new ArrayList<Order>();orderList.add(order);logisticsTxnIdOrdersMap.put(order.getLogisticsTransactionId(), orderList);}if(logisticsTxnIdOrderQuantityMap.containsKey(order.getLogisticsTransactionId())){Map<Long, Double> orderItemQuantityMap = logisticsTxnIdOrderQuantityMap.get(order.getLogisticsTransactionId());if(orderItemQuantityMap.containsKey(lineItem.getItem_id())){double orderQuantity = orderItemQuantityMap.get(lineItem.getItem_id())+ lineItem.getQuantity();orderItemQuantityMap.put(lineItem.getItem_id(),orderQuantity);logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);}else{double orderQuantity = lineItem.getQuantity();orderItemQuantityMap.put(lineItem.getItem_id(),orderQuantity);logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);}}else{Map<Long, Double> orderItemQuantityMap = new HashMap<Long, Double>();orderItemQuantityMap.put(lineItem.getItem_id(), lineItem.getQuantity());logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);}}else{List<Order> orderList = new ArrayList<Order>();orderList.add(order);logisticsTxnIdOrdersMap.put(order.getId()+"", orderList);Map<Long, Double> orderItemQuantityMap = new HashMap<Long, Double>();orderItemQuantityMap.put(lineItem.getItem_id(), lineItem.getQuantity());logisticsTxnIdOrderQuantityMap.put(order.getId()+"", orderItemQuantityMap);}if(!itemNamesMap.containsKey(lineItem.getItem_id())){itemNamesMap.put(lineItem.getItem_id(), getItemDisplayName(lineItem, false));}}int serialNo = 0;for(String logisticsTxnId : logisticsTxnIdOrdersMap.keySet()){serialNo++;Row contentRow = sheet.createRow((short)serialNo);contentRow.createCell(0).setCellValue(serialNo);List<Order> ordersList = logisticsTxnIdOrdersMap.get(logisticsTxnId);double totalAmount = 0.0;double totalWeight = 0.0;for(Order o:ordersList){totalAmount = totalAmount + (o.getTotal_amount()+o.getShippingCost()+o.getCodCharges()-o.getGvAmount());totalWeight = totalWeight + o.getTotal_weight();}Order order = logisticsTxnIdOrdersMap.get(logisticsTxnId).get(0);contentRow.createCell(1).setCellValue(order.getAirwaybill_no());Cell awbDateCell = contentRow.createCell(2);awbDateCell.setCellValue(awbDate);awbDateCell.setCellStyle(dateCellStyle);contentRow.createCell(3).setCellValue(logisticsTxnId);if(order.getPickupStoreId() != 0){PickupStore store = null;try {store = logisticsClient.getPickupStore(order.getPickupStoreId());} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}contentRow.createCell(4).setCellValue(getValueForEmptyString(store.getName()));contentRow.createCell(5).setCellValue(getValueForEmptyString(store.getLine1()));contentRow.createCell(6).setCellValue(getValueForEmptyString(store.getLine2()));contentRow.createCell(7).setCellValue(getValueForEmptyString(store.getCity()));contentRow.createCell(8).setCellValue(getValueForEmptyString(store.getState()));contentRow.createCell(9).setCellValue(getValueForEmptyString(store.getPin()));contentRow.createCell(10).setCellValue(getValueForEmptyString(store.getPhone()));}else{contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));}contentRow.createCell(11).setCellValue("-");if(isCod){contentRow.createCell(12).setCellValue("COD");contentRow.createCell(13).setCellValue(totalAmount);} else {contentRow.createCell(12).setCellValue("Prepaid");contentRow.createCell(13).setCellValue(0);}contentRow.createCell(14).setCellValue(totalAmount);Cell weightCell = contentRow.createCell(15);weightCell.setCellValue(totalWeight);weightCell.setCellStyle(weightStyle);StringBuffer productNameBuffer = new StringBuffer();int skuSizeTxn = logisticsTxnIdOrderQuantityMap.get(logisticsTxnId).keySet().size();int count = 1;for(Long itemId : logisticsTxnIdOrderQuantityMap.get(logisticsTxnId).keySet()){Map<Long, Double> quantityMap = logisticsTxnIdOrderQuantityMap.get(logisticsTxnId);double quantity = quantityMap.get(itemId);productNameBuffer.append(itemNamesMap.get(itemId)+"("+quantity+")");if(count<skuSizeTxn){productNameBuffer.append(",");}count++;}contentRow.createCell(16).setCellValue(productNameBuffer.toString());contentRow.createCell(17).setCellValue(location);contentRow.createCell(18).setCellValue(accountNo);}/*for(int i = 0; i<orders.size(); i++){Order order = orders.get(i);if(order.getLogistics_provider_id()!=providerId)continue;if(order.isLogisticsCod() != isCod)continue;serialNo++;Row contentRow = sheet.createRow((short)serialNo);contentRow.createCell(0).setCellValue(serialNo);contentRow.createCell(1).setCellValue(order.getAirwaybill_no());Cell awbDateCell = contentRow.createCell(2);awbDateCell.setCellValue(awbDate);awbDateCell.setCellStyle(dateCellStyle);contentRow.createCell(3).setCellValue(order.getId());contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));contentRow.createCell(11).setCellValue("-");if(isCod){contentRow.createCell(12).setCellValue("COD");contentRow.createCell(13).setCellValue(order.getTotal_amount()-order.getGvAmount());} else {contentRow.createCell(12).setCellValue("Prepaid");contentRow.createCell(13).setCellValue(0);}List<LineItem> lineItems = order.getLineitems();LineItem lineItem = lineItems.get(0);contentRow.createCell(14).setCellValue(order.getTotal_amount()-order.getGvAmount());contentRow.createCell(15).setCellValue(lineItem.getId());Cell weightCell = contentRow.createCell(16);weightCell.setCellValue(lineItem.getTotal_weight());weightCell.setCellStyle(weightStyle);contentRow.createCell(17).setCellValue(lineItem.getBrand() + " " + lineItem.getModel_number() + " " + lineItem.getModel_name() + " " + lineItem.getColor());contentRow.createCell(18).setCellValue(location);contentRow.createCell(19).setCellValue(accountNo);}*/// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {logger.error("Exception while creating the Courier Details report", e);}return baosXLS;}private String getValueForEmptyString(String s){if(s==null || s.equals(""))return "-";elsereturn s;}private String removeNewLines(String str){return str.replace('\n', ' ');}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.isSetSerial_number()){itemName.append("\nIMEI No. " + lineitem.getSerial_number());}return itemName.toString();}/*** @param args*/public static void main(String[] args) {System.out.println("Hey There");CourierDetailsGenerator g = new CourierDetailsGenerator();try {FileOutputStream f = new FileOutputStream("/home/ashish/Downloads/courier-details.xls");ByteArrayOutputStream baosXLS = g.generateCourierDetails(1, 1, true);baosXLS.writeTo(f);f.close();} catch (FileNotFoundException e) {logger.error("Error while creating the Courier Details report", e);} catch (IOException e) {logger.error("IO error while writing the Courier Details report", e);}System.out.println("Successfully generated the detailed courier report");}}