Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
7082 rajveer 1
package in.shop2020.recharge.controllers;
2
 
3
 
7169 anupam.sin 4
import in.shop2020.model.v1.order.HotspotStore;
7082 rajveer 5
import in.shop2020.model.v1.order.RechargeOrderStatus;
6
import in.shop2020.model.v1.order.RechargeTransaction;
7169 anupam.sin 7
import in.shop2020.thrift.clients.HelperClient;
7082 rajveer 8
import in.shop2020.thrift.clients.TransactionClient;
7169 anupam.sin 9
import in.shop2020.utils.Mail;
7082 rajveer 10
 
7151 amit.gupta 11
import java.io.ByteArrayOutputStream;
12
import java.io.IOException;
7082 rajveer 13
import java.text.DateFormat;
14
import java.text.SimpleDateFormat;
7169 anupam.sin 15
import java.util.ArrayList;
7082 rajveer 16
import java.util.Calendar;
7151 amit.gupta 17
import java.util.Date;
7082 rajveer 18
import java.util.List;
7169 anupam.sin 19
import java.util.Random;
7082 rajveer 20
 
21
import javax.servlet.ServletContext;
7151 amit.gupta 22
import javax.servlet.ServletOutputStream;
7082 rajveer 23
import javax.servlet.http.HttpServletRequest;
7151 amit.gupta 24
import javax.servlet.http.HttpServletResponse;
7082 rajveer 25
import javax.servlet.http.HttpSession;
26
 
7151 amit.gupta 27
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
28
import org.apache.poi.ss.usermodel.Row;
29
import org.apache.poi.ss.usermodel.Sheet;
30
import org.apache.poi.ss.usermodel.Workbook;
7082 rajveer 31
import org.apache.struts2.interceptor.ServletRequestAware;
7151 amit.gupta 32
import org.apache.struts2.interceptor.ServletResponseAware;
7082 rajveer 33
import org.apache.struts2.util.ServletContextAware;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
 
37
 
38
 
7151 amit.gupta 39
public class ReportController implements ServletRequestAware, ServletContextAware, ServletResponseAware {
40
 
7082 rajveer 41
    private static Logger logger = LoggerFactory.getLogger(ReportController.class);
42
 
7151 amit.gupta 43
    protected HttpServletRequest request;
44
    protected HttpSession session;
45
    protected HttpServletResponse response;
7082 rajveer 46
    private ServletContext context;
7151 amit.gupta 47
    private String startDate;
48
	private String endDate;
49
	private String status;
50
	private String dateselector;
7169 anupam.sin 51
	private String searchError = "";
52
	private String number = "";
53
	private String passwordGeneration = "";
7151 amit.gupta 54
 
7169 anupam.sin 55
	private static final String chars = "0123456789";
56
	private static final int LENGTH = 4;
57
	private static final Random random = new Random();
7082 rajveer 58
	private TransactionClient tsc;
59
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
60
 
61
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
7151 amit.gupta 62
    private final DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
7082 rajveer 63
 
64
    private List<RechargeTransaction> txns = null;
7169 anupam.sin 65
    private List<RechargeTransaction> searchResult = null;
66
 
67
    private boolean showReprintColumn = false;
68
 
7082 rajveer 69
    public ReportController(){
70
	    try {
71
            tsc = new TransactionClient();
72
            tClient = tsc.getClient();
73
        } catch (Exception e) {
74
            logger.error("Error connecting to one of the user, order or payment service", e);
75
        }
76
	}
77
 
78
 
7087 rajveer 79
	public String index() throws Exception{
7096 anupam.sin 80
	    String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
81
        if(loginStatus == null || !loginStatus.equals("TRUE")){
82
            return "authfail";
83
        }
84
        txns = tClient.getRechargeTransactions(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")));
7082 rajveer 85
        return "index";
86
	}
87
 
7169 anupam.sin 88
	public String search() throws Exception {
89
	    String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
90
        if(loginStatus == null || !loginStatus.equals("TRUE")){
91
            return "authfail";
92
        }
93
	    if(number != null && !number.equals("")) {
94
	        try {
7172 anupam.sin 95
	            setSearchResult(tClient.getRechargeTransactionsByNumber(number.trim(), Long.parseLong((String) request.getSession().getAttribute("STORE_ID"))));
7169 anupam.sin 96
	            if(searchResult.size() == 0) {
97
	                setSearchError("Could not find any recharges with this number. Please try again.");
98
	            }
99
	            for(RechargeTransaction txn : getSearchResult()) {
100
	                if(txn.getStatus().equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)) {
101
	                    setShowReprintColumn(true);
102
	                    break;
103
	                }
104
	            }
105
	        } catch(Exception e) {
106
	            setSearchError("Some error occured. Please try again.");
107
	            logger.error("Error during search", e);
108
	        }
109
	    }
110
        return index();
111
	}
112
 
113
	public String getCollection() throws Exception{
114
	    String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
115
        if(loginStatus == null || !loginStatus.equals("TRUE")){
116
            return "authfail";
117
        }
118
	    long today = -1;
119
	    today = new Date().getTime();
120
	    List<RechargeTransaction> successfulRecharges = tClient.getRechargeTrans(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")), today, today, RechargeOrderStatus.RECHARGE_SUCCESSFUL);
121
	    List<RechargeTransaction> refundedRecharges = tClient.getRechargeTrans(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")), today, today, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED);
122
	    ByteArrayOutputStream baos = generateCollectionReport(successfulRecharges, refundedRecharges);
123
	    response.setContentType("application/vnd.ms-excel");
124
        String fileName = "collection-report";
125
        String todayDate = dateFormatter.format(new Date(today));
126
        fileName = fileName + "-" + todayDate.replaceAll("\\\\", "-") ;
127
        fileName = fileName + ".xls";
128
        response.setHeader("Content-disposition", "inline; filename=" + fileName);
129
        ServletOutputStream sos;
130
        try {
131
            sos = response.getOutputStream();
132
            baos.writeTo(sos);
133
            sos.flush();
134
        } catch (IOException e) {
135
            e.printStackTrace();
136
        }
137
        return null;
138
 
139
	}
140
 
141
	private ByteArrayOutputStream generateCollectionReport(List<RechargeTransaction> successfulRecharges,
142
                                                            List<RechargeTransaction> refundedRecharges) {
143
	    ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
144
        Workbook wb = new HSSFWorkbook();
145
        Sheet reportSheet = wb.createSheet("Recharge Report");
146
        Row reportSheetHeader = reportSheet.createRow((short)0);
147
        reportSheetHeader.createCell(0).setCellValue("Order ID");
148
        reportSheetHeader.createCell(1).setCellValue("Order Date");
149
        reportSheetHeader.createCell(2).setCellValue("Operator");
150
        reportSheetHeader.createCell(3).setCellValue("Device Number");
151
        reportSheetHeader.createCell(4).setCellValue("Status");
152
        reportSheetHeader.createCell(5).setCellValue("Pay Method");
153
        reportSheetHeader.createCell(6).setCellValue("Refund Date");
154
        reportSheetHeader.createCell(7).setCellValue("Type");
155
        reportSheetHeader.createCell(8).setCellValue("Total Amount");
156
        reportSheetHeader.createCell(9).setCellValue("Discount");
157
        reportSheetHeader.createCell(10).setCellValue("Net Amount");
158
 
159
        int serialNo = 0;
160
 
161
        for(RechargeTransaction transaction : successfulRecharges) {
162
            serialNo++;
163
            Row contentRow = reportSheet.createRow((short)serialNo);
164
            contentRow.createCell(0).setCellValue(transaction.getId());
165
            contentRow.createCell(1).setCellValue(formatter.format(new Date(transaction.getTransactionTime())));
166
            contentRow.createCell(2).setCellValue(getOperatorName(transaction.getOperatorId()));
167
            contentRow.createCell(3).setCellValue(transaction.getDeviceNum());
168
            contentRow.createCell(4).setCellValue(transaction.getStatus().name());
169
            contentRow.createCell(5).setCellValue(transaction.getPayMethod().name());
170
            contentRow.createCell(6).setCellValue("N/A");
171
            contentRow.createCell(7).setCellValue(transaction.isIsFrc() ? "FRC" : "Normal");
172
            contentRow.createCell(8).setCellValue(transaction.getAmount());
173
            contentRow.createCell(9).setCellValue(transaction.getDiscount());
174
            contentRow.createCell(10).setCellValue(transaction.getPaymentAmount());
175
        }
176
 
177
        for(RechargeTransaction transaction : refundedRecharges) {
178
            serialNo++;
179
            Row contentRow = reportSheet.createRow((short)serialNo);
180
            contentRow.createCell(0).setCellValue(transaction.getId());
181
            contentRow.createCell(1).setCellValue(formatter.format(new Date(transaction.getTransactionTime())));
182
            contentRow.createCell(2).setCellValue(getOperatorName(transaction.getOperatorId()));
183
            contentRow.createCell(3).setCellValue(transaction.getDeviceNum());
184
            contentRow.createCell(4).setCellValue(transaction.getStatus().name());
185
            contentRow.createCell(5).setCellValue(transaction.getPayMethod().name());
186
            contentRow.createCell(6).setCellValue(formatter.format(new Date(transaction.getResponseTime())));
187
            contentRow.createCell(7).setCellValue(transaction.isIsFrc() ? "FRC" : "Normal");
188
            contentRow.createCell(8).setCellValue(transaction.getAmount());
189
            contentRow.createCell(9).setCellValue(transaction.getDiscount());
190
            contentRow.createCell(10).setCellValue(transaction.getPaymentAmount());
191
        }
192
 
193
        try {
194
            wb.write(baosXLS);
195
        } catch (IOException e) {
196
            // TODO Auto-generated catch block
197
            e.printStackTrace();
198
        }
199
        return baosXLS;
200
    }
201
 
202
 
7151 amit.gupta 203
	public String create() throws Exception{
204
		long sDate = -1;
205
		long eDate = -1;
206
		RechargeOrderStatus st = null;
207
		String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
208
		if(loginStatus == null || !loginStatus.equals("TRUE")){
209
			return "authfail";
210
		}
211
		if(dateselector.equals("1")) {
212
			sDate = new Date().getTime();
213
			eDate = sDate;
214
		}else if (dateselector.equals("2")) {
215
			sDate = new Date().getTime() - 86400*1000;
216
			eDate = sDate;
217
		}else {
218
			if(!(startDate.equals(""))) {
219
				sDate = dateFormatter.parse(startDate).getTime();
220
				startDate = dateFormatter.format(sDate);
221
			}
222
			if(!endDate.equals("")) {
223
				eDate = dateFormatter.parse(endDate).getTime();
224
				endDate = dateFormatter.format(sDate);
225
			}
226
		}
227
		if(!status.equals("-1")){
228
			st = RechargeOrderStatus.findByValue(Integer.parseInt("0"));
229
		}
230
 
231
		ByteArrayOutputStream baos = generateReport(tClient.getRechargeTrans(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")), sDate, eDate, st));
232
 
233
		response.setContentType("application/vnd.ms-excel");
234
		String st1 = "ALL";
235
		if(st!=null){
236
			st1 = st.name();
237
		}
238
		String fileName = st1 + "-recharge-report";
239
		if (!startDate.equals("")) {
240
			fileName = fileName + "-" + startDate.replaceAll("\\\\", "") ;
241
		}
242
		if (!endDate.equals("")) {
243
			fileName = fileName + "-" + endDate.replaceAll("\\\\", "") ;
244
		}
245
		fileName = fileName + ".xls";
246
		response.setHeader("Content-disposition", "inline; filename=" + fileName);
247
        ServletOutputStream sos;
248
        try {
249
            sos = response.getOutputStream();
250
            baos.writeTo(sos);
251
            sos.flush();
252
        } catch (IOException e) {
253
            e.printStackTrace();
254
        }
255
		return "index";
256
	}
257
 
7082 rajveer 258
 
7151 amit.gupta 259
    private ByteArrayOutputStream generateReport(
260
			List<RechargeTransaction> rechargeTrans) {
261
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
262
 
263
	    Workbook wb = new HSSFWorkbook();
264
 
265
	    Sheet reportSheet = wb.createSheet("Recharge Report");
266
 
267
	    Row reportSheetHeader = reportSheet.createRow((short)0);
268
	    reportSheetHeader.createCell(0).setCellValue("Order ID");
269
	    reportSheetHeader.createCell(1).setCellValue("Order Date");
270
	    reportSheetHeader.createCell(2).setCellValue("Operator");
271
	    reportSheetHeader.createCell(3).setCellValue("Device");
272
	    reportSheetHeader.createCell(4).setCellValue("Status");
7169 anupam.sin 273
	    reportSheetHeader.createCell(5).setCellValue("Recharge Amount");
7151 amit.gupta 274
	    reportSheetHeader.createCell(6).setCellValue("Discount");
7169 anupam.sin 275
	    reportSheetHeader.createCell(7).setCellValue("Net Amount");
276
	    reportSheetHeader.createCell(8).setCellValue("Type");
277
	    reportSheetHeader.createCell(9).setCellValue("Pay Method");
7151 amit.gupta 278
 
279
	    int serialNo = 0;
280
 
281
	    for(RechargeTransaction transaction : rechargeTrans) {
282
	    	serialNo++;
283
	    	Row contentRow = reportSheet.createRow((short)serialNo);
284
	    	contentRow.createCell(0).setCellValue(transaction.getId());
285
	    	contentRow.createCell(1).setCellValue(formatter.format(new Date(transaction.getTransactionTime())));
286
	    	contentRow.createCell(2).setCellValue(getOperatorName(transaction.getOperatorId()));
287
	    	contentRow.createCell(3).setCellValue(transaction.getDeviceNum());
288
	    	contentRow.createCell(4).setCellValue(transaction.getStatus().name());
289
	    	contentRow.createCell(5).setCellValue(transaction.getAmount());
290
	    	contentRow.createCell(6).setCellValue(transaction.getDiscount());
7169 anupam.sin 291
	    	contentRow.createCell(7).setCellValue(transaction.getPaymentAmount());
292
	    	contentRow.createCell(8).setCellValue(transaction.isIsFrc() ? "FRC" : "Normal");
293
	    	contentRow.createCell(9).setCellValue(transaction.getPayMethod().name());
7151 amit.gupta 294
	    }
295
	    try {
296
			wb.write(baosXLS);
297
		} catch (IOException e) {
298
			// TODO Auto-generated catch block
299
			e.printStackTrace();
300
		}
301
	    return baosXLS;
302
	}
303
 
7169 anupam.sin 304
 
305
    public String sendPassword() throws Exception {
306
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
307
        if(loginStatus == null || !loginStatus.equals("TRUE")){
308
            return "authfail";
309
        }
310
        Long storeId = Long.parseLong((String) request.getSession().getAttribute("STORE_ID"));
311
 
312
        char[] buf = new char[LENGTH];
313
        for (int i = 0; i < buf.length; i++) {
314
            buf[i] = chars.charAt(random.nextInt(chars.length()));
315
        }
316
        String password = new String(buf);
317
 
318
        try {
319
            TransactionClient tcl = new TransactionClient(); 
320
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(storeId, "");
321
            boolean wasPasswordSet = tcl.getClient().updateHotspotStorePassword(storeId, password);
322
            if(!wasPasswordSet) {
323
                passwordGeneration = "FAIL";
324
                return index();
325
            }
326
            List<String> toList = new ArrayList<String>();
327
            toList.add(hotSpotStore.getEmail());
328
            HelperClient helperServiceClient = null;
329
            helperServiceClient = new HelperClient();
330
            in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
331
 
332
 
333
            Mail mail = new Mail();
334
            mail.setSubject("New Password for Saholic Recharge");
335
            mail.setTo(toList);
336
            mail.setData("Your new password is : " + password);
337
            client.sendMail(mail);
338
        } catch(Exception e) {
339
            passwordGeneration = "FAIL";
340
            logger.error("Password generation/sending failed for storeId : " + storeId, e);
341
        }
342
        passwordGeneration = "SUCCESS";
343
        return index();
344
    }
7151 amit.gupta 345
 
346
	public String getDateTime(long milliseconds) {
7082 rajveer 347
        Calendar cal = Calendar.getInstance();
348
        cal.setTimeInMillis(milliseconds);
349
        return formatter.format(cal.getTime());
350
    }
351
 
352
    public List<RechargeTransaction> getTxns(){
353
    	return txns;
354
    }
355
 
356
    public void setServletRequest(HttpServletRequest req) {
357
        this.request = req;
358
        this.session = req.getSession();        
359
    }
360
 
361
    public void setServletContext(ServletContext context) {
362
        this.context = context;
363
    }
7151 amit.gupta 364
 
365
    public void setServletResponse(HttpServletResponse response) {
366
        this.response = response;
367
    }
7082 rajveer 368
 
369
    public String getServletContextPath() {
370
        return context.getContextPath();
371
    }
7096 anupam.sin 372
 
373
    public String getOperatorName(long operatorId) {
374
        String operatorName = "";
375
        if (HomeController.getMobileProvidersMap().containsKey(operatorId)) {
376
            operatorName = HomeController.getMobileProvidersMap().get(operatorId);
377
        } else if(HomeController.getDthProvidersMap().containsKey(operatorId)) {
378
            operatorName = HomeController.getDthProvidersMap().get(operatorId);
379
        } else {
380
            operatorName = "N/A";
381
        }
382
        return operatorName;
383
    }
7151 amit.gupta 384
 
385
	public String getStartDate() {
386
		return startDate;
387
	}
388
 
389
 
390
	public void setStartDate(String startDate) {
391
		this.startDate = startDate;
392
	}
7082 rajveer 393
 
7151 amit.gupta 394
	public String getEndDate() {
395
		return endDate;
396
	}
397
 
398
 
399
	public void setEndDate(String endDate) {
400
		this.endDate = endDate;
401
	}
402
 
403
	public String getStatus() {
404
		return status;
405
	}
406
 
407
 
408
	public void setStatus(String status) {
409
		this.status = status;
410
	}
411
 
412
 
413
	public String getDateselector() {
414
		return dateselector;
415
	}
416
 
417
 
418
	public void setDateselector(String dateselector) {
419
		this.dateselector = dateselector;
420
	}
7169 anupam.sin 421
 
422
 
423
    public void setSearchError(String searchError) {
424
        this.searchError = searchError;
425
    }
426
 
427
 
428
    public String getSearchError() {
429
        return searchError;
430
    }
431
 
432
 
433
    public void setNumber(String number) {
434
        this.number = number;
435
    }
436
 
437
 
438
    public String getNumber() {
439
        return number;
440
    }
441
 
442
 
443
    public void setSearchResult(List<RechargeTransaction> searchResult) {
444
        this.searchResult = searchResult;
445
    }
446
 
447
 
448
    public List<RechargeTransaction> getSearchResult() {
449
        if(searchResult == null)
450
            searchResult = new ArrayList<RechargeTransaction>();
451
        return searchResult;
452
    }
453
 
454
 
455
    public void setShowReprintColumn(boolean showReprintColumn) {
456
        this.showReprintColumn = showReprintColumn;
457
    }
458
 
459
 
460
    public boolean shouldShowReprintColumn() {
461
        return showReprintColumn;
462
    }
463
 
464
 
465
    public void setPasswordGeneration(String passwordGeneration) {
466
        this.passwordGeneration = passwordGeneration;
467
    }
468
 
469
 
470
    public String getPasswordGeneration() {
471
        return passwordGeneration;
472
    }
7151 amit.gupta 473
 
474
 
475
 
476
}