Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22981 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
23494 ashik.ali 3
import com.spice.profitmandi.common.enumuration.ContentType;
23955 govind 4
import com.spice.profitmandi.common.enumuration.CounterSize;
28825 tejbeer 5
import com.spice.profitmandi.common.enumuration.FofoType;
22981 ashik.ali 6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30017 amit.gupta 7
import com.spice.profitmandi.common.model.*;
28024 tejbeer 8
import com.spice.profitmandi.common.util.Utils;
23330 ashik.ali 9
import com.spice.profitmandi.common.web.util.ResponseSender;
27231 tejbeer 10
import com.spice.profitmandi.dao.entity.auth.AuthUser;
30017 amit.gupta 11
import com.spice.profitmandi.dao.entity.dtr.*;
12
import com.spice.profitmandi.dao.entity.fofo.*;
24159 tejbeer 13
import com.spice.profitmandi.dao.entity.user.Location;
14
import com.spice.profitmandi.dao.entity.user.Promoter;
24123 tejbeer 15
import com.spice.profitmandi.dao.entity.user.User;
27231 tejbeer 16
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
17
import com.spice.profitmandi.dao.repository.cs.CsService;
28272 tejbeer 18
import com.spice.profitmandi.dao.repository.cs.RetailerBrandsLimitService;
30017 amit.gupta 19
import com.spice.profitmandi.dao.repository.dtr.*;
27797 tejbeer 20
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeRepository;
21
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
28381 tejbeer 22
import com.spice.profitmandi.dao.repository.fofo.PincodePartnerRepository;
24123 tejbeer 23
import com.spice.profitmandi.dao.repository.user.LocationRepository;
24159 tejbeer 24
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
24123 tejbeer 25
import com.spice.profitmandi.dao.repository.user.UserRepository;
24349 amit.gupta 26
import com.spice.profitmandi.service.PartnerInvestmentService;
32731 ranu 27
import com.spice.profitmandi.service.catalog.BrandsService;
24159 tejbeer 28
import com.spice.profitmandi.service.inventory.InventoryService;
22981 ashik.ali 29
import com.spice.profitmandi.service.user.RetailerService;
28908 tejbeer 30
import com.spice.profitmandi.service.user.StoreTimelineTatService;
25276 amit.gupta 31
import com.spice.profitmandi.web.model.LoginDetails;
32
import com.spice.profitmandi.web.util.CookiesProcessor;
24123 tejbeer 33
import com.spice.profitmandi.web.util.MVCResponseSender;
30017 amit.gupta 34
import org.apache.logging.log4j.LogManager;
35
import org.apache.logging.log4j.Logger;
36
import org.json.JSONObject;
37
import org.springframework.beans.factory.annotation.Autowired;
38
import org.springframework.beans.factory.annotation.Qualifier;
39
import org.springframework.core.io.InputStreamResource;
40
import org.springframework.http.HttpHeaders;
41
import org.springframework.http.HttpStatus;
42
import org.springframework.http.ResponseEntity;
43
import org.springframework.stereotype.Controller;
44
import org.springframework.ui.Model;
45
import org.springframework.web.bind.annotation.*;
22981 ashik.ali 46
 
30017 amit.gupta 47
import javax.servlet.http.HttpServletRequest;
48
import javax.transaction.Transactional;
49
import java.io.File;
50
import java.io.FileInputStream;
51
import java.io.FileNotFoundException;
52
import java.time.LocalDate;
53
import java.time.LocalDateTime;
54
import java.util.*;
55
import java.util.stream.Collectors;
56
 
22981 ashik.ali 57
@Controller
25277 amit.gupta 58
@Transactional(rollbackOn = Throwable.class)
22981 ashik.ali 59
public class RetailerController {
60
 
23568 govind 61
	private static final Logger LOGGER = LogManager.getLogger(RetailerController.class);
24124 govind 62
 
22981 ashik.ali 63
	@Autowired
64
	private RetailerService retailerService;
24124 govind 65
 
32667 raveendra. 66
 
23330 ashik.ali 67
	@Autowired
28832 tejbeer 68
	private RetailerRepository retailerRepository;
69
 
23494 ashik.ali 70
	@Autowired
32751 amit.gupta 71
	private PincodePartnerRepository pincodePartnerRepository;
28832 tejbeer 72
 
28381 tejbeer 73
	@Autowired
28272 tejbeer 74
	private BrandLimitRepository brandLimitRepository;
75
 
76
	@Autowired
77
	private RetailerBrandsLimitService retailerBrandsLimitService;
78
 
79
	@Autowired
80
	private RetailerBrandsLimitRepository retailerBrandsLimitRepository;
81
 
82
	@Autowired
23494 ashik.ali 83
	private ShopRepository shopRepository;
24124 govind 84
 
23494 ashik.ali 85
	@Autowired
28024 tejbeer 86
	private RetailerBlockBrandsRepository retailerBlockBrandsRepository;
87
 
88
	@Autowired
24124 govind 89
	private FofoStoreRepository fofoStoreRepository;
90
 
91
	@Autowired
23494 ashik.ali 92
	private DocumentRepository documentRepository;
24124 govind 93
 
23494 ashik.ali 94
	@Autowired
25276 amit.gupta 95
	private CookiesProcessor cookiesProcessor;
96
 
97
	@Autowired
24159 tejbeer 98
	@Qualifier("userUserRepository")
24123 tejbeer 99
	private UserRepository userRepository;
24124 govind 100
 
24123 tejbeer 101
	@Autowired
27231 tejbeer 102
	private AuthRepository authRepository;
103
 
104
	@Autowired
105
	private CsService csService;
106
 
107
	@Autowired
24123 tejbeer 108
	private LocationRepository locationRepository;
24124 govind 109
 
24123 tejbeer 110
	@Autowired
23330 ashik.ali 111
	private ResponseSender<?> responseSender;
24124 govind 112
 
24123 tejbeer 113
	@Autowired
114
	private MVCResponseSender mvcResponseSender;
24124 govind 115
 
24159 tejbeer 116
	@Autowired
117
	private InventoryService inventoryService;
118
 
119
	@Autowired
120
	private PromoterRepository promoterRepository;
121
 
122
	@Autowired
24349 amit.gupta 123
	private PartnerInvestmentService partnerInvestmentService;
124
 
125
	@Autowired
24159 tejbeer 126
	private Mongo mongoClient;
127
 
27797 tejbeer 128
	@Autowired
129
	private PartnerTypeChangeService partnerTypeChangeService;
130
 
131
	@Autowired
132
	private PartnerTypeChangeRepository partnerTypeChangeRepository;
133
 
28908 tejbeer 134
	@Autowired
135
	PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
136
 
137
	@Autowired
138
	private StoreTimelineTatService storeTimelineTatService;
139
 
23784 ashik.ali 140
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.GET)
24124 govind 141
	public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request,
30426 tejbeer 142
			@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
143
			Model model) throws ProfitMandiBusinessException {
24124 govind 144
		LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(),
145
				emailIdOrMobileNumber);
28071 tejbeer 146
 
147
		Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
148
 
26771 amit.gupta 149
		model.addAllAttributes(map);
28071 tejbeer 150
 
23955 govind 151
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 152
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
28825 tejbeer 153
 
26209 tejbeer 154
		LOGGER.info("warehouseMap", warehouseMap);
155
		model.addAttribute("warehouses", warehouseMap);
28825 tejbeer 156
		model.addAttribute("fofoTypes", FofoType.values());
26771 amit.gupta 157
		if (map.containsKey("retailer")) {
158
			Retailer retailer = (Retailer) map.get("retailer");
159
			User user = userRepository.selectById(retailer.getId());
160
			if (user.getLocation() != null) {
161
				Location location = locationRepository.selectById(user.getLocation());
162
				model.addAttribute("locationdetail", location);
163
				LOGGER.info("location" + location);
164
			}
165
		}
22981 ashik.ali 166
		return "retailer-details";
167
	}
24124 govind 168
 
23026 ashik.ali 169
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 170
	public String updateRetailerDetails(HttpServletRequest request,
30426 tejbeer 171
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 172
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
173
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
32667 raveendra. 174
		//retailerService.up
28071 tejbeer 175
 
23026 ashik.ali 176
		model.addAllAttributes(map);
23955 govind 177
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 178
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
179
		LOGGER.info("warehouseMap", warehouseMap);
180
		model.addAttribute("warehouses", warehouseMap);
28832 tejbeer 181
		model.addAttribute("fofoTypes", FofoType.values());
23026 ashik.ali 182
		return "retailer-details";
183
	}
22981 ashik.ali 184
 
32667 raveendra. 185
	@RequestMapping(value = "/retailer/update-email", method = RequestMethod.POST)
186
	public String updateRetailerEmail(@RequestParam("newEmail") String newEmail, @RequestParam("fofoID") int fofoID) throws ProfitMandiBusinessException {
187
		retailerService.updateRetailerEmail(fofoID, newEmail);
188
		return "retailer-details";
189
	}
190
 
22981 ashik.ali 191
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 192
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 193
		return "retailer-info";
194
	}
24124 govind 195
 
23330 ashik.ali 196
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 197
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 198
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
199
	}
24124 govind 200
 
23494 ashik.ali 201
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 202
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
30426 tejbeer 203
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
204
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 205
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 206
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 207
 
208
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 209
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
210
		}
24124 govind 211
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 212
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
213
		}
214
		return responseSender.ok(document);
215
	}
24124 govind 216
 
23494 ashik.ali 217
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 218
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
30426 tejbeer 219
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
24124 govind 220
			throws ProfitMandiBusinessException {
221
 
23499 ashik.ali 222
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 223
 
224
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 225
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
226
		}
24124 govind 227
 
23494 ashik.ali 228
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 229
 
23494 ashik.ali 230
		FileInputStream file = null;
231
		try {
232
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
233
		} catch (FileNotFoundException e) {
234
			LOGGER.error("Retailer Document file not found : ", e);
235
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
236
		}
24159 tejbeer 237
		// ByteArrayOutputStream byteArrayOutputStream = new
238
		// ByteArrayOutputStream();
24124 govind 239
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
240
 
241
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 242
		String contentType = "";
24124 govind 243
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 244
			contentType = "image/jpeg";
24124 govind 245
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 246
			contentType = "image/png";
24124 govind 247
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 248
			contentType = "application/pdf";
249
		}
24124 govind 250
		headers.set("Content-Type", contentType);
251
		headers.set("Content-disposition", "inline; filename=" + document.getName());
252
		headers.setContentLength(document.getSize());
253
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
254
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
255
 
24159 tejbeer 256
		// return
257
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 258
	}
24124 govind 259
 
23494 ashik.ali 260
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 261
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
30426 tejbeer 262
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
263
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 264
		Shop shop = shopRepository.selectById(shopId);
24124 govind 265
 
266
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 267
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
268
		}
24124 govind 269
 
270
		if (shop.getDocumentId() == null) {
23494 ashik.ali 271
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
272
		}
24124 govind 273
 
23494 ashik.ali 274
		Document document = documentRepository.selectById(shop.getDocumentId());
275
		return responseSender.ok(document);
276
	}
24124 govind 277
 
23494 ashik.ali 278
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 279
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
30426 tejbeer 280
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
281
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
24124 govind 282
			throws ProfitMandiBusinessException {
283
 
23494 ashik.ali 284
		Shop shop = shopRepository.selectById(shopId);
24124 govind 285
 
286
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 287
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
288
		}
24124 govind 289
 
290
		if (shop.getDocumentId() == null) {
23494 ashik.ali 291
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
292
		}
24124 govind 293
 
23494 ashik.ali 294
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 295
 
23494 ashik.ali 296
		FileInputStream file = null;
297
		try {
298
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
299
		} catch (FileNotFoundException e) {
300
			LOGGER.error("Retailer Document file not found : ", e);
301
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
302
		}
24159 tejbeer 303
		// ByteArrayOutputStream byteArrayOutputStream = new
304
		// ByteArrayOutputStream();
24124 govind 305
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
306
 
307
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 308
		String contentType = "";
24124 govind 309
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 310
			contentType = "image/jpeg";
24124 govind 311
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 312
			contentType = "image/png";
24124 govind 313
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 314
			contentType = "application/pdf";
315
		}
24124 govind 316
		headers.set("Content-Type", contentType);
317
		headers.set("Content-disposition", "inline; filename=" + document.getName());
318
		headers.setContentLength(document.getSize());
319
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
320
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
321
 
24159 tejbeer 322
		// return
323
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 324
	}
30017 amit.gupta 325
 
29267 manish 326
	@GetMapping(value = "/getstore")
327
	public String getstore(HttpServletRequest request, Model model) {
30017 amit.gupta 328
 
29267 manish 329
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
330
				.collect(Collectors.toList());
30426 tejbeer 331
 
332
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
333
 
334
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = fofoIds.stream().map(x -> customRetailerMap.get(x))
335
				.filter(x -> x != null).collect(Collectors.toList()).stream()
336
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
337
 
29267 manish 338
		LOGGER.info("fofoIds" + fofoIds);
339
		model.addAttribute("fofoIdsAndCustomRetailer", fofoIdsAndCustomRetailer);
340
		LOGGER.info("fofoIdsAndCustomRetailer" + fofoIdsAndCustomRetailer);
341
		model.addAttribute("fofoIds", fofoIds);
24124 govind 342
 
29267 manish 343
		return "store";
344
 
345
	}
30017 amit.gupta 346
 
32207 amit.gupta 347
	private static final List<String> CHANGE_PARTNER_CATEGORY_PERMISSIONS = Arrays.asList("tarun.verma@smartdukaan.com", "kamini.sharma@smartdukaan.com", "shankar.mushra@smartdukaan.com");
348
 
29267 manish 349
	@RequestMapping(value = "/getPartnerinfo")
30426 tejbeer 350
	public String getPartnerinfo(HttpServletRequest request, @RequestBody SelectStorePartnerInfo selectStorePartnerInfo,
32207 amit.gupta 351
								 Model model) throws ProfitMandiBusinessException {
352
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
29267 manish 353
		LOGGER.info("selectStorePartnerInfo" + selectStorePartnerInfo.getUserIds());
354
		List<FofoStore> fofoStores = fofoStoreRepository.selectByRetailerIds(selectStorePartnerInfo.getUserIds());
355
		LOGGER.info("fofoStoresInfo" + fofoStores);
356
		Map<Integer, Boolean> investments = new HashMap<>();
357
 
358
		for (FofoStore fofoStore : fofoStores) {
359
			boolean isOk = false;
360
			if (fofoStore.getGraceDate() != null && fofoStore.getGraceDate().isAfter(LocalDate.now())) {
361
				isOk = true;
362
			} else {
363
				try {
31521 amit.gupta 364
					isOk = partnerInvestmentService.isInvestmentOk(fofoStore.getId(), ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE,
29267 manish 365
							ProfitMandiConstants.CUTOFF_INVESTMENT);
366
					if (fofoStore.getGraceCount() > 0) {
367
						fofoStore.setGraceDate(null);
368
						fofoStore.setGraceCount(0);
369
					}
370
				} catch (ProfitMandiBusinessException e) {
30017 amit.gupta 371
				}
29267 manish 372
			}
373
			investments.put(fofoStore.getId(), isOk);
374
		}
375
 
376
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(false);
377
		model.addAttribute("fofoStores", fofoStores);
378
		model.addAttribute("investments", investments);
379
		model.addAttribute("customRetailers", customRetailers);
32207 amit.gupta 380
		boolean canChangeCategory = CHANGE_PARTNER_CATEGORY_PERMISSIONS.contains(loginDetails.getEmailId());
381
		model.addAttribute("canChangeCategory", canChangeCategory);
29267 manish 382
 
383
		return "store-investment";
384
 
385
	}
30017 amit.gupta 386
 
24124 govind 387
	@GetMapping(value = "/getAllStores")
32207 amit.gupta 388
	public String getAllStores(HttpServletRequest request, Model model) throws Exception {
24124 govind 389
 
25276 amit.gupta 390
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
391
				.collect(Collectors.toList());
24349 amit.gupta 392
		Map<Integer, Boolean> investments = new HashMap<>();
24124 govind 393
 
24349 amit.gupta 394
		for (FofoStore fofoStore : fofoStores) {
395
			boolean isOk = false;
396
			if (fofoStore.getGraceDate() != null && fofoStore.getGraceDate().isAfter(LocalDate.now())) {
397
				isOk = true;
398
			} else {
399
				try {
31521 amit.gupta 400
					isOk = partnerInvestmentService.isInvestmentOk(fofoStore.getId(), ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE,
25276 amit.gupta 401
							ProfitMandiConstants.CUTOFF_INVESTMENT);
402
					if (fofoStore.getGraceCount() > 0) {
24349 amit.gupta 403
						fofoStore.setGraceDate(null);
404
						fofoStore.setGraceCount(0);
405
					}
406
				} catch (ProfitMandiBusinessException e) {
407
				}
408
			}
409
			investments.put(fofoStore.getId(), isOk);
410
		}
24124 govind 411
 
26963 amit.gupta 412
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(false);
24124 govind 413
		model.addAttribute("fofoStores", fofoStores);
24349 amit.gupta 414
		model.addAttribute("investments", investments);
24124 govind 415
		model.addAttribute("customRetailers", customRetailers);
416
 
32207 amit.gupta 417
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
418
		boolean canChangeCategory = CHANGE_PARTNER_CATEGORY_PERMISSIONS.contains(loginDetails.getEmailId());
419
		model.addAttribute("canChangeCategory", canChangeCategory);
420
 
29267 manish 421
		return "store-investment";
24124 govind 422
 
423
	}
424
 
27797 tejbeer 425
	@GetMapping(value = "/getPartnerCategory")
426
	public String getPartnerCategory(HttpServletRequest request, @RequestParam int fofoId, Model model) {
427
 
428
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
429
		List<PartnerType> partnerTypes = partnerType.nextPartnerTypes();
430
		LOGGER.info("partnerType" + partnerTypes);
431
		model.addAttribute("partnerTypes", partnerTypes);
432
		model.addAttribute("fofoId", fofoId);
433
		return "partner-category";
434
	}
435
 
436
	@PostMapping(value = "/updatePartnerCategory")
437
	public String updatePartnerCategory(HttpServletRequest request, @RequestParam(name = "fofoId") int fofoId,
30426 tejbeer 438
			@RequestParam(name = "partnerType") PartnerType partnerType, Model model) throws Exception {
27797 tejbeer 439
 
440
		PartnerTypeChange ptc = new PartnerTypeChange();
441
		ptc.setFofoId(fofoId);
442
		ptc.setPartnerType(partnerType);
443
		ptc.setCreateTimestamp(LocalDate.now().atStartOfDay().plusDays(1));
444
		partnerTypeChangeRepository.persist(ptc);
31238 amit.gupta 445
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
27797 tejbeer 446
 
447
		return "response";
448
	}
449
 
24349 amit.gupta 450
	@GetMapping(value = "/partners")
451
	public String getStoreName(HttpServletRequest request, Model model, @RequestParam String query) throws Exception {
31321 tejbeer 452
		model.addAttribute("response1",
453
				mvcResponseSender.createResponseString(retailerService.getFofoRetailers(false).values().stream()
31238 amit.gupta 454
						.filter(x -> x.getDisplayName().toLowerCase().matches(".*?" + query.toLowerCase() + ".*?"))
455
						.collect(Collectors.toList())));
24349 amit.gupta 456
		return "response";
457
	}
458
 
24124 govind 459
	@PostMapping(value = "/deactivateStore")
460
	public String deActivateStore(HttpServletRequest request,
30426 tejbeer 461
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
24124 govind 462
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 463
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
30964 tejbeer 464
		if (!Arrays
465
				.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "amit.gupta@smartdukaan.com",
31321 tejbeer 466
						"rahul.katyal@smartdukaan.com", "tejbeer.kaur@smartdukaan.com", "sm@smartdukaan.com")
30964 tejbeer 467
				.contains(loginDetails.getEmailId())) {
26131 tejbeer 468
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
469
					"You are not authorise to deactivate retailer");
25276 amit.gupta 470
		}
24843 govind 471
		fofoStore.setActive(false);
24124 govind 472
		LOGGER.info("inserted into InActiveFofoStore successfully");
31238 amit.gupta 473
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
24124 govind 474
		return "response";
475
	}
28825 tejbeer 476
 
28710 amit.gupta 477
	@PostMapping(value = "/activateStoreForever")
478
	public String activateStoreForever(HttpServletRequest request,
30426 tejbeer 479
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
28710 amit.gupta 480
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
481
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
30964 tejbeer 482
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "amit.gupta@smartdukaan.com",
31321 tejbeer 483
				"rahul.katyal@smartdukaan.com", "sm@smartdukaan.com").contains(loginDetails.getEmailId())) {
28710 amit.gupta 484
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
485
					"You are not authorise to activate retailer");
486
		}
487
		fofoStore.setActive(true);
488
		LOGGER.info("inserted into ActiveFofoStore successfully");
31238 amit.gupta 489
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
28710 amit.gupta 490
		return "response";
491
	}
28825 tejbeer 492
 
28710 amit.gupta 493
	@PostMapping(value = "/activateStoreTemporary")
494
	public String activateStoreTemporary(HttpServletRequest request,
30426 tejbeer 495
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId,
496
			@RequestParam(name = "days", required = true) int days, Model model) throws Exception {
28825 tejbeer 497
 
28710 amit.gupta 498
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
499
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
30964 tejbeer 500
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "amit.gupta@smartdukaan.com",
31321 tejbeer 501
				"rahul.katyal@smartdukaan.com", "sm@smartdukaan.com").contains(loginDetails.getEmailId())) {
28710 amit.gupta 502
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
503
					"You are not authorise to deactivate retailer");
504
		}
28825 tejbeer 505
 
506
		LocalDateTime currentDate = LocalDate.now().atStartOfDay();
507
 
28710 amit.gupta 508
		LocalDateTime EndDate = currentDate.plusDays(days);
509
		fofoStore.setActiveTimeStamp(EndDate);
28825 tejbeer 510
 
511
		LOGGER.info("EndDate" + EndDate);
512
		LOGGER.info("getActiveTimeStamp" + fofoStore.getActiveTimeStamp());
513
 
514
		if (currentDate.isBefore(fofoStore.getActiveTimeStamp())) {
515
			fofoStore.setActive(true);
516
			LOGGER.info("inserted into InActiveFofoStore successfully");
28710 amit.gupta 517
		}
28825 tejbeer 518
 
31238 amit.gupta 519
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
28710 amit.gupta 520
		return "response";
521
	}
25276 amit.gupta 522
 
523
	@GetMapping(value = "/getAllInactiveStores")
524
	public String getInactiveStores(HttpServletRequest request, Model model) {
525
		List<FofoStore> inActiveFofoStores = fofoStoreRepository.selectInActiveStore();
30426 tejbeer 526
 
30431 tejbeer 527
		LOGGER.info("inActiveFofoStores {}", inActiveFofoStores);
30426 tejbeer 528
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
529
 
30431 tejbeer 530
		List<Integer> fofoIds = inActiveFofoStores.stream().map(x -> x.getId()).collect(Collectors.toList());
531
 
532
		Map<Integer, CustomRetailer> customRetailers = fofoIds.stream().map(x -> customRetailerMap.get(x))
30426 tejbeer 533
				.filter(x -> x != null).collect(Collectors.toList()).stream()
534
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
30431 tejbeer 535
		LOGGER.info("customRetailers {}", customRetailers);
30426 tejbeer 536
 
24680 govind 537
		model.addAttribute("inActiveFofoStores", inActiveFofoStores);
538
		model.addAttribute("customRetailers", customRetailers);
539
		return "inactive-stores";
540
	}
25276 amit.gupta 541
 
542
	// Extend billing for 2 days
24349 amit.gupta 543
	@PostMapping(value = "/extendBilling")
544
	public String extendBilling(HttpServletRequest request,
30426 tejbeer 545
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
24349 amit.gupta 546
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 547
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
30963 tejbeer 548
		if (Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "amit.gupta@smartdukaan.com",
32190 amit.gupta 549
				"rahul.katyal@smartdukaan.com", "sm@smartdukaan.com", "mohit.gulati@smartdukaan.com").contains(loginDetails.getEmailId())) {
25276 amit.gupta 550
			fofoStore.setGraceDate(LocalDate.now().plusDays(2));
551
			fofoStore.setGraceCount(fofoStore.getGraceCount() + 1);
31238 amit.gupta 552
			model.addAttribute("response1", mvcResponseSender.createResponseString(fofoStore.getGraceCount()));
25276 amit.gupta 553
		} else {
26131 tejbeer 554
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
555
					"You are not authorise to extend billing");
25276 amit.gupta 556
		}
24349 amit.gupta 557
		return "response";
25276 amit.gupta 558
 
24124 govind 559
	}
560
 
24159 tejbeer 561
	@RequestMapping(value = "/getPromoterInfo", method = RequestMethod.GET)
562
	public String getPromoterInfo(HttpServletRequest request,
30426 tejbeer 563
			@RequestParam(name = "offset", defaultValue = "0") int offset,
564
			@RequestParam(name = "limit", defaultValue = "10") int limit,
565
			@RequestParam(name = "fofoId", required = false, defaultValue = "0") int fofoId, Model model)
27489 tejbeer 566
			throws Exception {
24159 tejbeer 567
		LOGGER.info("requested url : " + request.getRequestURL().toString());
568
		List<Promoter> promoterInfo = null;
569
 
570
		long size = 0;
27489 tejbeer 571
 
24159 tejbeer 572
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
573
				.collect(Collectors.toList());
30426 tejbeer 574
 
575
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
576
 
577
		Map<Integer, CustomRetailer> customRetailersMap = fofoIds.stream().map(x -> customRetailerMap.get(x))
578
				.filter(x -> x != null).collect(Collectors.toList()).stream()
579
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
24159 tejbeer 580
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
24986 tejbeer 581
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
582
		brands.add("Airtel");
583
		brands.add("Vodafone");
584
		brands.add("Idea");
25276 amit.gupta 585
		LOGGER.info("brands" + brands);
24159 tejbeer 586
		model.addAttribute("customRetailers", customRetailers);
25276 amit.gupta 587
		model.addAttribute("brands", brands);
24159 tejbeer 588
 
27489 tejbeer 589
		if (fofoId != 0) {
590
			promoterInfo = promoterRepository.selectAllPromoterByFofoId(fofoId, offset, limit);
24159 tejbeer 591
 
27489 tejbeer 592
			size = promoterRepository.selectPromoterCount(fofoId);
593
 
594
		} else {
32667 raveendra. 595
			//rkb
27489 tejbeer 596
			promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
597
 
598
			size = promoterRepository.selectAllCount();
599
 
600
		}
24159 tejbeer 601
		if (!promoterInfo.isEmpty()) {
30944 tejbeer 602
			Set<Integer> partnerIds = new HashSet<>();
24159 tejbeer 603
 
604
			for (Promoter promoterdetail : promoterInfo) {
605
				partnerIds.add(promoterdetail.getRetailerId());
606
			}
607
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 608
			if (!partnerIds.equals(null)) {
24159 tejbeer 609
				LOGGER.info("partnerIds" + partnerIds);
30426 tejbeer 610
 
611
				partnersMap = partnerIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null)
612
						.collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
613
 
24159 tejbeer 614
			}
24349 amit.gupta 615
 
24159 tejbeer 616
			LOGGER.info("partnerIds" + partnersMap);
617
			model.addAttribute("promoterInfo", promoterInfo);
618
			model.addAttribute("partnersMap", partnersMap);
619
			model.addAttribute("start", offset + 1);
620
			model.addAttribute("size", size);
621
			model.addAttribute("url", "/getPaginatedPromoterInfo");
622
 
623
			if (promoterInfo.size() < limit) {
624
				model.addAttribute("end", offset + promoterInfo.size());
625
			} else {
626
				model.addAttribute("end", offset + limit);
627
			}
628
		} else {
629
			model.addAttribute("promoterInfo", promoterInfo);
630
			model.addAttribute("size", size);
631
		}
632
		return "promoter-info";
633
 
634
	}
24349 amit.gupta 635
 
24159 tejbeer 636
	@RequestMapping(value = "/getPaginatedPromoterInfo", method = RequestMethod.GET)
637
	public String getPaginatedPromoterInfo(HttpServletRequest request,
30426 tejbeer 638
			@RequestParam(name = "offset", defaultValue = "0") int offset,
639
			@RequestParam(name = "limit", defaultValue = "10") int limit,
640
			@RequestParam(name = "fofoId", required = false, defaultValue = "0") int fofoId, Model model)
24159 tejbeer 641
			throws ProfitMandiBusinessException {
642
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
643
		List<Promoter> promoterInfo = null;
27489 tejbeer 644
		if (fofoId != 0) {
645
			promoterInfo = promoterRepository.selectAllPromoterByFofoId(fofoId, offset, limit);
646
		} else {
647
			promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
648
		}
24159 tejbeer 649
		LOGGER.info("promoterInfo" + promoterInfo);
650
		if (!promoterInfo.isEmpty()) {
30945 tejbeer 651
			Set<Integer> partnerIds = new HashSet<>();
24159 tejbeer 652
 
653
			for (Promoter promoterdetail : promoterInfo) {
654
				partnerIds.add(promoterdetail.getRetailerId());
655
			}
656
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 657
			if (!partnerIds.equals(null)) {
24159 tejbeer 658
				LOGGER.info("partnerIds" + partnerIds);
30426 tejbeer 659
 
660
				Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
661
 
662
				partnersMap = partnerIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null)
663
						.collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
664
 
24159 tejbeer 665
			}
24349 amit.gupta 666
 
24159 tejbeer 667
			LOGGER.info("partnerIds" + partnersMap);
668
			model.addAttribute("promoterInfo", promoterInfo);
669
			model.addAttribute("partnersMap", partnersMap);
670
			model.addAttribute("url", "/getPaginatedPromoterInfo");
671
		} else {
672
			model.addAttribute("promoterInfo", promoterInfo);
673
 
674
		}
675
 
676
		return "promoter-info-paginated";
677
	}
678
 
679
	@RequestMapping(value = "/createPromoter", method = RequestMethod.POST)
680
	public String createPromoter(HttpServletRequest request, @RequestBody PromoterDetailModel promoterdetailModel,
30426 tejbeer 681
			Model model) throws Exception {
24159 tejbeer 682
		LOGGER.info("requested url : " + request.getRequestURL().toString());
683
		LOGGER.info("requested url : " + promoterdetailModel);
26131 tejbeer 684
		Promoter promoter = promoterRepository.selectById(promoterdetailModel.getId());
685
		if (promoter == null) {
686
			promoter = new Promoter();
24159 tejbeer 687
			promoter.setBrand(promoterdetailModel.getBrand());
688
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
689
			promoter.setCreatedTimestamp(LocalDateTime.now());
28996 amit.gupta 690
			promoter.setEmail(promoterdetailModel.getEmail());
691
			promoter.setName(promoterdetailModel.getName());
692
			promoter.setMobile(promoterdetailModel.getMobile());
693
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
694
			promoter.setStatus(promoterdetailModel.isStatus());
28908 tejbeer 695
			promoterRepository.persist(promoter);
26131 tejbeer 696
 
28908 tejbeer 697
			List<Promoter> promoters = promoterRepository.selectAllByRetailer(promoterdetailModel.getRetailerId());
698
			if (!promoters.isEmpty()) {
28970 tejbeer 699
				FofoStore fs = fofoStoreRepository.selectByRetailerId(promoterdetailModel.getRetailerId());
28908 tejbeer 700
				PartnerOnBoardingPanel pobp = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
701
				if (pobp != null) {
702
					storeTimelineTatService.promoterComplete(pobp.getId());
703
				}
704
			}
705
 
28996 amit.gupta 706
		} else {
707
			promoter.setEmail(promoterdetailModel.getEmail());
708
			promoter.setName(promoterdetailModel.getName());
709
			promoter.setMobile(promoterdetailModel.getMobile());
710
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
711
			promoter.setStatus(promoterdetailModel.isStatus());
24159 tejbeer 712
		}
27489 tejbeer 713
		LOGGER.info("sdport" + promoterdetailModel.isStatus());
24159 tejbeer 714
 
31238 amit.gupta 715
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
24159 tejbeer 716
 
717
		return "response";
718
 
719
	}
720
 
721
	@RequestMapping(value = "/removePromoterfromPartnerStore", method = RequestMethod.POST)
722
	public String removePromoterfromPartnerStore(HttpServletRequest request,
30426 tejbeer 723
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
24159 tejbeer 724
 
725
		Promoter promoter = promoterRepository.selectById(id);
726
 
26131 tejbeer 727
		promoter.setStatus(false);
24159 tejbeer 728
		promoterRepository.persist(promoter);
729
 
31238 amit.gupta 730
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
24159 tejbeer 731
 
732
		return "response";
733
	}
734
 
735
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
736
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
737
			throws Exception {
738
 
739
		Location location = new Location();
740
		location.setName(addLocationModel.getName());
741
		location.setLine1(addLocationModel.getLine1());
742
		location.setLine2(addLocationModel.getLine2());
743
		location.setCity(addLocationModel.getCity());
744
		location.setState(addLocationModel.getState());
745
		location.setPin(addLocationModel.getPin());
746
 
747
		LOGGER.info("PostLocation" + location);
748
		locationRepository.persist(location);
749
 
750
		User user = userRepository.selectById(addLocationModel.getUserId());
751
		user.setLocation(location.getId());
752
		userRepository.persist(user);
753
 
31238 amit.gupta 754
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
24159 tejbeer 755
		return "response";
756
 
757
	}
758
 
759
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
760
	public String updateLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel,
30426 tejbeer 761
			Model model) throws Exception {
24159 tejbeer 762
 
763
		Location userlocation = locationRepository.selectById(addLocationModel.getUserId());
764
 
765
		userlocation.setName(addLocationModel.getName());
766
		userlocation.setCity(addLocationModel.getCity());
767
		userlocation.setLine1(addLocationModel.getLine1());
768
		userlocation.setLine2(addLocationModel.getLine2());
769
		userlocation.setPin(addLocationModel.getPin());
770
		userlocation.setState(addLocationModel.getState());
771
 
772
		locationRepository.persist(userlocation);
773
 
31238 amit.gupta 774
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
24159 tejbeer 775
		return "response";
776
 
777
	}
778
 
27231 tejbeer 779
	@RequestMapping(value = "/getPartnerReadonlyInfo")
780
	public String getPartnerReadonlyInfo(HttpServletRequest request, Model model) throws Exception {
781
 
782
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
783
		AuthUser authUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
784
 
785
		Map<Integer, List<Integer>> pp = csService.getAuthUserIdPartnerIdMapping();
786
 
29633 amit.gupta 787
		Set<Integer> fofoIds = new HashSet<>(pp.get(authUser.getId()));
27231 tejbeer 788
 
30017 amit.gupta 789
		Map<Integer, CustomRetailer> customRetailersMap = fofoIds.stream().map(x -> {
29604 amit.gupta 790
			try {
791
				return retailerService.getFofoRetailer(x);
792
			} catch (ProfitMandiBusinessException e) {
793
				// TODO Auto-generated catch block
794
				return null;
795
			}
30017 amit.gupta 796
		}).filter(x -> x != null).collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
27231 tejbeer 797
 
798
		model.addAttribute("customRetailersMap", customRetailersMap);
27243 tejbeer 799
 
27231 tejbeer 800
		return "partner-readonly-info";
801
 
802
	}
803
 
28071 tejbeer 804
	@RequestMapping(value = "/getBlockBrandMapping")
805
	public String getBlockBrandMapping(HttpServletRequest request, Model model) throws Exception {
806
 
807
		int fofoId = Utils.SYSTEM_PARTNER_ID;
808
		Set<String> brands = null;
809
 
810
		brands = mongoClient.getMongoBrands(fofoId, null, 3).stream().map(x -> (String) x.get("name"))
811
				.collect(Collectors.toSet());
812
 
813
		LOGGER.info("brands" + brands);
814
		model.addAttribute("brands", brands);
815
 
816
		return "block-brand-mapping";
817
 
818
	}
819
 
820
	@RequestMapping(value = "/getBlockBrand")
821
	public String getBlockBrand(HttpServletRequest request,
30426 tejbeer 822
			@RequestParam(name = "brand", required = true, defaultValue = "0") String brand, Model model)
28071 tejbeer 823
			throws Exception {
824
 
825
		List<RetailerBlockBrands> retailerBlockBrands = retailerBlockBrandsRepository.selectAllByBrands(brand);
826
 
827
		List<Integer> blockBrandPartners = retailerBlockBrands.stream().map(x -> x.getFofoId())
828
				.collect(Collectors.toList());
829
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(true);
28272 tejbeer 830
 
28071 tejbeer 831
		LOGGER.info("retailerBlockBrands" + retailerBlockBrands);
832
		model.addAttribute("blockBrandPartners", blockBrandPartners);
833
		model.addAttribute("retailerBlockBrands", retailerBlockBrands);
834
		model.addAttribute("customRetailers", customRetailers);
835
 
836
		return "retailer-block-brand";
837
 
838
	}
839
 
840
	@RequestMapping(value = "/getRetailerBlockBrandMappping", method = RequestMethod.POST)
841
	public String getBlockBrandMappping(HttpServletRequest request,
30426 tejbeer 842
			@RequestParam(name = "brand", required = true, defaultValue = "0") String brand,
843
			@RequestParam(name = "fofoIds", required = true, defaultValue = "0") List<Integer> fofoIds, Model model)
28071 tejbeer 844
			throws Exception {
845
		List<RetailerBlockBrands> retailerBlockBrands = retailerBlockBrandsRepository.selectAllByBrands(brand);
846
		if (!retailerBlockBrands.isEmpty()) {
847
			retailerBlockBrandsRepository.deleteAllbyBrands(brand);
848
		}
849
 
850
		for (int id : fofoIds) {
851
			RetailerBlockBrands retailerBlockBrand = new RetailerBlockBrands();
852
			retailerBlockBrand.setFofoId(id);
853
			retailerBlockBrand.setBlockBrands(brand);
854
			retailerBlockBrandsRepository.persist(retailerBlockBrand);
855
 
856
		}
31238 amit.gupta 857
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
28071 tejbeer 858
 
859
		return "response";
860
	}
861
 
32731 ranu 862
	@Autowired
863
	BrandsService brandsService;
864
 
28272 tejbeer 865
	@RequestMapping(value = "/getBrandslimit", method = RequestMethod.GET)
866
	public String getBrandslimit(HttpServletRequest request, Model model) throws Exception {
867
 
868
		int fofoId = Utils.SYSTEM_PARTNER_ID;
869
 
32731 ranu 870
		List<String> brands = brandsService.getBrandsToDisplay(3).stream().map(x -> x.getName()).collect(Collectors.toList());
28272 tejbeer 871
		model.addAttribute("brands", brands);
872
 
873
		return "brands-limit";
874
 
875
	}
876
 
877
	@RequestMapping(value = "/setUpdateLimit", method = RequestMethod.POST)
878
	public String getUpdateLimit(HttpServletRequest request,
30028 manish 879
			@RequestParam(name = "brand", required = true, defaultValue = "0") String brand,
880
			@RequestParam(name = "fofoId", required = true, defaultValue = "0") int fofoId,
881
			@RequestParam(name = "limit", required = true, defaultValue = "0") float limit,
882
			@RequestParam(name = "minStockLimit", required = true, defaultValue = "0") float minStockLimit,
883
			@RequestParam(name = "preLimit", required = true, defaultValue = "0") float preLimit,
884
			@RequestParam(name = "preStockLimit", required = true, defaultValue = "0") float preStockLimit, Model model)
28272 tejbeer 885
			throws Exception {
886
 
887
		CustomRetailer cr = retailerService.getFofoRetailer(fofoId);
28277 tejbeer 888
 
889
		Map.Entry<Integer, CustomRetailer> customRetailers = new AbstractMap.SimpleEntry(cr.getPartnerId(), cr);
890
 
891
		// Map<Integer, CustomRetailer> customRetailers =
892
		// retailerService.getFofoRetailers(true);
30028 manish 893
 
28272 tejbeer 894
		RetailerBrandsLimit retailerBrands = retailerBrandsLimitRepository.selectLimitByBrandAndFofoId(fofoId, brand);
28277 tejbeer 895
 
28272 tejbeer 896
		if (retailerBrands == null) {
897
			retailerBrands = new RetailerBrandsLimit();
32736 ranu 898
			retailerBrands.setMinStockLimit(minStockLimit);
899
			retailerBrands.setBrandLimit(limit);
28272 tejbeer 900
			retailerBrands.setIsUpdate(1);
901
			retailerBrands.setCreatedTimestamp(LocalDateTime.now());
902
			retailerBrands.setPartnerId(fofoId);
903
			retailerBrands.setBrandName(brand);
904
			retailerBrands.setUpdatedTimestamp(LocalDateTime.now());
905
			retailerBrandsLimitRepository.persist(retailerBrands);
30028 manish 906
		} else {
32736 ranu 907
			retailerBrands.setMinStockLimit(minStockLimit);
908
			retailerBrands.setBrandLimit(limit);
28272 tejbeer 909
			retailerBrands.setIsUpdate(1);
910
			retailerBrands.setUpdatedTimestamp(LocalDateTime.now());
911
			retailerBrandsLimitRepository.persist(retailerBrands);
912
 
913
		}
914
		retailerBrands = retailerBrandsLimitRepository.selectLimitByBrandAndFofoId(fofoId, brand);
915
 
916
		Map<Integer, RetailerBrandsLimit> retailerBrandsLimitMap = new HashMap<>();
917
 
918
		retailerBrandsLimitMap.put(retailerBrands.getPartnerId(), retailerBrands);
919
 
920
		BrandLimit brandLimit = brandLimitRepository.setLimitByBrands(brand);
921
 
922
		model.addAttribute("retailerBrandsLimitMap", retailerBrandsLimitMap);
923
		model.addAttribute("brandLimit", brandLimit);
924
		model.addAttribute("customRetailers", customRetailers);
28277 tejbeer 925
 
926
		model.addAttribute("brand", brand);
28272 tejbeer 927
		return "brands-limit-row-mapping";
928
 
929
	}
930
 
931
	@RequestMapping(value = "/getRetailerBrandslimitMapping", method = RequestMethod.GET)
932
	public String getRetailerBrandslimit(HttpServletRequest request,
30426 tejbeer 933
			@RequestParam(name = "brand", required = true, defaultValue = "0") String brand, Model model)
28272 tejbeer 934
			throws Exception {
935
 
936
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(true);
32751 amit.gupta 937
		List<RetailerBrandsLimit> retailerBrandsLimit = retailerBrandsLimitRepository.setAllLimitByBrand(brand);
28283 tejbeer 938
		Map<Integer, RetailerBrandsLimit> retailerBrandsLimitMap = retailerBrandsLimit.stream()
939
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
28272 tejbeer 940
 
941
		model.addAttribute("customRetailers", customRetailers);
942
		model.addAttribute("brand", brand);
28283 tejbeer 943
		model.addAttribute("retailerBrandsLimitMap", retailerBrandsLimitMap);
28272 tejbeer 944
 
945
		BrandLimit brandLimit = brandLimitRepository.setLimitByBrands(brand);
946
 
947
		model.addAttribute("brandLimit", brandLimit);
948
 
949
		return "brands-limit-mapping";
950
 
951
	}
30426 tejbeer 952
 
28272 tejbeer 953
	@RequestMapping(value = "/setBrandWiseLimit", method = RequestMethod.POST)
954
	public String brandwiseLimit(HttpServletRequest request,
30028 manish 955
			@RequestParam(name = "brands", required = true, defaultValue = "") String brands,
956
			@RequestParam(name = "limit", required = true, defaultValue = "") float limit, Model model)
28272 tejbeer 957
			throws Exception {
958
 
959
		LOGGER.info("limit" + limit);
960
 
961
		BrandLimit brandLimit = brandLimitRepository.setLimitByBrands(brands);
962
 
32751 amit.gupta 963
		List<RetailerBrandsLimit> retailerBrandsLimit = retailerBrandsLimitRepository.setAllLimitByBrand(brands);
28272 tejbeer 964
 
965
		Map<Integer, RetailerBrandsLimit> retailerBrandsLimitMap = retailerBrandsLimit.stream()
966
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
967
 
968
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(true);
969
 
970
		if (brandLimit == null) {
971
 
972
			brandLimit = new BrandLimit();
973
			brandLimit.setBrandLimit(limit);
974
			brandLimit.setBrandName(brands);
975
			brandLimit.setCreatedTimestamp(LocalDateTime.now());
976
			brandLimit.setUpdatedTimestamp(LocalDateTime.now());
977
			brandLimitRepository.persist(brandLimit);
978
 
30028 manish 979
		}
980
 
981
		else {
28272 tejbeer 982
			brandLimit.setBrandLimit(limit);
983
 
984
			brandLimit.setUpdatedTimestamp(LocalDateTime.now());
985
			brandLimitRepository.persist(brandLimit);
986
 
987
		}
988
		brandLimit = brandLimitRepository.setLimitByBrands(brands);
989
 
990
		LOGGER.info("brandLimit" + brandLimit);
28277 tejbeer 991
		model.addAttribute("brand", brands);
28272 tejbeer 992
		model.addAttribute("brandLimit", brandLimit);
993
		model.addAttribute("customRetailers", customRetailers);
994
		model.addAttribute("retailerBrandsLimitMap", retailerBrandsLimitMap);
995
		return "brands-limit-mapping";
996
 
997
	}
28825 tejbeer 998
 
30028 manish 999
	@RequestMapping(value = "/setBrandWiseStockLimit", method = RequestMethod.POST)
1000
	public String setBrandWiseStockLimit(HttpServletRequest request,
1001
			@RequestParam(name = "brands", required = true, defaultValue = "") String brands,
1002
			@RequestParam(name = "stockLimit", required = true, defaultValue = "") float stockLimit, Model model)
1003
			throws Exception {
1004
 
1005
		LOGGER.info("limit" + stockLimit);
1006
 
1007
		BrandLimit brandLimit = brandLimitRepository.setLimitByBrands(brands);
1008
 
32751 amit.gupta 1009
		List<RetailerBrandsLimit> retailerBrandsLimit = retailerBrandsLimitRepository.setAllLimitByBrand(brands);
30028 manish 1010
 
1011
		Map<Integer, RetailerBrandsLimit> retailerBrandsLimitMap = retailerBrandsLimit.stream()
1012
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
1013
 
1014
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(true);
1015
 
1016
		if (brandLimit == null) {
1017
 
1018
			brandLimit = new BrandLimit();
1019
			brandLimit.setBrandLimit(0);
1020
			brandLimit.setBrandName(brands);
1021
			brandLimit.setStockLimit(stockLimit);
1022
			brandLimit.setCreatedTimestamp(LocalDateTime.now());
1023
			brandLimit.setUpdatedTimestamp(LocalDateTime.now());
1024
			brandLimitRepository.persist(brandLimit);
1025
 
1026
		}
1027
 
1028
		else {
1029
			brandLimit.setStockLimit(stockLimit);
1030
 
1031
			brandLimit.setUpdatedTimestamp(LocalDateTime.now());
1032
			brandLimitRepository.persist(brandLimit);
1033
 
1034
		}
1035
		brandLimit = brandLimitRepository.setLimitByBrands(brands);
1036
 
1037
		model.addAttribute("brand", brands);
1038
		model.addAttribute("brandLimit", brandLimit);
1039
		model.addAttribute("customRetailers", customRetailers);
1040
		model.addAttribute("retailerBrandsLimitMap", retailerBrandsLimitMap);
1041
		return "brands-limit-mapping";
1042
 
1043
	}
1044
 
28381 tejbeer 1045
	@RequestMapping(value = "/setPartnerPincode", method = RequestMethod.POST)
1046
	public String setPartnerPincode(HttpServletRequest request,
30426 tejbeer 1047
			@RequestParam(name = "pincode", required = true, defaultValue = "") String pincode,
1048
			@RequestParam(name = "fofoId", required = true, defaultValue = "") int fofoId, Model model)
28825 tejbeer 1049
			throws Exception {
28272 tejbeer 1050
 
28825 tejbeer 1051
		if (!pincode.equals("")) {
1052
			PincodePartner pinPartner = new PincodePartner();
1053
			pinPartner.setFofoId(fofoId);
1054
			pinPartner.setPincode(pincode);
32751 amit.gupta 1055
			pincodePartnerRepository.perist(pinPartner);
28381 tejbeer 1056
		}
1057
 
32751 amit.gupta 1058
		List<PincodePartner> partnerPincodeList = pincodePartnerRepository.selectByPartnerId(fofoId);
28825 tejbeer 1059
 
1060
		LOGGER.info("partnerPincodeList" + partnerPincodeList);
1061
 
1062
		model.addAttribute("partnerPincodeList", partnerPincodeList);
28381 tejbeer 1063
		return "pincode-partner-index";
1064
 
1065
	}
28825 tejbeer 1066
 
28381 tejbeer 1067
	@RequestMapping(value = "/removePartnerPincode", method = RequestMethod.POST)
28825 tejbeer 1068
	public String removePartnerPincode(HttpServletRequest request, @RequestParam int id,
30426 tejbeer 1069
			@RequestParam(name = "fofoId", required = true, defaultValue = "") int fofoId, Model model)
28825 tejbeer 1070
			throws Exception {
28381 tejbeer 1071
 
32751 amit.gupta 1072
		pincodePartnerRepository.delete(id);
28381 tejbeer 1073
 
32751 amit.gupta 1074
		List<PincodePartner> partnerPincodeList = pincodePartnerRepository.selectByPartnerId(fofoId);
28825 tejbeer 1075
 
1076
		LOGGER.info("partnerPincodeList" + partnerPincodeList);
1077
 
1078
		model.addAttribute("partnerPincodeList", partnerPincodeList);
1079
 
28381 tejbeer 1080
		return "pincode-partner-index";
1081
 
1082
	}
28825 tejbeer 1083
 
28381 tejbeer 1084
	@RequestMapping(value = "/removeSearchPincode", method = RequestMethod.POST)
28825 tejbeer 1085
	public String removeSearchPincode(HttpServletRequest request, @RequestParam int id,
30426 tejbeer 1086
			@RequestParam(name = "pin", required = true, defaultValue = "") String pin,
1087
			@RequestParam(name = "fofoId", required = true, defaultValue = "") int fofoId, Model model)
28825 tejbeer 1088
			throws Exception {
28381 tejbeer 1089
 
32751 amit.gupta 1090
		pincodePartnerRepository.delete(id);
28381 tejbeer 1091
 
32751 amit.gupta 1092
		List<PincodePartner> partnerPincodeList = pincodePartnerRepository.selectPartnersByPincode(pin);
28381 tejbeer 1093
 
28825 tejbeer 1094
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getFofoRetailers(true);
1095
 
1096
		model.addAttribute("customRetailerMap", customRetailerMap);
1097
 
1098
		model.addAttribute("partnerPincodeList", partnerPincodeList);
1099
 
28381 tejbeer 1100
		return "search-pincode-partner";
1101
 
1102
	}
28825 tejbeer 1103
 
28381 tejbeer 1104
	@RequestMapping(value = "/searchPincodePartner", method = RequestMethod.GET)
1105
	public String searchPincodePartner(HttpServletRequest request,
30426 tejbeer 1106
			@RequestParam(name = "pincode", required = true, defaultValue = "") String pincode, Model model)
28825 tejbeer 1107
			throws Exception {
28381 tejbeer 1108
 
32751 amit.gupta 1109
		List<PincodePartner> partnerPincodeList = pincodePartnerRepository.selectPartnersByPincode(pincode);
28825 tejbeer 1110
 
28381 tejbeer 1111
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getFofoRetailers(true);
1112
 
1113
		model.addAttribute("customRetailerMap", customRetailerMap);
28825 tejbeer 1114
 
28381 tejbeer 1115
		model.addAttribute("partnerPincodeList", partnerPincodeList);
28825 tejbeer 1116
 
28381 tejbeer 1117
		return "search-pincode-partner";
28825 tejbeer 1118
 
28381 tejbeer 1119
	}
28825 tejbeer 1120
 
28381 tejbeer 1121
	@RequestMapping(value = "/showPartnerPincode", method = RequestMethod.GET)
1122
	public String showPartnerPincode(HttpServletRequest request,
30426 tejbeer 1123
			@RequestParam(name = "partnerId", required = true, defaultValue = "") int partnerId, Model model)
28825 tejbeer 1124
			throws Exception {
28381 tejbeer 1125
 
32751 amit.gupta 1126
		List<PincodePartner> partnerPincodeList = pincodePartnerRepository.selectByPartnerId(partnerId);
28825 tejbeer 1127
 
28381 tejbeer 1128
		model.addAttribute("partnerPincodeList", partnerPincodeList);
28825 tejbeer 1129
 
28381 tejbeer 1130
		return "pincode-partner-index";
28825 tejbeer 1131
 
28381 tejbeer 1132
	}
28825 tejbeer 1133
 
28381 tejbeer 1134
	@RequestMapping(value = "/getAllPartnerPincode", method = RequestMethod.GET)
28825 tejbeer 1135
	public String getAllPartnerPincode(HttpServletRequest request, Model model) throws Exception {
28381 tejbeer 1136
		return "add-partner-pincode";
1137
	}
32629 amit.gupta 1138
 
32667 raveendra. 1139
 
28272 tejbeer 1140
}