Subversion Repositories SmartDukaan

Rev

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