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