| 5717 |
rajveer |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.logistics.PickupStore;
|
|
|
4 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
5 |
import in.shop2020.model.v1.order.Order;
|
|
|
6 |
import in.shop2020.model.v1.order.TransactionService.Client;
|
|
|
7 |
import in.shop2020.support.utils.ReportsUtils;
|
|
|
8 |
import in.shop2020.thrift.clients.LogisticsClient;
|
|
|
9 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
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.ServletContext;
|
|
|
21 |
import javax.servlet.ServletOutputStream;
|
|
|
22 |
import javax.servlet.http.HttpServletRequest;
|
|
|
23 |
import javax.servlet.http.HttpServletResponse;
|
|
|
24 |
import javax.servlet.http.HttpSession;
|
|
|
25 |
|
|
|
26 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
27 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
28 |
import org.apache.poi.ss.usermodel.DataFormat;
|
|
|
29 |
import org.apache.poi.ss.usermodel.Font;
|
|
|
30 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
31 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
32 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
33 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
34 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
35 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
36 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
37 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
38 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
39 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
40 |
import org.apache.thrift.TException;
|
|
|
41 |
import org.apache.thrift.transport.TTransportException;
|
|
|
42 |
import org.slf4j.Logger;
|
|
|
43 |
import org.slf4j.LoggerFactory;
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
@InterceptorRefs({
|
|
|
47 |
@InterceptorRef("defaultStack"),
|
|
|
48 |
@InterceptorRef("login")
|
|
|
49 |
})
|
|
|
50 |
@Results({
|
|
|
51 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
52 |
})
|
|
|
53 |
public class StoreCollectionController implements ServletRequestAware, ServletResponseAware, ServletContextAware {
|
|
|
54 |
|
|
|
55 |
private static Logger logger = LoggerFactory.getLogger(StoreCollectionController.class);
|
|
|
56 |
|
|
|
57 |
private HttpServletRequest request;
|
|
|
58 |
private HttpSession session;
|
|
|
59 |
private HttpServletResponse response;
|
|
|
60 |
private ServletContext context;
|
|
|
61 |
|
|
|
62 |
private String error = "";
|
|
|
63 |
|
|
|
64 |
private Client client;
|
|
|
65 |
|
|
|
66 |
public StoreCollectionController() throws TTransportException{
|
|
|
67 |
client = (new TransactionClient()).getClient();
|
|
|
68 |
}
|
|
|
69 |
public String index() {
|
|
|
70 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
71 |
return "authfail";
|
|
|
72 |
}
|
|
|
73 |
return "index";
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Handles the POST request (Form Submission)
|
|
|
77 |
public String create() throws TException {
|
|
|
78 |
String storeIdString = request.getParameter("storeId");
|
|
|
79 |
String startDateStr = request.getParameter("startDate");
|
|
|
80 |
String endDateStr = request.getParameter("endDate");
|
|
|
81 |
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
|
|
|
82 |
Date startDate = null, endDate = null;
|
|
|
83 |
long storeId = 0;
|
|
|
84 |
try {
|
|
|
85 |
storeId = Long.parseLong(storeIdString);
|
|
|
86 |
startDate = df.parse(startDateStr);
|
|
|
87 |
endDate = df.parse(endDateStr);
|
|
|
88 |
Calendar cal = Calendar.getInstance();
|
|
|
89 |
cal.setTime(endDate);
|
|
|
90 |
cal.add(Calendar.DATE, 1);
|
|
|
91 |
endDate.setTime(cal.getTimeInMillis());
|
|
|
92 |
} catch (ParseException pe) {
|
|
|
93 |
error = "Please enter start and end dates in format MM/dd/yyyy";
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
ByteArrayOutputStream baos = generateCollectionReport(startDate, endDate, storeId);
|
|
|
97 |
|
|
|
98 |
if (baos == null) {
|
|
|
99 |
error = "No output for given date range";
|
|
|
100 |
} else {
|
|
|
101 |
error = "Generating report...";
|
|
|
102 |
}
|
|
|
103 |
response.setContentType("application/vnd.ms-excel");
|
|
|
104 |
response.setHeader("Content-disposition", "inline; filename=collection-report.xls");
|
|
|
105 |
ServletOutputStream sos;
|
|
|
106 |
try {
|
|
|
107 |
sos = response.getOutputStream();
|
|
|
108 |
baos.writeTo(sos);
|
|
|
109 |
sos.flush();
|
|
|
110 |
error = "Report generated";
|
|
|
111 |
} catch (IOException e) {
|
|
|
112 |
error = "Failed to write to response.";
|
|
|
113 |
logger.error("Unable to stream the colection report", e);
|
|
|
114 |
}
|
|
|
115 |
return "index";
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
private ByteArrayOutputStream generateCollectionReport(Date startDate, Date endDate, long storeId) throws TException {
|
|
|
120 |
List<Order> orders = client.getOrdersCollectionAtStore(storeId, startDate.getTime(), endDate.getTime(), true);
|
|
|
121 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
122 |
|
|
|
123 |
Workbook wb = new HSSFWorkbook();
|
|
|
124 |
|
|
|
125 |
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
|
|
|
126 |
Font font = wb.createFont();
|
|
|
127 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
128 |
CellStyle style = wb.createCellStyle();
|
|
|
129 |
style.setFont(font);
|
|
|
130 |
|
|
|
131 |
DataFormat format = wb.createDataFormat();
|
|
|
132 |
CellStyle styleAmount = wb.createCellStyle();
|
|
|
133 |
styleAmount.setDataFormat(format.getFormat("#,##0.00"));
|
|
|
134 |
|
|
|
135 |
Sheet collectionSheet = wb.createSheet("Collection");
|
|
|
136 |
short collectionSerialNo = 0;
|
|
|
137 |
|
|
|
138 |
Row headerRow = collectionSheet.createRow(collectionSerialNo++);
|
|
|
139 |
headerRow.createCell(0).setCellValue("S.No");
|
|
|
140 |
headerRow.createCell(1).setCellValue("Order Id");
|
|
|
141 |
headerRow.createCell(2).setCellValue("Product");
|
|
|
142 |
headerRow.createCell(3).setCellValue("Order Type");
|
|
|
143 |
headerRow.createCell(4).setCellValue("Collection Date");
|
|
|
144 |
headerRow.createCell(5).setCellValue("Order Amount");
|
|
|
145 |
headerRow.createCell(6).setCellValue("Collected Amount");
|
|
|
146 |
//headerRow.createCell(7).setCellValue("Store Margin");
|
|
|
147 |
|
|
|
148 |
for(Order order: orders){
|
|
|
149 |
Row contentRow = collectionSheet.createRow(collectionSerialNo++);
|
|
|
150 |
LineItem line = order.getLineitems().get(0);
|
|
|
151 |
String productName = line.getBrand() + " " + line.getModel_name() + " " + line.getModel_number() + " " + line.getColor();
|
|
|
152 |
productName = productName.replaceAll("null", "").replaceAll(" ", " ");
|
|
|
153 |
contentRow.createCell(0).setCellValue(collectionSerialNo-1);
|
|
|
154 |
contentRow.createCell(1).setCellValue(order.getId());
|
|
|
155 |
contentRow.createCell(2).setCellValue(productName);
|
|
|
156 |
contentRow.createCell(3).setCellValue(order.isCod() ? "COD" : "Prepaid");
|
|
|
157 |
contentRow.createCell(4).setCellValue(formatter.format(order.getDelivery_timestamp()));
|
|
|
158 |
contentRow.createCell(5).setCellValue(order.getTotal_amount());
|
|
|
159 |
contentRow.createCell(6).setCellValue(order.isCod() ? order.getTotal_amount() : 0.0);
|
|
|
160 |
//contentRow.createCell(7).setCellValue(order.isCod() ? .01 * order.getTotal_amount() : .005 * order.getTotal_amount());
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
try {
|
|
|
164 |
wb.write(baosXLS);
|
|
|
165 |
baosXLS.close();
|
|
|
166 |
} catch (IOException e) {
|
|
|
167 |
logger.error("Error while streaming payment details report", e);
|
|
|
168 |
}
|
|
|
169 |
return baosXLS;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
public String getErrorMsg() {
|
|
|
174 |
return error;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
@Override
|
|
|
178 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
179 |
this.request = req;
|
|
|
180 |
this.session = req.getSession();
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
@Override
|
|
|
184 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
185 |
this.response = res;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
@Override
|
|
|
189 |
public void setServletContext(ServletContext context) {
|
|
|
190 |
this.context = context;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
public String getServletContextPath() {
|
|
|
194 |
return context.getContextPath();
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
public List<PickupStore> getAllStores() throws TTransportException, TException {
|
|
|
198 |
return (new LogisticsClient()).getClient().getAllPickupStores();
|
|
|
199 |
}
|
|
|
200 |
}
|