Subversion Repositories SmartDukaan

Rev

Rev 7151 | Rev 7172 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7151 Rev 7169
Line 1... Line 1...
1
package in.shop2020.recharge.controllers;
1
package in.shop2020.recharge.controllers;
2
 
2
 
3
 
3
 
-
 
4
import in.shop2020.model.v1.order.HotspotStore;
4
import in.shop2020.model.v1.order.RechargeOrderStatus;
5
import in.shop2020.model.v1.order.RechargeOrderStatus;
5
import in.shop2020.model.v1.order.RechargeTransaction;
6
import in.shop2020.model.v1.order.RechargeTransaction;
-
 
7
import in.shop2020.thrift.clients.HelperClient;
6
import in.shop2020.thrift.clients.TransactionClient;
8
import in.shop2020.thrift.clients.TransactionClient;
7
import in.shop2020.warehouse.InventoryAvailability;
9
import in.shop2020.utils.Mail;
8
 
10
 
9
import java.io.ByteArrayOutputStream;
11
import java.io.ByteArrayOutputStream;
10
import java.io.IOException;
12
import java.io.IOException;
11
import java.text.DateFormat;
13
import java.text.DateFormat;
12
import java.text.SimpleDateFormat;
14
import java.text.SimpleDateFormat;
-
 
15
import java.util.ArrayList;
13
import java.util.Calendar;
16
import java.util.Calendar;
14
import java.util.Date;
17
import java.util.Date;
15
import java.util.List;
18
import java.util.List;
16
import java.util.Map;
19
import java.util.Random;
17
 
20
 
18
import javax.servlet.ServletContext;
21
import javax.servlet.ServletContext;
19
import javax.servlet.ServletOutputStream;
22
import javax.servlet.ServletOutputStream;
20
import javax.servlet.http.HttpServletRequest;
23
import javax.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletResponse;
24
import javax.servlet.http.HttpServletResponse;
22
import javax.servlet.http.HttpSession;
25
import javax.servlet.http.HttpSession;
23
 
26
 
24
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25
import org.apache.poi.ss.usermodel.CreationHelper;
-
 
26
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
-
 
27
import org.apache.poi.ss.usermodel.Row;
28
import org.apache.poi.ss.usermodel.Row;
28
import org.apache.poi.ss.usermodel.Sheet;
29
import org.apache.poi.ss.usermodel.Sheet;
29
import org.apache.poi.ss.usermodel.Workbook;
30
import org.apache.poi.ss.usermodel.Workbook;
30
import org.apache.struts2.interceptor.ServletRequestAware;
31
import org.apache.struts2.interceptor.ServletRequestAware;
31
import org.apache.struts2.interceptor.ServletResponseAware;
32
import org.apache.struts2.interceptor.ServletResponseAware;
32
import org.apache.struts2.util.ServletContextAware;
33
import org.apache.struts2.util.ServletContextAware;
33
import org.slf4j.Logger;
34
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35
import org.slf4j.LoggerFactory;
35
 
36
 
36
import com.sun.mail.iap.Response;
-
 
37
 
-
 
38
 
37
 
39
 
38
 
40
public class ReportController implements ServletRequestAware, ServletContextAware, ServletResponseAware {
39
public class ReportController implements ServletRequestAware, ServletContextAware, ServletResponseAware {
41
 
40
 
42
    private static Logger logger = LoggerFactory.getLogger(ReportController.class);
41
    private static Logger logger = LoggerFactory.getLogger(ReportController.class);
Line 47... Line 46...
47
    private ServletContext context;
46
    private ServletContext context;
48
    private String startDate;
47
    private String startDate;
49
	private String endDate;
48
	private String endDate;
50
	private String status;
49
	private String status;
51
	private String dateselector;
50
	private String dateselector;
-
 
51
	private String searchError = "";
-
 
52
	private String number = "";
-
 
53
	private String passwordGeneration = "";
52
	
54
	
-
 
55
	private static final String chars = "0123456789";
-
 
56
	private static final int LENGTH = 4;
-
 
57
	private static final Random random = new Random();
53
	private TransactionClient tsc;
58
	private TransactionClient tsc;
54
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
59
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
55
    
60
    
56
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
61
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
57
    private final DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
62
    private final DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
58
	
63
	
59
    private List<RechargeTransaction> txns = null;
64
    private List<RechargeTransaction> txns = null;
-
 
65
    private List<RechargeTransaction> searchResult = null;
-
 
66
 
-
 
67
    private boolean showReprintColumn = false;
-
 
68
    
60
    public ReportController(){
69
    public ReportController(){
61
	    try {
70
	    try {
62
            tsc = new TransactionClient();
71
            tsc = new TransactionClient();
63
            tClient = tsc.getClient();
72
            tClient = tsc.getClient();
64
        } catch (Exception e) {
73
        } catch (Exception e) {
Line 74... Line 83...
74
        }
83
        }
75
        txns = tClient.getRechargeTransactions(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")));
84
        txns = tClient.getRechargeTransactions(Long.parseLong((String) request.getSession().getAttribute("STORE_ID")));
76
        return "index";
85
        return "index";
77
	}
86
	}
78
	
87
	
-
 
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 {
-
 
95
	            setSearchResult(tClient.getRechargeTransactionsByNumber(number.trim()));
-
 
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
	
79
	public String create() throws Exception{
203
	public String create() throws Exception{
80
		long sDate = -1;
204
		long sDate = -1;
81
		long eDate = -1;
205
		long eDate = -1;
82
		RechargeOrderStatus st = null;
206
		RechargeOrderStatus st = null;
83
		String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
207
		String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
Line 144... Line 268...
144
	    reportSheetHeader.createCell(0).setCellValue("Order ID");
268
	    reportSheetHeader.createCell(0).setCellValue("Order ID");
145
	    reportSheetHeader.createCell(1).setCellValue("Order Date");
269
	    reportSheetHeader.createCell(1).setCellValue("Order Date");
146
	    reportSheetHeader.createCell(2).setCellValue("Operator");
270
	    reportSheetHeader.createCell(2).setCellValue("Operator");
147
	    reportSheetHeader.createCell(3).setCellValue("Device");
271
	    reportSheetHeader.createCell(3).setCellValue("Device");
148
	    reportSheetHeader.createCell(4).setCellValue("Status");
272
	    reportSheetHeader.createCell(4).setCellValue("Status");
149
	    reportSheetHeader.createCell(5).setCellValue("Amount");
273
	    reportSheetHeader.createCell(5).setCellValue("Recharge Amount");
150
	    reportSheetHeader.createCell(6).setCellValue("Discount");
274
	    reportSheetHeader.createCell(6).setCellValue("Discount");
-
 
275
	    reportSheetHeader.createCell(7).setCellValue("Net Amount");
-
 
276
	    reportSheetHeader.createCell(8).setCellValue("Type");
-
 
277
	    reportSheetHeader.createCell(9).setCellValue("Pay Method");
151
	    
278
	    
152
	    int serialNo = 0;
279
	    int serialNo = 0;
153
	    
280
	    
154
	    for(RechargeTransaction transaction : rechargeTrans) {
281
	    for(RechargeTransaction transaction : rechargeTrans) {
155
	    	serialNo++;
282
	    	serialNo++;
Line 159... Line 286...
159
	    	contentRow.createCell(2).setCellValue(getOperatorName(transaction.getOperatorId()));
286
	    	contentRow.createCell(2).setCellValue(getOperatorName(transaction.getOperatorId()));
160
	    	contentRow.createCell(3).setCellValue(transaction.getDeviceNum());
287
	    	contentRow.createCell(3).setCellValue(transaction.getDeviceNum());
161
	    	contentRow.createCell(4).setCellValue(transaction.getStatus().name());
288
	    	contentRow.createCell(4).setCellValue(transaction.getStatus().name());
162
	    	contentRow.createCell(5).setCellValue(transaction.getAmount());
289
	    	contentRow.createCell(5).setCellValue(transaction.getAmount());
163
	    	contentRow.createCell(6).setCellValue(transaction.getDiscount());
290
	    	contentRow.createCell(6).setCellValue(transaction.getDiscount());
-
 
291
	    	contentRow.createCell(7).setCellValue(transaction.getPaymentAmount());
-
 
292
	    	contentRow.createCell(8).setCellValue(transaction.isIsFrc() ? "FRC" : "Normal");
-
 
293
	    	contentRow.createCell(9).setCellValue(transaction.getPayMethod().name());
164
	    }
294
	    }
165
	    try {
295
	    try {
166
			wb.write(baosXLS);
296
			wb.write(baosXLS);
167
		} catch (IOException e) {
297
		} catch (IOException e) {
168
			// TODO Auto-generated catch block
298
			// TODO Auto-generated catch block
169
			e.printStackTrace();
299
			e.printStackTrace();
170
		}
300
		}
171
	    return baosXLS;
301
	    return baosXLS;
172
	}
302
	}
173
 
303
 
-
 
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
    }
174
 
345
 
175
	public String getDateTime(long milliseconds) {
346
	public String getDateTime(long milliseconds) {
176
        Calendar cal = Calendar.getInstance();
347
        Calendar cal = Calendar.getInstance();
177
        cal.setTimeInMillis(milliseconds);
348
        cal.setTimeInMillis(milliseconds);
178
        return formatter.format(cal.getTime());
349
        return formatter.format(cal.getTime());
Line 180... Line 351...
180
 
351
 
181
    public List<RechargeTransaction> getTxns(){
352
    public List<RechargeTransaction> getTxns(){
182
    	return txns;
353
    	return txns;
183
    }
354
    }
184
    
355
    
185
    @Override
-
 
186
    public void setServletRequest(HttpServletRequest req) {
356
    public void setServletRequest(HttpServletRequest req) {
187
        this.request = req;
357
        this.request = req;
188
        this.session = req.getSession();        
358
        this.session = req.getSession();        
189
    }
359
    }
190
    
360
    
191
    @Override
-
 
192
    public void setServletContext(ServletContext context) {
361
    public void setServletContext(ServletContext context) {
193
        this.context = context;
362
        this.context = context;
194
    }
363
    }
195
    
364
    
196
    @Override
-
 
197
    public void setServletResponse(HttpServletResponse response) {
365
    public void setServletResponse(HttpServletResponse response) {
198
        this.response = response;
366
        this.response = response;
199
    }
367
    }
200
 
368
 
201
    public String getServletContextPath() {
369
    public String getServletContextPath() {
Line 248... Line 416...
248
 
416
 
249
 
417
 
250
	public void setDateselector(String dateselector) {
418
	public void setDateselector(String dateselector) {
251
		this.dateselector = dateselector;
419
		this.dateselector = dateselector;
252
	}
420
	}
-
 
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
    }
253
	
473
	
254
	
474
	
255
 
475
 
256
}
476
}
257
477