Subversion Repositories SmartDukaan

Rev

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