Subversion Repositories SmartDukaan

Rev

Rev 1660 | Rev 2061 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1631 ankur.sing 1
package in.shop2020.support.services;
2
 
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.TransactionServiceException;
6
import in.shop2020.payments.Payment;
7
import in.shop2020.payments.PaymentException;
8
import in.shop2020.payments.PaymentStatus;
9
import in.shop2020.thrift.clients.PaymentServiceClient;
10
import in.shop2020.thrift.clients.TransactionServiceClient;
11
 
12
import java.io.ByteArrayOutputStream;
13
import java.io.FileNotFoundException;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.text.DateFormat;
17
import java.text.ParseException;
18
import java.text.SimpleDateFormat;
19
import java.util.Calendar;
20
import java.util.Date;
21
import java.util.List;
22
 
23
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
24
import org.apache.poi.ss.usermodel.Cell;
25
import org.apache.poi.ss.usermodel.CellStyle;
1655 ankur.sing 26
import org.apache.poi.ss.usermodel.DataFormat;
1631 ankur.sing 27
import org.apache.poi.ss.usermodel.Font;
28
import org.apache.poi.ss.usermodel.Row;
29
import org.apache.poi.ss.usermodel.Sheet;
30
import org.apache.poi.ss.usermodel.Workbook;
31
import org.apache.poi.ss.util.CellRangeAddress;
32
import org.apache.thrift.TException;
33
 
34
public class PaymentDetailsGenerator {
1660 ankur.sing 35
    TransactionServiceClient tsc;
36
    in.shop2020.model.v1.order.TransactionService.Client tClient;
1631 ankur.sing 37
 
1660 ankur.sing 38
    PaymentServiceClient psc;
39
    in.shop2020.payments.PaymentService.Client pClient;
1631 ankur.sing 40
 
1660 ankur.sing 41
    public PaymentDetailsGenerator() {
42
        try {
43
            tsc = new TransactionServiceClient();
44
            tClient = tsc.getClient();
45
            psc = new PaymentServiceClient();
46
            pClient = psc.getClient();
47
        } catch (Exception e) {
48
            e.printStackTrace();
49
        }
50
    }
1631 ankur.sing 51
 
1660 ankur.sing 52
    /**
53
     * This method is used in PaymentDetailsController.
54
     * If any pending or failed payment exists between given date range, it returns ByteArrayOutputStream,
55
     * otherwise it returns null.
56
     * @param startDate
57
     * @param endDate
1881 ankur.sing 58
     * @param status 0 --> user input: Successful  1: Failed & pending
1660 ankur.sing 59
     * @return
60
     */
1881 ankur.sing 61
    public ByteArrayOutputStream generatePaymentDetailsReport(Date startDate, Date endDate, int status) {
1631 ankur.sing 62
 
1660 ankur.sing 63
        // Retrieving all the payments between start and end dates with status
64
        // as FAILED or INIT and for gateway Id = 1 (HDFC)
65
        List<Payment> payments = null;
66
        List<Payment> pendingPayments = null;
67
        try {
1881 ankur.sing 68
            if(status == 0) {
69
                payments = pClient.getPayments(startDate.getTime(), endDate.getTime(), PaymentStatus.SUCCESS, 1);
70
            } else {
71
                payments = pClient.getPayments(startDate.getTime(), endDate.getTime(), PaymentStatus.FAILED, 1);
72
                pendingPayments = pClient.getPayments(startDate.getTime(), endDate.getTime(), PaymentStatus.INIT, 1);
73
                if (payments != null && pendingPayments != null) {
74
                    payments.addAll(pendingPayments);
75
                } else if (pendingPayments != null){
76
                    payments = pendingPayments;
77
                }
78
            }
1660 ankur.sing 79
        } catch (PaymentException e) {
80
            e.printStackTrace();
81
        } catch (TException e) {
82
            e.printStackTrace();
83
        }
84
        if (payments == null || payments.isEmpty()) {
85
            return null;
86
        }
1631 ankur.sing 87
 
1660 ankur.sing 88
        // Preparing XLS file for output
89
        return getSpreadSheetData(payments);
1631 ankur.sing 90
 
1660 ankur.sing 91
    }
1631 ankur.sing 92
 
1660 ankur.sing 93
    // Prepares the XLS worksheet object and fills in the data with proper
94
    // formatting
95
    private ByteArrayOutputStream getSpreadSheetData(List<Payment> payments) {
96
        ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
1631 ankur.sing 97
 
1660 ankur.sing 98
        Workbook wb = new HSSFWorkbook();
1631 ankur.sing 99
 
1660 ankur.sing 100
        Font font = wb.createFont();
101
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
102
        CellStyle style = wb.createCellStyle();
103
        style.setFont(font);
104
 
105
        CellStyle styleWT = wb.createCellStyle();
1655 ankur.sing 106
        styleWT.setWrapText(true);
1660 ankur.sing 107
 
1655 ankur.sing 108
        DataFormat format = wb.createDataFormat();
109
        CellStyle styleAmount = wb.createCellStyle();
110
        styleAmount.setDataFormat(format.getFormat("#,##0.00"));
1631 ankur.sing 111
 
1660 ankur.sing 112
        Sheet paymentSheet = wb.createSheet("Payment");
113
        short paymentSerialNo = 0;
1631 ankur.sing 114
 
1660 ankur.sing 115
        Row titleRow = paymentSheet.createRow(paymentSerialNo++);
116
        Cell titleCell = titleRow.createCell(0);
117
        titleCell.setCellValue("Payment Details");
118
        titleCell.setCellStyle(style);
1631 ankur.sing 119
 
1660 ankur.sing 120
        paymentSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
1631 ankur.sing 121
 
1660 ankur.sing 122
        paymentSheet.createRow(paymentSerialNo++);
1631 ankur.sing 123
 
1660 ankur.sing 124
        Row headerRow = paymentSheet.createRow(paymentSerialNo++);
125
        headerRow.createCell(0).setCellValue("Payment Id");
126
        headerRow.createCell(1).setCellValue("Txn Id");
127
        headerRow.createCell(2).setCellValue("Amount");
128
        headerRow.createCell(3).setCellValue("Payment Status");
129
        headerRow.createCell(4).setCellValue("Payment Status Description");
130
        headerRow.createCell(5).setCellValue("Reference Code");
131
        headerRow.createCell(6).setCellValue("Transaction Time");
1631 ankur.sing 132
 
1660 ankur.sing 133
        headerRow.createCell(7).setCellValue("Customer Id");
134
        headerRow.createCell(8).setCellValue("Name");
135
        headerRow.createCell(9).setCellValue("MobileNo");
136
        headerRow.createCell(10).setCellValue("Address");
137
        headerRow.getCell(10).setCellStyle(styleWT);
138
        headerRow.createCell(11).setCellValue("Pincode");
139
        headerRow.createCell(12).setCellValue("City");
140
        headerRow.createCell(13).setCellValue("State");
141
        headerRow.createCell(14).setCellValue("Email");
1631 ankur.sing 142
 
1660 ankur.sing 143
        headerRow.createCell(15).setCellValue("Order Id");
144
        headerRow.createCell(16).setCellValue("Order Status");
145
        headerRow.createCell(17).setCellValue("Order Status Description");
1631 ankur.sing 146
 
1660 ankur.sing 147
        headerRow.createCell(18).setCellValue("Item Id");
148
        headerRow.createCell(19).setCellValue("Product Group");
149
        headerRow.createCell(20).setCellValue("Brand");
150
        headerRow.createCell(21).setCellValue("Model Name");
151
        headerRow.createCell(22).setCellValue("Model Number");
152
        headerRow.createCell(23).setCellValue("Qty");
1631 ankur.sing 153
 
1660 ankur.sing 154
        int addrColWidth = 35;
155
        paymentSheet.setColumnWidth(10, 256 * addrColWidth); // set width of address column to 35 characters
156
        List<Order> orders;
157
        long txnId;
158
        Row contentRow;
159
        Calendar calendar = Calendar.getInstance();
160
        DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
161
        for (Payment payment : payments) {
162
            try {
163
                paymentSerialNo++;
164
                contentRow = paymentSheet.createRow(paymentSerialNo);
165
                contentRow.createCell(0).setCellValue(payment.getPaymentId());
166
                contentRow.createCell(1).setCellValue(payment.getMerchantTxnId());
167
                contentRow.createCell(2).setCellValue(payment.getAmount());
168
                contentRow.getCell(2).setCellStyle(styleAmount);
169
                contentRow.createCell(3).setCellValue(payment.getStatus().name());
170
                contentRow.createCell(4).setCellValue(payment.getDescription());
171
                contentRow.createCell(5).setCellValue(payment.getReferenceCode());
172
                calendar.setTimeInMillis(payment.getInitTimestamp());
173
                contentRow.createCell(6).setCellValue(formatter.format(calendar.getTime()));
1631 ankur.sing 174
 
1660 ankur.sing 175
                txnId = tClient.getTransaction(payment.getMerchantTxnId()).getId();
176
                orders = tClient.getOrdersForTransaction(txnId, payment.getUserId());
177
                List<LineItem> lineItems;
1631 ankur.sing 178
 
1660 ankur.sing 179
                String address = "";
180
                float rowHeight = paymentSheet.getDefaultRowHeightInPoints();
181
                if (orders == null || orders.isEmpty()) {
182
                    continue;
183
                }
184
                contentRow.createCell(7).setCellValue(orders.get(0).getCustomer_id());
185
                contentRow.createCell(8).setCellValue(orders.get(0).getCustomer_name());
186
                contentRow.createCell(9).setCellValue(orders.get(0).getCustomer_mobilenumber());
187
                address = orders.get(0).getCustomer_address1() + ", " + orders.get(0).getCustomer_address2();
188
                contentRow.createCell(10).setCellValue(address);
189
                contentRow.getCell(10).setCellStyle(styleWT);
190
                int maxLength = Math.max(address.length(), paymentSheet.getDefaultColumnWidth());
191
                contentRow.setHeightInPoints((maxLength / addrColWidth + 1) * rowHeight); // Setting Row Height
192
                contentRow.createCell(11).setCellValue(orders.get(0).getCustomer_pincode());
193
                contentRow.createCell(12).setCellValue(orders.get(0).getCustomer_city());
194
                contentRow.createCell(13).setCellValue(orders.get(0).getCustomer_state());
195
                contentRow.createCell(14).setCellValue(orders.get(0).getCustomer_email());
1631 ankur.sing 196
 
1660 ankur.sing 197
                for (Order o : orders) {
198
                    paymentSerialNo++;
199
                    contentRow = paymentSheet.createRow(paymentSerialNo);
1631 ankur.sing 200
 
1660 ankur.sing 201
                    contentRow.createCell(15).setCellValue(o.getId());
202
                    contentRow.createCell(16).setCellValue(o.getStatus().name());
203
                    contentRow.createCell(17).setCellValue(o.getStatusDescription());
1631 ankur.sing 204
 
1660 ankur.sing 205
                    lineItems = tClient.getLineItemsForOrder(o.getId());
206
                    for (LineItem i : lineItems) {
207
                        /*
208
                         * Right now there can be only one line item in an
209
                         * order. So putting line item details in the same
210
                         * row as order details. Commenting below 2 lines
211
                         * for this.
212
                         */
213
                        // paymentSerialNo++;
214
                        // contentRow =
215
                        // paymentSheet.createRow(paymentSerialNo);
1631 ankur.sing 216
 
1660 ankur.sing 217
                        contentRow.createCell(18).setCellValue(i.getId());
218
                        contentRow.createCell(19).setCellValue(i.getProductGroup());
219
                        contentRow.createCell(20).setCellValue(i.getBrand());
220
                        contentRow.createCell(21).setCellValue(i.getModel_name());
221
                        contentRow.createCell(22).setCellValue(i.getModel_number());
222
                        contentRow.createCell(23).setCellValue(i.getQuantity());
223
                    }
224
                }
225
            } catch (TransactionServiceException e) {
226
                e.printStackTrace();
227
            } catch (TException e) {
228
                e.printStackTrace();
229
            }
230
        }
231
 
232
        for (int i = 0; i <= 23; i++) {
233
            if (i == 10) // Address Column is of fixed size with wrap text style
234
                continue;
235
            paymentSheet.autoSizeColumn(i);
236
        }
237
 
238
        // Write the workbook to the output stream
239
        try {
240
            wb.write(baosXLS);
241
            baosXLS.close();
242
        } catch (IOException e) {
243
            e.printStackTrace();
244
        }
245
        return baosXLS;
246
    }
247
 
248
    public static void main(String[] args) {
249
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
250
        Date startDate = null, endDate = null;
251
        try {
252
            startDate = df.parse("01/01/2011");
253
            endDate = df.parse("04/30/2011");
254
            Calendar cal = Calendar.getInstance();
255
            cal.setTime(endDate);
256
            cal.add(Calendar.DATE, 1);
257
            endDate.setTime(cal.getTimeInMillis());
258
        } catch (ParseException pe) {
259
            pe.printStackTrace();
260
        }
261
        PaymentDetailsGenerator pdg = new PaymentDetailsGenerator();
262
        try {
263
            String userHome = System.getProperty("user.home");
264
            FileOutputStream f = new FileOutputStream(userHome + "/payment-details-report.xls");
1881 ankur.sing 265
            ByteArrayOutputStream baosXLS = pdg.generatePaymentDetailsReport(startDate, endDate, 1);
1660 ankur.sing 266
            baosXLS.writeTo(f);
267
            f.close();
268
        } catch (FileNotFoundException e) {
269
            e.printStackTrace();
270
        } catch (IOException e) {
271
            e.printStackTrace();
272
        }
273
        System.out.println("Successfully generated the payment details report");
274
    }
1631 ankur.sing 275
}