| 7082 |
rajveer |
1 |
package in.shop2020.recharge.controllers;
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import in.shop2020.model.v1.order.RechargeOrderStatus;
|
|
|
5 |
import in.shop2020.model.v1.order.RechargeTransaction;
|
|
|
6 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 7151 |
amit.gupta |
7 |
import in.shop2020.warehouse.InventoryAvailability;
|
| 7082 |
rajveer |
8 |
|
| 7151 |
amit.gupta |
9 |
import java.io.ByteArrayOutputStream;
|
|
|
10 |
import java.io.IOException;
|
| 7082 |
rajveer |
11 |
import java.text.DateFormat;
|
|
|
12 |
import java.text.SimpleDateFormat;
|
|
|
13 |
import java.util.Calendar;
|
| 7151 |
amit.gupta |
14 |
import java.util.Date;
|
| 7082 |
rajveer |
15 |
import java.util.List;
|
|
|
16 |
import java.util.Map;
|
|
|
17 |
|
|
|
18 |
import javax.servlet.ServletContext;
|
| 7151 |
amit.gupta |
19 |
import javax.servlet.ServletOutputStream;
|
| 7082 |
rajveer |
20 |
import javax.servlet.http.HttpServletRequest;
|
| 7151 |
amit.gupta |
21 |
import javax.servlet.http.HttpServletResponse;
|
| 7082 |
rajveer |
22 |
import javax.servlet.http.HttpSession;
|
|
|
23 |
|
| 7151 |
amit.gupta |
24 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
25 |
import org.apache.poi.ss.usermodel.CreationHelper;
|
|
|
26 |
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
|
|
|
27 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
28 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
29 |
import org.apache.poi.ss.usermodel.Workbook;
|
| 7082 |
rajveer |
30 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
| 7151 |
amit.gupta |
31 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
| 7082 |
rajveer |
32 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
33 |
import org.slf4j.Logger;
|
|
|
34 |
import org.slf4j.LoggerFactory;
|
|
|
35 |
|
| 7151 |
amit.gupta |
36 |
import com.sun.mail.iap.Response;
|
| 7082 |
rajveer |
37 |
|
|
|
38 |
|
|
|
39 |
|
| 7151 |
amit.gupta |
40 |
public class ReportController implements ServletRequestAware, ServletContextAware, ServletResponseAware {
|
|
|
41 |
|
| 7082 |
rajveer |
42 |
private static Logger logger = LoggerFactory.getLogger(ReportController.class);
|
|
|
43 |
|
| 7151 |
amit.gupta |
44 |
protected HttpServletRequest request;
|
|
|
45 |
protected HttpSession session;
|
|
|
46 |
protected HttpServletResponse response;
|
| 7082 |
rajveer |
47 |
private ServletContext context;
|
| 7151 |
amit.gupta |
48 |
private String startDate;
|
|
|
49 |
private String endDate;
|
|
|
50 |
private String status;
|
|
|
51 |
private String dateselector;
|
|
|
52 |
|
| 7082 |
rajveer |
53 |
private TransactionClient tsc;
|
|
|
54 |
private in.shop2020.model.v1.order.TransactionService.Client tClient;
|
|
|
55 |
|
|
|
56 |
private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
| 7151 |
amit.gupta |
57 |
private final DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
|
| 7082 |
rajveer |
58 |
|
|
|
59 |
private List<RechargeTransaction> txns = null;
|
|
|
60 |
public ReportController(){
|
|
|
61 |
try {
|
|
|
62 |
tsc = new TransactionClient();
|
|
|
63 |
tClient = tsc.getClient();
|
|
|
64 |
} catch (Exception e) {
|
|
|
65 |
logger.error("Error connecting to one of the user, order or payment service", e);
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
|
| 7087 |
rajveer |
70 |
public String index() throws Exception{
|
| 7096 |
anupam.sin |
71 |
String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
|
|
|
72 |
if(loginStatus == null || !loginStatus.equals("TRUE")){
|
|
|
73 |
return "authfail";
|
|
|
74 |
}
|
|
|
75 |
txns = tClient.getRechargeTransactions(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")));
|
| 7082 |
rajveer |
76 |
return "index";
|
|
|
77 |
}
|
|
|
78 |
|
| 7151 |
amit.gupta |
79 |
public String create() throws Exception{
|
|
|
80 |
long sDate = -1;
|
|
|
81 |
long eDate = -1;
|
|
|
82 |
RechargeOrderStatus st = null;
|
|
|
83 |
String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
|
|
|
84 |
if(loginStatus == null || !loginStatus.equals("TRUE")){
|
|
|
85 |
return "authfail";
|
|
|
86 |
}
|
|
|
87 |
if(dateselector.equals("1")) {
|
|
|
88 |
sDate = new Date().getTime();
|
|
|
89 |
eDate = sDate;
|
|
|
90 |
}else if (dateselector.equals("2")) {
|
|
|
91 |
sDate = new Date().getTime() - 86400*1000;
|
|
|
92 |
eDate = sDate;
|
|
|
93 |
}else {
|
|
|
94 |
if(!(startDate.equals(""))) {
|
|
|
95 |
sDate = dateFormatter.parse(startDate).getTime();
|
|
|
96 |
startDate = dateFormatter.format(sDate);
|
|
|
97 |
}
|
|
|
98 |
if(!endDate.equals("")) {
|
|
|
99 |
eDate = dateFormatter.parse(endDate).getTime();
|
|
|
100 |
endDate = dateFormatter.format(sDate);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
if(!status.equals("-1")){
|
|
|
104 |
st = RechargeOrderStatus.findByValue(Integer.parseInt("0"));
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
ByteArrayOutputStream baos = generateReport(tClient.getRechargeTrans(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")), sDate, eDate, st));
|
|
|
108 |
|
|
|
109 |
response.setContentType("application/vnd.ms-excel");
|
|
|
110 |
String st1 = "ALL";
|
|
|
111 |
if(st!=null){
|
|
|
112 |
st1 = st.name();
|
|
|
113 |
}
|
|
|
114 |
String fileName = st1 + "-recharge-report";
|
|
|
115 |
if (!startDate.equals("")) {
|
|
|
116 |
fileName = fileName + "-" + startDate.replaceAll("\\\\", "") ;
|
|
|
117 |
}
|
|
|
118 |
if (!endDate.equals("")) {
|
|
|
119 |
fileName = fileName + "-" + endDate.replaceAll("\\\\", "") ;
|
|
|
120 |
}
|
|
|
121 |
fileName = fileName + ".xls";
|
|
|
122 |
response.setHeader("Content-disposition", "inline; filename=" + fileName);
|
|
|
123 |
ServletOutputStream sos;
|
|
|
124 |
try {
|
|
|
125 |
sos = response.getOutputStream();
|
|
|
126 |
baos.writeTo(sos);
|
|
|
127 |
sos.flush();
|
|
|
128 |
} catch (IOException e) {
|
|
|
129 |
e.printStackTrace();
|
|
|
130 |
}
|
|
|
131 |
return "index";
|
|
|
132 |
}
|
|
|
133 |
|
| 7082 |
rajveer |
134 |
|
| 7151 |
amit.gupta |
135 |
private ByteArrayOutputStream generateReport(
|
|
|
136 |
List<RechargeTransaction> rechargeTrans) {
|
|
|
137 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
138 |
|
|
|
139 |
Workbook wb = new HSSFWorkbook();
|
|
|
140 |
|
|
|
141 |
Sheet reportSheet = wb.createSheet("Recharge Report");
|
|
|
142 |
|
|
|
143 |
Row reportSheetHeader = reportSheet.createRow((short)0);
|
|
|
144 |
reportSheetHeader.createCell(0).setCellValue("Order ID");
|
|
|
145 |
reportSheetHeader.createCell(1).setCellValue("Order Date");
|
|
|
146 |
reportSheetHeader.createCell(2).setCellValue("Operator");
|
|
|
147 |
reportSheetHeader.createCell(3).setCellValue("Device");
|
|
|
148 |
reportSheetHeader.createCell(4).setCellValue("Status");
|
|
|
149 |
reportSheetHeader.createCell(5).setCellValue("Amount");
|
|
|
150 |
reportSheetHeader.createCell(6).setCellValue("Discount");
|
|
|
151 |
|
|
|
152 |
int serialNo = 0;
|
|
|
153 |
|
|
|
154 |
for(RechargeTransaction transaction : rechargeTrans) {
|
|
|
155 |
serialNo++;
|
|
|
156 |
Row contentRow = reportSheet.createRow((short)serialNo);
|
|
|
157 |
contentRow.createCell(0).setCellValue(transaction.getId());
|
|
|
158 |
contentRow.createCell(1).setCellValue(formatter.format(new Date(transaction.getTransactionTime())));
|
|
|
159 |
contentRow.createCell(2).setCellValue(getOperatorName(transaction.getOperatorId()));
|
|
|
160 |
contentRow.createCell(3).setCellValue(transaction.getDeviceNum());
|
|
|
161 |
contentRow.createCell(4).setCellValue(transaction.getStatus().name());
|
|
|
162 |
contentRow.createCell(5).setCellValue(transaction.getAmount());
|
|
|
163 |
contentRow.createCell(6).setCellValue(transaction.getDiscount());
|
|
|
164 |
}
|
|
|
165 |
try {
|
|
|
166 |
wb.write(baosXLS);
|
|
|
167 |
} catch (IOException e) {
|
|
|
168 |
// TODO Auto-generated catch block
|
|
|
169 |
e.printStackTrace();
|
|
|
170 |
}
|
|
|
171 |
return baosXLS;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
public String getDateTime(long milliseconds) {
|
| 7082 |
rajveer |
176 |
Calendar cal = Calendar.getInstance();
|
|
|
177 |
cal.setTimeInMillis(milliseconds);
|
|
|
178 |
return formatter.format(cal.getTime());
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
public List<RechargeTransaction> getTxns(){
|
|
|
182 |
return txns;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
@Override
|
|
|
186 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
187 |
this.request = req;
|
|
|
188 |
this.session = req.getSession();
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
@Override
|
|
|
192 |
public void setServletContext(ServletContext context) {
|
|
|
193 |
this.context = context;
|
|
|
194 |
}
|
| 7151 |
amit.gupta |
195 |
|
|
|
196 |
@Override
|
|
|
197 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
198 |
this.response = response;
|
|
|
199 |
}
|
| 7082 |
rajveer |
200 |
|
|
|
201 |
public String getServletContextPath() {
|
|
|
202 |
return context.getContextPath();
|
|
|
203 |
}
|
| 7096 |
anupam.sin |
204 |
|
|
|
205 |
public String getOperatorName(long operatorId) {
|
|
|
206 |
String operatorName = "";
|
|
|
207 |
if (HomeController.getMobileProvidersMap().containsKey(operatorId)) {
|
|
|
208 |
operatorName = HomeController.getMobileProvidersMap().get(operatorId);
|
|
|
209 |
} else if(HomeController.getDthProvidersMap().containsKey(operatorId)) {
|
|
|
210 |
operatorName = HomeController.getDthProvidersMap().get(operatorId);
|
|
|
211 |
} else {
|
|
|
212 |
operatorName = "N/A";
|
|
|
213 |
}
|
|
|
214 |
return operatorName;
|
|
|
215 |
}
|
| 7151 |
amit.gupta |
216 |
|
|
|
217 |
public String getStartDate() {
|
|
|
218 |
return startDate;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
public void setStartDate(String startDate) {
|
|
|
223 |
this.startDate = startDate;
|
|
|
224 |
}
|
| 7082 |
rajveer |
225 |
|
| 7151 |
amit.gupta |
226 |
public String getEndDate() {
|
|
|
227 |
return endDate;
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
public void setEndDate(String endDate) {
|
|
|
232 |
this.endDate = endDate;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
public String getStatus() {
|
|
|
236 |
return status;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
public void setStatus(String status) {
|
|
|
241 |
this.status = status;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
public String getDateselector() {
|
|
|
246 |
return dateselector;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
public void setDateselector(String dateselector) {
|
|
|
251 |
this.dateselector = dateselector;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
}
|