Subversion Repositories SmartDukaan

Rev

Rev 3427 | Go to most recent revision | Details | Compare with Previous | 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
 
3449 chandransh 44
    public ByteArrayOutputStream generateCourierReconciliationReport(Date startDate, Date endDate, int providerId, boolean cod){
3427 chandransh 45
        ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
46
        in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
47
 
48
        List<Order> orders = null;
49
        try {
3449 chandransh 50
            orders = txnClient.getOrdersByShippingDate(startDate.getTime(), endDate.getTime(), providerId, -1, cod);
3427 chandransh 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
 
3449 chandransh 68
        createOutscannedOrdersSheet(orders, wb, dateCellStyle, weightStyle);
69
        createReturnedOrdersSheet(orders, wb, dateCellStyle, weightStyle);
3427 chandransh 70
 
3449 chandransh 71
        // Write the workbook to the output stream
72
        try {
73
            wb.write(baosXLS);
74
            baosXLS.close();
75
        } catch (IOException e) {
76
            logger.error("Exception while creating the Courier Details report", e);
77
        }
78
 
79
        return baosXLS;
80
    }
81
 
82
    /**
83
     * Creates a sheet named Outscanned Orders in the given workbook.
84
     * 
85
     * @param orders
86
     *            Outscanned orders to insert in the sheet.
87
     * @param wb
88
     *            Workbook to create the sheet in.
89
     * @param dateCellStyle
90
     *            Formatter for any date cells.
91
     * @param weightStyle
92
     *            Formatter for weight cells.
93
     */
94
    private void createOutscannedOrdersSheet(List<Order> orders, Workbook wb, CellStyle dateCellStyle, CellStyle weightStyle) {
95
        Sheet sheet = wb.createSheet("Outscanned Orders");
96
 
3427 chandransh 97
        // Create the header row and put all the titles in it. Rows are 0 based.
98
        Row headerRow = sheet.createRow((short)0);
99
        headerRow.createCell(0).setCellValue("AWB No");
100
        headerRow.createCell(1).setCellValue("Order Id");
101
        headerRow.createCell(2).setCellValue("Shipment Date");
102
        headerRow.createCell(3).setCellValue("City");
103
        headerRow.createCell(4).setCellValue("Packet Weight(in Kg)");
104
        headerRow.createCell(5).setCellValue("Order Amount");
105
 
106
        int serialNo = 0;
107
        for(Order order : orders){
108
            serialNo++;
109
            Row contentRow = sheet.createRow((short)serialNo);
110
            contentRow.createCell(0).setCellValue(order.getAirwaybill_no());
111
            contentRow.createCell(1).setCellValue(order.getId());
112
            Cell awbDateCell = contentRow.createCell(2);
113
            awbDateCell.setCellValue(new Date(order.getShipping_timestamp()));
114
            awbDateCell.setCellStyle(dateCellStyle);
115
 
116
            contentRow.createCell(3).setCellValue(getValueForEmptyString(order.getCustomer_city()));
117
 
118
            Cell weightCell = contentRow.createCell(4);
119
            weightCell.setCellValue(order.getTotal_weight());
120
            weightCell.setCellStyle(weightStyle);
121
 
122
            contentRow.createCell(5).setCellValue(order.getTotal_amount());
123
        }
3449 chandransh 124
    }
125
 
126
    /**
127
     * Creates a sheet named Returned Orders in the given workbook.
128
     * 
129
     * @param orders
130
     *            Returned orders to insert in the sheet.
131
     * @param wb
132
     *            Workbook to create the sheet in.
133
     * @param dateCellStyle
134
     *            Formatter for any date cells.
135
     * @param weightStyle
136
     *            Formatter for weight cells.
137
     */
138
    private void createReturnedOrdersSheet(List<Order> orders, Workbook wb, CellStyle dateCellStyle, CellStyle weightStyle) {
139
        Sheet sheet = wb.createSheet("Returned Orders");
3427 chandransh 140
 
3449 chandransh 141
        // Create the header row and put all the titles in it. Rows are 0 based.
142
        Row headerRow = sheet.createRow((short)0);
143
        headerRow.createCell(0).setCellValue("Reference No");
144
        headerRow.createCell(1).setCellValue("Order Id");
145
        headerRow.createCell(2).setCellValue("Return Date");
146
        headerRow.createCell(3).setCellValue("City");
147
        headerRow.createCell(4).setCellValue("Packet Weight(in Kg)");
148
        headerRow.createCell(5).setCellValue("Order Amount");
149
 
150
        int serialNo = 0;
151
        for(Order order : orders){
152
            serialNo++;
153
            Row contentRow = sheet.createRow((short)serialNo);
154
            contentRow.createCell(0).setCellValue(order.getAirwaybill_no());
155
            contentRow.createCell(1).setCellValue(order.getId());
156
            Cell awbDateCell = contentRow.createCell(2);
157
            awbDateCell.setCellValue(new Date(order.getShipping_timestamp()));
158
            awbDateCell.setCellStyle(dateCellStyle);
159
 
160
            contentRow.createCell(3).setCellValue(getValueForEmptyString(order.getCustomer_city()));
161
 
162
            Cell weightCell = contentRow.createCell(4);
163
            weightCell.setCellValue(order.getTotal_weight());
164
            weightCell.setCellStyle(weightStyle);
165
 
166
            contentRow.createCell(5).setCellValue(order.getTotal_amount());
3427 chandransh 167
        }
168
    }
169
 
170
    private String getValueForEmptyString(String s){
171
        if(s==null || s.equals(""))
172
            return "-";
173
        else
174
            return s; 
175
    }
176
 
177
    public static void main(String[] args){
178
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
179
        Date startDate = null, endDate = null;
180
        try {
181
            startDate = df.parse("01/01/2011");
182
            endDate = df.parse("06/30/2011");
183
            Calendar cal = Calendar.getInstance();
184
            cal.setTime(endDate);
185
            cal.add(Calendar.DATE, 1);
186
            endDate.setTime(cal.getTimeInMillis());
187
        } catch (ParseException pe) {
188
            logger.error("Error parsing the supplied date", pe);
189
        }
190
 
191
        CourierReconciliationGenerator crg = new CourierReconciliationGenerator();
192
        try {
193
            String userHome = System.getProperty("user.home");
194
            FileOutputStream f = new FileOutputStream(userHome + "/courier-reconciliation-report.xls");
3449 chandransh 195
            ByteArrayOutputStream baosXLS = crg.generateCourierReconciliationReport(startDate, endDate, 1, false);
3427 chandransh 196
            baosXLS.writeTo(f);
197
            f.close();
198
        } catch (FileNotFoundException e) {
199
            logger.error("Error creating payment details report", e);
200
        } catch (IOException e) {
201
            logger.error("IO error while creating payment details report", e);
202
        }
203
        System.out.println("Successfully generated the payment details report");
204
    }
205
}