Subversion Repositories SmartDukaan

Rev

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