Subversion Repositories SmartDukaan

Rev

Rev 29943 | Rev 29949 | 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)
29948 amit.gupta 164
	public String activateOffer(HttpServletRequest request, @PathVariable(name = "offerId") 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";
29948 amit.gupta 220
		String htmlFileName = fileName.replace("png", "html");
29900 amit.gupta 221
		CreateOfferRequest createOfferRequest = offerService.getCreateOfferRequest(offer);
222
		SendNotificationModel sendNotificationModel = new SendNotificationModel();
223
		sendNotificationModel.setCampaignName("SchemeOffer");
224
		sendNotificationModel.setTitle(offer.getName());
29926 amit.gupta 225
		sendNotificationModel.setMessage(createOfferRequest.getSchemeType().name() + " of select models, "
226
				+ FormattingUtils.formatDateMonth(offer.getStartDate()) + " to "
227
				+ FormattingUtils.formatDateMonth(offer.getEndDate()));
29900 amit.gupta 228
		sendNotificationModel.setType("url");
229
		sendNotificationModel.setImageUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
230
		sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
231
		sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
232
		sendNotificationModel.setMessageType(MessageType.scheme);
233
		Map<Integer, List<Offer>> offersMap = offerRepository.selectAllPublishedMapByPartner(YearMonth.now());
234
		List<Integer> fofoIds = offersMap.entrySet().stream().filter(x -> x.getValue().contains(offer))
235
				.map(x -> x.getKey()).collect(Collectors.toList());
29926 amit.gupta 236
		List<Integer> userIds = userAccountRepository.selectUserIdsByRetailerIds(fofoIds);
237
		sendNotificationModel.setUserIds(userIds);
29900 amit.gupta 238
		Map<String, InputStream> fileStreamsMap = new HashMap<>();
29926 amit.gupta 239
		Map<String, Object> model = new HashMap<>();
240
		model.put("offer", createOfferRequest);
241
		String htmlContent = this.getContentFromTemplate("offer_margin_detail_partner.vm", model);
242
		LOGGER.info("this.getContentFromTemplate {}", htmlContent);
243
		fileStreamsMap.put(fileName, this.getImageBuffer(htmlContent));
29948 amit.gupta 244
		//fileStreamsMap.put(htmlFileName, new ByteArrayInputStream(htmlContent.getBytes()));
29926 amit.gupta 245
 
246
		this.uploadFile(fileStreamsMap);
29900 amit.gupta 247
		notificationService.sendNotification(sendNotificationModel);
248
 
249
	}
29926 amit.gupta 250
 
251
	private InputStream asInputStream(BufferedImage bi) throws Exception {
29900 amit.gupta 252
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
253
		ImageIO.write(bi, "png", baos);
254
		return new ByteArrayInputStream(baos.toByteArray());
29926 amit.gupta 255
 
29900 amit.gupta 256
	}
257
 
258
	private ChannelSftp setupJsch() throws JSchException {
259
		JSch jsch = new JSch();
260
		Session jschSession = jsch.getSession("root", "192.168.179.131");
29926 amit.gupta 261
		// Session jschSession = jsch.getSession("root", "173.255.254.24");
262
		LOGGER.info("getClass().getResource(\"id_rsa\") {}",
263
				getClass().getClassLoader().getResource("id_rsa").getPath());
29900 amit.gupta 264
		jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
29926 amit.gupta 265
		// jschSession.setPassword("spic@2015static0");
29900 amit.gupta 266
		jschSession.setConfig("StrictHostKeyChecking", "no");
267
		jschSession.connect();
268
		return (ChannelSftp) jschSession.openChannel("sftp");
269
	}
270
 
271
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
272
			throws SftpException, FileNotFoundException {
273
 
274
		channelSftp.cd(destinationPath);
275
		String folderName = "image" + LocalDate.now();
276
 
277
		channelSftp.cd(destinationPath);
278
		SftpATTRS attrs = null;
279
 
280
		// check if the directory is already existing
281
		try {
282
			attrs = channelSftp.stat(folderName);
283
		} catch (Exception e) {
284
			System.out.println(destinationPath + "/" + folderName + " not found");
285
		}
286
 
287
		// else create a directory
288
		if (attrs == null) {
289
			channelSftp.mkdir(folderName);
290
			channelSftp.chmod(0755, ".");
291
		}
292
		channelSftp.cd(folderName);
293
 
294
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
295
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
296
		}
297
 
298
	}
29926 amit.gupta 299
 
29900 amit.gupta 300
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
301
		ChannelSftp channelSftp = setupJsch();
302
		channelSftp.connect();
303
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
304
		channelSftp.exit();
305
	}
29926 amit.gupta 306
 
307
	private InputStream getImageBuffer(String html) throws Exception {
308
 
29900 amit.gupta 309
		String fileName = "/tmp/" + Instant.now().toEpochMilli();
310
		FileUtils.writeStringToFile(new File(fileName), html, "UTF-8");
29920 amit.gupta 311
		String address = "file:" + fileName;
29921 amit.gupta 312
		Java2DRenderer renderer = new Java2DRenderer(address, 400);
29926 amit.gupta 313
		RenderingHints hints = new RenderingHints(RenderingHints.KEY_COLOR_RENDERING,
314
				RenderingHints.VALUE_COLOR_RENDER_QUALITY);
29920 amit.gupta 315
		hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
316
		hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
317
		hints.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC));
318
		renderer.setRenderingHints(hints);
319
		BufferedImage img = renderer.getImage();
320
		ByteArrayOutputStream os = new ByteArrayOutputStream();
321
		ImageIO.write(img, "png", os);
322
		return new ByteArrayInputStream(os.toByteArray());
29900 amit.gupta 323
	}
324
 
325
	@Autowired
326
	VelocityEngine velocityEngine;
327
 
328
	private String getContentFromTemplate(String template, Map<String, Object> model) {
329
		StringBuffer content = new StringBuffer();
330
		try {
331
			content.append(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model));
332
		} catch (Exception e) {
333
			e.printStackTrace();
334
		}
335
		return content.toString();
336
	}
337
 
27876 amit.gupta 338
	@RequestMapping(value = "/offerHistory", method = RequestMethod.GET)
339
	public String getPaginatedOffers(HttpServletRequest request, @RequestParam YearMonth yearMonth, Model model)
340
			throws ProfitMandiBusinessException {
27391 tejbeer 341
 
29900 amit.gupta 342
		List<CreateOfferRequest> publishedOffers = offerService.getAllOffers(yearMonth).values().stream()
343
				.sorted(Comparator.comparing(CreateOfferRequest::getId).reversed()).collect(Collectors.toList());
27876 amit.gupta 344
		model.addAttribute("offers", publishedOffers);
345
		model.addAttribute("yearMonth", yearMonth);
27391 tejbeer 346
 
347
		return "offer_history";
348
	}
349
 
27876 amit.gupta 350
	@RequestMapping(value = "/offer-details", method = RequestMethod.GET)
351
	public String schemeDetails(HttpServletRequest request, @RequestParam int offerId, Model model)
27391 tejbeer 352
			throws ProfitMandiBusinessException {
29839 amit.gupta 353
		CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
27391 tejbeer 354
 
27876 amit.gupta 355
		model.addAttribute("offer", createOfferRequest);
356
		return "offer-details";
27391 tejbeer 357
	}
358
 
27876 amit.gupta 359
	@RequestMapping(value = "/offerDownload", method = RequestMethod.GET)
360
	public ResponseEntity<?> dowloadOfferSummary(HttpServletRequest request, @RequestParam int offerId, Model model)
361
			throws Exception {
362
		List<List<?>> listOfRows = new ArrayList<>();
363
		final HttpHeaders headers = new HttpHeaders();
364
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
365
		headers.set("Content-disposition", "inline; filename=offer-" + offerId + ".csv");
29785 amit.gupta 366
		CreateOfferRequest createOfferRequest = offerService.getOffer(0, offerId);
27876 amit.gupta 367
		Collection<OfferRowModel> offerRowModels = offerRepository.getOfferRows(createOfferRequest);
27391 tejbeer 368
 
27876 amit.gupta 369
		for (OfferRowModel offerRowModel : offerRowModels) {
370
			CustomRetailer customRetailer = retailerService.getFofoRetailer(offerRowModel.getFofoId());
371
			listOfRows.add(Arrays.asList(createOfferRequest.getId(), createOfferRequest.getName(),
372
					createOfferRequest.getTargetType(), createOfferRequest.getSchemeType(),
373
					createOfferRequest.getBrandShareTerms(), createOfferRequest.getSellinPercentage(),
374
					createOfferRequest.getPartnerCriteriaString(), createOfferRequest.getItemCriteriaString(),
375
					createOfferRequest.getStartDate(), createOfferRequest.getEndDate(),
376
					createOfferRequest.getCreatedOn(), customRetailer.getPartnerId(), customRetailer.getBusinessName(),
377
					customRetailer.getCode(), offerRowModel.getTotalSale(), offerRowModel.getEligibleSale(),
378
					offerRowModel.getAchievedTarget(), offerRowModel.getNextTarget(), offerRowModel.getEligibleSaleDp(),
379
					offerRowModel.getTotalPurchaseValue(), offerRowModel.getCurrentPayoutTarget(),
380
					offerRowModel.getPayoutTargetAchieved(), offerRowModel.getAmountType(),
381
					offerRowModel.getPayoutValue(), offerRowModel.getPayoutValueDp(), offerRowModel.getFinalPayout(),
382
					String.join(", ", offerRowModel.getPendingImeis())));
27391 tejbeer 383
		}
27876 amit.gupta 384
		ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
385
				Arrays.asList("Id", "Name", "Target Type", "Scheme Type", "Brand %", "Sellin %", "Partner Criteria",
386
						"Item Criteria", "Start", "End", "Created", "Partner Id", "Partner Name", "Partner Code",
387
						"Total Sale", "Eligible Sale", "Achieved Target", "Next Target", "Eligible Sale DP",
388
						"Total Purchase DP", "Current Payout Target", "Payout Target Achieved", "Payout Amount Type",
389
						"Payout Value", "Payout Value DP", "Amount to be credited", "IMEIs pending for Activation"
29926 amit.gupta 390
						// "Payout Sale Qty", "Activated Value", "Activated Qty",
27876 amit.gupta 391
				), listOfRows);
392
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
393
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
394
		return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
27391 tejbeer 395
	}
29900 amit.gupta 396
 
27876 amit.gupta 397
	@RequestMapping(value = "/offerById", method = RequestMethod.GET)
398
	public String offerById(HttpServletRequest request, int offerId, Model model) throws ProfitMandiBusinessException {
27391 tejbeer 399
		Offer offer = offerRepository.selectById(offerId);
400
 
401
		model.addAttribute("offer", offer);
402
 
27876 amit.gupta 403
		return "offer-edit";
27391 tejbeer 404
 
405
	}
29900 amit.gupta 406
 
29675 amit.gupta 407
	@RequestMapping(value = "/published-offers/{yearMonth}", method = RequestMethod.GET)
29900 amit.gupta 408
	public String publishedOffersOnMonthBefore(HttpServletRequest request, @PathVariable int yearMonth, Model model)
409
			throws ProfitMandiBusinessException {
29675 amit.gupta 410
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
411
		int fofoId = loginDetails.getFofoId();
29900 amit.gupta 412
		List<CreateOfferRequest> createOffers = offerService.getPublishedOffers(fofoId,
413
				YearMonth.from(LocalDate.now()).minusMonths(yearMonth));
414
 
29675 amit.gupta 415
		model.addAttribute("publishedOffers", createOffers);
29900 amit.gupta 416
 
29675 amit.gupta 417
		return "published-offers";
418
	}
29900 amit.gupta 419
 
29682 amit.gupta 420
	@PostMapping(value = "/offers/upload")
421
	public String uploadOffers(HttpServletRequest request, @RequestPart("file") MultipartFile targetFile, Model model)
422
			throws Exception {
423
		offerService.createOffers(targetFile.getInputStream());
424
		model.addAttribute("response", true);
425
		return "response";
426
	}
29900 amit.gupta 427
 
29785 amit.gupta 428
	@Autowired
429
	RoleManager roleManager;
27391 tejbeer 430
 
27876 amit.gupta 431
	@RequestMapping(value = "/getOfferMargins", method = RequestMethod.GET)
432
	public String getOfferMargins(HttpServletRequest request,
29926 amit.gupta 433
								  @RequestParam(name = "offerId", defaultValue = "0") int offerId, Model model) throws Exception {
27876 amit.gupta 434
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
29785 amit.gupta 435
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
29900 amit.gupta 436
		CreateOfferRequest createOfferRequest = offerService.getOffer(isAdmin ? 0 : loginDetails.getFofoId(), offerId);
437
 
27876 amit.gupta 438
		model.addAttribute("offer", createOfferRequest);
27391 tejbeer 439
 
27876 amit.gupta 440
		return "offer_margin_detail_partner";
27391 tejbeer 441
 
442
	}
443
 
27895 amit.gupta 444
}