Subversion Repositories SmartDukaan

Rev

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