| 23405 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 23785 |
amit.gupta |
3 |
import java.util.ArrayList;
|
| 23405 |
amit.gupta |
4 |
import java.util.Arrays;
|
| 23796 |
amit.gupta |
5 |
import java.util.Collections;
|
|
|
6 |
import java.util.Comparator;
|
| 23785 |
amit.gupta |
7 |
import java.util.DoubleSummaryStatistics;
|
| 23796 |
amit.gupta |
8 |
import java.util.HashMap;
|
| 23785 |
amit.gupta |
9 |
import java.util.Iterator;
|
| 23405 |
amit.gupta |
10 |
import java.util.List;
|
|
|
11 |
import java.util.Map;
|
| 24231 |
amit.gupta |
12 |
import java.util.Optional;
|
| 23785 |
amit.gupta |
13 |
import java.util.Set;
|
| 23405 |
amit.gupta |
14 |
import java.util.stream.Collectors;
|
|
|
15 |
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
|
| 24231 |
amit.gupta |
18 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 23717 |
amit.gupta |
19 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
20 |
import org.apache.logging.log4j.Logger;
|
| 23796 |
amit.gupta |
21 |
import org.apache.thrift.TException;
|
| 23779 |
amit.gupta |
22 |
import org.json.JSONObject;
|
| 23405 |
amit.gupta |
23 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 24231 |
amit.gupta |
24 |
import org.springframework.core.io.ByteArrayResource;
|
|
|
25 |
import org.springframework.http.HttpHeaders;
|
|
|
26 |
import org.springframework.http.HttpStatus;
|
|
|
27 |
import org.springframework.http.ResponseEntity;
|
| 23405 |
amit.gupta |
28 |
import org.springframework.stereotype.Controller;
|
|
|
29 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
30 |
import org.springframework.ui.Model;
|
| 23855 |
amit.gupta |
31 |
import org.springframework.web.bind.annotation.PathVariable;
|
| 23405 |
amit.gupta |
32 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
33 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
34 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 23779 |
amit.gupta |
35 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 23405 |
amit.gupta |
36 |
|
| 23785 |
amit.gupta |
37 |
import com.spice.profitmandi.common.enumuration.CounterSize;
|
| 23405 |
amit.gupta |
38 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 23785 |
amit.gupta |
39 |
import com.spice.profitmandi.common.model.BrandPerformance;
|
| 24231 |
amit.gupta |
40 |
import com.spice.profitmandi.common.model.CatalogListingModel;
|
| 23785 |
amit.gupta |
41 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 23786 |
amit.gupta |
42 |
import com.spice.profitmandi.common.model.ItemIdAvailability;
|
| 23779 |
amit.gupta |
43 |
import com.spice.profitmandi.common.model.StockAllocationModel;
|
| 24231 |
amit.gupta |
44 |
import com.spice.profitmandi.common.util.FileUtil;
|
| 23785 |
amit.gupta |
45 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 23405 |
amit.gupta |
46 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
|
|
47 |
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
|
| 23785 |
amit.gupta |
48 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 23796 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
50 |
import com.spice.profitmandi.dao.entity.user.User;
|
| 23405 |
amit.gupta |
51 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
52 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23779 |
amit.gupta |
53 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 23405 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
| 23796 |
amit.gupta |
55 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
|
|
56 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
57 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
58 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 23798 |
amit.gupta |
59 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 23779 |
amit.gupta |
60 |
import com.spice.profitmandi.service.inventory.StockAllocationService;
|
|
|
61 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 23796 |
amit.gupta |
62 |
import com.spice.profitmandi.thrift.clients.PaymentClient;
|
|
|
63 |
import com.spice.profitmandi.thrift.clients.TransactionClient;
|
|
|
64 |
import com.spice.profitmandi.thrift.clients.UserClient;
|
| 23405 |
amit.gupta |
65 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
66 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
67 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
68 |
|
| 23796 |
amit.gupta |
69 |
import in.shop2020.logistics.PickUpType;
|
|
|
70 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
71 |
import in.shop2020.model.v1.order.OrderSource;
|
|
|
72 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
73 |
import in.shop2020.model.v1.order.OrderType;
|
|
|
74 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
75 |
import in.shop2020.model.v1.order.TransactionService;
|
|
|
76 |
import in.shop2020.model.v1.order.TransactionStatus;
|
|
|
77 |
import in.shop2020.model.v1.user.ItemPriceQuantity;
|
|
|
78 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
|
|
79 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
|
|
80 |
import in.shop2020.payments.Attribute;
|
|
|
81 |
import in.shop2020.payments.PaymentException;
|
|
|
82 |
import in.shop2020.payments.PaymentStatus;
|
|
|
83 |
|
| 23405 |
amit.gupta |
84 |
@Controller
|
|
|
85 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
86 |
public class IndentController {
|
|
|
87 |
|
| 23568 |
govind |
88 |
private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
|
| 23796 |
amit.gupta |
89 |
private static final int WALLET_GATEWAY_ID = 8;
|
| 23405 |
amit.gupta |
90 |
|
| 23796 |
amit.gupta |
91 |
private final List<OrderStatus> validOrderStatusList = Arrays.asList(OrderStatus.ACCEPTED,
|
|
|
92 |
OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH,
|
|
|
93 |
OrderStatus.DELIVERY_SUCCESS, OrderStatus.SHIPPED_TO_DESTINATION_CITY, OrderStatus.SHIPPED_TO_LOGST,
|
|
|
94 |
OrderStatus.REACHED_DESTINATION_CITY);
|
|
|
95 |
|
| 24231 |
amit.gupta |
96 |
private final List<OrderStatus> partnerPendingOrderList = Arrays.asList(OrderStatus.ACCEPTED,
|
|
|
97 |
OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH,
|
|
|
98 |
OrderStatus.SHIPPED_TO_DESTINATION_CITY, OrderStatus.SHIPPED_TO_LOGST,
|
|
|
99 |
OrderStatus.REACHED_DESTINATION_CITY);
|
|
|
100 |
|
| 23405 |
amit.gupta |
101 |
@Autowired
|
|
|
102 |
private CookiesProcessor cookiesProcessor;
|
| 24231 |
amit.gupta |
103 |
|
| 23796 |
amit.gupta |
104 |
@Autowired
|
|
|
105 |
private UserWalletRepository userWalletRepository;
|
| 23785 |
amit.gupta |
106 |
|
| 23779 |
amit.gupta |
107 |
@Autowired
|
| 23796 |
amit.gupta |
108 |
private UserRepository userRepository;
|
|
|
109 |
|
|
|
110 |
@Autowired
|
| 23779 |
amit.gupta |
111 |
FofoStoreRepository fofoStoreRepository;
|
| 23405 |
amit.gupta |
112 |
|
|
|
113 |
@Autowired
|
| 23796 |
amit.gupta |
114 |
private OrderRepository orderRepository;
|
|
|
115 |
|
|
|
116 |
@Autowired
|
| 23405 |
amit.gupta |
117 |
private ItemRepository itemRepository;
|
|
|
118 |
|
|
|
119 |
@Autowired
|
| 23779 |
amit.gupta |
120 |
private StockAllocationService stockAllocationService;
|
|
|
121 |
|
|
|
122 |
@Autowired
|
|
|
123 |
private RetailerService retailerService;
|
| 23405 |
amit.gupta |
124 |
|
|
|
125 |
@Autowired
|
|
|
126 |
private TagListingRepository tagListingRepository;
|
|
|
127 |
|
|
|
128 |
@Autowired
|
|
|
129 |
private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
|
|
130 |
|
|
|
131 |
@Autowired
|
| 23796 |
amit.gupta |
132 |
private PurchaseRepository purchaseRepository;
|
|
|
133 |
|
|
|
134 |
@Autowired
|
| 23405 |
amit.gupta |
135 |
private MVCResponseSender mvcResponseSender;
|
|
|
136 |
|
| 23785 |
amit.gupta |
137 |
@Autowired
|
|
|
138 |
RoleManager roleManager;
|
| 23405 |
amit.gupta |
139 |
|
| 23779 |
amit.gupta |
140 |
@RequestMapping(value = "/open-indent/save", method = RequestMethod.POST)
|
| 23785 |
amit.gupta |
141 |
public String saveOpenIndent(HttpServletRequest request, Model model,
|
| 23786 |
amit.gupta |
142 |
@RequestBody List<StockAllocationModel> stockAllocationModelList, @RequestParam int fofoId,
|
|
|
143 |
@RequestParam(defaultValue = "TEN_LAC") CounterSize counterSize) throws Exception {
|
| 23405 |
amit.gupta |
144 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 23779 |
amit.gupta |
145 |
boolean response = false;
|
| 23785 |
amit.gupta |
146 |
if (fofoId > 0) {
|
|
|
147 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
|
| 23786 |
amit.gupta |
148 |
stockAllocationModelList.forEach(x -> {
|
|
|
149 |
x.setFofoId(fofoId);
|
|
|
150 |
x.setCounterSize(fs.getCounterSize());
|
|
|
151 |
});
|
|
|
152 |
} else {
|
|
|
153 |
stockAllocationModelList.forEach(x -> {
|
|
|
154 |
x.setFofoId(fofoId);
|
|
|
155 |
x.setCounterSize(counterSize);
|
|
|
156 |
});
|
| 23785 |
amit.gupta |
157 |
}
|
|
|
158 |
if (roleManager.isAdmin(loginDetails.getRoleIds())) {
|
|
|
159 |
response = stockAllocationService.addToAllocation(stockAllocationModelList);
|
|
|
160 |
model.addAttribute("response", mvcResponseSender.createResponseString(response));
|
|
|
161 |
}
|
| 23405 |
amit.gupta |
162 |
return "response";
|
|
|
163 |
}
|
|
|
164 |
|
| 24231 |
amit.gupta |
165 |
@RequestMapping(value = "/indent/download", method = RequestMethod.GET)
|
|
|
166 |
public ResponseEntity<ByteArrayResource> downloadIndent(HttpServletRequest request, Model model,
|
|
|
167 |
@RequestParam(required = false, defaultValue = "0") int fofoId,
|
|
|
168 |
@RequestParam(defaultValue = "TEN_LAC") CounterSize counterSize) throws Exception {
|
|
|
169 |
Set<Integer> roleIds = cookiesProcessor.getCookiesObject(request).getRoleIds();
|
|
|
170 |
LOGGER.info("Counter size is {}", counterSize);
|
|
|
171 |
LOGGER.info("Fofo Id is {}", fofoId);
|
|
|
172 |
if (roleManager.isAdmin(roleIds)) {
|
|
|
173 |
List<StockAllocationModel> stockAllocationList = stockAllocationService.getStockAllocation(counterSize,
|
|
|
174 |
true);
|
|
|
175 |
Map<Integer, StockAllocationModel> modelStockAllocationMap = stockAllocationList.stream()
|
|
|
176 |
.collect(Collectors.toMap(x -> x.getCatalogId(), x -> x));
|
|
|
177 |
List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
|
|
|
178 |
.collect(Collectors.toList());
|
|
|
179 |
Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
|
| 23415 |
amit.gupta |
180 |
|
| 24231 |
amit.gupta |
181 |
Map<String, CatalogListingModel> catalogListingMap = new HashMap<>();
|
|
|
182 |
List<TagListing> tagListings = tagListingRepository.selectAll(true);
|
|
|
183 |
Iterator<TagListing> iterator = tagListings.iterator();
|
|
|
184 |
Map<Integer, List<CurrentInventorySnapshot>> itemListSnapshot = new HashMap<>();
|
| 23405 |
amit.gupta |
185 |
|
| 24231 |
amit.gupta |
186 |
currentInventorySnapshotRepository.selectAll(Optional.of(true)).stream().forEach(x -> {
|
|
|
187 |
if (!itemListSnapshot.containsKey(x.getItemId())) {
|
|
|
188 |
itemListSnapshot.put(x.getItemId(), new ArrayList<>());
|
|
|
189 |
}
|
|
|
190 |
itemListSnapshot.get(x.getItemId()).add(x);
|
|
|
191 |
});
|
| 23779 |
amit.gupta |
192 |
|
| 24231 |
amit.gupta |
193 |
List<Order> inTransitOrders = orderRepository.selectOrders(fofoIds, partnerPendingOrderList);
|
|
|
194 |
Map<String, Integer> itemsInTransit = inTransitOrders.stream()
|
|
|
195 |
.collect(Collectors.groupingBy(
|
|
|
196 |
x -> new String(x.getLineItem().getItemId() + "-" + x.getRetailerId()),
|
|
|
197 |
Collectors.summingInt(x -> x.getLineItem().getQuantity())));
|
| 23779 |
amit.gupta |
198 |
|
| 24231 |
amit.gupta |
199 |
while (iterator.hasNext()) {
|
|
|
200 |
TagListing tagListing = iterator.next();
|
|
|
201 |
Item item = itemRepository.selectById(tagListing.getItemId());
|
|
|
202 |
if (item.getCategoryId() != 10006) {
|
|
|
203 |
continue;
|
|
|
204 |
}
|
|
|
205 |
int catalogId = item.getCatalogItemId();
|
|
|
206 |
if ((modelStockAllocationMap.containsKey(catalogId)
|
|
|
207 |
&& modelStockAllocationMap.get(catalogId).getQuantity() > 0)
|
|
|
208 |
|| itemListSnapshot.containsKey(item.getId())) {
|
|
|
209 |
List<CurrentInventorySnapshot> cisList = itemListSnapshot.get(item.getId());
|
|
|
210 |
if (cisList != null) {
|
|
|
211 |
for (CurrentInventorySnapshot cis : cisList) {
|
|
|
212 |
if (!catalogListingMap.containsKey(catalogId + "-" + cis.getFofoId())) {
|
|
|
213 |
CatalogListingModel catalogListingModel = new CatalogListingModel();
|
|
|
214 |
catalogListingModel.setFofoId(cis.getFofoId());
|
|
|
215 |
catalogListingModel.setModelName(item.getModelName());
|
|
|
216 |
catalogListingModel.setModelNumber(item.getModelNumber());
|
|
|
217 |
catalogListingModel.setCatalogId(catalogId);
|
|
|
218 |
catalogListingModel.setDp(tagListing.getSellingPrice());
|
|
|
219 |
catalogListingModel.setMop(tagListing.getMop());
|
|
|
220 |
catalogListingModel.setItemDescription(item.getItemDescriptionNoColor());
|
|
|
221 |
StockAllocationModel stockAllocationModel = modelStockAllocationMap.get(item.getCatalogItemId());
|
|
|
222 |
int stockAllocationQuantity = stockAllocationModel == null ? 0
|
|
|
223 |
: stockAllocationModel.getQuantity();
|
|
|
224 |
catalogListingModel.setAllocatedQuantity(stockAllocationQuantity);
|
|
|
225 |
catalogListingModel.setToBeOrdered(stockAllocationQuantity);
|
|
|
226 |
catalogListingModel.setBrand(item.getBrand());
|
|
|
227 |
if (item.getCategoryId() == 10006) {
|
|
|
228 |
catalogListingModel.setCategoryId(item.getCategoryId());
|
|
|
229 |
}
|
|
|
230 |
catalogListingMap.put(catalogId + "-" + cis.getFofoId(), catalogListingModel);
|
|
|
231 |
}
|
|
|
232 |
CatalogListingModel catalogListingModel = catalogListingMap
|
|
|
233 |
.get(catalogId + "-" + cis.getFofoId());
|
| 23779 |
amit.gupta |
234 |
|
| 24231 |
amit.gupta |
235 |
int itemAvailability = cis.getAvailability();
|
|
|
236 |
catalogListingModel.setStockInHand(catalogListingModel.getStockInHand() + itemAvailability);
|
| 23779 |
amit.gupta |
237 |
|
| 24231 |
amit.gupta |
238 |
Integer inTransitQuantity = itemsInTransit.get(item.getId() + "-" + cis.getFofoId());
|
|
|
239 |
int inTransitQty = (inTransitQuantity == null ? 0 : inTransitQuantity);
|
|
|
240 |
catalogListingModel
|
|
|
241 |
.setInTransitQuantity(catalogListingModel.getInTransitQuantity() + inTransitQty);
|
|
|
242 |
|
|
|
243 |
if (catalogListingModel.getAllocatedQuantity() > 0) {
|
|
|
244 |
int toBeOrdered = Math
|
|
|
245 |
.max(catalogListingModel.getToBeOrdered() - inTransitQty - itemAvailability, 0);
|
|
|
246 |
catalogListingModel.setToBeOrdered(toBeOrdered);
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
}
|
| 23405 |
amit.gupta |
250 |
}
|
|
|
251 |
}
|
| 24231 |
amit.gupta |
252 |
List<List<Object>> listOfRows = new ArrayList<>();
|
|
|
253 |
for (CatalogListingModel clm : catalogListingMap.values()) {
|
|
|
254 |
CustomRetailer cr = customRetailersMap.get(clm.getFofoId());
|
|
|
255 |
listOfRows
|
|
|
256 |
.add(Arrays.asList(cr.getPartnerId(), cr.getBusinessName(), clm.getBrand(), clm.getCatalogId(),
|
|
|
257 |
clm.getItemDescription(), clm.getDp(), clm.getMop(), clm.getAllocatedQuantity(),
|
|
|
258 |
clm.getInTransitQuantity(), clm.getStockInHand(), clm.getToBeOrdered()));
|
|
|
259 |
}
|
|
|
260 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("StoreId", "StoreName", "Catalog Id", "Brand", "Model", "DP", "MOP",
|
|
|
261 |
"Allocated Quantity", "In Transit", "Stock In hand", "Shortage"), listOfRows);
|
|
|
262 |
HttpHeaders headers = new HttpHeaders();
|
|
|
263 |
headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
264 |
headers.set("Content-disposition", "inline; filename=retailer-allocation.csv");
|
|
|
265 |
return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(baos.toByteArray()), headers, HttpStatus.OK);
|
| 23405 |
amit.gupta |
266 |
}
|
| 24231 |
amit.gupta |
267 |
return null;
|
| 23405 |
amit.gupta |
268 |
}
|
|
|
269 |
|
|
|
270 |
@RequestMapping(value = "/indent/loadIndent")
|
| 23785 |
amit.gupta |
271 |
public String loadOpenIndent(HttpServletRequest request, Model model,
|
|
|
272 |
@RequestParam(required = false, defaultValue = "0") int fofoId,
|
| 23786 |
amit.gupta |
273 |
@RequestParam(defaultValue = "TEN_LAC") CounterSize counterSize) throws ProfitMandiBusinessException {
|
| 23785 |
amit.gupta |
274 |
Set<Integer> roleIds = cookiesProcessor.getCookiesObject(request).getRoleIds();
|
|
|
275 |
LOGGER.info("Counter size is {}", counterSize);
|
|
|
276 |
LOGGER.info("Fofo Id is {}", fofoId);
|
| 23786 |
amit.gupta |
277 |
|
|
|
278 |
Map<Integer, ItemIdAvailability> itemCisMap = null;
|
|
|
279 |
if (!roleManager.isAdmin(roleIds)) {
|
|
|
280 |
fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
|
| 23796 |
amit.gupta |
281 |
List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
|
|
|
282 |
.selectItemsStock(fofoId);
|
|
|
283 |
itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
|
|
|
284 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
| 23786 |
amit.gupta |
285 |
} else {
|
|
|
286 |
if (fofoId == 0) {
|
| 23796 |
amit.gupta |
287 |
List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
|
|
|
288 |
.selectItemsStock();
|
|
|
289 |
itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
|
|
|
290 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
| 23779 |
amit.gupta |
291 |
} else {
|
| 23796 |
amit.gupta |
292 |
List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
|
|
|
293 |
.selectItemsStock(fofoId);
|
|
|
294 |
itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
|
|
|
295 |
.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
| 23405 |
amit.gupta |
296 |
}
|
| 23786 |
amit.gupta |
297 |
}
|
|
|
298 |
List<BrandPerformance> brandPerformanceList = new ArrayList<>();
|
|
|
299 |
List<BrandPerformance> accsBrandPerformanceList = new ArrayList<>();
|
|
|
300 |
List<StockAllocationModel> stockAllocationList;
|
|
|
301 |
if (fofoId > 0) {
|
|
|
302 |
stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
|
|
|
303 |
} else {
|
|
|
304 |
stockAllocationList = stockAllocationService.getStockAllocation(counterSize, true);
|
|
|
305 |
}
|
|
|
306 |
LOGGER.info("Stock Allocation list is {}", stockAllocationList);
|
| 23405 |
amit.gupta |
307 |
|
| 23786 |
amit.gupta |
308 |
Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
|
| 24231 |
amit.gupta |
309 |
.collect(Collectors.toMap(x -> x.getCatalogId(), x -> x));
|
| 23796 |
amit.gupta |
310 |
Map<Integer, Integer> itemsInTransit = null;
|
| 23786 |
amit.gupta |
311 |
LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
|
| 23855 |
amit.gupta |
312 |
List<TagListing> tagListings = tagListingRepository.selectAll(true);
|
| 23796 |
amit.gupta |
313 |
if (!roleManager.isAdmin(roleIds)) {
|
| 23786 |
amit.gupta |
314 |
tagListings = new ArrayList<>(tagListings);
|
| 23796 |
amit.gupta |
315 |
List<Order> inTransitOrders = orderRepository.selectOrders(fofoId, validOrderStatusList);
|
|
|
316 |
inTransitOrders = this.filterValidOrders(inTransitOrders);
|
|
|
317 |
itemsInTransit = inTransitOrders.stream().collect(Collectors.groupingBy(x -> x.getLineItem().getItemId(),
|
|
|
318 |
Collectors.summingInt(x -> x.getLineItem().getQuantity())));
|
|
|
319 |
} else {
|
|
|
320 |
itemsInTransit = new HashMap<>();
|
| 23786 |
amit.gupta |
321 |
}
|
|
|
322 |
Iterator<TagListing> iterator = tagListings.iterator();
|
| 23796 |
amit.gupta |
323 |
int totalPcs = 0;
|
|
|
324 |
int toBeOrdered = 0;
|
|
|
325 |
float totalAmount = 0;
|
| 24231 |
amit.gupta |
326 |
Map<Integer, CatalogListingModel> catalogListingMap = new HashMap<>();
|
| 23786 |
amit.gupta |
327 |
while (iterator.hasNext()) {
|
|
|
328 |
TagListing tagListing = iterator.next();
|
| 24231 |
amit.gupta |
329 |
|
| 23786 |
amit.gupta |
330 |
Item item = itemRepository.selectById(tagListing.getItemId());
|
| 24231 |
amit.gupta |
331 |
// itemRepository.select
|
|
|
332 |
// catalogTagListingMap
|
| 23786 |
amit.gupta |
333 |
if (roleManager.isAdmin(roleIds)) {
|
| 24231 |
amit.gupta |
334 |
if (item.getBrand() == null || item.getCategoryId() != 10006) {
|
| 23785 |
amit.gupta |
335 |
iterator.remove();
|
|
|
336 |
continue;
|
|
|
337 |
}
|
| 23786 |
amit.gupta |
338 |
} else {
|
| 24231 |
amit.gupta |
339 |
if (!(itemCisMap.containsKey(tagListing.getItemId())
|
|
|
340 |
|| itemStockAllocationMap.containsKey(tagListing.getItemId()))
|
|
|
341 |
) {
|
| 23786 |
amit.gupta |
342 |
iterator.remove();
|
|
|
343 |
continue;
|
| 23779 |
amit.gupta |
344 |
}
|
|
|
345 |
}
|
| 24231 |
amit.gupta |
346 |
int catalogId = item.getCatalogItemId();
|
|
|
347 |
|
|
|
348 |
CatalogListingModel catalogListingModel = catalogListingMap.get(catalogId);
|
|
|
349 |
if (!catalogListingMap.containsKey(catalogId)) {
|
|
|
350 |
catalogListingModel = new CatalogListingModel();
|
|
|
351 |
catalogListingModel.setCatalogId(catalogId);
|
|
|
352 |
catalogListingModel.setDp(tagListing.getSellingPrice());
|
|
|
353 |
catalogListingModel.setMop(tagListing.getMop());
|
|
|
354 |
catalogListingModel.setItemDescription(item.getItemDescriptionNoColor());
|
|
|
355 |
StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(item.getCatalogItemId());
|
|
|
356 |
int stockAllocationQuantity = stockAllocationModel == null ? 0 : stockAllocationModel.getQuantity();
|
|
|
357 |
catalogListingModel.setAllocatedQuantity(stockAllocationQuantity);
|
|
|
358 |
catalogListingModel.setToBeOrdered(stockAllocationQuantity);
|
|
|
359 |
catalogListingModel.setBrand(item.getBrand());
|
|
|
360 |
if (item.getCategoryId() == 10006) {
|
|
|
361 |
catalogListingModel.setCategoryId(item.getCategoryId());
|
|
|
362 |
}
|
|
|
363 |
catalogListingMap.put(catalogId, catalogListingModel);
|
|
|
364 |
}
|
|
|
365 |
|
| 23796 |
amit.gupta |
366 |
ItemIdAvailability itemIdAvailability = itemCisMap.get(tagListing.getItemId());
|
| 24231 |
amit.gupta |
367 |
int itemAvailability = itemIdAvailability == null ? 0 : itemIdAvailability.getAvailability();
|
|
|
368 |
catalogListingModel.setStockInHand(catalogListingModel.getStockInHand() + itemAvailability);
|
| 23796 |
amit.gupta |
369 |
|
| 24231 |
amit.gupta |
370 |
Integer inTransitQuantity = itemsInTransit.get(item.getId());
|
|
|
371 |
int inTransitQty = (inTransitQuantity == null ? 0 : inTransitQuantity);
|
|
|
372 |
catalogListingModel.setInTransitQuantity(catalogListingModel.getInTransitQuantity() + inTransitQty);
|
|
|
373 |
|
|
|
374 |
if (catalogListingModel.getAllocatedQuantity() > 0) {
|
|
|
375 |
toBeOrdered = Math.max(catalogListingModel.getToBeOrdered() - inTransitQty - itemAvailability, 0);
|
|
|
376 |
catalogListingModel.setToBeOrdered(toBeOrdered);
|
| 23796 |
amit.gupta |
377 |
}
|
| 23786 |
amit.gupta |
378 |
}
|
| 24231 |
amit.gupta |
379 |
List<CatalogListingModel> catalogModelList = new ArrayList<>(catalogListingMap.values());
|
| 23405 |
amit.gupta |
380 |
|
| 24231 |
amit.gupta |
381 |
Map<Boolean, List<CatalogListingModel>> performanceMap = catalogModelList.stream()
|
|
|
382 |
.collect(Collectors.groupingBy(x -> x.getCategoryId() == 10006));
|
|
|
383 |
List<CatalogListingModel> mobileListing = performanceMap.get(true) == null ? new ArrayList<>()
|
|
|
384 |
: performanceMap.get(true);
|
|
|
385 |
List<CatalogListingModel> accsListing = performanceMap.get(false) == null ? new ArrayList<>()
|
|
|
386 |
: performanceMap.get(false);
|
| 23785 |
amit.gupta |
387 |
|
| 23786 |
amit.gupta |
388 |
Map<String, DoubleSummaryStatistics> accsStats = accsListing.stream().collect(Collectors.groupingBy(
|
|
|
389 |
x -> x.getBrand(), Collectors.summarizingDouble(x -> x.getAllocatedQuantity() * x.getMop())));
|
|
|
390 |
Map<String, DoubleSummaryStatistics> mobStats = mobileListing.stream().collect(Collectors.groupingBy(
|
|
|
391 |
x -> x.getBrand(), Collectors.summarizingDouble(x -> x.getAllocatedQuantity() * x.getMop())));
|
| 23785 |
amit.gupta |
392 |
|
| 23786 |
amit.gupta |
393 |
for (Map.Entry<String, DoubleSummaryStatistics> entry : mobStats.entrySet()) {
|
|
|
394 |
DoubleSummaryStatistics dss = entry.getValue();
|
|
|
395 |
BrandPerformance bp = new BrandPerformance();
|
|
|
396 |
bp.setTarget((float) dss.getSum());
|
|
|
397 |
bp.setBrandName(entry.getKey());
|
|
|
398 |
brandPerformanceList.add(bp);
|
|
|
399 |
}
|
|
|
400 |
for (Map.Entry<String, DoubleSummaryStatistics> entry : accsStats.entrySet()) {
|
|
|
401 |
DoubleSummaryStatistics dss = entry.getValue();
|
|
|
402 |
BrandPerformance bp = new BrandPerformance();
|
|
|
403 |
bp.setTarget((float) dss.getSum());
|
|
|
404 |
bp.setBrandName(entry.getKey());
|
|
|
405 |
accsBrandPerformanceList.add(bp);
|
|
|
406 |
}
|
|
|
407 |
brandPerformanceList = brandPerformanceList.stream().filter(x -> x.getTarget() > 0)
|
| 23796 |
amit.gupta |
408 |
.sorted(Comparator.comparing(x -> x.getTarget())).collect(Collectors.toList());
|
| 23786 |
amit.gupta |
409 |
accsBrandPerformanceList = accsBrandPerformanceList.stream().filter(x -> x.getTarget() > 0)
|
| 23796 |
amit.gupta |
410 |
.sorted(Comparator.comparing(x -> x.getTarget())).collect(Collectors.toList());
|
| 23785 |
amit.gupta |
411 |
|
| 23786 |
amit.gupta |
412 |
List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
|
|
|
413 |
.collect(Collectors.toList());
|
|
|
414 |
Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
|
|
|
415 |
if (fofoId > 0) {
|
|
|
416 |
CustomRetailer customRetailer = customRetailersMap.get(fofoId);
|
|
|
417 |
model.addAttribute("retailerName",
|
|
|
418 |
customRetailer.getBusinessName() + "-" + customRetailer.getAddress().getCity());
|
|
|
419 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
420 |
model.addAttribute("retailerId", customRetailer.getPartnerId());
|
|
|
421 |
model.addAttribute("counterSize", fs.getCounterSize().toString());
|
| 23779 |
amit.gupta |
422 |
} else {
|
| 23786 |
amit.gupta |
423 |
model.addAttribute("counterSize", counterSize.toString());
|
| 23405 |
amit.gupta |
424 |
}
|
| 23786 |
amit.gupta |
425 |
String customRetailers = JSONObject.valueToString(customRetailersMap.values());
|
|
|
426 |
LOGGER.info("Custom retailers {}", customRetailers);
|
| 24231 |
amit.gupta |
427 |
// Lits<CatalogTagListing> catalog
|
|
|
428 |
Comparator<CatalogListingModel> firstCmp = Comparator.comparing(CatalogListingModel::getToBeOrdered,
|
|
|
429 |
Comparator.reverseOrder());
|
|
|
430 |
model.addAttribute("catalogTagListings",
|
|
|
431 |
catalogModelList.stream().sorted(firstCmp).collect(Collectors.toList()));
|
| 23786 |
amit.gupta |
432 |
model.addAttribute("customRetailers", customRetailers);
|
|
|
433 |
model.addAttribute("brandPerformanceList", brandPerformanceList);
|
|
|
434 |
model.addAttribute("accsBrandPerformanceList", accsBrandPerformanceList);
|
|
|
435 |
model.addAttribute("counterSizes", CounterSize.values());
|
| 23796 |
amit.gupta |
436 |
model.addAttribute("isAdmin", roleManager.isAdmin(roleIds));
|
|
|
437 |
model.addAttribute("totalAmount", totalAmount);
|
|
|
438 |
model.addAttribute("totalPcs", totalPcs);
|
|
|
439 |
model.addAttribute("walletAmount", userWalletRepository.selectByRetailerId(fofoId).getAmount());
|
|
|
440 |
// model.addAttribute("itemCisMap", itemCisMap);
|
| 23786 |
amit.gupta |
441 |
return "open-indent";
|
| 23405 |
amit.gupta |
442 |
}
|
| 23796 |
amit.gupta |
443 |
|
|
|
444 |
private List<Order> filterValidOrders(List<Order> lastOrdersList) {
|
|
|
445 |
Iterator<Order> orderIterator = lastOrdersList.iterator();
|
|
|
446 |
while (orderIterator.hasNext()) {
|
|
|
447 |
Order o = orderIterator.next();
|
|
|
448 |
if (o.getInvoiceNumber() != null) {
|
|
|
449 |
try {
|
|
|
450 |
purchaseRepository.selectByPurchaseReferenceAndFofoId(o.getInvoiceNumber(), o.getRetailerId());
|
|
|
451 |
orderIterator.remove();
|
|
|
452 |
continue;
|
|
|
453 |
} catch (Exception e) {
|
|
|
454 |
|
|
|
455 |
}
|
|
|
456 |
}
|
|
|
457 |
}
|
|
|
458 |
return lastOrdersList;
|
|
|
459 |
}
|
|
|
460 |
|
| 24231 |
amit.gupta |
461 |
@RequestMapping(value = "/indent/confirm-pause/{tagId}", method = RequestMethod.POST)
|
| 23855 |
amit.gupta |
462 |
public String raisePO(HttpServletRequest request, Model model, @PathVariable int tagId) throws Exception {
|
|
|
463 |
tagListingRepository.updateActiveById(tagId, false);
|
|
|
464 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
|
|
465 |
return "response";
|
|
|
466 |
}
|
| 24231 |
amit.gupta |
467 |
|
|
|
468 |
@RequestMapping(value = "/indent/create-po", method = RequestMethod.POST)
|
| 23796 |
amit.gupta |
469 |
public String raisePO(HttpServletRequest request, Model model) throws Exception {
|
|
|
470 |
boolean success = false;
|
|
|
471 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
472 |
int fofoId = loginDetails.getFofoId();
|
|
|
473 |
List<ItemPriceQuantity> itemQuantities = this.getItemQuantities(fofoId);
|
|
|
474 |
CustomRetailer customRetailer = retailerService.getFofoRetailers(Arrays.asList(fofoId)).get(fofoId);
|
|
|
475 |
Client userClient = new UserClient().getClient();
|
| 24231 |
amit.gupta |
476 |
double totalAmount = itemQuantities.stream().mapToDouble(x -> x.getQty() * x.getPrice()).sum();
|
| 23796 |
amit.gupta |
477 |
|
| 24231 |
amit.gupta |
478 |
if (totalAmount > 0) {
|
| 23796 |
amit.gupta |
479 |
userClient.addItemPricingToCart(customRetailer.getCartId(), itemQuantities);
|
|
|
480 |
User user = userRepository.selectById(loginDetails.getFofoId());
|
|
|
481 |
userClient = new UserClient().getClient();
|
|
|
482 |
LOGGER.info("Setting wallet amount in cart");
|
| 24231 |
amit.gupta |
483 |
long transactionId = userClient.createOrders(user.getActiveCartId(), "", 0, "", 0, loginDetails.getFofoId(),
|
|
|
484 |
7890, OrderSource.WEBSITE.getValue(), true);
|
| 23796 |
amit.gupta |
485 |
LOGGER.info("Creating wallet payment for transactionId - {}", transactionId);
|
|
|
486 |
createPayment(user, totalAmount, transactionId);
|
| 24231 |
amit.gupta |
487 |
TransactionService.Client transactionClient = new TransactionClient().getClient();
|
| 23796 |
amit.gupta |
488 |
transactionClient.changeTransactionStatus(transactionId, TransactionStatus.AUTHORIZED,
|
| 24231 |
amit.gupta |
489 |
"Payment received for the order", PickUpType.RUNNER.getValue(), OrderType.B2B, OrderSource.WEBSITE);
|
| 23796 |
amit.gupta |
490 |
transactionClient.changeTransactionStatus(Long.valueOf(transactionId), TransactionStatus.IN_PROCESS,
|
| 24231 |
amit.gupta |
491 |
"Paid fully through wallet", PickUpType.RUNNER.getValue(), OrderType.B2B, OrderSource.WEBSITE);
|
| 23796 |
amit.gupta |
492 |
LOGGER.info("Successfully created transaction: " + transactionId + " for amount: " + totalAmount);
|
|
|
493 |
transactionClient = new TransactionClient().getClient();
|
|
|
494 |
transactionClient.markOrderForRegisteredGstInvoice(Collections.singletonList(Long.valueOf(transactionId)));
|
|
|
495 |
try {
|
|
|
496 |
transactionClient.enqueueTransactionInfoEmail(transactionId);
|
|
|
497 |
} catch (Exception e1) {
|
|
|
498 |
e1.printStackTrace();
|
|
|
499 |
LOGGER.error("Unable to update status of transaction. Thrift Exception:", e1);
|
|
|
500 |
}
|
|
|
501 |
resetCart(transactionClient.getTransaction(transactionId));
|
|
|
502 |
}
|
|
|
503 |
model.addAttribute("response", mvcResponseSender.createResponseString(success));
|
|
|
504 |
return "response";
|
|
|
505 |
}
|
| 24231 |
amit.gupta |
506 |
|
|
|
507 |
private void createPayment(User user, double totalAmount, long transactionId)
|
|
|
508 |
throws NumberFormatException, PaymentException, TException {
|
| 23796 |
amit.gupta |
509 |
List<Attribute> paymentAttributes = new ArrayList<Attribute>();
|
|
|
510 |
in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
|
|
|
511 |
paymentAttributes.add(new Attribute("payMethod", "7890"));
|
| 24231 |
amit.gupta |
512 |
long paymentId = paymentClient.createPayment(user.getId(), totalAmount, WALLET_GATEWAY_ID, transactionId,
|
|
|
513 |
false);
|
|
|
514 |
paymentClient.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null,
|
|
|
515 |
PaymentStatus.SUCCESS, null, paymentAttributes);
|
| 23796 |
amit.gupta |
516 |
}
|
| 24231 |
amit.gupta |
517 |
|
| 23796 |
amit.gupta |
518 |
private List<ItemPriceQuantity> getItemQuantities(int fofoId) throws ProfitMandiBusinessException {
|
|
|
519 |
List<ItemPriceQuantity> itemQuantities = new ArrayList<>();
|
| 24231 |
amit.gupta |
520 |
/*
|
|
|
521 |
* Map<Integer, ItemIdAvailability> itemCisMap = null; List<ItemIdAvailability>
|
|
|
522 |
* currentInventorySnapshots = currentInventorySnapshotRepository
|
|
|
523 |
* .selectItemsStock(fofoId); itemCisMap =
|
|
|
524 |
* currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
|
|
|
525 |
* .collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
|
|
526 |
* List<StockAllocationModel> stockAllocationList =
|
|
|
527 |
* stockAllocationService.getStockAllocation(fofoId, true);
|
|
|
528 |
*
|
|
|
529 |
* Map<Integer, StockAllocationModel> itemStockAllocationMap =
|
|
|
530 |
* stockAllocationList.stream() .collect(Collectors.toMap(x -> x.catalogId(), x
|
|
|
531 |
* -> x)); Map<Integer, Integer> itemsInTransit = null;
|
|
|
532 |
* LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
|
|
|
533 |
* List<TagListing> tagListings = tagListingRepository.selectAll(false);
|
|
|
534 |
* List<Order> inTransitOrders = orderRepository.selectOrders(fofoId,
|
|
|
535 |
* validOrderStatusList); inTransitOrders =
|
|
|
536 |
* this.filterValidOrders(inTransitOrders); itemsInTransit =
|
|
|
537 |
* inTransitOrders.stream().collect(Collectors.groupingBy(x ->
|
|
|
538 |
* x.getLineItem().getItemId(), Collectors.summingInt(x ->
|
|
|
539 |
* x.getLineItem().getQuantity())));
|
|
|
540 |
*
|
|
|
541 |
* Iterator<TagListing> iterator = tagListings.iterator();
|
|
|
542 |
*
|
|
|
543 |
* int toBeOrdered = 0; while (iterator.hasNext()) { TagListing tagListing =
|
|
|
544 |
* iterator.next(); LOGGER.info(" tagListing.setAllocatedQuantity {}",
|
|
|
545 |
* tagListing.getAllocatedQuantity()); if
|
|
|
546 |
* (!itemCisMap.containsKey(tagListing.getItemId()) &&
|
|
|
547 |
* !itemStockAllocationMap.containsKey(tagListing.getItemId())) {
|
|
|
548 |
* iterator.remove(); continue; } ItemIdAvailability itemIdAvailability =
|
|
|
549 |
* itemCisMap.get(tagListing.getItemId());
|
|
|
550 |
* tagListing.setStockInHand(itemIdAvailability == null ? 0 :
|
|
|
551 |
* itemIdAvailability.getAvailability()); StockAllocationModel
|
|
|
552 |
* stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
|
|
|
553 |
*
|
|
|
554 |
* if (itemsInTransit.containsKey(tagListing.getItemId())) {
|
|
|
555 |
* tagListing.setInTransitQuantity(itemsInTransit.get(tagListing.getItemId()));
|
|
|
556 |
* } else { tagListing.setInTransitQuantity(0); } if (stockAllocationModel !=
|
|
|
557 |
* null) { tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
|
|
|
558 |
* toBeOrdered = Math.max(tagListing.getAllocatedQuantity() -
|
|
|
559 |
* tagListing.getInTransitQuantity() - tagListing.getStockInHand(), 0); if
|
|
|
560 |
* (toBeOrdered > 0) { ItemPriceQuantity ipq = new ItemPriceQuantity();
|
|
|
561 |
* ipq.setItemId(tagListing.getItemId()); ipq.setQty((long) toBeOrdered);
|
|
|
562 |
* ipq.setPrice(tagListing.getSellingPrice()); itemQuantities.add(ipq); } } }
|
|
|
563 |
*/
|
| 23796 |
amit.gupta |
564 |
return itemQuantities;
|
|
|
565 |
|
|
|
566 |
}
|
| 24231 |
amit.gupta |
567 |
|
| 23796 |
amit.gupta |
568 |
private long resetCart(Transaction transaction) {
|
|
|
569 |
long sum = 0;
|
|
|
570 |
Map<Long, Double> items = new HashMap<Long, Double>();
|
|
|
571 |
for (in.shop2020.model.v1.order.Order order : transaction.getOrders()) {
|
|
|
572 |
sum += order.getGvAmount();
|
|
|
573 |
for (LineItem lineitem : order.getLineitems()) {
|
|
|
574 |
Long itemId = lineitem.getItem_id();
|
|
|
575 |
Double quantity = items.get(itemId);
|
|
|
576 |
if (quantity == null) {
|
|
|
577 |
quantity = lineitem.getQuantity();
|
|
|
578 |
} else {
|
|
|
579 |
quantity = quantity + lineitem.getQuantity();
|
|
|
580 |
}
|
|
|
581 |
items.put(itemId, quantity);
|
|
|
582 |
}
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
LOGGER.debug("Items to reset in cart are: " + items);
|
|
|
586 |
|
|
|
587 |
try {
|
|
|
588 |
Client userClient = new UserClient().getClient();
|
|
|
589 |
userClient.resetCart(transaction.getShoppingCartid(), items);
|
|
|
590 |
} catch (TException e) {
|
|
|
591 |
LOGGER.error("Error while updating information in payment database.", e);
|
|
|
592 |
} catch (ShoppingCartException e) {
|
|
|
593 |
LOGGER.error("Error while reseting the cart in cart database.", e);
|
|
|
594 |
} catch (Exception e) {
|
|
|
595 |
LOGGER.error("Unexpected exception", e);
|
|
|
596 |
}
|
|
|
597 |
return sum;
|
|
|
598 |
}
|
|
|
599 |
|
| 23405 |
amit.gupta |
600 |
}
|