Subversion Repositories SmartDukaan

Rev

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