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