Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
1429 varun.gupt 4
import in.shop2020.model.v1.catalog.Item;
507 rajveer 5
import in.shop2020.model.v1.user.Address;
6
import in.shop2020.model.v1.user.AddressType;
841 vikas 7
import in.shop2020.model.v1.user.Cart;
1429 varun.gupt 8
import in.shop2020.model.v1.user.Line;
1774 varun.gupt 9
import in.shop2020.model.v1.user.User;
572 chandransh 10
import in.shop2020.model.v1.user.UserContextService;
2137 chandransh 11
import in.shop2020.serving.utils.FormattingUtils;
839 vikas 12
import in.shop2020.serving.utils.Utils;
1429 varun.gupt 13
import in.shop2020.thrift.clients.CatalogServiceClient;
3063 chandransh 14
import in.shop2020.thrift.clients.LogisticsServiceClient;
507 rajveer 15
import in.shop2020.thrift.clients.UserContextServiceClient;
2511 vikas 16
import in.shop2020.utils.DataLogger;
507 rajveer 17
 
2419 vikas 18
import java.util.ArrayList;
19
import java.util.Collection;
3063 chandransh 20
import java.util.Enumeration;
2419 vikas 21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
3063 chandransh 24
import java.util.Properties;
25
import java.util.ResourceBundle;
507 rajveer 26
 
3063 chandransh 27
import net.tanesha.recaptcha.ReCaptcha;
28
import net.tanesha.recaptcha.ReCaptchaFactory;
29
 
832 rajveer 30
import org.apache.log4j.Logger;
822 vikas 31
import org.apache.struts2.convention.annotation.InterceptorRef;
32
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 33
import org.apache.struts2.convention.annotation.Result;
34
import org.apache.struts2.convention.annotation.Results;
1429 varun.gupt 35
import org.apache.velocity.VelocityContext;
507 rajveer 36
 
822 vikas 37
@InterceptorRefs({
38
    @InterceptorRef("myDefault"),
39
    @InterceptorRef("login")
40
})
41
 
637 rajveer 42
@Results({
43
    @Result(name="redirect", type="redirectAction", 
822 vikas 44
    		params = {"actionName" , "shipping"})
637 rajveer 45
})
822 vikas 46
public class ShippingController extends BaseController{
507 rajveer 47
 
48
	private static final long serialVersionUID = 1L;
832 rajveer 49
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 50
 
3063 chandransh 51
	private static final ResourceBundle resource = ResourceBundle.getBundle(ShippingController.class.getName());
52
	private static final double COD_CUTOFF_AMOUNT = Double.parseDouble(resource.getString("cod_cutoff_amount")); 
53
	private static final String RECAPTCHA_PUBLIC_KEY = resource.getString("recaptcha_public_key");
54
 
517 rajveer 55
	private long addressId = 0;
572 chandransh 56
	private String errorMsg = "";
507 rajveer 57
 
822 vikas 58
	private String name;
59
	private String line1;
60
	private String line2;
61
	private String city;
839 vikas 62
	private String state = "";
822 vikas 63
	private String pincode;
64
	private String phone;
65
	private String country;
66
 
3063 chandransh 67
	private boolean showCodOption = true;
68
 
507 rajveer 69
	public ShippingController(){
70
		super();
71
	}
72
 
73
	 // GET /shipping
637 rajveer 74
	 public String index() {
555 chandransh 75
    	long userId = userinfo.getUserId();
692 chandransh 76
    	long cartId = userinfo.getCartId();
572 chandransh 77
    	try {
78
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
692 chandransh 79
			userClient.checkOut(cartId);
841 vikas 80
			Cart cart = userClient.getCart(cartId);
81
			long defaultAddressId = cart.getAddressId();
82
			if (defaultAddressId == 0) {
83
			    defaultAddressId = userClient.getDefaultAddressId(userId);
84
			}
637 rajveer 85
			log.info("The default address id of this user is: " + defaultAddressId);
86
			if(defaultAddressId > 0)
692 chandransh 87
				userClient.addAddressToCart(cartId, defaultAddressId);
1466 ankur.sing 88
			errorMsg = userClient.validateCart(cartId);
572 chandransh 89
 
90
		} catch (Exception e) {
2949 chandransh 91
			// This exception can be ignored for showing the shipping page. Not so
572 chandransh 92
			// innocent when this occurs at the time of checkout or when the
93
			// user is proceeding to pay.
2949 chandransh 94
			log.error("Unable to get all the data required for displaying the shipping page", e);
572 chandransh 95
		}
712 rajveer 96
    	// in case this page is redirected from payment failure
822 vikas 97
 
839 vikas 98
		Collection<String> actionErrors = getActionErrors();
99
		if(actionErrors != null && !actionErrors.isEmpty()){
100
			for (String str : actionErrors) {
101
			    errorMsg += "<BR/>" + str;
102
			}
712 rajveer 103
		}
2419 vikas 104
        DataLogger.logData(EventType.SHIPPINIG_ACCESS, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 105
                Long.toString(cartId));
822 vikas 106
 
637 rajveer 107
    	return "index";
507 rajveer 108
	 }
109
 
110
	// POST /entity
637 rajveer 111
	public String create(){
555 chandransh 112
		UserContextServiceClient userContextServiceClient = null;
113
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
114
 
637 rajveer 115
    	String action = this.request.getParameter("action");
517 rajveer 116
		if(action == null){
637 rajveer 117
			return "failure";
517 rajveer 118
		}
507 rajveer 119
 
555 chandransh 120
		try {
121
			userContextServiceClient = new UserContextServiceClient();
122
			userClient = userContextServiceClient.getClient();
517 rajveer 123
 
637 rajveer 124
			if(action != null){
125
				if(action.equals("add")){
839 vikas 126
					boolean invalidInput = false;
127
					if (!Utils.validatePin(this.pincode)) {
128
						addActionError("Invalid pincode.");
129
						invalidInput = true;
130
					}
131
					if (!Utils.validatePhone(this.phone)) {
132
						addActionError("Invalid phone number.");
133
						invalidInput = true;
134
					}
135
					if (this.line1.isEmpty()) {
136
						addActionError("Address line1 is empty.");
137
						invalidInput = true;
138
					}
139
					if (this.city.isEmpty()) {
140
						addActionError("City name is empty.");
141
						invalidInput = true;
142
					}
143
					if (this.state.isEmpty()) {
144
						addActionError("State name is empty.");
145
						invalidInput = true;
146
					}
147
					if (!invalidInput) {
148
						Address address = new Address();
149
						address.setName(this.name);
150
						address.setLine1(this.line1);
151
						address.setLine2(this.line2);
152
						address.setCity(this.city);
153
						address.setState(this.state);
154
						address.setPin(this.pincode);
155
						address.setPhone(this.phone);
156
						address.setCountry(this.country);
157
						address.setEnabled(true);
158
						address.setType(AddressType.HOME);
1446 varun.gupt 159
						long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
160
						userClient.addAddressToCart(userinfo.getCartId(), addressId);
839 vikas 161
						addActionMessage("Address added successfully.");
2419 vikas 162
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS, session.getId(), userinfo.getUserId(), userinfo.getEmail(), address.getName(),
2157 vikas 163
                                        address.getCity(), address.getPhone(), address.getPin());
839 vikas 164
					}
165
					return "redirect";				
517 rajveer 166
				}
167
 
1446 varun.gupt 168
				if(action.equals("change"))	{
841 vikas 169
					addressId = Long.parseLong(this.request.getParameter("addressid"));
637 rajveer 170
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
1446 varun.gupt 171
 
1466 ankur.sing 172
					errorMsg = userClient.validateCart(userinfo.getCartId());
2419 vikas 173
                    DataLogger.logData(EventType.SHIPPINIG_ADD_CHANGE, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 174
                            Long.toString(userinfo.getCartId()), Long.toString(addressId));
1446 varun.gupt 175
					return "index";
637 rajveer 176
				}
517 rajveer 177
			}
637 rajveer 178
		} catch (Exception e) {
179
			return "failure";
517 rajveer 180
		}
637 rajveer 181
		return null;
507 rajveer 182
	}
183
 
1429 varun.gupt 184
	public String getShippingHeaderSnippet() {
185
		String htmlString = "";
186
		VelocityContext context = new VelocityContext();
187
		String templateFile = "templates/shippingheader.vm";
188
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
189
		return htmlString;
507 rajveer 190
	}
191
 
1429 varun.gupt 192
	public String getShippingDetailsSnippet() {
193
 
194
		long cartId = userinfo.getCartId();
195
		String htmlString = "";
196
		VelocityContext context = new VelocityContext();
197
		String templateFile = "templates/shippingdetails.vm";
198
		List<Map<String,String>> items = null;
199
		double totalamount= 0.0;
200
		List<Address> addresses = null;
201
		long defaultAddressId = 0;
1774 varun.gupt 202
		String fullName = "";
203
		String phoneNumber = "";
1429 varun.gupt 204
 
205
		CatalogServiceClient catalogServiceClient  = null;
206
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
207
		UserContextServiceClient userContextServiceClient = null;
208
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
209
 
2137 chandransh 210
		FormattingUtils formattingUtils = new FormattingUtils();
211
 
1429 varun.gupt 212
		try {
213
			catalogServiceClient = new CatalogServiceClient();
214
			catalogClient = catalogServiceClient.getClient();
215
			userContextServiceClient = new UserContextServiceClient();
216
			userClient = userContextServiceClient.getClient();
217
 
218
			Cart cart = userClient.getCart(cartId);
219
			List<Line> lineItems = cart.getLines();
220
 
221
			if( ! lineItems.isEmpty())	{
222
				items = new ArrayList<Map<String,String>>();
223
 
224
				for (Line line : lineItems) {
225
					Map<String, String> itemdetail = new HashMap<String, String>();
226
					Item item = catalogClient.getItem(line.getItemId());
227
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
228
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
229
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
230
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
231
 
232
					itemdetail.put("ITEM_NAME", itemName);
233
					itemdetail.put("ITEM_ID", line.getItemId()+"");
234
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 235
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
236
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
3063 chandransh 237
					if(item.getSellingPrice() > COD_CUTOFF_AMOUNT)
238
					    showCodOption = false;
2137 chandransh 239
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 240
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
2137 chandransh 241
					items.add(itemdetail);
1429 varun.gupt 242
				}
243
			}
2137 chandransh 244
			totalamount = cart.getTotalPrice();
1429 varun.gupt 245
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 246
			User user = userClient.getUserById(cart.getUserId());
247
 
248
			fullName = user.getName();
249
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 250
 
251
			if(cart.isSetAddressId())	{
252
				defaultAddressId = cart.getAddressId();
253
			} else	{
254
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
255
			}
3063 chandransh 256
 
257
	        for(Address address : addresses){
258
	            if(address.getId() == defaultAddressId){
259
	                try {
260
	                    LogisticsServiceClient lClient = new LogisticsServiceClient();
261
	                    showCodOption = showCodOption && lClient.getClient().isCodAllowed(address.getPin());
262
	                } catch(Exception e) {
263
	                    log.error("Error while checking if COD is available for: " + showCodOption);
264
	                    showCodOption = false; //Defaulting to defensive behaviour
265
	                }
266
	            }
267
	        }
1981 varun.gupt 268
 
269
			String couponCode = cart.getCouponCode();
270
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 271
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 272
 
1429 varun.gupt 273
		} catch (Exception e)	{
2949 chandransh 274
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 275
		}
1774 varun.gupt 276
		System.out.println(fullName + " | " + phoneNumber);
1981 varun.gupt 277
 
1774 varun.gupt 278
		context.put("fullname", fullName);
279
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 280
 
281
		context.put("items", items);
2137 chandransh 282
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 283
		context.put("addresses", addresses);
284
		context.put("defaultAddressId", defaultAddressId+"");
285
		context.put("errorMsg", errorMsg);
286
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
287
		return htmlString;
507 rajveer 288
	}
289
 
517 rajveer 290
	public long getAddressId(){
291
		return this.addressId;
292
	}
572 chandransh 293
 
294
	public String getErrorMsg(){
295
		return this.errorMsg;
296
	}
822 vikas 297
 
298
	public String getName() {
299
		return name;
300
	}
301
 
302
	public void setName(String name) {
303
		this.name = name;
304
	}
305
 
306
	public String getLine1() {
307
		return line1;
308
	}
309
 
310
	public void setLine1(String line1) {
311
		this.line1 = line1;
312
	}
313
 
314
	public String getLine2() {
315
		return line2;
316
	}
317
 
318
	public void setLine2(String line2) {
319
		this.line2 = line2;
320
	}
321
 
322
	public String getCity() {
323
		return city;
324
	}
325
 
326
	public void setCity(String city) {
327
		this.city = city;
328
	}
329
 
330
	public String getState() {
331
		return state;
332
	}
333
 
334
	public void setState(String state) {
335
		this.state = state;
336
	}
337
 
338
	public String getPincode() {
339
		return pincode;
340
	}
341
 
342
	public void setPincode(String pincode) {
343
		this.pincode = pincode;
344
	}
345
 
346
	public String getCountry() {
347
		return country;
348
	}
349
 
350
	public void setCountry(String country) {
351
		this.country = country;
352
	}
353
 
354
	public String getPhone() {
355
		return phone;
356
	}
357
 
358
	public void setPhone(String phone) {
359
		this.phone = phone;
360
	}
3063 chandransh 361
 
362
    public boolean shouldShowCodOption() {
363
        return showCodOption;
364
    }
365
 
366
    public String getPublicKey(){
367
        return RECAPTCHA_PUBLIC_KEY;
368
    }
1429 varun.gupt 369
}