Subversion Repositories SmartDukaan

Rev

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