Subversion Repositories SmartDukaan

Rev

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