Subversion Repositories SmartDukaan

Rev

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