Subversion Repositories SmartDukaan

Rev

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