Subversion Repositories SmartDukaan

Rev

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