Subversion Repositories SmartDukaan

Rev

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

Rev 7563 Rev 8707
Line 1... Line 1...
1
package in.shop2020.support.controllers;
1
package in.shop2020.support.controllers;
2
 
2
 
-
 
3
import in.shop2020.model.v1.order.Order;
-
 
4
import in.shop2020.model.v1.order.TransactionServiceException;
3
import in.shop2020.model.v1.user.Coupon;
5
import in.shop2020.model.v1.user.Coupon;
-
 
6
import in.shop2020.model.v1.user.CouponCategory;
4
import in.shop2020.model.v1.user.PromotionException;
7
import in.shop2020.model.v1.user.PromotionException;
5
import in.shop2020.support.utils.ReportsUtils;
8
import in.shop2020.support.utils.ReportsUtils;
6
import in.shop2020.thrift.clients.PromotionClient;
9
import in.shop2020.thrift.clients.PromotionClient;
-
 
10
import in.shop2020.thrift.clients.TransactionClient;
7
 
11
 
8
import java.text.DateFormat;
12
import java.text.DateFormat;
9
import java.text.SimpleDateFormat;
13
import java.text.SimpleDateFormat;
10
import java.util.ArrayList;
14
import java.util.ArrayList;
11
import java.util.Calendar;
15
import java.util.Calendar;
Line 52... Line 56...
52
    private ServletContext context;
56
    private ServletContext context;
53
    
57
    
54
    private PromotionClient psc;
58
    private PromotionClient psc;
55
    private in.shop2020.model.v1.user.PromotionService.Client pClient;
59
    private in.shop2020.model.v1.user.PromotionService.Client pClient;
56
    
60
    
-
 
61
    private TransactionClient tsc;
-
 
62
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
-
 
63
    
57
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
64
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
58
    
65
    
59
 
66
 
60
 
67
 
61
    private String discountType;
68
    private String discountType;
Line 75... Line 82...
75
    private Long endMinute;
82
    private Long endMinute;
76
 
83
 
77
    private String couponCode;
84
    private String couponCode;
78
 
85
 
79
    private int isCod;
86
    private int isCod;
-
 
87
    private long couponcategory;
-
 
88
    private Long orderId;
80
	
89
	
81
    public CouponGvsController(){
90
    public CouponGvsController(){
82
	    try {
91
	    try {
83
            psc = new PromotionClient();
92
            psc = new PromotionClient();
84
            pClient = psc.getClient();
93
            pClient = psc.getClient();
-
 
94
            tsc = new TransactionClient();
-
 
95
            tClient = tsc.getClient();
85
        } catch (Exception e) {
96
        } catch (Exception e) {
86
            logger.error("Error connecting to promotion service", e);
97
            logger.error("Error connecting to promotion/transaction service", e);
87
        }
98
        }
88
	}
99
	}
89
		
100
		
90
        
101
        
91
	public String index() {
102
	public String index() {
Line 102... Line 113...
102
		}
113
		}
103
		//Coupon would be valid till 2 months of creation.
114
		//Coupon would be valid till 2 months of creation.
104
		Calendar cal = Calendar.getInstance();
115
		Calendar cal = Calendar.getInstance();
105
		cal.add(Calendar.DATE, 60);
116
		cal.add(Calendar.DATE, 60);
106
		Date endOn = cal.getTime();
117
		Date endOn = cal.getTime();
-
 
118
		
107
		String arguments = "{";
119
		String arguments = "{";
-
 
120
 
108
		if(!(StringUtils.isEmpty(userEmail) || discount == null || discount <= 0) ) {
121
		if(!(StringUtils.isEmpty(userEmail) || discount == null || discount <= 0) ) {
109
			try {
122
			if(checkConditionsForRefund()){
110
			    
123
				try {
111
			    arguments = arguments + "\"endOn\":" + String.valueOf(endOn.getTime());
124
				    
112
			    
125
				    arguments = arguments + "\"endOn\":" + String.valueOf(endOn.getTime());
113
			    if(userEmail.equals("*")) {
126
				    
114
			        arguments = arguments + ", \"emails\":\"*\"";
127
				    if(userEmail.equals("*")) {
115
			    } else {
128
				        arguments = arguments + ", \"emails\":\"*\"";
116
			        String[] emails = userEmail.split(",");
129
				    } else {
117
			        arguments = arguments + ", \"emails\":[";
130
				        String[] emails = userEmail.split(",");
118
			        int i = 0;
131
				        arguments = arguments + ", \"emails\":[";
119
			        for (String email : emails) {
132
				        int i = 0;
120
			            if(i == 0) {
133
				        for (String email : emails) {
121
			                arguments = arguments + "\"" + email.trim() + "\"";
134
				            if(i == 0) {
122
			                i = 1;
135
				                arguments = arguments + "\"" + email.trim() + "\"";
123
			            } else {
136
				                i = 1;
124
			                arguments = arguments + ",\"" + email.trim() + "\"";
137
				            } else {
125
			            }
138
				                arguments = arguments + ",\"" + email.trim() + "\"";
126
			        }
139
				            }
127
			        arguments = arguments + "]";
140
				        }
128
			    }
141
				        arguments = arguments + "]";
129
			    
142
				    }
130
			    arguments = arguments + ", \"couponType\":\"" + couponType + "\"";
143
				    
131
			    
144
				    arguments = arguments + ", \"couponType\":\"" + couponType + "\"";
132
			    arguments = arguments + ", \"discountType\":\"" + discountType + "\"";
145
				    
133
			    
146
				    arguments = arguments + ", \"discountType\":\"" + discountType + "\"";
134
			    arguments = arguments + ", \"discount\":" + discount.toString();
147
				    
135
			    
148
				    arguments = arguments + ", \"discount\":" + discount.toString();
136
			    if(userLimit == null || userLimit < 1) {
149
				    
137
			        arguments = arguments + ", \"usage_limit_for_user\":1";
150
				    if(orderId != null) {
138
			    } else {
151
				    	arguments = arguments + ", \"orderId\":" + orderId.toString();
139
			        arguments = arguments + ", \"usage_limit_for_user\":" + userLimit.toString();
152
				    }
140
			    }
153
				    
141
			    
154
				    if(userLimit == null || userLimit < 1) {
142
			    if(globalLimit == null || globalLimit < 1) {
155
				        arguments = arguments + ", \"usage_limit_for_user\":1";
143
			        //Do not add this option
156
				    } else {
144
                    ;
157
				        arguments = arguments + ", \"usage_limit_for_user\":" + userLimit.toString();
145
                } else {
158
				    }
146
                    arguments = arguments + ", \"globalLimit\":" + globalLimit.toString();
159
				    
147
                }
160
				    if(globalLimit == null || globalLimit < 1) {
148
			    
161
				        //Do not add this option
149
			    if(maxDiscount == null || maxDiscount < 1) {
162
	                    ;
150
                    //Do not add this option
163
	                } else {
151
			        ;
164
	                    arguments = arguments + ", \"globalLimit\":" + globalLimit.toString();
152
                } else {
165
	                }
153
                    arguments = arguments + ", \"maxDiscount\":" + maxDiscount.toString();
166
				    
154
                }
167
				    if(maxDiscount == null || maxDiscount < 1) {
155
			    
168
	                    //Do not add this option
156
			    if(minDiscountableVal == null || minDiscountableVal < 1) {
169
				        ;
157
                    //Do not add this option
170
	                } else {
158
                    ;
171
	                    arguments = arguments + ", \"maxDiscount\":" + maxDiscount.toString();
159
                } else {
172
	                }
160
                    arguments = arguments + ", \"minDiscountableVal\":" + minDiscountableVal.toString();
173
				    
161
                }
174
				    if(minDiscountableVal == null || minDiscountableVal < 1) {
162
			    
175
	                    //Do not add this option
163
			    if(startHour == null || startHour < 1 || startHour > 23 || startHour > endHour) {
176
	                    ;
164
                    //Do not add this option
177
	                } else {
165
                    ;
178
	                    arguments = arguments + ", \"minDiscountableVal\":" + minDiscountableVal.toString();
166
                } else {
179
	                }
167
                    arguments = arguments + ", \"startHour\":" + startHour.toString();
180
				    
168
                }
181
				    if(startHour == null || startHour < 1 || startHour > 23 || startHour > endHour) {
169
			    
182
	                    //Do not add this option
170
			    if(startMinute == null || startMinute < 1 || startMinute < 59) {
183
	                    ;
171
                    //Do not add this option
184
	                } else {
172
                    ;
185
	                    arguments = arguments + ", \"startHour\":" + startHour.toString();
173
                } else {
186
	                }
174
                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
187
				    
175
                }
188
				    if(startMinute == null || startMinute < 1 || startMinute < 59) {
176
			    
189
	                    //Do not add this option
177
			    if(endHour == null || endHour < 1 || endHour > 23) {
190
	                    ;
178
                    //Do not add this option
191
	                } else {
179
                    ;
192
	                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
180
                } else {
193
	                }
181
                    arguments = arguments + ", \"endHour\":" + endHour.toString();
194
				    
182
                }
195
				    if(endHour == null || endHour < 1 || endHour > 23) {
183
			    
196
	                    //Do not add this option
184
			    if(endMinute == null || endMinute < 1 || endMinute < 59) {
197
	                    ;
185
                    //Do not add this option
198
	                } else {
186
                    ;
199
	                    arguments = arguments + ", \"endHour\":" + endHour.toString();
187
                } else {
200
	                }
188
                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
201
				    
189
                }
202
				    if(endMinute == null || endMinute < 1 || endMinute < 59) {
190
			    
203
	                    //Do not add this option
191
			    if(isCod == 1) {
204
	                    ;
192
			        arguments = arguments + ", \"isCod\":True}";
205
	                } else {
193
			    } else {
206
	                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
194
			        arguments = arguments + ", \"isCod\":False}";
207
	                }
195
			    }
208
				    
196
			    
209
				    if(isCod == 1) {
197
			    if(couponCode == null) {
210
				        arguments = arguments + ", \"isCod\":True}";
198
			        couponCode = "";
211
				    } else {
199
			    }
212
				        arguments = arguments + ", \"isCod\":False}";
200
			    
213
				    }
201
				coupon = pClient.createCoupon(type, couponCode, arguments, false, null);
214
				    
202
			} catch (Exception e){
215
				    if(couponCode == null) {
203
				return "authsuccess";
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
				}
204
			}
235
			}
205
		}
236
		}
206
		return "authsuccess";	
237
		return "authsuccess";	
207
	}
238
	}
208
 
239
 
Line 210... Line 241...
210
	@Override
241
	@Override
211
	public void setServletContext(ServletContext context) {
242
	public void setServletContext(ServletContext context) {
212
		this.context= context;
243
		this.context= context;
213
	}
244
	}
214
 
245
 
-
 
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
	}
215
 
263
 
216
	@Override
264
	@Override
217
	public void setServletRequest(HttpServletRequest req) {
265
	public void setServletRequest(HttpServletRequest req) {
218
	       this.request = req;
266
	       this.request = req;
219
	        this.session = req.getSession();
267
	        this.session = req.getSession();
Line 444... Line 492...
444
 
492
 
445
 
493
 
446
    public void setIsCod(int isCod) {
494
    public void setIsCod(int isCod) {
447
        this.isCod = isCod;
495
        this.isCod = isCod;
448
    }
496
    }
-
 
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
	}
449
}
517
}