Subversion Repositories SmartDukaan

Rev

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

Rev 23784 Rev 23785
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
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.Arrays;
7
import java.util.Arrays;
-
 
8
import java.util.DoubleSummaryStatistics;
7
import java.util.HashSet;
9
import java.util.HashSet;
-
 
10
import java.util.Iterator;
8
import java.util.List;
11
import java.util.List;
9
import java.util.Map;
12
import java.util.Map;
-
 
13
import java.util.Set;
10
import java.util.stream.Collectors;
14
import java.util.stream.Collectors;
11
 
15
 
12
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletRequest;
13
 
17
 
14
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
19
import org.apache.logging.log4j.Logger;
16
import org.json.JSONObject;
20
import org.json.JSONObject;
17
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Qualifier;
-
 
19
import org.springframework.stereotype.Controller;
22
import org.springframework.stereotype.Controller;
20
import org.springframework.transaction.annotation.Transactional;
23
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.ui.Model;
24
import org.springframework.ui.Model;
22
import org.springframework.web.bind.annotation.RequestBody;
25
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestParam;
28
import org.springframework.web.bind.annotation.RequestParam;
26
 
29
 
-
 
30
import com.spice.profitmandi.common.enumuration.CounterSize;
27
import com.spice.profitmandi.common.enumuration.IndentStatus;
31
import com.spice.profitmandi.common.enumuration.IndentStatus;
28
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
33
import com.spice.profitmandi.common.model.BrandPerformance;
-
 
34
import com.spice.profitmandi.common.model.CustomRetailer;
29
import com.spice.profitmandi.common.model.StockAllocationModel;
35
import com.spice.profitmandi.common.model.StockAllocationModel;
-
 
36
import com.spice.profitmandi.dao.entity.catalog.Item;
30
import com.spice.profitmandi.dao.entity.catalog.TagListing;
37
import com.spice.profitmandi.dao.entity.catalog.TagListing;
31
import com.spice.profitmandi.dao.entity.dtr.Role;
-
 
32
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
38
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
33
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
39
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
-
 
40
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
34
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
41
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
35
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
-
 
36
import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;
-
 
37
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
42
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
38
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
43
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
39
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
44
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
40
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
45
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
41
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
46
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
42
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
-
 
43
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
47
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
44
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
48
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
45
import com.spice.profitmandi.service.indent.IndentService;
-
 
46
import com.spice.profitmandi.service.inventory.StockAllocationService;
49
import com.spice.profitmandi.service.inventory.StockAllocationService;
47
import com.spice.profitmandi.service.user.RetailerService;
50
import com.spice.profitmandi.service.user.RetailerService;
48
import com.spice.profitmandi.web.model.LoginDetails;
51
import com.spice.profitmandi.web.model.LoginDetails;
49
import com.spice.profitmandi.web.util.CookiesProcessor;
52
import com.spice.profitmandi.web.util.CookiesProcessor;
50
import com.spice.profitmandi.web.util.MVCResponseSender;
53
import com.spice.profitmandi.web.util.MVCResponseSender;
-
 
54
import com.spice.profitmandi.web.util.RoleManager;
51
 
55
 
52
@Controller
56
@Controller
53
@Transactional(rollbackFor = Throwable.class)
57
@Transactional(rollbackFor = Throwable.class)
54
public class IndentController {
58
public class IndentController {
55
 
59
 
56
	private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
60
	private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
57
 
61
 
58
	@Autowired
62
	@Autowired
59
	private CookiesProcessor cookiesProcessor;
63
	private CookiesProcessor cookiesProcessor;
60
	
-
 
61
	@Autowired
-
 
62
	FofoStoreRepository fofoStoreRepository;
-
 
63
 
64
 
64
	@Autowired
65
	@Autowired
65
	private CategoryRepository categoryRepository;
66
	FofoStoreRepository fofoStoreRepository;
66
 
67
 
67
	@Autowired
68
	@Autowired
68
	@Qualifier("catalogItemRepository")
-
 
69
	private ItemRepository itemRepository;
69
	private ItemRepository itemRepository;
70
 
70
 
71
	@Autowired
71
	@Autowired
72
	private IndentRepository indentRepository;
72
	private IndentRepository indentRepository;
73
 
73
 
74
	@Autowired
74
	@Autowired
75
	private StockAllocationService stockAllocationService;
75
	private StockAllocationService stockAllocationService;
76
 
76
 
77
	@Autowired
77
	@Autowired
78
	private IndentItemRepository indentItemRepository;
78
	private IndentItemRepository indentItemRepository;
79
	
-
 
80
	@Autowired
-
 
81
	private RetailerService retailerService;
-
 
82
 
79
 
83
	@Autowired
80
	@Autowired
84
	private IndentService indentService;
81
	private RetailerService retailerService;
85
 
82
 
86
	@Autowired
83
	@Autowired
87
	private TagListingRepository tagListingRepository;
84
	private TagListingRepository tagListingRepository;
88
 
85
 
89
	@Autowired
86
	@Autowired
90
	private FofoOrderRepository fofoOrderRepository;
87
	private FofoOrderRepository fofoOrderRepository;
91
 
88
 
92
	@Autowired
89
	@Autowired
93
	@Qualifier("fofoCurrentInventorySnapshotRepository")
-
 
94
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
90
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
95
	
-
 
96
	@Autowired
-
 
97
	private RoleRepository roleRepository;
-
 
98
 
91
 
99
	@Autowired
92
	@Autowired
100
	private MVCResponseSender mvcResponseSender;
93
	private MVCResponseSender mvcResponseSender;
101
 
94
 
-
 
95
	/*
102
	/*@RequestMapping(value = "/indent-item/save", method = RequestMethod.PUT)
96
	 * @RequestMapping(value = "/indent-item/save", method = RequestMethod.PUT)
103
	public String saveIndentItem(HttpServletRequest request, @RequestBody ItemIdQuantity itemIdQuantity, Model model)
97
	 * public String saveIndentItem(HttpServletRequest request, @RequestBody
104
			throws Exception {
98
	 * ItemIdQuantity itemIdQuantity, Model model) throws Exception { LoginDetails
105
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
99
	 * loginDetails = cookiesProcessor.getCookiesObject(request);
106
		LOGGER.info("Item id is {}, And quantity is {}", itemIdQuantity.getItemId(), itemIdQuantity.getItemId());
100
	 * LOGGER.info("Item id is {}, And quantity is {}", itemIdQuantity.getItemId(),
107
		int fofoId = loginDetails.getFofoId();
101
	 * itemIdQuantity.getItemId()); boolean response = false;
108
		boolean response = indentService.updateOpenIndentItem(fofoId, itemIdQuantity.getItemId(),
102
	 * if(loginDetails.isAdmin()) { StockAllocationModel stockAllocationModel = new
-
 
103
	 * StockAllocationModel();
109
				itemIdQuantity.getQuantity());
104
	 * stockAllocationModel.setQuantity(itemIdQuantity.getQuantity());
110
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
105
	 * stockAllocationModel.setItemId(itemIdQuantity.getItemId()); response =
-
 
106
	 * stockAllocationService.addToAllocation(stockAllocationModel); } //int fofoId
111
		return "response";
107
	 * = loginDetails.getFofoId(); model.addAttribute("response",
-
 
108
	 * mvcResponseSender.createResponseString(response)); return "response"; }
112
	}*/
109
	 */
-
 
110
	
-
 
111
	@Autowired
-
 
112
	RoleManager roleManager;
113
 
113
 
114
	@RequestMapping(value = "/open-indent/save", method = RequestMethod.POST)
114
	@RequestMapping(value = "/open-indent/save", method = RequestMethod.POST)
-
 
115
	public String saveOpenIndent(HttpServletRequest request, Model model,
115
	public String saveOpenIndent(HttpServletRequest request, Model model, @RequestBody List<StockAllocationModel> stockAllocationModelList) throws Exception {
116
			@RequestBody List<StockAllocationModel> stockAllocationModelList, @RequestParam int fofoId)
-
 
117
			throws Exception {
116
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
118
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
117
		boolean response = false;
119
		boolean response = false;
-
 
120
		if (fofoId > 0) {
118
		Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
121
			FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
-
 
122
			stockAllocationModelList.forEach(x -> {x.setFofoId(fofoId);x.setCounterSize(fs.getCounterSize());});
-
 
123
		}
-
 
124
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
119
		response = stockAllocationService.addToAllocation(stockAllocationModelList);
125
			response = stockAllocationService.addToAllocation(stockAllocationModelList);
120
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
126
			model.addAttribute("response", mvcResponseSender.createResponseString(response));
-
 
127
		}
121
		return "response";
128
		return "response";
122
	}
129
	}
123
 
130
 
124
	@RequestMapping(value = "/migrate", method = RequestMethod.PUT)
131
	@RequestMapping(value = "/migrate", method = RequestMethod.PUT)
125
	public String migrate(HttpServletRequest request, Model model) throws Exception {
132
	public String migrate(HttpServletRequest request, Model model) throws Exception {
Line 174... Line 181...
174
		}
181
		}
175
		return "pending-indent";
182
		return "pending-indent";
176
	}
183
	}
177
 
184
 
178
	@RequestMapping(value = "/indent/loadIndent")
185
	@RequestMapping(value = "/indent/loadIndent")
-
 
186
	public String loadOpenIndent(HttpServletRequest request, Model model,
-
 
187
			@RequestParam(required = false, defaultValue = "0") int fofoId,
179
	public String loadOpenIndent(HttpServletRequest request, Model model, @RequestParam(required=false, defaultValue="0") int fofoId ) throws ProfitMandiBusinessException {
188
			@RequestParam(defaultValue="TEN_LAC") CounterSize counterSize) throws ProfitMandiBusinessException {
180
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
189
		Set<Integer> roleIds = cookiesProcessor.getCookiesObject(request).getRoleIds();
-
 
190
		LOGGER.info("Counter size is {}", counterSize);
-
 
191
		LOGGER.info("Fofo Id is {}", fofoId);
181
		if (loginDetails.isAdmin()) {
192
		if (roleManager.isAdmin(roleIds)) {
-
 
193
			List<BrandPerformance> brandPerformanceList = new ArrayList<>();
-
 
194
			List<BrandPerformance> accsBrandPerformanceList = new ArrayList<>();
182
			List<StockAllocationModel> stockAllocationList;
195
			List<StockAllocationModel> stockAllocationList;
183
			if(fofoId > 0) {
196
			if (fofoId > 0) {
184
				stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
197
				stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
185
			} else {
198
			} else {
186
				stockAllocationList = stockAllocationService.getStockAllocation(true);
199
				stockAllocationList = stockAllocationService.getStockAllocation(counterSize, true);
187
			}
200
			}
-
 
201
			LOGGER.info("Stock Allocation list  is {}", stockAllocationList);
188
 
202
 
189
			Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
203
			Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
190
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
204
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
191
 
205
 
-
 
206
			LOGGER.info("Item Stock Allocation Model Map {}", itemStockAllocationMap);
192
			List<TagListing> tagListings = tagListingRepository.selectAll();
207
			List<TagListing> tagListings = tagListingRepository.selectAll();
193
 
-
 
-
 
208
			Iterator<TagListing> iterator = tagListings.iterator();
-
 
209
			while (iterator.hasNext()) {
194
			for (TagListing tagListing : tagListings) {
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
				}
195
				StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
217
				StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
-
 
218
				LOGGER.info(" stockAllocationModel {}", stockAllocationModel);
-
 
219
				LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
196
				if(stockAllocationModel!= null ) {
220
				if (stockAllocationModel != null) {
197
					tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
221
					tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
-
 
222
				} else {
-
 
223
					tagListing.setAllocatedQuantity(0);
-
 
224
				}
-
 
225
				tagListing.setBrand(item.getBrand());
-
 
226
				tagListing.setItemDescription(item.getItemDescription());
-
 
227
				if (item.getCategoryId() == 10006) {
-
 
228
					tagListing.setCategoryId(item.getCategoryId());
198
				}
229
				}
199
				tagListing.setItemDescription(itemRepository.selectById(tagListing.getItemId()).getItemDescription());
230
				LOGGER.info(" tagListing.setAllocatedQuantity {}", tagListing.getAllocatedQuantity());
200
			}
231
			}
201
 
232
 
-
 
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
 
202
			List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x->x.getId()).collect(Collectors.toList());
261
			List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
-
 
262
					.collect(Collectors.toList());
203
			model.addAttribute("tagListings", tagListings);
263
			model.addAttribute("tagListings", tagListings);
-
 
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
			}
204
			String customRetailers = JSONObject.valueToString(retailerService.getFofoRetailers(fofoIds).values());
275
			String customRetailers = JSONObject.valueToString(customRetailersMap.values());
205
			LOGGER.info("Custom retailesr {}", customRetailers);
276
			LOGGER.info("Custom retailers {}", customRetailers);
206
			model.addAttribute("customRetailers", customRetailers);
277
			model.addAttribute("customRetailers", customRetailers);
-
 
278
			model.addAttribute("brandPerformanceList", brandPerformanceList);
-
 
279
			model.addAttribute("accsBrandPerformanceList", accsBrandPerformanceList);
-
 
280
			model.addAttribute("counterSizes", CounterSize.values());
207
			return "open-indent";
281
			return "open-indent";
208
		} else {
282
		} else {
209
			throw new ProfitMandiBusinessException("Unauthorised", loginDetails.getEmailId(),
283
			throw new ProfitMandiBusinessException("Unauthorised", cookiesProcessor.getCookiesObject(request).getEmailId(),
210
					"Can't access the resource");
284
					"Can't access the resource");
211
		}
285
		}
212
	}
286
	}
213
}
287
}