Subversion Repositories SmartDukaan

Rev

Rev 27797 | Rev 28071 | 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);
28024 tejbeer 150
 
151
				Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
152
 
26771 amit.gupta 153
		model.addAllAttributes(map);
28024 tejbeer 154
 
155
 
23955 govind 156
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 157
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
158
		LOGGER.info("warehouseMap", warehouseMap);
159
		model.addAttribute("warehouses", warehouseMap);
26771 amit.gupta 160
		if (map.containsKey("retailer")) {
161
			Retailer retailer = (Retailer) map.get("retailer");
162
			User user = userRepository.selectById(retailer.getId());
163
			if (user.getLocation() != null) {
164
				Location location = locationRepository.selectById(user.getLocation());
165
				model.addAttribute("locationdetail", location);
166
				LOGGER.info("location" + location);
167
			}
168
		}
22981 ashik.ali 169
		return "retailer-details";
170
	}
24124 govind 171
 
23026 ashik.ali 172
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 173
	public String updateRetailerDetails(HttpServletRequest request,
174
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 175
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
176
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
28024 tejbeer 177
 
178
 
23026 ashik.ali 179
		model.addAllAttributes(map);
23955 govind 180
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 181
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
182
		LOGGER.info("warehouseMap", warehouseMap);
183
		model.addAttribute("warehouses", warehouseMap);
23026 ashik.ali 184
		return "retailer-details";
185
	}
22981 ashik.ali 186
 
187
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 188
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 189
		return "retailer-info";
190
	}
24124 govind 191
 
23330 ashik.ali 192
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 193
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 194
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
195
	}
24124 govind 196
 
23494 ashik.ali 197
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 198
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
199
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
200
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 201
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 202
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 203
 
204
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 205
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
206
		}
24124 govind 207
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 208
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
209
		}
210
		return responseSender.ok(document);
211
	}
24124 govind 212
 
23494 ashik.ali 213
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 214
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
215
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
216
			throws ProfitMandiBusinessException {
217
 
23499 ashik.ali 218
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 219
 
220
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 221
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
222
		}
24124 govind 223
 
23494 ashik.ali 224
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 225
 
23494 ashik.ali 226
		FileInputStream file = null;
227
		try {
228
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
229
		} catch (FileNotFoundException e) {
230
			LOGGER.error("Retailer Document file not found : ", e);
231
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
232
		}
24159 tejbeer 233
		// ByteArrayOutputStream byteArrayOutputStream = new
234
		// ByteArrayOutputStream();
24124 govind 235
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
236
 
237
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 238
		String contentType = "";
24124 govind 239
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 240
			contentType = "image/jpeg";
24124 govind 241
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 242
			contentType = "image/png";
24124 govind 243
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 244
			contentType = "application/pdf";
245
		}
24124 govind 246
		headers.set("Content-Type", contentType);
247
		headers.set("Content-disposition", "inline; filename=" + document.getName());
248
		headers.setContentLength(document.getSize());
249
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
250
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
251
 
24159 tejbeer 252
		// return
253
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 254
	}
24124 govind 255
 
23494 ashik.ali 256
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 257
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
258
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
259
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 260
		Shop shop = shopRepository.selectById(shopId);
24124 govind 261
 
262
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 263
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
264
		}
24124 govind 265
 
266
		if (shop.getDocumentId() == null) {
23494 ashik.ali 267
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
268
		}
24124 govind 269
 
23494 ashik.ali 270
		Document document = documentRepository.selectById(shop.getDocumentId());
271
		return responseSender.ok(document);
272
	}
24124 govind 273
 
23494 ashik.ali 274
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 275
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
276
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
277
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
278
			throws ProfitMandiBusinessException {
279
 
23494 ashik.ali 280
		Shop shop = shopRepository.selectById(shopId);
24124 govind 281
 
282
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 283
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
284
		}
24124 govind 285
 
286
		if (shop.getDocumentId() == null) {
23494 ashik.ali 287
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
288
		}
24124 govind 289
 
23494 ashik.ali 290
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 291
 
23494 ashik.ali 292
		FileInputStream file = null;
293
		try {
294
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
295
		} catch (FileNotFoundException e) {
296
			LOGGER.error("Retailer Document file not found : ", e);
297
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
298
		}
24159 tejbeer 299
		// ByteArrayOutputStream byteArrayOutputStream = new
300
		// ByteArrayOutputStream();
24124 govind 301
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
302
 
303
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 304
		String contentType = "";
24124 govind 305
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 306
			contentType = "image/jpeg";
24124 govind 307
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 308
			contentType = "image/png";
24124 govind 309
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 310
			contentType = "application/pdf";
311
		}
24124 govind 312
		headers.set("Content-Type", contentType);
313
		headers.set("Content-disposition", "inline; filename=" + document.getName());
314
		headers.setContentLength(document.getSize());
315
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
316
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
317
 
24159 tejbeer 318
		// return
319
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 320
	}
24124 govind 321
 
322
	@GetMapping(value = "/getAllStores")
323
	public String getAllStores(HttpServletRequest request, Model model) {
324
 
25276 amit.gupta 325
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
326
				.collect(Collectors.toList());
24349 amit.gupta 327
		Map<Integer, Boolean> investments = new HashMap<>();
24124 govind 328
 
24349 amit.gupta 329
		for (FofoStore fofoStore : fofoStores) {
330
			boolean isOk = false;
331
			if (fofoStore.getGraceDate() != null && fofoStore.getGraceDate().isAfter(LocalDate.now())) {
332
				isOk = true;
333
			} else {
334
				try {
25276 amit.gupta 335
					isOk = partnerInvestmentService.isInvestmentOk(fofoStore.getId(), 10,
336
							ProfitMandiConstants.CUTOFF_INVESTMENT);
337
					if (fofoStore.getGraceCount() > 0) {
24349 amit.gupta 338
						fofoStore.setGraceDate(null);
339
						fofoStore.setGraceCount(0);
340
						fofoStoreRepository.persist(fofoStore);
341
					}
342
				} catch (ProfitMandiBusinessException e) {
343
				}
344
			}
345
			investments.put(fofoStore.getId(), isOk);
346
		}
24124 govind 347
 
26963 amit.gupta 348
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(false);
24124 govind 349
		model.addAttribute("fofoStores", fofoStores);
24349 amit.gupta 350
		model.addAttribute("investments", investments);
24124 govind 351
		model.addAttribute("customRetailers", customRetailers);
352
 
353
		return "store";
354
 
355
	}
356
 
27797 tejbeer 357
	@GetMapping(value = "/getPartnerCategory")
358
	public String getPartnerCategory(HttpServletRequest request, @RequestParam int fofoId, Model model) {
359
 
360
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
361
		List<PartnerType> partnerTypes = partnerType.nextPartnerTypes();
362
		LOGGER.info("partnerType" + partnerTypes);
363
		model.addAttribute("partnerTypes", partnerTypes);
364
		model.addAttribute("fofoId", fofoId);
365
		return "partner-category";
366
	}
367
 
368
	@PostMapping(value = "/updatePartnerCategory")
369
	public String updatePartnerCategory(HttpServletRequest request, @RequestParam(name = "fofoId") int fofoId,
370
			@RequestParam(name = "partnerType") PartnerType partnerType, Model model) throws Exception {
371
 
372
		PartnerTypeChange ptc = new PartnerTypeChange();
373
		ptc.setFofoId(fofoId);
374
		ptc.setPartnerType(partnerType);
375
		ptc.setCreateTimestamp(LocalDate.now().atStartOfDay().plusDays(1));
376
		partnerTypeChangeRepository.persist(ptc);
377
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
378
 
379
		return "response";
380
	}
381
 
24349 amit.gupta 382
	@GetMapping(value = "/partners")
383
	public String getStoreName(HttpServletRequest request, Model model, @RequestParam String query) throws Exception {
384
		model.addAttribute("response", mvcResponseSender
26963 amit.gupta 385
				.createResponseString(retailerService.getFofoRetailers(false).values().stream().filter(x -> {
24349 amit.gupta 386
					return x.getDisplayName().toLowerCase().matches(".*?" + query.toLowerCase() + ".*?");
387
				}).collect(Collectors.toList())));
388
		return "response";
389
	}
390
 
24124 govind 391
	@PostMapping(value = "/deactivateStore")
392
	public String deActivateStore(HttpServletRequest request,
393
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
394
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 395
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
27231 tejbeer 396
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "amit.gupta@smartdukaan.com")
26131 tejbeer 397
				.contains(loginDetails.getEmailId())) {
398
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
399
					"You are not authorise to deactivate retailer");
25276 amit.gupta 400
		}
24843 govind 401
		fofoStore.setActive(false);
402
		fofoStoreRepository.persist(fofoStore);
24124 govind 403
		LOGGER.info("inserted into InActiveFofoStore successfully");
404
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
405
 
406
		return "response";
407
 
408
	}
25276 amit.gupta 409
 
410
	@GetMapping(value = "/getAllInactiveStores")
411
	public String getInactiveStores(HttpServletRequest request, Model model) {
412
		List<FofoStore> inActiveFofoStores = fofoStoreRepository.selectInActiveStore();
413
		Map<Integer, CustomRetailer> customRetailers = retailerService
414
				.getFofoRetailers(inActiveFofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
24680 govind 415
		model.addAttribute("inActiveFofoStores", inActiveFofoStores);
416
		model.addAttribute("customRetailers", customRetailers);
417
		return "inactive-stores";
418
	}
25276 amit.gupta 419
 
420
	// Extend billing for 2 days
24349 amit.gupta 421
	@PostMapping(value = "/extendBilling")
422
	public String extendBilling(HttpServletRequest request,
423
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
424
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 425
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
27231 tejbeer 426
		if (Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "prakash.rai@smartdukaan.com",
427
				"amit.gupta@smartdukaan.com").contains(loginDetails.getEmailId())) {
25276 amit.gupta 428
			fofoStore.setGraceDate(LocalDate.now().plusDays(2));
429
			fofoStore.setGraceCount(fofoStore.getGraceCount() + 1);
430
			fofoStoreRepository.persist(fofoStore);
431
			model.addAttribute("response", mvcResponseSender.createResponseString(fofoStore.getGraceCount()));
432
		} else {
26131 tejbeer 433
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
434
					"You are not authorise to extend billing");
25276 amit.gupta 435
		}
24349 amit.gupta 436
		return "response";
25276 amit.gupta 437
 
24124 govind 438
	}
439
 
24159 tejbeer 440
	@RequestMapping(value = "/getPromoterInfo", method = RequestMethod.GET)
441
	public String getPromoterInfo(HttpServletRequest request,
442
			@RequestParam(name = "offset", defaultValue = "0") int offset,
27489 tejbeer 443
			@RequestParam(name = "limit", defaultValue = "10") int limit,
444
			@RequestParam(name = "fofoId", required = false, defaultValue = "0") int fofoId, Model model)
445
			throws Exception {
24159 tejbeer 446
		LOGGER.info("requested url : " + request.getRequestURL().toString());
447
		List<Promoter> promoterInfo = null;
448
 
449
		long size = 0;
27489 tejbeer 450
 
24159 tejbeer 451
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
452
				.collect(Collectors.toList());
453
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
454
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
24986 tejbeer 455
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
456
		brands.add("Airtel");
457
		brands.add("Vodafone");
458
		brands.add("Idea");
25276 amit.gupta 459
		LOGGER.info("brands" + brands);
24159 tejbeer 460
		model.addAttribute("customRetailers", customRetailers);
25276 amit.gupta 461
		model.addAttribute("brands", brands);
24159 tejbeer 462
 
27489 tejbeer 463
		if (fofoId != 0) {
464
			promoterInfo = promoterRepository.selectAllPromoterByFofoId(fofoId, offset, limit);
24159 tejbeer 465
 
27489 tejbeer 466
			size = promoterRepository.selectPromoterCount(fofoId);
467
 
468
		} else {
469
			promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
470
 
471
			size = promoterRepository.selectAllCount();
472
 
473
		}
24159 tejbeer 474
		if (!promoterInfo.isEmpty()) {
475
			List<Integer> partnerIds = new ArrayList<>();
476
 
477
			for (Promoter promoterdetail : promoterInfo) {
478
				partnerIds.add(promoterdetail.getRetailerId());
479
			}
480
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 481
			if (!partnerIds.equals(null)) {
24159 tejbeer 482
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 483
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 484
			}
24349 amit.gupta 485
 
24159 tejbeer 486
			LOGGER.info("partnerIds" + partnersMap);
487
			model.addAttribute("promoterInfo", promoterInfo);
488
			model.addAttribute("partnersMap", partnersMap);
489
			model.addAttribute("start", offset + 1);
490
			model.addAttribute("size", size);
491
			model.addAttribute("url", "/getPaginatedPromoterInfo");
492
 
493
			if (promoterInfo.size() < limit) {
494
				model.addAttribute("end", offset + promoterInfo.size());
495
			} else {
496
				model.addAttribute("end", offset + limit);
497
			}
498
		} else {
499
			model.addAttribute("promoterInfo", promoterInfo);
500
			model.addAttribute("size", size);
501
		}
502
		return "promoter-info";
503
 
504
	}
24349 amit.gupta 505
 
24159 tejbeer 506
	@RequestMapping(value = "/getPaginatedPromoterInfo", method = RequestMethod.GET)
507
	public String getPaginatedPromoterInfo(HttpServletRequest request,
508
			@RequestParam(name = "offset", defaultValue = "0") int offset,
27489 tejbeer 509
			@RequestParam(name = "limit", defaultValue = "10") int limit,
510
			@RequestParam(name = "fofoId", required = false, defaultValue = "0") int fofoId, Model model)
24159 tejbeer 511
			throws ProfitMandiBusinessException {
512
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
513
		List<Promoter> promoterInfo = null;
27489 tejbeer 514
		if (fofoId != 0) {
515
			promoterInfo = promoterRepository.selectAllPromoterByFofoId(fofoId, offset, limit);
516
		} else {
517
			promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
518
		}
24159 tejbeer 519
		LOGGER.info("promoterInfo" + promoterInfo);
520
		if (!promoterInfo.isEmpty()) {
521
			List<Integer> partnerIds = new ArrayList<>();
522
 
523
			for (Promoter promoterdetail : promoterInfo) {
524
				partnerIds.add(promoterdetail.getRetailerId());
525
			}
526
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 527
			if (!partnerIds.equals(null)) {
24159 tejbeer 528
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 529
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 530
			}
24349 amit.gupta 531
 
24159 tejbeer 532
			LOGGER.info("partnerIds" + partnersMap);
533
			model.addAttribute("promoterInfo", promoterInfo);
534
			model.addAttribute("partnersMap", partnersMap);
535
			model.addAttribute("url", "/getPaginatedPromoterInfo");
536
		} else {
537
			model.addAttribute("promoterInfo", promoterInfo);
538
 
539
		}
540
 
541
		return "promoter-info-paginated";
542
	}
543
 
544
	@RequestMapping(value = "/createPromoter", method = RequestMethod.POST)
545
	public String createPromoter(HttpServletRequest request, @RequestBody PromoterDetailModel promoterdetailModel,
546
			Model model) throws Exception {
547
		LOGGER.info("requested url : " + request.getRequestURL().toString());
548
		LOGGER.info("requested url : " + promoterdetailModel);
26131 tejbeer 549
		Promoter promoter = promoterRepository.selectById(promoterdetailModel.getId());
550
		if (promoter == null) {
551
			promoter = new Promoter();
24159 tejbeer 552
			promoter.setBrand(promoterdetailModel.getBrand());
553
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
554
			promoter.setCreatedTimestamp(LocalDateTime.now());
26131 tejbeer 555
 
24159 tejbeer 556
		}
27489 tejbeer 557
		promoter.setName(promoterdetailModel.getName());
558
		promoter.setEmail(promoterdetailModel.getEmail());
559
		promoter.setMobile(promoterdetailModel.getMobile());
560
		promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
561
		promoter.setStatus(promoterdetailModel.isStatus());
562
		LOGGER.info("sdport" + promoterdetailModel.isStatus());
563
		promoterRepository.persist(promoter);
24159 tejbeer 564
 
565
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
566
 
567
		return "response";
568
 
569
	}
570
 
571
	@RequestMapping(value = "/removePromoterfromPartnerStore", method = RequestMethod.POST)
572
	public String removePromoterfromPartnerStore(HttpServletRequest request,
573
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
574
 
575
		Promoter promoter = promoterRepository.selectById(id);
576
 
26131 tejbeer 577
		promoter.setStatus(false);
24159 tejbeer 578
		promoterRepository.persist(promoter);
579
 
580
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
581
 
582
		return "response";
583
	}
584
 
585
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
586
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
587
			throws Exception {
588
 
589
		Location location = new Location();
590
		location.setName(addLocationModel.getName());
591
		location.setLine1(addLocationModel.getLine1());
592
		location.setLine2(addLocationModel.getLine2());
593
		location.setCity(addLocationModel.getCity());
594
		location.setState(addLocationModel.getState());
595
		location.setPin(addLocationModel.getPin());
596
 
597
		LOGGER.info("PostLocation" + location);
598
		locationRepository.persist(location);
599
 
600
		User user = userRepository.selectById(addLocationModel.getUserId());
601
		user.setLocation(location.getId());
602
		userRepository.persist(user);
603
 
604
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
605
		return "response";
606
 
607
	}
608
 
609
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
610
	public String updateLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel,
611
			Model model) throws Exception {
612
 
613
		Location userlocation = locationRepository.selectById(addLocationModel.getUserId());
614
 
615
		userlocation.setName(addLocationModel.getName());
616
		userlocation.setCity(addLocationModel.getCity());
617
		userlocation.setLine1(addLocationModel.getLine1());
618
		userlocation.setLine2(addLocationModel.getLine2());
619
		userlocation.setPin(addLocationModel.getPin());
620
		userlocation.setState(addLocationModel.getState());
621
 
622
		locationRepository.persist(userlocation);
623
 
624
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
625
		return "response";
626
 
627
	}
628
 
27231 tejbeer 629
	@RequestMapping(value = "/getPartnerReadonlyInfo")
630
	public String getPartnerReadonlyInfo(HttpServletRequest request, Model model) throws Exception {
631
 
632
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
633
		AuthUser authUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
634
 
635
		Map<Integer, List<Integer>> pp = csService.getAuthUserIdPartnerIdMapping();
636
 
637
		List<Integer> fofoIds = pp.get(authUser.getId());
638
 
639
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
640
 
641
		model.addAttribute("customRetailersMap", customRetailersMap);
27243 tejbeer 642
 
27231 tejbeer 643
		return "partner-readonly-info";
644
 
645
	}
646
 
24124 govind 647
}