Subversion Repositories SmartDukaan

Rev

Rev 20786 | Rev 20788 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1597 ankur.sing 1
package in.shop2020.support.controllers;
2
 
1886 ankur.sing 3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.OrderStatus;
6
import in.shop2020.model.v1.order.TransactionServiceException;
1941 ankur.sing 7
import in.shop2020.support.utils.ReportsUtils;
3125 rajveer 8
import in.shop2020.thrift.clients.PaymentClient;
9
import in.shop2020.thrift.clients.TransactionClient;
10
import in.shop2020.thrift.clients.UserClient;
1597 ankur.sing 11
 
20779 amit.gupta 12
import java.io.InputStream;
1891 ankur.sing 13
import java.text.DateFormat;
14
import java.text.SimpleDateFormat;
20779 amit.gupta 15
import java.util.ArrayList;
1891 ankur.sing 16
import java.util.Calendar;
20779 amit.gupta 17
import java.util.Date;
1731 ankur.sing 18
import java.util.List;
19
 
1941 ankur.sing 20
import javax.servlet.ServletContext;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpSession;
23
 
20779 amit.gupta 24
import org.apache.poi.ss.usermodel.Row;
25
import org.apache.poi.ss.usermodel.Sheet;
26
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1891 ankur.sing 27
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRefs;
3936 chandransh 29
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Results;
1941 ankur.sing 31
import org.apache.struts2.interceptor.ServletRequestAware;
32
import org.apache.struts2.util.ServletContextAware;
1886 ankur.sing 33
import org.apache.thrift.TException;
3071 chandransh 34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
1597 ankur.sing 36
 
20779 amit.gupta 37
@InterceptorRefs({ @InterceptorRef("defaultStack"), @InterceptorRef("login") })
38
@Results({ @Result(name = "authfail", type = "redirectAction", params = {
20780 amit.gupta 39
		"actionName", "reports" }),
40
	@Result(	
41
	    name = "report", 
42
	    type = "stream", 
43
	    params = { 
44
	        "contentType", "${type}", 
45
	        "inputName", "${stream}", 
46
	        "bufferSize", "1024", 
47
	        "contentDisposition", "attachment;filename=\"${filename}\""
48
	    }
49
	)
50
})
20779 amit.gupta 51
public class StatisticsController implements ServletRequestAware,
52
		ServletContextAware {
1597 ankur.sing 53
 
20779 amit.gupta 54
	private static Logger logger = LoggerFactory
55
			.getLogger(StatisticsController.class);
20780 amit.gupta 56
 
57
 
58
    public String getFilename() {
59
		return filename;
60
	}
1630 ankur.sing 61
 
20780 amit.gupta 62
	public void setFilename(String filename) {
63
		this.filename = filename;
64
	}
65
 
66
	public InputStream getStream() {
67
		return stream;
68
	}
69
 
70
	public void setStream(InputStream stream) {
71
		this.stream = stream;
72
	}
73
 
74
	private InputStream stream;
75
	private String filename;
76
	private String type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
77
 
78
	public String getType() {
79
		return type;
80
	}
81
 
20779 amit.gupta 82
	private HttpServletRequest request;
83
	private HttpSession session;
84
	private ServletContext context;
1891 ankur.sing 85
 
86
	private long noOfRegisterUsers;
87
	private long noOfOrders;
88
	private long noOfCustomers;
20779 amit.gupta 89
	private int quantity;
1891 ankur.sing 90
	private double maxOrderAmount;
91
	private double minOrderAmount;
92
	private double maxPaymentAmount;
93
	private double minPaymentAmount;
94
	private List<Double> paymentAmountRange;
95
	private List<Double> orderAmountRange;
96
	private List<Order> validOrders;
97
 
20779 amit.gupta 98
	private String my;
99
 
100
	private UserClient usc;
101
	private in.shop2020.model.v1.user.UserContextService.Client uclient;
102
 
103
	private TransactionClient tsc;
104
	private in.shop2020.model.v1.order.TransactionService.Client tClient;
105
 
106
	private PaymentClient psc;
107
	private in.shop2020.payments.PaymentService.Client pClient;
108
 
109
	private final DateFormat formatter = new SimpleDateFormat(
110
			"EEE, dd-MMM-yyyy hh:mm a");
111
 
112
	public StatisticsController() {
113
		try {
114
			usc = new UserClient();
115
			uclient = usc.getClient();
116
 
117
			tsc = new TransactionClient();
118
			tClient = tsc.getClient();
119
 
120
			psc = new PaymentClient();
121
			pClient = psc.getClient();
122
		} catch (Exception e) {
123
			logger.error(
124
					"Error connecting to one of the user, order or payment service",
125
					e);
126
		}
1891 ankur.sing 127
	}
20779 amit.gupta 128
 
1941 ankur.sing 129
	public String index() {
20779 amit.gupta 130
		if (!ReportsUtils.canAccessReport(
131
				(Long) session.getAttribute(ReportsUtils.ROLE),
132
				request.getServletPath())) {
1891 ankur.sing 133
			return "authfail";
134
		}
20779 amit.gupta 135
		getStats();
136
		return "authsuccess";
137
 
138
		/*
139
		 * if(getSessionUserName()==null) { return "authfail"; } else {
140
		 * if(!canAccessReport()) { return "exception"; } getStats(); return
141
		 * "authsuccess"; }
142
		 */
1891 ankur.sing 143
	}
20779 amit.gupta 144
 
1891 ankur.sing 145
	private void getStats() {
20779 amit.gupta 146
		// try {
147
		// noOfRegisterUsers = uclient.getUserCount(UserType.USER);
148
		// } catch(Exception e){
149
		// logger.error("Error while getting the no. of registered users", e);
150
		// }
151
 
3071 chandransh 152
		try {
20779 amit.gupta 153
			// noOfOrders = tClient.getValidOrderCount();
154
			// noOfCustomers =
155
			// tClient.getNoOfCustomersWithSuccessfulTransaction();
156
			// orderAmountRange = tClient.getValidOrdersAmountRange();
157
			// minOrderAmount = orderAmountRange.get(0);
158
			// maxOrderAmount = orderAmountRange.get(1);
159
			//
20783 amit.gupta 160
			validOrders = tClient.getValidOrders(50, false);
20779 amit.gupta 161
		} catch (Exception e) {
162
			logger.error("Error while getting order statistics", e);
3071 chandransh 163
		}
20779 amit.gupta 164
 
3071 chandransh 165
		try {
20779 amit.gupta 166
			paymentAmountRange = pClient.getSuccessfulPaymentsAmountRange();
167
			minPaymentAmount = paymentAmountRange.get(0);
168
			maxPaymentAmount = paymentAmountRange.get(1);
169
		} catch (Exception e) {
170
			logger.error("Error while getting payment statistics", e);
171
		}
1891 ankur.sing 172
	}
20779 amit.gupta 173
 
1891 ankur.sing 174
	public long getNoOfRegisterUsers() {
175
		return noOfRegisterUsers;
176
	}
1886 ankur.sing 177
 
1891 ankur.sing 178
	public long getNoOfOrders() {
179
		return noOfOrders;
180
	}
20779 amit.gupta 181
 
1891 ankur.sing 182
	public long getNoOfCustomers() {
183
		return noOfCustomers;
184
	}
1886 ankur.sing 185
 
1891 ankur.sing 186
	public double getMaxOrderAmount() {
187
		return maxOrderAmount;
188
	}
1886 ankur.sing 189
 
1891 ankur.sing 190
	public double getMinOrderAmount() {
191
		return minOrderAmount;
192
	}
1886 ankur.sing 193
 
1891 ankur.sing 194
	public double getMaxPaymentAmount() {
195
		return maxPaymentAmount;
196
	}
1886 ankur.sing 197
 
1891 ankur.sing 198
	public double getMinPaymentAmount() {
199
		return minPaymentAmount;
200
	}
1886 ankur.sing 201
 
20779 amit.gupta 202
	public List<Order> getValidOrders() {
203
		return validOrders;
204
	}
1941 ankur.sing 205
 
20779 amit.gupta 206
	public LineItem getItem(Order order) throws TransactionServiceException,
207
			TException {
208
		LineItem lItem = order.getLineitems().get(0);
209
		return lItem;
210
	}
1941 ankur.sing 211
 
20779 amit.gupta 212
	public String getOrderStatusString(OrderStatus status) {
213
		return status.getDescription();
214
	}
215
 
216
	public String getDateTime(long milliseconds) {
217
		Calendar cal = Calendar.getInstance();
218
		cal.setTimeInMillis(milliseconds);
219
		return formatter.format(cal.getTime());
220
	}
221
 
222
	@Override
223
	public void setServletRequest(HttpServletRequest req) {
224
		this.request = req;
225
		this.session = req.getSession();
226
	}
227
 
228
	@Override
229
	public void setServletContext(ServletContext context) {
230
		this.context = context;
231
	}
232
 
233
	public String getServletContextPath() {
234
		return context.getContextPath();
235
	}
236
 
237
	public void setQuantity(int quantity) {
238
		this.quantity = quantity;
239
	}
240
 
241
	public int getQuantity() {
242
		return quantity;
243
	}
244
 
245
	public String download() throws Exception {
246
		InputStream is = getClass().getClassLoader().getResourceAsStream("orderformat.xlsx");
20787 amit.gupta 247
		logger.info("is stream is " + (is==null?"null" : "not null"));
20779 amit.gupta 248
		XSSFWorkbook workbook = new XSSFWorkbook(is);
249
		Sheet sheet = workbook.getSheetAt(0);
250
		workbook.setSheetName(0, this.getMy());
251
		DateFormat df = new SimpleDateFormat("MMM, yyyy");
252
		Date fromDate = df.parse(this.getMy());
253
 
254
		Calendar toDateCal = Calendar.getInstance();
255
		toDateCal.add(Calendar.MONTH, 1);
256
		List<Order> orders = tClient.getAllOrders(new ArrayList<OrderStatus>(), fromDate.getTime(), toDateCal.getTime().getTime(), 0);
257
		int rowNum=2;
258
		for(Order o : orders){
259
			Row row = sheet.createRow(rowNum);
260
			int i=0;
261
			row.createCell(++i).setCellValue(o.getId());
262
			row.createCell(++i).setCellValue(new Date(o.getCreated_timestamp()));
263
 
264
			++i;
265
			row.createCell(++i).setCellValue(new Date(o.getVerification_timestamp()));
266
			++i;
267
 
268
			row.createCell(++i).setCellValue(o.getInvoice_number());
269
			row.createCell(++i).setCellValue(o.getBilled_by());
270
			++i;
271
			row.createCell(++i).setCellValue(new Date(o.getBilling_timestamp()));
20783 amit.gupta 272
			row.createCell(++i).setCellValue(new Date(o.getShipping_timestamp()));
20779 amit.gupta 273
			row.createCell(++i).setCellValue(o.getAirwaybill_no());
274
			row.createCell(++i).setCellValue(o.getLogistics_provider_id());
275
			++i;
276
			row.createCell(++i).setCellValue(new Date(o.getDelivery_timestamp()));
277
			row.createCell(++i).setCellValue(o.getLineitems().get(0).getQuantity());
278
			row.createCell(++i).setCellValue(o.getTotal_amount() + o.getShippingCost());
279
			if(o.getStatus().equals(OrderStatus.DELIVERY_SUCCESS)) {
280
				row.createCell(++i).setCellValue(o.getTotal_amount() + o.getShippingCost());
281
			}else {
282
				++i;
283
			}
284
			if(o.isCod()) {
285
				row.createCell(++i).setCellValue("COD");
286
			} else {
287
				row.createCell(++i).setCellValue("PREPAID");
288
			}
289
			row.createCell(++i).setCellValue(o.getStatusDescription());
290
			row.createCell(++i).setCellValue(o.getCustomer_city());
291
 
292
		}
20780 amit.gupta 293
		stream = is;
294
		filename=this.getMy() + "-order.xlsx";
20779 amit.gupta 295
 
20780 amit.gupta 296
		return "report";
20779 amit.gupta 297
	}
298
 
299
	public void setMy(String my) {
300
		this.my = my;
301
	}
302
 
303
	public String getMy() {
304
		return my;
305
	}
1597 ankur.sing 306
}