Subversion Repositories SmartDukaan

Rev

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