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