Subversion Repositories SmartDukaan

Rev

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

Rev 26607 Rev 28653
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.util.ArrayList;
3
import java.util.ArrayList;
4
import java.util.Arrays;
-
 
5
import java.util.HashMap;
4
import java.util.HashMap;
6
import java.util.List;
5
import java.util.List;
7
import java.util.Map;
6
import java.util.Map;
8
 
7
 
9
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletRequest;
10
 
9
 
11
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
11
import org.apache.logging.log4j.Logger;
13
import org.apache.thrift.TException;
-
 
14
import org.json.JSONArray;
-
 
15
import org.json.JSONException;
-
 
16
import org.json.JSONObject;
-
 
17
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.http.MediaType;
13
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
14
import org.springframework.http.ResponseEntity;
20
import org.springframework.stereotype.Controller;
15
import org.springframework.stereotype.Controller;
21
import org.springframework.transaction.annotation.Transactional;
16
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.web.bind.annotation.RequestBody;
17
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
19
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestParam;
20
import org.springframework.web.bind.annotation.RequestParam;
26
 
21
 
27
import com.google.gson.Gson;
-
 
28
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29
import com.spice.profitmandi.common.web.util.ResponseSender;
23
import com.spice.profitmandi.common.web.util.ResponseSender;
30
import com.spice.profitmandi.dao.entity.catalog.Item;
24
import com.spice.profitmandi.dao.cart.CartService;
31
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
25
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
32
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
26
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
33
import com.spice.profitmandi.dao.model.AddCartRequest;
27
import com.spice.profitmandi.dao.model.AddCartRequest;
34
import com.spice.profitmandi.dao.model.CartItem;
28
import com.spice.profitmandi.dao.model.CartItem;
-
 
29
import com.spice.profitmandi.dao.model.CartItemResponseModel;
35
import com.spice.profitmandi.dao.model.CartResponse;
30
import com.spice.profitmandi.dao.model.CartResponse;
36
import com.spice.profitmandi.dao.model.ProductPojo;
31
import com.spice.profitmandi.dao.model.ProductPojo;
37
import com.spice.profitmandi.dao.model.UserCart;
32
import com.spice.profitmandi.dao.model.UserCart;
38
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
33
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
39
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
40
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
35
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
41
import com.spice.profitmandi.dao.util.LogisticsService;
-
 
42
import com.spice.profitmandi.service.inventory.ItemBucketService;
36
import com.spice.profitmandi.service.inventory.ItemBucketService;
43
import com.spice.profitmandi.thrift.clients.UserClient;
37
import com.spice.profitmandi.thrift.clients.UserClient;
44
import com.spice.profitmandi.web.res.ValidateCartResponse;
38
import com.spice.profitmandi.web.res.ValidateCartResponse;
45
 
39
 
46
import in.shop2020.logistics.DeliveryType;
-
 
47
import in.shop2020.model.v1.user.ItemQuantity;
40
import in.shop2020.model.v1.user.ItemQuantity;
48
import in.shop2020.model.v1.user.UserContextService;
41
import in.shop2020.model.v1.user.UserContextService;
49
import in.shop2020.model.v1.user.UserContextService.Client;
-
 
50
import io.swagger.annotations.ApiImplicitParam;
42
import io.swagger.annotations.ApiImplicitParam;
51
import io.swagger.annotations.ApiImplicitParams;
43
import io.swagger.annotations.ApiImplicitParams;
52
import io.swagger.annotations.ApiOperation;
44
import io.swagger.annotations.ApiOperation;
53
 
45
 
54
@Controller
46
@Controller
Line 65... Line 57...
65
 
57
 
66
	@Autowired
58
	@Autowired
67
	private ItemBucketService itemBucketService;
59
	private ItemBucketService itemBucketService;
68
 
60
 
69
	@Autowired
61
	@Autowired
-
 
62
	CartService cartService;
-
 
63
 
-
 
64
	@Autowired
70
	private ContentPojoPopulator contentPojoPopulator;
65
	private ContentPojoPopulator contentPojoPopulator;
71
 
66
 
72
	@Autowired
67
	@Autowired
73
	private ItemRepository itemRepository;
68
	private ItemRepository itemRepository;
74
 
69
 
Line 88... Line 83...
88
			throws Throwable {
83
			throws Throwable {
89
 
84
 
90
		AddCartRequest cartRequest = new AddCartRequest();
85
		AddCartRequest cartRequest = new AddCartRequest();
91
		List<CartItem> ci = new ArrayList<>();
86
		List<CartItem> ci = new ArrayList<>();
92
		itemBucketService.getBucketDetails(bucketId).stream().forEach(x -> {
87
		itemBucketService.getBucketDetails(bucketId).stream().forEach(x -> {
93
			ci.add(new CartItem(x.getQuantity(),x.getItemId()));
88
			ci.add(new CartItem(x.getQuantity(), x.getItemId()));
94
		});
89
		});
95
		cartRequest.setCartItems(ci);
90
		cartRequest.setCartItems(ci);
96
		return this.validateCart(request, cartRequest, "110001");
91
		return this.validateCart(request, cartRequest, "110001");
97
 
92
 
98
	}
93
	}
Line 105... Line 100...
105
			@RequestParam(value = "pincode", defaultValue = "110001") String pincode) throws Throwable {
100
			@RequestParam(value = "pincode", defaultValue = "110001") String pincode) throws Throwable {
106
		UserAccount userAccount = null;
101
		UserAccount userAccount = null;
107
		ValidateCartResponse vc = null;
102
		ValidateCartResponse vc = null;
108
		int userId = (int) request.getAttribute("userId");
103
		int userId = (int) request.getAttribute("userId");
109
 
104
 
110
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.cartId);
105
		int cartId = userAccountRepository.selectByUserIdType(userId, AccountType.cartId).getAccountKey();
111
		logger.info("UserAccount................." + userAccount);
-
 
112
		Map<String, Integer> brandQtyMap = new HashMap<>();
-
 
113
		List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
-
 
114
		List<CartItem> cartItems = cartRequest.getCartItems();
106
		List<CartItem> cartItems = cartRequest.getCartItems();
115
		List<Integer> itemsList = new ArrayList<Integer>();
-
 
116
		// Only Keep Non Billable Stock if partner adds both
-
 
117
		boolean nogst = false;
-
 
118
 
-
 
119
		for (CartItem ci : cartItems) {
107
		cartService.addItemsToCart(cartId, cartItems);
120
			if (ci.getQuantity() > 0 && itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
-
 
121
				nogst = true;
-
 
122
			}
-
 
123
		}
-
 
124
 
-
 
125
		JSONObject cartMessage = null;
-
 
126
		for (CartItem ci : cartItems) {
-
 
127
			if (nogst && !itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
-
 
128
				if (cartMessage == null) {
-
 
129
					cartMessage = new JSONObject();
-
 
130
					cartMessage.put("messageText", "Billable items can't be billed along with non Billable items");
-
 
131
				}
-
 
132
				continue;
-
 
133
 
-
 
134
			}
-
 
135
			itemsList.add(ci.getItemId());
-
 
136
			ItemQuantity iq = new ItemQuantity(ci.getItemId(), ci.getQuantity());
-
 
137
			itemQuantities.add(iq);
-
 
138
		}
-
 
139
		if (cartMessage == null) {
-
 
140
			cartMessage = new JSONObject();
-
 
141
		}
-
 
142
 
-
 
143
		Client userClient = null;
-
 
144
		try {
-
 
145
			userClient = new UserClient().getClient();
-
 
146
			userClient.addItemsToCart(Integer.valueOf(userAccount.getAccountKey()), itemQuantities, null);
-
 
147
		} catch (Exception e) {
-
 
148
			vc = new ValidateCartResponse(null, "Error", "Problem occurred while updating cart");
108
		CartResponse cartValidationResponse = cartService.getCartValidation(cartId);
149
			return responseSender.internalServerError(vc);
-
 
150
		}
-
 
151
		String cartString = null;
-
 
152
		// Use source id temporarily for purpose of tag id as it is just one lets
-
 
153
		// hardcode the tag id
-
 
154
		int source = -1;
-
 
155
		cartString = userClient.validateCartNew(Integer.valueOf(userAccount.getAccountKey()), pincode, source);
-
 
156
		JSONObject cartObj = new JSONObject(cartString);
-
 
157
		JSONArray arr = cartObj.getJSONArray("cartItems");
-
 
158
		int maxEstimate = -2;
-
 
159
		boolean allSame = true;
-
 
160
		int removedCount = 0;
-
 
161
		cartObj.put("cartMessagesMerged", 0);
-
 
162
 
-
 
163
		for (int j = 0; j < arr.length(); j++) {
-
 
164
			JSONObject itemObj = arr.getJSONObject(j - removedCount);
-
 
165
			if (!itemsList.contains(itemObj.getInt("itemId"))) {
-
 
166
				if (itemObj.getInt("quantity") == 0) {
-
 
167
					arr.remove(j - removedCount);
-
 
168
					removedCount++;
-
 
169
					if (itemObj.has("estimate") && itemObj.getInt("estimate") == -1) {
-
 
170
						cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
-
 
171
					} else {
-
 
172
						cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
-
 
173
					}
-
 
174
					continue;
-
 
175
				} else {
-
 
176
					JSONArray messagesArray = new JSONArray();
-
 
177
					itemObj.put("cartItemMessages", messagesArray);
-
 
178
					if (itemsList.size() > 0) {
-
 
179
						JSONObject message = new JSONObject();
-
 
180
						message.put("type", "info");
-
 
181
						message.put("messageText", "Added from earlier cart");
-
 
182
						messagesArray.put(message);
-
 
183
						cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
-
 
184
					}
-
 
185
				}
-
 
186
			}
-
 
187
			Item item = itemRepository.selectById(itemObj.getInt("itemId"));
109
		for (CartItemResponseModel cartItem : cartValidationResponse.getCartItems()) {
188
			if (!brandQtyMap.containsKey(item.getBrand())) {
-
 
189
				brandQtyMap.put(item.getBrand(), 0);
-
 
190
			}
-
 
191
			brandQtyMap.put(item.getBrand(), brandQtyMap.get(item.getBrand()) + itemObj.getInt("quantity"));
-
 
192
			try {
-
 
193
				ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
110
			ProductPojo pp = contentPojoPopulator.getShortContent(cartItem.getCatalogItemId());
194
 
-
 
195
				if (itemObj.has("estimate")) {
-
 
196
					if (allSame) {
111
			if(pp!=null) {
197
						allSame = maxEstimate == -2 || maxEstimate == itemObj.getInt("estimate");
-
 
198
					}
-
 
199
					if (itemObj.getInt("estimate") > maxEstimate) {
-
 
200
						maxEstimate = itemObj.getInt("estimate");
-
 
201
					}
-
 
202
				}
-
 
203
				itemObj.put("imageUrl", pp.getImageUrl());
112
				cartItem.setImageUrl(pp.getImageUrl());
204
				itemObj.put("title", pp.getTitle());
113
				cartItem.setTitle(pp.getTitle());
205
			} catch (Throwable t) {
-
 
206
				itemObj.put("imageUrl", "");
-
 
207
				itemObj.put("title", item.getBrand() + " " + item.getModelName() + "" + item.getModelNumber());
-
 
208
			}
114
			}
209
		}
115
		}
210
 
-
 
211
		ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();
-
 
212
		if (arr != null) {
-
 
213
			for (int i = 0; i < arr.length(); i++) {
-
 
214
				JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
-
 
215
				if (itemMessages != null && itemMessages.length() > 0) {
-
 
216
					listdata.add(0, arr.getJSONObject(i));
-
 
217
				} else {
-
 
218
					listdata.add(arr.getJSONObject(i));
-
 
219
				}
-
 
220
			}
-
 
221
		}
-
 
222
		arr = new JSONArray();
-
 
223
		for (int i = 0; i < listdata.size(); i++) {
-
 
224
			arr.put(listdata.get(i));
-
 
225
		}
-
 
226
 
-
 
227
		cartObj.put("cartItems", arr);
-
 
228
		JSONArray cartMessagesArray = new JSONArray();
-
 
229
		for (Map.Entry<String, Integer> minBrandQtyLimit : MIN_BRAND_QTY_LIMIT.entrySet()) {
-
 
230
			if (brandQtyMap.containsKey(minBrandQtyLimit.getKey())) {
-
 
231
				if (brandQtyMap.get(minBrandQtyLimit.getKey()) < minBrandQtyLimit.getValue()) {
-
 
232
					cartMessagesArray
-
 
233
							.put(new JSONObject()
-
 
234
									.put("messageText",
-
 
235
											String.format("Minimum qty for %s brand should be %d",
-
 
236
													minBrandQtyLimit.getKey(), minBrandQtyLimit.getValue()))
-
 
237
									.put("type", "warn"));
-
 
238
				}
-
 
239
			}
-
 
240
		}
-
 
241
 
-
 
242
		for (String message : Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged",
-
 
243
				"cartMessagesMerged")) {
-
 
244
			int count = cartObj.getInt(message);
-
 
245
			if (count > 0) {
-
 
246
				String type = "danger";
-
 
247
				if (message.equals("cartMessagesMerged")) {
-
 
248
					type = "info";
-
 
249
					if (count == 1) {
-
 
250
						cartMessage.put("messageText", "1 item is added from earlier cart");
-
 
251
					} else {
-
 
252
						cartMessage.put("messageText", "Few items are added from earlier cart");
-
 
253
					}
-
 
254
				} else if (message.equals("cartMessageOOS")) {
-
 
255
					if (count == 1) {
-
 
256
						cartMessage.put("messageText", "One item is currently Out of Stock");
-
 
257
					} else {
-
 
258
						cartMessage.put("messageText", "Few items are currently Out of Stock");
-
 
259
					}
-
 
260
				} else if (message.equals("cartMessageUndeliverable")) {
-
 
261
					if (count == 1) {
-
 
262
						cartMessage.put("messageText", "One item is undeliverable");
-
 
263
					} else {
-
 
264
						cartMessage.put("messageText", "Few items are underiverable");
-
 
265
					}
-
 
266
				} else if (message.equals("cartMessageChanged")) {
-
 
267
					if (count == 1) {
-
 
268
						cartMessage.put("messageText", "One item qty has changed");
-
 
269
					} else {
-
 
270
						cartMessage.put("messageText", "Few items qty have changed");
-
 
271
					}
-
 
272
				}
-
 
273
				cartMessage.put("type", type);
-
 
274
				cartMessagesArray.put(cartMessage);
-
 
275
			}
-
 
276
		}
-
 
277
		cartObj.put("cartMessages", cartMessagesArray);
-
 
278
		// Hardcode maxEstimate to -3 for managing brands quantity
-
 
279
		if (maxEstimate == -1) {
-
 
280
			cartObj.put("estimateString", "Can't ship here");
-
 
281
		} else if (maxEstimate == -2) {
-
 
282
			cartObj.put("estimateString", "Out of Stock");
-
 
283
		} else {
-
 
284
			try {
-
 
285
				cartObj.put("cod", cartObj.getBoolean("codAllowed"));
-
 
286
				cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate, DeliveryType.COD));
-
 
287
			} catch (NumberFormatException | JSONException | TException e) {
-
 
288
				// TODO Auto-generated catch block
-
 
289
				e.printStackTrace();
-
 
290
				vc = new ValidateCartResponse(null, "Error", "Problem occurred while getting delivery estimates");
-
 
291
				return responseSender.internalServerError(vc);
-
 
292
			}
-
 
293
 
-
 
294
		}
-
 
295
		cartObj.put("maxEstimate", maxEstimate);
-
 
296
		CartResponse cartResponse = new Gson().fromJson(cartObj.toString(), CartResponse.class);
-
 
297
		vc = new ValidateCartResponse(cartResponse, "Success", "Items added to cart successfully");
116
		vc = new ValidateCartResponse(cartValidationResponse, "Success", "Items added to cart successfully");
298
		return responseSender.ok(vc);
117
		return responseSender.ok(vc);
299
	}
118
	}
300
 
119
 
301
	@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
120
	@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
302
	@ApiImplicitParams({
121
	@ApiImplicitParams({
Line 304... Line 123...
304
 
123
 
305
	@ApiOperation(value = "Change address")
124
	@ApiOperation(value = "Change address")
306
	public ResponseEntity<?> changeAddress(HttpServletRequest request,
125
	public ResponseEntity<?> changeAddress(HttpServletRequest request,
307
			@RequestParam(value = "addressId") long addressId) throws Throwable {
126
			@RequestParam(value = "addressId") long addressId) throws Throwable {
308
		UserCart uc = userAccountRepository.getUserCart((int) request.getAttribute("userId"));
127
		UserCart uc = userAccountRepository.getUserCart((int) request.getAttribute("userId"));
309
		UserContextService.Client userClient = new UserClient().getClient();
-
 
310
		userClient.addAddressToCart(uc.getCartId(), addressId);
128
		cartService.addAddressToCart(uc.getCartId(), addressId);
311
		return responseSender.ok("Address Changed successfully");
129
		return responseSender.ok("Address Changed successfully");
312
	}
130
	}
313
}
131
}
314
132