Subversion Repositories SmartDukaan

Rev

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