Subversion Repositories SmartDukaan

Rev

Rev 34665 | Rev 35189 | 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 {
33071 amit.gupta 53
    @Autowired
54
    private Gson gson;
28468 tejbeer 55
 
33071 amit.gupta 56
    @Autowired
57
    private OrderRepository orderRepository;
28468 tejbeer 58
 
33071 amit.gupta 59
    @Autowired
60
    TicketRepository ticketRepository;
28468 tejbeer 61
 
33071 amit.gupta 62
    @Autowired
63
    private ChartService chartService;
28468 tejbeer 64
 
33071 amit.gupta 65
    @Autowired
66
    RetailerService retailerService;
28468 tejbeer 67
 
33071 amit.gupta 68
    @Autowired
69
    AuthRepository authRepository;
28468 tejbeer 70
 
33071 amit.gupta 71
    @Autowired
72
    FofoStoreRepository fofoStoreRepository;
28468 tejbeer 73
 
33071 amit.gupta 74
    @Autowired
75
    CsService1 csService1;
31662 amit.gupta 76
 
33071 amit.gupta 77
    @Autowired
78
    CsService csService;
28468 tejbeer 79
 
33071 amit.gupta 80
    @Autowired
81
    private MenuRepository menuRepository;
28468 tejbeer 82
 
33071 amit.gupta 83
    @Autowired
84
    private PositionRepository positionRepository;
28468 tejbeer 85
 
33071 amit.gupta 86
    @Autowired
87
    private MenuCategoryRepository menuCategoryRepository;
28468 tejbeer 88
 
33071 amit.gupta 89
    @Autowired
90
    private SaholicInventoryCISRepository saholicInventoryCISRepository;
28468 tejbeer 91
 
33071 amit.gupta 92
    @Autowired
93
    private ReporticoCacheTableRepository reporticoCacheTableRepository;
28468 tejbeer 94
 
33071 amit.gupta 95
    @Autowired
96
    private ActivatedImeiRepository activatedImeiRepository;
28825 tejbeer 97
 
33071 amit.gupta 98
    @Autowired
99
    private PartnerStatsService partnerStatsService;
100
    @Autowired
101
    private SDCreditService sdCreditService;
28468 tejbeer 102
 
33071 amit.gupta 103
    List<String> colorList = Arrays.asList("mediumseagreen", "dodgerblue", "darkblue", "gold", "#eb0029", "coral",
104
            "#63C5DA", "red", "deeppink", "midnightblue", "cornsilk");
28468 tejbeer 105
 
33071 amit.gupta 106
    List<String> borderList = Arrays.asList("lawngreen", "lightblue", "#0000cd", "#f7e98e", "#eb0029", "lightcoral",
107
            "#0000cd", "lightsalmon", "pink", "#0000cd", "cornsilk");
28468 tejbeer 108
 
33071 amit.gupta 109
    List<String> brands = Arrays.asList("Oppo", "Vivo", "Samsung", "Realme", "OnePlus", "Xiaomi", "Tecno", "Itel",
110
            "Lava", "Nokia");
28468 tejbeer 111
 
33071 amit.gupta 112
    private static final Logger LOGGER = LogManager.getLogger(AdminUser.class);
28468 tejbeer 113
 
33071 amit.gupta 114
    public ChartModel getBrandWiseLmp(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
115
                                      LocalDate endDate, String filterType) {
33072 amit.gupta 116
        //LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 117
 
33071 amit.gupta 118
        List<BrandWiseModel> brandWiseLms = orderRepository.selectGroupByBrandLmp(fofoIds, warehouseIds, startDate,
119
                endDate, filterType);
120
        LOGGER.info("brandWiseLms" + brandWiseLms);
121
        ChartModel cm = null;
28468 tejbeer 122
 
33071 amit.gupta 123
        if (filterType.equals("dateWise")) {
124
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 125
 
33071 amit.gupta 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());
133
            }
28468 tejbeer 134
 
33071 amit.gupta 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
 
33071 amit.gupta 140
            Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 141
 
33071 amit.gupta 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
                }
30279 tejbeer 152
 
33071 amit.gupta 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());
156
            }
30279 tejbeer 157
 
33071 amit.gupta 158
            LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
30279 tejbeer 159
 
33071 amit.gupta 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");
30279 tejbeer 165
 
33071 amit.gupta 166
        } else {
30279 tejbeer 167
 
33071 amit.gupta 168
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
30279 tejbeer 169
 
33071 amit.gupta 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 {
30279 tejbeer 178
 
33071 amit.gupta 179
                    yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
180
                            (double) bwl.getAmount());
30279 tejbeer 181
 
33071 amit.gupta 182
                }
183
                brandMonthValue.put(bwl.getBrand(), yearMonthValue);
30279 tejbeer 184
 
33071 amit.gupta 185
            }
186
            LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 187
 
33071 amit.gupta 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
 
33071 amit.gupta 194
            Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 195
 
33071 amit.gupta 196
            for (String brand : brands) {
197
                Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
198
                for (int i = months; i >= 0; i--) {
28468 tejbeer 199
 
33071 amit.gupta 200
                    LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
28468 tejbeer 201
 
33071 amit.gupta 202
                    if (yearMonthValue != null) {
203
                        if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
204
                            yearMonthValue.put(YearMonth.from(startMonth), 0.0);
205
                        }
30279 tejbeer 206
 
33071 amit.gupta 207
                    } else {
208
                        yearMonthValue = new HashMap<>();
209
                        yearMonthValue.put(YearMonth.from(startMonth), 0.0);
210
                    }
211
                }
30279 tejbeer 212
 
33071 amit.gupta 213
                Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
30279 tejbeer 214
 
33071 amit.gupta 215
                brandMonthValue.put(brand, sortedMonthBrandValue);
30279 tejbeer 216
 
33071 amit.gupta 217
                sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
30279 tejbeer 218
 
33071 amit.gupta 219
            }
30279 tejbeer 220
 
33071 amit.gupta 221
            cm = chartService.createChart(months, sortedBrandValue, colorList, borderList,
222
                    "Brand Wise Monthly Purchase");
30279 tejbeer 223
 
33071 amit.gupta 224
        }
30279 tejbeer 225
 
33071 amit.gupta 226
        return cm;
227
    }
28468 tejbeer 228
 
33071 amit.gupta 229
    public ChartInvestmentModel getAllStatePartnerType(Map<Integer, PartnerDetailModel> fofoIdAndallValues)
230
            throws ProfitMandiBusinessException {
29165 manish 231
 
33071 amit.gupta 232
        Map<String, Float> partnerTypeCount = new HashMap<>();
29165 manish 233
 
33071 amit.gupta 234
        List<PartnerType> partnerTypes = Arrays.asList(PartnerType.values());
29165 manish 235
 
33071 amit.gupta 236
        for (PartnerType partnerType : partnerTypes) {
29165 manish 237
 
33071 amit.gupta 238
            float count = fofoIdAndallValues.entrySet().stream()
239
                    .filter(x -> x.getValue().getPartnerType().equals(partnerType)).count();
29165 manish 240
 
33071 amit.gupta 241
            partnerTypeCount.put(partnerType.toString(), count);
29165 manish 242
 
33071 amit.gupta 243
        }
29165 manish 244
 
33071 amit.gupta 245
        ChartInvestmentModel cm = new ChartInvestmentModel();
29165 manish 246
 
33071 amit.gupta 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);
29165 manish 250
 
33071 amit.gupta 251
        List<String> backgroundColor = new ArrayList<>();
29165 manish 252
 
33071 amit.gupta 253
        List<Float> values = new ArrayList<>();
29165 manish 254
 
33071 amit.gupta 255
        for (String label : labelList) {
256
            values.add(partnerTypeCount.get(label));
257
            if (label.equals("BRONZE")) {
258
                backgroundColor.add("#CD7F32");
259
            }
260
            if (label.equals("SILVER")) {
261
                backgroundColor.add("silver");
262
            }
263
            if (label.equals("GOLD")) {
264
                backgroundColor.add("#FFD700");
265
            }
266
            if (label.equals("DIAMOND")) {
267
                backgroundColor.add("#B9F2FF");
268
            }
29165 manish 269
 
33071 amit.gupta 270
            if (label.equals("PLATINUM")) {
271
                backgroundColor.add("#800080");
272
            }
273
            if (label.equals("NEW")) {
274
                backgroundColor.add("green");
275
            }
29165 manish 276
 
33071 amit.gupta 277
        }
29165 manish 278
 
33071 amit.gupta 279
        LOGGER.info("valuesPartnerType" + values);
280
        LOGGER.info("partnerTypeCount" + partnerTypeCount);
29165 manish 281
 
33071 amit.gupta 282
        Data data = new Data();
283
        data.setData(values);
284
        data.setBackgroundColor(backgroundColor);
285
        data.setLabel("DataSet 1");
29165 manish 286
 
33071 amit.gupta 287
        PieLables label = new PieLables();
288
        label.setFontColor("black");
289
        label.setFontSize(22);
29165 manish 290
 
33071 amit.gupta 291
        Legend legend = new Legend();
292
        legend.setLabels(label);
293
        legend.setPosition("left");
29165 manish 294
 
33071 amit.gupta 295
        List<Data> dataList = new ArrayList<>();
296
        dataList.add(data);
29165 manish 297
 
33071 amit.gupta 298
        DataInvestmentModel datasets = new DataInvestmentModel();
299
        datasets.setDatasets(dataList);
300
        datasets.setLabels(labels);
29165 manish 301
 
33071 amit.gupta 302
        OptionModel om = new OptionModel();
303
        om.setLegend(legend);
304
        cm.setType("pie");
305
        cm.setData(datasets);
306
        cm.setOptions(om);
29165 manish 307
 
33071 amit.gupta 308
        return cm;
309
    }
29165 manish 310
 
33071 amit.gupta 311
    public Map<String, Object> getFilter(int warehouseId, String email, LocalDateTime date, LocalDateTime endDate)
312
            throws ProfitMandiBusinessException {
313
        Map<String, Object> map = new HashMap<>();
314
        Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
315
        Map<Integer, CustomRetailer> crm = null;
316
        Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
28468 tejbeer 317
 
33071 amit.gupta 318
        Set<Integer> fofoIds = csService1.getAuthFofoIds(email, true);
28468 tejbeer 319
 
33071 amit.gupta 320
        if (warehouseId != 0) {
28468 tejbeer 321
 
33071 amit.gupta 322
            fofoIds = fofoStoreRepository.selectPartnerByfofoIdAndWarehouse(new ArrayList<>(fofoIds), warehouseId)
323
                    .stream().map(x -> x).collect(Collectors.toSet());
28468 tejbeer 324
 
33071 amit.gupta 325
        }
28468 tejbeer 326
 
33071 amit.gupta 327
        if (fofoIds != null) {
28468 tejbeer 328
 
33071 amit.gupta 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));
30426 tejbeer 331
 
33071 amit.gupta 332
        }
28468 tejbeer 333
 
33071 amit.gupta 334
        map.put("date", date);
335
        map.put("endDate", endDate);
336
        map.put("customRetailersMap", crm);
337
        map.put("fofoIds", fofoIds);
338
        map.put("warehouseMap", warehouseMap);
28468 tejbeer 339
 
33071 amit.gupta 340
        return map;
28468 tejbeer 341
 
33071 amit.gupta 342
    }
28468 tejbeer 343
 
33071 amit.gupta 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
 
33071 amit.gupta 348
        List<BrandWiseModel> brandWiseLms = fofoStoreRepository.selectGroupByBrandLms(fofoIds, warehouseIds, startDate,
349
                endDate, filterType);
350
        //LOGGER.info("brandWiseLms" + brandWiseLms);
28468 tejbeer 351
 
33071 amit.gupta 352
        ChartModel cm = null;
28468 tejbeer 353
 
33071 amit.gupta 354
        if (filterType.equals("dateWise")) {
355
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 356
 
33071 amit.gupta 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
 
33071 amit.gupta 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);
30279 tejbeer 370
 
33071 amit.gupta 371
            Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
30279 tejbeer 372
 
33071 amit.gupta 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
                }
30279 tejbeer 383
 
33071 amit.gupta 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());
387
            }
28468 tejbeer 388
 
33071 amit.gupta 389
            LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
28468 tejbeer 390
 
33071 amit.gupta 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
 
33071 amit.gupta 397
        } else {
28468 tejbeer 398
 
33071 amit.gupta 399
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
28468 tejbeer 400
 
33071 amit.gupta 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
 
33071 amit.gupta 410
                    yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
411
                            (double) bwl.getAmount());
28468 tejbeer 412
 
33071 amit.gupta 413
                }
414
                brandMonthValue.put(bwl.getBrand(), yearMonthValue);
30279 tejbeer 415
 
33071 amit.gupta 416
            }
417
            LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 418
 
33071 amit.gupta 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
 
33071 amit.gupta 425
            Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 426
 
33071 amit.gupta 427
            for (String brand : brands) {
428
                Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
429
                for (int i = months; i >= 0; i--) {
28468 tejbeer 430
 
33071 amit.gupta 431
                    LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
30279 tejbeer 432
 
33071 amit.gupta 433
                    if (yearMonthValue != null) {
434
                        if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
435
                            yearMonthValue.put(YearMonth.from(startMonth), 0.0);
436
                        }
30279 tejbeer 437
 
33071 amit.gupta 438
                    } else {
439
                        yearMonthValue = new HashMap<>();
440
                        yearMonthValue.put(YearMonth.from(startMonth), 0.0);
441
                    }
442
                }
30279 tejbeer 443
 
33071 amit.gupta 444
                Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
30279 tejbeer 445
 
33071 amit.gupta 446
                brandMonthValue.put(brand, sortedMonthBrandValue);
30279 tejbeer 447
 
33071 amit.gupta 448
                sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
30279 tejbeer 449
 
33071 amit.gupta 450
            }
30279 tejbeer 451
 
33071 amit.gupta 452
            cm = chartService.createChart(months, sortedBrandValue, colorList, borderList, "Brand Wise Monthly Sale");
453
        }
28468 tejbeer 454
 
33071 amit.gupta 455
        return cm;
456
    }
28468 tejbeer 457
 
33071 amit.gupta 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());
34779 ranu 463
            LOGGER.info("positionslist - {}", positions);
33071 amit.gupta 464
            Map<Integer, PartnerDetailModel> warehousePartnerDetailMap = null;
28468 tejbeer 465
 
33071 amit.gupta 466
            Map<Integer, WarehouseWiseStockModel> warehouseStockMap = new HashMap<>();
467
            LinkedHashMap<Integer, WarehouseWiseStockModel> warehouseStockSortedMap = new LinkedHashMap<>();
28468 tejbeer 468
 
33071 amit.gupta 469
            Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
470
            Map<Integer, String> wm = new LinkedHashMap<>();
28468 tejbeer 471
 
33071 amit.gupta 472
            Map<AuthUser, Long> authUserTicketCount = null;
473
            LoanSummary loanSummary = sdCreditService.getLoanSummary();
474
            model.addAttribute("loanSummary", loanSummary);
28468 tejbeer 475
 
32172 jai.hind 476
 
33071 amit.gupta 477
            for (Map.Entry<Integer, String> entry : warehouseMap.entrySet()) {
478
                wm.put(entry.getKey(), entry.getValue());
479
            }
28468 tejbeer 480
 
33071 amit.gupta 481
            wm.put(0, "Total Values");
482
            long stockValue = 0;
483
            long stockQty = 0;
484
            long pendingIndent = 0;
485
            long tertiary = 0;
28468 tejbeer 486
 
34779 ranu 487
            boolean isAboveL1 = positions.stream().anyMatch(pos -> pos.getEscalationType() != EscalationType.L1);
488
            boolean isRBM = positions.stream().anyMatch(pos -> pos.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_RBM);
28468 tejbeer 489
 
34779 ranu 490
            Set<Integer> fofoIds = new HashSet<>();
491
            if (isAboveL1 && isRBM) {
492
                fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toSet());
493
            } else {
494
                fofoIds = csService1.getAuthFofoIds(email, true);
495
            }
496
 
497
 
33071 amit.gupta 498
            if (fofoIds != null && fofoIds.size() > 0) {
499
                Map<Integer, List<Integer>> warehouseIdFofoIdMap = fofoStoreRepository
500
                        .selectActivePartnersByRetailerIds(new ArrayList<>(fofoIds)).stream()
501
                        .collect(Collectors.groupingBy(FofoStore::getWarehouseId,
502
                                Collectors.mapping(FofoStore::getId, Collectors.toList())));
503
                // warehouseStock
504
                List<WarehouseWiseStockModel> warehouseStocks = saholicInventoryCISRepository
505
                        .selectGroupByWarehouse(new ArrayList<>(warehouseIdFofoIdMap.keySet()));
28468 tejbeer 506
 
33071 amit.gupta 507
                warehouseStockMap = warehouseStocks.stream().collect(Collectors.toMap(x -> x.getWarehouseId(), x -> x));
28468 tejbeer 508
 
33071 amit.gupta 509
                if (!warehouseStockMap.isEmpty()) {
510
                    for (Entry<Integer, WarehouseWiseStockModel> warehouseStock : warehouseStockMap.entrySet()) {
511
                        stockValue += warehouseStock.getValue().getStockValue();
512
                        stockQty += warehouseStock.getValue().getStockQty();
513
                        pendingIndent += warehouseStock.getValue().getPendingIndent();
514
                        tertiary += warehouseStock.getValue().getTertiary();
515
                    }
516
                    WarehouseWiseStockModel ws = new WarehouseWiseStockModel();
517
                    ws.setStockQty(stockQty);
518
                    ws.setStockValue(stockValue);
519
                    ws.setPendingIndent(pendingIndent);
520
                    ws.setTertiary(tertiary);
521
                    ws.setWarehouseId(0);
522
                    warehouseStockMap.put(0, ws);
28468 tejbeer 523
 
33071 amit.gupta 524
                }
28468 tejbeer 525
 
33071 amit.gupta 526
                warehouseStockMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
527
                        .forEachOrdered(x -> warehouseStockSortedMap.put(x.getKey(), x.getValue()));
28468 tejbeer 528
 
33071 amit.gupta 529
                ReporticoCacheTable rctSaholic = reporticoCacheTableRepository.selectByTableName("SaholicInventoryCIS");
530
                model.addAttribute("reporticoDate", rctSaholic);
28468 tejbeer 531
 
33071 amit.gupta 532
                // warehouseStock
533
                warehousePartnerDetailMap = this.getWarehousePartnerDetail(warehouseIdFofoIdMap);
31259 tejbeer 534
 
33071 amit.gupta 535
                ReporticoCacheTable rctPartneStat = reporticoCacheTableRepository.selectByTableName("partnerStat");
28468 tejbeer 536
 
33071 amit.gupta 537
                Set<CustomRetailer> positionRetailers = fofoIds.stream()
33248 ranu 538
                        .map(x -> {
539
                            try {
540
                                return retailerService.getAllFofoRetailers().get(x);
541
                            } catch (ProfitMandiBusinessException e) {
542
                                throw new RuntimeException(e);
543
                            }
544
                        }).filter(x -> x != null)
33071 amit.gupta 545
                        .collect(Collectors.toSet());
546
                model.addAttribute("retailers", gson.toJson(positionRetailers));
547
                model.addAttribute("reporticoProjectMap", ReporticoProject.salesReporticoProjectMap);
548
                model.addAttribute("warehouses", getWarehouses(positionRetailers));
549
                model.addAttribute("rctPartneStat", rctPartneStat);
550
            } else {
551
                List<Position> warehousePositions = positions.stream()
552
                        .filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_WAREHOUSE)
553
                        .collect(Collectors.toList());
554
                if (warehousePositions.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
                }
563
                List<Position> categoryPositions = positions.stream()
564
                        .filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_CATEGORY)
565
                        .collect(Collectors.toList());
566
                if (categoryPositions.size() > 0) {
567
                    Set<CustomRetailer> positionRetailers = new HashSet<>();
568
                    csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
569
                        positionRetailers.addAll(customRetailers);
570
                    });
571
                    model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
572
                    model.addAttribute("retailers", gson.toJson(positionRetailers));
573
                    model.addAttribute("warehouses", getWarehouses(positionRetailers));
574
                }
575
            }
28468 tejbeer 576
 
33071 amit.gupta 577
            if (positions.size() > 0) {
578
                if (positions.stream()
579
                        .filter(x -> x.getEscalationType().equals(EscalationType.L3)
580
                                || x.getEscalationType().equals(EscalationType.L4)
581
                                || x.getEscalationType().equals(EscalationType.L5))
582
                        .count() > 0) {
583
                    authUserTicketCount = ticketRepository.selectAllAuthUserTicketCount(Optional.of(false));
584
                }
585
            }
28468 tejbeer 586
 
33071 amit.gupta 587
            if (Arrays.asList("amit.gupta@smartdukaan.com", "ranu.rajput@smartdukaan.com",
34152 tejus.loha 588
                    "vikas.jangra@smartdukaan.com", "tejus.lohani@smartdukaan.com","aman.gupta@smartdukaan.com").contains(email)) {
33071 amit.gupta 589
                menus = menuRepository.selectAllBySequence();
590
            } else if (positions.size() > 0) {
591
                if (positions.stream().filter(x -> x.getEscalationType().equals(EscalationType.L5)).count() > 0) {
592
                    menus = menuRepository.selectAllBySequence();
593
                } else {
594
                    List<Integer> menuIds = menuCategoryRepository.selectAllByPositions(positions).stream()
595
                            .map(x -> x.getMenuId()).collect(Collectors.toList());
596
                    if (menuIds.size() > 0) {
597
                        menus = menuRepository.selectAllByIds(menuIds);
598
                    }
599
                }
600
            }
28468 tejbeer 601
 
33071 amit.gupta 602
            model.addAttribute("authId", authUser.getId());
28468 tejbeer 603
 
33071 amit.gupta 604
            model.addAttribute("warehousePartnerDetailMap", warehousePartnerDetailMap);
605
            model.addAttribute("warehouseMap", wm);
606
            model.addAttribute("authUserTicketCount", authUserTicketCount);
607
            model.addAttribute("warehouseStockMap", warehouseStockSortedMap);
608
            LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 609
 
33071 amit.gupta 610
            model.addAttribute("date", curDate.withDayOfMonth(1).minusMonths(6).toLocalDate());
28468 tejbeer 611
 
33071 amit.gupta 612
        } catch (ProfitMandiBusinessException e) {
613
        }
614
        List<Menu> menuList = (menus != null) ? this.prepareMenu(menus) : new ArrayList<>();
33072 amit.gupta 615
        //LOGGER.info("menu" + menuList);
33071 amit.gupta 616
        model.addAttribute("menu", menuList);
617
        return "admin";
618
    }
28468 tejbeer 619
 
33071 amit.gupta 620
    private List<Menu> prepareMenu(List<Menu> menus) {
621
        List<Menu> returnMenu = new ArrayList<>();
622
        Map<Menu, List<Menu>> subMenuMap = new HashMap<>();
623
        for (Menu menu : menus) {
624
            if (menu.get_parent() == null) {
625
                if (!subMenuMap.containsKey(menu)) {
626
                    subMenuMap.put(menu, new ArrayList<>());
627
                }
628
            } else {
629
                Menu parentMenu = menu.get_parent();
630
                if (!subMenuMap.containsKey(parentMenu)) {
631
                    subMenuMap.put(parentMenu, new ArrayList<>());
632
                }
633
                subMenuMap.get(parentMenu).add(menu);
634
            }
635
        }
636
        subMenuMap.entrySet().stream().forEach(entry -> {
637
            entry.getKey().setSubMenus(entry.getValue());
638
            returnMenu.add(entry.getKey());
639
        });
640
        return returnMenu;
641
    }
28468 tejbeer 642
 
33071 amit.gupta 643
    public Map<Integer, PartnerDetailModel> getWarehousePartnerDetail(Map<Integer, List<Integer>> warehousePartnerMap)
644
            throws Exception {
28468 tejbeer 645
 
33071 amit.gupta 646
        Map<Integer, PartnerDetailModel> warehouseIdAndallValues = new LinkedHashMap<>();
647
        // Map<Integer, List<FofoStore>> warehousePartnerMap =
648
        // fofoStoreRepository.getWarehousePartnerMap();
649
        Map<Integer, PartnerDetailModel> partnerStats = this.getPartnersStatDataFromFile();
650
        List<Integer> allfofoIds = new ArrayList<>();
651
        if (partnerStats != null) {
652
            for (Entry<Integer, List<Integer>> warehouse : warehousePartnerMap.entrySet()) {
653
                List<Integer> fofoIds = warehouse.getValue().stream().collect(Collectors.toList());
654
                allfofoIds.addAll(fofoIds);
655
                List<PartnerDetailModel> partnerDetails = fofoIds.stream().map(x -> partnerStats.get(x))
656
                        .collect(Collectors.toList());
657
                if (partnerDetails != null && !partnerDetails.isEmpty()) {
658
                    PartnerDetailModel partnerDetailModel = partnerStatsService.getAggregateStats(partnerDetails);
659
                    warehouseIdAndallValues.put(warehouse.getKey(), partnerDetailModel);
660
                }
28468 tejbeer 661
 
33071 amit.gupta 662
            }
663
            List<PartnerDetailModel> allPartnerDetails = allfofoIds.stream().map(x -> partnerStats.get(x))
664
                    .collect(Collectors.toList());
33072 amit.gupta 665
            //LOGGER.info("allPartnerDetails" + allPartnerDetails);
33071 amit.gupta 666
            PartnerDetailModel partnerDetailModel = partnerStatsService
667
                    .getAggregateStats(new ArrayList<>(allPartnerDetails));
668
            warehouseIdAndallValues.put(0, partnerDetailModel);
669
        }
670
        return warehouseIdAndallValues;
671
    }
28468 tejbeer 672
 
33071 amit.gupta 673
    public Map<Integer, PartnerDetailModel> getPartnersStatDataFromFile() throws Exception {
674
        ObjectInputStream objectinputstream = null;
675
        Map<Integer, PartnerDetailModel> partnerStat = null;
676
        try {
34665 ranu 677
//            FileInputStream streamIn = new FileInputStream("/tmp/partnerStat.tmp");
678
            FileInputStream streamIn = new FileInputStream("/var/www/partner_stats/partnerStat.tmp");
33071 amit.gupta 679
            objectinputstream = new ObjectInputStream(streamIn);
680
            partnerStat = (Map<Integer, PartnerDetailModel>) objectinputstream.readObject();
28468 tejbeer 681
 
33071 amit.gupta 682
            objectinputstream.close();
28468 tejbeer 683
 
33071 amit.gupta 684
        } catch (Exception e) {
685
            LOGGER.info("exceptionddd" + e);
28468 tejbeer 686
 
33071 amit.gupta 687
            e.printStackTrace();
28468 tejbeer 688
 
33071 amit.gupta 689
        } finally {
690
            if (objectinputstream != null) {
691
                objectinputstream.close();
692
            }
693
        }
694
        return partnerStat;
28468 tejbeer 695
 
33071 amit.gupta 696
    }
28468 tejbeer 697
 
33071 amit.gupta 698
    private String getWarehouses(Set<CustomRetailer> positionRetailers) {
699
        Map<Integer, String> warehouses = new HashMap<>();
700
        positionRetailers.stream().forEach(x -> {
701
            if (x.getWarehouseId() != 0) {
702
                warehouses.put(x.getWarehouseId(), ProfitMandiConstants.WAREHOUSE_MAP.get(x.getWarehouseId()));
703
            }
704
        });
705
        return gson.toJson(warehouses);
28468 tejbeer 706
 
33071 amit.gupta 707
    }
28468 tejbeer 708
 
709
 
710
}