| 1895 |
vikas |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
4 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
5 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
6 |
import in.shop2020.model.v1.order.TransactionStatus;
|
|
|
7 |
import in.shop2020.model.v1.user.Address;
|
|
|
8 |
import in.shop2020.model.v1.user.Cart;
|
|
|
9 |
import in.shop2020.model.v1.user.Line;
|
|
|
10 |
import in.shop2020.model.v1.user.User;
|
|
|
11 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
|
|
12 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
|
|
13 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
|
|
14 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
15 |
|
|
|
16 |
import java.io.ByteArrayOutputStream;
|
|
|
17 |
import java.io.IOException;
|
|
|
18 |
import java.text.ParseException;
|
|
|
19 |
import java.util.Calendar;
|
|
|
20 |
import java.util.Date;
|
|
|
21 |
import java.util.HashMap;
|
|
|
22 |
import java.util.Iterator;
|
|
|
23 |
import java.util.LinkedList;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
|
|
26 |
|
|
|
27 |
import javax.servlet.ServletOutputStream;
|
|
|
28 |
import javax.servlet.http.HttpServletResponse;
|
|
|
29 |
|
|
|
30 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
31 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
32 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
33 |
import org.apache.poi.ss.usermodel.CreationHelper;
|
|
|
34 |
import org.apache.poi.ss.usermodel.Font;
|
|
|
35 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
36 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
37 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
38 |
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
39 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
40 |
|
| 2904 |
chandransh |
41 |
/**
|
|
|
42 |
* Provides reports about carts created in last 15 days. Also provides data
|
|
|
43 |
* about transactions created for a cart.
|
|
|
44 |
*
|
|
|
45 |
* @author Ankur Singhal
|
|
|
46 |
*
|
|
|
47 |
*/
|
| 1895 |
vikas |
48 |
public class CartReportController implements ServletResponseAware{
|
|
|
49 |
|
|
|
50 |
private HttpServletResponse response;
|
|
|
51 |
|
|
|
52 |
private String errorMsg = "";
|
|
|
53 |
|
|
|
54 |
@Override
|
|
|
55 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
56 |
this.response = res;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public void index(){
|
|
|
60 |
try {
|
|
|
61 |
TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
|
|
|
62 |
in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
|
|
|
63 |
|
|
|
64 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
65 |
Client userClient = userContextServiceClient.getClient();
|
|
|
66 |
|
|
|
67 |
// get carts created in last 15 days.
|
|
|
68 |
Date now = new Date();
|
|
|
69 |
Date from = new Date();
|
|
|
70 |
Calendar c = Calendar.getInstance();
|
|
|
71 |
c.setTime(from);
|
|
|
72 |
c.add(Calendar.DATE, -15);
|
|
|
73 |
from.setTime(c.getTime().getTime());
|
|
|
74 |
|
|
|
75 |
List<Cart> carts = userClient.getCartsByTime(from.getTime(), now.getTime(), null);
|
|
|
76 |
System.out.println("Total carts : " + carts.size());
|
|
|
77 |
Map<Long, Transaction> cartToTransactionMap = new HashMap<Long, Transaction>();
|
|
|
78 |
for (Iterator<Cart> itr = carts.iterator(); itr.hasNext();) {
|
|
|
79 |
Cart cart = itr.next();
|
|
|
80 |
if (cart.getCheckedOutOn() == 0) {
|
|
|
81 |
itr.remove();
|
|
|
82 |
}
|
|
|
83 |
else {
|
|
|
84 |
List<Transaction> cartTransactions = transactionClient.getTransactionsForShoppingCartId(cart.getId());
|
|
|
85 |
long cartTime = cart.getCheckedOutOn();
|
|
|
86 |
for (Transaction t : cartTransactions) {
|
|
|
87 |
long tTime = t.getCreatedOn();
|
|
|
88 |
if (tTime > cartTime && ((tTime - cartTime)/(60 * 1000) < 30)) { // transaction with in 30 min of checkout
|
|
|
89 |
if (t.getTransactionStatus() == TransactionStatus.IN_PROCESS || t.getTransactionStatus() == TransactionStatus.FAILED) {
|
|
|
90 |
itr.remove();
|
|
|
91 |
}
|
|
|
92 |
else {
|
|
|
93 |
if (cartToTransactionMap.containsKey(cart.getId())) {
|
|
|
94 |
if (cartToTransactionMap.get(cart.getId()).getCreatedOn() > t.getCreatedOn())
|
|
|
95 |
{
|
|
|
96 |
cartToTransactionMap.put(cart.getId(),t);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
else {
|
|
|
100 |
cartToTransactionMap.put(cart.getId(),t);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
System.out.println("Checked out carts with unsuccessful payment : " + carts.size());
|
|
|
108 |
|
|
|
109 |
// Preparing XLS file for output
|
|
|
110 |
response.setContentType("application/vnd.ms-excel");
|
|
|
111 |
|
|
|
112 |
response.setHeader("Content-disposition", "inline; filename=cart-report" + ".xls");
|
|
|
113 |
|
|
|
114 |
ServletOutputStream sos;
|
|
|
115 |
try {
|
|
|
116 |
ByteArrayOutputStream baos = getSpreadSheetData(carts, cartToTransactionMap, userClient);
|
|
|
117 |
sos = response.getOutputStream();
|
|
|
118 |
baos.writeTo(sos);
|
|
|
119 |
sos.flush();
|
|
|
120 |
} catch (IOException e) {
|
|
|
121 |
errorMsg = "Failed to write to response.";
|
|
|
122 |
e.printStackTrace();
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
} catch (ParseException e) {
|
|
|
126 |
errorMsg = e.getMessage();
|
|
|
127 |
e.printStackTrace();
|
|
|
128 |
} catch (TransactionServiceException e) {
|
|
|
129 |
errorMsg = e.getMessage();
|
|
|
130 |
e.printStackTrace();
|
|
|
131 |
} catch (Exception e) {
|
|
|
132 |
errorMsg = e.getMessage();
|
|
|
133 |
e.printStackTrace();
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// Prepares the XLS worksheet object and fills in the data with proper formatting
|
|
|
138 |
private ByteArrayOutputStream getSpreadSheetData(List<Cart> carts,
|
|
|
139 |
Map<Long, Transaction> cartToTransactionMap, Client userClient)
|
|
|
140 |
throws Exception
|
|
|
141 |
{
|
|
|
142 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
143 |
|
|
|
144 |
Workbook wb = new HSSFWorkbook();
|
|
|
145 |
|
|
|
146 |
Font font = wb.createFont();
|
|
|
147 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
148 |
CellStyle style = wb.createCellStyle();
|
|
|
149 |
style.setFont(font);
|
|
|
150 |
|
|
|
151 |
CreationHelper createHelper = wb.getCreationHelper();
|
|
|
152 |
CellStyle dateCellStyle = wb.createCellStyle();
|
|
|
153 |
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
|
|
|
154 |
|
|
|
155 |
createCartSheet(carts, cartToTransactionMap, userClient, wb, style, dateCellStyle);
|
|
|
156 |
|
|
|
157 |
// Write the workbook to the output stream
|
|
|
158 |
try {
|
|
|
159 |
wb.write(baosXLS);
|
|
|
160 |
baosXLS.close();
|
|
|
161 |
} catch (IOException e) {
|
|
|
162 |
e.printStackTrace();
|
|
|
163 |
}
|
|
|
164 |
return baosXLS;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
private void createCartSheet(List<Cart> carts,
|
|
|
168 |
Map<Long, Transaction> cartToTransactionMap, Client userClient,
|
|
|
169 |
Workbook wb, CellStyle style, CellStyle dateCellStyle)
|
|
|
170 |
throws Exception
|
|
|
171 |
{
|
|
|
172 |
CellStyle styleWT = wb.createCellStyle();
|
|
|
173 |
styleWT.setWrapText(true);
|
|
|
174 |
CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
|
|
|
175 |
in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
|
|
|
176 |
|
|
|
177 |
// CART SHEET
|
|
|
178 |
Sheet cartSheet = wb.createSheet("Cart");
|
|
|
179 |
short cartSerialNo = 0;
|
|
|
180 |
|
|
|
181 |
List<Cart> noAddressCarts = new LinkedList<Cart>();
|
|
|
182 |
List<Cart> noPaymentCarts = new LinkedList<Cart>();
|
|
|
183 |
List<Cart> otherCarts = new LinkedList<Cart>();
|
|
|
184 |
for (Cart cart : carts) {
|
|
|
185 |
if (cart.getAddressId() == 0) {
|
|
|
186 |
noAddressCarts.add(cart);
|
|
|
187 |
}
|
|
|
188 |
else if (!cartToTransactionMap.containsKey(cart.getId())) {
|
|
|
189 |
noPaymentCarts.add(cart);
|
|
|
190 |
}
|
|
|
191 |
else {
|
|
|
192 |
otherCarts.add(cart);
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
List<Cart> orderedCarts = new LinkedList<Cart>();
|
|
|
196 |
orderedCarts.addAll(noAddressCarts);
|
|
|
197 |
orderedCarts.addAll(noPaymentCarts);
|
|
|
198 |
orderedCarts.addAll(otherCarts);
|
|
|
199 |
|
|
|
200 |
Row cartTitleRow = cartSheet.createRow(cartSerialNo ++);
|
|
|
201 |
Cell cartTitleCell = cartTitleRow.createCell(0);
|
|
|
202 |
cartTitleCell.setCellValue("User Cart Conversions");
|
|
|
203 |
cartTitleCell.setCellStyle(style);
|
|
|
204 |
|
|
|
205 |
cartSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
206 |
|
|
|
207 |
cartSheet.createRow(cartSerialNo ++);
|
|
|
208 |
|
|
|
209 |
Row cartHeaderRow = cartSheet.createRow(cartSerialNo ++);
|
|
|
210 |
cartHeaderRow.createCell(0).setCellValue("Cart Id");
|
|
|
211 |
cartHeaderRow.createCell(1).setCellValue("Cart Address");
|
|
|
212 |
cartHeaderRow.getCell(1).setCellStyle(styleWT);
|
|
|
213 |
cartHeaderRow.createCell(2).setCellValue("Checked out date");
|
|
|
214 |
cartHeaderRow.createCell(3).setCellValue("Transaction Date");
|
|
|
215 |
cartHeaderRow.createCell(4).setCellValue("Transaction Status");
|
|
|
216 |
cartHeaderRow.createCell(5).setCellValue("Created On");
|
|
|
217 |
cartHeaderRow.createCell(6).setCellValue("Item size");
|
|
|
218 |
cartHeaderRow.createCell(7).setCellValue("Updated On");
|
|
|
219 |
cartHeaderRow.createCell(8).setCellValue("Cart Status");
|
|
|
220 |
cartHeaderRow.createCell(9).setCellValue("Cart Items");
|
|
|
221 |
cartHeaderRow.createCell(10).setCellValue("User Id");
|
|
|
222 |
cartHeaderRow.createCell(11).setCellValue("Comm Email");
|
|
|
223 |
cartHeaderRow.createCell(12).setCellValue("Date Of Birth");
|
|
|
224 |
cartHeaderRow.createCell(13).setCellValue("Mobile");
|
|
|
225 |
cartHeaderRow.createCell(14).setCellValue("Name");
|
|
|
226 |
|
|
|
227 |
for (int i=0; i<15 ;i++) {
|
|
|
228 |
cartHeaderRow.getCell(i).setCellStyle(style);
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
int addrColWidth = 35;
|
|
|
232 |
cartSheet.setColumnWidth(1, 256 * addrColWidth); // set width of address column to 35 characters
|
|
|
233 |
for(Cart cart : orderedCarts) {
|
|
|
234 |
cartSerialNo ++;
|
|
|
235 |
Row contentRow = cartSheet.createRow(cartSerialNo);
|
|
|
236 |
Transaction transaction = cartToTransactionMap.get(cart.getId());
|
|
|
237 |
User user = userClient.getUserById(cart.getUserId());
|
|
|
238 |
List<Line> cartLines = cart.getLines();
|
|
|
239 |
String cartItems = "";
|
|
|
240 |
for (Line line : cartLines) {
|
|
|
241 |
Item item = catalogClient.getItem(line.getItemId());
|
|
|
242 |
if (!cartItems.isEmpty()) {
|
|
|
243 |
cartItems += ", ";
|
|
|
244 |
}
|
|
|
245 |
cartItems += item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + "(" + line.getQuantity()+ ")";
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
String cartAddress = "-";
|
|
|
249 |
if (cart.getAddressId() != 0) {
|
|
|
250 |
Address address = userClient.getAddressById(cart.getAddressId());
|
|
|
251 |
cartAddress = address.getName() + ", " + address.getLine1() + ", " + address.getLine2() + ", " + address.getCity()
|
|
|
252 |
+ ", " + address.getState() + "-" + address.getPin() + ", " + address.getPhone();
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
contentRow.createCell(0).setCellValue(cart.getId());
|
|
|
256 |
contentRow.createCell(1).setCellValue(cartAddress);
|
|
|
257 |
contentRow.getCell(1).setCellStyle(styleWT);
|
|
|
258 |
|
|
|
259 |
float rowHeight = cartSheet.getDefaultRowHeightInPoints();
|
|
|
260 |
int maxLength = Math.max(cartAddress.length(), cartSheet.getDefaultColumnWidth());
|
|
|
261 |
contentRow.setHeightInPoints((maxLength / addrColWidth + 1) * rowHeight); // Setting Row Height
|
|
|
262 |
|
|
|
263 |
contentRow.createCell(2).setCellValue(new Date(cart.getCheckedOutOn()));
|
|
|
264 |
contentRow.getCell(2).setCellStyle(dateCellStyle);
|
|
|
265 |
if (transaction != null) {
|
|
|
266 |
contentRow.createCell(3).setCellValue(new Date(transaction.getCreatedOn()));
|
|
|
267 |
contentRow.getCell(3).setCellStyle(dateCellStyle);
|
|
|
268 |
contentRow.createCell(4).setCellValue(transaction.getStatusDescription());
|
|
|
269 |
}
|
|
|
270 |
contentRow.createCell(5).setCellValue(new Date(cart.getCreatedOn()));
|
|
|
271 |
contentRow.getCell(5).setCellStyle(dateCellStyle);
|
|
|
272 |
contentRow.createCell(6).setCellValue(cart.getLinesSize());
|
|
|
273 |
contentRow.createCell(7).setCellValue(new Date(cart.getUpdatedOn()));
|
|
|
274 |
contentRow.getCell(7).setCellStyle(dateCellStyle);
|
|
|
275 |
contentRow.createCell(8).setCellValue(cart.getStatus().name());
|
|
|
276 |
contentRow.createCell(9).setCellValue(cartItems);
|
|
|
277 |
|
|
|
278 |
contentRow.createCell(10).setCellValue(user.getEmail());
|
|
|
279 |
contentRow.createCell(11).setCellValue(user.getCommunicationEmail());
|
|
|
280 |
contentRow.createCell(12).setCellValue(user.getDateOfBirth());
|
|
|
281 |
contentRow.createCell(13).setCellValue(user.getMobileNumber());
|
|
|
282 |
contentRow.createCell(14).setCellValue(user.getName());
|
|
|
283 |
}
|
|
|
284 |
for (int i = 0; i <= 14; i++) {
|
|
|
285 |
if (i != 1) {
|
|
|
286 |
cartSheet.autoSizeColumn(i);
|
|
|
287 |
}
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
public String getErrorMsg() {
|
|
|
292 |
return errorMsg;
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
|