Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
6497 amit.gupta 1
package in.shop2020.support.controllers;
2
 
8707 manish.sha 3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.TransactionServiceException;
6497 amit.gupta 5
import in.shop2020.model.v1.user.Coupon;
8707 manish.sha 6
import in.shop2020.model.v1.user.CouponCategory;
6497 amit.gupta 7
import in.shop2020.model.v1.user.PromotionException;
8
import in.shop2020.support.utils.ReportsUtils;
9
import in.shop2020.thrift.clients.PromotionClient;
8707 manish.sha 10
import in.shop2020.thrift.clients.TransactionClient;
6497 amit.gupta 11
 
12
import java.text.DateFormat;
13
import java.text.SimpleDateFormat;
14
import java.util.ArrayList;
15
import java.util.Calendar;
16
import java.util.Date;
6679 anupam.sin 17
import java.util.HashMap;
6497 amit.gupta 18
import java.util.List;
6679 anupam.sin 19
import java.util.Map;
6497 amit.gupta 20
 
21
import javax.servlet.ServletContext;
22
import javax.servlet.http.HttpServletRequest;
23
import javax.servlet.http.HttpServletResponse;
24
import javax.servlet.http.HttpSession;
25
 
6500 amit.gupta 26
import org.apache.commons.lang.StringUtils;
6497 amit.gupta 27
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRefs;
29
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Results;
31
import org.apache.struts2.interceptor.ServletRequestAware;
32
import org.apache.struts2.util.ServletContextAware;
33
import org.apache.thrift.TException;
34
import org.json.JSONException;
35
import org.json.JSONObject;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38
 
39
 
40
@InterceptorRefs({
41
    @InterceptorRef("defaultStack"),
42
    @InterceptorRef("login")
43
})
44
 
45
@Results({
46
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"}),
47
	@Result(name="redirect", type="redirectAction", location = "coupon-gvs",  params={"statusCode", "301"})
48
})
49
public class CouponGvsController implements ServletRequestAware, ServletContextAware {
50
 
51
    private static Logger logger = LoggerFactory.getLogger(CouponGvsController.class);
52
 
53
    private HttpServletRequest request;
54
    private HttpServletResponse response;
55
    private HttpSession session;
56
    private ServletContext context;
57
 
58
    private PromotionClient psc;
59
    private in.shop2020.model.v1.user.PromotionService.Client pClient;
60
 
8707 manish.sha 61
    private TransactionClient tsc;
62
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
63
 
6497 amit.gupta 64
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
65
 
66
 
67
 
6679 anupam.sin 68
    private String discountType;
69
    private Long discount;
70
    private String couponType;
6497 amit.gupta 71
	private String coupon;
72
    private long type;
73
    private String userEmail;
6679 anupam.sin 74
 
75
    private Long userLimit;
76
    private Long globalLimit;
77
    private Long maxDiscount;
78
    private Long minDiscountableVal;
79
    private Long startHour;
80
    private Long startMinute;
81
    private Long endHour;
82
    private Long endMinute;
6730 anupam.sin 83
 
84
    private String couponCode;
7563 anupam.sin 85
 
86
    private int isCod;
8707 manish.sha 87
    private long couponcategory;
88
    private Long orderId;
6497 amit.gupta 89
 
90
    public CouponGvsController(){
91
	    try {
92
            psc = new PromotionClient();
93
            pClient = psc.getClient();
8707 manish.sha 94
            tsc = new TransactionClient();
95
            tClient = tsc.getClient();
6497 amit.gupta 96
        } catch (Exception e) {
8707 manish.sha 97
            logger.error("Error connecting to promotion/transaction service", e);
6497 amit.gupta 98
        }
99
	}
100
 
101
 
102
	public String index() {
103
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
104
            return "authfail";
105
        }
106
        return "authsuccess";
107
 
108
	}
109
 
110
	public String create(){
111
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
112
			return "authfail";
113
		}
6679 anupam.sin 114
		//Coupon would be valid till 2 months of creation.
6497 amit.gupta 115
		Calendar cal = Calendar.getInstance();
116
		cal.add(Calendar.DATE, 60);
117
		Date endOn = cal.getTime();
8707 manish.sha 118
 
6679 anupam.sin 119
		String arguments = "{";
8707 manish.sha 120
 
6679 anupam.sin 121
		if(!(StringUtils.isEmpty(userEmail) || discount == null || discount <= 0) ) {
8707 manish.sha 122
			if(checkConditionsForRefund()){
123
				try {
124
 
125
				    arguments = arguments + "\"endOn\":" + String.valueOf(endOn.getTime());
126
 
127
				    if(userEmail.equals("*")) {
128
				        arguments = arguments + ", \"emails\":\"*\"";
129
				    } else {
130
				        String[] emails = userEmail.split(",");
131
				        arguments = arguments + ", \"emails\":[";
132
				        int i = 0;
133
				        for (String email : emails) {
134
				            if(i == 0) {
135
				                arguments = arguments + "\"" + email.trim() + "\"";
136
				                i = 1;
137
				            } else {
138
				                arguments = arguments + ",\"" + email.trim() + "\"";
139
				            }
140
				        }
141
				        arguments = arguments + "]";
142
				    }
143
 
144
				    arguments = arguments + ", \"couponType\":\"" + couponType + "\"";
145
 
146
				    arguments = arguments + ", \"discountType\":\"" + discountType + "\"";
147
 
148
				    arguments = arguments + ", \"discount\":" + discount.toString();
149
 
150
				    if(orderId != null) {
151
				    	arguments = arguments + ", \"orderId\":" + orderId.toString();
152
				    }
153
 
154
				    if(userLimit == null || userLimit < 1) {
155
				        arguments = arguments + ", \"usage_limit_for_user\":1";
156
				    } else {
157
				        arguments = arguments + ", \"usage_limit_for_user\":" + userLimit.toString();
158
				    }
159
 
160
				    if(globalLimit == null || globalLimit < 1) {
161
				        //Do not add this option
162
	                    ;
163
	                } else {
164
	                    arguments = arguments + ", \"globalLimit\":" + globalLimit.toString();
165
	                }
166
 
167
				    if(maxDiscount == null || maxDiscount < 1) {
168
	                    //Do not add this option
169
				        ;
170
	                } else {
171
	                    arguments = arguments + ", \"maxDiscount\":" + maxDiscount.toString();
172
	                }
173
 
174
				    if(minDiscountableVal == null || minDiscountableVal < 1) {
175
	                    //Do not add this option
176
	                    ;
177
	                } else {
178
	                    arguments = arguments + ", \"minDiscountableVal\":" + minDiscountableVal.toString();
179
	                }
180
 
181
				    if(startHour == null || startHour < 1 || startHour > 23 || startHour > endHour) {
182
	                    //Do not add this option
183
	                    ;
184
	                } else {
185
	                    arguments = arguments + ", \"startHour\":" + startHour.toString();
186
	                }
187
 
188
				    if(startMinute == null || startMinute < 1 || startMinute < 59) {
189
	                    //Do not add this option
190
	                    ;
191
	                } else {
192
	                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
193
	                }
194
 
195
				    if(endHour == null || endHour < 1 || endHour > 23) {
196
	                    //Do not add this option
197
	                    ;
198
	                } else {
199
	                    arguments = arguments + ", \"endHour\":" + endHour.toString();
200
	                }
201
 
202
				    if(endMinute == null || endMinute < 1 || endMinute < 59) {
203
	                    //Do not add this option
204
	                    ;
205
	                } else {
206
	                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
207
	                }
208
 
209
				    if(isCod == 1) {
210
				        arguments = arguments + ", \"isCod\":True}";
211
				    } else {
212
				        arguments = arguments + ", \"isCod\":False}";
213
				    }
214
 
215
				    if(couponCode == null) {
216
				        couponCode = "";
217
				    }
218
 
219
				    String prefix="";
220
				    switch((int)couponcategory){
221
				    case 0 : prefix = "CUS";
222
				    		 break;
223
				    case 1 : prefix = "MKT";
224
				    		 break;
225
				    case 2 : prefix = "RFD";
226
		    		 		 break;
227
		    		default : break;
228
				    }
229
 
230
				    coupon = pClient.createCoupon(type, couponcategory, couponCode, arguments, false, prefix);
231
 
232
				} catch (Exception e){
233
					return "authsuccess";
234
				}
6500 amit.gupta 235
			}
6497 amit.gupta 236
		}
237
		return "authsuccess";	
238
	}
239
 
240
 
241
	@Override
242
	public void setServletContext(ServletContext context) {
243
		this.context= context;
244
	}
245
 
8707 manish.sha 246
	public boolean checkConditionsForRefund(){
247
		Order order =null;
248
		if(orderId!=null){
249
			try {
250
				order = tClient.getOrder(orderId);
251
			} catch (Exception e1) {
252
				logger.error("Error getting order object for orderId: "+orderId, e1);
253
			}
254
			if(order!=null && discount<=order.getTotal_amount()){
255
				return true;
256
			}
257
		}
258
		else if(couponcategory==CouponCategory.CUSTOMER_SATISFACTION.getValue() || couponcategory==CouponCategory.MARKETING.getValue()){
259
			return true;
260
		}
261
		return false;
262
	}
6497 amit.gupta 263
 
264
	@Override
265
	public void setServletRequest(HttpServletRequest req) {
266
	       this.request = req;
267
	        this.session = req.getSession();
268
	}
269
 
270
	//Crea
271
	public String getCoupon() {
272
		return coupon;
273
 	}
274
 
275
 
276
	public void setCoupon(String coupon) {
277
		this.coupon = coupon;
278
	}
279
 
280
 
281
	public long getType() {
282
		return type;
283
	}
284
 
285
 
286
	public void setType(long type) {
287
		this.type = type;
288
	}
289
 
290
	public String getUserEmail() {
291
		return userEmail;
292
	}
293
 
294
	public void setUserEmail(String userEmail) {
295
		this.userEmail = userEmail;
296
	}
297
 
298
	public List<Coupon> getCoupons() {
299
		try {
300
			return pClient.getActiveCodes(27l);
301
		} catch (PromotionException e) {
302
			// TODO Auto-generated catch block
303
			e.printStackTrace();
304
		} catch (TException e) {
305
			// TODO Auto-generated catch block
306
			e.printStackTrace();
307
		}
308
		return null;
309
	}
310
 
311
	public List<String> getArguments(Coupon coupon){
312
		List<String> result = new ArrayList<String>();
313
		try {
314
			JSONObject obj = new JSONObject(coupon.getArguments());
6727 anupam.sin 315
			result.add(obj.getString("emails"));
6730 anupam.sin 316
			try {
317
    			if(obj.getString("discountType").equals("absolute")) {
318
    			    result.add(obj.getString("discount"));
319
    			} else {
320
    			    result.add(obj.getString("discount") + " %");
321
    			}
322
    		} catch (JSONException e){
323
    		    result.add(obj.getString("discount"));
324
    		}
325
 
326
    		try {
327
    			result.add(obj.getString("couponType"));
328
    		} catch (JSONException e){
329
    		    result.add("Physical");
330
    		}
331
 
6497 amit.gupta 332
		} catch (JSONException e) {
6730 anupam.sin 333
			result.add("ERROR");
334
			result.add("ERROR");
335
			result.add("ERROR");
6497 amit.gupta 336
		}
337
		return result;
338
	}
339
 
340
	public void setId(String id) {
341
		this.coupon = id;
342
	}
343
 
344
	public String destroy(){
345
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath().split("/")[1])) {
346
			return "athfail";
347
		}
348
		try {
349
			pClient.deleteCoupon(coupon);
350
		} catch (PromotionException e) {
351
			// TODO Auto-generated catch block
352
			e.printStackTrace();
353
		} catch (TException e) {
354
			// TODO Auto-generated catch block
355
			e.printStackTrace();
356
		}
357
		return "redirect"; 
358
	}
359
	public String show(){
360
		return destroy(); 
361
	}
6574 amit.gupta 362
 
363
 
364
    public String getServletContextPath() {
365
        return context.getContextPath();
366
    }
6679 anupam.sin 367
 
368
 
369
    public String getDiscountType() {
370
        return discountType;
371
    }
372
 
373
 
374
    public void setDiscountType(String discountType) {
375
        this.discountType = discountType;
376
    }
377
 
378
 
379
    public Long getDiscount() {
380
        return discount;
381
    }
382
 
383
 
384
    public void setDiscount(Long discount) {
385
        this.discount = discount;
386
    }
387
 
388
 
389
    public String getCouponType() {
390
        return couponType;
391
    }
392
 
393
 
394
    public void setCouponType(String couponType) {
395
        this.couponType = couponType;
396
    }
397
 
398
 
399
    public Long getUserLimit() {
400
        return userLimit;
401
    }
402
 
403
 
404
    public void setUserLimit(Long userLimit) {
405
        this.userLimit = userLimit;
406
    }
407
 
408
 
409
    public Long getGlobalLimit() {
410
        return globalLimit;
411
    }
412
 
413
 
414
    public void setGlobalLimit(Long globalLimit) {
415
        this.globalLimit = globalLimit;
416
    }
417
 
418
 
419
    public Long getMaxDiscount() {
420
        return maxDiscount;
421
    }
422
 
423
 
424
    public void setMaxDiscount(Long maxDiscount) {
425
        this.maxDiscount = maxDiscount;
426
    }
427
 
428
 
429
    public Long getMinDiscountableVal() {
430
        return minDiscountableVal;
431
    }
432
 
433
 
434
    public void setMinDiscountableVal(Long minDiscountableVal) {
435
        this.minDiscountableVal = minDiscountableVal;
436
    }
437
 
438
 
439
    public Long getStartHour() {
440
        return startHour;
441
    }
442
 
443
 
444
    public void setStartHour(Long startHour) {
445
        this.startHour = startHour;
446
    }
447
 
448
 
449
    public Long getStartMinute() {
450
        return startMinute;
451
    }
452
 
453
 
454
    public void setStartMinute(Long startMinute) {
455
        this.startMinute = startMinute;
456
    }
457
 
458
 
459
    public Long getEndHour() {
460
        return endHour;
461
    }
462
 
463
 
464
    public void setEndHour(Long endHour) {
465
        this.endHour = endHour;
466
    }
467
 
468
 
469
    public Long getEndMinute() {
470
        return endMinute;
471
    }
472
 
473
 
474
    public void setEndMinute(Long endMinute) {
475
        this.endMinute = endMinute;
476
    }
6730 anupam.sin 477
 
478
 
479
    public String getCouponCode() {
480
        return couponCode;
481
    }
482
 
483
 
484
    public void setCouponCode(String couponCode) {
485
        this.couponCode = couponCode;
486
    }
7563 anupam.sin 487
 
488
 
489
    public int getIsCod() {
490
        return isCod;
491
    }
492
 
493
 
494
    public void setIsCod(int isCod) {
495
        this.isCod = isCod;
496
    }
8707 manish.sha 497
 
498
 
499
	public long getCouponcategory() {
500
		return couponcategory;
501
	}
502
 
503
 
504
	public void setCouponcategory(long couponcategory) {
505
		this.couponcategory = couponcategory;
506
	}
507
 
508
 
509
	public Long getOrderId() {
510
		return orderId;
511
	}
512
 
513
 
514
	public void setOrderId(Long orderId) {
515
		this.orderId = orderId;
516
	}
6497 amit.gupta 517
}