Subversion Repositories SmartDukaan

Rev

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