Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22860 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
23020 ashik.ali 3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.InputStream;
26674 tejbeer 6
import java.time.LocalDate;
23020 ashik.ali 7
import java.time.LocalDateTime;
28916 amit.gupta 8
import java.time.LocalTime;
29585 manish 9
import java.time.Month;
29608 amit.gupta 10
import java.time.YearMonth;
27897 amit.gupta 11
import java.util.ArrayList;
23556 amit.gupta 12
import java.util.Arrays;
23914 govind 13
import java.util.HashMap;
23556 amit.gupta 14
import java.util.HashSet;
22860 ashik.ali 15
import java.util.List;
23020 ashik.ali 16
import java.util.Map;
25368 amit.gupta 17
import java.util.Set;
28491 amit.gupta 18
import java.util.stream.Collectors;
22860 ashik.ali 19
 
20
import javax.servlet.http.HttpServletRequest;
29707 tejbeer 21
import javax.servlet.http.HttpServletResponse;
25368 amit.gupta 22
import javax.transaction.Transactional;
22860 ashik.ali 23
 
29707 tejbeer 24
import org.apache.commons.csv.CSVRecord;
23784 ashik.ali 25
import org.apache.logging.log4j.LogManager;
23568 govind 26
import org.apache.logging.log4j.Logger;
22860 ashik.ali 27
import org.springframework.beans.factory.annotation.Autowired;
23784 ashik.ali 28
import org.springframework.beans.factory.annotation.Qualifier;
23020 ashik.ali 29
import org.springframework.core.io.InputStreamResource;
30
import org.springframework.http.HttpHeaders;
31
import org.springframework.http.HttpStatus;
32
import org.springframework.http.ResponseEntity;
22860 ashik.ali 33
import org.springframework.stereotype.Controller;
34
import org.springframework.ui.Model;
35
import org.springframework.web.bind.annotation.RequestBody;
36
import org.springframework.web.bind.annotation.RequestMapping;
37
import org.springframework.web.bind.annotation.RequestMethod;
38
import org.springframework.web.bind.annotation.RequestParam;
29707 tejbeer 39
import org.springframework.web.bind.annotation.RequestPart;
40
import org.springframework.web.multipart.MultipartFile;
22860 ashik.ali 41
 
23020 ashik.ali 42
import com.spice.profitmandi.common.enumuration.DateTimePattern;
29585 manish 43
import com.spice.profitmandi.common.enumuration.ReporticoProject;
22860 ashik.ali 44
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29585 manish 45
import com.spice.profitmandi.common.model.CustomRetailer;
23914 govind 46
import com.spice.profitmandi.common.model.MapWrapper;
22860 ashik.ali 47
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23557 amit.gupta 48
import com.spice.profitmandi.common.model.SchemeItems;
23020 ashik.ali 49
import com.spice.profitmandi.common.model.SchemeModel;
29585 manish 50
import com.spice.profitmandi.common.services.ReporticoService;
23020 ashik.ali 51
import com.spice.profitmandi.common.util.ExcelUtils;
29585 manish 52
import com.spice.profitmandi.common.util.FileUtil;
27394 amit.gupta 53
import com.spice.profitmandi.common.util.FormattingUtils;
23020 ashik.ali 54
import com.spice.profitmandi.common.util.StringUtils;
28924 amit.gupta 55
import com.spice.profitmandi.common.util.Utils;
23784 ashik.ali 56
import com.spice.profitmandi.common.web.util.ResponseSender;
29707 tejbeer 57
import com.spice.profitmandi.dao.entity.catalog.CustomerOffer;
58
import com.spice.profitmandi.dao.entity.catalog.CustomerOfferItem;
29585 manish 59
import com.spice.profitmandi.dao.entity.catalog.EvaluateSchemeInvestmentPayoutModel;
28492 amit.gupta 60
import com.spice.profitmandi.dao.entity.catalog.Item;
29707 tejbeer 61
import com.spice.profitmandi.dao.entity.catalog.SamsungUpgradeOffer;
22860 ashik.ali 62
import com.spice.profitmandi.dao.entity.catalog.Scheme;
27389 amit.gupta 63
import com.spice.profitmandi.dao.entity.catalog.TagListing;
28136 tejbeer 64
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
28795 tejbeer 65
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
29585 manish 66
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
25505 amit.gupta 67
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
28795 tejbeer 68
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
23556 amit.gupta 69
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
28795 tejbeer 70
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
71
import com.spice.profitmandi.dao.entity.transaction.PriceDropIMEI;
27389 amit.gupta 72
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
28491 amit.gupta 73
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
29707 tejbeer 74
import com.spice.profitmandi.dao.enumuration.catalog.UpgradeOfferStatus;
29899 tejbeer 75
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
29585 manish 76
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
27876 amit.gupta 77
import com.spice.profitmandi.dao.model.CreateOfferRequest;
25505 amit.gupta 78
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
29707 tejbeer 79
import com.spice.profitmandi.dao.repository.catalog.CustomerOfferItemRepository;
80
import com.spice.profitmandi.dao.repository.catalog.CustomerOfferRepository;
26674 tejbeer 81
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
29707 tejbeer 82
import com.spice.profitmandi.dao.repository.catalog.SamsungUpgradeOfferRepository;
22860 ashik.ali 83
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
27389 amit.gupta 84
import com.spice.profitmandi.dao.repository.catalog.StateGstRateRepository;
23556 amit.gupta 85
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
29899 tejbeer 86
import com.spice.profitmandi.dao.repository.cs.CsService;
26588 tejbeer 87
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
88
import com.spice.profitmandi.dao.repository.dtr.Mongo;
28795 tejbeer 89
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
29585 manish 90
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
26674 tejbeer 91
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
28795 tejbeer 92
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
23556 amit.gupta 93
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
28795 tejbeer 94
import com.spice.profitmandi.dao.repository.transaction.PriceDropIMEIRepository;
95
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
23798 amit.gupta 96
import com.spice.profitmandi.service.authentication.RoleManager;
23020 ashik.ali 97
import com.spice.profitmandi.service.inventory.InventoryService;
27876 amit.gupta 98
import com.spice.profitmandi.service.offers.OfferService;
22860 ashik.ali 99
import com.spice.profitmandi.service.scheme.SchemeService;
26588 tejbeer 100
import com.spice.profitmandi.service.user.RetailerService;
29585 manish 101
import com.spice.profitmandi.service.wallet.WalletService;
22860 ashik.ali 102
import com.spice.profitmandi.web.model.LoginDetails;
103
import com.spice.profitmandi.web.util.CookiesProcessor;
23570 amit.gupta 104
import com.spice.profitmandi.web.util.MVCResponseSender;
22860 ashik.ali 105
 
29585 manish 106
import in.shop2020.model.v1.order.WalletReferenceType;
107
 
22860 ashik.ali 108
@Controller
25369 amit.gupta 109
@Transactional(rollbackOn = Throwable.class)
22860 ashik.ali 110
public class SchemeController {
111
 
23568 govind 112
	private static final Logger LOGGER = LogManager.getLogger(SchemeController.class);
22860 ashik.ali 113
 
114
	@Autowired
22927 ashik.ali 115
	private SchemeService schemeService;
27897 amit.gupta 116
 
27876 amit.gupta 117
	@Autowired
118
	private OfferService offerService;
23786 amit.gupta 119
 
22860 ashik.ali 120
	@Autowired
27389 amit.gupta 121
	private StateGstRateRepository stateGstRateRepository;
27612 tejbeer 122
 
27389 amit.gupta 123
	@Autowired
27416 amit.gupta 124
	private ItemRepository itemRepository;
29608 amit.gupta 125
 
29585 manish 126
	@Autowired
127
	private ReporticoService reporticoService;
27612 tejbeer 128
 
27416 amit.gupta 129
	@Autowired
22927 ashik.ali 130
	private SchemeRepository schemeRepository;
22860 ashik.ali 131
 
132
	@Autowired
23556 amit.gupta 133
	private SchemeItemRepository schemeItemRepository;
23786 amit.gupta 134
 
23556 amit.gupta 135
	@Autowired
23570 amit.gupta 136
	private MVCResponseSender mvcResponseSender;
23786 amit.gupta 137
 
23570 amit.gupta 138
	@Autowired
22927 ashik.ali 139
	private CookiesProcessor cookiesProcessor;
23786 amit.gupta 140
 
23020 ashik.ali 141
	@Autowired
23784 ashik.ali 142
	@Qualifier("fofoInventoryService")
23020 ashik.ali 143
	private InventoryService inventoryService;
23556 amit.gupta 144
 
145
	@Autowired
146
	private TagListingRepository tagListingRepository;
23914 govind 147
 
23798 amit.gupta 148
	@Autowired
149
	private RoleManager roleManager;
23786 amit.gupta 150
 
23506 amit.gupta 151
	@Autowired
27394 amit.gupta 152
	private ResponseSender<?> responseSender;
23786 amit.gupta 153
 
23784 ashik.ali 154
	@Autowired
27394 amit.gupta 155
	private FofoStoreRepository fofoStoreRepository;
23786 amit.gupta 156
 
23784 ashik.ali 157
	@Autowired
27394 amit.gupta 158
	private RetailerService retailerService;
22860 ashik.ali 159
 
27394 amit.gupta 160
	@Autowired
161
	private Mongo mongoClient;
29608 amit.gupta 162
 
29585 manish 163
	@Autowired
164
	WalletService walletService;
26588 tejbeer 165
 
166
	@Autowired
29585 manish 167
	PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
168
 
169
	@Autowired
26674 tejbeer 170
	private PartnerTypeChangeService partnerTypeChangeService;
171
 
28795 tejbeer 172
	@Autowired
173
	InventoryItemRepository inventoryItemRepository;
174
 
175
	@Autowired
176
	PriceDropIMEIRepository priceDropIMEIRepository;
177
 
178
	@Autowired
179
	PriceDropRepository priceDropRepository;
180
 
181
	@Autowired
182
	SchemeInOutRepository schemeInOutRepository;
183
 
29707 tejbeer 184
	@Autowired
185
	private CustomerOfferRepository customerOfferRepository;
186
 
187
	@Autowired
188
	private CustomerOfferItemRepository customerOfferItemRepository;
189
 
190
	@Autowired
191
	private SamsungUpgradeOfferRepository samsungUpgradeOfferRepository;
192
 
29899 tejbeer 193
	@Autowired
194
	private CsService csService;
195
 
196
	List<String> adminEmail = Arrays.asList("tarun.verma@smartdukaan.com", "neeraj.gupta@smartdukaan.com",
197
			"amit.gupta@shop2020.in", "manish.tiwari@smartdukaan.com", "tejbeer.kaur@shop2020.in");
198
 
22860 ashik.ali 199
	@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
29899 tejbeer 200
	public String createScheme(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
201
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
29608 amit.gupta 202
 
29585 manish 203
		LocalDate currentdate = LocalDate.now();
204
		Month month = currentdate.getMonth().minus(1);
205
		model.addAttribute("month", month);
29608 amit.gupta 206
 
23786 amit.gupta 207
		// Map<Integer, String> itemIdItemDescriptionMap =
208
		// inventoryService.getAllItemIdItemDescriptionMap();
209
		// model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
29899 tejbeer 210
 
25368 amit.gupta 211
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
29899 tejbeer 212
 
25368 amit.gupta 213
		brands.addAll(inventoryService.getAllTagListingBrands(14206));
29899 tejbeer 214
 
215
		boolean fullAccesss = this.getAccess(loginDetails.getEmailId());
216
 
217
		model.addAttribute("fullAccesss", fullAccesss);
218
 
25368 amit.gupta 219
		model.addAttribute("brands", brands);
25505 amit.gupta 220
		model.addAttribute("retailerTypes", PartnerType.values());
22860 ashik.ali 221
		return "create-scheme";
222
	}
23786 amit.gupta 223
 
29899 tejbeer 224
	private boolean getAccess(String emailId) {
225
 
226
		boolean fullAccesss = false;
227
		List<String> emails = csService
29926 amit.gupta 228
				.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY, EscalationType.L3).stream()
29899 tejbeer 229
				.map(x -> x.getEmailId()).collect(Collectors.toList());
230
 
231
		emails.addAll(
29926 amit.gupta 232
				csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY, EscalationType.L2)
29899 tejbeer 233
						.stream().map(x -> x.getEmailId()).collect(Collectors.toList()));
234
 
235
		if (adminEmail.contains(emailId)) {
236
			fullAccesss = true;
237
		}
238
 
239
		if (emails.contains(emailId)) {
240
			fullAccesss = false;
241
		}
242
 
243
		return fullAccesss;
244
 
245
	}
246
 
23914 govind 247
	@RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.POST)
248
	public String getTagListingItemsByBrand(HttpServletRequest request, @RequestBody List<String> brands, Model model) {
249
		Map<Integer, String> itemIdItemDescriptionMap = new HashMap<>();
250
		LOGGER.info("brands" + brands);
251
 
252
		List<MapWrapper<Integer, String>> itemIdItemDescriptionMaplist = inventoryService
253
				.getAllTagListingItemIdItemDescriptionMap(new HashSet<>(brands));
254
		for (MapWrapper<Integer, String> mapWrapper : itemIdItemDescriptionMaplist) {
255
			itemIdItemDescriptionMap.put(mapWrapper.getKey(), mapWrapper.getValue());
256
		}
23020 ashik.ali 257
		model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
23786 amit.gupta 258
		// model.addAttribute("brands", inventoryService.getAllBrands());
23914 govind 259
 
23419 ashik.ali 260
		return "tag-listing-items-description";
23020 ashik.ali 261
	}
23556 amit.gupta 262
 
263
	@RequestMapping(value = "/schemes/update-schemes-page", method = RequestMethod.GET)
264
	public String updateShcemes(HttpServletRequest request) throws ProfitMandiBusinessException {
265
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23799 amit.gupta 266
		if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
23556 amit.gupta 267
			throw new ProfitMandiBusinessException("User", loginDetails.getEmailId(), "Unauthorised access");
268
		}
269
		return "update-schemes-page";
270
	}
29608 amit.gupta 271
 
29585 manish 272
	@RequestMapping(value = "/payMonthlyInvestment", method = RequestMethod.POST)
273
	public void payMonthlyInvestment(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
29608 amit.gupta 274
 
29585 manish 275
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
276
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
277
		int referenceId = Integer.parseInt(FormattingUtils.getYearMonth(startOfPreviousMonth.atStartOfDay()));
278
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
279
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
280
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
281
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
282
				.filter(x -> x.getShortPercentage() <= 10)
283
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
284
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
285
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT,
286
				startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
29608 amit.gupta 287
 
288
		if (schemeInOuts.isEmpty()) {
289
			throw new ProfitMandiBusinessException("Investment Payout", "", "No data Found");
29585 manish 290
		}
29608 amit.gupta 291
 
29585 manish 292
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream()
293
				.collect(Collectors.groupingBy(x -> x.getInventoryItemId()));
294
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
295
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream().collect(
296
				Collectors.groupingBy(x -> x.getFofoId(), Collectors.mapping(x -> x.getId(), Collectors.toList())));
297
		System.out.println("Fofo Id\tInvestment Maintained Days\tEligible payout");
298
		for (Map.Entry<Integer, List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
299
			int fofoId = retailerEntry.getKey();
300
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0
301
					: investmentMaintainedDaysMap.get(fofoId);
25256 amit.gupta 302
 
29585 manish 303
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x -> inventoryItemIdSchemeMap.get(x))
304
					.flatMap(List::stream).filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList());
305
			float totalAmount = 0;
29608 amit.gupta 306
			for (SchemeInOut sio : schemeInouts) {
307
 
29585 manish 308
				if (investmentMaintainedDays < 8) {
309
					sio.setStatus(SchemePayoutStatus.REJECTED);
29608 amit.gupta 310
					// sio.setRolledBackTimestamp(LocalDateTime.now());
29585 manish 311
					sio.setStatusDescription("Investment maintained for " + investmentMaintainedDays + "(< 8) days");
312
				} else if (investmentMaintainedDays < 12) {
313
					sio.setStatus(SchemePayoutStatus.CREDITED);
29608 amit.gupta 314
					sio.setAmount(sio.getAmount() / 2);
29585 manish 315
					sio.setCreditTimestamp(LocalDateTime.now());
316
					sio.setStatusDescription("Investment maintained for " + investmentMaintainedDays + "(< 12) days");
317
					totalAmount += sio.getAmount();
318
				} else {
319
					sio.setStatus(SchemePayoutStatus.CREDITED);
320
					sio.setCreditTimestamp(LocalDateTime.now());
321
					totalAmount += sio.getAmount();
322
				}
323
			}
29608 amit.gupta 324
			if (totalAmount > 0) {
325
				String description = "Investment margin paid for "
326
						+ FormattingUtils.formatYearMonth(startOfPreviousMonth.atStartOfDay());
327
				if (investmentMaintainedDays < 12) {
328
					description += ", as maintained for " + investmentMaintainedDays + "(< 12) days";
29585 manish 329
				}
29608 amit.gupta 330
				walletService.addAmountToWallet(fofoId, referenceId, WalletReferenceType.INVESTMENT_PAYOUT, description,
331
						totalAmount, lastOfPreviousMonth.atTime(LocalTime.MAX));
29585 manish 332
			}
333
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
334
		}
29608 amit.gupta 335
 
29585 manish 336
	}
29608 amit.gupta 337
 
29585 manish 338
	@RequestMapping(value = "/evaluateActualInvestmentPayout", method = RequestMethod.GET)
29663 manish 339
	public ResponseEntity<?> evaluateActualInvestmentPayout(HttpServletRequest request, Model model) throws Exception {
340
 
341
		List<List<?>> rows = new ArrayList<>();
342
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
343
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
344
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
345
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
346
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
347
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
348
				.filter(x -> x.getShortPercentage() <= 10)
349
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
350
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
351
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT,
352
				startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
353
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream()
354
				.collect(Collectors.groupingBy(x -> x.getInventoryItemId()));
355
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
356
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream().collect(
357
				Collectors.groupingBy(x -> x.getFofoId(), Collectors.mapping(x -> x.getId(), Collectors.toList())));
358
		System.out.println("Fofo Id\tInvestment Maintained Days\tEligible payout");
359
		for (Map.Entry<Integer, List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
360
			int fofoId = retailerEntry.getKey();
361
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x -> inventoryItemIdSchemeMap.get(x))
362
					.flatMap(List::stream).collect(Collectors.toList());
363
			double totalAmount = schemeInouts.stream().filter(x -> x.getRolledBackTimestamp() == null)
364
					.collect(Collectors.summingDouble(x -> x.getAmount()));
365
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0
366
					: investmentMaintainedDaysMap.get(fofoId);
367
			if (investmentMaintainedDays < 8) {
368
				totalAmount = 0;
369
			} else if (investmentMaintainedDays < 12) {
370
				totalAmount = totalAmount / 2;
29585 manish 371
			}
29663 manish 372
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
373
			CustomRetailer customRetailer = retailerService.getFofoRetailer(fofoId);
29608 amit.gupta 374
 
29663 manish 375
			rows.add(Arrays.asList(fofoId, customRetailer.getBusinessName(), customRetailer.getCode(),
376
					investmentMaintainedDays, totalAmount));
29585 manish 377
		}
378
 
29663 manish 379
		org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
380
				Arrays.asList("fofoId", "Name", "Code", "investmentMaintainedDays", "totalAmount"), rows);
381
 
382
		final HttpHeaders headers = new HttpHeaders();
383
		headers.set("Content-Type", "text/csv");
384
		headers.set("Content-disposition", "inline; filename=investmentMaintainedDays.csv");
385
		headers.setContentLength(baos.toByteArray().length);
386
 
387
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
388
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
389
 
390
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
391
 
392
	}
393
 
29585 manish 394
	@RequestMapping(value = "/processInvestmentDryRun", method = RequestMethod.GET)
29608 amit.gupta 395
	public ResponseEntity<?> processInvestmentDryRun(HttpServletRequest request, Model model) throws Exception {
396
 
29585 manish 397
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
398
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
399
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
400
		List<List<?>> rows = new ArrayList<>();
401
 
29608 amit.gupta 402
		Map<String, String> params = new HashMap<>();
29585 manish 403
 
29608 amit.gupta 404
		params.put("MANUAL_datesBetween_FROMDATE", firstDateOfCurrentMonth.toString());
405
		params.put("MANUAL_datesBetween_TODATE", firstDateOfCurrentMonth.plusDays(9).toString());
29585 manish 406
 
29608 amit.gupta 407
		params.put("type", "INVESTMENT");
29585 manish 408
 
29608 amit.gupta 409
		List<EvaluateSchemeInvestmentPayoutModel> evaluateSchemeInvestmentPayouts = reporticoService.getReports(
410
				EvaluateSchemeInvestmentPayoutModel.class, ReporticoProject.FOCO, "schemepayout.xml", params);
411
		LOGGER.info("reportResponse {}", evaluateSchemeInvestmentPayouts);
412
 
413
		int referenceId = Integer.parseInt(FormattingUtils.getYearMonth(startOfPreviousMonth.atStartOfDay()));
414
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
415
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
416
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
417
				.filter(x -> x.getShortPercentage() <= 10)
418
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
419
 
420
		for (EvaluateSchemeInvestmentPayoutModel esip : evaluateSchemeInvestmentPayouts) {
421
 
422
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(esip.getFofoId()) == null ? 0
423
					: investmentMaintainedDaysMap.get(esip.getFofoId());
424
 
425
			esip.setInvestmentDays(investmentMaintainedDays);
426
 
427
			float processAmount = esip.getPaidAmount() / 2;
428
 
429
			esip.setProcessAmount(processAmount);
430
 
431
			rows.add(Arrays.asList(esip.getCode(), esip.getStoreName(), esip.getFofoId(), esip.getItemId(),
432
					esip.getBrand(), esip.getModelName(), esip.getModelNumber(), esip.getColor(), esip.getSchemeInDp(),
433
					esip.getSchemeOutDp(), esip.getSchemeId(), esip.getName(), esip.getType(), esip.getPartnerType(),
434
					esip.getAmountType(), esip.getAmount(), esip.getPurchaseInvoice(), esip.getSaleInovoice(),
435
					esip.getPaidAmount(), esip.getCreateTimestamp(), esip.getRolledBackTimestamp(),
436
					esip.getSerialNumber(), esip.getInRef(), esip.getOutRef(), esip.getBusinessDate(), esip.getStatus(),
437
					esip.getDescription(), esip.getProcessAmount(), esip.getInvestmentDays()));
29585 manish 438
		}
439
 
29608 amit.gupta 440
		org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil
441
				.getCSVByteStream(Arrays.asList("Code", "Store Name", "Fofo Id", "Item Id", "Brand", "Model Name",
442
						"Model Number", "Color", "Scheme In Dp", "Scheme Out Dp", "Scheme Id", "Name", "Type",
443
						"Partner Type", "Amount Type", "Amount", "Purchase Invoice", "Sale Inovoice", "Paid Amount",
444
						"Create Timestamp", "Rolled Back Timestamp", "Serial Number", "In Ref", "Out Ref",
445
						"Business Date", "Status", "Description", "Process Amount", "Investment Days"), rows);
446
 
447
		final HttpHeaders headers = new HttpHeaders();
448
		headers.set("Content-Type", "text/csv");
449
		headers.set("Content-disposition", "inline; filename=schemePayout.csv");
450
		headers.setContentLength(baos.toByteArray().length);
451
 
452
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
453
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
454
 
455
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
456
 
457
	}
458
 
23556 amit.gupta 459
	@RequestMapping(value = "/schemes/update", method = RequestMethod.POST)
23786 amit.gupta 460
	public String updateShcemes(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)
461
			throws Exception {
462
		for (int schemeId : schemeItems.getSchemeIds()) {
23928 govind 463
			if (schemeRepository.selectById(schemeId) != null)
464
				for (int itemId : schemeItems.getItemIds()) {
465
					if (tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)),
466
							new HashSet<>(Arrays.asList(4, 7))).size() > 0) {
467
						SchemeItem si = new SchemeItem();
468
						si.setItemId(itemId);
469
						si.setSchemeId(schemeId);
29608 amit.gupta 470
						si.setCreateTimestamp(LocalDateTime.now());
23928 govind 471
						try {
472
							schemeItemRepository.persist(si);
473
						} catch (Exception e) {
474
							LOGGER.info("Scheme aleady exist");
475
						}
476
						model.addAttribute("response", mvcResponseSender.createResponseString(true));
477
					} else {
478
						model.addAttribute("response", mvcResponseSender.createResponseString(false));
479
						throw new ProfitMandiBusinessException("ItemId", itemId, "Invalid Item Id");
480
					}
481
				}
482
		}
483
		return "response";
484
	}
25256 amit.gupta 485
 
23928 govind 486
	@RequestMapping(value = "/addItemToScheme", method = RequestMethod.POST)
487
	public String updateScheme(HttpServletRequest request, @RequestBody SchemeItems schemeItems, Model model)
488
			throws Exception {
489
		for (int schemeId : schemeItems.getSchemeIds()) {
23914 govind 490
			List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(schemeId);
23786 amit.gupta 491
			if (schemeRepository.selectById(schemeId) != null)
492
				for (int itemId : schemeItems.getItemIds()) {
493
					if (tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(Arrays.asList(itemId)),
23914 govind 494
							new HashSet<>(Arrays.asList(4, 7))).size() > 0 && (!(itemIds.contains(itemId)))) {
23786 amit.gupta 495
						SchemeItem si = new SchemeItem();
496
						si.setItemId(itemId);
497
						si.setSchemeId(schemeId);
29608 amit.gupta 498
						si.setCreateTimestamp(LocalDateTime.now());
23786 amit.gupta 499
						try {
500
							schemeItemRepository.persist(si);
501
						} catch (Exception e) {
23914 govind 502
							LOGGER.info("Scheme already exist");
23786 amit.gupta 503
						}
504
						model.addAttribute("response", mvcResponseSender.createResponseString(true));
505
					} else {
506
						model.addAttribute("response", mvcResponseSender.createResponseString(false));
23556 amit.gupta 507
					}
508
				}
509
		}
510
		return "response";
511
	}
23914 govind 512
 
513
	@RequestMapping(value = "/schemes/delete", method = RequestMethod.DELETE)
514
	public String deleteShcemes(HttpServletRequest request,
515
			@RequestParam(name = "schemeId", required = false, defaultValue = "0") int schemeId,
516
			@RequestParam(name = "itemId", required = false, defaultValue = "0") int itemId, Model model)
23819 govind 517
			throws Exception {
23914 govind 518
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
519
		if (!(schemeId == 0 && itemId == 0) || (!(schemeId == 0 || itemId == 0))) {
520
			schemeItemRepository.deletebyItemIdsandSchemeIds(itemId, schemeId);
521
 
522
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
523
 
524
			model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
525
 
526
		}
527
		return "response";
528
	}
529
 
530
	@RequestMapping(value = "/extendAllSchemes", method = RequestMethod.POST)
531
	public String extendAllScheme(HttpServletRequest request, @RequestBody LocalDateTime extendDatetime, Model model)
532
			throws Exception {
28924 amit.gupta 533
		extendDatetime = LocalDateTime.of(extendDatetime.toLocalDate(), Utils.MAX_TIME);
23914 govind 534
		List<Scheme> schemes = schemeRepository.selectActiveAll();
535
		if (schemes.size() > 0) {
536
			for (Scheme scheme : schemes) {
25261 amit.gupta 537
				if (scheme.getExpireTimestamp() == null) {
23914 govind 538
					scheme.setEndDateTime(extendDatetime);
539
					schemeRepository.persist(scheme);
23819 govind 540
				}
541
			}
23914 govind 542
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
543
			return "response";
23819 govind 544
		}
23914 govind 545
		model.addAttribute("response", mvcResponseSender.createResponseString(false));
23819 govind 546
		return "response";
547
	}
23786 amit.gupta 548
 
23914 govind 549
	@RequestMapping(value = "/extendSchemeById", method = RequestMethod.POST)
550
	public String extendSchemeById(HttpServletRequest request,
23819 govind 551
 
23914 govind 552
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,
553
			@RequestBody LocalDateTime extendDatetime, Model model) throws Exception {
28924 amit.gupta 554
		extendDatetime = LocalDateTime.of(extendDatetime.toLocalDate(), Utils.MAX_TIME);
23914 govind 555
		Scheme scheme = schemeRepository.selectById(schemeId);
556
		if ((!(scheme.getActiveTimestamp() == null)) && scheme.getExpireTimestamp() == null) {
557
			scheme.setEndDateTime(extendDatetime);
558
			schemeRepository.persist(scheme);
559
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
560
			return "response";
561
		}
562
		model.addAttribute("response", mvcResponseSender.createResponseString(false));
563
		return "response";
564
	}
565
 
22860 ashik.ali 566
	@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
23715 govind 567
	public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest,
568
			@RequestParam(name = "offset", defaultValue = "0") int offset,
23752 govind 569
			@RequestParam(name = "limit", defaultValue = "10") int limit,
570
			@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem,
571
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
23715 govind 572
			throws ProfitMandiBusinessException {
22927 ashik.ali 573
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
22860 ashik.ali 574
		LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
22927 ashik.ali 575
		schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
576
		LOGGER.info("Scheme saved successfully");
23271 ashik.ali 577
		long size = schemeRepository.selectAllCount();
22927 ashik.ali 578
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
29899 tejbeer 579
 
580
		if (!schemes.isEmpty()) {
581
			for (Scheme scheme : schemes) {
582
				if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
583
					scheme.setAmountModel(scheme.getAmount() + "%");
584
				} else {
585
					scheme.setAmountModel(scheme.getAmount() + "");
586
				}
587
			}
588
		}
22927 ashik.ali 589
		model.addAttribute("schemes", schemes);
590
		model.addAttribute("start", offset + 1);
23271 ashik.ali 591
		model.addAttribute("size", size);
23752 govind 592
		model.addAttribute("searchItem", searchItem);
593
		model.addAttribute("searchTerm", searchTerm);
23914 govind 594
		model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
23715 govind 595
		if (schemes.size() < limit) {
22927 ashik.ali 596
			model.addAttribute("end", offset + schemes.size());
23786 amit.gupta 597
		} else {
22927 ashik.ali 598
			model.addAttribute("end", offset + limit);
599
		}
600
		return "schemes";
23786 amit.gupta 601
 
22860 ashik.ali 602
	}
23786 amit.gupta 603
 
22860 ashik.ali 604
	@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
23715 govind 605
	public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
27876 amit.gupta 606
			@RequestParam(required = false) LocalDate date,
23752 govind 607
			@RequestParam(name = "limit", defaultValue = "10") int limit,
27897 amit.gupta 608
			@RequestParam(name = "searchItem", required = false, defaultValue = "0") int searchItem,
26802 tejbeer 609
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm,
610
			@RequestParam(name = "partnerType", required = false, defaultValue = "") PartnerType partnerType,
28795 tejbeer 611
			@RequestParam(name = "searchImei", required = false, defaultValue = "") String searchImei, Model model)
612
			throws ProfitMandiBusinessException {
23343 ashik.ali 613
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26802 tejbeer 614
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
27876 amit.gupta 615
		if (date == null) {
616
			date = LocalDate.now();
617
		}
618
		model.addAttribute("date", date);
26802 tejbeer 619
 
23343 ashik.ali 620
		List<Scheme> schemes = null;
28796 tejbeer 621
		List<SchemeInOut> schemeInOut = null;
622
 
23343 ashik.ali 623
		long size = 0;
28980 amit.gupta 624
		date = date.isAfter(LocalDate.now()) ? LocalDate.now() : date;
29649 amit.gupta 625
		final LocalDate date1 = date;
26802 tejbeer 626
		if (partnerType == null) {
29608 amit.gupta 627
			// TODO - SCHEME
628
 
629
			partnerType = partnerTypeChangeService.getTypeOnMonth(loginDetails.getFofoId(), YearMonth.from(date));
630
 
631
			// partnerType =
632
			// partnerTypeChangeService.getTypeOnDate(loginDetails.getFofoId(), date);
23343 ashik.ali 633
		}
27394 amit.gupta 634
		if (!(searchTerm.equals(""))) {
26802 tejbeer 635
			schemes = schemeRepository.selectBySearchTerm(searchTerm, offset, limit);
27612 tejbeer 636
			for (Scheme scheme : schemes) {
637
				if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
27394 amit.gupta 638
					scheme.setAmountModel(scheme.getAmount() + "%");
639
				} else {
640
					scheme.setAmountModel(scheme.getAmount() + "");
641
				}
642
			}
27876 amit.gupta 643
			if (schemes.size() > 0) {
26802 tejbeer 644
				size = schemeRepository.selectAllCount();
645
				LOGGER.info("schemes" + schemes);
646
				model.addAttribute("schemes", schemes);
647
				model.addAttribute("start", offset + 1);
648
				model.addAttribute("size", size);
649
				if (schemes.size() < limit) {
650
					model.addAttribute("end", offset + schemes.size());
27876 amit.gupta 651
				} else {
26802 tejbeer 652
					model.addAttribute("end", offset + limit);
653
				}
27612 tejbeer 654
			}
655
 
656
			else {
26802 tejbeer 657
				throw new ProfitMandiBusinessException("SchemeId", searchTerm, "SchemeId Not Found");
658
			}
27612 tejbeer 659
		}
660
 
27897 amit.gupta 661
		else if (searchItem > 0) {
27394 amit.gupta 662
			TagListing tagListing = null;
28134 tejbeer 663
			tagListing = tagListingRepository.selectByItemId(searchItem);
28492 amit.gupta 664
			Item item = itemRepository.selectById(searchItem);
28134 tejbeer 665
			if (tagListing != null) {
666
				model.addAttribute("dp", tagListing.getSellingPrice());
667
				model.addAttribute("mop", tagListing.getMop());
26802 tejbeer 668
			}
29707 tejbeer 669
			schemes = schemeService.selectSchemeByPartnerType(partnerType, date, searchItem, isAdmin, offset, limit)
670
					.stream().filter(x -> {
671
						return (x.getId() != 411 && x.getId() != 612) || date1.isBefore(LocalDate.of(2021, 12, 1));
672
					}).collect(Collectors.toList());
673
			// Remove 411 and 612
29658 amit.gupta 674
			model.addAttribute("schemes", schemes);
27612 tejbeer 675
			if (schemes.size() == 0) {
28795 tejbeer 676
				if (isAdmin)
677
					return "schemes";
678
				else {
27612 tejbeer 679
 
28795 tejbeer 680
					return "schemes-partner";
27389 amit.gupta 681
				}
682
			}
28795 tejbeer 683
			// For 7720(HR) remove investment
684
			int nlc = this.getNlc(item, loginDetails.getFofoId(), schemes, tagListing, searchItem);
27897 amit.gupta 685
			size = schemeService.selectSchemeCount(partnerType, date, searchItem, isAdmin);
26802 tejbeer 686
			model.addAttribute("start", offset + 1);
687
			model.addAttribute("size", size);
688
			if (schemes.size() < limit) {
689
				model.addAttribute("end", offset + schemes.size());
690
			} else {
691
				model.addAttribute("end", offset + limit);
692
			}
28547 amit.gupta 693
			model.addAttribute("nlc", Math.round(nlc));
28795 tejbeer 694
		} else if (!(searchImei.equals(""))) {
695
 
696
			LOGGER.info("searchImei" + searchImei);
697
 
698
			InventoryItem inventoryItem = inventoryItemRepository.selectBySerialNumberFofoId(searchImei,
699
					loginDetails.getFofoId());
28802 tejbeer 700
			Item item = itemRepository.selectById(inventoryItem.getItemId());
28795 tejbeer 701
			Map<Integer, Scheme> schemeMap = new HashMap<>();
702
			if (inventoryItem != null) {
703
 
704
				LOGGER.info("inventoryItem" + inventoryItem);
705
 
706
				schemeInOut = schemeInOutRepository
707
						.selectByInventoryItemIds(new HashSet<>(Arrays.asList(inventoryItem.getId())));
708
 
709
				LOGGER.info("schemeInOut" + schemeInOut);
710
 
711
				if (!schemeInOut.isEmpty()) {
712
 
713
					List<Integer> schemeIds = schemeInOut.stream().map(x -> x.getSchemeId())
714
							.collect(Collectors.toList());
715
 
716
					schemes = schemeRepository.selectBySchemeIds(schemeIds);
717
					for (Scheme scheme : schemes) {
718
						if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
719
							scheme.setAmountModel(scheme.getAmount() + "%");
720
						} else {
721
							scheme.setAmountModel(scheme.getAmount() + "");
722
						}
723
					}
724
 
725
					schemeMap = schemes.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
726
				}
727
			}
728
 
729
			model.addAttribute("schemeMap", schemeMap);
28802 tejbeer 730
			model.addAttribute("item", item);
28795 tejbeer 731
 
28796 tejbeer 732
		}
733
		model.addAttribute("schemeInOut", schemeInOut);
28795 tejbeer 734
 
27876 amit.gupta 735
		model.addAttribute("searchItem", searchItem);
736
		model.addAttribute("searchTerm", searchTerm);
737
		model.addAttribute("partnerType", partnerType);
738
		model.addAttribute("isAdmin", isAdmin);
29650 amit.gupta 739
		LOGGER.info("schemes" + schemes);
29707 tejbeer 740
		model.addAttribute("schemes", schemes != null ? schemes.stream().filter(x -> {
741
			return (x.getId() != 411 && x.getId() != 612) || date1.isBefore(LocalDate.of(2021, 12, 1));
29651 amit.gupta 742
		}).collect(Collectors.toList()) : null);
27876 amit.gupta 743
		LOGGER.info("schemes" + schemes);
23786 amit.gupta 744
		// model.addAttribute("roleTypes", loginDetails.getRoleTypes());
29899 tejbeer 745
		if (isAdmin) {
746
 
747
			boolean fullAccess = this.getAccess(loginDetails.getEmailId());
748
 
749
			model.addAttribute("fullAccess", fullAccess);
750
 
27876 amit.gupta 751
			return "schemes";
29899 tejbeer 752
		} else {
27897 amit.gupta 753
			List<CreateOfferRequest> offers = new ArrayList<>();
29883 amit.gupta 754
			Map<Integer, Map<Integer, Long>> offerSlabPayoutMap = new HashMap<>();
28795 tejbeer 755
			List<PriceDropIMEI> priceDropImeis = new ArrayList<>();
28134 tejbeer 756
			if (searchItem > 0) {
27897 amit.gupta 757
				offers = offerService.getPublishedOffers(date, loginDetails.getFofoId(), searchItem);
29899 tejbeer 758
				for (CreateOfferRequest createOfferRequest : offers) {
759
					Map<Integer, Map<Integer, Long>> itemSlabPayoutMap = offerService
760
							.getSlabPayoutMap(createOfferRequest);
761
					Map<Integer, Long> slabPayoutMap = itemSlabPayoutMap.get(searchItem);
29883 amit.gupta 762
					offerSlabPayoutMap.put(createOfferRequest.getId(), slabPayoutMap);
763
				}
27897 amit.gupta 764
			}
28795 tejbeer 765
			if (!(searchImei.equals(""))) {
766
				InventoryItem inventoryItem = inventoryItemRepository.selectBySerialNumberFofoId(searchImei,
767
						loginDetails.getFofoId());
768
 
769
				priceDropImeis = priceDropIMEIRepository.selectByFofoIdImei(loginDetails.getFofoId(), searchImei);
770
				for (PriceDropIMEI priceDropIMEI : priceDropImeis) {
771
					int priceDropId = priceDropIMEI.getPriceDropId();
772
					PriceDrop pd = priceDropRepository.selectById(priceDropId);
773
 
774
					priceDropIMEI.setPriceDrop(pd);
775
				}
776
			}
28136 tejbeer 777
			FofoStore fs = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
778
 
27876 amit.gupta 779
			model.addAttribute("offers", offers);
29883 amit.gupta 780
			model.addAttribute("offerSlabPayoutMap", offerSlabPayoutMap);
28136 tejbeer 781
			model.addAttribute("partnerCode", fs.getCode());
28795 tejbeer 782
			model.addAttribute("fofoId", fs.getId());
783
			model.addAttribute("priceDropImeis", priceDropImeis);
27876 amit.gupta 784
			return "schemes-partner";
785
		}
29899 tejbeer 786
 
22860 ashik.ali 787
	}
23786 amit.gupta 788
 
28795 tejbeer 789
	private int getNlc(Item item, int fofoId, List<Scheme> schemes, TagListing tagListing, int itemId)
790
			throws ProfitMandiBusinessException {
791
 
792
		if (item.getBrand().equals("Vivo") && fofoStoreRepository.getWarehousePartnerMap().get(7720).stream()
793
				.filter(x -> x.getId() == fofoId).count() > 0) {
794
			schemes = schemes.stream().filter(x -> !x.getType().equals(SchemeType.INVESTMENT))
795
					.collect(Collectors.toList());
796
		}
797
		float nlc = tagListing.getSellingPrice();
798
		for (Scheme scheme : schemes) {
799
			if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
800
				if (tagListing != null) {
801
					float taxRate = stateGstRateRepository.getIgstTaxRate(Arrays.asList(itemId)).get(itemId);
802
					float taxableSellingPrice = tagListing.getSellingPrice() / (1 + taxRate / 100);
803
					float amount = taxableSellingPrice * scheme.getAmount() / 100;
804
					scheme.setAmountModel(
805
							FormattingUtils.formatDecimalTwoDigits(amount) + " (" + scheme.getAmount() + "%)");
806
					nlc -= amount;
807
				}
808
 
809
				else {
810
					scheme.setAmountModel(scheme.getAmount() + "%");
811
				}
812
			} else {
813
				scheme.setAmountModel(scheme.getAmount() + "");
814
				nlc -= scheme.getAmount();
815
			}
816
		}
817
 
818
		return Math.round(nlc);
819
 
820
	}
821
 
22860 ashik.ali 822
	@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
27876 amit.gupta 823
	public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(required = false) LocalDate date,
23786 amit.gupta 824
			@RequestParam(name = "offset", defaultValue = "0") int offset,
26802 tejbeer 825
			@RequestParam(name = "limit", defaultValue = "10") int limit,
27876 amit.gupta 826
			@RequestParam(name = "partnerType", required = false, defaultValue = "ALL") PartnerType partnerType,
26802 tejbeer 827
			Model model) throws ProfitMandiBusinessException {
828
 
23343 ashik.ali 829
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26802 tejbeer 830
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
27876 amit.gupta 831
		if (date == null) {
832
			date = LocalDate.now();
833
		}
23271 ashik.ali 834
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
27876 amit.gupta 835
		List<Scheme> schemes = schemeRepository.selectAll();
26802 tejbeer 836
		int itemId = 0;
27394 amit.gupta 837
		TagListing tagListing = null;
27897 amit.gupta 838
 
27612 tejbeer 839
		for (Scheme scheme : schemes) {
840
			if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
841
				if (itemId > 0) {
27394 amit.gupta 842
					float taxRate = stateGstRateRepository.getIgstTaxRate(Arrays.asList(itemId)).get(itemId);
843
					float taxableSellingPrice = tagListing.getSellingPrice() / (1 + taxRate / 100);
844
					float amount = taxableSellingPrice * scheme.getAmount() / 100;
27612 tejbeer 845
					scheme.setAmountModel(
846
							FormattingUtils.formatDecimalTwoDigits(amount) + " (" + scheme.getAmount() + "%)");
27394 amit.gupta 847
				} else {
848
					scheme.setAmountModel(scheme.getAmount() + "%");
849
				}
850
			} else {
27876 amit.gupta 851
				scheme.setAmountModel("" + scheme.getAmount());
27394 amit.gupta 852
			}
853
		}
26802 tejbeer 854
 
22860 ashik.ali 855
		model.addAttribute("schemes", schemes);
26912 tejbeer 856
		model.addAttribute("partnerType", partnerType);
23914 govind 857
		model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
22860 ashik.ali 858
		return "schemes-paginated";
859
	}
23786 amit.gupta 860
 
23020 ashik.ali 861
	@RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
23786 amit.gupta 862
	public String downloadPage(HttpServletRequest request, Model model) {
23020 ashik.ali 863
		return "schemes-download";
864
	}
23786 amit.gupta 865
 
23020 ashik.ali 866
	@RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
23786 amit.gupta 867
	public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request,
23914 govind 868
			@RequestParam(name = ProfitMandiConstants.START_DATE_TIME) String startDateTimeString,
869
			@RequestParam(name = ProfitMandiConstants.END_DATE_TIME) String endDateTimeString, Model model)
23786 amit.gupta 870
			throws ProfitMandiBusinessException {
23914 govind 871
		LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString,
24406 amit.gupta 872
				DateTimePattern.DD_MM_yyyy_T_HH_MM_SS);
873
		LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_yyyy_T_HH_MM_SS);
23914 govind 874
 
23020 ashik.ali 875
		List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
23786 amit.gupta 876
 
23020 ashik.ali 877
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
878
		ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
23786 amit.gupta 879
 
880
		final HttpHeaders headers = new HttpHeaders();
881
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
23020 ashik.ali 882
		headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
23786 amit.gupta 883
		headers.setContentLength(byteArrayOutputStream.toByteArray().length);
884
		final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
885
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
886
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
23020 ashik.ali 887
	}
23786 amit.gupta 888
 
22860 ashik.ali 889
	@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
23786 amit.gupta 890
	public String getSchemeById(HttpServletRequest request,
891
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)
892
			throws ProfitMandiBusinessException {
23343 ashik.ali 893
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
29899 tejbeer 894
 
895
		boolean fullAccess = this.getAccess(loginDetails.getEmailId());
22860 ashik.ali 896
		Scheme scheme = schemeService.getSchemeById(schemeId);
29899 tejbeer 897
		model.addAttribute("fullAccess", fullAccess);
22860 ashik.ali 898
		model.addAttribute("scheme", scheme);
24445 amit.gupta 899
		model.addAttribute("isAdmin", roleManager.isAdmin(loginDetails.getRoleIds()));
22860 ashik.ali 900
		return "scheme-details";
23914 govind 901
 
22860 ashik.ali 902
	}
23786 amit.gupta 903
 
22860 ashik.ali 904
	@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
23786 amit.gupta 905
	public String activeSchemeById(HttpServletRequest request,
906
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,
907
			@RequestParam(name = "offset", defaultValue = "0") int offset,
908
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
909
			throws ProfitMandiBusinessException {
22860 ashik.ali 910
		schemeService.activeSchemeById(schemeId);
911
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
912
		model.addAttribute("schemes", schemes);
913
		return "schemes-paginated";
914
	}
23786 amit.gupta 915
 
22860 ashik.ali 916
	@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
23786 amit.gupta 917
	public String expireSchemeById(HttpServletRequest request,
918
			@RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId,
25069 amit.gupta 919
			@RequestParam(name = ProfitMandiConstants.EXPIRE_TIMESTAMP) LocalDateTime expiryTimestamp,
23786 amit.gupta 920
			@RequestParam(name = "offset", defaultValue = "0") int offset,
921
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
922
			throws ProfitMandiBusinessException {
28924 amit.gupta 923
		expiryTimestamp = LocalDateTime.of(expiryTimestamp.toLocalDate(), Utils.MAX_TIME);
25069 amit.gupta 924
		schemeService.expireSchemeById(schemeId, expiryTimestamp);
22860 ashik.ali 925
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
926
		model.addAttribute("schemes", schemes);
927
		return "schemes-paginated";
928
	}
23786 amit.gupta 929
 
23784 ashik.ali 930
	@RequestMapping(value = "/getSchemesJson", method = RequestMethod.GET)
23786 amit.gupta 931
	public ResponseEntity<?> getSchemesJson(HttpServletRequest request,
932
			@RequestParam(name = "offset", defaultValue = "0") int offset,
933
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
934
			throws ProfitMandiBusinessException {
23784 ashik.ali 935
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
936
		return responseSender.ok(schemeService.getSchemes(loginDetails.getRoleIds(), offset, limit));
937
	}
26802 tejbeer 938
 
26763 tejbeer 939
	@RequestMapping(value = "/searchSchemeByCategory")
940
	public String getSchemeByCategory(HttpServletRequest request,
941
			@RequestParam(name = "offset", defaultValue = "0") int offset,
942
			@RequestParam(name = "limit", defaultValue = "10") int limit,
943
			@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem,
944
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm,
945
			@RequestParam(name = "category", required = true, defaultValue = "") PartnerType category, Model model)
946
			throws ProfitMandiBusinessException {
947
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
948
		List<Scheme> schemes = null;
949
		long size = 0;
26802 tejbeer 950
 
951
		schemes = schemeRepository.selectByPartnerType(category, offset, limit);
952
 
953
		if (!(schemes.size() == 0)) {
954
			size = schemeRepository.selectAllCount();
955
			LOGGER.info("schemes" + schemes);
956
			model.addAttribute("schemes", schemes);
957
			model.addAttribute("start", offset + 1);
958
			model.addAttribute("size", size);
959
			model.addAttribute("searchTerm", searchTerm);
960
			model.addAttribute("searchItem", searchItem);
961
			model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
962
			if (schemes.size() < limit) {
963
				model.addAttribute("end", offset + schemes.size());
964
			} else {
965
				model.addAttribute("end", offset + limit);
26763 tejbeer 966
			}
26802 tejbeer 967
		}
968
 
26763 tejbeer 969
		return "schemes";
970
	}
26802 tejbeer 971
 
26763 tejbeer 972
	@RequestMapping(value = "/getSchemeByCategoryPaginated")
973
	public String getSchemeByCategoryPaginated(HttpServletRequest request,
974
			@RequestParam(name = "offset", defaultValue = "0") int offset,
975
			@RequestParam(name = "limit", defaultValue = "10") int limit,
976
			@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem,
977
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm,
978
			@RequestParam(name = "category", required = true, defaultValue = "") PartnerType category, Model model)
979
			throws ProfitMandiBusinessException {
980
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
981
		LOGGER.info("In search Item....");
982
		List<Scheme> schemes = null;
26802 tejbeer 983
 
26763 tejbeer 984
		schemes = schemeRepository.selectByPartnerType(category, offset, limit);
23506 amit.gupta 985
 
26763 tejbeer 986
		model.addAttribute("schemes", schemes);
987
		model.addAttribute("searchItem", searchItem);
988
		model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
989
		return "schemes-paginated";
990
	}
991
 
23752 govind 992
	@RequestMapping(value = "/searchScheme")
993
	public String getSchemeBySchemeId(HttpServletRequest request,
994
			@RequestParam(name = "offset", defaultValue = "0") int offset,
995
			@RequestParam(name = "limit", defaultValue = "10") int limit,
996
			@RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm,
997
			@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem, Model model)
998
			throws ProfitMandiBusinessException {
999
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
1000
		List<Scheme> schemes = null;
1001
		long size = 0;
1002
		if (!(searchTerm.equals("")) && searchItem.equals("")) {
1003
			schemes = schemeRepository.selectBySearchTerm(searchTerm, offset, limit);
23786 amit.gupta 1004
			if (!(schemes.size() == 0)) {
1005
				size = schemeRepository.selectAllCount();
1006
				LOGGER.info("schemes" + schemes);
1007
				model.addAttribute("schemes", schemes);
1008
				model.addAttribute("start", offset + 1);
1009
				model.addAttribute("size", size);
1010
				model.addAttribute("searchTerm", searchTerm);
1011
				model.addAttribute("searchItem", searchItem);
23914 govind 1012
				model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
23786 amit.gupta 1013
				if (schemes.size() < limit) {
1014
					model.addAttribute("end", offset + schemes.size());
1015
				} else {
1016
					model.addAttribute("end", offset + limit);
1017
				}
23752 govind 1018
			} else {
23786 amit.gupta 1019
				throw new ProfitMandiBusinessException("SchemeId", searchTerm, "SchemeId Not Found");
23752 govind 1020
			}
1021
		} else if (!(searchItem.equals("")) && searchTerm.equals("")) {
1022
			List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(Integer.parseInt(searchItem));
23786 amit.gupta 1023
			if (!(schemeIds.size() == 0)) {
23752 govind 1024
				LOGGER.info("schemeIds in searchItemScheme" + schemeIds);
1025
				schemes = schemeRepository.selectBySchemeIds(schemeIds, offset, limit);
1026
				size = schemeIds.size();
1027
				LOGGER.info("Size" + size);
1028
				model.addAttribute("schemes", schemes);
1029
				model.addAttribute("start", offset + 1);
1030
				model.addAttribute("size", size);
1031
				model.addAttribute("searchItem", searchItem);
1032
				model.addAttribute("searchTerm", searchTerm);
23914 govind 1033
				model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
23752 govind 1034
				if (schemes.size() < limit) {
1035
					model.addAttribute("end", offset + schemes.size());
1036
				} else {
1037
					model.addAttribute("end", offset + limit);
1038
				}
1039
 
1040
			} else {
1041
				throw new ProfitMandiBusinessException("SchemeIds", searchItem, "SchemeId Not Found");
1042
			}
1043
		} else {
23798 amit.gupta 1044
			if (roleManager.isAdmin(loginDetails.getRoleIds())) {
23752 govind 1045
				schemes = schemeRepository.selectAll(offset, limit);
1046
				size = schemeRepository.selectAllCount();
1047
			} else {
1048
				schemes = schemeRepository.selectActiveAll(offset, limit);
1049
				size = schemeRepository.selectAllActiveCount();
1050
			}
1051
			model.addAttribute("schemes", schemes);
1052
			model.addAttribute("start", offset + 1);
1053
			model.addAttribute("size", size);
1054
			model.addAttribute("searchItem", searchItem);
1055
			model.addAttribute("searchTerm", searchTerm);
23914 govind 1056
			model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
23752 govind 1057
			if (schemes.size() < limit) {
1058
				model.addAttribute("end", offset + schemes.size());
1059
			} else {
1060
				model.addAttribute("end", offset + limit);
1061
			}
1062
		}
1063
		return "schemes";
1064
	}
1065
 
1066
	@RequestMapping(value = "/searchItemSchemePanigated")
1067
	public String getSchemeByItemPanigated(HttpServletRequest request,
1068
			@RequestParam(name = "offset", defaultValue = "0") int offset,
1069
			@RequestParam(name = "limit", defaultValue = "10") int limit,
1070
			@RequestParam(name = "searchItem", required = false, defaultValue = "") String searchItem, Model model)
1071
			throws ProfitMandiBusinessException {
23914 govind 1072
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23752 govind 1073
		LOGGER.info("In search Item....");
1074
		List<Scheme> schemes = null;
1075
		if (!searchItem.equals("")) {
1076
			List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(Integer.parseInt(searchItem));
1077
			if (schemeIds != null) {
1078
				LOGGER.info(schemeIds);
1079
				schemes = schemeRepository.selectBySchemeIds(schemeIds, offset, limit);
1080
 
1081
			}
1082
		}
1083
		model.addAttribute("schemes", schemes);
1084
		model.addAttribute("searchItem", searchItem);
23914 govind 1085
		model.addAttribute("roleType", roleManager.isAdmin(loginDetails.getRoleIds()));
23752 govind 1086
		return "schemes-paginated";
1087
 
1088
	}
29707 tejbeer 1089
 
1090
	@RequestMapping(value = "/getCustomerOffer", method = RequestMethod.GET)
1091
	public String getCustomerOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
1092
 
1093
		List<CustomerOffer> customerOffers = customerOfferRepository.selectAll();
1094
 
1095
		model.addAttribute("customerOffers", customerOffers);
1096
		return "customer-offer";
1097
 
1098
	}
1099
 
1100
	@RequestMapping(value = "/createCustomerOffer", method = RequestMethod.POST)
1101
	public String createCustomerOffer(HttpServletRequest request, @RequestParam String offerName,
1102
			@RequestParam LocalDateTime startDate, @RequestParam LocalDateTime endDate, Model model) throws Exception {
1103
 
1104
		CustomerOffer co = new CustomerOffer();
1105
		co.setOfferName(offerName);
1106
		co.setStartDate(startDate);
1107
		co.setEndDate(endDate);
1108
		co.setCreatedTimestamp(LocalDateTime.now());
1109
		co.setUpdatedTimestamp(LocalDateTime.now());
1110
		customerOfferRepository.persist(co);
1111
 
1112
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
1113
 
1114
		return "response";
1115
 
1116
	}
1117
 
1118
	@RequestMapping(value = "/customerOffer/downloadTemplate", method = RequestMethod.GET)
1119
	public ResponseEntity<?> downloadCustomerOfferTemplate(HttpServletRequest request) throws Exception {
1120
		List<List<?>> rows = new ArrayList<>();
1121
 
1122
		org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil
1123
				.getCSVByteStream(Arrays.asList("id", "Catalog Id", "Scheme Payout", "Dealer Payout"), rows);
1124
 
1125
		final HttpHeaders headers = new HttpHeaders();
1126
		headers.set("Content-Type", "text/csv");
1127
		headers.set("Content-disposition", "inline; filename=customer-offer-template.csv");
1128
		headers.setContentLength(baos.toByteArray().length);
1129
 
1130
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
1131
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
1132
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
1133
 
1134
	}
1135
 
1136
	@RequestMapping(value = "/customerOfferItem/upload", method = RequestMethod.POST)
1137
	public String uploadCustomerOfferItem(HttpServletRequest request, Model model, @RequestParam int offerId,
1138
			HttpServletResponse response, @RequestPart MultipartFile file) throws Throwable {
1139
 
1140
		List<CSVRecord> records = FileUtil.readFile(file);
1141
 
1142
		for (CSVRecord record : records) {
1143
			if (Integer.parseInt(record.get(0)) == 0) {
1144
				CustomerOfferItem coi = new CustomerOfferItem();
1145
				coi.setCatalogId(Integer.parseInt(record.get(1)));
1146
				coi.setSchemePayout(Integer.parseInt(record.get(2)));
1147
				coi.setDealerPayout(Integer.parseInt(record.get(3)));
1148
				coi.setCustomerOfferId(offerId);
1149
				coi.setUpdatedTimestamp(LocalDateTime.now());
1150
				coi.setCreatedTimestamp(LocalDateTime.now());
1151
				customerOfferItemRepository.persist(coi);
1152
 
1153
			} else {
1154
				CustomerOfferItem coi = customerOfferItemRepository.selectById(Integer.parseInt(record.get(0)));
1155
				coi.setSchemePayout(Integer.parseInt(record.get(2)));
1156
				coi.setDealerPayout(Integer.parseInt(record.get(3)));
1157
				coi.setUpdatedTimestamp(LocalDateTime.now());
1158
 
1159
			}
1160
 
1161
		}
1162
 
1163
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
1164
 
1165
		return "response";
1166
 
1167
	}
1168
 
1169
	@RequestMapping(value = "/customerOfferItem/download", method = RequestMethod.GET)
1170
	public ResponseEntity<?> downloadOfferItem(HttpServletRequest request, @RequestParam int offerId) throws Exception {
1171
 
1172
		List<CustomerOfferItem> cois = customerOfferItemRepository.selectByOfferId(offerId);
1173
		List<List<?>> rows = new ArrayList<>();
1174
 
1175
		for (CustomerOfferItem coi : cois) {
1176
			rows.add(Arrays.asList(coi.getId(), coi.getCatalogId(), coi.getSchemePayout(), coi.getDealerPayout()));
1177
 
1178
		}
1179
 
1180
		org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil
1181
				.getCSVByteStream(Arrays.asList("id", "Catalog Id", "Scheme Payout", "Dealer Payout"), rows);
1182
 
1183
		final HttpHeaders headers = new HttpHeaders();
1184
		headers.set("Content-Type", "text/csv");
1185
		headers.set("Content-disposition", "inline; filename=customer-offer-template.csv");
1186
		headers.setContentLength(baos.toByteArray().length);
1187
 
1188
		final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
1189
		final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
1190
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
1191
 
1192
	}
1193
 
1194
	@RequestMapping(value = "/extendCustomerOffer", method = RequestMethod.POST)
1195
	public String extendCustomerOffer(HttpServletRequest request, @RequestParam int id,
1196
			@RequestParam LocalDateTime endDate, Model model) throws ProfitMandiBusinessException {
1197
 
1198
		CustomerOffer co = customerOfferRepository.selectById(id);
1199
		co.setEndDate(endDate);
1200
		co.setUpdatedTimestamp(LocalDateTime.now());
1201
 
1202
		model.addAttribute("co", co);
1203
 
1204
		return "customer-index-offer";
1205
 
1206
	}
1207
 
1208
	@RequestMapping(value = "/getSamsungUpgradeOffer", method = RequestMethod.GET)
1209
	public String getSamsungUpgradeOffer(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
1210
 
1211
		List<UpgradeOfferStatus> uos = UpgradeOfferStatus.offerStatus;
1212
		List<SamsungUpgradeOffer> samsungUpgradeOffers = samsungUpgradeOfferRepository.selectByStatus(
1213
				uos.stream().filter(x -> !x.equals(UpgradeOfferStatus.rejected)).collect(Collectors.toList()));
1214
 
1215
		model.addAttribute("samsungUpgradeOffers", samsungUpgradeOffers);
1216
		return "samsung-upgrade-offer";
1217
 
1218
	}
1219
 
1220
	@RequestMapping(value = "/approveSamsungUpgradeOffer", method = RequestMethod.POST)
1221
	public String approveSamsungUpgradeOffer(HttpServletRequest request, @RequestParam int id, Model model)
1222
			throws ProfitMandiBusinessException {
1223
 
1224
		SamsungUpgradeOffer suo = samsungUpgradeOfferRepository.selectById(id);
1225
		suo.setStatus(UpgradeOfferStatus.approved);
1226
		suo.setApprovedTimestamp(LocalDateTime.now());
1227
		model.addAttribute("suo", suo);
1228
		return "samsung-upgrade-offer-index";
1229
 
1230
	}
1231
 
1232
	@RequestMapping(value = "/rejectSamsungUpgradeOffer", method = RequestMethod.POST)
1233
	public String rejectSamsungUpgradeOffer(HttpServletRequest request, @RequestParam int id, Model model)
1234
			throws ProfitMandiBusinessException {
1235
 
1236
		SamsungUpgradeOffer suo = samsungUpgradeOfferRepository.selectById(id);
1237
		suo.setStatus(UpgradeOfferStatus.rejected);
1238
 
1239
		model.addAttribute("suo", suo);
1240
		return "samsung-upgrade-offer-index";
1241
 
1242
	}
1243
 
22860 ashik.ali 1244
}