| 27391 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 27876 |
amit.gupta |
3 |
import java.io.ByteArrayInputStream;
|
|
|
4 |
import java.io.InputStream;
|
|
|
5 |
import java.time.LocalDate;
|
| 27391 |
tejbeer |
6 |
import java.time.LocalDateTime;
|
| 27876 |
amit.gupta |
7 |
import java.time.YearMonth;
|
|
|
8 |
import java.time.temporal.ChronoField;
|
| 27391 |
tejbeer |
9 |
import java.util.ArrayList;
|
| 27876 |
amit.gupta |
10 |
import java.util.Arrays;
|
|
|
11 |
import java.util.Collection;
|
|
|
12 |
import java.util.Comparator;
|
| 27391 |
tejbeer |
13 |
import java.util.List;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
import java.util.Set;
|
|
|
16 |
import java.util.stream.Collectors;
|
|
|
17 |
|
|
|
18 |
import javax.servlet.http.HttpServletRequest;
|
|
|
19 |
import javax.transaction.Transactional;
|
|
|
20 |
|
| 27876 |
amit.gupta |
21 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 27391 |
tejbeer |
22 |
import org.apache.logging.log4j.LogManager;
|
|
|
23 |
import org.apache.logging.log4j.Logger;
|
|
|
24 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 29809 |
amit.gupta |
25 |
import org.springframework.cache.CacheManager;
|
| 27876 |
amit.gupta |
26 |
import org.springframework.core.io.InputStreamResource;
|
|
|
27 |
import org.springframework.http.HttpHeaders;
|
|
|
28 |
import org.springframework.http.HttpStatus;
|
|
|
29 |
import org.springframework.http.ResponseEntity;
|
| 27391 |
tejbeer |
30 |
import org.springframework.stereotype.Controller;
|
|
|
31 |
import org.springframework.ui.Model;
|
| 27876 |
amit.gupta |
32 |
import org.springframework.web.bind.annotation.PathVariable;
|
| 29682 |
amit.gupta |
33 |
import org.springframework.web.bind.annotation.PostMapping;
|
| 27391 |
tejbeer |
34 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
35 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
36 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
37 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 29682 |
amit.gupta |
38 |
import org.springframework.web.bind.annotation.RequestPart;
|
|
|
39 |
import org.springframework.web.multipart.MultipartFile;
|
| 27391 |
tejbeer |
40 |
|
|
|
41 |
import com.google.gson.Gson;
|
|
|
42 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
43 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 27876 |
amit.gupta |
44 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
45 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
46 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 27391 |
tejbeer |
47 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
|
|
48 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
|
|
49 |
import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;
|
|
|
50 |
import com.spice.profitmandi.dao.model.CreateOfferRequest;
|
| 29785 |
amit.gupta |
51 |
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
|
| 27876 |
amit.gupta |
52 |
import com.spice.profitmandi.dao.model.OfferRowModel;
|
| 27391 |
tejbeer |
53 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
54 |
import com.spice.profitmandi.dao.repository.catalog.OfferMarginRepository;
|
|
|
55 |
import com.spice.profitmandi.dao.repository.catalog.OfferPartnerRepository;
|
|
|
56 |
import com.spice.profitmandi.dao.repository.catalog.OfferRepository;
|
|
|
57 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
58 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
|
|
59 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 29785 |
amit.gupta |
60 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 27876 |
amit.gupta |
61 |
import com.spice.profitmandi.service.offers.OfferService;
|
| 27391 |
tejbeer |
62 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
63 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
64 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
65 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
66 |
|
|
|
67 |
@Controller
|
|
|
68 |
@Transactional(rollbackOn = Throwable.class)
|
|
|
69 |
public class OfferController {
|
|
|
70 |
private static final Logger LOGGER = LogManager.getLogger(OfferController.class);
|
|
|
71 |
@Autowired
|
|
|
72 |
private OfferRepository offerRepository;
|
|
|
73 |
|
|
|
74 |
@Autowired
|
|
|
75 |
private OfferMarginRepository offerMarginRepository;
|
|
|
76 |
|
|
|
77 |
@Autowired
|
|
|
78 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
79 |
|
|
|
80 |
@Autowired
|
| 27876 |
amit.gupta |
81 |
private ResponseSender responseSender;
|
|
|
82 |
|
|
|
83 |
@Autowired
|
| 27391 |
tejbeer |
84 |
private OfferPartnerRepository offerPartnerRepository;
|
|
|
85 |
|
|
|
86 |
@Autowired
|
|
|
87 |
private ItemRepository itemRepository;
|
|
|
88 |
|
|
|
89 |
@Autowired
|
|
|
90 |
private MVCResponseSender mvcResponseSender;
|
|
|
91 |
|
|
|
92 |
@Autowired
|
|
|
93 |
private Gson gson;
|
|
|
94 |
|
|
|
95 |
@Autowired
|
|
|
96 |
private RetailerService retailerService;
|
|
|
97 |
|
|
|
98 |
@Autowired
|
|
|
99 |
private Mongo mongoClient;
|
|
|
100 |
|
|
|
101 |
@Autowired
|
|
|
102 |
private CookiesProcessor cookiesProcessor;
|
|
|
103 |
|
|
|
104 |
@Autowired
|
| 27876 |
amit.gupta |
105 |
private OfferService offerService;
|
| 29809 |
amit.gupta |
106 |
|
|
|
107 |
@Autowired
|
|
|
108 |
private CacheManager thirtyMinsTimeOutCacheManager;
|
| 29855 |
amit.gupta |
109 |
|
|
|
110 |
@Autowired
|
|
|
111 |
private CacheManager oneDayCacheManager;
|
| 27876 |
amit.gupta |
112 |
|
|
|
113 |
@Autowired
|
| 27391 |
tejbeer |
114 |
private PartnerTypeChangeService partnerTypeChangeService;
|
|
|
115 |
|
|
|
116 |
@RequestMapping(value = "/getCreateOffer", method = RequestMethod.GET)
|
|
|
117 |
public String getCreateOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
|
|
118 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
119 |
List<Integer> fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
|
|
|
120 |
.collect(Collectors.toList());
|
|
|
121 |
|
| 27876 |
amit.gupta |
122 |
Set<String> brands = mongoClient.getMongoBrands(loginDetails.getFofoId(), null, 3).stream()
|
|
|
123 |
.map(x -> (String) x.get("name")).collect(Collectors.toSet());
|
| 27391 |
tejbeer |
124 |
|
|
|
125 |
Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
|
|
|
126 |
|
|
|
127 |
model.addAttribute("customRetailersMap", customRetailersMap);
|
|
|
128 |
model.addAttribute("itemCriteriaType", ItemCriteriaType.values());
|
|
|
129 |
model.addAttribute("brands", brands);
|
|
|
130 |
model.addAttribute("partnerCategories", PartnerType.values());
|
| 27876 |
amit.gupta |
131 |
model.addAttribute("warehouseRegion", ProfitMandiConstants.WAREHOUSE_MAP);
|
| 27391 |
tejbeer |
132 |
return "scheme_offer";
|
|
|
133 |
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
@RequestMapping(value = "/createOffer", method = RequestMethod.POST)
|
|
|
137 |
public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
|
|
|
138 |
Model model) throws Exception {
|
| 27876 |
amit.gupta |
139 |
LOGGER.info("createOfferRequest [{}]", createOfferRequest);
|
|
|
140 |
offerService.addOfferService(createOfferRequest);
|
| 29809 |
amit.gupta |
141 |
thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());
|
| 27391 |
tejbeer |
142 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
|
|
143 |
return "response";
|
|
|
144 |
|
|
|
145 |
}
|
|
|
146 |
|
| 27876 |
amit.gupta |
147 |
@RequestMapping(value = "/offers/published", method = RequestMethod.GET)
|
|
|
148 |
public String getPublishedOffers(HttpServletRequest request, @RequestParam int fofoId, Model model)
|
| 27391 |
tejbeer |
149 |
throws Exception {
|
| 27876 |
amit.gupta |
150 |
LOGGER.info("Published");
|
|
|
151 |
offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));
|
|
|
152 |
return "scheme_offer/published";
|
| 27391 |
tejbeer |
153 |
|
| 27876 |
amit.gupta |
154 |
}
|
| 27391 |
tejbeer |
155 |
|
| 27876 |
amit.gupta |
156 |
@RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
|
|
|
157 |
public String activateOffer(HttpServletRequest request, @PathVariable int offerId, Model model)
|
|
|
158 |
throws ProfitMandiBusinessException, Exception {
|
|
|
159 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
160 |
offer.setActive(true);
|
| 29855 |
amit.gupta |
161 |
oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());
|
| 27876 |
amit.gupta |
162 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
|
|
163 |
return "response";
|
|
|
164 |
}
|
| 27391 |
tejbeer |
165 |
|
| 27876 |
amit.gupta |
166 |
@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
|
|
|
167 |
public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
|
|
|
168 |
throws ProfitMandiBusinessException {
|
| 27391 |
tejbeer |
169 |
|
| 29785 |
amit.gupta |
170 |
List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values()
|
| 29682 |
amit.gupta |
171 |
.stream().sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
|
| 27876 |
amit.gupta |
172 |
model.addAttribute("offers", publishedOffers);
|
|
|
173 |
model.addAttribute("yearMonth", yearMonth);
|
| 27391 |
tejbeer |
174 |
|
|
|
175 |
return "offer_history";
|
|
|
176 |
}
|
|
|
177 |
|
| 27876 |
amit.gupta |
178 |
@RequestMapping(value = "/offer-details", method = RequestMethod.GET)
|
|
|
179 |
public String schemeDetails(HttpServletRequest request, @RequestParam int offerId, Model model)
|
| 27391 |
tejbeer |
180 |
throws ProfitMandiBusinessException {
|
| 29839 |
amit.gupta |
181 |
CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
|
| 27391 |
tejbeer |
182 |
|
| 27876 |
amit.gupta |
183 |
model.addAttribute("offer", createOfferRequest);
|
|
|
184 |
return "offer-details";
|
| 27391 |
tejbeer |
185 |
}
|
|
|
186 |
|
| 27876 |
amit.gupta |
187 |
@RequestMapping(value = "/offerDownload", method = RequestMethod.GET)
|
|
|
188 |
public ResponseEntity<?> dowloadOfferSummary(HttpServletRequest request, @RequestParam int offerId, Model model)
|
|
|
189 |
throws Exception {
|
|
|
190 |
List<List<?>> listOfRows = new ArrayList<>();
|
|
|
191 |
final HttpHeaders headers = new HttpHeaders();
|
|
|
192 |
headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
193 |
headers.set("Content-disposition", "inline; filename=offer-" + offerId + ".csv");
|
| 29785 |
amit.gupta |
194 |
CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
|
| 27876 |
amit.gupta |
195 |
Collection<OfferRowModel> offerRowModels = offerRepository.getOfferRows(createOfferRequest);
|
| 27391 |
tejbeer |
196 |
|
| 27876 |
amit.gupta |
197 |
for (OfferRowModel offerRowModel : offerRowModels) {
|
|
|
198 |
CustomRetailer customRetailer = retailerService.getFofoRetailer(offerRowModel.getFofoId());
|
|
|
199 |
listOfRows.add(Arrays.asList(createOfferRequest.getId(), createOfferRequest.getName(),
|
|
|
200 |
createOfferRequest.getTargetType(), createOfferRequest.getSchemeType(),
|
|
|
201 |
createOfferRequest.getBrandShareTerms(), createOfferRequest.getSellinPercentage(),
|
|
|
202 |
createOfferRequest.getPartnerCriteriaString(), createOfferRequest.getItemCriteriaString(),
|
|
|
203 |
createOfferRequest.getStartDate(), createOfferRequest.getEndDate(),
|
|
|
204 |
createOfferRequest.getCreatedOn(), customRetailer.getPartnerId(), customRetailer.getBusinessName(),
|
|
|
205 |
customRetailer.getCode(), offerRowModel.getTotalSale(), offerRowModel.getEligibleSale(),
|
|
|
206 |
offerRowModel.getAchievedTarget(), offerRowModel.getNextTarget(), offerRowModel.getEligibleSaleDp(),
|
|
|
207 |
offerRowModel.getTotalPurchaseValue(), offerRowModel.getCurrentPayoutTarget(),
|
|
|
208 |
offerRowModel.getPayoutTargetAchieved(), offerRowModel.getAmountType(),
|
|
|
209 |
offerRowModel.getPayoutValue(), offerRowModel.getPayoutValueDp(), offerRowModel.getFinalPayout(),
|
|
|
210 |
String.join(", ", offerRowModel.getPendingImeis())));
|
| 27391 |
tejbeer |
211 |
}
|
| 27876 |
amit.gupta |
212 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
|
|
|
213 |
Arrays.asList("Id", "Name", "Target Type", "Scheme Type", "Brand %", "Sellin %", "Partner Criteria",
|
|
|
214 |
"Item Criteria", "Start", "End", "Created", "Partner Id", "Partner Name", "Partner Code",
|
|
|
215 |
"Total Sale", "Eligible Sale", "Achieved Target", "Next Target", "Eligible Sale DP",
|
|
|
216 |
"Total Purchase DP", "Current Payout Target", "Payout Target Achieved", "Payout Amount Type",
|
|
|
217 |
"Payout Value", "Payout Value DP", "Amount to be credited", "IMEIs pending for Activation"
|
|
|
218 |
// "Payout Sale Qty", "Activated Value", "Activated Qty",
|
|
|
219 |
), listOfRows);
|
|
|
220 |
final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
|
|
|
221 |
final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
|
|
|
222 |
return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
|
| 27391 |
tejbeer |
223 |
}
|
| 27876 |
amit.gupta |
224 |
|
|
|
225 |
@RequestMapping(value = "/offerById", method = RequestMethod.GET)
|
|
|
226 |
public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
|
| 27391 |
tejbeer |
227 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
228 |
|
|
|
229 |
model.addAttribute("offer", offer);
|
|
|
230 |
|
| 27876 |
amit.gupta |
231 |
return "offer-edit";
|
| 27391 |
tejbeer |
232 |
|
|
|
233 |
}
|
| 29675 |
amit.gupta |
234 |
|
|
|
235 |
@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)
|
| 29677 |
amit.gupta |
236 |
public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model) throws ProfitMandiBusinessException {
|
| 29675 |
amit.gupta |
237 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
238 |
int fofoId = loginDetails.getFofoId();
|
| 29677 |
amit.gupta |
239 |
List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
|
| 29675 |
amit.gupta |
240 |
|
|
|
241 |
model.addAttribute("publishedOffers", createOffers);
|
|
|
242 |
|
| 29785 |
amit.gupta |
243 |
|
|
|
244 |
|
| 29675 |
amit.gupta |
245 |
return "published-offers";
|
|
|
246 |
}
|
| 29682 |
amit.gupta |
247 |
|
|
|
248 |
@PostMapping(value = "/offers/upload")
|
|
|
249 |
public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
|
|
|
250 |
throws Exception {
|
|
|
251 |
offerService.createOffers(targetFile.getInputStream());
|
|
|
252 |
model.addAttribute("response", true);
|
|
|
253 |
return "response";
|
|
|
254 |
}
|
| 29785 |
amit.gupta |
255 |
|
|
|
256 |
@Autowired
|
|
|
257 |
RoleManager roleManager;
|
| 27391 |
tejbeer |
258 |
|
| 27876 |
amit.gupta |
259 |
@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
|
|
|
260 |
public String getOfferMargins(HttpServletRequest request,
|
|
|
261 |
@RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
|
|
|
262 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 29785 |
amit.gupta |
263 |
boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
|
|
|
264 |
CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin?0:loginDetails.getFofoId(), offerId);
|
|
|
265 |
|
| 27876 |
amit.gupta |
266 |
model.addAttribute("offer", createOfferRequest);
|
| 27391 |
tejbeer |
267 |
|
| 27876 |
amit.gupta |
268 |
return "offer_margin_detail_partner";
|
| 27391 |
tejbeer |
269 |
|
|
|
270 |
}
|
|
|
271 |
|
| 27895 |
amit.gupta |
272 |
}
|