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