| 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.HashMap;
|
|
|
9 |
import java.util.HashSet;
|
|
|
10 |
import java.util.List;
|
|
|
11 |
import java.util.Map;
|
|
|
12 |
import java.util.stream.Collectors;
|
|
|
13 |
|
|
|
14 |
import javax.servlet.http.HttpServletRequest;
|
|
|
15 |
|
| 23717 |
amit.gupta |
16 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
17 |
import org.apache.logging.log4j.Logger;
|
| 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;
|
|
|
25 |
|
|
|
26 |
import com.spice.profitmandi.common.enumuration.IndentStatus;
|
|
|
27 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
28 |
import com.spice.profitmandi.common.model.ItemIdQuantity;
|
|
|
29 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
|
|
30 |
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
|
|
|
31 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
|
|
32 |
import com.spice.profitmandi.dao.entity.fofo.Indent;
|
|
|
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;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
|
|
|
38 |
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
|
|
|
39 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
|
|
40 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
|
|
41 |
import com.spice.profitmandi.service.indent.IndentService;
|
|
|
42 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
43 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
44 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
45 |
|
|
|
46 |
@Controller
|
|
|
47 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
48 |
public class IndentController {
|
|
|
49 |
|
| 23568 |
govind |
50 |
private static final Logger LOGGER = LogManager.getLogger(IndentController.class);
|
| 23405 |
amit.gupta |
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
private CookiesProcessor cookiesProcessor;
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
private CategoryRepository categoryRepository;
|
|
|
57 |
|
|
|
58 |
@Autowired
|
|
|
59 |
private ItemRepository itemRepository;
|
|
|
60 |
|
|
|
61 |
@Autowired
|
|
|
62 |
private IndentRepository indentRepository;
|
|
|
63 |
|
|
|
64 |
@Autowired
|
|
|
65 |
private IndentItemRepository indentItemRepository;
|
|
|
66 |
|
|
|
67 |
@Autowired
|
|
|
68 |
private IndentService indentService;
|
|
|
69 |
|
|
|
70 |
@Autowired
|
|
|
71 |
private TagListingRepository tagListingRepository;
|
|
|
72 |
|
|
|
73 |
@Autowired
|
|
|
74 |
private FofoOrderRepository fofoOrderRepository;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
|
|
77 |
private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
|
|
78 |
|
|
|
79 |
@Autowired
|
|
|
80 |
private MVCResponseSender mvcResponseSender;
|
|
|
81 |
|
|
|
82 |
@RequestMapping(value = "/indent-item/save", method = RequestMethod.PUT)
|
|
|
83 |
public String saveIndentItem(HttpServletRequest request, @RequestBody ItemIdQuantity itemIdQuantity, Model model)
|
|
|
84 |
throws Exception {
|
|
|
85 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
86 |
LOGGER.info("Item id is {}, And quantity is {}", itemIdQuantity.getItemId(), itemIdQuantity.getItemId());
|
|
|
87 |
int fofoId = loginDetails.getFofoId();
|
|
|
88 |
boolean response = indentService.updateOpenIndentItem(fofoId, itemIdQuantity.getItemId(),
|
|
|
89 |
itemIdQuantity.getQuantity());
|
|
|
90 |
model.addAttribute("response", mvcResponseSender.createResponseString(response));
|
|
|
91 |
return "response";
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
@RequestMapping(value = "/open-indent/save", method = RequestMethod.PUT)
|
|
|
95 |
public String saveOpenIndent(HttpServletRequest request, Model model) throws Exception {
|
|
|
96 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
97 |
int fofoId = loginDetails.getFofoId();
|
|
|
98 |
List<Indent> openIndents = indentRepository.selectIndentByStatus(fofoId, IndentStatus.OPEN);
|
|
|
99 |
if (openIndents.size() > 0) {
|
|
|
100 |
Indent openIndent = openIndents.get(0);
|
|
|
101 |
if (indentItemRepository.selectIndentItems(Arrays.asList(openIndent.getId())).size() == 0) {
|
|
|
102 |
model.addAttribute("response", mvcResponseSender.createResponseString(false));
|
|
|
103 |
}
|
|
|
104 |
openIndent.setCreateTimestamp(LocalDateTime.now());
|
|
|
105 |
openIndent.setStatus(IndentStatus.ALLOCATED);
|
|
|
106 |
indentRepository.persist(openIndent);
|
|
|
107 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
|
|
108 |
}
|
|
|
109 |
return "response";
|
|
|
110 |
}
|
|
|
111 |
|
| 23415 |
amit.gupta |
112 |
@RequestMapping(value = "/migrate", method = RequestMethod.PUT)
|
|
|
113 |
public String migrate(HttpServletRequest request, Model model) throws Exception {
|
|
|
114 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
115 |
int fofoId = loginDetails.getFofoId();
|
|
|
116 |
List<TagListing> tagListings = tagListingRepository.selectAll();
|
|
|
117 |
for(TagListing tagListing : tagListings) {
|
|
|
118 |
int itemId = tagListing.getItemId();
|
|
|
119 |
}
|
|
|
120 |
return "";
|
|
|
121 |
}
|
|
|
122 |
|
| 23405 |
amit.gupta |
123 |
@RequestMapping(value = "/indent/inProcess")
|
|
|
124 |
public String loadInProcessIndents(HttpServletRequest request, Model model) throws ProfitMandiBusinessException{
|
|
|
125 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
126 |
int fofoId = loginDetails.getFofoId();
|
|
|
127 |
|
|
|
128 |
List<Integer> pendingIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.PENDING).stream().map(x->x.getId()).collect(Collectors.toList());
|
|
|
129 |
List<Integer> allocatedIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.ALLOCATED).stream().map(x->x.getId()).collect(Collectors.toList());
|
|
|
130 |
pendingIndentIds.addAll(allocatedIndentIds);
|
|
|
131 |
|
|
|
132 |
if(pendingIndentIds.size() > 0) {
|
|
|
133 |
Map<Integer, IndentItem> pendingIndentItemIdMap = indentItemRepository.selectIndentItems(pendingIndentIds).stream().collect(Collectors.toMap(x->x.getItemId(), x->x));
|
|
|
134 |
List<TagListing> tagListings = tagListingRepository.selectByItemIdsAndTagIds(pendingIndentItemIdMap.keySet(), new HashSet<>(Arrays.asList(4)));
|
|
|
135 |
|
|
|
136 |
List<FofoOrderItem> fofoOrderItems = fofoOrderRepository.selectByFofoItemIds(fofoId, pendingIndentIds, LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).minusDays(30), LocalDateTime.now());
|
|
|
137 |
Map<Integer, Integer> itemQuantity = fofoOrderItems.stream().collect(
|
|
|
138 |
Collectors.groupingBy(FofoOrderItem::getItemId, Collectors.summingInt(FofoOrderItem::getQuantity)));
|
|
|
139 |
|
|
|
140 |
List<CurrentInventorySnapshot> cis = currentInventorySnapshotRepository.selectByFofoId(fofoId);
|
| 23717 |
amit.gupta |
141 |
Map<Integer, CurrentInventorySnapshot> itemIdSnapshotMap = cis.stream().collect(Collectors.toMap(x->x.getItemId(), x->x));
|
| 23405 |
amit.gupta |
142 |
|
|
|
143 |
for(TagListing tagListing : tagListings){
|
|
|
144 |
Integer itemId = tagListing.getItemId();
|
|
|
145 |
tagListing.setItemDescription(itemRepository.selectById(itemId).getItemDescription());
|
|
|
146 |
if(itemQuantity.containsKey(itemId)) {
|
|
|
147 |
tagListing.setLast30DaysSale(itemQuantity.get(itemId));
|
|
|
148 |
}
|
|
|
149 |
if(itemIdSnapshotMap.containsKey(itemId)){
|
|
|
150 |
tagListing.setStockInHand(itemIdSnapshotMap.get(itemId).getAvailability());
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
model.addAttribute("pendingIndentItemIdMap", pendingIndentItemIdMap);
|
|
|
155 |
model.addAttribute("tagListings", tagListings);
|
|
|
156 |
}
|
|
|
157 |
return "pending-indent";
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
@RequestMapping(value = "/indent/loadIndent")
|
|
|
161 |
public String loadOpenIndent(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
|
|
162 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
163 |
int fofoId = loginDetails.getFofoId();
|
|
|
164 |
List<Indent> openIndents = indentRepository.selectIndentByStatus(fofoId, IndentStatus.OPEN);
|
|
|
165 |
Indent openIndent = null;
|
|
|
166 |
if (openIndents.size() > 0) {
|
|
|
167 |
openIndent = openIndents.get(0);
|
|
|
168 |
} else {
|
|
|
169 |
LOGGER.info("Indent does not exist");
|
|
|
170 |
}
|
|
|
171 |
Map<Integer, IndentItem> itemIndentMap = new HashMap<>();
|
|
|
172 |
if (openIndent != null) {
|
|
|
173 |
List<IndentItem> openIndentItems = indentItemRepository
|
|
|
174 |
.selectIndentItems(Arrays.asList(openIndent.getId()));
|
|
|
175 |
if (openIndentItems.size() > 0) {
|
|
|
176 |
itemIndentMap = openIndentItems.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
List<TagListing> tagListings = tagListingRepository.selectAll();
|
|
|
181 |
|
|
|
182 |
List<Integer> itemIds = new ArrayList<>();
|
|
|
183 |
for (TagListing tagListing : tagListings) {
|
|
|
184 |
itemIds.add(tagListing.getItemId());
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
List<FofoOrderItem> fofoOrderItems = fofoOrderRepository.selectByFofoItemIds(fofoId, itemIds,
|
|
|
188 |
LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).minusDays(30), LocalDateTime.now());
|
|
|
189 |
Map<Integer, Integer> itemQuantity = fofoOrderItems.stream().collect(
|
|
|
190 |
Collectors.groupingBy(FofoOrderItem::getItemId, Collectors.summingInt(FofoOrderItem::getQuantity)));
|
|
|
191 |
|
|
|
192 |
List<CurrentInventorySnapshot> cis = currentInventorySnapshotRepository.selectByFofoId(fofoId);
|
|
|
193 |
Map<Integer, CurrentInventorySnapshot> itemIdSnapshotMap = cis.stream()
|
| 23717 |
amit.gupta |
194 |
.collect(Collectors.toMap(x->x.getItemId(), x->x));
|
| 23405 |
amit.gupta |
195 |
|
|
|
196 |
List<Integer> pendingIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.PENDING).stream()
|
|
|
197 |
.map(x -> x.getId()).collect(Collectors.toList());
|
|
|
198 |
List<Integer> allocatedIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.ALLOCATED)
|
|
|
199 |
.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
200 |
pendingIndentIds.addAll(allocatedIndentIds);
|
|
|
201 |
|
|
|
202 |
if (pendingIndentIds.size() > 0) {
|
|
|
203 |
List<Integer> pendingItemIds = indentItemRepository.selectIndentItems(pendingIndentIds).stream()
|
|
|
204 |
.map(x -> x.getItemId()).collect(Collectors.toList());
|
|
|
205 |
tagListings = tagListings.stream().filter(x -> pendingItemIds.indexOf(x.getItemId()) == -1)
|
|
|
206 |
.collect(Collectors.toList());
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
for (TagListing tagListing : tagListings) {
|
|
|
210 |
Integer itemId = tagListing.getItemId();
|
|
|
211 |
tagListing.setItemDescription(itemRepository.selectById(itemId).getItemDescription());
|
|
|
212 |
if (itemQuantity.containsKey(itemId)) {
|
|
|
213 |
tagListing.setLast30DaysSale(itemQuantity.get(itemId));
|
|
|
214 |
}
|
|
|
215 |
if (itemIdSnapshotMap.containsKey(itemId)) {
|
|
|
216 |
tagListing.setStockInHand(itemIdSnapshotMap.get(itemId).getAvailability());
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
model.addAttribute("tagListings", tagListings);
|
|
|
221 |
model.addAttribute("itemIndentMap", itemIndentMap);
|
|
|
222 |
return "open-indent";
|
|
|
223 |
}
|
|
|
224 |
}
|