Subversion Repositories SmartDukaan

Rev

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