Subversion Repositories SmartDukaan

Rev

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

Rev 24203 Rev 24422
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;
4
import java.util.Arrays;
-
 
5
import java.util.HashMap;
5
import java.util.List;
6
import java.util.List;
-
 
7
import java.util.Map;
6
 
8
 
7
import javax.servlet.http.HttpServletRequest;
9
import javax.servlet.http.HttpServletRequest;
8
 
10
 
9
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
12
import org.apache.logging.log4j.Logger;
Line 64... Line 66...
64
	private ContentPojoPopulator contentPojoPopulator;
66
	private ContentPojoPopulator contentPojoPopulator;
65
 
67
 
66
	@Autowired
68
	@Autowired
67
	private ItemRepository itemRepository;
69
	private ItemRepository itemRepository;
68
 
70
 
-
 
71
	public static final Map<String, Integer> MIN_BRAND_QTY_LIMIT = new HashMap<>();
-
 
72
 
-
 
73
	static {
-
 
74
		MIN_BRAND_QTY_LIMIT.put("RealMe", 10);
-
 
75
	}
-
 
76
 
69
	@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
77
	@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
70
	@ApiImplicitParams({
78
	@ApiImplicitParams({
71
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
79
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
72
	@ApiOperation(value = "Add items to cart")
80
	@ApiOperation(value = "Add items to cart")
73
	public ResponseEntity<?> validateCart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest,
81
	public ResponseEntity<?> validateCart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest,
Line 76... Line 84...
76
		ValidateCartResponse vc = null;
84
		ValidateCartResponse vc = null;
77
		int userId = (int) request.getAttribute("userId");
85
		int userId = (int) request.getAttribute("userId");
78
 
86
 
79
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.cartId);
87
		userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.cartId);
80
		logger.info("UserAccount................." + userAccount);
88
		logger.info("UserAccount................." + userAccount);
81
 
-
 
-
 
89
		Map<String, Integer> brandQtyMap = new HashMap<>();
82
		List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
90
		List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
83
		CartItems[] cartItems = cartRequest.getCartItems();
91
		CartItems[] cartItems = cartRequest.getCartItems();
84
		List<Integer> itemsList = new ArrayList<Integer>();
92
		List<Integer> itemsList = new ArrayList<Integer>();
85
		// Only Keep Billable Stock if partner adds both
93
		// Only Keep Billable Stock if partner adds both
86
		boolean nogst = false;
94
		boolean nogst = false;
-
 
95
		
-
 
96
		
87
		for (CartItems ci : cartItems) {
97
		for (CartItems ci : cartItems) {
88
			if (itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
98
			if (itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
89
				nogst = true;
99
				nogst = true;
90
			}
100
			}
91
		}
101
		}
Line 126... Line 136...
126
		JSONArray arr = cartObj.getJSONArray("cartItems");
136
		JSONArray arr = cartObj.getJSONArray("cartItems");
127
		int maxEstimate = -2;
137
		int maxEstimate = -2;
128
		boolean allSame = true;
138
		boolean allSame = true;
129
		int removedCount = 0;
139
		int removedCount = 0;
130
		cartObj.put("cartMessagesMerged", 0);
140
		cartObj.put("cartMessagesMerged", 0);
-
 
141
		
131
		for (int j = 0; j < arr.length(); j++) {
142
		for (int j = 0; j < arr.length(); j++) {
132
			JSONObject itemObj = arr.getJSONObject(j - removedCount);
143
			JSONObject itemObj = arr.getJSONObject(j - removedCount);
133
			if (!itemsList.contains(itemObj.getInt("itemId"))) {
144
			if (!itemsList.contains(itemObj.getInt("itemId"))) {
134
				if (itemObj.getInt("quantity") == 0) {
145
				if (itemObj.getInt("quantity") == 0) {
135
					arr.remove(j - removedCount);
146
					arr.remove(j - removedCount);
Line 150... Line 161...
150
						messagesArray.put(message);
161
						messagesArray.put(message);
151
						cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
162
						cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
152
					}
163
					}
153
				}
164
				}
154
			}
165
			}
-
 
166
			Item item = itemRepository.selectById(itemObj.getInt("itemId"));
-
 
167
			if(!brandQtyMap.containsKey(item.getBrand())) {
-
 
168
				brandQtyMap.put(item.getBrand(), 0);
-
 
169
			}
-
 
170
			brandQtyMap.put(item.getBrand(), brandQtyMap.get(item.getBrand()) + itemObj.getInt("quantity"));
155
			try {
171
			try {
156
				ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
172
				ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
157
//			String productProperties = ContentServingService.getSnippet(Utils.PRODUCT_PROPERTIES_SNIPPET, pp.getId() +"", -1);
-
 
158
//			JSONObject productPropertiesInJson = new JSONObject(productProperties);
-
 
159
//			pp.setCategoryName(productPropertiesInJson.getString("categoryName"));
-
 
160
 
173
 
161
				if (itemObj.has("estimate")) {
174
				if (itemObj.has("estimate")) {
162
					if (allSame) {
175
					if (allSame) {
163
						allSame = maxEstimate == -2 || maxEstimate == itemObj.getInt("estimate");
176
						allSame = maxEstimate == -2 || maxEstimate == itemObj.getInt("estimate");
164
					}
177
					}
Line 167... Line 180...
167
					}
180
					}
168
				}
181
				}
169
				itemObj.put("imageUrl", pp.getImageUrl());
182
				itemObj.put("imageUrl", pp.getImageUrl());
170
				itemObj.put("title", pp.getTitle());
183
				itemObj.put("title", pp.getTitle());
171
			} catch (Throwable t) {
184
			} catch (Throwable t) {
172
				Item item = itemRepository.selectById(itemObj.getInt("itemId"));
-
 
173
				itemObj.put("imageUrl", "");
185
				itemObj.put("imageUrl", "");
174
				itemObj.put("title", item.getBrand() + " " + item.getModelName() + "" + item.getModelNumber());
186
				itemObj.put("title", item.getBrand() + " " + item.getModelName() + "" + item.getModelNumber());
175
			}
187
			}
176
		}
188
		}
177
 
189
 
Line 188... Line 200...
188
		}
200
		}
189
		arr = new JSONArray();
201
		arr = new JSONArray();
190
		for (int i = 0; i < listdata.size(); i++) {
202
		for (int i = 0; i < listdata.size(); i++) {
191
			arr.put(listdata.get(i));
203
			arr.put(listdata.get(i));
192
		}
204
		}
-
 
205
		Map.Entry<String, Integer> failedBrandItemQty = null;
-
 
206
		for(Map.Entry<String, Integer> minBrandQtyLimit : MIN_BRAND_QTY_LIMIT.entrySet()) {
-
 
207
			if(brandQtyMap.containsKey(minBrandQtyLimit.getKey())) {
-
 
208
				if(brandQtyMap.get(minBrandQtyLimit.getKey()) < minBrandQtyLimit.getValue()) {
-
 
209
					maxEstimate = -3;
-
 
210
					failedBrandItemQty = minBrandQtyLimit;
-
 
211
					break;
-
 
212
				}
-
 
213
			}
-
 
214
		}
193
		cartObj.put("cartItems", arr);
215
		cartObj.put("cartItems", arr);
194
		JSONArray cartMessagesArray = new JSONArray();
216
		JSONArray cartMessagesArray = new JSONArray();
195
		for (String message : Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged",
217
		for (String message : Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged",
196
				"cartMessagesMerged")) {
218
				"cartMessagesMerged")) {
197
			int count = cartObj.getInt(message);
219
			int count = cartObj.getInt(message);
Line 226... Line 248...
226
				cartMessage.put("type", type);
248
				cartMessage.put("type", type);
227
				cartMessagesArray.put(cartMessage);
249
				cartMessagesArray.put(cartMessage);
228
			}
250
			}
229
		}
251
		}
230
		cartObj.put("cartMessages", cartMessagesArray);
252
		cartObj.put("cartMessages", cartMessagesArray);
231
 
-
 
-
 
253
		//Hardcode maxEstimate to -3 for managing brands quantity
232
		if (maxEstimate == -1) {
254
		if (maxEstimate == -1) {
233
			cartObj.put("estimateString", "Can't ship here");
255
			cartObj.put("estimateString", "Can't ship here");
234
		} else if (maxEstimate == -2) {
256
		} else if (maxEstimate == -2) {
235
			cartObj.put("estimateString", "Out of Stock");
257
			cartObj.put("estimateString", "Out of Stock");
-
 
258
		} else if(maxEstimate == -3){
-
 
259
			maxEstimate = -1;
-
 
260
			cartObj.put("estimateString", String.format("Minimum qty for %s brand should be %d", failedBrandItemQty.getKey(), failedBrandItemQty.getValue()));
236
		} else {
261
		} else {
237
			try {
262
			try {
238
				cartObj.put("cod", cartObj.getBoolean("codAllowed"));
263
				cartObj.put("cod", cartObj.getBoolean("codAllowed"));
239
				cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate, DeliveryType.COD));
264
				cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate, DeliveryType.COD));
240
			} catch (NumberFormatException | JSONException | TException e) {
265
			} catch (NumberFormatException | JSONException | TException e) {