Subversion Repositories SmartDukaan

Rev

Rev 28024 | Rev 28272 | 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 java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
24349 amit.gupta 6
import java.time.LocalDate;
24159 tejbeer 7
import java.time.LocalDateTime;
24124 govind 8
import java.util.ArrayList;
25276 amit.gupta 9
import java.util.Arrays;
24349 amit.gupta 10
import java.util.HashMap;
24124 govind 11
import java.util.List;
22981 ashik.ali 12
import java.util.Map;
24986 tejbeer 13
import java.util.Set;
24159 tejbeer 14
import java.util.stream.Collectors;
22981 ashik.ali 15
 
16
import javax.servlet.http.HttpServletRequest;
25276 amit.gupta 17
import javax.transaction.Transactional;
22981 ashik.ali 18
 
24159 tejbeer 19
import org.apache.logging.log4j.LogManager;
23955 govind 20
import org.apache.logging.log4j.Logger;
24159 tejbeer 21
import org.json.JSONObject;
22981 ashik.ali 22
import org.springframework.beans.factory.annotation.Autowired;
24159 tejbeer 23
import org.springframework.beans.factory.annotation.Qualifier;
23494 ashik.ali 24
import org.springframework.core.io.InputStreamResource;
25
import org.springframework.http.HttpHeaders;
26
import org.springframework.http.HttpStatus;
23330 ashik.ali 27
import org.springframework.http.ResponseEntity;
22981 ashik.ali 28
import org.springframework.stereotype.Controller;
29
import org.springframework.ui.Model;
24124 govind 30
import org.springframework.web.bind.annotation.GetMapping;
31
import org.springframework.web.bind.annotation.PostMapping;
23026 ashik.ali 32
import org.springframework.web.bind.annotation.RequestBody;
22981 ashik.ali 33
import org.springframework.web.bind.annotation.RequestMapping;
34
import org.springframework.web.bind.annotation.RequestMethod;
35
import org.springframework.web.bind.annotation.RequestParam;
36
 
23494 ashik.ali 37
import com.spice.profitmandi.common.enumuration.ContentType;
23955 govind 38
import com.spice.profitmandi.common.enumuration.CounterSize;
22981 ashik.ali 39
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24123 tejbeer 40
import com.spice.profitmandi.common.model.AddLocationModel;
24124 govind 41
import com.spice.profitmandi.common.model.CustomRetailer;
22981 ashik.ali 42
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24159 tejbeer 43
import com.spice.profitmandi.common.model.PromoterDetailModel;
23026 ashik.ali 44
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
28024 tejbeer 45
import com.spice.profitmandi.common.util.Utils;
23330 ashik.ali 46
import com.spice.profitmandi.common.web.util.ResponseSender;
27231 tejbeer 47
import com.spice.profitmandi.dao.entity.auth.AuthUser;
27797 tejbeer 48
import com.spice.profitmandi.dao.entity.cs.PartnerPosition;
49
import com.spice.profitmandi.dao.entity.cs.Position;
23494 ashik.ali 50
import com.spice.profitmandi.dao.entity.dtr.Document;
51
import com.spice.profitmandi.dao.entity.dtr.Retailer;
28024 tejbeer 52
import com.spice.profitmandi.dao.entity.dtr.RetailerBlockBrands;
23494 ashik.ali 53
import com.spice.profitmandi.dao.entity.dtr.Shop;
24124 govind 54
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
27797 tejbeer 55
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
56
import com.spice.profitmandi.dao.entity.fofo.PartnerTypeChange;
24159 tejbeer 57
import com.spice.profitmandi.dao.entity.user.Location;
58
import com.spice.profitmandi.dao.entity.user.Promoter;
24123 tejbeer 59
import com.spice.profitmandi.dao.entity.user.User;
27231 tejbeer 60
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
61
import com.spice.profitmandi.dao.repository.cs.CsService;
23494 ashik.ali 62
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
24124 govind 63
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24159 tejbeer 64
import com.spice.profitmandi.dao.repository.dtr.Mongo;
28024 tejbeer 65
import com.spice.profitmandi.dao.repository.dtr.RetailerBlockBrandsRepository;
23494 ashik.ali 66
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
67
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
27797 tejbeer 68
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeRepository;
69
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
24123 tejbeer 70
import com.spice.profitmandi.dao.repository.user.LocationRepository;
24159 tejbeer 71
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
24123 tejbeer 72
import com.spice.profitmandi.dao.repository.user.UserRepository;
24349 amit.gupta 73
import com.spice.profitmandi.service.PartnerInvestmentService;
24159 tejbeer 74
import com.spice.profitmandi.service.inventory.InventoryService;
22981 ashik.ali 75
import com.spice.profitmandi.service.user.RetailerService;
25276 amit.gupta 76
import com.spice.profitmandi.web.model.LoginDetails;
77
import com.spice.profitmandi.web.util.CookiesProcessor;
24123 tejbeer 78
import com.spice.profitmandi.web.util.MVCResponseSender;
22981 ashik.ali 79
 
80
@Controller
25277 amit.gupta 81
@Transactional(rollbackOn = Throwable.class)
22981 ashik.ali 82
public class RetailerController {
83
 
23568 govind 84
	private static final Logger LOGGER = LogManager.getLogger(RetailerController.class);
24124 govind 85
 
22981 ashik.ali 86
	@Autowired
87
	private RetailerService retailerService;
24124 govind 88
 
23330 ashik.ali 89
	@Autowired
23494 ashik.ali 90
	private RetailerRepository retailerRepository;
24124 govind 91
 
23494 ashik.ali 92
	@Autowired
93
	private ShopRepository shopRepository;
24124 govind 94
 
23494 ashik.ali 95
	@Autowired
28024 tejbeer 96
	private RetailerBlockBrandsRepository retailerBlockBrandsRepository;
97
 
98
	@Autowired
24124 govind 99
	private FofoStoreRepository fofoStoreRepository;
100
 
101
	@Autowired
23494 ashik.ali 102
	private DocumentRepository documentRepository;
24124 govind 103
 
23494 ashik.ali 104
	@Autowired
25276 amit.gupta 105
	private CookiesProcessor cookiesProcessor;
106
 
107
	@Autowired
24159 tejbeer 108
	@Qualifier("userUserRepository")
24123 tejbeer 109
	private UserRepository userRepository;
24124 govind 110
 
24123 tejbeer 111
	@Autowired
27231 tejbeer 112
	private AuthRepository authRepository;
113
 
114
	@Autowired
115
	private CsService csService;
116
 
117
	@Autowired
24123 tejbeer 118
	private LocationRepository locationRepository;
24124 govind 119
 
24123 tejbeer 120
	@Autowired
23330 ashik.ali 121
	private ResponseSender<?> responseSender;
24124 govind 122
 
24123 tejbeer 123
	@Autowired
124
	private MVCResponseSender mvcResponseSender;
24124 govind 125
 
24159 tejbeer 126
	@Autowired
127
	private InventoryService inventoryService;
128
 
129
	@Autowired
130
	private PromoterRepository promoterRepository;
131
 
132
	@Autowired
24349 amit.gupta 133
	private PartnerInvestmentService partnerInvestmentService;
134
 
135
	@Autowired
24159 tejbeer 136
	private Mongo mongoClient;
137
 
27797 tejbeer 138
	@Autowired
139
	private PartnerTypeChangeService partnerTypeChangeService;
140
 
141
	@Autowired
142
	private PartnerTypeChangeRepository partnerTypeChangeRepository;
143
 
23784 ashik.ali 144
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.GET)
24124 govind 145
	public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request,
146
			@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
147
			Model model) throws ProfitMandiBusinessException {
148
		LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(),
149
				emailIdOrMobileNumber);
28071 tejbeer 150
 
151
		Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
152
 
26771 amit.gupta 153
		model.addAllAttributes(map);
28071 tejbeer 154
 
23955 govind 155
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 156
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
157
		LOGGER.info("warehouseMap", warehouseMap);
158
		model.addAttribute("warehouses", warehouseMap);
26771 amit.gupta 159
		if (map.containsKey("retailer")) {
160
			Retailer retailer = (Retailer) map.get("retailer");
161
			User user = userRepository.selectById(retailer.getId());
162
			if (user.getLocation() != null) {
163
				Location location = locationRepository.selectById(user.getLocation());
164
				model.addAttribute("locationdetail", location);
165
				LOGGER.info("location" + location);
166
			}
167
		}
22981 ashik.ali 168
		return "retailer-details";
169
	}
24124 govind 170
 
23026 ashik.ali 171
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 172
	public String updateRetailerDetails(HttpServletRequest request,
173
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 174
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
175
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
28071 tejbeer 176
 
23026 ashik.ali 177
		model.addAllAttributes(map);
23955 govind 178
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 179
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
180
		LOGGER.info("warehouseMap", warehouseMap);
181
		model.addAttribute("warehouses", warehouseMap);
23026 ashik.ali 182
		return "retailer-details";
183
	}
22981 ashik.ali 184
 
185
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 186
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 187
		return "retailer-info";
188
	}
24124 govind 189
 
23330 ashik.ali 190
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 191
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 192
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
193
	}
24124 govind 194
 
23494 ashik.ali 195
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 196
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
197
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
198
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 199
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 200
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 201
 
202
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 203
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
204
		}
24124 govind 205
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 206
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
207
		}
208
		return responseSender.ok(document);
209
	}
24124 govind 210
 
23494 ashik.ali 211
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 212
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
213
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
214
			throws ProfitMandiBusinessException {
215
 
23499 ashik.ali 216
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 217
 
218
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 219
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
220
		}
24124 govind 221
 
23494 ashik.ali 222
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 223
 
23494 ashik.ali 224
		FileInputStream file = null;
225
		try {
226
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
227
		} catch (FileNotFoundException e) {
228
			LOGGER.error("Retailer Document file not found : ", e);
229
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
230
		}
24159 tejbeer 231
		// ByteArrayOutputStream byteArrayOutputStream = new
232
		// ByteArrayOutputStream();
24124 govind 233
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
234
 
235
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 236
		String contentType = "";
24124 govind 237
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 238
			contentType = "image/jpeg";
24124 govind 239
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 240
			contentType = "image/png";
24124 govind 241
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 242
			contentType = "application/pdf";
243
		}
24124 govind 244
		headers.set("Content-Type", contentType);
245
		headers.set("Content-disposition", "inline; filename=" + document.getName());
246
		headers.setContentLength(document.getSize());
247
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
248
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
249
 
24159 tejbeer 250
		// return
251
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 252
	}
24124 govind 253
 
23494 ashik.ali 254
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 255
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
256
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
257
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 258
		Shop shop = shopRepository.selectById(shopId);
24124 govind 259
 
260
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 261
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
262
		}
24124 govind 263
 
264
		if (shop.getDocumentId() == null) {
23494 ashik.ali 265
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
266
		}
24124 govind 267
 
23494 ashik.ali 268
		Document document = documentRepository.selectById(shop.getDocumentId());
269
		return responseSender.ok(document);
270
	}
24124 govind 271
 
23494 ashik.ali 272
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 273
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
274
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
275
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
276
			throws ProfitMandiBusinessException {
277
 
23494 ashik.ali 278
		Shop shop = shopRepository.selectById(shopId);
24124 govind 279
 
280
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 281
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
282
		}
24124 govind 283
 
284
		if (shop.getDocumentId() == null) {
23494 ashik.ali 285
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
286
		}
24124 govind 287
 
23494 ashik.ali 288
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 289
 
23494 ashik.ali 290
		FileInputStream file = null;
291
		try {
292
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
293
		} catch (FileNotFoundException e) {
294
			LOGGER.error("Retailer Document file not found : ", e);
295
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
296
		}
24159 tejbeer 297
		// ByteArrayOutputStream byteArrayOutputStream = new
298
		// ByteArrayOutputStream();
24124 govind 299
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
300
 
301
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 302
		String contentType = "";
24124 govind 303
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 304
			contentType = "image/jpeg";
24124 govind 305
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 306
			contentType = "image/png";
24124 govind 307
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 308
			contentType = "application/pdf";
309
		}
24124 govind 310
		headers.set("Content-Type", contentType);
311
		headers.set("Content-disposition", "inline; filename=" + document.getName());
312
		headers.setContentLength(document.getSize());
313
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
314
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
315
 
24159 tejbeer 316
		// return
317
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 318
	}
24124 govind 319
 
320
	@GetMapping(value = "/getAllStores")
321
	public String getAllStores(HttpServletRequest request, Model model) {
322
 
25276 amit.gupta 323
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
324
				.collect(Collectors.toList());
24349 amit.gupta 325
		Map<Integer, Boolean> investments = new HashMap<>();
24124 govind 326
 
24349 amit.gupta 327
		for (FofoStore fofoStore : fofoStores) {
328
			boolean isOk = false;
329
			if (fofoStore.getGraceDate() != null && fofoStore.getGraceDate().isAfter(LocalDate.now())) {
330
				isOk = true;
331
			} else {
332
				try {
25276 amit.gupta 333
					isOk = partnerInvestmentService.isInvestmentOk(fofoStore.getId(), 10,
334
							ProfitMandiConstants.CUTOFF_INVESTMENT);
335
					if (fofoStore.getGraceCount() > 0) {
24349 amit.gupta 336
						fofoStore.setGraceDate(null);
337
						fofoStore.setGraceCount(0);
338
						fofoStoreRepository.persist(fofoStore);
339
					}
340
				} catch (ProfitMandiBusinessException e) {
341
				}
342
			}
343
			investments.put(fofoStore.getId(), isOk);
344
		}
24124 govind 345
 
26963 amit.gupta 346
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(false);
24124 govind 347
		model.addAttribute("fofoStores", fofoStores);
24349 amit.gupta 348
		model.addAttribute("investments", investments);
24124 govind 349
		model.addAttribute("customRetailers", customRetailers);
350
 
351
		return "store";
352
 
353
	}
354
 
27797 tejbeer 355
	@GetMapping(value = "/getPartnerCategory")
356
	public String getPartnerCategory(HttpServletRequest request, @RequestParam int fofoId, Model model) {
357
 
358
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
359
		List<PartnerType> partnerTypes = partnerType.nextPartnerTypes();
360
		LOGGER.info("partnerType" + partnerTypes);
361
		model.addAttribute("partnerTypes", partnerTypes);
362
		model.addAttribute("fofoId", fofoId);
363
		return "partner-category";
364
	}
365
 
366
	@PostMapping(value = "/updatePartnerCategory")
367
	public String updatePartnerCategory(HttpServletRequest request, @RequestParam(name = "fofoId") int fofoId,
368
			@RequestParam(name = "partnerType") PartnerType partnerType, Model model) throws Exception {
369
 
370
		PartnerTypeChange ptc = new PartnerTypeChange();
371
		ptc.setFofoId(fofoId);
372
		ptc.setPartnerType(partnerType);
373
		ptc.setCreateTimestamp(LocalDate.now().atStartOfDay().plusDays(1));
374
		partnerTypeChangeRepository.persist(ptc);
375
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
376
 
377
		return "response";
378
	}
379
 
24349 amit.gupta 380
	@GetMapping(value = "/partners")
381
	public String getStoreName(HttpServletRequest request, Model model, @RequestParam String query) throws Exception {
382
		model.addAttribute("response", mvcResponseSender
26963 amit.gupta 383
				.createResponseString(retailerService.getFofoRetailers(false).values().stream().filter(x -> {
24349 amit.gupta 384
					return x.getDisplayName().toLowerCase().matches(".*?" + query.toLowerCase() + ".*?");
385
				}).collect(Collectors.toList())));
386
		return "response";
387
	}
388
 
24124 govind 389
	@PostMapping(value = "/deactivateStore")
390
	public String deActivateStore(HttpServletRequest request,
391
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
392
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 393
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
27231 tejbeer 394
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "amit.gupta@smartdukaan.com")
26131 tejbeer 395
				.contains(loginDetails.getEmailId())) {
396
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
397
					"You are not authorise to deactivate retailer");
25276 amit.gupta 398
		}
24843 govind 399
		fofoStore.setActive(false);
400
		fofoStoreRepository.persist(fofoStore);
24124 govind 401
		LOGGER.info("inserted into InActiveFofoStore successfully");
402
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
403
 
404
		return "response";
405
 
406
	}
25276 amit.gupta 407
 
408
	@GetMapping(value = "/getAllInactiveStores")
409
	public String getInactiveStores(HttpServletRequest request, Model model) {
410
		List<FofoStore> inActiveFofoStores = fofoStoreRepository.selectInActiveStore();
411
		Map<Integer, CustomRetailer> customRetailers = retailerService
412
				.getFofoRetailers(inActiveFofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
24680 govind 413
		model.addAttribute("inActiveFofoStores", inActiveFofoStores);
414
		model.addAttribute("customRetailers", customRetailers);
415
		return "inactive-stores";
416
	}
25276 amit.gupta 417
 
418
	// Extend billing for 2 days
24349 amit.gupta 419
	@PostMapping(value = "/extendBilling")
420
	public String extendBilling(HttpServletRequest request,
421
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
422
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 423
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
27231 tejbeer 424
		if (Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "prakash.rai@smartdukaan.com",
425
				"amit.gupta@smartdukaan.com").contains(loginDetails.getEmailId())) {
25276 amit.gupta 426
			fofoStore.setGraceDate(LocalDate.now().plusDays(2));
427
			fofoStore.setGraceCount(fofoStore.getGraceCount() + 1);
428
			fofoStoreRepository.persist(fofoStore);
429
			model.addAttribute("response", mvcResponseSender.createResponseString(fofoStore.getGraceCount()));
430
		} else {
26131 tejbeer 431
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
432
					"You are not authorise to extend billing");
25276 amit.gupta 433
		}
24349 amit.gupta 434
		return "response";
25276 amit.gupta 435
 
24124 govind 436
	}
437
 
24159 tejbeer 438
	@RequestMapping(value = "/getPromoterInfo", method = RequestMethod.GET)
439
	public String getPromoterInfo(HttpServletRequest request,
440
			@RequestParam(name = "offset", defaultValue = "0") int offset,
27489 tejbeer 441
			@RequestParam(name = "limit", defaultValue = "10") int limit,
442
			@RequestParam(name = "fofoId", required = false, defaultValue = "0") int fofoId, Model model)
443
			throws Exception {
24159 tejbeer 444
		LOGGER.info("requested url : " + request.getRequestURL().toString());
445
		List<Promoter> promoterInfo = null;
446
 
447
		long size = 0;
27489 tejbeer 448
 
24159 tejbeer 449
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
450
				.collect(Collectors.toList());
451
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
452
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
24986 tejbeer 453
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
454
		brands.add("Airtel");
455
		brands.add("Vodafone");
456
		brands.add("Idea");
25276 amit.gupta 457
		LOGGER.info("brands" + brands);
24159 tejbeer 458
		model.addAttribute("customRetailers", customRetailers);
25276 amit.gupta 459
		model.addAttribute("brands", brands);
24159 tejbeer 460
 
27489 tejbeer 461
		if (fofoId != 0) {
462
			promoterInfo = promoterRepository.selectAllPromoterByFofoId(fofoId, offset, limit);
24159 tejbeer 463
 
27489 tejbeer 464
			size = promoterRepository.selectPromoterCount(fofoId);
465
 
466
		} else {
467
			promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
468
 
469
			size = promoterRepository.selectAllCount();
470
 
471
		}
24159 tejbeer 472
		if (!promoterInfo.isEmpty()) {
473
			List<Integer> partnerIds = new ArrayList<>();
474
 
475
			for (Promoter promoterdetail : promoterInfo) {
476
				partnerIds.add(promoterdetail.getRetailerId());
477
			}
478
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 479
			if (!partnerIds.equals(null)) {
24159 tejbeer 480
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 481
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 482
			}
24349 amit.gupta 483
 
24159 tejbeer 484
			LOGGER.info("partnerIds" + partnersMap);
485
			model.addAttribute("promoterInfo", promoterInfo);
486
			model.addAttribute("partnersMap", partnersMap);
487
			model.addAttribute("start", offset + 1);
488
			model.addAttribute("size", size);
489
			model.addAttribute("url", "/getPaginatedPromoterInfo");
490
 
491
			if (promoterInfo.size() < limit) {
492
				model.addAttribute("end", offset + promoterInfo.size());
493
			} else {
494
				model.addAttribute("end", offset + limit);
495
			}
496
		} else {
497
			model.addAttribute("promoterInfo", promoterInfo);
498
			model.addAttribute("size", size);
499
		}
500
		return "promoter-info";
501
 
502
	}
24349 amit.gupta 503
 
24159 tejbeer 504
	@RequestMapping(value = "/getPaginatedPromoterInfo", method = RequestMethod.GET)
505
	public String getPaginatedPromoterInfo(HttpServletRequest request,
506
			@RequestParam(name = "offset", defaultValue = "0") int offset,
27489 tejbeer 507
			@RequestParam(name = "limit", defaultValue = "10") int limit,
508
			@RequestParam(name = "fofoId", required = false, defaultValue = "0") int fofoId, Model model)
24159 tejbeer 509
			throws ProfitMandiBusinessException {
510
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
511
		List<Promoter> promoterInfo = null;
27489 tejbeer 512
		if (fofoId != 0) {
513
			promoterInfo = promoterRepository.selectAllPromoterByFofoId(fofoId, offset, limit);
514
		} else {
515
			promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
516
		}
24159 tejbeer 517
		LOGGER.info("promoterInfo" + promoterInfo);
518
		if (!promoterInfo.isEmpty()) {
519
			List<Integer> partnerIds = new ArrayList<>();
520
 
521
			for (Promoter promoterdetail : promoterInfo) {
522
				partnerIds.add(promoterdetail.getRetailerId());
523
			}
524
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 525
			if (!partnerIds.equals(null)) {
24159 tejbeer 526
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 527
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 528
			}
24349 amit.gupta 529
 
24159 tejbeer 530
			LOGGER.info("partnerIds" + partnersMap);
531
			model.addAttribute("promoterInfo", promoterInfo);
532
			model.addAttribute("partnersMap", partnersMap);
533
			model.addAttribute("url", "/getPaginatedPromoterInfo");
534
		} else {
535
			model.addAttribute("promoterInfo", promoterInfo);
536
 
537
		}
538
 
539
		return "promoter-info-paginated";
540
	}
541
 
542
	@RequestMapping(value = "/createPromoter", method = RequestMethod.POST)
543
	public String createPromoter(HttpServletRequest request, @RequestBody PromoterDetailModel promoterdetailModel,
544
			Model model) throws Exception {
545
		LOGGER.info("requested url : " + request.getRequestURL().toString());
546
		LOGGER.info("requested url : " + promoterdetailModel);
26131 tejbeer 547
		Promoter promoter = promoterRepository.selectById(promoterdetailModel.getId());
548
		if (promoter == null) {
549
			promoter = new Promoter();
24159 tejbeer 550
			promoter.setBrand(promoterdetailModel.getBrand());
551
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
552
			promoter.setCreatedTimestamp(LocalDateTime.now());
26131 tejbeer 553
 
24159 tejbeer 554
		}
27489 tejbeer 555
		promoter.setName(promoterdetailModel.getName());
556
		promoter.setEmail(promoterdetailModel.getEmail());
557
		promoter.setMobile(promoterdetailModel.getMobile());
558
		promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
559
		promoter.setStatus(promoterdetailModel.isStatus());
560
		LOGGER.info("sdport" + promoterdetailModel.isStatus());
561
		promoterRepository.persist(promoter);
24159 tejbeer 562
 
563
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
564
 
565
		return "response";
566
 
567
	}
568
 
569
	@RequestMapping(value = "/removePromoterfromPartnerStore", method = RequestMethod.POST)
570
	public String removePromoterfromPartnerStore(HttpServletRequest request,
571
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
572
 
573
		Promoter promoter = promoterRepository.selectById(id);
574
 
26131 tejbeer 575
		promoter.setStatus(false);
24159 tejbeer 576
		promoterRepository.persist(promoter);
577
 
578
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
579
 
580
		return "response";
581
	}
582
 
583
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
584
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
585
			throws Exception {
586
 
587
		Location location = new Location();
588
		location.setName(addLocationModel.getName());
589
		location.setLine1(addLocationModel.getLine1());
590
		location.setLine2(addLocationModel.getLine2());
591
		location.setCity(addLocationModel.getCity());
592
		location.setState(addLocationModel.getState());
593
		location.setPin(addLocationModel.getPin());
594
 
595
		LOGGER.info("PostLocation" + location);
596
		locationRepository.persist(location);
597
 
598
		User user = userRepository.selectById(addLocationModel.getUserId());
599
		user.setLocation(location.getId());
600
		userRepository.persist(user);
601
 
602
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
603
		return "response";
604
 
605
	}
606
 
607
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
608
	public String updateLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel,
609
			Model model) throws Exception {
610
 
611
		Location userlocation = locationRepository.selectById(addLocationModel.getUserId());
612
 
613
		userlocation.setName(addLocationModel.getName());
614
		userlocation.setCity(addLocationModel.getCity());
615
		userlocation.setLine1(addLocationModel.getLine1());
616
		userlocation.setLine2(addLocationModel.getLine2());
617
		userlocation.setPin(addLocationModel.getPin());
618
		userlocation.setState(addLocationModel.getState());
619
 
620
		locationRepository.persist(userlocation);
621
 
622
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
623
		return "response";
624
 
625
	}
626
 
27231 tejbeer 627
	@RequestMapping(value = "/getPartnerReadonlyInfo")
628
	public String getPartnerReadonlyInfo(HttpServletRequest request, Model model) throws Exception {
629
 
630
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
631
		AuthUser authUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
632
 
633
		Map<Integer, List<Integer>> pp = csService.getAuthUserIdPartnerIdMapping();
634
 
635
		List<Integer> fofoIds = pp.get(authUser.getId());
636
 
637
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
638
 
639
		model.addAttribute("customRetailersMap", customRetailersMap);
27243 tejbeer 640
 
27231 tejbeer 641
		return "partner-readonly-info";
642
 
643
	}
644
 
28071 tejbeer 645
	@RequestMapping(value = "/getBlockBrandMapping")
646
	public String getBlockBrandMapping(HttpServletRequest request, Model model) throws Exception {
647
 
648
		int fofoId = Utils.SYSTEM_PARTNER_ID;
649
		Set<String> brands = null;
650
 
651
		brands = mongoClient.getMongoBrands(fofoId, null, 3).stream().map(x -> (String) x.get("name"))
652
				.collect(Collectors.toSet());
653
 
654
		LOGGER.info("brands" + brands);
655
		model.addAttribute("brands", brands);
656
 
657
		return "block-brand-mapping";
658
 
659
	}
660
 
661
	@RequestMapping(value = "/getBlockBrand")
662
	public String getBlockBrand(HttpServletRequest request,
663
			@RequestParam(name = "brand", required = true, defaultValue = "0") String brand, Model model)
664
			throws Exception {
665
 
666
		List<RetailerBlockBrands> retailerBlockBrands = retailerBlockBrandsRepository.selectAllByBrands(brand);
667
 
668
		List<Integer> blockBrandPartners = retailerBlockBrands.stream().map(x -> x.getFofoId())
669
				.collect(Collectors.toList());
670
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(true);
671
		LOGGER.info("retailerBlockBrands" + retailerBlockBrands);
672
		model.addAttribute("blockBrandPartners", blockBrandPartners);
673
		model.addAttribute("retailerBlockBrands", retailerBlockBrands);
674
		model.addAttribute("customRetailers", customRetailers);
675
 
676
		return "retailer-block-brand";
677
 
678
	}
679
 
680
	@RequestMapping(value = "/getRetailerBlockBrandMappping", method = RequestMethod.POST)
681
	public String getBlockBrandMappping(HttpServletRequest request,
682
			@RequestParam(name = "brand", required = true, defaultValue = "0") String brand,
683
			@RequestParam(name = "fofoIds", required = true, defaultValue = "0") List<Integer> fofoIds, Model model)
684
			throws Exception {
685
		List<RetailerBlockBrands> retailerBlockBrands = retailerBlockBrandsRepository.selectAllByBrands(brand);
686
		if (!retailerBlockBrands.isEmpty()) {
687
			retailerBlockBrandsRepository.deleteAllbyBrands(brand);
688
		}
689
 
690
		for (int id : fofoIds) {
691
			RetailerBlockBrands retailerBlockBrand = new RetailerBlockBrands();
692
			retailerBlockBrand.setFofoId(id);
693
			retailerBlockBrand.setBlockBrands(brand);
694
			retailerBlockBrandsRepository.persist(retailerBlockBrand);
695
 
696
		}
697
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
698
 
699
		return "response";
700
	}
701
 
24124 govind 702
}