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