Subversion Repositories SmartDukaan

Rev

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

Rev 24198 Rev 24199
Line 48... Line 48...
48
import io.swagger.annotations.ApiImplicitParam;
48
import io.swagger.annotations.ApiImplicitParam;
49
import io.swagger.annotations.ApiImplicitParams;
49
import io.swagger.annotations.ApiImplicitParams;
50
import io.swagger.annotations.ApiOperation;
50
import io.swagger.annotations.ApiOperation;
51
 
51
 
52
@Controller
52
@Controller
53
@Transactional(rollbackFor=Throwable.class)
53
@Transactional(rollbackFor = Throwable.class)
54
public class CartController {
54
public class CartController {
55
 
55
 
56
	private static final Logger logger=LogManager.getLogger(CartController.class);
56
	private static final Logger logger = LogManager.getLogger(CartController.class);
57
	
57
 
58
	@Autowired
58
	@Autowired
59
	private ResponseSender<?> responseSender;
59
	private ResponseSender<?> responseSender;
60
 
60
 
61
	@Autowired
61
	@Autowired
62
	private UserAccountRepository userAccountRepository;
62
	private UserAccountRepository userAccountRepository;
63
	
63
 
64
	@Autowired
64
	@Autowired
65
	private ContentPojoPopulator contentPojoPopulator;
65
	private ContentPojoPopulator contentPojoPopulator;
66
	
66
 
67
	@Autowired
67
	@Autowired
68
	private ItemRepository itemRepository;
68
	private ItemRepository itemRepository;
69
	
-
 
70
 
69
 
71
	@RequestMapping(value = ProfitMandiConstants.URL_CART, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
70
	@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
72
	@ApiImplicitParams({
71
	@ApiImplicitParams({
73
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
72
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
74
				required = true, dataType = "string", paramType = "header")
-
 
75
	})
-
 
76
	@ApiOperation(value = "Add items to cart")
73
	@ApiOperation(value = "Add items to cart")
77
	public ResponseEntity<?> validateCart(HttpServletRequest request,@RequestBody AddCartRequest cartRequest, @RequestParam(value="pincode", defaultValue="110001") String pincode) throws Throwable{
74
	public ResponseEntity<?> validateCart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest,
-
 
75
			@RequestParam(value = "pincode", defaultValue = "110001") String pincode) throws Throwable {
78
		UserAccount userAccount = null;
76
		UserAccount userAccount = null;
79
		ValidateCartResponse vc = null;
77
		ValidateCartResponse vc = null;
80
		int userId = (int)request.getAttribute("userId");
78
		int userId = (int) request.getAttribute("userId");
81
		
79
 
82
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.cartId);
80
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.cartId);
83
		logger.info("UserAccount................."+userAccount);
81
		logger.info("UserAccount................." + userAccount);
84
 
82
 
85
		List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
83
		List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
86
		CartItems[] cartItems = cartRequest.getCartItems();
84
		CartItems[] cartItems = cartRequest.getCartItems();
87
		List<Integer> itemsList = new ArrayList<Integer>();
85
		List<Integer> itemsList = new ArrayList<Integer>();
88
		//Only Keep Billable Stock if partner adds both
86
		// Only Keep Billable Stock if partner adds both
89
		boolean noGst = false;
87
		boolean nogst = false;
90
		for (CartItems ci: cartItems) {
88
		for (CartItems ci : cartItems) {
91
			if (itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
89
			if (itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
92
				noGst=true;
90
				nogst = true;
93
			}
91
			}
94
		}
92
		}
-
 
93
 
95
		JSONObject cartMessage=null;
94
		JSONObject cartMessage = null;
96
		for (CartItems ci: cartItems) {
95
		for (CartItems ci : cartItems) {
97
			if (noGst && !itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
96
			if (nogst && !itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
98
				if(cartMessage == null) {
97
				if (cartMessage == null) {
99
					cartMessage = new JSONObject();
98
					cartMessage = new JSONObject();
100
					cartMessage.put("messageText", "Billable items can't be billed along with non Billable items");
99
					cartMessage.put("messageText", "Billable items can't be billed along with non Billable items");
101
				}
100
				}
102
				continue;
101
				continue;
-
 
102
 
103
			}
103
			}
104
			itemsList.add(ci.getItemId());
104
			itemsList.add(ci.getItemId());
105
			ItemQuantity iq = new ItemQuantity(ci.getItemId(), ci.getQuantity());
105
			ItemQuantity iq = new ItemQuantity(ci.getItemId(), ci.getQuantity());
106
			itemQuantities.add(iq);
106
			itemQuantities.add(iq);
107
		}
107
		}
108
		if(cartMessage == null) {
108
		if (cartMessage == null)
-
 
109
 
-
 
110
		{
109
			cartMessage = new JSONObject();
111
			cartMessage = new JSONObject();
110
		}
112
		}
111
 
113
 
112
		Client userClient = null;
114
		Client userClient = null;
113
		try {
115
		try {
114
			userClient = new UserClient().getClient();
116
			userClient = new UserClient().getClient();
115
			userClient.addItemsToCart(Integer.valueOf(userAccount.getAccountKey()), itemQuantities, null);
117
			userClient.addItemsToCart(Integer.valueOf(userAccount.getAccountKey()), itemQuantities, null);
116
		} catch (Exception e) {
118
		} catch (Exception e) {
117
			vc = new ValidateCartResponse(null, "Error","Problem occurred while updating cart");
119
			vc = new ValidateCartResponse(null, "Error", "Problem occurred while updating cart");
118
			return responseSender.internalServerError(vc);
120
			return responseSender.internalServerError(vc);
119
		}
121
		}
120
		String cartString=null;
122
		String cartString = null;
121
		//Use source id temporarily for purpose of tag id as it is just one lets hardcode the tag id
123
		// Use source id temporarily for purpose of tag id as it is just one lets
-
 
124
		// hardcode the tag id
122
		int source = -1;
125
		int source = -1;
123
		cartString = userClient.validateCartNew(Integer.valueOf(userAccount.getAccountKey()), pincode, source);
126
		cartString = userClient.validateCartNew(Integer.valueOf(userAccount.getAccountKey()), pincode, source);
124
		JSONObject cartObj = new JSONObject(cartString);
127
		JSONObject cartObj = new JSONObject(cartString);
125
		JSONArray arr = cartObj.getJSONArray("cartItems");
128
		JSONArray arr = cartObj.getJSONArray("cartItems");
126
		int maxEstimate=-2;
129
		int maxEstimate = -2;
127
		boolean allSame=true;
130
		boolean allSame = true;
128
		int removedCount = 0;
131
		int removedCount = 0;
129
		cartObj.put("cartMessagesMerged", 0);
132
		cartObj.put("cartMessagesMerged", 0);
130
		for (int j=0; j<arr.length(); j++){
133
		for (int j = 0; j < arr.length(); j++) {
131
			JSONObject itemObj = arr.getJSONObject(j-removedCount);
134
			JSONObject itemObj = arr.getJSONObject(j - removedCount);
132
			if(!itemsList.contains(itemObj.getInt("itemId"))){
135
			if (!itemsList.contains(itemObj.getInt("itemId"))) {
133
				if(itemObj.getInt("quantity")==0){
136
				if (itemObj.getInt("quantity") == 0) {
134
					arr.remove(j-removedCount);
137
					arr.remove(j - removedCount);
135
					removedCount++;
138
					removedCount++;
136
					if (itemObj.has("estimate") && itemObj.getInt("estimate")==-1){
139
					if (itemObj.has("estimate") && itemObj.getInt("estimate") == -1) {
137
						cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
140
						cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
138
					} else {
141
					} else {
139
						cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
142
						cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
140
					}
143
					}
141
					continue;
144
					continue;
142
				} else {
145
				} else {
143
					JSONArray messagesArray = new JSONArray();
146
					JSONArray messagesArray = new JSONArray();
144
					itemObj.put("cartItemMessages", messagesArray);
147
					itemObj.put("cartItemMessages", messagesArray);
145
					if(itemsList.size()>0){
148
					if (itemsList.size() > 0) {
146
						JSONObject message=new JSONObject();
149
						JSONObject message = new JSONObject();
147
						message.put("type","info");
150
						message.put("type", "info");
148
						message.put("messageText","Added from earlier cart");
151
						message.put("messageText", "Added from earlier cart");
149
						messagesArray.put(message);
152
						messagesArray.put(message);
150
						cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
153
						cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
151
					}
154
					}
152
				}
155
				}
153
			}
156
			}
154
			try {
157
			try {
155
			ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
158
				ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
156
//			String productProperties = ContentServingService.getSnippet(Utils.PRODUCT_PROPERTIES_SNIPPET, pp.getId() +"", -1);
159
//			String productProperties = ContentServingService.getSnippet(Utils.PRODUCT_PROPERTIES_SNIPPET, pp.getId() +"", -1);
157
//			JSONObject productPropertiesInJson = new JSONObject(productProperties);
160
//			JSONObject productPropertiesInJson = new JSONObject(productProperties);
158
//			pp.setCategoryName(productPropertiesInJson.getString("categoryName"));
161
//			pp.setCategoryName(productPropertiesInJson.getString("categoryName"));
159
 
162
 
160
			if(itemObj.has("estimate")){
163
				if (itemObj.has("estimate")) {
161
				if(allSame){
164
					if (allSame) {
162
					allSame = maxEstimate==-2 || maxEstimate==itemObj.getInt("estimate");
165
						allSame = maxEstimate == -2 || maxEstimate == itemObj.getInt("estimate");
163
				}
166
					}
164
				if(itemObj.getInt("estimate") > maxEstimate){
167
					if (itemObj.getInt("estimate") > maxEstimate) {
165
					maxEstimate = itemObj.getInt("estimate");
168
						maxEstimate = itemObj.getInt("estimate");
-
 
169
					}
166
				}
170
				}
167
			}
-
 
168
				itemObj.put("imageUrl", pp.getImageUrl());
171
				itemObj.put("imageUrl", pp.getImageUrl());
169
				itemObj.put("title", pp.getTitle());
172
				itemObj.put("title", pp.getTitle());
170
			} catch (Throwable t) {
173
			} catch (Throwable t) {
171
				Item item = itemRepository.selectById(itemObj.getInt("itemId"));
174
				Item item = itemRepository.selectById(itemObj.getInt("itemId"));
172
				itemObj.put("imageUrl", "");
175
				itemObj.put("imageUrl", "");
173
				itemObj.put("title", item.getBrand() + " " + item.getModelName() + "" + item.getModelNumber());
176
				itemObj.put("title", item.getBrand() + " " + item.getModelName() + "" + item.getModelNumber());
174
			}
177
			}
175
		}
178
		}
176
 
179
 
177
		ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();     
180
		ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();
178
		if (arr != null) { 
181
		if (arr != null) {
179
			for (int i=0;i<arr.length();i++){
182
			for (int i = 0; i < arr.length(); i++) {
180
				JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
183
				JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
181
				if(itemMessages!=null && itemMessages.length()>0){
184
				if (itemMessages != null && itemMessages.length() > 0) {
182
					listdata.add(0,arr.getJSONObject(i));
185
					listdata.add(0, arr.getJSONObject(i));
183
				} else {
186
				} else {
184
					listdata.add(arr.getJSONObject(i));
187
					listdata.add(arr.getJSONObject(i));
185
				}
188
				}
186
			} 
189
			}
187
		}
190
		}
188
		arr = new JSONArray();
191
		arr = new JSONArray();
189
		for(int i=0;i<listdata.size();i++){
192
		for (int i = 0; i < listdata.size(); i++) {
190
			arr.put(listdata.get(i));
193
			arr.put(listdata.get(i));
191
		}
194
		}
192
		cartObj.put("cartItems", arr);
195
		cartObj.put("cartItems", arr);
193
		JSONArray cartMessagesArray = new JSONArray();
196
		JSONArray cartMessagesArray = new JSONArray();
194
		for (String message :Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged","cartMessagesMerged")){
197
		for (String message : Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged",
-
 
198
				"cartMessagesMerged")) {
195
			int count = cartObj.getInt(message);
199
			int count = cartObj.getInt(message);
196
			if (count>0) {
200
			if (count > 0) {
197
				String type="danger";
201
				String type = "danger";
198
				if (message.equals("cartMessagesMerged")) {
202
				if (message.equals("cartMessagesMerged")) {
199
					type = "info";
203
					type = "info";
200
					if (count==1){
204
					if (count == 1) {
201
						cartMessage.put("messageText","1 item is added from earlier cart");
205
						cartMessage.put("messageText", "1 item is added from earlier cart");
202
					}else {
206
					} else {
203
						cartMessage.put("messageText","Few items are added from earlier cart");
207
						cartMessage.put("messageText", "Few items are added from earlier cart");
204
					}
208
					}
205
				} else if (message.equals("cartMessageOOS")) {
209
				} else if (message.equals("cartMessageOOS")) {
206
					if (count==1){
210
					if (count == 1) {
207
						cartMessage.put("messageText","One item is currently Out of Stock");
211
						cartMessage.put("messageText", "One item is currently Out of Stock");
208
					}else {
212
					} else {
209
						cartMessage.put("messageText","Few items are currently Out of Stock");
213
						cartMessage.put("messageText", "Few items are currently Out of Stock");
210
					}
214
					}
211
				} else if (message.equals("cartMessageUndeliverable")) {
215
				} else if (message.equals("cartMessageUndeliverable")) {
212
					if (count==1){
216
					if (count == 1) {
213
						cartMessage.put("messageText","One item is undeliverable");
217
						cartMessage.put("messageText", "One item is undeliverable");
214
					}else {
218
					} else {
215
						cartMessage.put("messageText","Few items are underiverable");
219
						cartMessage.put("messageText", "Few items are underiverable");
216
					}
220
					}
217
				} else {
221
				} else {
218
					if (count==1){
222
					if (count == 1) {
219
						cartMessage.put("messageText","One item qty has changed");
223
						cartMessage.put("messageText", "One item qty has changed");
220
					}else {
224
					} else {
221
						cartMessage.put("messageText","Few items qty have changed");
225
						cartMessage.put("messageText", "Few items qty have changed");
222
					}
226
					}
223
				}
227
				}
224
				cartMessage.put("type",type);
228
				cartMessage.put("type", type);
225
				cartMessagesArray.put(cartMessage);
229
				cartMessagesArray.put(cartMessage);
226
			}
230
			}
227
		}
231
		}
228
		cartObj.put("cartMessages", cartMessagesArray);
232
		cartObj.put("cartMessages", cartMessagesArray);
229
 
233
 
230
		if (maxEstimate==-1){
234
		if (maxEstimate == -1) {
231
			cartObj.put("estimateString", "Can't ship here");
235
			cartObj.put("estimateString", "Can't ship here");
232
		}
-
 
233
		else if(maxEstimate==-2){
236
		} else if (maxEstimate == -2) {
234
			cartObj.put("estimateString", "Out of Stock");
237
			cartObj.put("estimateString", "Out of Stock");
235
		} else {
238
		} else {
236
			try {
239
			try {
237
				cartObj.put("cod", cartObj.getBoolean("codAllowed"));
240
				cartObj.put("cod", cartObj.getBoolean("codAllowed"));
238
				cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
241
				cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate, DeliveryType.COD));
239
			} catch (NumberFormatException | JSONException | TException e) {
242
			} catch (NumberFormatException | JSONException | TException e) {
240
				// TODO Auto-generated catch block
243
				// TODO Auto-generated catch block
241
				e.printStackTrace();
244
				e.printStackTrace();
242
				vc = new ValidateCartResponse(null, "Error","Problem occurred while getting delivery estimates");
245
				vc = new ValidateCartResponse(null, "Error", "Problem occurred while getting delivery estimates");
243
				return responseSender.internalServerError(vc);
246
				return responseSender.internalServerError(vc);
244
			}
247
			}
245
			
248
 
246
		}
249
		}
247
		cartObj.put("maxEstimate", maxEstimate);
250
		cartObj.put("maxEstimate", maxEstimate);
248
		CartResponse cartResponse = new Gson().fromJson(cartObj.toString(), CartResponse.class);
251
		CartResponse cartResponse = new Gson().fromJson(cartObj.toString(), CartResponse.class);
249
		vc = new ValidateCartResponse(cartResponse, "Success","Items added to cart successfully");
252
		vc = new ValidateCartResponse(cartResponse, "Success", "Items added to cart successfully");
250
		return responseSender.ok(vc);
253
		return responseSender.ok(vc);
251
	}
254
	}
252
 
255
 
253
	@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
256
	@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
254
	@ApiImplicitParams({
257
	@ApiImplicitParams({
255
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
258
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
256
				required = true, dataType = "string", paramType = "header")
-
 
257
	})
-
 
258
	
259
 
259
	@ApiOperation(value = "Change address")
260
	@ApiOperation(value = "Change address")
260
	public ResponseEntity<?> changeAddress(HttpServletRequest request, @RequestParam(value="addressId") long addressId) throws Throwable{
261
	public ResponseEntity<?> changeAddress(HttpServletRequest request,
-
 
262
			@RequestParam(value = "addressId") long addressId) throws Throwable {
261
		UserCart uc = userAccountRepository.getUserCart((int)request.getAttribute("userId"));
263
		UserCart uc = userAccountRepository.getUserCart((int) request.getAttribute("userId"));
262
    	UserContextService.Client userClient = new UserClient().getClient();
264
		UserContextService.Client userClient = new UserClient().getClient();
263
    	userClient.addAddressToCart(uc.getCartId(), addressId);
265
		userClient.addAddressToCart(uc.getCartId(), addressId);
264
    	return responseSender.ok("Address Changed successfully");
266
		return responseSender.ok("Address Changed successfully");
265
    }
267
	}
266
}
268
}
267
269