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