Subversion Repositories SmartDukaan

Rev

Rev 3449 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3427 chandransh 1
package in.shop2020.support.services;
2
 
3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.TransactionServiceException;
5
import in.shop2020.thrift.clients.TransactionClient;
6
 
7
import java.io.ByteArrayOutputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.text.DateFormat;
12
import java.text.ParseException;
13
import java.text.SimpleDateFormat;
14
import java.util.Calendar;
15
import java.util.Date;
16
import java.util.List;
17
 
18
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
19
import org.apache.poi.ss.usermodel.Cell;
20
import org.apache.poi.ss.usermodel.CellStyle;
21
import org.apache.poi.ss.usermodel.CreationHelper;
22
import org.apache.poi.ss.usermodel.Row;
23
import org.apache.poi.ss.usermodel.Sheet;
24
import org.apache.poi.ss.usermodel.Workbook;
25
import org.apache.thrift.TException;
26
import org.apache.thrift.transport.TTransportException;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29
 
30
public class CourierReconciliationGenerator {
31
 
32
    private static Logger logger = LoggerFactory.getLogger(CourierReconciliationGenerator.class);
33
 
34
    private TransactionClient tsc = null;
35
 
36
    public CourierReconciliationGenerator(){
37
        try {
38
            tsc = new TransactionClient();
39
        } catch (TTransportException e) {
40
            logger.error("Error establishing connection to one of txn, payment or user service", e);
41
        }
42
    }
43
 
44
    public ByteArrayOutputStream generateCourierReconciliationReport(Date startDate, Date endDate, int providerId){
45
        ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
46
        in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
47
 
48
        List<Order> orders = null;
49
        try {
50
            orders = txnClient.getOrdersByShippingDate(startDate.getTime(), endDate.getTime(), providerId, -1);
51
        } catch (TException e) {
52
            logger.error("Error getting information from one of the Thrift Services: ", e);
53
            return baosXLS;
54
        } catch (TransactionServiceException e) {
55
            logger.error("Error getting orders from the transaction service: ", e);
56
            return baosXLS;
57
        }
58
 
59
        Workbook wb = new HSSFWorkbook();
60
        CreationHelper createHelper = wb.getCreationHelper();
61
 
62
        CellStyle dateCellStyle = wb.createCellStyle();
63
        dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));
64
 
65
        CellStyle weightStyle = wb.createCellStyle();
66
        weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));
67
 
68
        Sheet sheet = wb.createSheet("new sheet");
69
 
70
        // Create the header row and put all the titles in it. Rows are 0 based.
71
        Row headerRow = sheet.createRow((short)0);
72
        headerRow.createCell(0).setCellValue("AWB No");
73
        headerRow.createCell(1).setCellValue("Order Id");
74
        headerRow.createCell(2).setCellValue("Shipment Date");
75
        headerRow.createCell(3).setCellValue("City");
76
        headerRow.createCell(4).setCellValue("Packet Weight(in Kg)");
77
        headerRow.createCell(5).setCellValue("Order Amount");
78
 
79
        int serialNo = 0;
80
        for(Order order : orders){
81
            serialNo++;
82
            Row contentRow = sheet.createRow((short)serialNo);
83
            contentRow.createCell(0).setCellValue(order.getAirwaybill_no());
84
            contentRow.createCell(1).setCellValue(order.getId());
85
            Cell awbDateCell = contentRow.createCell(2);
86
            awbDateCell.setCellValue(new Date(order.getShipping_timestamp()));
87
            awbDateCell.setCellStyle(dateCellStyle);
88
 
89
            contentRow.createCell(3).setCellValue(getValueForEmptyString(order.getCustomer_city()));
90
 
91
            Cell weightCell = contentRow.createCell(4);
92
            weightCell.setCellValue(order.getTotal_weight());
93
            weightCell.setCellStyle(weightStyle);
94
 
95
            contentRow.createCell(5).setCellValue(order.getTotal_amount());
96
        }
97
 
98
        // Write the workbook to the output stream
99
        try {
100
            wb.write(baosXLS);
101
            baosXLS.close();
102
        } catch (IOException e) {
103
            logger.error("Exception while creating the Courier Details report", e);
104
        }
105
 
106
        return baosXLS;
107
    }
108
 
109
    private String getValueForEmptyString(String s){
110
        if(s==null || s.equals(""))
111
            return "-";
112
        else
113
            return s; 
114
    }
115
 
116
    public static void main(String[] args){
117
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
118
        Date startDate = null, endDate = null;
119
        try {
120
            startDate = df.parse("01/01/2011");
121
            endDate = df.parse("06/30/2011");
122
            Calendar cal = Calendar.getInstance();
123
            cal.setTime(endDate);
124
            cal.add(Calendar.DATE, 1);
125
            endDate.setTime(cal.getTimeInMillis());
126
        } catch (ParseException pe) {
127
            logger.error("Error parsing the supplied date", pe);
128
        }
129
 
130
        CourierReconciliationGenerator crg = new CourierReconciliationGenerator();
131
        try {
132
            String userHome = System.getProperty("user.home");
133
            FileOutputStream f = new FileOutputStream(userHome + "/courier-reconciliation-report.xls");
134
            ByteArrayOutputStream baosXLS = crg.generateCourierReconciliationReport(startDate, endDate, 1);
135
            baosXLS.writeTo(f);
136
            f.close();
137
        } catch (FileNotFoundException e) {
138
            logger.error("Error creating payment details report", e);
139
        } catch (IOException e) {
140
            logger.error("IO error while creating payment details report", e);
141
        }
142
        System.out.println("Successfully generated the payment details report");
143
    }
144
}