Subversion Repositories SmartDukaan

Rev

Rev 3427 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.services;

import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.thrift.clients.TransactionClient;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CourierReconciliationGenerator {
    
    private static Logger logger = LoggerFactory.getLogger(CourierReconciliationGenerator.class);
    
    private TransactionClient tsc = null;

    public CourierReconciliationGenerator(){
        try {
            tsc = new TransactionClient();
        } catch (TTransportException e) {
            logger.error("Error establishing connection to one of txn, payment or user service", e);
        }
    }
    
    public ByteArrayOutputStream generateCourierReconciliationReport(Date startDate, Date endDate, int providerId, boolean cod){
        ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
        in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
        
        List<Order> orders = null;
        try {
            orders = txnClient.getOrdersByShippingDate(startDate.getTime(), endDate.getTime(), providerId, -1, cod);
        } catch (TException e) {
            logger.error("Error getting information from one of the Thrift Services: ", 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();
        
        CellStyle dateCellStyle = wb.createCellStyle();
        dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));
        
        CellStyle weightStyle = wb.createCellStyle();
        weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));
        
        createOutscannedOrdersSheet(orders, wb, dateCellStyle, weightStyle);
        createReturnedOrdersSheet(orders, wb, dateCellStyle, weightStyle);
        
        // 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;
    }

    /**
     * Creates a sheet named Outscanned Orders in the given workbook.
     * 
     * @param orders
     *            Outscanned orders to insert in the sheet.
     * @param wb
     *            Workbook to create the sheet in.
     * @param dateCellStyle
     *            Formatter for any date cells.
     * @param weightStyle
     *            Formatter for weight cells.
     */
    private void createOutscannedOrdersSheet(List<Order> orders, Workbook wb, CellStyle dateCellStyle, CellStyle weightStyle) {
        Sheet sheet = wb.createSheet("Outscanned Orders");
        
        // 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("AWB No");
        headerRow.createCell(1).setCellValue("Order Id");
        headerRow.createCell(2).setCellValue("Shipment Date");
        headerRow.createCell(3).setCellValue("City");
        headerRow.createCell(4).setCellValue("Packet Weight(in Kg)");
        headerRow.createCell(5).setCellValue("Order Amount");
        
        int serialNo = 0;
        for(Order order : orders){
            serialNo++;
            Row contentRow = sheet.createRow((short)serialNo);
            contentRow.createCell(0).setCellValue(order.getAirwaybill_no());
            contentRow.createCell(1).setCellValue(order.getId());
            Cell awbDateCell = contentRow.createCell(2);
            awbDateCell.setCellValue(new Date(order.getShipping_timestamp()));
            awbDateCell.setCellStyle(dateCellStyle);
            
            contentRow.createCell(3).setCellValue(getValueForEmptyString(order.getCustomer_city()));
            
            Cell weightCell = contentRow.createCell(4);
            weightCell.setCellValue(order.getTotal_weight());
            weightCell.setCellStyle(weightStyle);
            
            contentRow.createCell(5).setCellValue(order.getTotal_amount());
        }
    }

    /**
     * Creates a sheet named Returned Orders in the given workbook.
     * 
     * @param orders
     *            Returned orders to insert in the sheet.
     * @param wb
     *            Workbook to create the sheet in.
     * @param dateCellStyle
     *            Formatter for any date cells.
     * @param weightStyle
     *            Formatter for weight cells.
     */
    private void createReturnedOrdersSheet(List<Order> orders, Workbook wb, CellStyle dateCellStyle, CellStyle weightStyle) {
        Sheet sheet = wb.createSheet("Returned Orders");
        
        // 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("Reference No");
        headerRow.createCell(1).setCellValue("Order Id");
        headerRow.createCell(2).setCellValue("Return Date");
        headerRow.createCell(3).setCellValue("City");
        headerRow.createCell(4).setCellValue("Packet Weight(in Kg)");
        headerRow.createCell(5).setCellValue("Order Amount");
        
        int serialNo = 0;
        for(Order order : orders){
            serialNo++;
            Row contentRow = sheet.createRow((short)serialNo);
            contentRow.createCell(0).setCellValue(order.getAirwaybill_no());
            contentRow.createCell(1).setCellValue(order.getId());
            Cell awbDateCell = contentRow.createCell(2);
            awbDateCell.setCellValue(new Date(order.getShipping_timestamp()));
            awbDateCell.setCellStyle(dateCellStyle);
            
            contentRow.createCell(3).setCellValue(getValueForEmptyString(order.getCustomer_city()));
            
            Cell weightCell = contentRow.createCell(4);
            weightCell.setCellValue(order.getTotal_weight());
            weightCell.setCellStyle(weightStyle);
            
            contentRow.createCell(5).setCellValue(order.getTotal_amount());
        }
    }
    
    private String getValueForEmptyString(String s){
        if(s==null || s.equals(""))
            return "-";
        else
            return s; 
    }
    
    public static void main(String[] args){
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        Date startDate = null, endDate = null;
        try {
            startDate = df.parse("01/01/2011");
            endDate = df.parse("06/30/2011");
            Calendar cal = Calendar.getInstance();
            cal.setTime(endDate);
            cal.add(Calendar.DATE, 1);
            endDate.setTime(cal.getTimeInMillis());
        } catch (ParseException pe) {
            logger.error("Error parsing the supplied date", pe);
        }

        CourierReconciliationGenerator crg = new CourierReconciliationGenerator();
        try {
            String userHome = System.getProperty("user.home");
            FileOutputStream f = new FileOutputStream(userHome + "/courier-reconciliation-report.xls");
            ByteArrayOutputStream baosXLS = crg.generateCourierReconciliationReport(startDate, endDate, 1, false);
            baosXLS.writeTo(f);
            f.close();
        } catch (FileNotFoundException e) {
            logger.error("Error creating payment details report", e);
        } catch (IOException e) {
            logger.error("IO error while creating payment details report", e);
        }
        System.out.println("Successfully generated the payment details report");
    }
}