Subversion Repositories SmartDukaan

Rev

Rev 23717 | Rev 23784 | 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;
6
import java.util.ArrayList;
7
import java.util.Arrays;
8
import java.util.HashSet;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.stream.Collectors;
12
 
13
import javax.servlet.http.HttpServletRequest;
14
 
23717 amit.gupta 15
import org.apache.logging.log4j.LogManager;
23568 govind 16
import org.apache.logging.log4j.Logger;
23779 amit.gupta 17
import org.json.JSONObject;
23405 amit.gupta 18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.stereotype.Controller;
20
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.ui.Model;
22
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
23779 amit.gupta 25
import org.springframework.web.bind.annotation.RequestParam;
23405 amit.gupta 26
 
27
import com.spice.profitmandi.common.enumuration.IndentStatus;
28
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23779 amit.gupta 29
import com.spice.profitmandi.common.model.StockAllocationModel;
23405 amit.gupta 30
import com.spice.profitmandi.dao.entity.catalog.TagListing;
31
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
32
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
33
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
34
import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;
35
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
36
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
23779 amit.gupta 37
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
23405 amit.gupta 38
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
39
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
40
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
41
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
42
import com.spice.profitmandi.service.indent.IndentService;
23779 amit.gupta 43
import com.spice.profitmandi.service.inventory.StockAllocationService;
44
import com.spice.profitmandi.service.user.RetailerService;
23405 amit.gupta 45
import com.spice.profitmandi.web.model.LoginDetails;
46
import com.spice.profitmandi.web.util.CookiesProcessor;
47
import com.spice.profitmandi.web.util.MVCResponseSender;
48
 
49
@Controller
50
@Transactional(rollbackFor = Throwable.class)
51
public class IndentController {
52
 
23568 govind 53
	private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
23405 amit.gupta 54
 
55
	@Autowired
56
	private CookiesProcessor cookiesProcessor;
23779 amit.gupta 57
 
58
	@Autowired
59
	FofoStoreRepository fofoStoreRepository;
23405 amit.gupta 60
 
61
	@Autowired
62
	private CategoryRepository categoryRepository;
63
 
64
	@Autowired
65
	private ItemRepository itemRepository;
66
 
67
	@Autowired
68
	private IndentRepository indentRepository;
69
 
70
	@Autowired
23779 amit.gupta 71
	private StockAllocationService stockAllocationService;
72
 
73
	@Autowired
23405 amit.gupta 74
	private IndentItemRepository indentItemRepository;
23779 amit.gupta 75
 
76
	@Autowired
77
	private RetailerService retailerService;
23405 amit.gupta 78
 
79
	@Autowired
80
	private IndentService indentService;
81
 
82
	@Autowired
83
	private TagListingRepository tagListingRepository;
84
 
85
	@Autowired
86
	private FofoOrderRepository fofoOrderRepository;
87
 
88
	@Autowired
89
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
90
 
91
	@Autowired
92
	private MVCResponseSender mvcResponseSender;
93
 
23779 amit.gupta 94
	/*@RequestMapping(value = "/indent-item/save", method = RequestMethod.PUT)
23405 amit.gupta 95
	public String saveIndentItem(HttpServletRequest request, @RequestBody ItemIdQuantity itemIdQuantity, Model model)
96
			throws Exception {
97
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
98
		LOGGER.info("Item id is {}, And quantity is {}", itemIdQuantity.getItemId(), itemIdQuantity.getItemId());
23779 amit.gupta 99
		boolean response = false;
100
		if(loginDetails.isAdmin()) {
101
			StockAllocationModel stockAllocationModel = new StockAllocationModel();
102
			stockAllocationModel.setQuantity(itemIdQuantity.getQuantity());
103
			stockAllocationModel.setItemId(itemIdQuantity.getItemId());
104
			response = stockAllocationService.addToAllocation(stockAllocationModel);
105
		}
106
		//int fofoId = loginDetails.getFofoId();
23405 amit.gupta 107
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
108
		return "response";
23779 amit.gupta 109
	}*/
23405 amit.gupta 110
 
23779 amit.gupta 111
	@RequestMapping(value = "/open-indent/save", method = RequestMethod.POST)
112
	public String saveOpenIndent(HttpServletRequest request, Model model, @RequestBody List<StockAllocationModel> stockAllocationModelList) throws Exception {
23405 amit.gupta 113
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23779 amit.gupta 114
		boolean response = false;
115
		if(loginDetails.isAdmin()) {
116
			response = stockAllocationService.addToAllocation(stockAllocationModelList);
117
			model.addAttribute("response", mvcResponseSender.createResponseString(response));
23405 amit.gupta 118
		}
119
		return "response";
120
	}
121
 
23415 amit.gupta 122
	@RequestMapping(value = "/migrate", method = RequestMethod.PUT)
123
	public String migrate(HttpServletRequest request, Model model) throws Exception {
124
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
125
		int fofoId = loginDetails.getFofoId();
126
		List<TagListing> tagListings = tagListingRepository.selectAll();
23779 amit.gupta 127
		for (TagListing tagListing : tagListings) {
23415 amit.gupta 128
			int itemId = tagListing.getItemId();
129
		}
130
		return "";
131
	}
132
 
23405 amit.gupta 133
	@RequestMapping(value = "/indent/inProcess")
23779 amit.gupta 134
	public String loadInProcessIndents(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
23405 amit.gupta 135
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
136
		int fofoId = loginDetails.getFofoId();
137
 
23779 amit.gupta 138
		List<Integer> pendingIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.PENDING).stream()
139
				.map(x -> x.getId()).collect(Collectors.toList());
140
		List<Integer> allocatedIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.ALLOCATED)
141
				.stream().map(x -> x.getId()).collect(Collectors.toList());
23405 amit.gupta 142
		pendingIndentIds.addAll(allocatedIndentIds);
23779 amit.gupta 143
 
144
		if (pendingIndentIds.size() > 0) {
145
			Map<Integer, IndentItem> pendingIndentItemIdMap = indentItemRepository.selectIndentItems(pendingIndentIds)
146
					.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
147
			List<TagListing> tagListings = tagListingRepository
148
					.selectByItemIdsAndTagIds(pendingIndentItemIdMap.keySet(), new HashSet<>(Arrays.asList(4)));
149
 
150
			List<FofoOrderItem> fofoOrderItems = fofoOrderRepository.selectByFofoItemIds(fofoId, pendingIndentIds,
151
					LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).minusDays(30), LocalDateTime.now());
23405 amit.gupta 152
			Map<Integer, Integer> itemQuantity = fofoOrderItems.stream().collect(
153
					Collectors.groupingBy(FofoOrderItem::getItemId, Collectors.summingInt(FofoOrderItem::getQuantity)));
23779 amit.gupta 154
 
23405 amit.gupta 155
			List<CurrentInventorySnapshot> cis = currentInventorySnapshotRepository.selectByFofoId(fofoId);
23779 amit.gupta 156
			Map<Integer, CurrentInventorySnapshot> itemIdSnapshotMap = cis.stream()
157
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
158
 
159
			for (TagListing tagListing : tagListings) {
23405 amit.gupta 160
				Integer itemId = tagListing.getItemId();
161
				tagListing.setItemDescription(itemRepository.selectById(itemId).getItemDescription());
23779 amit.gupta 162
				if (itemQuantity.containsKey(itemId)) {
23405 amit.gupta 163
					tagListing.setLast30DaysSale(itemQuantity.get(itemId));
164
				}
23779 amit.gupta 165
				if (itemIdSnapshotMap.containsKey(itemId)) {
23405 amit.gupta 166
					tagListing.setStockInHand(itemIdSnapshotMap.get(itemId).getAvailability());
167
				}
168
			}
23779 amit.gupta 169
 
23405 amit.gupta 170
			model.addAttribute("pendingIndentItemIdMap", pendingIndentItemIdMap);
171
			model.addAttribute("tagListings", tagListings);
172
		}
173
		return "pending-indent";
174
	}
175
 
176
	@RequestMapping(value = "/indent/loadIndent")
23779 amit.gupta 177
	public String loadOpenIndent(HttpServletRequest request, Model model, @RequestParam(required=false, defaultValue="0") int fofoId ) throws ProfitMandiBusinessException {
23405 amit.gupta 178
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23779 amit.gupta 179
		if (loginDetails.isAdmin()) {
180
			List<StockAllocationModel> stockAllocationList;
181
			if(fofoId > 0) {
182
				stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
183
			} else {
184
				stockAllocationList = stockAllocationService.getStockAllocation(true);
23405 amit.gupta 185
			}
186
 
23779 amit.gupta 187
			Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
188
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
23405 amit.gupta 189
 
23779 amit.gupta 190
			List<TagListing> tagListings = tagListingRepository.selectAll();
23405 amit.gupta 191
 
23779 amit.gupta 192
			for (TagListing tagListing : tagListings) {
193
				StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
194
				if(stockAllocationModel!= null ) {
195
					tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
196
				}
197
				tagListing.setItemDescription(itemRepository.selectById(tagListing.getItemId()).getItemDescription());
198
			}
23405 amit.gupta 199
 
23779 amit.gupta 200
			List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x->x.getId()).collect(Collectors.toList());
201
			model.addAttribute("tagListings", tagListings);
202
			String customRetailers = JSONObject.valueToString(retailerService.getFofoRetailers(fofoIds).values());
203
			LOGGER.info("Custom retailesr {}", customRetailers);
204
			model.addAttribute("customRetailers", customRetailers);
205
			return "open-indent";
206
		} else {
207
			throw new ProfitMandiBusinessException("Unauthorised", loginDetails.getEmailId(),
208
					"Can't access the resource");
23405 amit.gupta 209
		}
210
	}
211
}