Subversion Repositories SmartDukaan

Rev

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