Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
28468 tejbeer 1
package com.spice.profitmandi.service;
2
 
3
import com.google.gson.Gson;
4
import com.spice.profitmandi.common.enumuration.ReporticoProject;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30449 amit.gupta 6
import com.spice.profitmandi.common.model.*;
28468 tejbeer 7
import com.spice.profitmandi.dao.entity.auth.AuthUser;
8
import com.spice.profitmandi.dao.entity.auth.Menu;
9
import com.spice.profitmandi.dao.entity.cs.Position;
10
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
29165 manish 11
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
28468 tejbeer 12
import com.spice.profitmandi.dao.entity.inventory.ReporticoCacheTable;
13
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
14
import com.spice.profitmandi.dao.model.BrandWiseModel;
15
import com.spice.profitmandi.dao.model.PartnerDetailModel;
16
import com.spice.profitmandi.dao.model.WarehouseWiseStockModel;
17
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
18
import com.spice.profitmandi.dao.repository.auth.MenuCategoryRepository;
19
import com.spice.profitmandi.dao.repository.auth.MenuRepository;
20
import com.spice.profitmandi.dao.repository.cs.CsService;
21
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
22
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
23
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
28825 tejbeer 24
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
28468 tejbeer 25
import com.spice.profitmandi.dao.repository.inventory.ReporticoCacheTableRepository;
26
import com.spice.profitmandi.dao.repository.inventory.SaholicInventoryCISRepository;
27
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
28
import com.spice.profitmandi.service.user.RetailerService;
30449 amit.gupta 29
import org.apache.logging.log4j.LogManager;
30
import org.apache.logging.log4j.Logger;
31
import org.springframework.beans.factory.annotation.Autowired;
32
import org.springframework.stereotype.Component;
33
import org.springframework.ui.Model;
28468 tejbeer 34
 
30449 amit.gupta 35
import java.io.FileInputStream;
36
import java.io.ObjectInputStream;
37
import java.time.LocalDate;
38
import java.time.LocalDateTime;
39
import java.time.Period;
40
import java.time.YearMonth;
41
import java.time.format.DateTimeFormatter;
42
import java.time.temporal.ChronoUnit;
43
import java.util.*;
44
import java.util.Map.Entry;
45
import java.util.stream.Collectors;
46
import java.util.stream.LongStream;
47
 
28468 tejbeer 48
@Component
49
public class AdminUser {
50
	@Autowired
51
	private Gson gson;
52
 
53
	@Autowired
54
	private OrderRepository orderRepository;
55
 
56
	@Autowired
57
	TicketRepository ticketRepository;
58
 
59
	@Autowired
60
	private ChartService chartService;
61
 
62
	@Autowired
63
	RetailerService retailerService;
64
 
65
	@Autowired
66
	AuthRepository authRepository;
67
 
68
	@Autowired
69
	FofoStoreRepository fofoStoreRepository;
70
 
71
	@Autowired
72
	CsService csService;
73
 
74
	@Autowired
75
	private MenuRepository menuRepository;
76
 
77
	@Autowired
78
	private PositionRepository positionRepository;
79
 
80
	@Autowired
81
	private MenuCategoryRepository menuCategoryRepository;
82
 
83
	@Autowired
84
	private SaholicInventoryCISRepository saholicInventoryCISRepository;
85
 
86
	@Autowired
87
	private ReporticoCacheTableRepository reporticoCacheTableRepository;
88
 
89
	@Autowired
28825 tejbeer 90
	private ActivatedImeiRepository activatedImeiRepository;
91
 
92
	@Autowired
28468 tejbeer 93
	private PartnerStatsService partnerStatsService;
94
	List<String> emails = Arrays.asList("kamini.sharma@smartdukaan.com", "neeraj.gupta@smartdukaan.com",
31130 tejbeer 95
			"tejbeer.kaur@smartdukaan.com", "amit.gupta@smartdukaan.com", "kanan.choudhary@smartdukaan.com");
28468 tejbeer 96
 
30647 tejbeer 97
	List<String> colorList = Arrays.asList("mediumseagreen", "dodgerblue", "darkblue", "gold", "coral", "#63C5DA",
28468 tejbeer 98
			"red", "deeppink", "midnightblue", "cornsilk");
99
 
100
	List<String> borderList = Arrays.asList("lawngreen", "lightblue", "#0000cd", "#f7e98e", "lightcoral", "#0000cd",
101
			"lightsalmon", "pink", "#0000cd", "cornsilk");
102
 
103
	List<String> brands = Arrays.asList("Oppo", "Vivo", "Samsung", "Realme", "Xiaomi", "Tecno", "Itel", "Lava",
104
			"Nokia");
105
 
106
	private static final Logger LOGGER = LogManager.getLogger(AdminUser.class);
107
 
30279 tejbeer 108
	public ChartModel getBrandWiseLmp(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
109
			LocalDate endDate, String filterType) {
110
		LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 111
 
30279 tejbeer 112
		List<BrandWiseModel> brandWiseLms = orderRepository.selectGroupByBrandLmp(fofoIds, warehouseIds, startDate,
113
				endDate, filterType);
28468 tejbeer 114
		LOGGER.info("brandWiseLms" + brandWiseLms);
30279 tejbeer 115
		ChartModel cm = null;
28468 tejbeer 116
 
30279 tejbeer 117
		if (filterType.equals("dateWise")) {
118
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 119
 
30279 tejbeer 120
			Map<String, Map<LocalDate, Double>> brandDateValue = new HashMap<>();
121
			for (BrandWiseModel bwl : brandWiseLms) {
122
				if (!brandDateValue.containsKey(bwl.getBrand())) {
123
					brandDateValue.put(bwl.getBrand(), new HashMap<>());
124
				}
125
				Map<LocalDate, Double> dateWiseSalesMap = brandDateValue.get(bwl.getBrand());
126
				dateWiseSalesMap.put(LocalDate.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
28468 tejbeer 127
			}
128
 
30279 tejbeer 129
			long days = startDate.until(endDate, ChronoUnit.DAYS);
130
			List<LocalDate> allDatesBetween = LongStream.range(0, days).mapToObj(x -> startDate.plusDays(x))
131
					.collect(Collectors.toList());
132
			LOGGER.info("All dates between {}", allDatesBetween);
28468 tejbeer 133
 
30279 tejbeer 134
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 135
 
30279 tejbeer 136
			for (String brand : brands) {
137
				if (!brandDateValue.containsKey(brand)) {
138
					brandDateValue.put(brand, new HashMap<>());
139
				}
140
				Map<LocalDate, Double> dateWiseBrand = brandDateValue.get(brand);
141
				for (LocalDate date : allDatesBetween) {
142
					if (dateWiseBrand.get(date) == null) {
143
						dateWiseBrand.put(date, 0.0);
144
					}
145
				}
146
 
147
				Map<LocalDate, Double> sortedMonthBrandValue = new TreeMap<>(dateWiseBrand);
148
				sortedBrandValue.put(brand, new ArrayList<>(sortedMonthBrandValue.values()));
149
				LOGGER.info("Sorted Brandwise values count {}", sortedMonthBrandValue.size());
30220 amit.gupta 150
			}
30279 tejbeer 151
 
152
			LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
153
 
154
			LOGGER.info("sortedBrandValueCount {}", sortedBrandValue.size());
155
			cm = chartService.createChartWithLabels(
156
					allDatesBetween.stream().map(x -> x.format((DateTimeFormatter.ofPattern("dd MMM''uu"))))
157
							.collect(Collectors.toCollection(LinkedHashSet::new)),
158
					sortedBrandValue, colorList, borderList, "Brand Wise Date Wise Purchase");
159
 
160
		} else {
161
 
162
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
163
 
164
			Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
165
			for (BrandWiseModel bwl : brandWiseLms) {
166
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
167
				if (brandMonthValue.containsKey(bwl.getBrand())) {
168
					yearMonthValue = brandMonthValue.get(bwl.getBrand());
169
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
170
							(double) bwl.getAmount());
171
				} else {
172
 
173
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
174
							(double) bwl.getAmount());
175
 
28468 tejbeer 176
				}
30279 tejbeer 177
				brandMonthValue.put(bwl.getBrand(), yearMonthValue);
178
 
28468 tejbeer 179
			}
30279 tejbeer 180
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 181
 
30279 tejbeer 182
			Period age = Period.between(startDate, endDate);
183
			int months = age.getMonths();
184
			LOGGER.info("months" + months);
185
			LOGGER.info("brandWiseLms" + brandWiseLms);
186
			LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 187
 
30279 tejbeer 188
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 189
 
30279 tejbeer 190
			for (String brand : brands) {
191
				Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
192
				for (int i = months; i >= 0; i--) {
28468 tejbeer 193
 
30279 tejbeer 194
					LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
28468 tejbeer 195
 
30279 tejbeer 196
					if (yearMonthValue != null) {
197
						if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
198
							yearMonthValue.put(YearMonth.from(startMonth), 0.0);
199
						}
200
 
201
					} else {
202
						yearMonthValue = new HashMap<>();
203
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
204
					}
205
				}
206
 
207
				Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
208
 
209
				brandMonthValue.put(brand, sortedMonthBrandValue);
210
 
211
				sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
212
 
213
			}
214
 
215
			cm = chartService.createChart(months, sortedBrandValue, colorList, borderList,
216
					"Brand Wise Monthly Purchase");
217
 
218
		}
219
 
28468 tejbeer 220
		return cm;
221
	}
222
 
29165 manish 223
	public ChartInvestmentModel getAllStatePartnerType(Map<Integer, PartnerDetailModel> fofoIdAndallValues)
224
			throws ProfitMandiBusinessException {
225
 
226
		Map<String, Float> partnerTypeCount = new HashMap<>();
227
 
228
		List<PartnerType> partnerTypes = Arrays.asList(PartnerType.values());
229
 
230
		for (PartnerType partnerType : partnerTypes) {
231
 
232
			float count = fofoIdAndallValues.entrySet().stream()
233
					.filter(x -> x.getValue().getPartnerType().equals(partnerType)).count();
234
 
235
			partnerTypeCount.put(partnerType.toString(), count);
236
 
237
		}
238
 
239
		ChartInvestmentModel cm = new ChartInvestmentModel();
240
 
241
		HashSet<String> labels = new HashSet<String>();
242
		labels.addAll(partnerTypes.stream().skip(1).map(x -> x.toString()).collect(Collectors.toList()));
243
		List<String> labelList = new ArrayList<>(labels);
244
 
245
		List<String> backgroundColor = new ArrayList<>();
246
 
247
		List<Float> values = new ArrayList<>();
248
 
249
		for (String label : labelList) {
250
			values.add(partnerTypeCount.get(label));
251
			if (label.equals("BRONZE")) {
29168 manish 252
				backgroundColor.add("#CD7F32");
29165 manish 253
			}
254
			if (label.equals("SILVER")) {
255
				backgroundColor.add("silver");
256
			}
257
			if (label.equals("GOLD")) {
29168 manish 258
				backgroundColor.add("#FFD700");
29165 manish 259
			}
260
			if (label.equals("DIAMOND")) {
261
				backgroundColor.add("#B9F2FF");
262
			}
263
 
264
			if (label.equals("PLATINUM")) {
29198 manish 265
				backgroundColor.add("#800080");
29165 manish 266
			}
267
			if (label.equals("NEW")) {
268
				backgroundColor.add("green");
269
			}
270
 
271
		}
272
 
273
		LOGGER.info("valuesPartnerType" + values);
274
		LOGGER.info("partnerTypeCount" + partnerTypeCount);
275
 
276
		Data data = new Data();
277
		data.setData(values);
278
		data.setBackgroundColor(backgroundColor);
279
		data.setLabel("DataSet 1");
280
 
281
		PieLables label = new PieLables();
282
		label.setFontColor("black");
283
		label.setFontSize(22);
284
 
285
		Legend legend = new Legend();
286
		legend.setLabels(label);
287
		legend.setPosition("left");
288
 
289
		List<Data> dataList = new ArrayList<>();
290
		dataList.add(data);
291
 
292
		DataInvestmentModel datasets = new DataInvestmentModel();
293
		datasets.setDatasets(dataList);
294
		datasets.setLabels(labels);
295
 
296
		OptionModel om = new OptionModel();
297
		om.setLegend(legend);
298
		cm.setType("pie");
299
		cm.setData(datasets);
300
		cm.setOptions(om);
301
 
302
		return cm;
303
	}
304
 
30279 tejbeer 305
	public Map<String, Object> getFilter(int warehouseId, String email, LocalDateTime date, LocalDateTime endDate)
28468 tejbeer 306
			throws ProfitMandiBusinessException {
307
		Map<String, Object> map = new HashMap<>();
30426 tejbeer 308
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
309
		Map<Integer, CustomRetailer> crm = null;
28468 tejbeer 310
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
311
 
31152 tejbeer 312
		Set<Integer> fofoIds = csService.getAuthFofoIds(email);
28468 tejbeer 313
 
314
		if (warehouseId != 0) {
315
 
316
			fofoIds = fofoStoreRepository.selectPartnerByfofoIdAndWarehouse(new ArrayList<>(fofoIds), warehouseId)
317
					.stream().map(x -> x).collect(Collectors.toSet());
318
 
319
		}
320
 
321
		if (fofoIds != null) {
322
 
30426 tejbeer 323
			crm = fofoIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null)
324
					.collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
325
 
28468 tejbeer 326
		}
327
 
328
		map.put("date", date);
30279 tejbeer 329
		map.put("endDate", endDate);
30426 tejbeer 330
		map.put("customRetailersMap", crm);
28468 tejbeer 331
		map.put("fofoIds", fofoIds);
332
		map.put("warehouseMap", warehouseMap);
333
 
334
		return map;
335
 
336
	}
337
 
30279 tejbeer 338
	public ChartModel getBrandWiseLms(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
339
			LocalDate endDate, String filterType) {
340
		LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 341
 
30279 tejbeer 342
		List<BrandWiseModel> brandWiseLms = fofoStoreRepository.selectGroupByBrandLms(fofoIds, warehouseIds, startDate,
343
				endDate, filterType);
28468 tejbeer 344
		LOGGER.info("brandWiseLms" + brandWiseLms);
345
 
30279 tejbeer 346
		ChartModel cm = null;
28468 tejbeer 347
 
30279 tejbeer 348
		if (filterType.equals("dateWise")) {
349
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 350
 
30279 tejbeer 351
			Map<String, Map<LocalDate, Double>> brandDateValue = new HashMap<>();
352
			for (BrandWiseModel bwl : brandWiseLms) {
353
				if (!brandDateValue.containsKey(bwl.getBrand())) {
354
					brandDateValue.put(bwl.getBrand(), new HashMap<>());
355
				}
356
				Map<LocalDate, Double> dateWiseSalesMap = brandDateValue.get(bwl.getBrand());
357
				dateWiseSalesMap.put(LocalDate.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
358
			}
28468 tejbeer 359
 
30279 tejbeer 360
			long days = startDate.until(endDate, ChronoUnit.DAYS);
361
			List<LocalDate> allDatesBetween = LongStream.range(0, days).mapToObj(x -> startDate.plusDays(x))
362
					.collect(Collectors.toList());
363
			LOGGER.info("All dates between {}", allDatesBetween);
364
 
365
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
366
 
367
			for (String brand : brands) {
368
				if (!brandDateValue.containsKey(brand)) {
369
					brandDateValue.put(brand, new HashMap<>());
370
				}
371
				Map<LocalDate, Double> dateWiseBrand = brandDateValue.get(brand);
372
				for (LocalDate date : allDatesBetween) {
373
					if (dateWiseBrand.get(date) == null) {
374
						dateWiseBrand.put(date, 0.0);
375
					}
376
				}
377
 
378
				Map<LocalDate, Double> sortedMonthBrandValue = new TreeMap<>(dateWiseBrand);
379
				sortedBrandValue.put(brand, new ArrayList<>(sortedMonthBrandValue.values()));
380
				LOGGER.info("Sorted Brandwise values count {}", sortedMonthBrandValue.size());
28468 tejbeer 381
			}
382
 
30279 tejbeer 383
			LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
28468 tejbeer 384
 
30279 tejbeer 385
			LOGGER.info("sortedBrandValueCount {}", sortedBrandValue.size());
386
			cm = chartService.createChartWithLabels(
387
					allDatesBetween.stream().map(x -> x.format((DateTimeFormatter.ofPattern("dd MMM''uu"))))
388
							.collect(Collectors.toCollection(LinkedHashSet::new)),
389
					sortedBrandValue, colorList, borderList, "Brand Wise Date Wise Sale");
28468 tejbeer 390
 
30279 tejbeer 391
		} else {
28468 tejbeer 392
 
30279 tejbeer 393
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
28468 tejbeer 394
 
30279 tejbeer 395
			Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
396
			for (BrandWiseModel bwl : brandWiseLms) {
397
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
398
				if (brandMonthValue.containsKey(bwl.getBrand())) {
399
					yearMonthValue = brandMonthValue.get(bwl.getBrand());
400
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
401
							(double) bwl.getAmount());
402
				} else {
28468 tejbeer 403
 
30279 tejbeer 404
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
405
							(double) bwl.getAmount());
28468 tejbeer 406
 
407
				}
30279 tejbeer 408
				brandMonthValue.put(bwl.getBrand(), yearMonthValue);
409
 
28468 tejbeer 410
			}
30279 tejbeer 411
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 412
 
30279 tejbeer 413
			Period age = Period.between(startDate, endDate);
414
			int months = age.getMonths();
415
			LOGGER.info("months" + months);
416
			LOGGER.info("brandWiseLms" + brandWiseLms);
417
			LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 418
 
30279 tejbeer 419
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 420
 
30279 tejbeer 421
			for (String brand : brands) {
422
				Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
423
				for (int i = months; i >= 0; i--) {
28468 tejbeer 424
 
30279 tejbeer 425
					LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
426
 
427
					if (yearMonthValue != null) {
428
						if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
429
							yearMonthValue.put(YearMonth.from(startMonth), 0.0);
430
						}
431
 
432
					} else {
433
						yearMonthValue = new HashMap<>();
434
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
435
					}
436
				}
437
 
438
				Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
439
 
440
				brandMonthValue.put(brand, sortedMonthBrandValue);
441
 
442
				sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
443
 
444
			}
445
 
446
			cm = chartService.createChart(months, sortedBrandValue, colorList, borderList, "Brand Wise Monthly Sale");
28468 tejbeer 447
		}
448
 
449
		return cm;
450
	}
451
 
452
	public String adminPanel(int fofoId, String email, Model model) throws Exception {
453
		List<Menu> menus = null;
454
		try {
455
			AuthUser authUser = authRepository.selectByEmailOrMobile(email);
456
			List<Position> positions = positionRepository.selectAll(authUser.getId());
457
			Map<Integer, PartnerDetailModel> warehousePartnerDetailMap = null;
458
 
459
			Map<Integer, WarehouseWiseStockModel> warehouseStockMap = new HashMap<>();
460
			LinkedHashMap<Integer, WarehouseWiseStockModel> warehouseStockSortedMap = new LinkedHashMap<>();
461
 
462
			Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
30449 amit.gupta 463
			Map<Integer, String> wm = new LinkedHashMap<>();
28468 tejbeer 464
 
465
			Map<AuthUser, Long> authUserTicketCount = null;
466
 
467
			for (Map.Entry<Integer, String> entry : warehouseMap.entrySet()) {
468
				wm.put(entry.getKey(), entry.getValue());
469
			}
470
 
471
			wm.put(0, "Total Values");
472
			long stockValue = 0;
473
			long stockQty = 0;
474
			long pendingIndent = 0;
475
			long tertiary = 0;
476
 
31152 tejbeer 477
			Set<Integer> fofoIds = csService.getAuthFofoIds(email);
28468 tejbeer 478
 
479
			if (fofoIds != null && fofoIds.size() > 0) {
480
				Map<Integer, List<Integer>> warehouseIdFofoIdMap = fofoStoreRepository
481
						.selectActivePartnersByRetailerIds(new ArrayList<>(fofoIds)).stream()
482
						.collect(Collectors.groupingBy(FofoStore::getWarehouseId,
483
								Collectors.mapping(FofoStore::getId, Collectors.toList())));
484
				// warehouseStock
485
				List<WarehouseWiseStockModel> warehouseStocks = saholicInventoryCISRepository
486
						.selectGroupByWarehouse(new ArrayList<>(warehouseIdFofoIdMap.keySet()));
487
 
488
				warehouseStockMap = warehouseStocks.stream().collect(Collectors.toMap(x -> x.getWarehouseId(), x -> x));
489
 
490
				if (!warehouseStockMap.isEmpty()) {
491
					for (Entry<Integer, WarehouseWiseStockModel> warehouseStock : warehouseStockMap.entrySet()) {
492
						stockValue += warehouseStock.getValue().getStockValue();
493
						stockQty += warehouseStock.getValue().getStockQty();
494
						pendingIndent += warehouseStock.getValue().getPendingIndent();
495
						tertiary += warehouseStock.getValue().getTertiary();
496
					}
497
					WarehouseWiseStockModel ws = new WarehouseWiseStockModel();
498
					ws.setStockQty(stockQty);
499
					ws.setStockValue(stockValue);
500
					ws.setPendingIndent(pendingIndent);
501
					ws.setTertiary(tertiary);
502
					ws.setWarehouseId(0);
503
					warehouseStockMap.put(0, ws);
504
 
505
				}
506
 
507
				warehouseStockMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
508
						.forEachOrdered(x -> warehouseStockSortedMap.put(x.getKey(), x.getValue()));
509
 
30449 amit.gupta 510
				ReporticoCacheTable rctSaholic = reporticoCacheTableRepository.selectByTableName("SaholicInventoryCIS");
511
				model.addAttribute("reporticoDate", rctSaholic);
28468 tejbeer 512
 
513
				// warehouseStock
514
				warehousePartnerDetailMap = this.getWarehousePartnerDetail(warehouseIdFofoIdMap);
30449 amit.gupta 515
				ReporticoCacheTable rctPartneStat = reporticoCacheTableRepository.selectByTableName("partnerStat");
28468 tejbeer 516
 
517
				Set<CustomRetailer> positionRetailers = fofoIds.stream()
518
						.map(x -> retailerService.getAllFofoRetailers().get(x)).filter(x -> x != null)
519
						.collect(Collectors.toSet());
520
				model.addAttribute("retailers", gson.toJson(positionRetailers));
521
				model.addAttribute("reporticoProjectMap", ReporticoProject.salesReporticoProjectMap);
522
				model.addAttribute("warehouses", getWarehouses(positionRetailers));
30449 amit.gupta 523
				model.addAttribute("rctPartneStat", rctPartneStat);
28468 tejbeer 524
			} else {
525
				List<Position> warehousePositions = positions.stream()
526
						.filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_WAREHOUSE)
527
						.collect(Collectors.toList());
528
				if (warehousePositions.size() > 0) {
529
					Set<CustomRetailer> positionRetailers = new HashSet<>();
530
					csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
531
						positionRetailers.addAll(customRetailers);
532
					});
533
					model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
534
					model.addAttribute("retailers", gson.toJson(positionRetailers));
535
					model.addAttribute("warehouses", getWarehouses(positionRetailers));
536
				}
30056 amit.gupta 537
				List<Position> categoryPositions = positions.stream()
538
						.filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_CATEGORY)
539
						.collect(Collectors.toList());
540
				if (categoryPositions.size() > 0) {
541
					Set<CustomRetailer> positionRetailers = new HashSet<>();
542
					csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
543
						positionRetailers.addAll(customRetailers);
544
					});
545
					model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
546
					model.addAttribute("retailers", gson.toJson(positionRetailers));
547
					model.addAttribute("warehouses", getWarehouses(positionRetailers));
548
				}
28468 tejbeer 549
			}
550
 
551
			if (positions.size() > 0) {
552
				if (positions.stream()
553
						.filter(x -> x.getEscalationType().equals(EscalationType.L3)
554
								|| x.getEscalationType().equals(EscalationType.L4)
555
								|| x.getEscalationType().equals(EscalationType.L5))
556
						.count() > 0) {
557
					authUserTicketCount = ticketRepository.selectAllAuthUserTicketCount(Optional.of(false));
558
				}
559
			}
560
 
31130 tejbeer 561
			if (Arrays.asList("amit.gupta@smartdukaan.com", "tejbeer.kaur@smartdukaan.com",
562
					"kanan.choudhary@smartdukaan.com").contains(email)) {
28468 tejbeer 563
				menus = menuRepository.selectAll();
564
			} else if (positions.size() > 0) {
565
				if (positions.stream().filter(x -> x.getEscalationType().equals(EscalationType.L5)).count() > 0) {
566
					menus = menuRepository.selectAll();
567
				} else {
568
					List<Integer> menuIds = menuCategoryRepository.selectAllByPositions(positions).stream()
569
							.map(x -> x.getMenuId()).collect(Collectors.toList());
570
					if (menuIds.size() > 0) {
571
						menus = menuRepository.selectAllByIds(menuIds);
572
					}
573
				}
574
			}
575
 
576
			model.addAttribute("authId", authUser.getId());
577
 
578
			model.addAttribute("warehousePartnerDetailMap", warehousePartnerDetailMap);
579
			model.addAttribute("warehouseMap", wm);
580
			model.addAttribute("authUserTicketCount", authUserTicketCount);
581
			model.addAttribute("warehouseStockMap", warehouseStockSortedMap);
582
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
583
 
584
			model.addAttribute("date", curDate.withDayOfMonth(1).minusMonths(6).toLocalDate());
585
 
586
		} catch (ProfitMandiBusinessException e) {
587
		}
588
		List<Menu> menuList = (menus != null) ? this.prepareMenu(menus) : new ArrayList<>();
589
		LOGGER.info("menu" + menuList);
590
		model.addAttribute("menu", menuList);
591
		return "admin";
592
	}
593
 
594
	private List<Menu> prepareMenu(List<Menu> menus) {
595
		List<Menu> returnMenu = new ArrayList<>();
30268 amit.gupta 596
		Map<Menu, List<Menu>> subMenuMap = new HashMap<>();
28468 tejbeer 597
		for (Menu menu : menus) {
598
			if (menu.get_parent() == null) {
599
				if (!subMenuMap.containsKey(menu)) {
600
					subMenuMap.put(menu, new ArrayList<>());
601
				}
602
			} else {
603
				Menu parentMenu = menu.get_parent();
604
				if (!subMenuMap.containsKey(parentMenu)) {
605
					subMenuMap.put(parentMenu, new ArrayList<>());
606
				}
607
				subMenuMap.get(parentMenu).add(menu);
608
			}
609
		}
610
		subMenuMap.entrySet().stream().forEach(entry -> {
611
			entry.getKey().setSubMenus(entry.getValue());
612
			returnMenu.add(entry.getKey());
613
		});
614
		return returnMenu;
615
	}
616
 
617
	public Map<Integer, PartnerDetailModel> getWarehousePartnerDetail(Map<Integer, List<Integer>> warehousePartnerMap)
618
			throws Exception {
619
 
620
		Map<Integer, PartnerDetailModel> warehouseIdAndallValues = new LinkedHashMap<>();
621
		// Map<Integer, List<FofoStore>> warehousePartnerMap =
622
		// fofoStoreRepository.getWarehousePartnerMap();
623
		Map<Integer, PartnerDetailModel> partnerStats = this.getPartnersStatDataFromFile();
624
		List<Integer> allfofoIds = new ArrayList<>();
625
		if (partnerStats != null) {
626
			for (Entry<Integer, List<Integer>> warehouse : warehousePartnerMap.entrySet()) {
30246 amit.gupta 627
				List<Integer> fofoIds = warehouse.getValue().stream().collect(Collectors.toList());
28468 tejbeer 628
				allfofoIds.addAll(fofoIds);
629
				List<PartnerDetailModel> partnerDetails = fofoIds.stream().map(x -> partnerStats.get(x))
630
						.collect(Collectors.toList());
631
				if (partnerDetails != null && !partnerDetails.isEmpty()) {
632
					PartnerDetailModel partnerDetailModel = partnerStatsService.getAggregateStats(partnerDetails);
633
					warehouseIdAndallValues.put(warehouse.getKey(), partnerDetailModel);
634
				}
635
 
636
			}
637
			List<PartnerDetailModel> allPartnerDetails = allfofoIds.stream().map(x -> partnerStats.get(x))
638
					.collect(Collectors.toList());
639
			LOGGER.info("allPartnerDetails" + allPartnerDetails);
640
			PartnerDetailModel partnerDetailModel = partnerStatsService
641
					.getAggregateStats(new ArrayList<>(allPartnerDetails));
30268 amit.gupta 642
			warehouseIdAndallValues.put(0, partnerDetailModel);
28468 tejbeer 643
		}
644
		return warehouseIdAndallValues;
645
	}
646
 
647
	public Map<Integer, PartnerDetailModel> getPartnersStatDataFromFile() throws Exception {
648
		ObjectInputStream objectinputstream = null;
649
		Map<Integer, PartnerDetailModel> partnerStat = null;
650
		try {
651
			FileInputStream streamIn = new FileInputStream("/tmp/partnerStat.tmp");
652
			objectinputstream = new ObjectInputStream(streamIn);
653
			partnerStat = (Map<Integer, PartnerDetailModel>) objectinputstream.readObject();
654
 
655
			LOGGER.info("partnerStat" + partnerStat);
656
			objectinputstream.close();
657
 
658
		} catch (Exception e) {
659
			LOGGER.info("exceptionddd" + e);
660
 
661
			e.printStackTrace();
662
 
663
		} finally {
664
			if (objectinputstream != null) {
665
				objectinputstream.close();
666
				LOGGER.info("clofddd" + partnerStat);
667
 
668
			}
669
		}
670
		return partnerStat;
671
 
672
	}
673
 
674
	private String getWarehouses(Set<CustomRetailer> positionRetailers) {
675
		Map<Integer, String> warehouses = new HashMap<>();
676
		positionRetailers.stream().forEach(x -> {
677
			if (x.getWarehouseId() != 0) {
678
				warehouses.put(x.getWarehouseId(), ProfitMandiConstants.WAREHOUSE_MAP.get(x.getWarehouseId()));
679
			}
680
		});
681
		return gson.toJson(warehouses);
682
 
683
	}
684
 
685
}