Subversion Repositories SmartDukaan

Rev

Rev 29926 | Rev 29948 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27391 tejbeer 1
package com.spice.profitmandi.web.controller;
2
 
3
import com.google.gson.Gson;
29943 amit.gupta 4
import com.jcraft.jsch.*;
29900 amit.gupta 5
import com.spice.profitmandi.common.enumuration.MessageType;
27391 tejbeer 6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
7
import com.spice.profitmandi.common.model.CustomRetailer;
27876 amit.gupta 8
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29900 amit.gupta 9
import com.spice.profitmandi.common.model.SendNotificationModel;
27876 amit.gupta 10
import com.spice.profitmandi.common.util.FileUtil;
29904 amit.gupta 11
import com.spice.profitmandi.common.util.FormattingUtils;
27876 amit.gupta 12
import com.spice.profitmandi.common.web.util.ResponseSender;
27391 tejbeer 13
import com.spice.profitmandi.dao.entity.catalog.Offer;
14
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
15
import com.spice.profitmandi.dao.enumuration.catalog.ItemCriteriaType;
16
import com.spice.profitmandi.dao.model.CreateOfferRequest;
27876 amit.gupta 17
import com.spice.profitmandi.dao.model.OfferRowModel;
27391 tejbeer 18
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
19
import com.spice.profitmandi.dao.repository.catalog.OfferMarginRepository;
20
import com.spice.profitmandi.dao.repository.catalog.OfferPartnerRepository;
21
import com.spice.profitmandi.dao.repository.catalog.OfferRepository;
22
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
23
import com.spice.profitmandi.dao.repository.dtr.Mongo;
29926 amit.gupta 24
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
27391 tejbeer 25
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
29900 amit.gupta 26
import com.spice.profitmandi.service.NotificationService;
29785 amit.gupta 27
import com.spice.profitmandi.service.authentication.RoleManager;
27876 amit.gupta 28
import com.spice.profitmandi.service.offers.OfferService;
27391 tejbeer 29
import com.spice.profitmandi.service.user.RetailerService;
30
import com.spice.profitmandi.web.model.LoginDetails;
31
import com.spice.profitmandi.web.util.CookiesProcessor;
32
import com.spice.profitmandi.web.util.MVCResponseSender;
29943 amit.gupta 33
import org.apache.commons.io.FileUtils;
34
import org.apache.commons.io.output.ByteArrayOutputStream;
35
import org.apache.logging.log4j.LogManager;
36
import org.apache.logging.log4j.Logger;
37
import org.apache.velocity.app.VelocityEngine;
38
import org.springframework.beans.factory.annotation.Autowired;
39
import org.springframework.cache.CacheManager;
40
import org.springframework.core.io.InputStreamResource;
41
import org.springframework.http.HttpHeaders;
42
import org.springframework.http.HttpStatus;
43
import org.springframework.http.ResponseEntity;
44
import org.springframework.stereotype.Controller;
45
import org.springframework.ui.Model;
46
import org.springframework.ui.velocity.VelocityEngineUtils;
47
import org.springframework.web.bind.annotation.*;
48
import org.springframework.web.multipart.MultipartFile;
49
import org.xhtmlrenderer.swing.Java2DRenderer;
27391 tejbeer 50
 
29943 amit.gupta 51
import javax.imageio.ImageIO;
52
import javax.servlet.http.HttpServletRequest;
53
import javax.transaction.Transactional;
54
import java.awt.*;
55
import java.awt.image.BufferedImage;
56
import java.io.ByteArrayInputStream;
57
import java.io.File;
58
import java.io.FileNotFoundException;
59
import java.io.InputStream;
60
import java.time.Instant;
61
import java.time.LocalDate;
62
import java.time.LocalDateTime;
63
import java.time.YearMonth;
64
import java.util.List;
65
import java.util.*;
66
import java.util.stream.Collectors;
67
 
27391 tejbeer 68
@Controller
69
@Transactional(rollbackOn = Throwable.class)
70
public class OfferController {
71
	private static final Logger LOGGER = LogManager.getLogger(OfferController.class);
72
	@Autowired
73
	private OfferRepository offerRepository;
74
 
75
	@Autowired
76
	private OfferMarginRepository offerMarginRepository;
77
 
78
	@Autowired
79
	private FofoStoreRepository fofoStoreRepository;
80
 
81
	@Autowired
27876 amit.gupta 82
	private ResponseSender responseSender;
83
 
84
	@Autowired
27391 tejbeer 85
	private OfferPartnerRepository offerPartnerRepository;
86
 
29900 amit.gupta 87
	private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
88
	private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
29926 amit.gupta 89
 
27391 tejbeer 90
	@Autowired
91
	private ItemRepository itemRepository;
92
 
93
	@Autowired
94
	private MVCResponseSender mvcResponseSender;
95
 
96
	@Autowired
97
	private Gson gson;
98
 
99
	@Autowired
100
	private RetailerService retailerService;
101
 
102
	@Autowired
103
	private Mongo mongoClient;
104
 
105
	@Autowired
29900 amit.gupta 106
	private NotificationService notificationService;
107
 
108
	@Autowired
27391 tejbeer 109
	private CookiesProcessor cookiesProcessor;
110
 
111
	@Autowired
27876 amit.gupta 112
	private OfferService offerService;
29900 amit.gupta 113
 
29809 amit.gupta 114
	@Autowired
115
	private CacheManager thirtyMinsTimeOutCacheManager;
29900 amit.gupta 116
 
29855 amit.gupta 117
	@Autowired
118
	private CacheManager oneDayCacheManager;
27876 amit.gupta 119
 
120
	@Autowired
27391 tejbeer 121
	private PartnerTypeChangeService partnerTypeChangeService;
122
 
123
	@RequestMapping(value = "/getCreateOffer", method = RequestMethod.GET)
124
	public String getCreateOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
125
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
126
		List<Integer> fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
127
				.collect(Collectors.toList());
128
 
27876 amit.gupta 129
		Set<String> brands = mongoClient.getMongoBrands(loginDetails.getFofoId(), null, 3).stream()
130
				.map(x -> (String) x.get("name")).collect(Collectors.toSet());
27391 tejbeer 131
 
132
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
133
 
134
		model.addAttribute("customRetailersMap", customRetailersMap);
135
		model.addAttribute("itemCriteriaType", ItemCriteriaType.values());
136
		model.addAttribute("brands", brands);
137
		model.addAttribute("partnerCategories", PartnerType.values());
27876 amit.gupta 138
		model.addAttribute("warehouseRegion", ProfitMandiConstants.WAREHOUSE_MAP);
27391 tejbeer 139
		return "scheme_offer";
140
 
141
	}
142
 
143
	@RequestMapping(value = "/createOffer", method = RequestMethod.POST)
144
	public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
29926 amit.gupta 145
							  Model model) throws Exception {
27876 amit.gupta 146
		LOGGER.info("createOfferRequest [{}]", createOfferRequest);
147
		offerService.addOfferService(createOfferRequest);
29809 amit.gupta 148
		thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());
27391 tejbeer 149
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
150
		return "response";
151
 
152
	}
153
 
27876 amit.gupta 154
	@RequestMapping(value = "/offers/published", method = RequestMethod.GET)
155
	public String getPublishedOffers(HttpServletRequest request, @RequestParam int fofoId, Model model)
27391 tejbeer 156
			throws Exception {
27876 amit.gupta 157
		LOGGER.info("Published");
158
		offerService.getPublishedOffers(fofoId, YearMonth.from(LocalDateTime.now()));
159
		return "scheme_offer/published";
27391 tejbeer 160
 
27876 amit.gupta 161
	}
27391 tejbeer 162
 
27876 amit.gupta 163
	@RequestMapping(value = "/offer/active/{offerId}", method = RequestMethod.GET)
29943 amit.gupta 164
	public String activateOffer(HttpServletRequest request, @PathVariable String offerIdsString, Model model,
29926 amit.gupta 165
								@RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {
29943 amit.gupta 166
		List<Integer> offerIds = Arrays.stream(offerIdsString.split(",")).map(x -> Integer.parseInt(x)).collect(Collectors.toList());
167
		List<Offer> offers = offerRepository.selectAllByIds(offerIds);
168
		boolean shouldEvict = false;
169
		for (Offer offer : offers) {
170
			boolean alreadyActive = offer.isActive();
171
			if (alreadyActive != active) {
172
				offer.setActive(active);
173
				shouldEvict = true;
174
			}
175
			if (active && !alreadyActive) {
176
				this.sendNotification(offer);
177
			}
178
		}
179
		if (shouldEvict) {
29900 amit.gupta 180
			oneDayCacheManager.getCache("catalog.published_yearmonth").evict(YearMonth.now());
181
		}
27876 amit.gupta 182
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
183
		return "response";
29926 amit.gupta 184
		// CreateOfferRequest createOfferRequest =
185
		// offerService.getCreateOfferRequest(offer);
186
		// model.addAttribute("offer", createOfferRequest);
187
		// return "offer_margin_detail_partner2";
27876 amit.gupta 188
	}
29926 amit.gupta 189
 
29920 amit.gupta 190
	@RequestMapping(value = "/offer/testimage/{offerId}", method = RequestMethod.GET)
29926 amit.gupta 191
	public String testOffer(HttpServletRequest request, @PathVariable int offerId, Model model,
192
							@RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {
29920 amit.gupta 193
		Offer offer = offerRepository.selectById(offerId);
29926 amit.gupta 194
		// model.addAttribute("response", mvcResponseSender.createResponseString(true));
195
		// return "response";
29920 amit.gupta 196
		CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
197
		model.addAttribute("offer", createOfferRequest);
198
		return "offer_margin_detail_partner2";
199
	}
27391 tejbeer 200
 
29900 amit.gupta 201
	@Autowired
202
	private com.spice.profitmandi.dao.repository.dtr.UserRepository dtrUserRepository;
203
 
29917 amit.gupta 204
	@RequestMapping(value = "/offer/active/fofo/{fofoId}", method = RequestMethod.GET)
29926 amit.gupta 205
	public String sendOfferNotification(HttpServletRequest request, @PathVariable int fofoId, Model model,
206
										@RequestParam(defaultValue = "true") boolean active) throws ProfitMandiBusinessException, Exception {
29917 amit.gupta 207
		List<Offer> offers = offerRepository.selectAllPublishedMapByPartner(YearMonth.now()).get(fofoId);
29926 amit.gupta 208
		for (Offer offer : offers) {
29917 amit.gupta 209
			this.sendNotification(offer);
210
		}
211
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
212
		return "response";
213
	}
29926 amit.gupta 214
 
215
	@Autowired
216
	UserAccountRepository userAccountRepository;
217
 
29900 amit.gupta 218
	private void sendNotification(Offer offer) throws Exception {
219
		String fileName = "offer-" + offer.getId() + ".png";
220
		CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
221
		SendNotificationModel sendNotificationModel = new SendNotificationModel();
222
		sendNotificationModel.setCampaignName("SchemeOffer");
223
		sendNotificationModel.setTitle(offer.getName());
29926 amit.gupta 224
		sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, "
225
				+ FormattingUtils.formatDateMonth(offer.getStartDate()) + " to "
226
				+ FormattingUtils.formatDateMonth(offer.getEndDate()));
29900 amit.gupta 227
		sendNotificationModel.setType("url");
228
		sendNotificationModel.setImageUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
229
		sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
230
		sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
231
		sendNotificationModel.setMessageType(MessageType.scheme);
232
		Map<Integer, List<Offer>> offersMap = offerRepository.selectAllPublishedMapByPartner(YearMonth.now());
233
		List<Integer> fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer))
234
				.map(x -> x.getKey()).collect(Collectors.toList());
29926 amit.gupta 235
		List<Integer> userIds = userAccountRepository.selectUserIdsByRetailerIds(fofoIds);
236
		sendNotificationModel.setUserIds(userIds);
29900 amit.gupta 237
		Map<String, InputStream> fileStreamsMap = new HashMap<>();
29926 amit.gupta 238
		Map<String, Object> model = new HashMap<>();
239
		model.put("offer", createOfferRequest);
240
		String htmlContent = this.getContentFromTemplate("offer_margin_detail_partner.vm", model);
241
		LOGGER.info("this.getContentFromTemplate {}", htmlContent);
242
		fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
243
 
244
		this.uploadFile(fileStreamsMap);
29900 amit.gupta 245
		notificationService.sendNotification(sendNotificationModel);
246
 
247
	}
29926 amit.gupta 248
 
249
	private InputStream asInputStream(BufferedImage bi) throws Exception {
29900 amit.gupta 250
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
251
		ImageIO.write(bi, "png", baos);
252
		return new ByteArrayInputStream(baos.toByteArray());
29926 amit.gupta 253
 
29900 amit.gupta 254
	}
255
 
256
	private ChannelSftp setupJsch() throws JSchException {
257
		JSch jsch = new JSch();
258
		Session jschSession = jsch.getSession("root", "192.168.179.131");
29926 amit.gupta 259
		// Session jschSession = jsch.getSession("root", "173.255.254.24");
260
		LOGGER.info("getClass().getResource(\"id_rsa\") {}",
261
				getClass().getClassLoader().getResource("id_rsa").getPath());
29900 amit.gupta 262
		jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
29926 amit.gupta 263
		// jschSession.setPassword("spic@2015static0");
29900 amit.gupta 264
		jschSession.setConfig("StrictHostKeyChecking", "no");
265
		jschSession.connect();
266
		return (ChannelSftp) jschSession.openChannel("sftp");
267
	}
268
 
269
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
270
			throws SftpException, FileNotFoundException {
271
 
272
		channelSftp.cd(destinationPath);
273
		String folderName = "image" + LocalDate.now();
274
 
275
		channelSftp.cd(destinationPath);
276
		SftpATTRS attrs = null;
277
 
278
		// check if the directory is already existing
279
		try {
280
			attrs = channelSftp.stat(folderName);
281
		} catch (Exception e) {
282
			System.out.println(destinationPath + "/" + folderName + " not found");
283
		}
284
 
285
		// else create a directory
286
		if (attrs == null) {
287
			channelSftp.mkdir(folderName);
288
			channelSftp.chmod(0755, ".");
289
		}
290
		channelSftp.cd(folderName);
291
 
292
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
293
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
294
		}
295
 
296
	}
29926 amit.gupta 297
 
29900 amit.gupta 298
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
299
		ChannelSftp channelSftp = setupJsch();
300
		channelSftp.connect();
301
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
302
		channelSftp.exit();
303
	}
29926 amit.gupta 304
 
305
	private InputStream getImageBuffer(String html) throws Exception {
306
 
29900 amit.gupta 307
		String fileName = "/tmp/" + Instant.now().toEpochMilli();
308
		FileUtils.writeStringToFile(new File(fileName), html, "UTF-8");
29920 amit.gupta 309
		String address = "file:" + fileName;
29921 amit.gupta 310
		Java2DRenderer renderer = new Java2DRenderer(address, 400);
29926 amit.gupta 311
		RenderingHints hints = new RenderingHints(RenderingHints.KEY_COLOR_RENDERING,
312
				RenderingHints.VALUE_COLOR_RENDER_QUALITY);
29920 amit.gupta 313
		hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
314
		hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
315
		hints.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC));
316
		renderer.setRenderingHints(hints);
317
		BufferedImage img = renderer.getImage();
318
		ByteArrayOutputStream os = new ByteArrayOutputStream();
319
		ImageIO.write(img, "png", os);
320
		return new ByteArrayInputStream(os.toByteArray());
29900 amit.gupta 321
	}
322
 
323
	@Autowired
324
	VelocityEngine velocityEngine;
325
 
326
	private String getContentFromTemplate(String template, Map<String, Object> model) {
327
		StringBuffer content = new StringBuffer();
328
		try {
329
			content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model));
330
		} catch (Exception e) {
331
			e.printStackTrace();
332
		}
333
		return content.toString();
334
	}
335
 
27876 amit.gupta 336
	@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
337
	public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
338
			throws ProfitMandiBusinessException {
27391 tejbeer 339
 
29900 amit.gupta 340
		List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values().stream()
341
				.sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
27876 amit.gupta 342
		model.addAttribute("offers", publishedOffers);
343
		model.addAttribute("yearMonth", yearMonth);
27391 tejbeer 344
 
345
		return "offer_history";
346
	}
347
 
27876 amit.gupta 348
	@RequestMapping(value = "/offer-details", method = RequestMethod.GET)
349
	public String schemeDetails(HttpServletRequest request, @RequestParam int offerId, Model model)
27391 tejbeer 350
			throws ProfitMandiBusinessException {
29839 amit.gupta 351
		CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
27391 tejbeer 352
 
27876 amit.gupta 353
		model.addAttribute("offer", createOfferRequest);
354
		return "offer-details";
27391 tejbeer 355
	}
356
 
27876 amit.gupta 357
	@RequestMapping(value = "/offerDownload", method = RequestMethod.GET)
358
	public ResponseEntity<?> dowloadOfferSummary(HttpServletRequest request, @RequestParam int offerId, Model model)
359
			throws Exception {
360
		List<List<?>> listOfRows = new ArrayList<>();
361
		final HttpHeaders headers = new HttpHeaders();
362
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
363
		headers.set("Content-disposition", "inline; filename=offer-" + offerId + ".csv");
29785 amit.gupta 364
		CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
27876 amit.gupta 365
		Collection<OfferRowModel> offerRowModels = offerRepository.getOfferRows(createOfferRequest);
27391 tejbeer 366
 
27876 amit.gupta 367
		for (OfferRowModel offerRowModel : offerRowModels) {
368
			CustomRetailer customRetailer = retailerService.getFofoRetailer(offerRowModel.getFofoId());
369
			listOfRows.add(Arrays.asList(createOfferRequest.getId(), createOfferRequest.getName(),
370
					createOfferRequest.getTargetType(), createOfferRequest.getSchemeType(),
371
					createOfferRequest.getBrandShareTerms(), createOfferRequest.getSellinPercentage(),
372
					createOfferRequest.getPartnerCriteriaString(), createOfferRequest.getItemCriteriaString(),
373
					createOfferRequest.getStartDate(), createOfferRequest.getEndDate(),
374
					createOfferRequest.getCreatedOn(), customRetailer.getPartnerId(), customRetailer.getBusinessName(),
375
					customRetailer.getCode(), offerRowModel.getTotalSale(), offerRowModel.getEligibleSale(),
376
					offerRowModel.getAchievedTarget(), offerRowModel.getNextTarget(), offerRowModel.getEligibleSaleDp(),
377
					offerRowModel.getTotalPurchaseValue(), offerRowModel.getCurrentPayoutTarget(),
378
					offerRowModel.getPayoutTargetAchieved(), offerRowModel.getAmountType(),
379
					offerRowModel.getPayoutValue(), offerRowModel.getPayoutValueDp(), offerRowModel.getFinalPayout(),
380
					String.join(", ", offerRowModel.getPendingImeis())));
27391 tejbeer 381
		}
27876 amit.gupta 382
		ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
383
				Arrays.asList("Id", "Name", "Target Type", "Scheme Type", "Brand %", "Sellin %", "Partner Criteria",
384
						"Item Criteria", "Start", "End", "Created", "Partner Id", "Partner Name", "Partner Code",
385
						"Total Sale", "Eligible Sale", "Achieved Target", "Next Target", "Eligible Sale DP",
386
						"Total Purchase DP", "Current Payout Target", "Payout Target Achieved", "Payout Amount Type",
387
						"Payout Value", "Payout Value DP", "Amount to be credited", "IMEIs pending for Activation"
29926 amit.gupta 388
						// "Payout Sale Qty", "Activated Value", "Activated Qty",
27876 amit.gupta 389
				), listOfRows);
390
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
391
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
392
		return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
27391 tejbeer 393
	}
29900 amit.gupta 394
 
27876 amit.gupta 395
	@RequestMapping(value = "/offerById", method = RequestMethod.GET)
396
	public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
27391 tejbeer 397
		Offer offer = offerRepository.selectById(offerId);
398
 
399
		model.addAttribute("offer", offer);
400
 
27876 amit.gupta 401
		return "offer-edit";
27391 tejbeer 402
 
403
	}
29900 amit.gupta 404
 
29675 amit.gupta 405
	@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)
29900 amit.gupta 406
	public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model)
407
			throws ProfitMandiBusinessException {
29675 amit.gupta 408
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
409
		int fofoId = loginDetails.getFofoId();
29900 amit.gupta 410
		List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId,
411
				YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
412
 
29675 amit.gupta 413
		model.addAttribute("publishedOffers", createOffers);
29900 amit.gupta 414
 
29675 amit.gupta 415
		return "published-offers";
416
	}
29900 amit.gupta 417
 
29682 amit.gupta 418
	@PostMapping(value = "/offers/upload")
419
	public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
420
			throws Exception {
421
		offerService.createOffers(targetFile.getInputStream());
422
		model.addAttribute("response", true);
423
		return "response";
424
	}
29900 amit.gupta 425
 
29785 amit.gupta 426
	@Autowired
427
	RoleManager roleManager;
27391 tejbeer 428
 
27876 amit.gupta 429
	@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
430
	public String getOfferMargins(HttpServletRequest request,
29926 amit.gupta 431
								  @RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
27876 amit.gupta 432
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
29785 amit.gupta 433
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
29900 amit.gupta 434
		CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin ? 0 : loginDetails.getFofoId(), offerId);
435
 
27876 amit.gupta 436
		model.addAttribute("offer", createOfferRequest);
27391 tejbeer 437
 
27876 amit.gupta 438
		return "offer_margin_detail_partner";
27391 tejbeer 439
 
440
	}
441
 
27895 amit.gupta 442
}