Subversion Repositories SmartDukaan

Rev

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