Subversion Repositories SmartDukaan

Rev

Rev 5554 | Rev 7792 | Go to most recent revision | 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.Provider;
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.List;

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("Order No");
            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("Item ID");
            headerRow.createCell(16).setCellValue("Packet Weight(in Kg)");
            headerRow.createCell(17).setCellValue("Product Name");
            headerRow.createCell(18).setCellValue("Pickup Location");
            headerRow.createCell(19).setCellValue("Customer A/C Code");

            String accountNo;
            if(isCod)
                accountNo = provider.getDetails().get(DeliveryType.COD).getAccountNo();
            else
                accountNo = provider.getDetails().get(DeliveryType.PREPAID).getAccountNo();
            
            Date awbDate = new Date();
            String location = removeNewLines(warehouse.getLocation());
            
            int serialNo = 0;
            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());
                    } 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());
                    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 stream
                try {
                        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 "-";
                else
                        return s; 
        }
        
        private String removeNewLines(String str){
            return str.replace('\n', ' ');
        }
        
        /**
         * @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");
        }

}