Subversion Repositories SmartDukaan

Rev

Rev 23785 | Rev 23796 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23405 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDate;
4
import java.time.LocalDateTime;
5
import java.time.LocalTime;
23785 amit.gupta 6
import java.util.ArrayList;
23405 amit.gupta 7
import java.util.Arrays;
23785 amit.gupta 8
import java.util.DoubleSummaryStatistics;
23405 amit.gupta 9
import java.util.HashSet;
23785 amit.gupta 10
import java.util.Iterator;
23405 amit.gupta 11
import java.util.List;
12
import java.util.Map;
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
 
23717 amit.gupta 18
import org.apache.logging.log4j.LogManager;
23568 govind 19
import org.apache.logging.log4j.Logger;
23779 amit.gupta 20
import org.json.JSONObject;
23405 amit.gupta 21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Controller;
23
import org.springframework.transaction.annotation.Transactional;
24
import org.springframework.ui.Model;
25
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMethod;
23779 amit.gupta 28
import org.springframework.web.bind.annotation.RequestParam;
23405 amit.gupta 29
 
23785 amit.gupta 30
import com.spice.profitmandi.common.enumuration.CounterSize;
23405 amit.gupta 31
import com.spice.profitmandi.common.enumuration.IndentStatus;
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23785 amit.gupta 33
import com.spice.profitmandi.common.model.BrandPerformance;
34
import com.spice.profitmandi.common.model.CustomRetailer;
23786 amit.gupta 35
import com.spice.profitmandi.common.model.ItemIdAvailability;
36
import com.spice.profitmandi.common.model.ItemIdQuantity;
37
import com.spice.profitmandi.common.model.ItemIdQuantityAvailability;
23779 amit.gupta 38
import com.spice.profitmandi.common.model.StockAllocationModel;
23785 amit.gupta 39
import com.spice.profitmandi.dao.entity.catalog.Item;
23405 amit.gupta 40
import com.spice.profitmandi.dao.entity.catalog.TagListing;
41
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
42
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
23785 amit.gupta 43
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
23405 amit.gupta 44
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
45
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
46
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
23779 amit.gupta 47
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
23405 amit.gupta 48
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
49
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
50
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
51
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
23779 amit.gupta 52
import com.spice.profitmandi.service.inventory.StockAllocationService;
53
import com.spice.profitmandi.service.user.RetailerService;
23405 amit.gupta 54
import com.spice.profitmandi.web.model.LoginDetails;
55
import com.spice.profitmandi.web.util.CookiesProcessor;
56
import com.spice.profitmandi.web.util.MVCResponseSender;
23785 amit.gupta 57
import com.spice.profitmandi.web.util.RoleManager;
23405 amit.gupta 58
 
59
@Controller
60
@Transactional(rollbackFor = Throwable.class)
61
public class IndentController {
62
 
23568 govind 63
	private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
23405 amit.gupta 64
 
65
	@Autowired
66
	private CookiesProcessor cookiesProcessor;
23785 amit.gupta 67
 
23779 amit.gupta 68
	@Autowired
69
	FofoStoreRepository fofoStoreRepository;
23405 amit.gupta 70
 
71
	@Autowired
72
	private ItemRepository itemRepository;
73
 
74
	@Autowired
75
	private IndentRepository indentRepository;
76
 
77
	@Autowired
23779 amit.gupta 78
	private StockAllocationService stockAllocationService;
79
 
80
	@Autowired
23405 amit.gupta 81
	private IndentItemRepository indentItemRepository;
23785 amit.gupta 82
 
23779 amit.gupta 83
	@Autowired
84
	private RetailerService retailerService;
23405 amit.gupta 85
 
86
	@Autowired
87
	private TagListingRepository tagListingRepository;
88
 
89
	@Autowired
90
	private FofoOrderRepository fofoOrderRepository;
91
 
92
	@Autowired
93
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
94
 
95
	@Autowired
96
	private MVCResponseSender mvcResponseSender;
97
 
23785 amit.gupta 98
	@Autowired
99
	RoleManager roleManager;
23405 amit.gupta 100
 
23779 amit.gupta 101
	@RequestMapping(value = "/open-indent/save", method = RequestMethod.POST)
23785 amit.gupta 102
	public String saveOpenIndent(HttpServletRequest request, Model model,
23786 amit.gupta 103
			@RequestBody List<StockAllocationModel> stockAllocationModelList, @RequestParam int fofoId,
104
			@RequestParam(defaultValue = "TEN_LAC") CounterSize counterSize) throws Exception {
23405 amit.gupta 105
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23779 amit.gupta 106
		boolean response = false;
23785 amit.gupta 107
		if (fofoId > 0) {
108
			FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
23786 amit.gupta 109
			stockAllocationModelList.forEach(x -> {
110
				x.setFofoId(fofoId);
111
				x.setCounterSize(fs.getCounterSize());
112
			});
113
		} else {
114
			stockAllocationModelList.forEach(x -> {
115
				x.setFofoId(fofoId);
116
				x.setCounterSize(counterSize);
117
			});
23785 amit.gupta 118
		}
119
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
120
			response = stockAllocationService.addToAllocation(stockAllocationModelList);
121
			model.addAttribute("response", mvcResponseSender.createResponseString(response));
122
		}
23405 amit.gupta 123
		return "response";
124
	}
125
 
23415 amit.gupta 126
	@RequestMapping(value = "/migrate", method = RequestMethod.PUT)
127
	public String migrate(HttpServletRequest request, Model model) throws Exception {
128
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
129
		int fofoId = loginDetails.getFofoId();
130
		List<TagListing> tagListings = tagListingRepository.selectAll();
23779 amit.gupta 131
		for (TagListing tagListing : tagListings) {
23415 amit.gupta 132
			int itemId = tagListing.getItemId();
133
		}
134
		return "";
135
	}
136
 
23405 amit.gupta 137
	@RequestMapping(value = "/indent/inProcess")
23779 amit.gupta 138
	public String loadInProcessIndents(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
23405 amit.gupta 139
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
140
		int fofoId = loginDetails.getFofoId();
141
 
23779 amit.gupta 142
		List<Integer> pendingIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.PENDING).stream()
143
				.map(x -> x.getId()).collect(Collectors.toList());
144
		List<Integer> allocatedIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.ALLOCATED)
145
				.stream().map(x -> x.getId()).collect(Collectors.toList());
23405 amit.gupta 146
		pendingIndentIds.addAll(allocatedIndentIds);
23779 amit.gupta 147
 
148
		if (pendingIndentIds.size() > 0) {
149
			Map<Integer, IndentItem> pendingIndentItemIdMap = indentItemRepository.selectIndentItems(pendingIndentIds)
150
					.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
151
			List<TagListing> tagListings = tagListingRepository
152
					.selectByItemIdsAndTagIds(pendingIndentItemIdMap.keySet(), new HashSet<>(Arrays.asList(4)));
153
 
154
			List<FofoOrderItem> fofoOrderItems = fofoOrderRepository.selectByFofoItemIds(fofoId, pendingIndentIds,
155
					LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).minusDays(30), LocalDateTime.now());
23405 amit.gupta 156
			Map<Integer, Integer> itemQuantity = fofoOrderItems.stream().collect(
157
					Collectors.groupingBy(FofoOrderItem::getItemId, Collectors.summingInt(FofoOrderItem::getQuantity)));
23779 amit.gupta 158
 
23405 amit.gupta 159
			List<CurrentInventorySnapshot> cis = currentInventorySnapshotRepository.selectByFofoId(fofoId);
23779 amit.gupta 160
			Map<Integer, CurrentInventorySnapshot> itemIdSnapshotMap = cis.stream()
161
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
162
 
163
			for (TagListing tagListing : tagListings) {
23405 amit.gupta 164
				Integer itemId = tagListing.getItemId();
165
				tagListing.setItemDescription(itemRepository.selectById(itemId).getItemDescription());
23779 amit.gupta 166
				if (itemQuantity.containsKey(itemId)) {
23405 amit.gupta 167
					tagListing.setLast30DaysSale(itemQuantity.get(itemId));
168
				}
23779 amit.gupta 169
				if (itemIdSnapshotMap.containsKey(itemId)) {
23405 amit.gupta 170
					tagListing.setStockInHand(itemIdSnapshotMap.get(itemId).getAvailability());
171
				}
172
			}
23779 amit.gupta 173
 
23405 amit.gupta 174
			model.addAttribute("pendingIndentItemIdMap", pendingIndentItemIdMap);
175
			model.addAttribute("tagListings", tagListings);
176
		}
177
		return "pending-indent";
178
	}
179
 
180
	@RequestMapping(value = "/indent/loadIndent")
23785 amit.gupta 181
	public String loadOpenIndent(HttpServletRequest request, Model model,
182
			@RequestParam(required = false, defaultValue = "0") int fofoId,
23786 amit.gupta 183
			@RequestParam(defaultValue = "TEN_LAC") CounterSize counterSize) throws ProfitMandiBusinessException {
23785 amit.gupta 184
		Set<Integer> roleIds = cookiesProcessor.getCookiesObject(request).getRoleIds();
185
		LOGGER.info("Counter size is {}", counterSize);
186
		LOGGER.info("Fofo Id is {}", fofoId);
23786 amit.gupta 187
 
188
		Map<Integer, ItemIdAvailability> itemCisMap = null;
189
		if (!roleManager.isAdmin(roleIds)) {
190
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
191
			List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository.selectItemsStock(fofoId);
192
			itemCisMap = currentInventorySnapshots.stream().filter(x->x.getAvailability()>0).collect(Collectors.toMap(x -> x.getItemId(), x -> x));
193
		} else {
194
			if (fofoId == 0) {
195
				List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository.selectItemsStock();
196
				itemCisMap = currentInventorySnapshots.stream().filter(x->x.getAvailability()>0).collect(Collectors.toMap(x -> x.getItemId(), x -> x));
23779 amit.gupta 197
			} else {
23786 amit.gupta 198
				List<ItemIdAvailability> currentInventorySnapshots = currentInventorySnapshotRepository.selectItemsStock(fofoId);
199
				itemCisMap = currentInventorySnapshots.stream().filter(x->x.getAvailability()>0).collect(Collectors.toMap(x -> x.getItemId(), x -> x));
23405 amit.gupta 200
			}
23786 amit.gupta 201
		}
202
		List<BrandPerformance> brandPerformanceList = new ArrayList<>();
203
		List<BrandPerformance> accsBrandPerformanceList = new ArrayList<>();
204
		List<StockAllocationModel> stockAllocationList;
205
		if (fofoId > 0) {
206
			stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
207
		} else {
208
			stockAllocationList = stockAllocationService.getStockAllocation(counterSize, true);
209
		}
210
		LOGGER.info("Stock Allocation list  is {}", stockAllocationList);
23405 amit.gupta 211
 
23786 amit.gupta 212
		Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
213
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
23405 amit.gupta 214
 
23786 amit.gupta 215
		LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
216
		List<TagListing> tagListings = tagListingRepository.selectAll();
217
		if(!roleManager.isAdmin(roleIds)) {
218
			tagListings = new ArrayList<>(tagListings);
219
		}
220
		Iterator<TagListing> iterator = tagListings.iterator();
221
		while (iterator.hasNext()) {
222
			TagListing tagListing = iterator.next();
223
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
224
			Item item = itemRepository.selectById(tagListing.getItemId());
225
			if (roleManager.isAdmin(roleIds)) {
226
				if (item.getBrand() == null || item.getCategoryId() == 0) {
23785 amit.gupta 227
					iterator.remove();
228
					continue;
229
				}
23786 amit.gupta 230
			} else {
231
				if(!itemCisMap.containsKey(tagListing.getItemId()) && itemStockAllocationMap.containsKey(tagListing.getItemId())) {
232
					iterator.remove();
233
					continue;
23779 amit.gupta 234
				}
235
			}
23786 amit.gupta 236
			StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
237
			LOGGER.info(" stockAllocationModel {}", stockAllocationModel);
238
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
239
			if (stockAllocationModel != null) {
240
				tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
241
			} else {
242
				tagListing.setAllocatedQuantity(0);
243
			}
244
			tagListing.setBrand(item.getBrand());
245
			tagListing.setItemDescription(item.getItemDescription());
246
			if (item.getCategoryId() == 10006) {
247
				tagListing.setCategoryId(item.getCategoryId());
248
			}
249
			LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
250
		}
23405 amit.gupta 251
 
23786 amit.gupta 252
		Map<Integer, List<TagListing>> performanceMap = tagListings.stream()
253
				.collect(Collectors.groupingBy(x -> x.getCategoryId()));
254
		List<TagListing> accsListing = performanceMap.get(0);
255
		List<TagListing> mobileListing = performanceMap.get(10006);
23785 amit.gupta 256
 
23786 amit.gupta 257
		Map<String, DoubleSummaryStatistics> accsStats = accsListing.stream().collect(Collectors.groupingBy(
258
				x -> x.getBrand(), Collectors.summarizingDouble(x -> x.getAllocatedQuantity() * x.getMop())));
259
		Map<String, DoubleSummaryStatistics> mobStats = mobileListing.stream().collect(Collectors.groupingBy(
260
				x -> x.getBrand(), Collectors.summarizingDouble(x -> x.getAllocatedQuantity() * x.getMop())));
23785 amit.gupta 261
 
23786 amit.gupta 262
		for (Map.Entry<String, DoubleSummaryStatistics> entry : mobStats.entrySet()) {
263
			DoubleSummaryStatistics dss = entry.getValue();
264
			BrandPerformance bp = new BrandPerformance();
265
			bp.setTarget((float) dss.getSum());
266
			bp.setBrandName(entry.getKey());
267
			brandPerformanceList.add(bp);
268
		}
269
		for (Map.Entry<String, DoubleSummaryStatistics> entry : accsStats.entrySet()) {
270
			DoubleSummaryStatistics dss = entry.getValue();
271
			BrandPerformance bp = new BrandPerformance();
272
			bp.setTarget((float) dss.getSum());
273
			bp.setBrandName(entry.getKey());
274
			accsBrandPerformanceList.add(bp);
275
		}
276
		brandPerformanceList = brandPerformanceList.stream().filter(x -> x.getTarget() > 0)
277
				.sorted((BrandPerformance o1, BrandPerformance o2) -> (int) (o1.getTarget() - o2.getTarget()))
278
				.collect(Collectors.toList());
279
		accsBrandPerformanceList = accsBrandPerformanceList.stream().filter(x -> x.getTarget() > 0)
280
				.sorted((BrandPerformance o1, BrandPerformance o2) -> (int) (o1.getTarget() - o2.getTarget()))
281
				.collect(Collectors.toList());
23785 amit.gupta 282
 
23786 amit.gupta 283
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
284
				.collect(Collectors.toList());
285
		model.addAttribute("tagListings", tagListings);
286
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
287
		if (fofoId > 0) {
288
			CustomRetailer customRetailer = customRetailersMap.get(fofoId);
289
			model.addAttribute("retailerName",
290
					customRetailer.getBusinessName() + "-" + customRetailer.getAddress().getCity());
291
			FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
292
			model.addAttribute("retailerId", customRetailer.getPartnerId());
293
			model.addAttribute("counterSize", fs.getCounterSize().toString());
23779 amit.gupta 294
		} else {
23786 amit.gupta 295
			model.addAttribute("counterSize", counterSize.toString());
23405 amit.gupta 296
		}
23786 amit.gupta 297
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
298
		LOGGER.info("Custom retailers {}", customRetailers);
299
		model.addAttribute("customRetailers", customRetailers);
300
		model.addAttribute("brandPerformanceList", brandPerformanceList);
301
		model.addAttribute("accsBrandPerformanceList", accsBrandPerformanceList);
302
		model.addAttribute("counterSizes", CounterSize.values());
303
		model.addAttribute("itemCisMap", itemCisMap);
304
		return "open-indent";
23405 amit.gupta 305
	}
306
}