Subversion Repositories SmartDukaan

Rev

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