Subversion Repositories SmartDukaan

Rev

Rev 7842 | Rev 9301 | 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;
7842 rajveer 4
import in.shop2020.logistics.DeliveryType;
5746 anupam.sin 5
import in.shop2020.logistics.LogisticsService;
5716 anupam.sin 6
import in.shop2020.logistics.PickupStore;
7
 
1429 varun.gupt 8
import in.shop2020.model.v1.catalog.Item;
507 rajveer 9
import in.shop2020.model.v1.user.Address;
10
import in.shop2020.model.v1.user.AddressType;
841 vikas 11
import in.shop2020.model.v1.user.Cart;
1429 varun.gupt 12
import in.shop2020.model.v1.user.Line;
1774 varun.gupt 13
import in.shop2020.model.v1.user.User;
572 chandransh 14
import in.shop2020.model.v1.user.UserContextService;
2137 chandransh 15
import in.shop2020.serving.utils.FormattingUtils;
839 vikas 16
import in.shop2020.serving.utils.Utils;
3126 rajveer 17
import in.shop2020.thrift.clients.CatalogClient;
5716 anupam.sin 18
import in.shop2020.thrift.clients.LogisticsClient;
3126 rajveer 19
import in.shop2020.thrift.clients.UserClient;
2511 vikas 20
import in.shop2020.utils.DataLogger;
507 rajveer 21
 
2419 vikas 22
import java.util.ArrayList;
23
import java.util.Collection;
24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
507 rajveer 27
 
832 rajveer 28
import org.apache.log4j.Logger;
822 vikas 29
import org.apache.struts2.convention.annotation.InterceptorRef;
30
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 31
import org.apache.struts2.convention.annotation.Result;
32
import org.apache.struts2.convention.annotation.Results;
5746 anupam.sin 33
import org.apache.thrift.TException;
1429 varun.gupt 34
import org.apache.velocity.VelocityContext;
507 rajveer 35
 
3076 chandransh 36
@SuppressWarnings("serial")
822 vikas 37
@InterceptorRefs({
38
    @InterceptorRef("myDefault"),
39
    @InterceptorRef("login")
40
})
41
 
637 rajveer 42
@Results({
43
    @Result(name="redirect", type="redirectAction", 
5716 anupam.sin 44
    		params = {"actionName" , "shipping", "st", "${selectedTab}"})
637 rajveer 45
})
822 vikas 46
public class ShippingController extends BaseController{
507 rajveer 47
 
3101 chandransh 48
	private static Logger log = Logger.getLogger(ShippingController.class);
3063 chandransh 49
 
517 rajveer 50
	private long addressId = 0;
572 chandransh 51
	private String errorMsg = "";
507 rajveer 52
 
822 vikas 53
	private String name;
54
	private String line1;
55
	private String line2;
56
	private String city;
839 vikas 57
	private String state = "";
822 vikas 58
	private String pincode;
59
	private String phone;
60
	private String country;
5716 anupam.sin 61
	private String selectedTab = "myLocation";
822 vikas 62
 
6903 anupam.sin 63
	private String dob;
64
    private String guardianName;
65
 
66
    private boolean isAnyItemInsured = false;
67
 
5746 anupam.sin 68
	private static List<PickupStore> storeAddresses = new ArrayList<PickupStore>();
69
	private static final List<String> allZones = new ArrayList<String>();
70
 
71
    static {
72
        try {
73
            allZones.add("All");
74
            LogisticsService.Client logisticsClient = (new LogisticsClient()).getClient();
75
            storeAddresses = logisticsClient.getAllPickupStores();
76
            for (PickupStore store : storeAddresses) {
77
                if(!allZones.contains(store.getZone())) {
78
                    allZones.add(store.getZone());
79
                }
80
            }
81
        } catch (TException e) {
82
            e.printStackTrace();
83
        }
84
    }
85
 
507 rajveer 86
	public ShippingController(){
87
		super();
88
	}
89
 
90
	 // GET /shipping
637 rajveer 91
	 public String index() {
555 chandransh 92
    	long userId = userinfo.getUserId();
692 chandransh 93
    	long cartId = userinfo.getCartId();
3209 vikas 94
    	String itemIdString = "";
572 chandransh 95
    	try {
3126 rajveer 96
			UserContextService.Client userClient = (new UserClient()).getClient();
692 chandransh 97
			userClient.checkOut(cartId);
841 vikas 98
			Cart cart = userClient.getCart(cartId);
3209 vikas 99
			itemIdString = Utils.getItemIdStringInCart(cart);
6903 anupam.sin 100
			for(Line line : cart.getLines()) {
101
			    if(line.getInsurer() > 0) {
102
			        setAnyItemInsured(true);
103
			    }
104
			}
841 vikas 105
			long defaultAddressId = cart.getAddressId();
106
			if (defaultAddressId == 0) {
107
			    defaultAddressId = userClient.getDefaultAddressId(userId);
108
			}
637 rajveer 109
			log.info("The default address id of this user is: " + defaultAddressId);
110
			if(defaultAddressId > 0)
692 chandransh 111
				userClient.addAddressToCart(cartId, defaultAddressId);
6736 amit.gupta 112
			errorMsg = userClient.validateCart(cartId, sourceId).get(0);
572 chandransh 113
 
114
		} catch (Exception e) {
2949 chandransh 115
			// This exception can be ignored for showing the shipping page. Not so
572 chandransh 116
			// innocent when this occurs at the time of checkout or when the
117
			// user is proceeding to pay.
2949 chandransh 118
			log.error("Unable to get all the data required for displaying the shipping page", e);
572 chandransh 119
		}
712 rajveer 120
    	// in case this page is redirected from payment failure
822 vikas 121
 
839 vikas 122
		Collection<String> actionErrors = getActionErrors();
123
		if(actionErrors != null && !actionErrors.isEmpty()){
124
			for (String str : actionErrors) {
125
			    errorMsg += "<BR/>" + str;
126
			}
712 rajveer 127
		}
3185 vikas 128
        DataLogger.logData(EventType.SHIPPINIG_ACCESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
3209 vikas 129
                Long.toString(cartId), itemIdString);
822 vikas 130
 
637 rajveer 131
    	return "index";
507 rajveer 132
	 }
133
 
134
	// POST /entity
637 rajveer 135
	public String create(){
3126 rajveer 136
		UserClient userContextServiceClient = null;
555 chandransh 137
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
138
 
637 rajveer 139
    	String action = this.request.getParameter("action");
517 rajveer 140
		if(action == null){
637 rajveer 141
			return "failure";
517 rajveer 142
		}
507 rajveer 143
 
555 chandransh 144
		try {
3126 rajveer 145
			userContextServiceClient = new UserClient();
555 chandransh 146
			userClient = userContextServiceClient.getClient();
517 rajveer 147
 
637 rajveer 148
			if(action != null){
149
				if(action.equals("add")){
839 vikas 150
					boolean invalidInput = false;
151
					if (!Utils.validatePin(this.pincode)) {
152
						addActionError("Invalid pincode.");
153
						invalidInput = true;
154
					}
155
					if (!Utils.validatePhone(this.phone)) {
156
						addActionError("Invalid phone number.");
157
						invalidInput = true;
158
					}
159
					if (this.line1.isEmpty()) {
160
						addActionError("Address line1 is empty.");
161
						invalidInput = true;
162
					}
163
					if (this.city.isEmpty()) {
164
						addActionError("City name is empty.");
165
						invalidInput = true;
166
					}
167
					if (this.state.isEmpty()) {
168
						addActionError("State name is empty.");
169
						invalidInput = true;
170
					}
5076 varun.gupt 171
					if (this.pincode.isEmpty()) {
172
						addActionError("Pincode is empty.");
173
						invalidInput = true;
174
					}
6903 anupam.sin 175
 
839 vikas 176
					if (!invalidInput) {
177
						Address address = new Address();
178
						address.setName(this.name);
179
						address.setLine1(this.line1);
180
						address.setLine2(this.line2);
181
						address.setCity(this.city);
182
						address.setState(this.state);
183
						address.setPin(this.pincode);
184
						address.setPhone(this.phone);
185
						address.setCountry(this.country);
186
						address.setEnabled(true);
187
						address.setType(AddressType.HOME);
1446 varun.gupt 188
						long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
189
						userClient.addAddressToCart(userinfo.getCartId(), addressId);
839 vikas 190
						addActionMessage("Address added successfully.");
6903 anupam.sin 191
						try {
192
    						if (this.guardianName != null &&
193
                                this.dob != null && !this.guardianName.isEmpty() && !this.dob.isEmpty()) {
194
                                userClient.storeInsuranceSpecificDetails(addressId, dob, guardianName);
195
                            }
196
						} catch(Exception e) {
197
						    log.error("Error while adding insurance specific details for address : " + addressId + ", DOB : " + dob + " GuardianName : " + guardianName, e);
198
						}
3185 vikas 199
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), address.getName(),
2157 vikas 200
                                        address.getCity(), address.getPhone(), address.getPin());
839 vikas 201
					}
202
					return "redirect";				
517 rajveer 203
				}
204
 
1446 varun.gupt 205
				if(action.equals("change"))	{
841 vikas 206
					addressId = Long.parseLong(this.request.getParameter("addressid"));
5716 anupam.sin 207
					if(request.getParameter("selectedTab").equals("HotSpot")) {
208
					    userClient.addStoreToCart(userinfo.getCartId(), addressId);
209
					} else {
210
					    userClient.addAddressToCart(userinfo.getCartId(), addressId);
211
					}
1446 varun.gupt 212
 
6736 amit.gupta 213
					errorMsg = userClient.validateCart(userinfo.getCartId(), sourceId).get(0);
3224 vikas 214
                    DataLogger.logData(EventType.SHIPPINIG_ADD_CHANGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 215
                            Long.toString(userinfo.getCartId()), Long.toString(addressId));
1446 varun.gupt 216
					return "index";
637 rajveer 217
				}
517 rajveer 218
			}
637 rajveer 219
		} catch (Exception e) {
3076 chandransh 220
		    log.error("Error while adding address to the cart", e);
637 rajveer 221
			return "failure";
517 rajveer 222
		}
637 rajveer 223
		return null;
507 rajveer 224
	}
6903 anupam.sin 225
 
226
	public String cancelInsurance() {
227
	    try {
228
            in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
229
            boolean result = userClient.cancelInsurance(userinfo.getCartId());
230
            if (result == false) {
231
                return "failure";
232
            }
233
        } catch (Exception e) {
234
            log.error("Unable to cancel insurance for cart : " + userinfo.getCartId(), e);
235
            return "failure";
236
        }
237
        return null;
238
	}
239
 
240
	public String storeInsuranceDetails() {
241
	    try {
242
	        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
243
	        boolean result = userClient.storeInsuranceSpecificDetails(addressId, dob, guardianName);
244
	        if (result == false) {
245
                return "failure";
246
            }
247
	    } catch (Exception e) {
248
	        log.error("Unable to store dob and guradianName for addressId : " + addressId, e);
249
	        return "failure";
250
	    }
251
	    return null;
252
	}
507 rajveer 253
 
1429 varun.gupt 254
	public String getShippingHeaderSnippet() {
255
		String htmlString = "";
256
		VelocityContext context = new VelocityContext();
257
		String templateFile = "templates/shippingheader.vm";
258
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
259
		return htmlString;
507 rajveer 260
	}
261
 
1429 varun.gupt 262
	public String getShippingDetailsSnippet() {
5326 rajveer 263
		long userId = userinfo.getUserId();
1429 varun.gupt 264
		long cartId = userinfo.getCartId();
265
		String htmlString = "";
266
		VelocityContext context = new VelocityContext();
267
		String templateFile = "templates/shippingdetails.vm";
268
		List<Map<String,String>> items = null;
269
		double totalamount= 0.0;
270
		List<Address> addresses = null;
271
		long defaultAddressId = 0;
5716 anupam.sin 272
		long defaultStoreAddressId = 0;
1774 varun.gupt 273
		String phoneNumber = "";
6903 anupam.sin 274
        double totalInsurance = 0.0;
275
        boolean isAnyItemInsured = false;
1429 varun.gupt 276
 
3126 rajveer 277
		CatalogClient catalogServiceClient  = null;
5945 mandeep.dh 278
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = null;
3126 rajveer 279
		UserClient userContextServiceClient = null;
1429 varun.gupt 280
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
281
 
2137 chandransh 282
		FormattingUtils formattingUtils = new FormattingUtils();
283
 
1429 varun.gupt 284
		try {
3126 rajveer 285
			catalogServiceClient = new CatalogClient();
1429 varun.gupt 286
			catalogClient = catalogServiceClient.getClient();
3126 rajveer 287
			userContextServiceClient = new UserClient();
1429 varun.gupt 288
			userClient = userContextServiceClient.getClient();
289
 
290
			Cart cart = userClient.getCart(cartId);
291
			List<Line> lineItems = cart.getLines();
292
 
293
			if( ! lineItems.isEmpty())	{
294
				items = new ArrayList<Map<String,String>>();
295
				for (Line line : lineItems) {
296
					Map<String, String> itemdetail = new HashMap<String, String>();
3561 rajveer 297
					Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
1429 varun.gupt 298
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
3830 chandransh 299
                                    + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
300
                                    + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
301
 
302
                    String itemColor = "";
303
                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
304
                    itemColor = "Color - " + item.getColor();
305
 
306
                    itemdetail.put("ITEM_NAME", itemName);
307
                    itemdetail.put("ITEM_COLOR", itemColor);
1429 varun.gupt 308
					itemdetail.put("ITEM_ID", line.getItemId()+"");
3830 chandransh 309
					itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
1429 varun.gupt 310
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 311
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
312
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
313
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
7913 rajveer 314
					itemdetail.put("SHIPPING_TIME", (line.getEstimate() == -1) ? "-1" : EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.PREPAID));
4217 varun.gupt 315
					itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
6903 anupam.sin 316
					itemdetail.put("INSURANCE_AMOUNT", line.getInsuranceAmount() + "");
317
					itemdetail.put("INSURER", line.getInsurer() + "");
2137 chandransh 318
					items.add(itemdetail);
6903 anupam.sin 319
					totalInsurance += line.getInsuranceAmount();
320
					if(line.getInsurer() > 0) {
321
					    isAnyItemInsured = true;
322
					}
1429 varun.gupt 323
				}
324
			}
2137 chandransh 325
			totalamount = cart.getTotalPrice();
5326 rajveer 326
			addresses = userClient.getAllAddressesForUser(userId);
327
			User user = userClient.getUserById(userId);
1774 varun.gupt 328
			phoneNumber = user.getMobileNumber();
5716 anupam.sin 329
 
1429 varun.gupt 330
			if(cart.isSetAddressId())	{
331
				defaultAddressId = cart.getAddressId();
332
			} else	{
5326 rajveer 333
				defaultAddressId = userClient.getDefaultAddressId(userId);
1429 varun.gupt 334
			}
5716 anupam.sin 335
 
336
			if(cart.isSetPickupStoreId())    {
337
                defaultStoreAddressId = cart.getPickupStoreId();
338
            }
1981 varun.gupt 339
 
6903 anupam.sin 340
			/*
341
			 *  ============================= START =====================================
342
			 *  The following code snippet is for extra details that are required for insurance purpose,
343
			 *  like father's/husband's name and Date of birth. If any item is insured in cart then we check
344
			 *  whether the user has any shipping addresses stored. If he has no previous addresses we ask
345
			 *  for those details 'in-Form' i.e. along with the address form. If he does have previous addresses,
346
			 *  we ask the user service if he has already stored insurance details corresponding to that address.
347
			 *  If such info is not present we ask for such detail in a "stand-Alone" form.
348
			 */
349
 
350
			if(isAnyItemInsured) {
351
			    if(addresses.size() == 0) {
352
			        context.put("insuranceDetailsRequired", "inForm");
353
			    } else {
354
			        if (!userClient.isInsuranceDetailPresent(defaultAddressId)) {
355
			            context.put("insuranceDetailsRequired", "standAlone");
356
			        } else {
357
			            context.put("insuranceDetailsRequired", "");
358
			        }
359
			    }
360
			}
361
			/*
362
			 * ============================== END ===========================================
363
			 */
364
 
1981 varun.gupt 365
			String couponCode = cart.getCouponCode();
366
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 367
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
6903 anupam.sin 368
	        context.put("discount", formattingUtils.formatPrice(cart.getDiscountedPrice() - cart.getTotalPrice()));
1981 varun.gupt 369
 
1429 varun.gupt 370
		} catch (Exception e)	{
2949 chandransh 371
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 372
		}
1981 varun.gupt 373
 
1774 varun.gupt 374
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 375
		context.put("items", items);
2137 chandransh 376
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
6903 anupam.sin 377
		context.put("totalInsurance", formattingUtils.formatPrice(totalInsurance));
1429 varun.gupt 378
		context.put("addresses", addresses);
379
		context.put("defaultAddressId", defaultAddressId+"");
5716 anupam.sin 380
		context.put("defaultStoreAddressId", defaultStoreAddressId+"");
1429 varun.gupt 381
		context.put("errorMsg", errorMsg);
5716 anupam.sin 382
		context.put("selectedTab", selectedTab);
383
		context.put("storeAddresses", storeAddresses);
5746 anupam.sin 384
		context.put("allZones", allZones);
6903 anupam.sin 385
		context.put("isAnyItemInsured", isAnyItemInsured + "");
1429 varun.gupt 386
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
387
		return htmlString;
507 rajveer 388
	}
389
 
517 rajveer 390
	public long getAddressId(){
391
		return this.addressId;
392
	}
572 chandransh 393
 
394
	public String getErrorMsg(){
395
		return this.errorMsg;
396
	}
822 vikas 397
 
398
	public String getName() {
399
		return name;
400
	}
401
 
402
	public void setName(String name) {
403
		this.name = name;
404
	}
405
 
406
	public String getLine1() {
407
		return line1;
408
	}
409
 
410
	public void setLine1(String line1) {
411
		this.line1 = line1;
412
	}
413
 
414
	public String getLine2() {
415
		return line2;
416
	}
417
 
418
	public void setLine2(String line2) {
419
		this.line2 = line2;
420
	}
421
 
422
	public String getCity() {
423
		return city;
424
	}
425
 
426
	public void setCity(String city) {
427
		this.city = city;
428
	}
429
 
430
	public String getState() {
431
		return state;
432
	}
433
 
434
	public void setState(String state) {
435
		this.state = state;
436
	}
437
 
438
	public String getPincode() {
439
		return pincode;
440
	}
441
 
442
	public void setPincode(String pincode) {
443
		this.pincode = pincode;
444
	}
445
 
446
	public String getCountry() {
447
		return country;
448
	}
449
 
450
	public void setCountry(String country) {
451
		this.country = country;
452
	}
453
 
454
	public String getPhone() {
455
		return phone;
456
	}
457
 
458
	public void setPhone(String phone) {
459
		this.phone = phone;
460
	}
3903 varun.gupt 461
 
5716 anupam.sin 462
    public String getSelectedTab() {
463
        return selectedTab;
464
    }
465
 
466
    public void setSelectedTab(String selectedTab) {
467
        this.selectedTab = selectedTab;
468
    }
469
 
470
    public void setSt(String selectedTab) {
471
        this.selectedTab = selectedTab;
472
    }
5746 anupam.sin 473
 
474
    public static List<String> getAllZones() {
475
        return allZones;
476
    }
6903 anupam.sin 477
 
478
    public void setAnyItemInsured(boolean isAnyItemInsured) {
479
        this.isAnyItemInsured = isAnyItemInsured;
480
    }
481
 
482
    public boolean isAnyItemInsured() {
483
        return isAnyItemInsured;
484
    }
485
 
486
    public String getDob() {
487
        return dob;
488
    }
489
 
490
    public void setDob(String dob) {
491
        this.dob = dob;
492
    }
493
 
494
    public String getGuardianName() {
495
        return guardianName;
496
    }
497
 
498
    public void setGuardianName(String guardianName) {
499
        this.guardianName = guardianName;
500
    }
501
 
502
    public void setAddressId(long addressId) {
503
        this.addressId = addressId;
504
    }
1429 varun.gupt 505
}