Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21615 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
25651 tejbeer 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
24288 amit.gupta 6
import java.time.LocalDate;
24276 amit.gupta 7
import java.time.LocalDateTime;
24880 govind 8
import java.time.LocalTime;
25649 tejbeer 9
import java.time.ZoneOffset;
24339 amit.gupta 10
import java.util.ArrayList;
26011 amit.gupta 11
import java.util.HashMap;
25136 amit.gupta 12
import java.util.LinkedHashMap;
23884 amit.gupta 13
import java.util.List;
24880 govind 14
import java.util.Map;
15
import java.util.stream.Collectors;
23568 govind 16
 
22086 amit.gupta 17
import javax.servlet.http.HttpServletRequest;
25221 amit.gupta 18
import javax.transaction.Transactional;
22086 amit.gupta 19
 
23786 amit.gupta 20
import org.apache.logging.log4j.LogManager;
23568 govind 21
import org.apache.logging.log4j.Logger;
22481 ashik.ali 22
import org.springframework.beans.factory.annotation.Autowired;
23379 ashik.ali 23
import org.springframework.beans.factory.annotation.Value;
25651 tejbeer 24
import org.springframework.core.io.InputStreamResource;
25
import org.springframework.http.HttpHeaders;
26
import org.springframework.http.HttpStatus;
25649 tejbeer 27
import org.springframework.http.ResponseEntity;
21615 kshitij.so 28
import org.springframework.stereotype.Controller;
22073 ashik.ali 29
import org.springframework.ui.Model;
21615 kshitij.so 30
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
25649 tejbeer 32
import org.springframework.web.bind.annotation.RequestParam;
21615 kshitij.so 33
 
25649 tejbeer 34
import com.google.gson.Gson;
25677 amit.gupta 35
import com.mongodb.DBObject;
25651 tejbeer 36
import com.spice.profitmandi.common.enumuration.ContentType;
25683 tejbeer 37
import com.spice.profitmandi.common.enumuration.MessageType;
22481 ashik.ali 38
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25677 amit.gupta 39
import com.spice.profitmandi.common.model.BrandStockPrice;
26005 amit.gupta 40
import com.spice.profitmandi.common.model.CustomRetailer;
25649 tejbeer 41
import com.spice.profitmandi.common.model.Notification;
24203 amit.gupta 42
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25651 tejbeer 43
import com.spice.profitmandi.common.web.util.ResponseSender;
25649 tejbeer 44
import com.spice.profitmandi.dao.Interface.Campaign;
26011 amit.gupta 45
import com.spice.profitmandi.dao.entity.auth.AuthUser;
46
import com.spice.profitmandi.dao.entity.auth.Menu;
47
import com.spice.profitmandi.dao.entity.cs.Position;
48
import com.spice.profitmandi.dao.entity.cs.TicketCategory;
25651 tejbeer 49
import com.spice.profitmandi.dao.entity.dtr.Document;
25649 tejbeer 50
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
51
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
22654 ashik.ali 52
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24288 amit.gupta 53
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
25214 amit.gupta 54
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
25566 tejbeer 55
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
25649 tejbeer 56
import com.spice.profitmandi.dao.model.SimpleCampaign;
57
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
25976 amit.gupta 58
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
26011 amit.gupta 59
import com.spice.profitmandi.dao.repository.auth.MenuCategoryRepository;
60
import com.spice.profitmandi.dao.repository.auth.MenuRepository;
61
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
62
import com.spice.profitmandi.dao.repository.cs.TicketCategoryRepository;
25651 tejbeer 63
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
22481 ashik.ali 64
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24996 amit.gupta 65
import com.spice.profitmandi.dao.repository.dtr.Mongo;
25649 tejbeer 66
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
67
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
68
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
24203 amit.gupta 69
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24880 govind 70
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
25649 tejbeer 71
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
24288 amit.gupta 72
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
25214 amit.gupta 73
import com.spice.profitmandi.dao.repository.fofo.PartnerTargetRepository;
25566 tejbeer 74
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
24336 amit.gupta 75
import com.spice.profitmandi.service.PartnerInvestmentService;
23844 amit.gupta 76
import com.spice.profitmandi.service.authentication.RoleManager;
25677 amit.gupta 77
import com.spice.profitmandi.service.inventory.InventoryService;
26005 amit.gupta 78
import com.spice.profitmandi.service.user.RetailerService;
22481 ashik.ali 79
import com.spice.profitmandi.web.model.LoginDetails;
80
import com.spice.profitmandi.web.util.CookiesProcessor;
81
 
21615 kshitij.so 82
@Controller
25222 amit.gupta 83
@Transactional(rollbackOn = Throwable.class)
21615 kshitij.so 84
public class DashboardController {
23923 amit.gupta 85
 
23379 ashik.ali 86
	@Value("${web.api.host}")
87
	private String webApiHost;
23923 amit.gupta 88
 
24072 amit.gupta 89
	@Value("${web.api.scheme}")
90
	private String webApiScheme;
24288 amit.gupta 91
 
24078 amit.gupta 92
	@Value("${web.api.root}")
93
	private String webApiRoot;
94
 
23379 ashik.ali 95
	@Value("${web.api.port}")
96
	private int webApiPort;
21615 kshitij.so 97
 
22481 ashik.ali 98
	@Autowired
22927 ashik.ali 99
	private CookiesProcessor cookiesProcessor;
23923 amit.gupta 100
 
23568 govind 101
	@Autowired
26011 amit.gupta 102
	private MenuRepository menuRepository;
103
 
104
	@Autowired
105
	private MenuCategoryRepository menuCategoryRepository;
106
 
107
	@Autowired
25214 amit.gupta 108
	private PartnerTargetRepository partnerTargetRepository;
109
 
110
	@Autowired
25653 amit.gupta 111
	private ResponseSender<?> responseSender;
26005 amit.gupta 112
 
113
	@Autowired
114
	RetailerService retailerService;
25214 amit.gupta 115
 
116
	@Autowired
23786 amit.gupta 117
	private RoleManager roleManager;
23923 amit.gupta 118
 
23838 ashik.ali 119
	@Autowired
120
	private FofoStoreRepository fofoStoreRepository;
23884 amit.gupta 121
 
122
	@Autowired
24880 govind 123
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
23923 amit.gupta 124
 
23884 amit.gupta 125
	@Autowired
24880 govind 126
	private PartnerInvestmentService partnerInvestmentService;
24288 amit.gupta 127
 
23884 amit.gupta 128
	@Autowired
25653 amit.gupta 129
	DocumentRepository documentRepository;
25683 tejbeer 130
 
25677 amit.gupta 131
	@Autowired
132
	InventoryService inventoryService;
23923 amit.gupta 133
 
23884 amit.gupta 134
	@Autowired
24203 amit.gupta 135
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
136
 
24880 govind 137
	@Autowired
138
	private FofoOrderItemRepository fofoOrderItemRepository;
26011 amit.gupta 139
 
140
	@Autowired
141
	private TicketCategoryRepository ticketCategoryRepository;
24880 govind 142
 
143
	@Autowired
25566 tejbeer 144
	private PartnerTypeChangeService partnerTypeChangeService;
25136 amit.gupta 145
 
24996 amit.gupta 146
	@Autowired
25649 tejbeer 147
	private HygieneDataRepository hygieneDataRepository;
148
 
149
	@Autowired
150
	private UserCampaignRepository userCampaignRepository;
26011 amit.gupta 151
 
152
	@Autowired
153
	private PositionRepository positionRepository;
25649 tejbeer 154
 
155
	@Autowired
156
	private UserAccountRepository userAccountRepository;
157
 
158
	@Autowired
159
	private NotificationCampaignRepository notificationCampaignRepository;
160
 
161
	@Autowired
24996 amit.gupta 162
	private Mongo mongoClient;
25976 amit.gupta 163
 
164
	@Autowired
165
	private AuthRepository authRepository;
24880 govind 166
 
25649 tejbeer 167
	@Autowired
168
	private Gson gson;
169
 
23568 govind 170
	private static final Logger LOGGER = LogManager.getLogger(DashboardController.class);
23923 amit.gupta 171
 
25136 amit.gupta 172
	@RequestMapping(value = "/12dashboard34", method = RequestMethod.GET)
25726 amit.gupta 173
	public String dashboard1(HttpServletRequest request, Model model, @RequestParam int fofoId) throws Exception {
25740 amit.gupta 174
		boolean isAdmin = false;
175
		model.addAttribute("isAdmin", isAdmin);
176
 
177
		model.addAttribute("webApiHost", webApiHost);
178
		model.addAttribute("webApiPort", webApiPort);
179
		model.addAttribute("webApiScheme", webApiScheme);
180
		model.addAttribute("webApiRoot", webApiRoot);
181
		if (isAdmin) {
182
			return "dashboard1";
183
		} else {
184
			FofoStore fofoStore = null;
185
			try {
26005 amit.gupta 186
				CustomRetailer customRetailer = retailerService.getFofoRetailer(fofoId);
25740 amit.gupta 187
				fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
188
				if (!fofoStore.isActive()) {
189
					return "redirect:/login";
190
				}
191
 
192
				PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoStore.getId(), LocalDate.now());
193
				model.addAttribute("partnerType", partnerType);
194
				model.addAttribute("partnerTypeImage", PartnerType.imageMap.get(partnerType));
26005 amit.gupta 195
				model.addAttribute("fofoStore", customRetailer);
25740 amit.gupta 196
				model.addAttribute("partnerType", partnerType);
197
				model.addAttribute("hasGift", hasGift(fofoId));
198
				model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
199
 
200
				model.addAttribute("brandStockPrices", this.getBrandStockPrices(fofoId));
201
				model.addAttribute("salesMap", this.getSales(fofoId));
202
				// this.setInvestments
203
				//
204
				model.addAttribute("investments", this.getInvestments(fofoId));
205
				model.addAttribute("isInvestmentOk", partnerInvestmentService.isInvestmentOk(fofoId,
206
						10, ProfitMandiConstants.CUTOFF_INVESTMENT));
207
			} catch (ProfitMandiBusinessException e) {
208
				LOGGER.error("FofoStore Code not found of fofoId {}", fofoId);
209
 
210
			}
211
		}
212
 
213
		LocalDateTime currentMonthStart = LocalDateTime.now().withDayOfMonth(1);
214
		LocalDateTime currentMonthEnd = currentMonthStart.plusMonths(1).withDayOfMonth(1);
215
		double currentMonthRating = hygieneDataRepository.selectRatingAvg(fofoId, currentMonthStart,
216
				currentMonthEnd) / 2;
217
		double lastMonthRating = hygieneDataRepository.selectRatingAvg(fofoId, currentMonthStart,
218
				currentMonthEnd) / 2;
219
		double ratingTillDate = hygieneDataRepository.selectRatingAvg(fofoId, currentMonthStart,
220
				currentMonthEnd) / 2;
221
		model.addAttribute("currentMonthRating", (float) Math.round(currentMonthRating * 10) / 10);
222
		model.addAttribute("lastMonthRating", (float) Math.round(lastMonthRating * 10) / 10);
223
		model.addAttribute("ratingTillDate", (float) Math.round(ratingTillDate * 10) / 10);
224
 
225
		long hygieneCount = hygieneDataRepository.selectHygieneCount(fofoId, true, currentMonthStart,
226
				currentMonthEnd);
227
 
228
		long invalidHygieneCount = hygieneDataRepository.selectHygieneCount(fofoId, false,
229
				currentMonthStart, currentMonthEnd);
230
		if (hygieneCount == 0 && invalidHygieneCount == 0) {
231
			invalidHygieneCount = 1;
232
		}
233
		model.addAttribute("hygienePercentage", (hygieneCount * 100) / (invalidHygieneCount + hygieneCount));
234
 
235
		model.addAttribute("monthDays", LocalDate.now().minusDays(1).lengthOfMonth());
236
		model.addAttribute("dayOfMonth", LocalDate.now().minusDays(1).getDayOfMonth());
237
		return "12dashboard34";
25136 amit.gupta 238
	}
239
 
240
	private Map<String, Object> getInvestments(int fofoId) throws Exception {
241
		Map<String, Object> investments = new LinkedHashMap<>();
242
		PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 1);
25649 tejbeer 243
		LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1);
25136 amit.gupta 244
		LocalDate yesterDate = LocalDate.now().minusDays(1);
245
		PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
246
		if (yesterdayInvestment == null) {
247
			yesterdayInvestment = new PartnerDailyInvestment();
248
		}
249
 
250
		List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId,
251
				currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
252
 
25140 amit.gupta 253
		long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= 10)
25136 amit.gupta 254
				.collect(Collectors.counting());
25140 amit.gupta 255
		investments.put("today", investment.getTotalInvestment());
256
		investments.put("investment", investment);
257
		investments.put("inStock", investment.getInStockAmount());
25182 amit.gupta 258
		investments.put("minimum", investment.getMinInvestmentString());
25140 amit.gupta 259
		investments.put("short", investment.getShortPercentage());
25649 tejbeer 260
		investments.put("okDays", okInvestmentDays);
25136 amit.gupta 261
		return investments;
262
	}
263
 
25221 amit.gupta 264
	private Map<String, Object> getSales(int fofoId) {
25653 amit.gupta 265
 
25221 amit.gupta 266
		Map<String, Object> salesMap = new LinkedHashMap<>();
25140 amit.gupta 267
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
25214 amit.gupta 268
		int monthLength = LocalDate.now().lengthOfMonth();
25864 amit.gupta 269
		Double todaySale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(curDate, curDate.with(LocalTime.MAX), 
25858 amit.gupta 270
				fofoId, false).get(fofoId);
25864 amit.gupta 271
		Double mtdSale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(curDate.withDayOfMonth(1),
25858 amit.gupta 272
				curDate.with(LocalTime.MAX), fofoId, false).get(fofoId);
25864 amit.gupta 273
		Double lmtdSale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(curDate.withDayOfMonth(1).minusMonths(1), 
25858 amit.gupta 274
				curDate.with(LocalTime.MAX).minusMonths(1), fofoId, false).get(fofoId);
25214 amit.gupta 275
 
276
		List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository
277
				.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
25544 amit.gupta 278
		if (partnerTargetDetails.isEmpty()) {
25372 tejbeer 279
			partnerTargetDetails = partnerTargetRepository
25653 amit.gupta 280
					.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now().minusMonths(3));
25372 tejbeer 281
		}
25214 amit.gupta 282
 
25653 amit.gupta 283
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
25214 amit.gupta 284
 
25664 amit.gupta 285
		int currentRate = (int) (mtdSale / curDate.getDayOfMonth());
25653 amit.gupta 286
 
287
		salesMap.put("requiredType", partnerType.next());
288
		float reqdAmount = partnerTypeChangeService.getMinimumAmount(partnerType.next());
289
		int requiredRate = (int) ((reqdAmount - mtdSale) / (monthLength - curDate.getDayOfMonth()));
290
		salesMap.put("requiredRate", requiredRate);
291
		salesMap.put("requiredTypeImage", PartnerType.imageMap.get(partnerType.next()));
292
 
25858 amit.gupta 293
		salesMap.put("todaySale", todaySale == null ? 0 : todaySale);
294
		salesMap.put("mtdSale", mtdSale == null ? 0 : mtdSale);
295
		salesMap.put("lmtdSale", lmtdSale == null ? 0  : lmtdSale);
25653 amit.gupta 296
 
297
		PartnerType currentType = partnerTypeChangeService.getPartnerTypeByAmount(currentRate * monthLength);
298
		salesMap.put("currentRate", currentRate);
299
		salesMap.put("currentType", currentType);
300
		salesMap.put("currentTypeImage", PartnerType.imageMap.get(currentType));
25136 amit.gupta 301
		return salesMap;
302
	}
303
 
25677 amit.gupta 304
	private List<BrandStockPrice> getBrandStockPrices(int fofoId) throws Exception {
305
		Map<String, BrandStockPrice> brandStockPricesMap = inventoryService.getBrandWiseStockValue(fofoId);
25175 amit.gupta 306
 
25677 amit.gupta 307
		List<DBObject> mobileBrands = mongoClient.getMongoBrands(fofoId, "", 3);
308
		List<BrandStockPrice> brandStockPrices = new ArrayList<>();
309
 
310
		mobileBrands.stream().forEach(x -> {
311
			String brand = (String) x.get("name");
312
			if (brandStockPricesMap.containsKey(brand)) {
313
				BrandStockPrice brandStockPrice = brandStockPricesMap.get(brand);
314
				brandStockPrice.setBrandUrl((String) x.get("url"));
315
				brandStockPrice.setRank(((Double) x.get("rank")).intValue());
316
				brandStockPrices.add(brandStockPrice);
317
			}
318
		});
319
 
320
		return brandStockPrices.stream().filter(x -> x.getTotalQty() > 0).sorted((x, y) -> x.getRank() - y.getRank())
321
				.collect(Collectors.toList());
322
	}
323
 
21615 kshitij.so 324
	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
25653 amit.gupta 325
	public String dashboard(HttpServletRequest request, Model model) throws Exception {
22927 ashik.ali 326
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26011 amit.gupta 327
		String email = loginDetails.getEmailId();
25180 amit.gupta 328
		boolean isAdmin = roleManager.isAdmin(loginDetails.getRoleIds());
329
		model.addAttribute("isAdmin", isAdmin);
23923 amit.gupta 330
 
25274 amit.gupta 331
		model.addAttribute("webApiHost", webApiHost);
332
		model.addAttribute("webApiPort", webApiPort);
333
		model.addAttribute("webApiScheme", webApiScheme);
25544 amit.gupta 334
		model.addAttribute("webApiRoot", webApiRoot);
25183 amit.gupta 335
		if (isAdmin) {
26011 amit.gupta 336
			return adminPanel(loginDetails.getFofoId(), email, model);
25180 amit.gupta 337
		} else {
25740 amit.gupta 338
			FofoStore fofoStore = null;
25180 amit.gupta 339
			try {
340
				fofoStore = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
25544 amit.gupta 341
				if (!fofoStore.isActive()) {
342
					return "redirect:/login";
343
				}
25649 tejbeer 344
 
25740 amit.gupta 345
				PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoStore.getId(), LocalDate.now());
25566 tejbeer 346
				model.addAttribute("partnerType", partnerType);
25653 amit.gupta 347
				model.addAttribute("partnerTypeImage", PartnerType.imageMap.get(partnerType));
25180 amit.gupta 348
				model.addAttribute("fofoStore", fofoStore);
25566 tejbeer 349
				model.addAttribute("partnerType", partnerType);
25180 amit.gupta 350
				model.addAttribute("hasGift", hasGift(loginDetails.getFofoId()));
351
				model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
352
 
25677 amit.gupta 353
				model.addAttribute("brandStockPrices", this.getBrandStockPrices(loginDetails.getFofoId()));
25180 amit.gupta 354
				model.addAttribute("salesMap", this.getSales(loginDetails.getFofoId()));
25544 amit.gupta 355
				// this.setInvestments
356
				//
25180 amit.gupta 357
				model.addAttribute("investments", this.getInvestments(loginDetails.getFofoId()));
25544 amit.gupta 358
				model.addAttribute("isInvestmentOk", partnerInvestmentService.isInvestmentOk(loginDetails.getFofoId(),
359
						10, ProfitMandiConstants.CUTOFF_INVESTMENT));
25180 amit.gupta 360
			} catch (ProfitMandiBusinessException e) {
361
				LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
362
 
363
			}
22481 ashik.ali 364
		}
25649 tejbeer 365
 
366
		LocalDateTime currentMonthStart = LocalDateTime.now().withDayOfMonth(1);
25747 amit.gupta 367
		LocalDateTime lastMonthStart = currentMonthStart.minusMonths(1);
25653 amit.gupta 368
		LocalDateTime currentMonthEnd = currentMonthStart.plusMonths(1).withDayOfMonth(1);
369
		double currentMonthRating = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), currentMonthStart,
25677 amit.gupta 370
				currentMonthEnd) / 2;
25747 amit.gupta 371
		double lastMonthRating = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), lastMonthStart,
372
				currentMonthStart) / 2;
373
		double ratingTillDate = hygieneDataRepository.selectRatingAvg(loginDetails.getFofoId(), LocalDateTime.MIN,
25677 amit.gupta 374
				currentMonthEnd) / 2;
375
		model.addAttribute("currentMonthRating", (float) Math.round(currentMonthRating * 10) / 10);
376
		model.addAttribute("lastMonthRating", (float) Math.round(lastMonthRating * 10) / 10);
377
		model.addAttribute("ratingTillDate", (float) Math.round(ratingTillDate * 10) / 10);
25649 tejbeer 378
 
25653 amit.gupta 379
		long hygieneCount = hygieneDataRepository.selectHygieneCount(loginDetails.getFofoId(), true, currentMonthStart,
380
				currentMonthEnd);
25651 tejbeer 381
 
25653 amit.gupta 382
		long invalidHygieneCount = hygieneDataRepository.selectHygieneCount(loginDetails.getFofoId(), false,
383
				currentMonthStart, currentMonthEnd);
384
		if (hygieneCount == 0 && invalidHygieneCount == 0) {
385
			invalidHygieneCount = 1;
386
		}
387
		model.addAttribute("hygienePercentage", (hygieneCount * 100) / (invalidHygieneCount + hygieneCount));
25651 tejbeer 388
 
25197 amit.gupta 389
		model.addAttribute("monthDays", LocalDate.now().minusDays(1).lengthOfMonth());
390
		model.addAttribute("dayOfMonth", LocalDate.now().minusDays(1).getDayOfMonth());
25140 amit.gupta 391
		return "dashboard1";
21615 kshitij.so 392
	}
23923 amit.gupta 393
 
26011 amit.gupta 394
	private String adminPanel(int fofoId, String email, Model model) throws ProfitMandiBusinessException {
395
		List<Menu> menus;
396
		try {
397
			AuthUser authUser = authRepository.selectByEmailOrMobile(email);
398
			List<Position> positions = positionRepository.selectAll(authUser.getId());
399
			menus = menuRepository.selectAllByIds(menuCategoryRepository.selectAllByPositions(positions).stream().map(x->x.getMenuId()).collect(Collectors.toList()));
400
		} catch (ProfitMandiBusinessException e){
401
			menus = menuRepository.selectAll();
402
		}
403
 
404
		Map<Integer, List<Menu>> subMenuMap = new HashMap<Integer, List<Menu>>();
405
		for(Menu menu : menus) {
406
			if(menu.get_parent()==null) {
407
				if(!subMenuMap.containsKey(menu.getId())) {
408
					subMenuMap.put(menu.getId(), new ArrayList<>());
409
				}
410
				menu.setSubMenus(subMenuMap.get(menu.getId()));
411
			} else {
412
				int parentId = menu.get_parent().getId();
413
				if(!subMenuMap.containsKey(parentId)) {
414
					subMenuMap.put(parentId, new ArrayList<>());
415
				}
416
				subMenuMap.get(parentId).add(menu);
417
			}
418
		}
419
		model.addAttribute("menu", menus);
26005 amit.gupta 420
		return "admin";
421
	}
422
 
24288 amit.gupta 423
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 424
	private boolean hasGift(int fofoId) {
425
		try {
24288 amit.gupta 426
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
427
					.getAvailability() > 0;
24203 amit.gupta 428
		} catch (ProfitMandiBusinessException e) {
429
			return false;
430
		}
431
	}
24288 amit.gupta 432
 
22354 ashik.ali 433
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 434
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 435
		model.addAttribute("appContextPath", request.getContextPath());
436
		return "contact-us";
437
	}
23923 amit.gupta 438
 
25649 tejbeer 439
	@RequestMapping(value = "/notifications", method = RequestMethod.GET)
25683 tejbeer 440
	public String getNotificationsWithType(HttpServletRequest request,
441
			@RequestParam(required = false) MessageType messageType,
25649 tejbeer 442
			@RequestParam(name = "offset", defaultValue = "0") int offset,
443
			@RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
444
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
445
		int userId = userAccountRepository.selectUserIdByRetailerId(loginDetails.getFofoId());
446
		List<UserCampaign> userCampaigns = userCampaignRepository.selectByUserId(userId);
25683 tejbeer 447
 
448
		List<Notification> notifications = null;
25649 tejbeer 449
		if (!userCampaigns.isEmpty()) {
450
			List<Integer> campaignIds = new ArrayList<>();
451
			for (UserCampaign userCampaign : userCampaigns) {
452
				campaignIds.add(userCampaign.getCampaignId());
453
			}
25988 tejbeer 454
			List<NotificationCampaign> notificationCampaigns = notificationCampaignRepository.selectbyCids(campaignIds,
455
					offset, limit);
25683 tejbeer 456
			LOGGER.info("messageType" + messageType);
457
			notifications = getNotifications(notificationCampaigns, messageType);
458
 
459
		}
460
 
461
		model.addAttribute("notifications", notifications);
462
 
463
		LOGGER.info("notifications" + notifications);
464
		return "notification-template";
465
	}
466
 
467
	public List<Notification> getNotifications(List<NotificationCampaign> nc, MessageType messageType)
468
			throws ProfitMandiBusinessException {
469
		List<Notification> notifications = new ArrayList<>();
470
		Document document = null;
471
		if (messageType != null) {
472
			for (NotificationCampaign notificationCampaign : nc) {
473
				if (notificationCampaign.getMessageType() == messageType) {
25649 tejbeer 474
					Notification ns = new Notification();
475
					SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
476
							SimpleCampaignParams.class);
477
					Campaign campaign = new SimpleCampaign(scp);
478
					LocalDateTime expire = campaign.getExpireTimestamp();
479
					ns.setCid(Integer.toString(notificationCampaign.getId()));
480
					ns.setType(campaign.getType());
481
					ns.setMessage(campaign.getMessage());
482
					ns.setTitle(campaign.getTitle());
25651 tejbeer 483
					if (notificationCampaign.getDocumentId() != null) {
484
						document = documentRepository.selectById(notificationCampaign.getDocumentId());
485
						ns.setDocumentName(document.getDisplayName());
486
					}
25683 tejbeer 487
					ns.setUrl(campaign.getUrl());
25649 tejbeer 488
					ns.setShowImage(campaign.getShowImage());
489
					ns.setImageUrl(campaign.getImageUrl());
25683 tejbeer 490
					ns.setDocumentId(notificationCampaign.getDocumentId());
491
					ns.setMessageType(notificationCampaign.getMessageType());
25649 tejbeer 492
					ns.setCreated(
493
							notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
494
									* 1000);
495
					if (LocalDateTime.now().isAfter(expire)) {
496
						ns.setExpired(true);
497
					} else {
498
						ns.setExpired(false);
499
					}
500
					notifications.add(ns);
501
				}
502
			}
25683 tejbeer 503
		} else {
504
			for (NotificationCampaign notificationCampaign : nc) {
505
 
506
				Notification ns = new Notification();
507
				SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
508
						SimpleCampaignParams.class);
509
				Campaign campaign = new SimpleCampaign(scp);
510
				LocalDateTime expire = campaign.getExpireTimestamp();
511
				ns.setCid(Integer.toString(notificationCampaign.getId()));
512
				ns.setType(campaign.getType());
513
				ns.setMessage(campaign.getMessage());
514
				ns.setTitle(campaign.getTitle());
515
				if (notificationCampaign.getDocumentId() != null) {
516
					document = documentRepository.selectById(notificationCampaign.getDocumentId());
517
					ns.setDocumentName(document.getDisplayName());
518
				}
519
				ns.setUrl(campaign.getUrl());
520
				ns.setShowImage(campaign.getShowImage());
521
				ns.setImageUrl(campaign.getImageUrl());
522
				ns.setDocumentId(notificationCampaign.getDocumentId());
523
				ns.setMessageType(notificationCampaign.getMessageType());
524
				ns.setCreated(notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
525
						* 1000);
526
				if (LocalDateTime.now().isAfter(expire)) {
527
					ns.setExpired(true);
528
				} else {
529
					ns.setExpired(false);
530
				}
531
				notifications.add(ns);
532
			}
533
 
25649 tejbeer 534
		}
25683 tejbeer 535
		return notifications;
25651 tejbeer 536
 
537
	}
25649 tejbeer 538
 
25651 tejbeer 539
	@RequestMapping(value = "/notifyDocument/documentId", method = RequestMethod.GET)
540
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
541
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId, @RequestParam int cid)
542
			throws ProfitMandiBusinessException {
543
		Document document = documentRepository.selectById(documentId);
544
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
545
		if (nc.getDocumentId() == null) {
546
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
547
		}
548
		if (nc.getDocumentId() != documentId) {
549
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
550
		}
551
		return responseSender.ok(document);
25649 tejbeer 552
	}
553
 
25651 tejbeer 554
	@RequestMapping(value = "/notifyDocument/download", method = RequestMethod.GET)
555
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request, @RequestParam int cid, Model model)
556
			throws ProfitMandiBusinessException {
557
 
558
		NotificationCampaign nc = notificationCampaignRepository.selectById(cid);
559
 
560
		if (nc.getDocumentId() == null) {
561
			throw new ProfitMandiBusinessException("cid", nc.getId(), "not available");
562
		}
563
 
564
		Document document = documentRepository.selectById(nc.getDocumentId());
565
 
566
		FileInputStream file = null;
567
		try {
568
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
569
		} catch (FileNotFoundException e) {
570
			LOGGER.error("Retailer Document file not found : ", e);
571
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
572
		}
573
		// ByteArrayOutputStream byteArrayOutputStream = new
574
		// ByteArrayOutputStream();
575
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
576
 
577
		final HttpHeaders headers = new HttpHeaders();
578
		String contentType = "";
579
		if (document.getContentType() == ContentType.JPEG) {
580
			contentType = "image/jpeg";
581
		} else if (document.getContentType() == ContentType.PNG) {
582
			contentType = "image/png";
583
		} else if (document.getContentType() == ContentType.PDF) {
584
			contentType = "application/pdf";
585
		}
586
		headers.set("Content-Type", contentType);
587
		headers.set("Content-disposition", "inline; filename=" + document.getName());
588
		headers.setContentLength(document.getSize());
589
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
590
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
591
	}
592
 
21615 kshitij.so 593
}