Subversion Repositories SmartDukaan

Rev

Rev 23786 | Rev 23798 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23786 Rev 23796
Line 3... Line 3...
3
import java.time.LocalDate;
3
import java.time.LocalDate;
4
import java.time.LocalDateTime;
4
import java.time.LocalDateTime;
5
import java.time.LocalTime;
5
import java.time.LocalTime;
6
import java.util.ArrayList;
6
import java.util.ArrayList;
7
import java.util.Arrays;
7
import java.util.Arrays;
-
 
8
import java.util.Collections;
-
 
9
import java.util.Comparator;
8
import java.util.DoubleSummaryStatistics;
10
import java.util.DoubleSummaryStatistics;
-
 
11
import java.util.HashMap;
9
import java.util.HashSet;
12
import java.util.HashSet;
10
import java.util.Iterator;
13
import java.util.Iterator;
11
import java.util.List;
14
import java.util.List;
12
import java.util.Map;
15
import java.util.Map;
13
import java.util.Set;
16
import java.util.Set;
Line 15... Line 18...
15
 
18
 
16
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletRequest;
17
 
20
 
18
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
22
import org.apache.logging.log4j.Logger;
-
 
23
import org.apache.thrift.TException;
20
import org.json.JSONObject;
24
import org.json.JSONObject;
21
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Controller;
26
import org.springframework.stereotype.Controller;
23
import org.springframework.transaction.annotation.Transactional;
27
import org.springframework.transaction.annotation.Transactional;
24
import org.springframework.ui.Model;
28
import org.springframework.ui.Model;
Line 31... Line 35...
31
import com.spice.profitmandi.common.enumuration.IndentStatus;
35
import com.spice.profitmandi.common.enumuration.IndentStatus;
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
36
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33
import com.spice.profitmandi.common.model.BrandPerformance;
37
import com.spice.profitmandi.common.model.BrandPerformance;
34
import com.spice.profitmandi.common.model.CustomRetailer;
38
import com.spice.profitmandi.common.model.CustomRetailer;
35
import com.spice.profitmandi.common.model.ItemIdAvailability;
39
import com.spice.profitmandi.common.model.ItemIdAvailability;
36
import com.spice.profitmandi.common.model.ItemIdQuantity;
-
 
37
import com.spice.profitmandi.common.model.ItemIdQuantityAvailability;
-
 
38
import com.spice.profitmandi.common.model.StockAllocationModel;
40
import com.spice.profitmandi.common.model.StockAllocationModel;
39
import com.spice.profitmandi.dao.entity.catalog.Item;
41
import com.spice.profitmandi.dao.entity.catalog.Item;
40
import com.spice.profitmandi.dao.entity.catalog.TagListing;
42
import com.spice.profitmandi.dao.entity.catalog.TagListing;
41
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
43
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
42
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
44
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
43
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
45
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
44
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
46
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
-
 
47
import com.spice.profitmandi.dao.entity.transaction.Order;
-
 
48
import com.spice.profitmandi.dao.entity.user.User;
45
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
49
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
46
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
50
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
47
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
51
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
48
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
52
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
49
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
53
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
50
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
54
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
51
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
55
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
-
 
56
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
-
 
57
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
-
 
58
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
-
 
59
import com.spice.profitmandi.dao.repository.user.UserRepository;
52
import com.spice.profitmandi.service.inventory.StockAllocationService;
60
import com.spice.profitmandi.service.inventory.StockAllocationService;
53
import com.spice.profitmandi.service.user.RetailerService;
61
import com.spice.profitmandi.service.user.RetailerService;
-
 
62
import com.spice.profitmandi.thrift.clients.PaymentClient;
-
 
63
import com.spice.profitmandi.thrift.clients.TransactionClient;
-
 
64
import com.spice.profitmandi.thrift.clients.UserClient;
54
import com.spice.profitmandi.web.model.LoginDetails;
65
import com.spice.profitmandi.web.model.LoginDetails;
55
import com.spice.profitmandi.web.util.CookiesProcessor;
66
import com.spice.profitmandi.web.util.CookiesProcessor;
56
import com.spice.profitmandi.web.util.MVCResponseSender;
67
import com.spice.profitmandi.web.util.MVCResponseSender;
57
import com.spice.profitmandi.web.util.RoleManager;
68
import com.spice.profitmandi.web.util.RoleManager;
58
 
69
 
-
 
70
import in.shop2020.logistics.PickUpType;
-
 
71
import in.shop2020.model.v1.order.LineItem;
-
 
72
import in.shop2020.model.v1.order.OrderSource;
-
 
73
import in.shop2020.model.v1.order.OrderStatus;
-
 
74
import in.shop2020.model.v1.order.OrderType;
-
 
75
import in.shop2020.model.v1.order.Transaction;
-
 
76
import in.shop2020.model.v1.order.TransactionService;
-
 
77
import in.shop2020.model.v1.order.TransactionStatus;
-
 
78
import in.shop2020.model.v1.user.ItemPriceQuantity;
-
 
79
import in.shop2020.model.v1.user.ItemQuantity;
-
 
80
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
81
import in.shop2020.model.v1.user.UserContextService.Client;
-
 
82
import in.shop2020.payments.Attribute;
-
 
83
import in.shop2020.payments.PaymentException;
-
 
84
import in.shop2020.payments.PaymentStatus;
-
 
85
 
59
@Controller
86
@Controller
60
@Transactional(rollbackFor = Throwable.class)
87
@Transactional(rollbackFor = Throwable.class)
61
public class IndentController {
88
public class IndentController {
62
 
89
 
63
	private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
90
	private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
-
 
91
	private static final int WALLET_GATEWAY_ID = 8;
-
 
92
 
-
 
93
	private final List<OrderStatus> validOrderStatusList = Arrays.asList(OrderStatus.ACCEPTED,
-
 
94
			OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.BILLED, OrderStatus.SHIPPED_FROM_WH,
-
 
95
			OrderStatus.DELIVERY_SUCCESS, OrderStatus.SHIPPED_TO_DESTINATION_CITY, OrderStatus.SHIPPED_TO_LOGST,
-
 
96
			OrderStatus.REACHED_DESTINATION_CITY);
64
 
97
 
65
	@Autowired
98
	@Autowired
66
	private CookiesProcessor cookiesProcessor;
99
	private CookiesProcessor cookiesProcessor;
-
 
100
	
-
 
101
	@Autowired
-
 
102
	private UserWalletRepository userWalletRepository;
-
 
103
 
-
 
104
	@Autowired
-
 
105
	private UserRepository userRepository;
67
 
106
 
68
	@Autowired
107
	@Autowired
69
	FofoStoreRepository fofoStoreRepository;
108
	FofoStoreRepository fofoStoreRepository;
70
 
109
 
71
	@Autowired
110
	@Autowired
-
 
111
	private OrderRepository orderRepository;
-
 
112
 
-
 
113
	@Autowired
72
	private ItemRepository itemRepository;
114
	private ItemRepository itemRepository;
73
 
115
 
74
	@Autowired
116
	@Autowired
75
	private IndentRepository indentRepository;
117
	private IndentRepository indentRepository;
76
 
118
 
Line 91... Line 133...
91
 
133
 
92
	@Autowired
134
	@Autowired
93
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
135
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
94
 
136
 
95
	@Autowired
137
	@Autowired
-
 
138
	private PurchaseRepository purchaseRepository;
-
 
139
 
-
 
140
	@Autowired
96
	private MVCResponseSender mvcResponseSender;
141
	private MVCResponseSender mvcResponseSender;
97
 
142
 
98
	@Autowired
143
	@Autowired
99
	RoleManager roleManager;
144
	RoleManager roleManager;
100
 
145
 
Line 186... Line 231...
186
		LOGGER.info("Fofo Id is {}", fofoId);
231
		LOGGER.info("Fofo Id is {}", fofoId);
187
 
232
 
188
		Map<Integer, ItemIdAvailability> itemCisMap = null;
233
		Map<Integer, ItemIdAvailability> itemCisMap = null;
189
		if (!roleManager.isAdmin(roleIds)) {
234
		if (!roleManager.isAdmin(roleIds)) {
190
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
235
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
191
			List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository.selectItemsStock(fofoId);
236
			List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
-
 
237
					.selectItemsStock(fofoId);
192
			itemCisMap = currentInventorySnapshots.stream().filter(x->x.getAvailability()>0).collect(Collectors.toMap(x -> x.getItemId(), x -> x));
238
			itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
-
 
239
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
193
		} else {
240
		} else {
194
			if (fofoId == 0) {
241
			if (fofoId == 0) {
195
				List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository.selectItemsStock();
242
				List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
-
 
243
						.selectItemsStock();
196
				itemCisMap = currentInventorySnapshots.stream().filter(x->x.getAvailability()>0).collect(Collectors.toMap(x -> x.getItemId(), x -> x));
244
				itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
-
 
245
						.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
197
			} else {
246
			} else {
198
				List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository.selectItemsStock(fofoId);
247
				List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
-
 
248
						.selectItemsStock(fofoId);
199
				itemCisMap = currentInventorySnapshots.stream().filter(x->x.getAvailability()>0).collect(Collectors.toMap(x -> x.getItemId(), x -> x));
249
				itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
-
 
250
						.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
200
			}
251
			}
201
		}
252
		}
202
		List<BrandPerformance> brandPerformanceList = new ArrayList<>();
253
		List<BrandPerformance> brandPerformanceList = new ArrayList<>();
203
		List<BrandPerformance> accsBrandPerformanceList = new ArrayList<>();
254
		List<BrandPerformance> accsBrandPerformanceList = new ArrayList<>();
204
		List<StockAllocationModel> stockAllocationList;
255
		List<StockAllocationModel> stockAllocationList;
Line 209... Line 260...
209
		}
260
		}
210
		LOGGER.info("Stock Allocation list  is {}", stockAllocationList);
261
		LOGGER.info("Stock Allocation list  is {}", stockAllocationList);
211
 
262
 
212
		Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
263
		Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
213
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
264
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
214
 
-
 
-
 
265
		Map<Integer, Integer> itemsInTransit = null;
215
		LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
266
		LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
216
		List<TagListing> tagListings = tagListingRepository.selectAll();
267
		List<TagListing> tagListings = tagListingRepository.selectAll();
217
		if(!roleManager.isAdmin(roleIds)) {
268
		if (!roleManager.isAdmin(roleIds)) {
218
			tagListings = new ArrayList<>(tagListings);
269
			tagListings = new ArrayList<>(tagListings);
-
 
270
			List<Order> inTransitOrders = orderRepository.selectOrders(fofoId, validOrderStatusList);
-
 
271
			inTransitOrders = this.filterValidOrders(inTransitOrders);
-
 
272
			itemsInTransit = inTransitOrders.stream().collect(Collectors.groupingBy(x -> x.getLineItem().getItemId(),
-
 
273
					Collectors.summingInt(x -> x.getLineItem().getQuantity())));
-
 
274
		} else {
-
 
275
			itemsInTransit = new HashMap<>();
219
		}
276
		}
220
		Iterator<TagListing> iterator = tagListings.iterator();
277
		Iterator<TagListing> iterator = tagListings.iterator();
-
 
278
		int totalPcs = 0;
-
 
279
		int toBeOrdered = 0;
-
 
280
		float totalAmount = 0;
221
		while (iterator.hasNext()) {
281
		while (iterator.hasNext()) {
222
			TagListing tagListing = iterator.next();
282
			TagListing tagListing = iterator.next();
223
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
283
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
224
			Item item = itemRepository.selectById(tagListing.getItemId());
284
			Item item = itemRepository.selectById(tagListing.getItemId());
225
			if (roleManager.isAdmin(roleIds)) {
285
			if (roleManager.isAdmin(roleIds)) {
226
				if (item.getBrand() == null || item.getCategoryId() == 0) {
286
				if (item.getBrand() == null || item.getCategoryId() == 0) {
227
					iterator.remove();
287
					iterator.remove();
228
					continue;
288
					continue;
229
				}
289
				}
230
			} else {
290
			} else {
-
 
291
				if (!itemCisMap.containsKey(tagListing.getItemId())
231
				if(!itemCisMap.containsKey(tagListing.getItemId()) && itemStockAllocationMap.containsKey(tagListing.getItemId())) {
292
						&& !itemStockAllocationMap.containsKey(tagListing.getItemId())) {
232
					iterator.remove();
293
					iterator.remove();
233
					continue;
294
					continue;
234
				}
295
				}
235
			}
296
			}
-
 
297
			ItemIdAvailability itemIdAvailability = itemCisMap.get(tagListing.getItemId());
-
 
298
			tagListing.setStockInHand(itemIdAvailability == null ? 0 : itemIdAvailability.getAvailability());
236
			StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
299
			StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
-
 
300
 
237
			LOGGER.info(" stockAllocationModel {}", stockAllocationModel);
301
			if (itemsInTransit.containsKey(tagListing.getItemId())) {
238
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
302
				tagListing.setInTransitQuantity(itemsInTransit.get(tagListing.getItemId()));
-
 
303
			} else {
-
 
304
				tagListing.setInTransitQuantity(0);
-
 
305
			}
239
			if (stockAllocationModel != null) {
306
			if (stockAllocationModel != null) {
240
				tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
307
				tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
-
 
308
				toBeOrdered = Math.max(tagListing.getAllocatedQuantity() - tagListing.getInTransitQuantity()
-
 
309
						- tagListing.getStockInHand(), 0);
-
 
310
				totalPcs += toBeOrdered;
-
 
311
				totalAmount += toBeOrdered * tagListing.getSellingPrice();
-
 
312
				tagListing.setToBeOrdered(toBeOrdered);
241
			} else {
313
			} else {
242
				tagListing.setAllocatedQuantity(0);
314
				tagListing.setAllocatedQuantity(0);
243
			}
315
			}
244
			tagListing.setBrand(item.getBrand());
316
			tagListing.setBrand(item.getBrand());
245
			tagListing.setItemDescription(item.getItemDescription());
317
			tagListing.setItemDescription(item.getItemDescription());
246
			if (item.getCategoryId() == 10006) {
318
			if (item.getCategoryId() == 10006) {
247
				tagListing.setCategoryId(item.getCategoryId());
319
				tagListing.setCategoryId(item.getCategoryId());
248
			}
320
			}
249
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
321
			// LOGGER.info(" tagListing.setAllocatedQuantity {}",
-
 
322
			// tagListing.getAllocatedQuantity());
250
		}
323
		}
251
 
324
 
252
		Map<Integer, List<TagListing>> performanceMap = tagListings.stream()
325
		Map<Integer, List<TagListing>> performanceMap = tagListings.stream()
253
				.collect(Collectors.groupingBy(x -> x.getCategoryId()));
326
				.collect(Collectors.groupingBy(x -> x.getCategoryId()));
254
		List<TagListing> accsListing = performanceMap.get(0);
327
		List<TagListing> accsListing = performanceMap.get(0);
Line 272... Line 345...
272
			bp.setTarget((float) dss.getSum());
345
			bp.setTarget((float) dss.getSum());
273
			bp.setBrandName(entry.getKey());
346
			bp.setBrandName(entry.getKey());
274
			accsBrandPerformanceList.add(bp);
347
			accsBrandPerformanceList.add(bp);
275
		}
348
		}
276
		brandPerformanceList = brandPerformanceList.stream().filter(x -> x.getTarget() > 0)
349
		brandPerformanceList = brandPerformanceList.stream().filter(x -> x.getTarget() > 0)
277
				.sorted((BrandPerformance o1, BrandPerformance o2) -> (int) (o1.getTarget() - o2.getTarget()))
350
				.sorted(Comparator.comparing(x -> x.getTarget())).collect(Collectors.toList());
278
				.collect(Collectors.toList());
-
 
279
		accsBrandPerformanceList = accsBrandPerformanceList.stream().filter(x -> x.getTarget() > 0)
351
		accsBrandPerformanceList = accsBrandPerformanceList.stream().filter(x -> x.getTarget() > 0)
280
				.sorted((BrandPerformance o1, BrandPerformance o2) -> (int) (o1.getTarget() - o2.getTarget()))
352
				.sorted(Comparator.comparing(x -> x.getTarget())).collect(Collectors.toList());
281
				.collect(Collectors.toList());
-
 
282
 
353
 
283
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
354
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
284
				.collect(Collectors.toList());
355
				.collect(Collectors.toList());
285
		model.addAttribute("tagListings", tagListings);
-
 
286
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
356
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
287
		if (fofoId > 0) {
357
		if (fofoId > 0) {
288
			CustomRetailer customRetailer = customRetailersMap.get(fofoId);
358
			CustomRetailer customRetailer = customRetailersMap.get(fofoId);
289
			model.addAttribute("retailerName",
359
			model.addAttribute("retailerName",
290
					customRetailer.getBusinessName() + "-" + customRetailer.getAddress().getCity());
360
					customRetailer.getBusinessName() + "-" + customRetailer.getAddress().getCity());
Line 294... Line 364...
294
		} else {
364
		} else {
295
			model.addAttribute("counterSize", counterSize.toString());
365
			model.addAttribute("counterSize", counterSize.toString());
296
		}
366
		}
297
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
367
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
298
		LOGGER.info("Custom retailers {}", customRetailers);
368
		LOGGER.info("Custom retailers {}", customRetailers);
-
 
369
		Comparator<TagListing> firstCmp = Comparator
-
 
370
				.comparing(x -> ((x.getAllocatedQuantity() - x.getStockInHand() - x.getInTransitQuantity() == 0)
-
 
371
						? Integer.MIN_VALUE
-
 
372
						: (x.getAllocatedQuantity() - x.getStockInHand())), Comparator.reverseOrder());
-
 
373
		model.addAttribute("tagListings", tagListings.stream().sorted(firstCmp).collect(Collectors.toList()));
299
		model.addAttribute("customRetailers", customRetailers);
374
		model.addAttribute("customRetailers", customRetailers);
300
		model.addAttribute("brandPerformanceList", brandPerformanceList);
375
		model.addAttribute("brandPerformanceList", brandPerformanceList);
301
		model.addAttribute("accsBrandPerformanceList", accsBrandPerformanceList);
376
		model.addAttribute("accsBrandPerformanceList", accsBrandPerformanceList);
302
		model.addAttribute("counterSizes", CounterSize.values());
377
		model.addAttribute("counterSizes", CounterSize.values());
-
 
378
		model.addAttribute("isAdmin", roleManager.isAdmin(roleIds));
-
 
379
		model.addAttribute("totalAmount", totalAmount);
-
 
380
		model.addAttribute("totalPcs", totalPcs);
-
 
381
		model.addAttribute("walletAmount", userWalletRepository.selectByRetailerId(fofoId).getAmount());
303
		model.addAttribute("itemCisMap", itemCisMap);
382
		// model.addAttribute("itemCisMap", itemCisMap);
304
		return "open-indent";
383
		return "open-indent";
305
	}
384
	}
-
 
385
 
-
 
386
	private List<Order> filterValidOrders(List<Order> lastOrdersList) {
-
 
387
		Iterator<Order> orderIterator = lastOrdersList.iterator();
-
 
388
		while (orderIterator.hasNext()) {
-
 
389
			Order o = orderIterator.next();
-
 
390
			if (o.getInvoiceNumber() != null) {
-
 
391
				try {
-
 
392
					purchaseRepository.selectByPurchaseReferenceAndFofoId(o.getInvoiceNumber(), o.getRetailerId());
-
 
393
					orderIterator.remove();
-
 
394
					continue;
-
 
395
				} catch (Exception e) {
-
 
396
 
-
 
397
				}
-
 
398
			}
-
 
399
		}
-
 
400
		return lastOrdersList;
-
 
401
	}
-
 
402
 
-
 
403
	@RequestMapping(value = "/indent/create-po", method=RequestMethod.POST)
-
 
404
	public String raisePO(HttpServletRequest request, Model model) throws Exception {
-
 
405
		boolean success = false;
-
 
406
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
-
 
407
		int fofoId = loginDetails.getFofoId();
-
 
408
		List<ItemPriceQuantity> itemQuantities = this.getItemQuantities(fofoId);
-
 
409
		CustomRetailer customRetailer = retailerService.getFofoRetailers(Arrays.asList(fofoId)).get(fofoId);
-
 
410
		Client userClient = new UserClient().getClient();
-
 
411
		double totalAmount = itemQuantities.stream().mapToDouble(x->x.getQty()*x.getPrice()).sum();
-
 
412
 
-
 
413
		if(totalAmount > 0) {
-
 
414
			userClient.addItemPricingToCart(customRetailer.getCartId(), itemQuantities);
-
 
415
			User user = userRepository.selectById(loginDetails.getFofoId());
-
 
416
			userClient = new UserClient().getClient();
-
 
417
			LOGGER.info("Setting wallet amount in cart");
-
 
418
			long transactionId = userClient.createOrders(user.getActiveCartId(), "", 0, "", 0, loginDetails.getFofoId(), 7890,
-
 
419
					OrderSource.WEBSITE.getValue(), true);
-
 
420
			LOGGER.info("Creating wallet payment for transactionId - {}", transactionId);
-
 
421
			createPayment(user, totalAmount, transactionId);
-
 
422
			TransactionService.Client  transactionClient = new TransactionClient().getClient();
-
 
423
			transactionClient.changeTransactionStatus(transactionId, TransactionStatus.AUTHORIZED,
-
 
424
					"Payment received for the order", PickUpType.RUNNER.getValue(), OrderType.B2B,
-
 
425
					OrderSource.WEBSITE);
-
 
426
			transactionClient.changeTransactionStatus(Long.valueOf(transactionId), TransactionStatus.IN_PROCESS,
-
 
427
					"Paid fully through wallet", PickUpType.RUNNER.getValue(), OrderType.B2B,
-
 
428
					OrderSource.WEBSITE);
-
 
429
			LOGGER.info("Successfully created transaction: " + transactionId + " for amount: " + totalAmount);
-
 
430
			transactionClient = new TransactionClient().getClient();
-
 
431
			transactionClient.markOrderForRegisteredGstInvoice(Collections.singletonList(Long.valueOf(transactionId)));
-
 
432
			try {
-
 
433
				transactionClient.enqueueTransactionInfoEmail(transactionId);
-
 
434
			} catch (Exception e1) {
-
 
435
				e1.printStackTrace();
-
 
436
				LOGGER.error("Unable to update status of transaction. Thrift Exception:", e1);
-
 
437
			}
-
 
438
			resetCart(transactionClient.getTransaction(transactionId));
-
 
439
		}
-
 
440
		model.addAttribute("response", mvcResponseSender.createResponseString(success));
-
 
441
		return "response";
-
 
442
	}
-
 
443
	
-
 
444
	private void createPayment(User user, double totalAmount, long transactionId) throws NumberFormatException, PaymentException, TException {
-
 
445
		List<Attribute> paymentAttributes = new ArrayList<Attribute>();
-
 
446
		in.shop2020.payments.PaymentService.Client paymentClient = new PaymentClient().getClient();
-
 
447
		paymentAttributes.add(new Attribute("payMethod", "7890"));
-
 
448
		long paymentId = paymentClient.createPayment(user.getId(), totalAmount, WALLET_GATEWAY_ID, transactionId, false);
-
 
449
		paymentClient.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.SUCCESS, null,
-
 
450
				paymentAttributes);
-
 
451
	}
-
 
452
	private List<ItemPriceQuantity> getItemQuantities(int fofoId) throws ProfitMandiBusinessException {
-
 
453
		List<ItemPriceQuantity> itemQuantities = new ArrayList<>();
-
 
454
		Map<Integer, ItemIdAvailability> itemCisMap = null;
-
 
455
		List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository
-
 
456
				.selectItemsStock(fofoId);
-
 
457
		itemCisMap = currentInventorySnapshots.stream().filter(x -> x.getAvailability() > 0)
-
 
458
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
-
 
459
		List<StockAllocationModel> stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
-
 
460
 
-
 
461
		Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
-
 
462
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
-
 
463
		Map<Integer, Integer> itemsInTransit = null;
-
 
464
		LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
-
 
465
		List<TagListing> tagListings = tagListingRepository.selectAll();
-
 
466
		List<Order> inTransitOrders = orderRepository.selectOrders(fofoId, validOrderStatusList);
-
 
467
		inTransitOrders = this.filterValidOrders(inTransitOrders);
-
 
468
		itemsInTransit = inTransitOrders.stream().collect(Collectors.groupingBy(x -> x.getLineItem().getItemId(),
-
 
469
				Collectors.summingInt(x -> x.getLineItem().getQuantity())));
-
 
470
 
-
 
471
		Iterator<TagListing> iterator = tagListings.iterator();
-
 
472
 
-
 
473
		int toBeOrdered = 0;
-
 
474
		while (iterator.hasNext()) {
-
 
475
			TagListing tagListing = iterator.next();
-
 
476
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
-
 
477
			if (!itemCisMap.containsKey(tagListing.getItemId())
-
 
478
					&& !itemStockAllocationMap.containsKey(tagListing.getItemId())) {
-
 
479
				iterator.remove();
-
 
480
				continue;
-
 
481
			}
-
 
482
			ItemIdAvailability itemIdAvailability = itemCisMap.get(tagListing.getItemId());
-
 
483
			tagListing.setStockInHand(itemIdAvailability == null ? 0 : itemIdAvailability.getAvailability());
-
 
484
			StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
-
 
485
 
-
 
486
			if (itemsInTransit.containsKey(tagListing.getItemId())) {
-
 
487
				tagListing.setInTransitQuantity(itemsInTransit.get(tagListing.getItemId()));
-
 
488
			} else {
-
 
489
				tagListing.setInTransitQuantity(0);
-
 
490
			}
-
 
491
			if (stockAllocationModel != null) {
-
 
492
				tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
-
 
493
				toBeOrdered = Math.max(tagListing.getAllocatedQuantity() - tagListing.getInTransitQuantity()
-
 
494
						- tagListing.getStockInHand(), 0);
-
 
495
				if (toBeOrdered > 0) {
-
 
496
					ItemPriceQuantity ipq = new ItemPriceQuantity();
-
 
497
					ipq.setItemId(tagListing.getItemId());
-
 
498
					ipq.setQty((long)toBeOrdered);
-
 
499
					ipq.setPrice(tagListing.getSellingPrice());
-
 
500
					itemQuantities.add(ipq);
-
 
501
				}
-
 
502
			}
-
 
503
		}
-
 
504
		return itemQuantities;
-
 
505
 
-
 
506
	}
-
 
507
	
-
 
508
	private long resetCart(Transaction transaction) {
-
 
509
		long sum = 0;
-
 
510
		Map<Long, Double> items = new HashMap<Long, Double>();
-
 
511
		for (in.shop2020.model.v1.order.Order order : transaction.getOrders()) {
-
 
512
			sum += order.getGvAmount();
-
 
513
			for (LineItem lineitem : order.getLineitems()) {
-
 
514
				Long itemId = lineitem.getItem_id();
-
 
515
				Double quantity = items.get(itemId);
-
 
516
				if (quantity == null) {
-
 
517
					quantity = lineitem.getQuantity();
-
 
518
				} else {
-
 
519
					quantity = quantity + lineitem.getQuantity();
-
 
520
				}
-
 
521
				items.put(itemId, quantity);
-
 
522
			}
-
 
523
		}
-
 
524
 
-
 
525
		LOGGER.debug("Items to reset in cart are: " + items);
-
 
526
 
-
 
527
		try {
-
 
528
			Client userClient = new UserClient().getClient();
-
 
529
			userClient.resetCart(transaction.getShoppingCartid(), items);
-
 
530
		} catch (TException e) {
-
 
531
			LOGGER.error("Error while updating information in payment database.", e);
-
 
532
		} catch (ShoppingCartException e) {
-
 
533
			LOGGER.error("Error while reseting the cart in cart database.", e);
-
 
534
		} catch (Exception e) {
-
 
535
			LOGGER.error("Unexpected exception", e);
-
 
536
		}
-
 
537
		return sum;
-
 
538
	}
-
 
539
 
306
}
540
}