Subversion Repositories SmartDukaan

Rev

Rev 33071 | Rev 33248 | 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());
463
            Map<Integer, PartnerDetailModel> warehousePartnerDetailMap = null;
28468 tejbeer 464
 
33071 amit.gupta 465
            Map<Integer, WarehouseWiseStockModel> warehouseStockMap = new HashMap<>();
466
            LinkedHashMap<Integer, WarehouseWiseStockModel> warehouseStockSortedMap = new LinkedHashMap<>();
28468 tejbeer 467
 
33071 amit.gupta 468
            Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
469
            Map<Integer, String> wm = new LinkedHashMap<>();
28468 tejbeer 470
 
33071 amit.gupta 471
            Map<AuthUser, Long> authUserTicketCount = null;
472
            LoanSummary loanSummary = sdCreditService.getLoanSummary();
473
            model.addAttribute("loanSummary", loanSummary);
28468 tejbeer 474
 
32172 jai.hind 475
 
33071 amit.gupta 476
            for (Map.Entry<Integer, String> entry : warehouseMap.entrySet()) {
477
                wm.put(entry.getKey(), entry.getValue());
478
            }
28468 tejbeer 479
 
33071 amit.gupta 480
            wm.put(0, "Total Values");
481
            long stockValue = 0;
482
            long stockQty = 0;
483
            long pendingIndent = 0;
484
            long tertiary = 0;
28468 tejbeer 485
 
33071 amit.gupta 486
            Set<Integer> fofoIds = csService1.getAuthFofoIds(email, true);
28468 tejbeer 487
 
33071 amit.gupta 488
            if (fofoIds != null && fofoIds.size() > 0) {
489
                Map<Integer, List<Integer>> warehouseIdFofoIdMap = fofoStoreRepository
490
                        .selectActivePartnersByRetailerIds(new ArrayList<>(fofoIds)).stream()
491
                        .collect(Collectors.groupingBy(FofoStore::getWarehouseId,
492
                                Collectors.mapping(FofoStore::getId, Collectors.toList())));
493
                // warehouseStock
494
                List<WarehouseWiseStockModel> warehouseStocks = saholicInventoryCISRepository
495
                        .selectGroupByWarehouse(new ArrayList<>(warehouseIdFofoIdMap.keySet()));
28468 tejbeer 496
 
33071 amit.gupta 497
                warehouseStockMap = warehouseStocks.stream().collect(Collectors.toMap(x -> x.getWarehouseId(), x -> x));
28468 tejbeer 498
 
33071 amit.gupta 499
                if (!warehouseStockMap.isEmpty()) {
500
                    for (Entry<Integer, WarehouseWiseStockModel> warehouseStock : warehouseStockMap.entrySet()) {
501
                        stockValue += warehouseStock.getValue().getStockValue();
502
                        stockQty += warehouseStock.getValue().getStockQty();
503
                        pendingIndent += warehouseStock.getValue().getPendingIndent();
504
                        tertiary += warehouseStock.getValue().getTertiary();
505
                    }
506
                    WarehouseWiseStockModel ws = new WarehouseWiseStockModel();
507
                    ws.setStockQty(stockQty);
508
                    ws.setStockValue(stockValue);
509
                    ws.setPendingIndent(pendingIndent);
510
                    ws.setTertiary(tertiary);
511
                    ws.setWarehouseId(0);
512
                    warehouseStockMap.put(0, ws);
28468 tejbeer 513
 
33071 amit.gupta 514
                }
28468 tejbeer 515
 
33071 amit.gupta 516
                warehouseStockMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
517
                        .forEachOrdered(x -> warehouseStockSortedMap.put(x.getKey(), x.getValue()));
28468 tejbeer 518
 
33071 amit.gupta 519
                ReporticoCacheTable rctSaholic = reporticoCacheTableRepository.selectByTableName("SaholicInventoryCIS");
520
                model.addAttribute("reporticoDate", rctSaholic);
28468 tejbeer 521
 
33071 amit.gupta 522
                // warehouseStock
523
                warehousePartnerDetailMap = this.getWarehousePartnerDetail(warehouseIdFofoIdMap);
31259 tejbeer 524
 
33071 amit.gupta 525
                ReporticoCacheTable rctPartneStat = reporticoCacheTableRepository.selectByTableName("partnerStat");
28468 tejbeer 526
 
33071 amit.gupta 527
                Set<CustomRetailer> positionRetailers = fofoIds.stream()
528
                        .map(x -> retailerService.getAllFofoRetailers().get(x)).filter(x -> x != null)
529
                        .collect(Collectors.toSet());
530
                model.addAttribute("retailers", gson.toJson(positionRetailers));
531
                model.addAttribute("reporticoProjectMap", ReporticoProject.salesReporticoProjectMap);
532
                model.addAttribute("warehouses", getWarehouses(positionRetailers));
533
                model.addAttribute("rctPartneStat", rctPartneStat);
534
            } else {
535
                List<Position> warehousePositions = positions.stream()
536
                        .filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_WAREHOUSE)
537
                        .collect(Collectors.toList());
538
                if (warehousePositions.size() > 0) {
539
                    Set<CustomRetailer> positionRetailers = new HashSet<>();
540
                    csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
541
                        positionRetailers.addAll(customRetailers);
542
                    });
543
                    model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
544
                    model.addAttribute("retailers", gson.toJson(positionRetailers));
545
                    model.addAttribute("warehouses", getWarehouses(positionRetailers));
546
                }
547
                List<Position> categoryPositions = positions.stream()
548
                        .filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_CATEGORY)
549
                        .collect(Collectors.toList());
550
                if (categoryPositions.size() > 0) {
551
                    Set<CustomRetailer> positionRetailers = new HashSet<>();
552
                    csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
553
                        positionRetailers.addAll(customRetailers);
554
                    });
555
                    model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
556
                    model.addAttribute("retailers", gson.toJson(positionRetailers));
557
                    model.addAttribute("warehouses", getWarehouses(positionRetailers));
558
                }
559
            }
28468 tejbeer 560
 
33071 amit.gupta 561
            if (positions.size() > 0) {
562
                if (positions.stream()
563
                        .filter(x -> x.getEscalationType().equals(EscalationType.L3)
564
                                || x.getEscalationType().equals(EscalationType.L4)
565
                                || x.getEscalationType().equals(EscalationType.L5))
566
                        .count() > 0) {
567
                    authUserTicketCount = ticketRepository.selectAllAuthUserTicketCount(Optional.of(false));
568
                }
569
            }
28468 tejbeer 570
 
33071 amit.gupta 571
            if (Arrays.asList("amit.gupta@smartdukaan.com", "ranu.rajput@smartdukaan.com",
572
                    "shampa.banerjee@smartdukaan.com", "tejus.lohani@smartdukaan.com").contains(email)) {
573
                menus = menuRepository.selectAllBySequence();
574
            } else if (positions.size() > 0) {
575
                if (positions.stream().filter(x -> x.getEscalationType().equals(EscalationType.L5)).count() > 0) {
576
                    menus = menuRepository.selectAllBySequence();
577
                } else {
578
                    List<Integer> menuIds = menuCategoryRepository.selectAllByPositions(positions).stream()
579
                            .map(x -> x.getMenuId()).collect(Collectors.toList());
580
                    if (menuIds.size() > 0) {
581
                        menus = menuRepository.selectAllByIds(menuIds);
582
                    }
583
                }
584
            }
28468 tejbeer 585
 
33071 amit.gupta 586
            model.addAttribute("authId", authUser.getId());
28468 tejbeer 587
 
33071 amit.gupta 588
            model.addAttribute("warehousePartnerDetailMap", warehousePartnerDetailMap);
589
            model.addAttribute("warehouseMap", wm);
590
            model.addAttribute("authUserTicketCount", authUserTicketCount);
591
            model.addAttribute("warehouseStockMap", warehouseStockSortedMap);
592
            LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 593
 
33071 amit.gupta 594
            model.addAttribute("date", curDate.withDayOfMonth(1).minusMonths(6).toLocalDate());
28468 tejbeer 595
 
33071 amit.gupta 596
        } catch (ProfitMandiBusinessException e) {
597
        }
598
        List<Menu> menuList = (menus != null) ? this.prepareMenu(menus) : new ArrayList<>();
33072 amit.gupta 599
        //LOGGER.info("menu" + menuList);
33071 amit.gupta 600
        model.addAttribute("menu", menuList);
601
        return "admin";
602
    }
28468 tejbeer 603
 
33071 amit.gupta 604
    private List<Menu> prepareMenu(List<Menu> menus) {
605
        List<Menu> returnMenu = new ArrayList<>();
606
        Map<Menu, List<Menu>> subMenuMap = new HashMap<>();
607
        for (Menu menu : menus) {
608
            if (menu.get_parent() == null) {
609
                if (!subMenuMap.containsKey(menu)) {
610
                    subMenuMap.put(menu, new ArrayList<>());
611
                }
612
            } else {
613
                Menu parentMenu = menu.get_parent();
614
                if (!subMenuMap.containsKey(parentMenu)) {
615
                    subMenuMap.put(parentMenu, new ArrayList<>());
616
                }
617
                subMenuMap.get(parentMenu).add(menu);
618
            }
619
        }
620
        subMenuMap.entrySet().stream().forEach(entry -> {
621
            entry.getKey().setSubMenus(entry.getValue());
622
            returnMenu.add(entry.getKey());
623
        });
624
        return returnMenu;
625
    }
28468 tejbeer 626
 
33071 amit.gupta 627
    public Map<Integer, PartnerDetailModel> getWarehousePartnerDetail(Map<Integer, List<Integer>> warehousePartnerMap)
628
            throws Exception {
28468 tejbeer 629
 
33071 amit.gupta 630
        Map<Integer, PartnerDetailModel> warehouseIdAndallValues = new LinkedHashMap<>();
631
        // Map<Integer, List<FofoStore>> warehousePartnerMap =
632
        // fofoStoreRepository.getWarehousePartnerMap();
633
        Map<Integer, PartnerDetailModel> partnerStats = this.getPartnersStatDataFromFile();
634
        List<Integer> allfofoIds = new ArrayList<>();
635
        if (partnerStats != null) {
636
            for (Entry<Integer, List<Integer>> warehouse : warehousePartnerMap.entrySet()) {
637
                List<Integer> fofoIds = warehouse.getValue().stream().collect(Collectors.toList());
638
                allfofoIds.addAll(fofoIds);
639
                List<PartnerDetailModel> partnerDetails = fofoIds.stream().map(x -> partnerStats.get(x))
640
                        .collect(Collectors.toList());
641
                if (partnerDetails != null && !partnerDetails.isEmpty()) {
642
                    PartnerDetailModel partnerDetailModel = partnerStatsService.getAggregateStats(partnerDetails);
643
                    warehouseIdAndallValues.put(warehouse.getKey(), partnerDetailModel);
644
                }
28468 tejbeer 645
 
33071 amit.gupta 646
            }
647
            List<PartnerDetailModel> allPartnerDetails = allfofoIds.stream().map(x -> partnerStats.get(x))
648
                    .collect(Collectors.toList());
33072 amit.gupta 649
            //LOGGER.info("allPartnerDetails" + allPartnerDetails);
33071 amit.gupta 650
            PartnerDetailModel partnerDetailModel = partnerStatsService
651
                    .getAggregateStats(new ArrayList<>(allPartnerDetails));
652
            warehouseIdAndallValues.put(0, partnerDetailModel);
653
        }
654
        return warehouseIdAndallValues;
655
    }
28468 tejbeer 656
 
33071 amit.gupta 657
    public Map<Integer, PartnerDetailModel> getPartnersStatDataFromFile() throws Exception {
658
        ObjectInputStream objectinputstream = null;
659
        Map<Integer, PartnerDetailModel> partnerStat = null;
660
        try {
661
            FileInputStream streamIn = new FileInputStream("/tmp/partnerStat.tmp");
662
            objectinputstream = new ObjectInputStream(streamIn);
663
            partnerStat = (Map<Integer, PartnerDetailModel>) objectinputstream.readObject();
28468 tejbeer 664
 
33071 amit.gupta 665
            objectinputstream.close();
28468 tejbeer 666
 
33071 amit.gupta 667
        } catch (Exception e) {
668
            LOGGER.info("exceptionddd" + e);
28468 tejbeer 669
 
33071 amit.gupta 670
            e.printStackTrace();
28468 tejbeer 671
 
33071 amit.gupta 672
        } finally {
673
            if (objectinputstream != null) {
674
                objectinputstream.close();
675
            }
676
        }
677
        return partnerStat;
28468 tejbeer 678
 
33071 amit.gupta 679
    }
28468 tejbeer 680
 
33071 amit.gupta 681
    private String getWarehouses(Set<CustomRetailer> positionRetailers) {
682
        Map<Integer, String> warehouses = new HashMap<>();
683
        positionRetailers.stream().forEach(x -> {
684
            if (x.getWarehouseId() != 0) {
685
                warehouses.put(x.getWarehouseId(), ProfitMandiConstants.WAREHOUSE_MAP.get(x.getWarehouseId()));
686
            }
687
        });
688
        return gson.toJson(warehouses);
28468 tejbeer 689
 
33071 amit.gupta 690
    }
28468 tejbeer 691
 
692
 
693
}