Subversion Repositories SmartDukaan

Rev

Rev 1655 | Rev 1881 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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