Subversion Repositories SmartDukaan

Rev

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