| 26522 |
amit.gupta |
1 |
package com.spice.profitmandi.dao.cart;
|
|
|
2 |
|
| 28166 |
tejbeer |
3 |
import java.time.LocalDateTime;
|
| 26522 |
amit.gupta |
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.Arrays;
|
| 29327 |
tejbeer |
6 |
import java.util.Collections;
|
| 26522 |
amit.gupta |
7 |
import java.util.HashMap;
|
|
|
8 |
import java.util.HashSet;
|
| 29327 |
tejbeer |
9 |
import java.util.LinkedHashMap;
|
| 26522 |
amit.gupta |
10 |
import java.util.List;
|
|
|
11 |
import java.util.Map;
|
|
|
12 |
import java.util.Set;
|
| 29327 |
tejbeer |
13 |
import java.util.Map.Entry;
|
| 26522 |
amit.gupta |
14 |
import java.util.stream.Collectors;
|
|
|
15 |
|
| 28653 |
amit.gupta |
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
| 26522 |
amit.gupta |
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 28170 |
tejbeer |
19 |
import org.springframework.stereotype.Component;
|
| 26522 |
amit.gupta |
20 |
|
| 28653 |
amit.gupta |
21 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 29261 |
amit.gupta |
22 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 26522 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
|
|
24 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 29327 |
tejbeer |
25 |
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
|
|
|
26 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
|
|
27 |
import com.spice.profitmandi.dao.entity.inventory.SaholicCIS;
|
| 26522 |
amit.gupta |
28 |
import com.spice.profitmandi.dao.entity.user.Cart;
|
| 28166 |
tejbeer |
29 |
import com.spice.profitmandi.dao.entity.user.CartLine;
|
| 26522 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.entity.user.User;
|
| 28653 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.model.CartItem;
|
|
|
32 |
import com.spice.profitmandi.dao.model.CartItemResponseModel;
|
|
|
33 |
import com.spice.profitmandi.dao.model.CartMessage;
|
|
|
34 |
import com.spice.profitmandi.dao.model.CartResponse;
|
| 29327 |
tejbeer |
35 |
import com.spice.profitmandi.dao.repository.catalog.FocusedModelRepository;
|
| 26522 |
amit.gupta |
36 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 29327 |
tejbeer |
38 |
import com.spice.profitmandi.dao.repository.cs.PartnerRegionRepository;
|
| 26522 |
amit.gupta |
39 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 29327 |
tejbeer |
40 |
import com.spice.profitmandi.dao.repository.dtr.RetailerBlockBrandsRepository;
|
| 26522 |
amit.gupta |
41 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
| 29327 |
tejbeer |
42 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 28166 |
tejbeer |
43 |
import com.spice.profitmandi.dao.repository.user.CartLineRepository;
|
| 26522 |
amit.gupta |
44 |
import com.spice.profitmandi.dao.repository.user.CartRepository;
|
|
|
45 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 29327 |
tejbeer |
46 |
import com.spice.profitmandi.service.NotificationService;
|
|
|
47 |
import com.spice.profitmandi.service.inventory.FocusedShortageModel;
|
| 28653 |
amit.gupta |
48 |
import com.spice.profitmandi.service.inventory.SaholicInventoryService;
|
| 29327 |
tejbeer |
49 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
| 26522 |
amit.gupta |
50 |
|
| 28170 |
tejbeer |
51 |
@Component
|
| 26522 |
amit.gupta |
52 |
public class CartServiceImpl implements CartService {
|
|
|
53 |
|
|
|
54 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
|
|
55 |
|
| 28653 |
amit.gupta |
56 |
private static final Logger logger = LogManager.getLogger(CartServiceImpl.class);
|
|
|
57 |
|
| 29404 |
tejbeer |
58 |
private static final Map<Integer, List<Integer>> focusedModelVariantMap = new HashMap<>();
|
|
|
59 |
|
| 26522 |
amit.gupta |
60 |
@Autowired
|
|
|
61 |
CartRepository cartRepository;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
| 28653 |
amit.gupta |
64 |
CartLineRepository cartLineRepository;
|
| 26522 |
amit.gupta |
65 |
|
|
|
66 |
@Autowired
|
|
|
67 |
FofoStoreRepository fofoStoreRepository;
|
|
|
68 |
|
|
|
69 |
@Autowired
|
|
|
70 |
TagListingRepository tagListingRepository;
|
|
|
71 |
|
|
|
72 |
@Autowired
|
|
|
73 |
UserRepository saholicUserRepository;
|
|
|
74 |
|
|
|
75 |
@Autowired
|
|
|
76 |
ItemRepository itemRepository;
|
|
|
77 |
|
|
|
78 |
@Autowired
|
| 28653 |
amit.gupta |
79 |
SaholicInventoryService saholicInventoryService;
|
| 28170 |
tejbeer |
80 |
|
| 28166 |
tejbeer |
81 |
@Autowired
|
| 28653 |
amit.gupta |
82 |
CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
| 26522 |
amit.gupta |
83 |
|
| 29327 |
tejbeer |
84 |
@Autowired
|
|
|
85 |
private FocusedModelRepository focusedModelRepository;
|
|
|
86 |
|
|
|
87 |
@Autowired
|
|
|
88 |
private PartnerRegionRepository partnerRegionRepository;
|
|
|
89 |
|
|
|
90 |
@Autowired
|
|
|
91 |
private TransactionService transactionService;
|
|
|
92 |
|
|
|
93 |
@Autowired
|
|
|
94 |
private RetailerBlockBrandsRepository retailerBlockBrandRepository;
|
|
|
95 |
|
|
|
96 |
@Autowired
|
|
|
97 |
private OrderRepository orderRepository;
|
|
|
98 |
|
|
|
99 |
@Autowired
|
|
|
100 |
private NotificationService notificationService;
|
|
|
101 |
|
| 26522 |
amit.gupta |
102 |
@Override
|
| 29327 |
tejbeer |
103 |
// Unused method
|
| 26522 |
amit.gupta |
104 |
public CartModel addToCart(int cartId, int itemId, int quantity, float sellingPrice) {
|
| 28653 |
amit.gupta |
105 |
CartLine cartItem = cartLineRepository.selectByCartItem(cartId, itemId);
|
| 26522 |
amit.gupta |
106 |
if (cartItem == null) {
|
|
|
107 |
this.createCartItem(cartId, itemId, quantity, sellingPrice);
|
|
|
108 |
} else {
|
|
|
109 |
cartItem.setQuantity(quantity);
|
|
|
110 |
cartItem.setActualPrice(sellingPrice);
|
|
|
111 |
}
|
|
|
112 |
return null;
|
|
|
113 |
}
|
| 29327 |
tejbeer |
114 |
|
| 26522 |
amit.gupta |
115 |
private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
|
| 28653 |
amit.gupta |
116 |
CartLine cartLine = new CartLine();
|
|
|
117 |
cartLine.setActualPrice(sellingPrice);
|
|
|
118 |
cartLine.setCartId(cartId);
|
|
|
119 |
cartLine.setQuantity(quantity);
|
| 26522 |
amit.gupta |
120 |
// TODO: Estmiate Logic
|
| 28653 |
amit.gupta |
121 |
cartLine.setEstimate(2);
|
|
|
122 |
cartLineRepository.persist(cartLine);
|
| 26522 |
amit.gupta |
123 |
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
@Override
|
| 28653 |
amit.gupta |
127 |
public boolean clearCart(int cartId) throws ProfitMandiBusinessException {
|
|
|
128 |
List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
|
|
|
129 |
cartLines.stream().forEach(cartLine -> cartLineRepository.delete(cartLine));
|
|
|
130 |
Cart cart = cartRepository.selectById(cartId);
|
| 26522 |
amit.gupta |
131 |
|
| 28653 |
amit.gupta |
132 |
cart.setUpdateTimestamp(LocalDateTime.now());
|
|
|
133 |
cart.setCheckoutTimestamp(null);
|
|
|
134 |
cart.setTotalPrice(0);
|
|
|
135 |
cart.setWalletAmount(0);
|
|
|
136 |
return true;
|
| 26522 |
amit.gupta |
137 |
}
|
|
|
138 |
|
|
|
139 |
@Override
|
|
|
140 |
public CartModel getCart(int cartId) {
|
|
|
141 |
// TODO Auto-generated method stub
|
|
|
142 |
return null;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
// As of now it only works for Fofo Partners need to be refactored later
|
|
|
146 |
private Map<Integer, Integer> getAvailabilityMap(User user, Set<Integer> itemIds) {
|
|
|
147 |
Map<Integer, Integer> itemAvailabilityMap = new HashMap<>();
|
|
|
148 |
List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
|
|
|
149 |
tags.stream().forEach(tagListing -> {
|
|
|
150 |
if (!tagListing.isActive() || tagListing.isHotDeals()) {
|
|
|
151 |
// show actual values here
|
|
|
152 |
} else {
|
|
|
153 |
itemAvailabilityMap.put(tagListing.getItemId(), 100);
|
|
|
154 |
}
|
|
|
155 |
});
|
|
|
156 |
return itemAvailabilityMap;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// As of now it only works for Fofo Partners need to be refactored later
|
|
|
160 |
private Map<Integer, Integer> getMinBuyQtyMap(User user, Set<Integer> itemIds) {
|
|
|
161 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
162 |
return items.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getCategoryId() == 10020 ? 1 : 10));
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// As of now it only works for Fofo Partners need to be refactored later
|
|
|
166 |
private Map<Integer, Float> getPriceMap(User user, Set<Integer> itemIds) {
|
|
|
167 |
List<TagListing> tags = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
|
|
|
168 |
return tags.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x.getSellingPrice()));
|
|
|
169 |
|
|
|
170 |
}
|
|
|
171 |
|
| 28166 |
tejbeer |
172 |
@Override
|
| 28653 |
amit.gupta |
173 |
public CartResponse getCartValidation(int cartId) throws ProfitMandiBusinessException {
|
|
|
174 |
/*
|
|
|
175 |
* # No need to validate duplicate items since there are only two ways # to add
|
|
|
176 |
* items to a cart and both of them check whether the item being # added is a
|
|
|
177 |
* duplicate of an already existing item.
|
|
|
178 |
*/
|
|
|
179 |
|
|
|
180 |
Cart cart = cartRepository.selectById(cartId);
|
|
|
181 |
List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
|
|
|
182 |
|
|
|
183 |
User saholicUser = saholicUserRepository.selectByCartId(cartId);
|
|
|
184 |
|
|
|
185 |
int totalQty = 0;
|
|
|
186 |
double totalAmount = 0;
|
|
|
187 |
CartResponse cartResponse = new CartResponse();
|
|
|
188 |
List<CartMessage> cartMessages = new ArrayList<>();
|
|
|
189 |
List<CartItemResponseModel> cartItemModels = new ArrayList<>();
|
|
|
190 |
cartResponse.setCartMessages(cartMessages);
|
|
|
191 |
cartResponse.setCartItems(cartItemModels);
|
|
|
192 |
|
|
|
193 |
int cartMessageChanged = 0;
|
|
|
194 |
int cartMessageOOS = 0;
|
|
|
195 |
int cartMessageUndeliverable = 0;
|
|
|
196 |
|
|
|
197 |
Set<Integer> itemIds = cartLines.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
198 |
logger.info("iteme ids -- {}", itemIds);
|
|
|
199 |
List<TagListing> tagListings = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds);
|
|
|
200 |
Map<Integer, TagListing> tagListingsMap = tagListings.stream()
|
|
|
201 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
|
|
202 |
|
|
|
203 |
cart.setTotalPrice(0);
|
|
|
204 |
int nonAccessoryQuantity = 0;
|
|
|
205 |
for (CartLine cartLine : cartLines) {
|
|
|
206 |
boolean itemQuantityChanged = false;
|
|
|
207 |
CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();
|
|
|
208 |
int oldEstimate = cartLine.getEstimate();
|
|
|
209 |
int itemId = cartLine.getItemId();
|
|
|
210 |
Item item = null;
|
|
|
211 |
try {
|
|
|
212 |
item = itemRepository.selectById(itemId);
|
|
|
213 |
cartItemResponseModel.setTitle(item.getItemDescriptionNoColor());
|
|
|
214 |
} catch (ProfitMandiBusinessException e) {
|
|
|
215 |
e.printStackTrace();
|
|
|
216 |
logger.info("Error finding item with id {}", itemId);
|
|
|
217 |
cartLineRepository.delete(cartLine);
|
|
|
218 |
continue;
|
|
|
219 |
}
|
|
|
220 |
cartItemResponseModel.setItemId(itemId);
|
|
|
221 |
cartItemResponseModel.setQuantity(0);
|
|
|
222 |
cartItemResponseModel.setColor(item.getColor());
|
|
|
223 |
List<CartItemMessage> cartItemMessages = new ArrayList<>();
|
|
|
224 |
cartItemResponseModel.setCartItemMessages(cartItemMessages);
|
|
|
225 |
cartItemResponseModel.setCatalogItemId(item.getCatalogItemId());
|
|
|
226 |
cartItemResponseModel.setMinBuyQuantity(1);
|
|
|
227 |
cartItemResponseModel.setQuantityStep(1);
|
|
|
228 |
cartItemResponseModel.setMaxQuantity(0);
|
|
|
229 |
|
|
|
230 |
/*
|
|
|
231 |
* int netAvailability = itemAvailabilityMap.get(itemId).stream()
|
|
|
232 |
* .collect(Collectors.summingInt(x -> x.getNetavailability()));
|
|
|
233 |
*/
|
| 30836 |
tejbeer |
234 |
int availability = 1000;
|
| 28653 |
amit.gupta |
235 |
if (availability < cartLine.getQuantity()) {
|
|
|
236 |
cartLine.setQuantity(availability);
|
|
|
237 |
itemQuantityChanged = true;
|
|
|
238 |
}
|
|
|
239 |
cartItemResponseModel.setMaxQuantity(Math.max(availability, 100));
|
|
|
240 |
if (availability < cartItemResponseModel.getMinBuyQuantity()) {
|
|
|
241 |
cartItemResponseModel.setMinBuyQuantity(availability);
|
|
|
242 |
}
|
|
|
243 |
if (cartLine.getQuantity() < cartItemResponseModel.getMinBuyQuantity()) {
|
|
|
244 |
itemQuantityChanged = true;
|
|
|
245 |
cartLine.setQuantity(cartItemResponseModel.getMinBuyQuantity());
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
if (cartLine.getQuantity() > cartItemResponseModel.getMaxQuantity()) {
|
|
|
249 |
itemQuantityChanged = true;
|
|
|
250 |
cartLine.setQuantity(cartItemResponseModel.getMaxQuantity());
|
|
|
251 |
}
|
|
|
252 |
cartLine.setActualPrice(tagListingsMap.get(itemId).getSellingPrice());
|
|
|
253 |
if (item.getCategoryId() == 10006 || item.getCategoryId() == 10010) {
|
|
|
254 |
nonAccessoryQuantity += cartItemResponseModel.getQuantity();
|
|
|
255 |
cartItemResponseModel.setCategoryName("mobile");
|
|
|
256 |
}
|
|
|
257 |
cartItemResponseModel.setSellingPrice(cartLine.getActualPrice());
|
|
|
258 |
|
|
|
259 |
if (availability > 0) {
|
|
|
260 |
cart.setTotalPrice(cart.getTotalPrice() + (cartLine.getActualPrice() * cartLine.getQuantity()));
|
|
|
261 |
cartItemResponseModel.setQuantity(cartLine.getQuantity());
|
|
|
262 |
cartItemResponseModel.setEstimate(2);
|
|
|
263 |
if (itemQuantityChanged) {
|
|
|
264 |
cartMessageChanged += 1;
|
|
|
265 |
if (oldEstimate != cartItemResponseModel.getEstimate()) {
|
|
|
266 |
cartLine.setEstimate(cartItemResponseModel.getEstimate());
|
|
|
267 |
cart.setUpdateTimestamp(LocalDateTime.now());
|
|
|
268 |
}
|
|
|
269 |
}
|
| 29497 |
amit.gupta |
270 |
totalAmount += cartLine.getActualPrice() * cartLine.getQuantity();
|
| 28653 |
amit.gupta |
271 |
} else {
|
|
|
272 |
cartItemResponseModel.setQuantity(0);
|
|
|
273 |
cartMessageOOS += 1;
|
|
|
274 |
CartItemMessage cartItemMessage = new CartItemMessage();
|
|
|
275 |
cartItemMessage.setType("danger");
|
|
|
276 |
cartItemMessage.setMessageText("Out of Stock");
|
|
|
277 |
cartItemMessages.add(cartItemMessage);
|
|
|
278 |
cartLineRepository.delete(cartLine);
|
|
|
279 |
}
|
|
|
280 |
totalQty += cartItemResponseModel.getQuantity();
|
|
|
281 |
|
|
|
282 |
if (cartItemMessages.size() > 0) {
|
|
|
283 |
cartResponse.getCartItems().add(0, cartItemResponseModel);
|
|
|
284 |
} else {
|
|
|
285 |
cartResponse.getCartItems().add(cartItemResponseModel);
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
if (cart.getCheckoutTimestamp() != null) {
|
|
|
289 |
if (cart.getUpdateTimestamp().isBefore(cart.getCheckoutTimestamp())) {
|
|
|
290 |
cart.setCheckoutTimestamp(null);
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
cartResponse.setTotalQty(totalQty);
|
|
|
294 |
cartResponse.setTotalAmount(totalAmount);
|
|
|
295 |
cartResponse.setNonAccessoryQuantity(nonAccessoryQuantity);
|
|
|
296 |
cartResponse.setShippingCharge(0);
|
|
|
297 |
cartResponse.setCartMessageChanged(cartMessageChanged);
|
|
|
298 |
cartResponse.setCartMessageOOS(cartMessageOOS);
|
|
|
299 |
cartResponse.setCartMessageUndeliverable(cartMessageUndeliverable);
|
|
|
300 |
cartResponse.setCod(false);
|
|
|
301 |
return cartResponse;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
@Override
|
|
|
305 |
public void addItemsToCart(int cartId, List<CartItem> cartItems) throws ProfitMandiBusinessException {
|
|
|
306 |
logger.info("cart items {}", cartItems);
|
|
|
307 |
cartItems = cartItems.stream().filter(x -> x.getQuantity() > 0).collect(Collectors.toList());
|
|
|
308 |
Map<Integer, Integer> itemQuantityMap = cartItems.stream()
|
|
|
309 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
|
|
|
310 |
List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
|
|
|
311 |
// Delete cartLines with 0 values
|
|
|
312 |
for (CartLine cartLine : cartLines) {
|
|
|
313 |
Integer quantity = itemQuantityMap.get(cartLine.getItemId());
|
|
|
314 |
if (quantity == null || quantity.intValue() == 0) {
|
|
|
315 |
cartLineRepository.delete(cartLine);
|
|
|
316 |
} else {
|
|
|
317 |
cartLine.setQuantity(itemQuantityMap.get(cartLine.getItemId()));
|
|
|
318 |
cartLine.setUpdateTimestapm(LocalDateTime.now());
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
for (CartItem cartItem : cartItems) {
|
|
|
322 |
if (cartLineRepository.selectByCartItem(cartId, cartItem.getItemId()) == null) {
|
|
|
323 |
CartLine cartLineNew = new CartLine();
|
|
|
324 |
cartLineNew.setItemId(cartItem.getItemId());
|
|
|
325 |
cartLineNew.setQuantity(cartItem.getQuantity());
|
|
|
326 |
cartLineNew.setCartId(cartId);
|
|
|
327 |
cartLineNew.setCreateTimestamp(LocalDateTime.now());
|
|
|
328 |
cartLineRepository.persist(cartLineNew);
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
@Override
|
|
|
335 |
public void addAddressToCart(int cartId, long addressId) throws ProfitMandiBusinessException {
|
|
|
336 |
Cart cart = cartRepository.selectById(cartId);
|
|
|
337 |
cart.setAddressId((int) addressId);
|
|
|
338 |
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
@Override
|
| 28166 |
tejbeer |
342 |
public void addShoppingBag(int cartId, long qty) {
|
|
|
343 |
CartLine cl = new CartLine();
|
| 29261 |
amit.gupta |
344 |
cl.setItemId(ProfitMandiConstants.ITEM_CARRY_BAG);
|
| 28166 |
tejbeer |
345 |
cl.setQuantity((int) qty);
|
|
|
346 |
cl.setCartId(cartId);
|
|
|
347 |
cl.setCreateTimestamp(LocalDateTime.now());
|
|
|
348 |
cl.setDataProtectionAmount(0);
|
|
|
349 |
cl.setEstimate(0);
|
| 29261 |
amit.gupta |
350 |
cl.setInsuranceAmount(0f);
|
| 28166 |
tejbeer |
351 |
cl.setDataProtectionInsurer(0);
|
| 29261 |
amit.gupta |
352 |
cl.setActualPrice(0.01f);
|
| 28166 |
tejbeer |
353 |
cartLineRepository.persist(cl);
|
|
|
354 |
|
|
|
355 |
}
|
|
|
356 |
|
| 28653 |
amit.gupta |
357 |
@Override
|
|
|
358 |
public CartModel removeFromCart(int cartId, int itemId) {
|
|
|
359 |
// TODO Auto-generated method stub
|
|
|
360 |
return null;
|
|
|
361 |
}
|
|
|
362 |
|
| 29327 |
tejbeer |
363 |
@Override
|
|
|
364 |
public List<FocusedShortageModel> focusedModelShortageValidation(int fofoId,
|
|
|
365 |
Map<Integer, Integer> catalogCartQtyMap) throws ProfitMandiBusinessException {
|
|
|
366 |
|
|
|
367 |
logger.info("fofoId {}", fofoId);
|
|
|
368 |
|
| 29404 |
tejbeer |
369 |
focusedModelVariantMap.put(1022771, Arrays.asList(1022771));
|
|
|
370 |
focusedModelVariantMap.put(1023025, Arrays.asList(1022975, 1022945));
|
|
|
371 |
|
|
|
372 |
focusedModelVariantMap.put(1023027, Arrays.asList(1022706));
|
|
|
373 |
|
|
|
374 |
focusedModelVariantMap.put(1022976, Arrays.asList(1022744));
|
|
|
375 |
focusedModelVariantMap.put(1023045, Arrays.asList(1022768));
|
|
|
376 |
|
|
|
377 |
focusedModelVariantMap.put(1023066, Arrays.asList(1022858));
|
|
|
378 |
|
| 29327 |
tejbeer |
379 |
Map<Integer, Long> inStockItemModel = new HashMap<>();
|
|
|
380 |
Map<Integer, Long> itempendingIndent = new HashMap<>();
|
|
|
381 |
Map<Integer, Long> grnItemPendingOrders = new HashMap<>();
|
|
|
382 |
|
|
|
383 |
Map<Integer, Integer> focusedModelMap = null;
|
|
|
384 |
|
|
|
385 |
inStockItemModel = currentInventorySnapshotRepository.selectSumInMobileStockGroupByCatalog(fofoId).stream()
|
|
|
386 |
.collect(Collectors.toMap(x -> x.getCatalogItemId(), x -> x.getQty()));
|
|
|
387 |
|
|
|
388 |
itempendingIndent = transactionService.getInTransitOrders(fofoId).stream()
|
|
|
389 |
.collect(Collectors.groupingBy(x -> x.getLineItem().getItem().getCatalogItemId(),
|
|
|
390 |
Collectors.summingLong(x -> (long) x.getLineItem().getQuantity())));
|
|
|
391 |
|
|
|
392 |
grnItemPendingOrders = orderRepository.selectPendingGrnOrders(fofoId).stream()
|
|
|
393 |
.collect(Collectors.groupingBy(x -> x.getLineItem().getItem().getCatalogItemId(),
|
|
|
394 |
Collectors.summingLong(x -> (long) x.getLineItem().getQuantity())));
|
|
|
395 |
List<PartnerRegion> partnerRegions = partnerRegionRepository.selectByfofoId(fofoId);
|
|
|
396 |
|
|
|
397 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
398 |
|
|
|
399 |
List<String> retailerBlockBrands = retailerBlockBrandRepository.selectAllByRetailer(fofoId).stream()
|
|
|
400 |
.map(x -> x.getBlockBrands()).collect(Collectors.toList());
|
|
|
401 |
|
|
|
402 |
if (partnerRegions.size() > 0) {
|
|
|
403 |
focusedModelMap = focusedModelRepository
|
|
|
404 |
.selectAllByRegionIds(
|
|
|
405 |
partnerRegions.stream().map(x -> x.getRegionId()).collect(Collectors.toList()))
|
|
|
406 |
.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> x.getMinimumQty()));
|
|
|
407 |
} else {
|
|
|
408 |
focusedModelMap = new HashMap<>();
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
Map<Integer, List<SaholicCIS>> itemAvailabilityMap = saholicInventoryService.getSaholicStock()
|
|
|
412 |
.get(fofoStore.getWarehouseId());
|
|
|
413 |
|
|
|
414 |
Map<Integer, Integer> catalogItemAailabilityMap = new HashMap<>();
|
|
|
415 |
|
|
|
416 |
for (Entry<Integer, Integer> fm : focusedModelMap.entrySet()) {
|
|
|
417 |
|
|
|
418 |
List<Item> items = itemRepository.selectAllByCatalogItemId(fm.getKey());
|
|
|
419 |
|
|
|
420 |
int allColorNetAvailability = 0;
|
|
|
421 |
for (Item item : items) {
|
|
|
422 |
if (retailerBlockBrands.contains(item.getBrand()))
|
|
|
423 |
continue;
|
|
|
424 |
List<SaholicCIS> currentAvailability = itemAvailabilityMap.get(item.getId());
|
|
|
425 |
|
|
|
426 |
if (currentAvailability != null) {
|
|
|
427 |
allColorNetAvailability += currentAvailability.stream()
|
|
|
428 |
.collect(Collectors.summingInt(SaholicCIS::getNetavailability));
|
|
|
429 |
}
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
catalogItemAailabilityMap.put(fm.getKey(), allColorNetAvailability);
|
|
|
433 |
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
List<FocusedShortageModel> focusedModelShortageList = new ArrayList<>();
|
|
|
437 |
|
|
|
438 |
for (Entry<Integer, Integer> fm : focusedModelMap.entrySet()) {
|
|
|
439 |
|
|
|
440 |
logger.info("fm {}", fm);
|
|
|
441 |
|
|
|
442 |
long overallStock = (inStockItemModel.get(fm.getKey()) == null ? 0 : inStockItemModel.get(fm.getKey()))
|
|
|
443 |
+ (itempendingIndent.get(fm.getKey()) == null ? 0 : itempendingIndent.get(fm.getKey()))
|
|
|
444 |
+ (grnItemPendingOrders.get(fm.getKey()) == null ? 0 : grnItemPendingOrders.get(fm.getKey()));
|
|
|
445 |
logger.info("overallStock {}", overallStock);
|
|
|
446 |
|
|
|
447 |
// Blocked brands available quantity is discussed to zero
|
|
|
448 |
long warehouseStock = catalogItemAailabilityMap.get(fm.getKey()) == null ? 0
|
|
|
449 |
: catalogItemAailabilityMap.get(fm.getKey());
|
|
|
450 |
logger.info("warehouseStock {}", warehouseStock);
|
|
|
451 |
|
|
|
452 |
long cartValue = catalogCartQtyMap.get(fm.getKey()) == null ? 0 : catalogCartQtyMap.get(fm.getKey());
|
|
|
453 |
|
|
|
454 |
logger.info("cartValue" + cartValue);
|
|
|
455 |
long totalStock = overallStock + cartValue;
|
|
|
456 |
logger.info("totalStock" + totalStock);
|
|
|
457 |
|
|
|
458 |
long shortQty = Math.min(fm.getValue() - totalStock, warehouseStock);
|
|
|
459 |
logger.info("shortQty {}", shortQty);
|
|
|
460 |
|
|
|
461 |
if (shortQty > 0) {
|
| 29404 |
tejbeer |
462 |
if (focusedModelVariantMap.get(fm.getKey()) != null) {
|
|
|
463 |
List<Integer> variantCatalogIds = focusedModelVariantMap.get(fm.getKey());
|
|
|
464 |
logger.info("variantCatalogIds {}", variantCatalogIds);
|
| 29327 |
tejbeer |
465 |
|
| 29404 |
tejbeer |
466 |
int variantAvailability = 0;
|
|
|
467 |
for (Integer vc : variantCatalogIds) {
|
|
|
468 |
long variantOverallStock = (inStockItemModel.get(vc) == null ? 0 : inStockItemModel.get(vc))
|
|
|
469 |
+ (itempendingIndent.get(vc) == null ? 0 : itempendingIndent.get(vc))
|
|
|
470 |
+ (grnItemPendingOrders.get(vc) == null ? 0 : grnItemPendingOrders.get(vc));
|
|
|
471 |
logger.info("variantOverallStock {}", variantOverallStock);
|
|
|
472 |
|
|
|
473 |
// Blocked brands available quantity is discussed to zero
|
|
|
474 |
long variantWarehouseStock = catalogItemAailabilityMap.get(vc) == null ? 0
|
|
|
475 |
: catalogItemAailabilityMap.get(vc);
|
|
|
476 |
logger.info("variantWarehouseStock {}", variantWarehouseStock);
|
|
|
477 |
|
|
|
478 |
long variantCartValue = catalogCartQtyMap.get(vc) == null ? 0 : catalogCartQtyMap.get(vc);
|
|
|
479 |
|
|
|
480 |
logger.info("variantCartValue" + variantCartValue);
|
|
|
481 |
long variantTotalStock = variantOverallStock + variantCartValue;
|
|
|
482 |
logger.info("variantTotalStock" + variantTotalStock);
|
|
|
483 |
|
|
|
484 |
variantAvailability += (int) variantTotalStock;
|
|
|
485 |
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
logger.info("variantAvailability {}", variantAvailability);
|
|
|
489 |
|
|
|
490 |
shortQty = shortQty - variantAvailability;
|
|
|
491 |
|
|
|
492 |
}
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
logger.info("shortQty" + shortQty);
|
|
|
496 |
|
|
|
497 |
if (shortQty > 0) {
|
|
|
498 |
|
| 29327 |
tejbeer |
499 |
Map<Integer, Integer> requiredItemQty = new HashMap<>();
|
|
|
500 |
|
|
|
501 |
List<Item> it = itemRepository.selectAllByCatalogItemId(fm.getKey());
|
|
|
502 |
FocusedShortageModel fms = new FocusedShortageModel();
|
|
|
503 |
fms.setShortageQty((int) shortQty);
|
|
|
504 |
fms.setCatalogId(fm.getKey());
|
|
|
505 |
fms.setAvailabitiy(fm.getValue());
|
|
|
506 |
fms.setInStock(overallStock);
|
|
|
507 |
|
|
|
508 |
fms.setItemName(it.get(0).getBrand() + it.get(0).getModelNumber() + it.get(0).getModelName());
|
|
|
509 |
|
|
|
510 |
Map<Integer, Integer> itemQtyMap = new HashMap<>();
|
|
|
511 |
|
|
|
512 |
for (Item i : it) {
|
|
|
513 |
|
|
|
514 |
List<SaholicCIS> ca = itemAvailabilityMap.get(i.getId());
|
|
|
515 |
|
|
|
516 |
if (ca != null) {
|
|
|
517 |
|
|
|
518 |
itemQtyMap.put(i.getId(),
|
|
|
519 |
ca.stream().collect(Collectors.summingInt(SaholicCIS::getNetavailability)));
|
|
|
520 |
}
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
itemQtyMap = itemQtyMap.entrySet().stream()
|
|
|
524 |
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).collect(Collectors
|
|
|
525 |
.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
|
|
|
526 |
|
|
|
527 |
for (long i = shortQty; i > 0; i--) {
|
|
|
528 |
if (shortQty == 0) {
|
|
|
529 |
break;
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
for (Entry<Integer, Integer> itemQty : itemQtyMap.entrySet()) {
|
|
|
533 |
|
|
|
534 |
int availbleQty = itemQty.getValue();
|
|
|
535 |
if (availbleQty == 0) {
|
|
|
536 |
break;
|
|
|
537 |
}
|
|
|
538 |
if (requiredItemQty.get(itemQty.getKey()) == null) {
|
|
|
539 |
requiredItemQty.put(itemQty.getKey(), 1);
|
|
|
540 |
availbleQty = availbleQty - 1;
|
|
|
541 |
if (availbleQty != 0) {
|
|
|
542 |
itemQtyMap.put(itemQty.getKey(), availbleQty);
|
|
|
543 |
}
|
|
|
544 |
shortQty -= 1;
|
|
|
545 |
} else {
|
|
|
546 |
|
|
|
547 |
int qty = requiredItemQty.get(itemQty.getKey());
|
|
|
548 |
|
|
|
549 |
requiredItemQty.put(itemQty.getKey(), qty + 1);
|
|
|
550 |
|
|
|
551 |
availbleQty = availbleQty - 1;
|
|
|
552 |
if (availbleQty != 0) {
|
|
|
553 |
itemQtyMap.put(itemQty.getKey(), availbleQty);
|
|
|
554 |
}
|
|
|
555 |
shortQty -= 1;
|
|
|
556 |
}
|
|
|
557 |
|
|
|
558 |
if (shortQty == 0) {
|
|
|
559 |
break;
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
}
|
|
|
565 |
|
|
|
566 |
Set<CartItem> cartItems = new HashSet<>();
|
|
|
567 |
for (Entry<Integer, Integer> itemQtySet : requiredItemQty.entrySet()) {
|
|
|
568 |
|
|
|
569 |
CartItem ci = new CartItem();
|
|
|
570 |
Item item = itemRepository.selectById(itemQtySet.getKey());
|
|
|
571 |
TagListing tl = tagListingRepository.selectByItemId(item.getId());
|
|
|
572 |
ci.setItemId(item.getId());
|
|
|
573 |
ci.setQuantity(itemQtySet.getValue());
|
|
|
574 |
ci.setSellingPrice(tl.getSellingPrice());
|
|
|
575 |
cartItems.add(ci);
|
|
|
576 |
|
|
|
577 |
}
|
|
|
578 |
fms.setCartItems(cartItems);
|
|
|
579 |
|
|
|
580 |
// fms.setItemQtyAvailability(requiredItemQty);
|
|
|
581 |
|
|
|
582 |
focusedModelShortageList.add(fms);
|
|
|
583 |
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
}
|
|
|
587 |
|
|
|
588 |
logger.info("focusedModelShortageList {}", focusedModelShortageList);
|
|
|
589 |
return focusedModelShortageList;
|
|
|
590 |
}
|
|
|
591 |
|
| 26522 |
amit.gupta |
592 |
}
|