| 22860 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 23020 |
ashik.ali |
3 |
import java.io.ByteArrayInputStream;
|
|
|
4 |
import java.io.ByteArrayOutputStream;
|
|
|
5 |
import java.io.InputStream;
|
|
|
6 |
import java.time.LocalDateTime;
|
| 23556 |
amit.gupta |
7 |
import java.util.Arrays;
|
|
|
8 |
import java.util.HashSet;
|
| 22860 |
ashik.ali |
9 |
import java.util.List;
|
| 23020 |
ashik.ali |
10 |
import java.util.Map;
|
| 22860 |
ashik.ali |
11 |
|
|
|
12 |
import javax.servlet.http.HttpServletRequest;
|
|
|
13 |
|
| 23568 |
govind |
14 |
import org.apache.logging.log4j.Logger;
|
|
|
15 |
import org.apache.logging.log4j.LogManager;
|
| 22860 |
ashik.ali |
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23020 |
ashik.ali |
17 |
import org.springframework.core.io.InputStreamResource;
|
|
|
18 |
import org.springframework.http.HttpHeaders;
|
|
|
19 |
import org.springframework.http.HttpStatus;
|
|
|
20 |
import org.springframework.http.ResponseEntity;
|
| 22860 |
ashik.ali |
21 |
import org.springframework.stereotype.Controller;
|
|
|
22 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
23 |
import org.springframework.ui.Model;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
25 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
26 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
27 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
28 |
|
| 23020 |
ashik.ali |
29 |
import com.spice.profitmandi.common.enumuration.DateTimePattern;
|
| 22860 |
ashik.ali |
30 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
31 |
import com.spice.profitmandi.common.model.CreateSchemeRequest;
|
|
|
32 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23557 |
amit.gupta |
33 |
import com.spice.profitmandi.common.model.SchemeItems;
|
| 23020 |
ashik.ali |
34 |
import com.spice.profitmandi.common.model.SchemeModel;
|
|
|
35 |
import com.spice.profitmandi.common.util.ExcelUtils;
|
|
|
36 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 22860 |
ashik.ali |
37 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| 23556 |
amit.gupta |
38 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 23343 |
ashik.ali |
39 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 22860 |
ashik.ali |
40 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23556 |
amit.gupta |
41 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23506 |
amit.gupta |
42 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 23556 |
amit.gupta |
43 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 23020 |
ashik.ali |
44 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 22860 |
ashik.ali |
45 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
|
|
46 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
47 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 23570 |
amit.gupta |
48 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 22860 |
ashik.ali |
49 |
|
|
|
50 |
@Controller
|
|
|
51 |
@Transactional(rollbackFor=Throwable.class)
|
|
|
52 |
public class SchemeController {
|
|
|
53 |
|
| 23568 |
govind |
54 |
private static final Logger LOGGER = LogManager.getLogger(SchemeController.class);
|
| 22860 |
ashik.ali |
55 |
|
|
|
56 |
@Autowired
|
| 22927 |
ashik.ali |
57 |
private SchemeService schemeService;
|
| 22860 |
ashik.ali |
58 |
|
|
|
59 |
@Autowired
|
| 22927 |
ashik.ali |
60 |
private SchemeRepository schemeRepository;
|
| 22860 |
ashik.ali |
61 |
|
|
|
62 |
@Autowired
|
| 23556 |
amit.gupta |
63 |
private SchemeItemRepository schemeItemRepository;
|
|
|
64 |
|
|
|
65 |
@Autowired
|
| 23570 |
amit.gupta |
66 |
private MVCResponseSender mvcResponseSender;
|
|
|
67 |
|
|
|
68 |
@Autowired
|
| 22927 |
ashik.ali |
69 |
private CookiesProcessor cookiesProcessor;
|
| 23020 |
ashik.ali |
70 |
|
|
|
71 |
@Autowired
|
|
|
72 |
private InventoryService inventoryService;
|
| 23556 |
amit.gupta |
73 |
|
|
|
74 |
@Autowired
|
|
|
75 |
private TagListingRepository tagListingRepository;
|
| 23506 |
amit.gupta |
76 |
|
|
|
77 |
@Autowired
|
|
|
78 |
PurchaseRepository purchaseRepository;
|
| 22860 |
ashik.ali |
79 |
|
|
|
80 |
@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
|
| 22927 |
ashik.ali |
81 |
public String createScheme(HttpServletRequest request, Model model){
|
| 23020 |
ashik.ali |
82 |
//Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllItemIdItemDescriptionMap();
|
|
|
83 |
//model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
|
| 23419 |
ashik.ali |
84 |
model.addAttribute("brands", inventoryService.getAllTagListingBrands());
|
| 22860 |
ashik.ali |
85 |
return "create-scheme";
|
|
|
86 |
}
|
|
|
87 |
|
| 23419 |
ashik.ali |
88 |
@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.GET)
|
|
|
89 |
public String getTagListingItemsByBrand(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model){
|
|
|
90 |
Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllTagListingItemIdItemDescriptionMap(brand);
|
| 23020 |
ashik.ali |
91 |
model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
|
|
|
92 |
//model.addAttribute("brands", inventoryService.getAllBrands());
|
| 23419 |
ashik.ali |
93 |
return "tag-listing-items-description";
|
| 23020 |
ashik.ali |
94 |
}
|
| 23556 |
amit.gupta |
95 |
|
|
|
96 |
@RequestMapping(value = "/schemes/update-schemes-page", method = RequestMethod.GET)
|
|
|
97 |
public String updateShcemes(HttpServletRequest request) throws ProfitMandiBusinessException {
|
|
|
98 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
99 |
if(!loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)) {
|
|
|
100 |
throw new ProfitMandiBusinessException("User", loginDetails.getEmailId(), "Unauthorised access");
|
|
|
101 |
}
|
|
|
102 |
return "update-schemes-page";
|
|
|
103 |
}
|
| 23020 |
ashik.ali |
104 |
|
| 23556 |
amit.gupta |
105 |
@RequestMapping(value = "/schemes/update", method = RequestMethod.POST)
|
| 23570 |
amit.gupta |
106 |
public String updateShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model) throws Exception {
|
| 23557 |
amit.gupta |
107 |
for(int schemeId : schemeItems.getSchemeIds()) {
|
| 23556 |
amit.gupta |
108 |
if(schemeRepository.selectById(schemeId) != null)
|
| 23557 |
amit.gupta |
109 |
for(int itemId : schemeItems.getItemIds()) {
|
| 23556 |
amit.gupta |
110 |
if(tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)), new HashSet<>(Arrays.asList(4,7))).size() > 0) {
|
|
|
111 |
SchemeItem si = new SchemeItem();
|
|
|
112 |
si.setItemId(itemId);
|
|
|
113 |
si.setSchemeId(schemeId);
|
|
|
114 |
try {
|
|
|
115 |
schemeItemRepository.persist(si);
|
|
|
116 |
} catch (Exception e) {
|
|
|
117 |
LOGGER.info("Scheme aleady exist");
|
|
|
118 |
}
|
| 23570 |
amit.gupta |
119 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
| 23556 |
amit.gupta |
120 |
} else {
|
| 23570 |
amit.gupta |
121 |
model.addAttribute("response", mvcResponseSender.createResponseString(false));
|
| 23556 |
amit.gupta |
122 |
throw new ProfitMandiBusinessException("ItemId", itemId,"Invalid Item Id");
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
return "response";
|
|
|
127 |
}
|
| 23020 |
ashik.ali |
128 |
|
| 23556 |
amit.gupta |
129 |
|
| 22860 |
ashik.ali |
130 |
@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
|
| 22927 |
ashik.ali |
131 |
public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
|
|
132 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 22860 |
ashik.ali |
133 |
LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
|
| 22927 |
ashik.ali |
134 |
schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
|
|
|
135 |
LOGGER.info("Scheme saved successfully");
|
| 23271 |
ashik.ali |
136 |
long size = schemeRepository.selectAllCount();
|
| 22927 |
ashik.ali |
137 |
List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
|
|
|
138 |
model.addAttribute("schemes", schemes);
|
|
|
139 |
model.addAttribute("start", offset + 1);
|
| 23271 |
ashik.ali |
140 |
model.addAttribute("size", size);
|
| 22927 |
ashik.ali |
141 |
if (schemes.size() < limit){
|
|
|
142 |
model.addAttribute("end", offset + schemes.size());
|
| 22860 |
ashik.ali |
143 |
}
|
| 22927 |
ashik.ali |
144 |
else{
|
|
|
145 |
model.addAttribute("end", offset + limit);
|
|
|
146 |
}
|
|
|
147 |
return "schemes";
|
|
|
148 |
|
| 22860 |
ashik.ali |
149 |
}
|
|
|
150 |
|
|
|
151 |
@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
|
| 23343 |
ashik.ali |
152 |
public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
|
|
153 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
154 |
List<Scheme> schemes = null;
|
|
|
155 |
long size = 0;
|
|
|
156 |
if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
|
|
|
157 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
158 |
size = schemeRepository.selectAllCount();
|
|
|
159 |
}else{
|
|
|
160 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
161 |
size = schemeRepository.selectAllActiveCount();
|
|
|
162 |
}
|
| 22860 |
ashik.ali |
163 |
model.addAttribute("schemes", schemes);
|
|
|
164 |
model.addAttribute("start", offset + 1);
|
| 23271 |
ashik.ali |
165 |
model.addAttribute("size", size);
|
| 22860 |
ashik.ali |
166 |
if (schemes.size() < limit){
|
|
|
167 |
model.addAttribute("end", offset + schemes.size());
|
|
|
168 |
}
|
|
|
169 |
else{
|
|
|
170 |
model.addAttribute("end", offset + limit);
|
|
|
171 |
}
|
| 23343 |
ashik.ali |
172 |
//model.addAttribute("roleTypes", loginDetails.getRoleTypes());
|
| 22860 |
ashik.ali |
173 |
return "schemes";
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
|
| 23343 |
ashik.ali |
177 |
public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
|
|
178 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 23271 |
ashik.ali |
179 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
| 23343 |
ashik.ali |
180 |
List<Scheme> schemes = null;
|
|
|
181 |
if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
|
|
|
182 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
183 |
}else{
|
|
|
184 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
185 |
}
|
| 22860 |
ashik.ali |
186 |
model.addAttribute("schemes", schemes);
|
| 23343 |
ashik.ali |
187 |
model.addAttribute("roleTypes", loginDetails.getRoleTypes());
|
| 22860 |
ashik.ali |
188 |
return "schemes-paginated";
|
|
|
189 |
}
|
|
|
190 |
|
| 23020 |
ashik.ali |
191 |
@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
|
|
|
192 |
public String downloadPage(HttpServletRequest request, Model model){
|
|
|
193 |
return "schemes-download";
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
|
|
|
197 |
public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_DATE_TIME) String startDateTimeString, @RequestParam(name = ProfitMandiConstants.END_DATE_TIME) String endDateTimeString, Model model) throws ProfitMandiBusinessException{
|
|
|
198 |
LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
|
|
|
199 |
LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
|
|
|
200 |
|
|
|
201 |
List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
|
|
|
202 |
|
|
|
203 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
204 |
ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
|
|
|
205 |
|
|
|
206 |
final HttpHeaders headers=new HttpHeaders();
|
|
|
207 |
headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
208 |
headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
|
|
|
209 |
headers.setContentLength(byteArrayOutputStream.toByteArray().length);
|
|
|
210 |
final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
211 |
final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
|
|
|
212 |
return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
|
|
|
213 |
|
|
|
214 |
//return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
|
|
|
215 |
}
|
|
|
216 |
|
| 22860 |
ashik.ali |
217 |
@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
|
| 22927 |
ashik.ali |
218 |
public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model) throws ProfitMandiBusinessException{
|
| 23343 |
ashik.ali |
219 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 22860 |
ashik.ali |
220 |
Scheme scheme = schemeService.getSchemeById(schemeId);
|
|
|
221 |
model.addAttribute("scheme", scheme);
|
| 23343 |
ashik.ali |
222 |
model.addAttribute("roleTypes", loginDetails.getRoleTypes());
|
| 22860 |
ashik.ali |
223 |
return "scheme-details";
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
|
| 22927 |
ashik.ali |
227 |
public String activeSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
| 22860 |
ashik.ali |
228 |
schemeService.activeSchemeById(schemeId);
|
|
|
229 |
List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
|
|
|
230 |
model.addAttribute("schemes", schemes);
|
|
|
231 |
return "schemes-paginated";
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
|
| 22927 |
ashik.ali |
236 |
public String expireSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
| 22860 |
ashik.ali |
237 |
schemeService.expireSchemeById(schemeId);
|
|
|
238 |
List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
|
|
|
239 |
model.addAttribute("schemes", schemes);
|
|
|
240 |
return "schemes-paginated";
|
|
|
241 |
}
|
| 23506 |
amit.gupta |
242 |
|
| 22860 |
ashik.ali |
243 |
}
|