| 1586 |
ankur.sing |
1 |
package in.shop2020.support.controllers;
|
|
|
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.PaymentStatus;
|
|
|
8 |
import in.shop2020.thrift.clients.PaymentServiceClient;
|
|
|
9 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
|
|
10 |
|
|
|
11 |
import java.io.ByteArrayOutputStream;
|
|
|
12 |
import java.io.IOException;
|
|
|
13 |
import java.text.DateFormat;
|
|
|
14 |
import java.text.ParseException;
|
|
|
15 |
import java.text.SimpleDateFormat;
|
|
|
16 |
import java.util.Calendar;
|
|
|
17 |
import java.util.Date;
|
|
|
18 |
import java.util.List;
|
|
|
19 |
|
|
|
20 |
import javax.servlet.ServletOutputStream;
|
|
|
21 |
import javax.servlet.http.HttpServletRequest;
|
|
|
22 |
import javax.servlet.http.HttpServletResponse;
|
|
|
23 |
|
|
|
24 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
25 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
26 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
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.struts2.interceptor.ServletRequestAware;
|
|
|
33 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
34 |
import org.apache.thrift.TException;
|
|
|
35 |
|
|
|
36 |
public class PaymentDetailsController implements ServletResponseAware, ServletRequestAware{
|
|
|
37 |
|
|
|
38 |
private HttpServletRequest request;
|
|
|
39 |
private HttpServletResponse response;
|
|
|
40 |
|
|
|
41 |
private String errorMsg = "";
|
|
|
42 |
|
|
|
43 |
public PaymentDetailsController(){
|
|
|
44 |
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
@Override
|
|
|
48 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
49 |
this.request = req;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
@Override
|
|
|
53 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
54 |
this.response = res;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public String index() {
|
|
|
58 |
return "report";
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public String show(){
|
|
|
62 |
return "report";
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Handles the POST request (Form Submission)
|
|
|
66 |
public String create(){
|
|
|
67 |
try {
|
|
|
68 |
//Formatting Form input parameters
|
|
|
69 |
String startDateStr = request.getParameter("startDate");
|
|
|
70 |
String endDateStr = request.getParameter("endDate");
|
|
|
71 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
72 |
Date startDate = null, endDate = null;
|
|
|
73 |
TransactionServiceClient tsc = new TransactionServiceClient();
|
|
|
74 |
in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
|
|
|
75 |
|
|
|
76 |
PaymentServiceClient psc = new PaymentServiceClient();
|
|
|
77 |
in.shop2020.payments.PaymentService.Client pClient = psc.getClient();
|
|
|
78 |
|
|
|
79 |
try {
|
|
|
80 |
startDate = df.parse(startDateStr);
|
|
|
81 |
endDate = df.parse(endDateStr);
|
|
|
82 |
Calendar cal = Calendar.getInstance();
|
|
|
83 |
cal.setTime(endDate);
|
|
|
84 |
cal.add(Calendar.DATE, 1);
|
|
|
85 |
endDate.setTime(cal.getTimeInMillis());
|
|
|
86 |
errorMsg = "Date Range: " + startDate + " to " + endDate;
|
|
|
87 |
} catch(ParseException pe) {
|
|
|
88 |
errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
|
|
|
89 |
return "report";
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Retrieving all the payments between start and end dates with status as failed and for gateway Id = 1 (HDFC)
|
|
|
93 |
List<Payment> payments = pClient.getPayments(startDate.getTime(), endDate.getTime(), PaymentStatus.FAILED, 1);
|
|
|
94 |
if(payments == null || payments.isEmpty()) {
|
|
|
95 |
errorMsg = "There is no FAILED payment within given date range";
|
|
|
96 |
return "report";
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Preparing XLS file for output
|
|
|
100 |
response.setContentType("application/vnd.ms-excel");
|
|
|
101 |
response.setHeader("Content-disposition", "inline; filename=payments-report" + ".xls");
|
|
|
102 |
ServletOutputStream sos;
|
|
|
103 |
try {
|
|
|
104 |
ByteArrayOutputStream baos = getSpreadSheetData(payments, tclient);
|
|
|
105 |
sos = response.getOutputStream();
|
|
|
106 |
baos.writeTo(sos);
|
|
|
107 |
sos.flush();
|
|
|
108 |
} catch (IOException e) {
|
|
|
109 |
errorMsg = "Failed to write to response.";
|
|
|
110 |
e.printStackTrace();
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
} catch (ParseException e) {
|
|
|
114 |
errorMsg = e.getMessage();
|
|
|
115 |
e.printStackTrace();
|
|
|
116 |
} catch (TransactionServiceException e) {
|
|
|
117 |
errorMsg = e.getMessage();
|
|
|
118 |
e.printStackTrace();
|
|
|
119 |
} catch (Exception e) {
|
|
|
120 |
errorMsg = e.getMessage();
|
|
|
121 |
e.printStackTrace();
|
|
|
122 |
}
|
|
|
123 |
return "report";
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
// Prepares the XLS worksheet object and fills in the data with proper formatting
|
|
|
127 |
private ByteArrayOutputStream getSpreadSheetData(List<Payment> payments, in.shop2020.model.v1.order.TransactionService.Client tsc) {
|
|
|
128 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
129 |
|
|
|
130 |
Workbook wb = new HSSFWorkbook();
|
|
|
131 |
|
|
|
132 |
Font font = wb.createFont();
|
|
|
133 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
134 |
CellStyle style = wb.createCellStyle();
|
|
|
135 |
style.setFont(font);
|
|
|
136 |
|
|
|
137 |
Sheet paymentSheet = wb.createSheet("Payment");
|
|
|
138 |
short paymentSerialNo = 0;
|
|
|
139 |
|
|
|
140 |
Row titleRow = paymentSheet.createRow(paymentSerialNo ++);
|
|
|
141 |
Cell titleCell = titleRow.createCell(0);
|
|
|
142 |
titleCell.setCellValue("Payment Details");
|
|
|
143 |
titleCell.setCellStyle(style);
|
|
|
144 |
|
|
|
145 |
paymentSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
146 |
|
|
|
147 |
paymentSheet.createRow(paymentSerialNo ++);
|
|
|
148 |
|
|
|
149 |
Row headerRow = paymentSheet.createRow(paymentSerialNo ++);
|
|
|
150 |
headerRow.createCell(0).setCellValue("Payment Id");
|
|
|
151 |
headerRow.createCell(1).setCellValue("Txn Id");
|
|
|
152 |
headerRow.createCell(2).setCellValue("Amount");
|
|
|
153 |
headerRow.createCell(3).setCellValue("Payment Status");
|
|
|
154 |
headerRow.createCell(4).setCellValue("Payment Status Description");
|
|
|
155 |
headerRow.createCell(5).setCellValue("Reference Code");
|
|
|
156 |
headerRow.createCell(6).setCellValue("Transaction Time");
|
|
|
157 |
|
|
|
158 |
headerRow.createCell(7).setCellValue("Customer Id");
|
|
|
159 |
headerRow.createCell(8).setCellValue("Name");
|
|
|
160 |
headerRow.createCell(9).setCellValue("MobileNo");
|
|
|
161 |
headerRow.createCell(10).setCellValue("Pincode");
|
|
|
162 |
headerRow.createCell(11).setCellValue("City");
|
|
|
163 |
headerRow.createCell(12).setCellValue("State");
|
|
|
164 |
headerRow.createCell(13).setCellValue("Email");
|
|
|
165 |
headerRow.createCell(14).setCellValue("Order Id");
|
|
|
166 |
headerRow.createCell(15).setCellValue("Order Status");
|
|
|
167 |
headerRow.createCell(16).setCellValue("Order Status Description");
|
|
|
168 |
|
|
|
169 |
headerRow.createCell(17).setCellValue("Item Id");
|
|
|
170 |
headerRow.createCell(18).setCellValue("Product Group");
|
|
|
171 |
headerRow.createCell(19).setCellValue("Brand");
|
|
|
172 |
headerRow.createCell(20).setCellValue("Model Name");
|
|
|
173 |
headerRow.createCell(21).setCellValue("Model Number");
|
|
|
174 |
headerRow.createCell(22).setCellValue("Quantity");
|
|
|
175 |
|
|
|
176 |
List<Order> orders;
|
|
|
177 |
long txnId;
|
|
|
178 |
Row contentRow;
|
|
|
179 |
Calendar calendar = Calendar.getInstance();
|
|
|
180 |
DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
|
|
181 |
for(Payment payment : payments) {
|
|
|
182 |
try {
|
|
|
183 |
paymentSerialNo++;
|
|
|
184 |
contentRow = paymentSheet.createRow(paymentSerialNo);
|
|
|
185 |
contentRow.createCell(0).setCellValue(payment.getPaymentId());
|
|
|
186 |
contentRow.createCell(1).setCellValue(payment.getMerchantTxnId());
|
|
|
187 |
contentRow.createCell(2).setCellValue(payment.getAmount());
|
|
|
188 |
contentRow.createCell(3).setCellValue(payment.getStatus().name());
|
|
|
189 |
contentRow.createCell(4).setCellValue(payment.getDescription());
|
|
|
190 |
contentRow.createCell(5).setCellValue(payment.getReferenceCode());
|
|
|
191 |
calendar.setTimeInMillis(payment.getInitTimestamp());
|
|
|
192 |
contentRow.createCell(6).setCellValue(formatter.format(calendar.getTime()));
|
|
|
193 |
|
|
|
194 |
txnId = tsc.getTransaction(payment.getMerchantTxnId()).getId();
|
|
|
195 |
orders = tsc.getOrdersForTransaction(txnId, payment.getUserId());
|
|
|
196 |
List<LineItem> lineItems;
|
|
|
197 |
|
|
|
198 |
if(orders != null && !orders.isEmpty()) {
|
|
|
199 |
contentRow.createCell(7).setCellValue(orders.get(0).getCustomer_id());
|
|
|
200 |
contentRow.createCell(8).setCellValue(orders.get(0).getCustomer_name());
|
|
|
201 |
contentRow.createCell(9).setCellValue(orders.get(0).getCustomer_mobilenumber());
|
|
|
202 |
contentRow.createCell(10).setCellValue(orders.get(0).getCustomer_pincode());
|
|
|
203 |
contentRow.createCell(11).setCellValue(orders.get(0).getCustomer_city());
|
|
|
204 |
contentRow.createCell(12).setCellValue(orders.get(0).getCustomer_state());
|
|
|
205 |
contentRow.createCell(13).setCellValue(orders.get(0).getCustomer_email());
|
|
|
206 |
|
|
|
207 |
for(Order o : orders) {
|
|
|
208 |
paymentSerialNo++;
|
|
|
209 |
contentRow = paymentSheet.createRow(paymentSerialNo);
|
|
|
210 |
|
|
|
211 |
contentRow.createCell(14).setCellValue(o.getId());
|
|
|
212 |
contentRow.createCell(15).setCellValue(o.getStatus().name());
|
|
|
213 |
contentRow.createCell(16).setCellValue(o.getStatusDescription());
|
|
|
214 |
|
|
|
215 |
lineItems = tsc.getLineItemsForOrder(o.getId());
|
|
|
216 |
for(LineItem i : lineItems) {
|
|
|
217 |
/*Right now there can be only one line item in an order.
|
|
|
218 |
So putting line item details in the same row as order details. Commenting below 2 lines for this.*/
|
|
|
219 |
//paymentSerialNo++;
|
|
|
220 |
//contentRow = paymentSheet.createRow(paymentSerialNo);
|
|
|
221 |
|
|
|
222 |
contentRow.createCell(17).setCellValue(i.getId());
|
|
|
223 |
contentRow.createCell(18).setCellValue(i.getProductGroup());
|
|
|
224 |
contentRow.createCell(19).setCellValue(i.getBrand());
|
|
|
225 |
contentRow.createCell(20).setCellValue(i.getModel_name());
|
|
|
226 |
contentRow.createCell(21).setCellValue(i.getModel_number());
|
|
|
227 |
contentRow.createCell(22).setCellValue(i.getQuantity());
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
} catch (TransactionServiceException e) {
|
|
|
232 |
e.printStackTrace();
|
|
|
233 |
} catch (TException e) {
|
|
|
234 |
e.printStackTrace();
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
paymentSheet.autoSizeColumn(0);
|
|
|
239 |
paymentSheet.autoSizeColumn(1);
|
|
|
240 |
paymentSheet.autoSizeColumn(2);
|
|
|
241 |
paymentSheet.autoSizeColumn(3);
|
|
|
242 |
paymentSheet.autoSizeColumn(4);
|
|
|
243 |
paymentSheet.autoSizeColumn(5);
|
|
|
244 |
paymentSheet.autoSizeColumn(6);
|
|
|
245 |
paymentSheet.autoSizeColumn(7);
|
|
|
246 |
paymentSheet.autoSizeColumn(8);
|
|
|
247 |
paymentSheet.autoSizeColumn(9);
|
|
|
248 |
paymentSheet.autoSizeColumn(10);
|
|
|
249 |
paymentSheet.autoSizeColumn(11);
|
|
|
250 |
paymentSheet.autoSizeColumn(12);
|
|
|
251 |
paymentSheet.autoSizeColumn(13);
|
|
|
252 |
paymentSheet.autoSizeColumn(14);
|
|
|
253 |
paymentSheet.autoSizeColumn(15);
|
|
|
254 |
paymentSheet.autoSizeColumn(16);
|
|
|
255 |
paymentSheet.autoSizeColumn(17);
|
|
|
256 |
paymentSheet.autoSizeColumn(18);
|
|
|
257 |
paymentSheet.autoSizeColumn(19);
|
|
|
258 |
paymentSheet.autoSizeColumn(20);
|
|
|
259 |
paymentSheet.autoSizeColumn(21);
|
|
|
260 |
paymentSheet.autoSizeColumn(22);
|
|
|
261 |
|
|
|
262 |
// Write the workbook to the output stream
|
|
|
263 |
try {
|
|
|
264 |
wb.write(baosXLS);
|
|
|
265 |
baosXLS.close();
|
|
|
266 |
} catch (IOException e) {
|
|
|
267 |
e.printStackTrace();
|
|
|
268 |
}
|
|
|
269 |
return baosXLS;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
public String getErrorMsg() {
|
|
|
273 |
return errorMsg;
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
|