Subversion Repositories SmartDukaan

Rev

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