Subversion Repositories SmartDukaan

Rev

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