Subversion Repositories SmartDukaan

Rev

Rev 20870 | Rev 23813 | 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<Order> validOrders;
78
 
20779 amit.gupta 79
	private String my;
80
 
81
	private TransactionClient tsc;
82
	private in.shop2020.model.v1.order.TransactionService.Client tClient;
83
 
84
	private PaymentClient psc;
85
	private in.shop2020.payments.PaymentService.Client pClient;
86
 
87
	private final DateFormat formatter = new SimpleDateFormat(
88
			"EEE, dd-MMM-yyyy hh:mm a");
89
 
90
	public StatisticsController() {
91
		try {
92
 
93
			tsc = new TransactionClient();
94
			tClient = tsc.getClient();
95
 
96
			psc = new PaymentClient();
97
			pClient = psc.getClient();
98
		} catch (Exception e) {
99
			logger.error(
100
					"Error connecting to one of the user, order or payment service",
101
					e);
102
		}
1891 ankur.sing 103
	}
20779 amit.gupta 104
 
1941 ankur.sing 105
	public String index() {
20779 amit.gupta 106
		if (!ReportsUtils.canAccessReport(
107
				(Long) session.getAttribute(ReportsUtils.ROLE),
108
				request.getServletPath())) {
1891 ankur.sing 109
			return "authfail";
110
		}
20779 amit.gupta 111
		getStats();
112
		return "authsuccess";
113
 
114
		/*
115
		 * if(getSessionUserName()==null) { return "authfail"; } else {
116
		 * if(!canAccessReport()) { return "exception"; } getStats(); return
117
		 * "authsuccess"; }
118
		 */
1891 ankur.sing 119
	}
20779 amit.gupta 120
 
1891 ankur.sing 121
	private void getStats() {
20779 amit.gupta 122
		// try {
123
		// noOfRegisterUsers = uclient.getUserCount(UserType.USER);
124
		// } catch(Exception e){
125
		// logger.error("Error while getting the no. of registered users", e);
126
		// }
127
 
3071 chandransh 128
		try {
20779 amit.gupta 129
			// noOfOrders = tClient.getValidOrderCount();
130
			// noOfCustomers =
131
			// tClient.getNoOfCustomersWithSuccessfulTransaction();
132
			// orderAmountRange = tClient.getValidOrdersAmountRange();
133
			// minOrderAmount = orderAmountRange.get(0);
134
			// maxOrderAmount = orderAmountRange.get(1);
135
			//
20783 amit.gupta 136
			validOrders = tClient.getValidOrders(50, false);
20779 amit.gupta 137
		} catch (Exception e) {
138
			logger.error("Error while getting order statistics", e);
3071 chandransh 139
		}
20779 amit.gupta 140
 
3071 chandransh 141
		try {
20779 amit.gupta 142
			paymentAmountRange = pClient.getSuccessfulPaymentsAmountRange();
143
			minPaymentAmount = paymentAmountRange.get(0);
144
			maxPaymentAmount = paymentAmountRange.get(1);
145
		} catch (Exception e) {
146
			logger.error("Error while getting payment statistics", e);
147
		}
1891 ankur.sing 148
	}
20779 amit.gupta 149
 
1891 ankur.sing 150
	public long getNoOfRegisterUsers() {
151
		return noOfRegisterUsers;
152
	}
1886 ankur.sing 153
 
1891 ankur.sing 154
	public long getNoOfOrders() {
155
		return noOfOrders;
156
	}
20779 amit.gupta 157
 
1891 ankur.sing 158
	public long getNoOfCustomers() {
159
		return noOfCustomers;
160
	}
1886 ankur.sing 161
 
1891 ankur.sing 162
	public double getMaxOrderAmount() {
163
		return maxOrderAmount;
164
	}
1886 ankur.sing 165
 
1891 ankur.sing 166
	public double getMinOrderAmount() {
167
		return minOrderAmount;
168
	}
1886 ankur.sing 169
 
1891 ankur.sing 170
	public double getMaxPaymentAmount() {
171
		return maxPaymentAmount;
172
	}
1886 ankur.sing 173
 
1891 ankur.sing 174
	public double getMinPaymentAmount() {
175
		return minPaymentAmount;
176
	}
1886 ankur.sing 177
 
20779 amit.gupta 178
	public List<Order> getValidOrders() {
179
		return validOrders;
180
	}
1941 ankur.sing 181
 
20779 amit.gupta 182
	public LineItem getItem(Order order) throws TransactionServiceException,
183
			TException {
184
		LineItem lItem = order.getLineitems().get(0);
185
		return lItem;
186
	}
1941 ankur.sing 187
 
20779 amit.gupta 188
	public String getOrderStatusString(OrderStatus status) {
189
		return status.getDescription();
190
	}
191
 
192
	public String getDateTime(long milliseconds) {
193
		Calendar cal = Calendar.getInstance();
194
		cal.setTimeInMillis(milliseconds);
195
		return formatter.format(cal.getTime());
196
	}
197
 
198
	@Override
199
	public void setServletRequest(HttpServletRequest req) {
200
		this.request = req;
201
		this.session = req.getSession();
202
	}
203
 
204
	@Override
205
	public void setServletContext(ServletContext context) {
206
		this.context = context;
207
	}
208
 
209
	public String getServletContextPath() {
210
		return context.getContextPath();
211
	}
212
 
213
	public void setQuantity(int quantity) {
214
		this.quantity = quantity;
215
	}
216
 
217
	public int getQuantity() {
218
		return quantity;
219
	}
220
 
221
	public String download() throws Exception {
222
		InputStream is = getClass().getClassLoader().getResourceAsStream("orderformat.xlsx");
20791 amit.gupta 223
		XSSFWorkbook workBook = new XSSFWorkbook(is);
224
		XSSFDataFormat df1 = workBook.createDataFormat();
225
		CellStyle cs = workBook.createCellStyle();
226
		cs.setDataFormat(df1.getFormat("dd-mm-yy HH:mm"));
20790 amit.gupta 227
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
20791 amit.gupta 228
		Sheet sheet = workBook.getSheetAt(0);
229
		workBook.setSheetName(0, this.getMy());
20779 amit.gupta 230
		DateFormat df = new SimpleDateFormat("MMM, yyyy");
231
		Date fromDate = df.parse(this.getMy());
232
 
233
		Calendar toDateCal = Calendar.getInstance();
234
		toDateCal.add(Calendar.MONTH, 1);
235
		List<Order> orders = tClient.getAllOrders(new ArrayList<OrderStatus>(), fromDate.getTime(), toDateCal.getTime().getTime(), 0);
236
		int rowNum=2;
237
		for(Order o : orders){
238
			Row row = sheet.createRow(rowNum);
239
			int i=0;
20791 amit.gupta 240
			row.createCell(i++).setCellValue(o.getId());
241
			Cell created  = row.createCell(i++);
242
			created.setCellValue(new Date(o.getCreated_timestamp()));
243
			created.setCellStyle(cs);
20779 amit.gupta 244
 
20791 amit.gupta 245
			i++;
246
			if(o.getVerification_timestamp()>0){
247
				Cell verified  = row.createCell(i++);
248
				verified.setCellValue(new Date(o.getVerification_timestamp()));
249
				verified.setCellStyle(cs);
250
			} else {
251
				i++;
252
			}
253
			i++;
20779 amit.gupta 254
 
20791 amit.gupta 255
			row.createCell(i++).setCellValue(o.getInvoice_number());
256
			row.createCell(i++).setCellValue(o.getBilled_by());
257
			i++;
258
			if(o.getBilling_timestamp()>0){
259
				Cell billed  = row.createCell(i++);
260
				billed.setCellValue(new Date(o.getBilling_timestamp()));
261
				billed.setCellStyle(cs);
262
			} else {
263
				i++;
264
			}
265
			if(o.getShipping_timestamp()>0) {
266
				Cell shipped  = row.createCell(i++);
267
				shipped.setCellValue(new Date(o.getShipping_timestamp()));
268
				shipped.setCellStyle(cs);
269
			} else {
270
				i++;
271
			}
272
			row.createCell(i++).setCellValue(o.getAirwaybill_no());
273
			row.createCell(i++).setCellValue(o.getLogistics_provider_id());
274
			i++;
275
			if(o.getDelivery_timestamp()>0){
276
				Cell delivered  = row.createCell(i++);
277
				delivered.setCellValue(new Date(o.getDelivery_timestamp()));
278
				delivered.setCellStyle(cs);
279
			} else {
280
				i++;
281
			}
282
			row.createCell(i++).setCellValue(o.getLineitems().get(0).getQuantity());
283
			row.createCell(i++).setCellValue(o.getTotal_amount() + o.getShippingCost());
20779 amit.gupta 284
			if(o.getStatus().equals(OrderStatus.DELIVERY_SUCCESS)) {
20791 amit.gupta 285
				row.createCell(i++).setCellValue(o.getTotal_amount() + o.getShippingCost());
20779 amit.gupta 286
			}else {
20791 amit.gupta 287
				i++;
20779 amit.gupta 288
			}
289
			if(o.isCod()) {
20791 amit.gupta 290
				row.createCell(i++).setCellValue("COD");
20779 amit.gupta 291
			} else {
20791 amit.gupta 292
				row.createCell(i++).setCellValue("PREPAID");
20779 amit.gupta 293
			}
20870 amit.gupta 294
			row.createCell(i++).setCellValue(o.getStatus().getDescription());
20803 amit.gupta 295
			row.createCell(i++).setCellValue(o.getCustomer_id());
296
			row.createCell(i++).setCellValue(o.getCustomer_name());
20791 amit.gupta 297
			row.createCell(i++).setCellValue(o.getCustomer_city());
21087 amit.gupta 298
			row.createCell(i++).setCellValue(o.getCustomer_pincode());
299
			row.createCell(i++).setCellValue(o.getCustomer_state());
20796 amit.gupta 300
			row.createCell(i++).setCellValue(o.getLineitems().get(0).getItem_id());
21087 amit.gupta 301
			LineItem lineItem = o.getLineitems().get(0);
302
			row.createCell(i++).setCellValue((lineItem.getBrand() +  " " + getValidName(lineItem.getModel_name()) +" " + getValidName(lineItem.getModel_number()).replaceAll("  ", " ")));
303
			row.createCell(i++).setCellValue(lineItem.getColor());
20813 amit.gupta 304
			row.createCell(i++).setCellValue(o.getLogisticsTransactionId());
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
	}
20800 amit.gupta 332
 
333
	private String getValidName(String name){
334
        return name!=null?name:"";
335
    }
1597 ankur.sing 336
}