Subversion Repositories SmartDukaan

Rev

Rev 35942 | 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.MessageType;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30250 amit.gupta 6
import com.spice.profitmandi.common.model.*;
28468 tejbeer 7
import com.spice.profitmandi.dao.Interface.Campaign;
33087 amit.gupta 8
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
28468 tejbeer 9
import com.spice.profitmandi.dao.entity.dtr.Document;
10
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
11
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
12
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
13
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
14
import com.spice.profitmandi.dao.model.BrandWiseModel;
15
import com.spice.profitmandi.dao.model.SimpleCampaign;
16
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
17
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
18
import com.spice.profitmandi.dao.repository.dtr.Mongo;
30250 amit.gupta 19
import com.spice.profitmandi.dao.repository.fofo.*;
28468 tejbeer 20
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
33087 amit.gupta 21
import com.spice.profitmandi.service.catalog.BrandsService;
28468 tejbeer 22
import com.spice.profitmandi.service.inventory.InventoryService;
30250 amit.gupta 23
import org.apache.logging.log4j.LogManager;
24
import org.apache.logging.log4j.Logger;
25
import org.springframework.beans.factory.annotation.Autowired;
35595 amit 26
import org.springframework.cache.annotation.Cacheable;
30250 amit.gupta 27
import org.springframework.stereotype.Component;
28468 tejbeer 28
 
30250 amit.gupta 29
import java.time.*;
30
import java.time.format.DateTimeFormatter;
31
import java.util.*;
32
import java.util.Map.Entry;
33
import java.util.stream.Collectors;
34
 
28468 tejbeer 35
@Component
36
public class FofoUser {
37
 
31884 tejbeer 38
    @Autowired
39
    private Gson gson;
28468 tejbeer 40
 
31884 tejbeer 41
    @Autowired
42
    private PartnerInvestmentService partnerInvestmentService;
28468 tejbeer 43
 
31884 tejbeer 44
    @Autowired
45
    private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
28468 tejbeer 46
 
31884 tejbeer 47
    @Autowired
48
    ChartService chartService;
28468 tejbeer 49
 
31884 tejbeer 50
    @Autowired
51
    FofoOrderItemRepository fofoOrderItemRepository;
28468 tejbeer 52
 
31884 tejbeer 53
    @Autowired
54
    FofoOrderRepository fofoOrderRepository;
28468 tejbeer 55
 
31884 tejbeer 56
    @Autowired
57
    PartnerTypeChangeService partnerTypeChangeService;
28468 tejbeer 58
 
31884 tejbeer 59
    @Autowired
60
    PartnerTargetRepository partnerTargetRepository;
28468 tejbeer 61
 
31884 tejbeer 62
    @Autowired
63
    InventoryService inventoryService;
28468 tejbeer 64
 
31884 tejbeer 65
    @Autowired
66
    private Mongo mongoClient;
28468 tejbeer 67
 
31884 tejbeer 68
    @Autowired
69
    private OrderRepository orderRepository;
28468 tejbeer 70
 
31884 tejbeer 71
    @Autowired
72
    DocumentRepository documentRepository;
28468 tejbeer 73
 
31884 tejbeer 74
    @Autowired
33087 amit.gupta 75
    BrandsService brandsService;
76
 
77
    @Autowired
31884 tejbeer 78
    CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
28468 tejbeer 79
 
31884 tejbeer 80
    private static final Logger LOGGER = LogManager.getLogger(FofoUser.class);
28468 tejbeer 81
 
31884 tejbeer 82
    List<String> colorList = Arrays.asList("papayawhip", "mediumseagreen", "dodgerblue", "darkblue", "gold", "#eb0029", "coral", "steelblue", "red", "deeppink", "midnightblue", "cornsilk");
28468 tejbeer 83
 
31884 tejbeer 84
    List<String> borderList = Arrays.asList("pink", "lawngreen", "lightblue", "#0000cd", "#f7e98e", "#eb0029", "lightcoral", "#0000cd", "lightsalmon", "pink", "#0000cd", "cornsilk");
28468 tejbeer 85
 
31884 tejbeer 86
    List<String> brands = Arrays.asList("Accessories", "Oppo", "Vivo", "Samsung", "Realme", "Xiaomi", "OnePlus", "Tecno", "Itel", "Lava", "Nokia");
28468 tejbeer 87
 
31884 tejbeer 88
    public String format(long value) {
28468 tejbeer 89
 
35285 aman 90
        if (value >= 1000 && value < 100000) {
91
            double lakhs = value / 100000.0;
92
            double truncated = Math.floor(lakhs * 100) / 100.0;
93
            return String.format("%.2f L", truncated);
94
        }
95
 
31884 tejbeer 96
        if (value >= 100000 && value < 10000000) {
35285 aman 97
            double lakhs = value / 100000.0;
98
            double truncated = Math.floor(lakhs * 100) / 100.0;
99
            return String.format("%.2f L", truncated);
31884 tejbeer 100
        }
28468 tejbeer 101
 
35285 aman 102
        if (value >= 10000000) {
103
            double crores = value / 10000000.0;
104
            double truncated = Math.floor(crores * 100) / 100.0;
105
            return String.format("%.2f Cr", truncated);
106
        }
107
 
108
        return String.valueOf(value);
31884 tejbeer 109
    }
28468 tejbeer 110
 
35285 aman 111
 
35600 amit 112
    @Cacheable(value = "brandStockPrices", cacheManager = "redisVeryShortCacheManager")
31884 tejbeer 113
    public List<BrandStockPrice> getBrandStockPrices(int fofoId, boolean allBrands) throws Exception {
114
        Map<String, BrandStockPrice> brandStockPricesMap = inventoryService.getBrandWiseStockValue(fofoId);
115
        List<BrandStockPrice> brandStockPrices = new ArrayList<>();
33127 amit.gupta 116
        List<BrandCatalog> mobileBrands = brandsService.getBrandsToDisplay(3);
31884 tejbeer 117
        mobileBrands.stream().forEach(x -> {
33087 amit.gupta 118
            if (brandStockPricesMap.containsKey(x.getName())) {
119
                BrandStockPrice brandStockPrice = brandStockPricesMap.get(x.getName());
33119 amit.gupta 120
                brandStockPrice.setBrandUrl(x.getLogoUrl());
33087 amit.gupta 121
                brandStockPrice.setRank(x.getBrandCategory().getRank());
31884 tejbeer 122
                brandStockPrices.add(brandStockPrice);
123
            }
124
        });
28468 tejbeer 125
 
31884 tejbeer 126
        if (allBrands) {
127
            return brandStockPrices.stream().sorted(Comparator.comparingInt(BrandStockPrice::getRank)).collect(Collectors.toList());
28468 tejbeer 128
 
31884 tejbeer 129
        }
28468 tejbeer 130
 
31884 tejbeer 131
        return brandStockPrices.stream().filter(x -> x.getTotalQty() > 0).sorted(Comparator.comparingInt(BrandStockPrice::getRank)).collect(Collectors.toList());
132
    }
28468 tejbeer 133
 
35600 amit 134
    @Cacheable(value = "partnerInvestments", cacheManager = "redisVeryShortCacheManager")
31884 tejbeer 135
    public Map<String, Object> getInvestments(int fofoId) throws Exception {
136
        Map<String, Object> investments = new LinkedHashMap<>();
137
        PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 0);
138
        LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1);
139
        LocalDate yesterDate = LocalDate.now().minusDays(1);
140
        PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
141
        if (yesterdayInvestment == null) {
142
            yesterdayInvestment = new PartnerDailyInvestment();
143
        }
28468 tejbeer 144
 
31884 tejbeer 145
        List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId, currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
34527 tejus.loha 146
        boolean isInvestmentOk = partnerInvestmentService.isInvestmentOk(fofoId, ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE, ProfitMandiConstants.CUTOFF_INVESTMENT);
28468 tejbeer 147
 
35848 amit 148
        long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= ProfitMandiConstants.OK_INVESTMENT_SHORT_PERCENTAGE).collect(Collectors.counting());
34527 tejus.loha 149
        PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
35848 amit 150
        investments.put("today", investment.getOwnInvestment());
34527 tejus.loha 151
        investments.put("partnerType", partnerType);
31884 tejbeer 152
        investments.put("investment", investment);
34527 tejus.loha 153
        investments.put("minimumInvestment", investment.getMinInvestment());
35848 amit 154
        investments.put("totalInvestment", investment.getOwnInvestment());
34527 tejus.loha 155
        investments.put("isInvestmentOk", isInvestmentOk);
31884 tejbeer 156
        investments.put("inStock", investment.getInStockAmount());
157
        investments.put("minimum", investment.getMinInvestmentString());
158
        investments.put("short", investment.getShortPercentage());
159
        investments.put("activated_stock", investment.getActivatedStockAmount());
37592 amit 160
        // Apple handsets held past the aging window. Deducted from investment when the credit limit
161
        // is calculated but not from the investment figures above, so it is surfaced separately.
162
        investments.put("aged_apple_stock", investment.getAgedAppleStockAmount());
163
        investments.put("aged_apple_qty", investment.getAgedAppleQty());
31884 tejbeer 164
        investments.put("okDays", okInvestmentDays);
34527 tejus.loha 165
        investments.put("cutOf", 100-ProfitMandiConstants.CUTOFF_INVESTMENT);
166
        investments.put("minInvestment", 100-ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE);
31884 tejbeer 167
        return investments;
168
    }
28468 tejbeer 169
 
31884 tejbeer 170
    public Map<String, Object> getInvestmentsMonths(int fofoId, int month) throws Exception {
171
        Map<String, Object> investments = new LinkedHashMap<>();
172
        PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 0);
173
        LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1).minusMonths(month);
28468 tejbeer 174
 
31884 tejbeer 175
        LocalDate yesterDate = LocalDate.now().minusDays(1);
176
        PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
177
        if (yesterdayInvestment == null) {
178
            yesterdayInvestment = new PartnerDailyInvestment();
179
        }
28468 tejbeer 180
 
31884 tejbeer 181
        List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId, currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
28468 tejbeer 182
 
35848 amit 183
        long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= ProfitMandiConstants.OK_INVESTMENT_SHORT_PERCENTAGE).collect(Collectors.counting());
184
        investments.put("today", investment.getOwnInvestment());
31884 tejbeer 185
        investments.put("investment", investment);
186
        investments.put("inStock", investment.getInStockAmount());
187
        investments.put("minimum", investment.getMinInvestmentString());
188
        investments.put("short", investment.getShortPercentage());
189
        investments.put("activated_stock", investment.getActivatedStockAmount());
190
        investments.put("okDays", okInvestmentDays);
191
        return investments;
192
    }
28468 tejbeer 193
 
31884 tejbeer 194
    public ChartModel getLmsLineChart(int fofoId) throws ProfitMandiBusinessException {
28468 tejbeer 195
 
31884 tejbeer 196
        LocalDateTime curDate = LocalDate.now().atStartOfDay();
197
        Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
198
        Map<YearMonth, Map<String, Double>> monthValueMap = new HashMap<>();
28468 tejbeer 199
 
31884 tejbeer 200
        Map<String, Double> lmsBrandWiseSale = null;
28468 tejbeer 201
 
31884 tejbeer 202
        for (int i = 0; i <= 6; i++) {
28530 tejbeer 203
 
31884 tejbeer 204
            LocalDateTime startOfMonth = curDate.withDayOfMonth(1).minusMonths(i);
28468 tejbeer 205
 
31884 tejbeer 206
            LOGGER.info("startOfMonth" + startOfMonth);
28468 tejbeer 207
 
31884 tejbeer 208
            lmsBrandWiseSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(
209
                    curDate.withDayOfMonth(1).minusMonths(i), curDate.withDayOfMonth(1).minusMonths(i - 1), fofoId);
28468 tejbeer 210
 
31884 tejbeer 211
            Map<Integer, Double> accesorieslmsSale = fofoOrderRepository.selectSumSaleGroupByFofoIdsForMobileOrAccessories(fofoId, curDate.withDayOfMonth(1).minusMonths(i), curDate.withDayOfMonth(1).minusMonths(i - 1), Optional.of(false));
212
            LOGGER.info("lmsBrandWiseSale" + lmsBrandWiseSale);
28468 tejbeer 213
 
31884 tejbeer 214
            lmsBrandWiseSale.put("Accessories", accesorieslmsSale.get(fofoId));
28468 tejbeer 215
 
31884 tejbeer 216
            monthValueMap.put(YearMonth.from(startOfMonth), lmsBrandWiseSale);
217
            for (Entry<String, Double> lbw : lmsBrandWiseSale.entrySet()) {
218
                Map<YearMonth, Double> yearMonthValue = new HashMap<>();
219
                if (brandMonthValue.containsKey(lbw.getKey())) {
220
                    yearMonthValue = brandMonthValue.get(lbw.getKey());
221
                    yearMonthValue.put(YearMonth.from(startOfMonth), lbw.getValue());
222
                } else {
28468 tejbeer 223
 
31884 tejbeer 224
                    yearMonthValue.put(YearMonth.from(startOfMonth), lbw.getValue());
28468 tejbeer 225
 
31884 tejbeer 226
                }
227
                brandMonthValue.put(lbw.getKey(), yearMonthValue);
28468 tejbeer 228
 
31884 tejbeer 229
            }
28530 tejbeer 230
 
31884 tejbeer 231
        }
28468 tejbeer 232
 
31884 tejbeer 233
        LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 234
 
31884 tejbeer 235
        Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 236
 
31884 tejbeer 237
        for (String brand : brands) {
238
            Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
239
            for (int i = 6; i >= 0; i--) {
28468 tejbeer 240
 
31884 tejbeer 241
                LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
35942 amit 242
                LOGGER.debug("startMonth {}", startMonth);
28468 tejbeer 243
 
31884 tejbeer 244
                if (yearMonthValue != null) {
245
                    if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
246
                        yearMonthValue.put(YearMonth.from(startMonth), 0.0);
247
                    }
28468 tejbeer 248
 
31884 tejbeer 249
                } else {
250
                    yearMonthValue = new HashMap<>();
251
                    yearMonthValue.put(YearMonth.from(startMonth), 0.0);
252
                }
253
            }
28468 tejbeer 254
 
31884 tejbeer 255
            Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
28468 tejbeer 256
 
31884 tejbeer 257
            brandMonthValue.put(brand, sortedMonthBrandValue);
28468 tejbeer 258
 
31884 tejbeer 259
            sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
28530 tejbeer 260
 
31884 tejbeer 261
        }
28468 tejbeer 262
 
31884 tejbeer 263
        LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 264
 
31884 tejbeer 265
        LOGGER.info("sortedBrandValue" + sortedBrandValue);
28468 tejbeer 266
 
31884 tejbeer 267
        ChartModel cm = chartService.createChart(6, sortedBrandValue, colorList, borderList, "Brand Wise LMS");
28468 tejbeer 268
 
31884 tejbeer 269
        return cm;
28468 tejbeer 270
 
31884 tejbeer 271
    }
28530 tejbeer 272
 
31884 tejbeer 273
    public ChartModel getPurchaseOrderChart(int fofoId) throws ProfitMandiBusinessException {
28530 tejbeer 274
 
31884 tejbeer 275
        LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 276
 
31884 tejbeer 277
        LOGGER.info("startMonth" + curDate.withDayOfMonth(1).minusMonths(6));
28468 tejbeer 278
 
31884 tejbeer 279
        LocalDateTime startOfMonth = curDate.withDayOfMonth(1).minusMonths(1);
280
        List<BrandWiseModel> soblms = orderRepository.selectAllBilledOrderGroupByBrandFofoId(fofoId, curDate.withDayOfMonth(1).minusMonths(6));
281
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
282
        Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
283
        for (BrandWiseModel bwl : soblms) {
284
            Map<YearMonth, Double> yearMonthValue = new HashMap<>();
285
            if (brandMonthValue.containsKey(bwl.getBrand())) {
286
                yearMonthValue = brandMonthValue.get(bwl.getBrand());
287
                yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
288
            } else {
28468 tejbeer 289
 
31884 tejbeer 290
                yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
28468 tejbeer 291
 
31884 tejbeer 292
            }
293
            brandMonthValue.put(bwl.getBrand(), yearMonthValue);
28468 tejbeer 294
 
31884 tejbeer 295
        }
296
        LOGGER.info("soblms" + brandMonthValue);
28468 tejbeer 297
 
31884 tejbeer 298
        Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 299
 
31884 tejbeer 300
        for (String brand : brands) {
301
            Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
302
            for (int i = 6; i >= 0; i--) {
28468 tejbeer 303
 
31884 tejbeer 304
                LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
28468 tejbeer 305
 
31884 tejbeer 306
                if (yearMonthValue != null) {
307
                    if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
308
                        yearMonthValue.put(YearMonth.from(startMonth), 0.0);
309
                    }
28468 tejbeer 310
 
31884 tejbeer 311
                } else {
312
                    yearMonthValue = new HashMap<>();
313
                    yearMonthValue.put(YearMonth.from(startMonth), 0.0);
314
                }
315
            }
28468 tejbeer 316
 
31884 tejbeer 317
            Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
28468 tejbeer 318
 
31884 tejbeer 319
            brandMonthValue.put(brand, sortedMonthBrandValue);
28530 tejbeer 320
 
31884 tejbeer 321
            sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
28530 tejbeer 322
 
31884 tejbeer 323
        }
28468 tejbeer 324
 
31884 tejbeer 325
        LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 326
 
31884 tejbeer 327
        ChartModel cm = chartService.createChart(6, sortedBrandValue, colorList, borderList, "Brand Wise Monthly Purchase");
28468 tejbeer 328
 
31884 tejbeer 329
        return cm;
28468 tejbeer 330
 
31884 tejbeer 331
    }
28468 tejbeer 332
 
35600 amit 333
    @Cacheable(value = "partnerSales", cacheManager = "redisVeryShortCacheManager")
33245 ranu 334
    public Map<String, Object> getSales(int fofoId) throws ProfitMandiBusinessException {
28468 tejbeer 335
 
31884 tejbeer 336
        Map<String, Object> salesMap = new LinkedHashMap<>();
337
        LocalDateTime now = LocalDateTime.now();
338
        LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
339
        int monthLength = LocalDate.now().lengthOfMonth();
340
        int daysGone = now.getDayOfMonth() - 1;
341
        int daysRemaining = monthLength - daysGone;
342
        Double todaySale = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday, now, fofoId, false).get(fofoId);
343
        Double mtdSaleTillYesterDay = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday.withDayOfMonth(1), startOfToday, fofoId, false).get(fofoId);
344
        Double mtdSale = mtdSaleTillYesterDay + todaySale;
34685 aman.kumar 345
        // This matches exactly what getMonthsale() does
346
        LocalDateTime startOfLastMonth = startOfToday.withDayOfMonth(1).minusMonths(1);
347
        int currentDayOfMonth = startOfToday.getDayOfMonth();
348
        int lastMonthLength = YearMonth.from(startOfLastMonth).lengthOfMonth();
349
        LocalDateTime lmtdEndDate = startOfLastMonth.plusDays(Math.min(currentDayOfMonth, lastMonthLength));
350
 
31884 tejbeer 351
        Double lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
34685 aman.kumar 352
                startOfLastMonth, lmtdEndDate, fofoId, false).get(fofoId);
28468 tejbeer 353
 
31884 tejbeer 354
        List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
355
        if (partnerTargetDetails.isEmpty()) {
356
            partnerTargetDetails = partnerTargetRepository.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now().minusMonths(3));
357
        }
28468 tejbeer 358
 
31884 tejbeer 359
        PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
28468 tejbeer 360
 
31884 tejbeer 361
        int currentRate = 0;
362
        if (mtdSaleTillYesterDay > 0) {
363
            currentRate = (int) (mtdSaleTillYesterDay / daysGone);
364
        }
28468 tejbeer 365
 
31884 tejbeer 366
        salesMap.put("requiredType", partnerType.next());
367
        float reqdAmount = partnerTypeChangeService.getMinimumAmount(partnerType.next());
368
        int requiredRate = (int) ((reqdAmount - mtdSaleTillYesterDay) / daysRemaining);
369
        if (partnerType.equals(PartnerType.PLATINUM) && requiredRate < currentRate) {
370
            requiredRate = currentRate;
371
        }
372
        salesMap.put("requiredRate", requiredRate);
373
        salesMap.put("requiredTypeImage", PartnerType.imageMap.get(partnerType.next()));
28468 tejbeer 374
 
31884 tejbeer 375
        salesMap.put("todaySale", todaySale == null ? 0 : todaySale);
376
        salesMap.put("mtdSale", mtdSale == null ? 0 : mtdSale);
377
        salesMap.put("lmtdSale", lmtdSale == null ? 0 : lmtdSale);
28468 tejbeer 378
 
31884 tejbeer 379
        PartnerType currentType = partnerTypeChangeService.getPartnerTypeByAmount(currentRate * monthLength);
380
        salesMap.put("currentRate", currentRate);
381
        salesMap.put("currentType", currentType);
382
        salesMap.put("currentTypeImage", PartnerType.imageMap.get(currentType));
383
        return salesMap;
384
    }
28468 tejbeer 385
 
386
 
35595 amit 387
    @Cacheable(value = "partnerBrandChart", cacheManager = "redisShortCacheManager")
34388 tejus.loha 388
    public ChartModel getBrandChart(int fofoId, LocalDate startDate, LocalDate endDate, boolean isQuantity) {
28468 tejbeer 389
 
34388 tejus.loha 390
        LocalDateTime curDate = startDate == null ? LocalDate.now().atStartOfDay().withDayOfMonth(1) : startDate.atStartOfDay();
391
        LocalDateTime lastDate = endDate == null ? LocalDate.now().atStartOfDay().with(LocalTime.MAX) : endDate.atStartOfDay();
28468 tejbeer 392
 
34388 tejus.loha 393
        Map<String, Double> brandWiseAllSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(curDate, lastDate, fofoId);
394
        Map<String, Long> brandWiseSaleQuantity = fofoOrderItemRepository.selectSumQuantityGroupByBrand(curDate, lastDate, fofoId);
28468 tejbeer 395
 
34388 tejus.loha 396
        Map<Integer, Double> accesoriesmtdsale = fofoOrderRepository.selectSumSaleGroupByFofoIdsForMobileOrAccessories(fofoId, curDate, lastDate, Optional.of(false));
397
        brandWiseAllSale.put("Accessories", accesoriesmtdsale.get(fofoId));
398
        Map<Integer, Long> accesoriesMtdSaleQuantity = fofoOrderRepository.selectSumSaleQuantityGroupByFofoIdsForMobileOrAccessories(fofoId, curDate, lastDate, Optional.of(false));
399
        brandWiseSaleQuantity.put("Accessories", accesoriesMtdSaleQuantity.get(fofoId));
400
        List<ActivatedImeisWithSellingPrice> activatedImeisWithSellingPrices = fofoOrderRepository.selectValueOfActivatedImeis(curDate, lastDate, fofoId);
401
        Map<String, Double> activatedImeisWithSellingPriceMTD = activatedImeisWithSellingPrices.stream().collect(Collectors.toMap(x -> x.getBrand(), x -> (double) x.getSellingPrice()));
402
        Map<String, Long> activatedImeisWithSellingQuantityMTD = activatedImeisWithSellingPrices.stream().collect(Collectors.toMap(x -> x.getBrand(), x -> (long) x.getQuantity()));
28468 tejbeer 403
 
34388 tejus.loha 404
        activatedImeisWithSellingPriceMTD.put("Lava", brandWiseAllSale.get("Lava"));
405
        activatedImeisWithSellingQuantityMTD.put("Lava", brandWiseSaleQuantity.get("Lava"));
28468 tejbeer 406
 
34388 tejus.loha 407
        //--------------------------
28468 tejbeer 408
 
34388 tejus.loha 409
        Map<String, Double> LMTDBrandWiseSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(curDate.minusMonths(1), lastDate.minusMonths(1), fofoId);
410
        Map<String, Long> LMTDBrandWiseSaleQuantity = fofoOrderItemRepository.selectSumQuantityGroupByBrand(curDate.minusMonths(1), lastDate.minusMonths(1), fofoId);
28468 tejbeer 411
 
34388 tejus.loha 412
        List<ActivatedImeisWithSellingPrice> LMTDActivatedImeisWithSellingPrices = fofoOrderRepository.selectValueOfActivatedImeis(curDate.minusMonths(1), lastDate.minusMonths(1), fofoId);
413
        Map<String, Double> LMTDActivatedImeisWithSellingPrice = LMTDActivatedImeisWithSellingPrices.stream().collect(Collectors.toMap(x -> x.getBrand(), x -> (double) x.getSellingPrice()));
414
        Map<String, Long> LMTDActivatedImeisWithSellingQuantity = LMTDActivatedImeisWithSellingPrices.stream().collect(Collectors.toMap(x -> x.getBrand(), x -> (long) x.getQuantity()));
28468 tejbeer 415
 
34388 tejus.loha 416
        LMTDActivatedImeisWithSellingPrice.put("Lava", LMTDBrandWiseSale.get("Lava"));
417
        LMTDActivatedImeisWithSellingQuantity.put("Lava", LMTDBrandWiseSaleQuantity.get("Lava"));
28468 tejbeer 418
 
31884 tejbeer 419
        ChartModel cm = new ChartModel();
28468 tejbeer 420
 
34388 tejus.loha 421
        List<String> brandList = Arrays.asList("Vivo", "Oppo", "Samsung", "Realme", "Xiaomi", "POCO", "Apple", "Tecno", "Lava", "Itel");
422
        LinkedHashSet<String> brandAsLabel = new LinkedHashSet<>(brandList);
28468 tejbeer 423
 
31884 tejbeer 424
        List<Double> mtdActivatedImeisValues = new ArrayList<>();
34388 tejus.loha 425
        List<Double> mtdValues = new ArrayList<>();
426
        List<Double> mtdUnActivatedImeisValues = new ArrayList<>();
427
        List<Double> mtdActivatedImeisQuantity = new ArrayList<>();
428
        List<Double> mtdQuantity = new ArrayList<>();
429
        List<Double> mtdUnActivatedImeisQuantity = new ArrayList<>();
28468 tejbeer 430
 
31884 tejbeer 431
        List<Double> lmtdActivatedImeisValues = new ArrayList<>();
432
        List<Double> lmtdValues = new ArrayList<>();
34388 tejus.loha 433
        List<Double> lmtdUnActivatedImeisValues = new ArrayList<>();
434
        List<Double> lmtdActivatedImeisQuantity = new ArrayList<>();
435
        List<Double> lmtdQuantity = new ArrayList<>();
436
        List<Double> lmtdUnActivatedImeisQuantity = new ArrayList<>();
28468 tejbeer 437
 
34388 tejus.loha 438
 
439
        for (String brand : brandList) {
440
            if (isQuantity) {
441
                mtdActivatedImeisQuantity.add(activatedImeisWithSellingQuantityMTD.get(brand) == null ? 0 : (double) activatedImeisWithSellingQuantityMTD.get(brand));
442
                mtdQuantity.add(brandWiseSaleQuantity.get(brand) == null ? 0 : (double) brandWiseSaleQuantity.get(brand));
443
                if (brandWiseSaleQuantity.get(brand) != null) {
444
                    mtdUnActivatedImeisQuantity.add(brandWiseSaleQuantity.get(brand) - (activatedImeisWithSellingQuantityMTD.get(brand) == null ? 0 : (double) activatedImeisWithSellingQuantityMTD.get(brand)));
445
                }
446
                lmtdQuantity.add(LMTDBrandWiseSaleQuantity.get(brand) == null ? 0 : (double) LMTDBrandWiseSaleQuantity.get(brand));
447
                lmtdActivatedImeisQuantity.add(LMTDActivatedImeisWithSellingQuantity.get(brand) == null ? 0 : (double) LMTDActivatedImeisWithSellingQuantity.get(brand));
448
                lmtdUnActivatedImeisQuantity.add(LMTDBrandWiseSaleQuantity.get(brand) == null ? 0 : LMTDBrandWiseSaleQuantity.get(brand) - (LMTDActivatedImeisWithSellingQuantity.get(brand) == null ? 0 : (double) LMTDActivatedImeisWithSellingQuantity.get(brand)));
31884 tejbeer 449
            } else {
34388 tejus.loha 450
                mtdActivatedImeisValues.add(activatedImeisWithSellingPriceMTD.get(brand) == null ? 0 : activatedImeisWithSellingPriceMTD.get(brand));
451
                mtdValues.add(brandWiseAllSale.get(brand) == null ? 0 : brandWiseAllSale.get(brand));
452
                if (brandWiseAllSale.get(brand) != null) {
453
                    mtdUnActivatedImeisValues.add(brandWiseAllSale.get(brand) - (activatedImeisWithSellingPriceMTD.get(brand) == null ? 0 : activatedImeisWithSellingPriceMTD.get(brand)));
454
                }
455
                lmtdValues.add(LMTDBrandWiseSale.get(brand) == null ? 0.0 : LMTDBrandWiseSale.get(brand));
456
                lmtdActivatedImeisValues.add(LMTDActivatedImeisWithSellingPrice.get(brand) == null ? 0.0 : LMTDActivatedImeisWithSellingPrice.get(brand));
457
                if (LMTDBrandWiseSale.get(brand) != null) {
458
                    lmtdUnActivatedImeisValues.add(LMTDBrandWiseSale.get(brand) - (LMTDActivatedImeisWithSellingPrice.get(brand) == null ? 0.0 : LMTDActivatedImeisWithSellingPrice.get(brand)));
459
                }
31884 tejbeer 460
            }
461
        }
28468 tejbeer 462
 
34388 tejus.loha 463
        DatasetModel mtdActivatedImeis = new DatasetModel();
464
        mtdActivatedImeis.setLabel("Total Activation");
465
        mtdActivatedImeis.setBorderColor("#00008b");
466
        mtdActivatedImeis.setBackgroundColor("#00008b");
467
        mtdActivatedImeis.setStack("stack0");
468
        mtdActivatedImeis.setOrder(0);
469
 
31884 tejbeer 470
        DatasetModel dsmUnactivated = new DatasetModel();
34388 tejus.loha 471
        dsmUnactivated.setLabel("Total Unactivated");
31884 tejbeer 472
        dsmUnactivated.setBackgroundColor("red");
473
        dsmUnactivated.setBorderColor("red");
474
        dsmUnactivated.setStack("stack0");
475
        dsmUnactivated.setOrder(1);
28468 tejbeer 476
 
34388 tejus.loha 477
        DatasetModel linemtdChart = new DatasetModel();
478
        linemtdChart.setLabel("Total");
479
        linemtdChart.setBackgroundColor("#006400");
480
        linemtdChart.setBorderColor("#006400");
481
        linemtdChart.setType("line");
482
        linemtdChart.setOrder(2);
483
        linemtdChart.setFill("false");
28468 tejbeer 484
 
34388 tejus.loha 485
        DatasetModel lmtdActivatedImeis = new DatasetModel();
34466 tejus.loha 486
        lmtdActivatedImeis.setLabel("prev mth Activation");
34388 tejus.loha 487
        lmtdActivatedImeis.setBackgroundColor("#87ceeb");
488
        lmtdActivatedImeis.setBorderColor("#87ceeb");
489
        lmtdActivatedImeis.setStack("stack1");
490
        lmtdActivatedImeis.setOrder(3);
28468 tejbeer 491
 
492
 
31884 tejbeer 493
        DatasetModel lmtdUnActivated = new DatasetModel();
34466 tejus.loha 494
        lmtdUnActivated.setLabel("prev mth Unactivation");
31884 tejbeer 495
        lmtdUnActivated.setBackgroundColor("red");
496
        lmtdUnActivated.setBorderColor("red");
497
        lmtdUnActivated.setStack("stack1");
34388 tejus.loha 498
        lmtdUnActivated.setOrder(4);
28468 tejbeer 499
 
500
 
31884 tejbeer 501
        DatasetModel lineLmtdChart = new DatasetModel();
34466 tejus.loha 502
        lineLmtdChart.setLabel("prev mth TOTAL");
31884 tejbeer 503
        lineLmtdChart.setBackgroundColor("hotpink");
504
        lineLmtdChart.setBorderColor("hotpink");
505
        lineLmtdChart.setType("line");
34388 tejus.loha 506
        lineLmtdChart.setOrder(5);
31884 tejbeer 507
        lineLmtdChart.setFill("false");
28468 tejbeer 508
 
34388 tejus.loha 509
        if (isQuantity) {
510
            mtdActivatedImeis.setData(mtdActivatedImeisQuantity);
511
            dsmUnactivated.setData(mtdUnActivatedImeisQuantity);
512
            linemtdChart.setData(mtdQuantity);
513
            lmtdActivatedImeis.setData(lmtdActivatedImeisQuantity);
514
            lmtdUnActivated.setData(lmtdUnActivatedImeisQuantity);
515
            lineLmtdChart.setData(lmtdQuantity);
516
        } else {
517
            mtdActivatedImeis.setData(mtdActivatedImeisValues);
518
            dsmUnactivated.setData(mtdUnActivatedImeisValues);
519
            linemtdChart.setData(mtdValues);
520
            lmtdActivatedImeis.setData(lmtdActivatedImeisValues);
521
            lmtdUnActivated.setData(lmtdUnActivatedImeisValues);
522
            lineLmtdChart.setData(lmtdValues);
523
        }
524
 
31884 tejbeer 525
        List<DatasetModel> datasets = new ArrayList<>();
526
        datasets.add(mtdActivatedImeis);
527
        datasets.add(dsmUnactivated);
34388 tejus.loha 528
        datasets.add(linemtdChart);
529
        datasets.add(lmtdActivatedImeis);
31884 tejbeer 530
        datasets.add(lmtdUnActivated);
531
        datasets.add(lineLmtdChart);
28468 tejbeer 532
 
31884 tejbeer 533
        DataModel dm = new DataModel();
534
        dm.setDatasets(datasets);
34388 tejus.loha 535
        dm.setLabels(brandAsLabel);
31884 tejbeer 536
        Tooltips tooltips = new Tooltips();
34388 tejus.loha 537
        tooltips.setBodyFontSize(25);
538
        tooltips.setTitleFontSize(25);
31884 tejbeer 539
        tooltips.setMode("index");
540
        tooltips.setIntersect(false);
34388 tejus.loha 541
        tooltips.setBackgroundColor("rgba(0, 0, 0, 0.7)");
31884 tejbeer 542
        HoverModel hover = new HoverModel();
543
        hover.setIntersect(false);
544
        hover.setMode("index");
28468 tejbeer 545
 
31884 tejbeer 546
        LegendModel lm = new LegendModel();
547
        lm.setPosition("top");
28468 tejbeer 548
 
31884 tejbeer 549
        TitleModel tm = new TitleModel();
34426 tejus.loha 550
        if (isQuantity) {
551
            tm.setText("Brand Wise Sales Quantity");
552
        } else {
553
            tm.setText("Brand Wise Sales Value");
554
        }
31884 tejbeer 555
        tm.setDisplay(true);
556
        tm.setFontSize(20);
557
        tm.setFontColor("#111");
28468 tejbeer 558
 
31884 tejbeer 559
        List<Axis> xAxes = new ArrayList<>();
560
        Axis xAxis = new Axis();
561
        xAxis.setStacked(true);
562
        xAxes.add(xAxis);
28468 tejbeer 563
 
31884 tejbeer 564
        List<Axis> yAxes = new ArrayList<>();
565
        Axis yAxis = new Axis();
566
        yAxis.setStacked(false);
567
        yAxes.add(yAxis);
28468 tejbeer 568
 
31884 tejbeer 569
        ScalesModel sm = new ScalesModel();
570
        sm.setxAxes(xAxes);
28468 tejbeer 571
 
31884 tejbeer 572
        OptionsModel om = new OptionsModel();
573
        om.setLegend(lm);
574
        om.setTitle(tm);
575
        om.setScales(sm);
576
        om.setHover(hover);
577
        om.setTooltips(tooltips);
28468 tejbeer 578
 
31884 tejbeer 579
        cm.setType("bar");
580
        cm.setData(dm);
581
        cm.setOptions(om);
28468 tejbeer 582
 
31884 tejbeer 583
        LOGGER.info("cm" + cm);
28468 tejbeer 584
 
31884 tejbeer 585
        return cm;
28468 tejbeer 586
 
31884 tejbeer 587
    }
28468 tejbeer 588
 
31884 tejbeer 589
    public ChartInvestmentModel getInvestmentChart(int fofoId) throws ProfitMandiBusinessException {
590
        PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 0);
28468 tejbeer 591
 
31884 tejbeer 592
        Map<String, Float> investmentWalletAmount = new HashMap<>();
593
        investmentWalletAmount.put("Wallet", investment.getWalletAmount());
594
        investmentWalletAmount.put("InStocks", investment.getInStockAmount() - investment.getActivatedStockAmount());
595
        investmentWalletAmount.put("Unbilled Order", investment.getUnbilledAmount());
596
        investmentWalletAmount.put("GrnPending", investment.getGrnPendingAmount() - investment.getActivatedGrnPendingAmount());
597
        investmentWalletAmount.put("ReturnInTransit", investment.getReturnInTransitAmount());
28468 tejbeer 598
 
35848 amit 599
        if (investment.getUtilizedAmount() > 0) {
600
            investmentWalletAmount.put("Credit Utilized", -investment.getUtilizedAmount());
601
        }
602
 
31884 tejbeer 603
        if (investment.getShortInvestment() > 0) {
604
            investmentWalletAmount.put("Short Investment", investment.getShortInvestment());
605
        }
28468 tejbeer 606
 
31884 tejbeer 607
        ChartInvestmentModel cm = new ChartInvestmentModel();
28468 tejbeer 608
 
31884 tejbeer 609
        HashSet<String> labels = new HashSet<String>();
610
        labels.addAll(investmentWalletAmount.keySet());
28468 tejbeer 611
 
31884 tejbeer 612
        List<String> labelList = new ArrayList<>(labels);
613
        List<String> backgroundColor = new ArrayList<>();
614
        List<Float> values = new ArrayList<>();
615
        for (String label : labelList) {
616
            values.add(investmentWalletAmount.get(label));
617
            if (label.equals("Wallet")) {
618
                backgroundColor.add("pink");
619
            }
620
            if (label.equals("Short Investment")) {
621
                backgroundColor.add("red");
622
            }
623
            if (label.equals("InStocks")) {
624
                backgroundColor.add("#9ACD32");
625
            }
626
            if (label.equals("Unbilled Order")) {
627
                backgroundColor.add("blue");
628
            }
28468 tejbeer 629
 
31884 tejbeer 630
            if (label.equals("ReturnInTransit")) {
631
                backgroundColor.add("orange");
632
            }
633
            if (label.equals("GrnPending")) {
634
                backgroundColor.add("yellow");
635
            }
35848 amit 636
            if (label.equals("Credit Utilized")) {
637
                backgroundColor.add("grey");
638
            }
28468 tejbeer 639
 
31884 tejbeer 640
        }
28468 tejbeer 641
 
31884 tejbeer 642
        Data data = new Data();
643
        data.setData(values);
644
        data.setBackgroundColor(backgroundColor);
645
        data.setLabel("DataSet 1");
28468 tejbeer 646
 
31884 tejbeer 647
        PieLables label = new PieLables();
648
        label.setFontColor("White");
649
        label.setFontSize(15);
28468 tejbeer 650
 
31884 tejbeer 651
        Legend legend = new Legend();
652
        legend.setLabels(label);
653
        legend.setPosition("left");
28468 tejbeer 654
 
31884 tejbeer 655
        List<Data> dataList = new ArrayList<>();
656
        dataList.add(data);
28468 tejbeer 657
 
31884 tejbeer 658
        DataInvestmentModel datasets = new DataInvestmentModel();
659
        datasets.setDatasets(dataList);
660
        datasets.setLabels(labels);
28468 tejbeer 661
 
31884 tejbeer 662
        OptionModel om = new OptionModel();
663
        om.setLegend(legend);
664
        cm.setType("pie");
665
        cm.setData(datasets);
666
        cm.setOptions(om);
28468 tejbeer 667
 
31884 tejbeer 668
        return cm;
669
    }
28468 tejbeer 670
 
31884 tejbeer 671
    public List<Notification> getNotifications(List<NotificationCampaign> nc, MessageType messageType) throws ProfitMandiBusinessException {
672
        List<Notification> notifications = new ArrayList<>();
673
        Document document = null;
674
        if (messageType != null) {
675
            for (NotificationCampaign notificationCampaign : nc) {
676
                if (notificationCampaign.getMessageType() == messageType) {
677
                    Notification ns = new Notification();
678
                    SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(), SimpleCampaignParams.class);
679
                    Campaign campaign = new SimpleCampaign(scp);
680
                    LocalDateTime expire = campaign.getExpireTimestamp();
681
                    ns.setCid(Integer.toString(notificationCampaign.getId()));
682
                    ns.setType(campaign.getType());
683
                    ns.setMessage(campaign.getMessage());
684
                    ns.setTitle(campaign.getTitle());
685
                    if (notificationCampaign.getDocumentId() != null) {
686
                        document = documentRepository.selectById(notificationCampaign.getDocumentId());
687
                        ns.setDocumentName(document.getDisplayName());
688
                    }
689
                    ns.setUrl(campaign.getUrl());
690
                    ns.setShowImage(campaign.getShowImage());
691
                    ns.setImageUrl(campaign.getImageUrl());
692
                    ns.setDocumentId(notificationCampaign.getDocumentId());
693
                    ns.setMessageType(notificationCampaign.getMessageType());
694
                    ns.setCreated(
695
                            notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30)) * 1000);
696
                    if (LocalDateTime.now().isAfter(expire)) {
697
                        ns.setExpired(true);
698
                    } else {
699
                        ns.setExpired(false);
700
                    }
701
                    notifications.add(ns);
702
                }
703
            }
704
        } else {
705
            for (NotificationCampaign notificationCampaign : nc) {
28468 tejbeer 706
 
31884 tejbeer 707
                Notification ns = new Notification();
708
                SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(), SimpleCampaignParams.class);
709
                Campaign campaign = new SimpleCampaign(scp);
710
                LocalDateTime expire = campaign.getExpireTimestamp();
711
                ns.setCid(Integer.toString(notificationCampaign.getId()));
712
                ns.setType(campaign.getType());
713
                ns.setMessage(campaign.getMessage());
714
                ns.setTitle(campaign.getTitle());
715
                if (notificationCampaign.getDocumentId() != null) {
716
                    document = documentRepository.selectById(notificationCampaign.getDocumentId());
717
                    ns.setDocumentName(document.getDisplayName());
718
                }
719
                ns.setUrl(campaign.getUrl());
720
                ns.setShowImage(campaign.getShowImage());
721
                ns.setImageUrl(campaign.getImageUrl());
722
                ns.setDocumentId(notificationCampaign.getDocumentId());
723
                ns.setMessageType(notificationCampaign.getMessageType());
724
                ns.setCreated(notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30)) * 1000);
725
                if (LocalDateTime.now().isAfter(expire)) {
726
                    ns.setExpired(true);
727
                } else {
728
                    ns.setExpired(false);
729
                }
730
                notifications.add(ns);
731
            }
28468 tejbeer 732
 
31884 tejbeer 733
        }
734
        return notifications;
28468 tejbeer 735
 
31884 tejbeer 736
    }
737
 
738
    public boolean hasGift(int fofoId) {
739
        try {
740
            return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId).getAvailability() > 0;
741
        } catch (ProfitMandiBusinessException e) {
742
            return false;
743
        }
744
    }
745
 
34388 tejus.loha 746
    public ChartModel getModelBrandChart(int fofoId, String brand, LocalDate startDate, LocalDate endDate, boolean isQuantity) {
747
 
748
        LocalDateTime curDate = startDate == null ? LocalDate.now().atStartOfDay().withDayOfMonth(1) : startDate.atStartOfDay();
749
        LocalDateTime lastDate = endDate == null ? LocalDate.now().atStartOfDay().with(LocalTime.MAX) : endDate.atStartOfDay();
750
 
751
        //----------------------------MTD-------------------------
752
        Map<String, Double> modelWiseSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(curDate, lastDate, fofoId, "modelNumber", brand);
753
        Map<String, Long> modelWiseSaleQuantity = fofoOrderItemRepository.selectSumQuantityGroupByBrand(curDate, lastDate, fofoId, "modelNumber", brand);
754
 
755
        List<ActivatedImeisWithSellingPrice> activatedImeisWithSellingPricesMTD = fofoOrderItemRepository.selectValueOfActivatedImeisModelWise(curDate, lastDate, fofoId, brand);
756
        Map<String, Double> activatedImeisWithSellingPriceMTD = activatedImeisWithSellingPricesMTD.stream().collect(Collectors.toMap(x -> x.getModel(), x -> (double) x.getSellingPrice()));
757
        Map<String, Long> activatedImeisWithSellingQuantityMTD = activatedImeisWithSellingPricesMTD.stream().collect(Collectors.toMap(x -> x.getModel(), x -> (long) x.getQuantity()));
758
 
759
 
760
        //---------------------------------LMTD----------------------
761
        Map<String, Double> lmtdModelWiseSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(curDate.minusMonths(1), lastDate.minusMonths(1), fofoId, "modelNumber", brand);
762
        Map<String, Long> lmtdModelWiseSaleQuantity = fofoOrderItemRepository.selectSumQuantityGroupByBrand(curDate.minusMonths(1), lastDate.minusMonths(1), fofoId, "modelNumber", brand);
763
 
764
        List<ActivatedImeisWithSellingPrice> lmtdActivatedImeisWithSellingPrices = fofoOrderItemRepository.selectValueOfActivatedImeisModelWise(curDate.minusMonths(1), lastDate.minusMonths(1), fofoId, brand);
765
        Map<String, Double> lmtdActivatedImeisWithSellingPrice = lmtdActivatedImeisWithSellingPrices.stream().collect(Collectors.toMap(x -> x.getModel(), x -> (double) x.getSellingPrice()));
766
        Map<String, Long> lmtdActivatedImeisWithSellingQuantity = lmtdActivatedImeisWithSellingPrices.stream().collect(Collectors.groupingBy(x -> x.getModel(), Collectors.summingLong(x -> x.getQuantity())));
767
 
768
        ChartModel cm = new ChartModel();
769
 
770
        List<String> brandList1 = lmtdModelWiseSale.keySet().stream().collect(Collectors.toList());
771
        List<String> brandList2 = modelWiseSale.keySet().stream().collect(Collectors.toList());
772
        HashSet<String> brandsAsLabelList = new HashSet<>();
773
        brandsAsLabelList.addAll(brandList1);
774
        brandsAsLabelList.addAll(brandList2);
775
        // for MTD
776
        List<Double> mtdActivatedImeisValues = new ArrayList<>();
777
        List<Double> mtdUnActivatedImeisValues = new ArrayList<>();
778
        List<Double> mtdValues = new ArrayList<>();
779
        List<Double> mtdActivatedImeisQuantity = new ArrayList<>();
780
        List<Double> mtdUnActivatedImeisQuantity = new ArrayList<>();
781
        List<Double> mtdQuantity = new ArrayList<>();
782
 
783
        // for LMTD
784
        List<Double> lmtdActivatedImeisValues = new ArrayList<>();
785
        List<Double> lmtdUnActivatedImeisValues = new ArrayList<>();
786
        List<Double> lmtdValues = new ArrayList<>();
787
        List<Double> lmtdActivatedImeisQuantity = new ArrayList<>();
788
        List<Double> lmtdUnActivatedImeisQuantity = new ArrayList<>();
789
        List<Double> lmtdQuantity = new ArrayList<>();
790
 
791
 
792
        LOGGER.info("labelsList - " + brandsAsLabelList);
793
        for (String brandAsLabel : brandsAsLabelList) {
794
            brandAsLabel = brandAsLabel.trim();
795
            if (isQuantity) {
796
                mtdActivatedImeisQuantity.add(activatedImeisWithSellingQuantityMTD.get(brandAsLabel) == null ? 0 : (double) activatedImeisWithSellingQuantityMTD.get(brandAsLabel));
797
                mtdQuantity.add(modelWiseSaleQuantity.get(brandAsLabel) == null ? 0 : (double) modelWiseSaleQuantity.get(brandAsLabel));
798
                mtdUnActivatedImeisQuantity.add(modelWiseSaleQuantity.get(brandAsLabel) == null ? 0 : modelWiseSaleQuantity.get(brandAsLabel) - (activatedImeisWithSellingQuantityMTD.get(brandAsLabel) == null ? 0 : (double) activatedImeisWithSellingQuantityMTD.get(brandAsLabel)));
799
 
800
                lmtdActivatedImeisQuantity.add(lmtdActivatedImeisWithSellingQuantity.get(brandAsLabel) == null ? 0 : (double) lmtdActivatedImeisWithSellingQuantity.get(brandAsLabel));
801
                lmtdUnActivatedImeisQuantity.add(lmtdModelWiseSaleQuantity.get(brandAsLabel) == null ? 0 : lmtdModelWiseSaleQuantity.get(brandAsLabel) - (lmtdActivatedImeisWithSellingQuantity.get(brandAsLabel) == null ? 0 : (double) lmtdActivatedImeisWithSellingQuantity.get(brandAsLabel)));
802
                lmtdQuantity.add(lmtdModelWiseSaleQuantity.get(brandAsLabel) == null ? 0 : (double) lmtdModelWiseSaleQuantity.get(brandAsLabel));
803
            } else {
804
                mtdActivatedImeisValues.add(activatedImeisWithSellingPriceMTD.get(brandAsLabel) == null ? 0 : activatedImeisWithSellingPriceMTD.get(brandAsLabel));
805
                mtdValues.add(modelWiseSale.get(brandAsLabel) == null ? 0 : modelWiseSale.get(brandAsLabel));
806
                if (modelWiseSale.get(brandAsLabel) != null) {
807
                    mtdUnActivatedImeisValues.add(modelWiseSale.get(brandAsLabel) - (activatedImeisWithSellingPriceMTD.get(brandAsLabel) == null ? 0 : activatedImeisWithSellingPriceMTD.get(brandAsLabel)));
808
                } else {
809
                    mtdUnActivatedImeisValues.add(0.0);
810
                }
811
                lmtdValues.add(lmtdModelWiseSale.get(brandAsLabel) == null ? 0 : lmtdModelWiseSale.get(brandAsLabel));
812
                lmtdActivatedImeisValues.add(lmtdActivatedImeisWithSellingPrice.get(brandAsLabel) == null ? 0 : lmtdActivatedImeisWithSellingPrice.get(brandAsLabel));
813
                if (lmtdModelWiseSale.get(brandAsLabel.trim()) != null) {
814
                    lmtdUnActivatedImeisValues.add(lmtdModelWiseSale.get(brandAsLabel) - (lmtdActivatedImeisWithSellingPrice.get(brandAsLabel) == null ? 0 : lmtdActivatedImeisWithSellingPrice.get(brandAsLabel)));
815
                } else {
816
                    lmtdUnActivatedImeisValues.add(0.0);
817
                }
818
            }
819
        }
820
 
821
        DatasetModel mtdActivatedImeis = new DatasetModel();
822
        mtdActivatedImeis.setLabel("Total Activation");
823
        mtdActivatedImeis.setBorderColor("#00008b");
824
        mtdActivatedImeis.setBackgroundColor("#00008b");
825
        mtdActivatedImeis.setStack("stack0");
826
        mtdActivatedImeis.setOrder(0);
827
 
828
        DatasetModel dsmUnactivated = new DatasetModel();
829
        dsmUnactivated.setLabel("Total Unactivated");
830
        dsmUnactivated.setBackgroundColor("red");
831
        dsmUnactivated.setBorderColor("red");
832
        dsmUnactivated.setStack("stack0");
833
        dsmUnactivated.setOrder(1);
834
 
835
        DatasetModel linemtdChart = new DatasetModel();
836
        linemtdChart.setLabel("Total");
837
        linemtdChart.setBackgroundColor("#006400");
838
        linemtdChart.setBorderColor("#006400");
839
        linemtdChart.setType("line");
840
        linemtdChart.setOrder(2);
841
        linemtdChart.setFill("false");
842
 
843
        DatasetModel LmtdActivatedImeis = new DatasetModel();
34466 tejus.loha 844
        LmtdActivatedImeis.setLabel("prev mth Activation");
34388 tejus.loha 845
        LmtdActivatedImeis.setBackgroundColor("#87ceeb");
846
        LmtdActivatedImeis.setBorderColor("#87ceeb");
847
        LmtdActivatedImeis.setStack("stack1");
848
        LmtdActivatedImeis.setOrder(3);
849
 
850
        DatasetModel lmtdUnActivated = new DatasetModel();
34466 tejus.loha 851
        lmtdUnActivated.setLabel("prev mth Unactivation");
34388 tejus.loha 852
        lmtdUnActivated.setBackgroundColor("red");
853
        lmtdUnActivated.setBorderColor("red");
854
        lmtdUnActivated.setStack("stack1");
855
        lmtdUnActivated.setOrder(4);
856
 
857
        DatasetModel lineLmtdChart = new DatasetModel();
34466 tejus.loha 858
        lineLmtdChart.setLabel("prev mth TOTAL");
34388 tejus.loha 859
        lineLmtdChart.setBackgroundColor("hotpink");
860
        lineLmtdChart.setBorderColor("hotpink");
861
        lineLmtdChart.setType("line");
862
        lineLmtdChart.setOrder(5);
863
        lineLmtdChart.setFill("false");
864
 
865
 
866
        if (isQuantity) {
867
            mtdActivatedImeis.setData(mtdActivatedImeisQuantity);
868
            dsmUnactivated.setData(mtdUnActivatedImeisQuantity);
869
            linemtdChart.setData(mtdQuantity);
870
            LmtdActivatedImeis.setData(lmtdActivatedImeisQuantity);
871
            lmtdUnActivated.setData(lmtdUnActivatedImeisQuantity);
872
            lineLmtdChart.setData(lmtdQuantity);
873
        } else {
874
            mtdActivatedImeis.setData(mtdActivatedImeisValues);
875
            dsmUnactivated.setData(mtdUnActivatedImeisValues);
876
            linemtdChart.setData(mtdValues);
877
            LmtdActivatedImeis.setData(lmtdActivatedImeisValues);
878
            lmtdUnActivated.setData(lmtdUnActivatedImeisValues);
879
            lineLmtdChart.setData(lmtdValues);
880
        }
881
 
882
        List<DatasetModel> datasets = new ArrayList<>();
883
        datasets.add(mtdActivatedImeis);
884
        datasets.add(dsmUnactivated);
885
        datasets.add(linemtdChart);
886
        datasets.add(LmtdActivatedImeis);
887
        datasets.add(lmtdUnActivated);
888
        datasets.add(lineLmtdChart);
889
 
890
        DataModel dm = new DataModel();
891
        dm.setDatasets(datasets);
892
        dm.setLabels(brandsAsLabelList);
893
 
894
        Tooltips tooltips = new Tooltips();
895
        tooltips.setBodyFontSize(25);
896
        tooltips.setTitleFontSize(25);
897
        tooltips.setMode("index");
898
        tooltips.setIntersect(false);
899
        tooltips.setBackgroundColor("rgba(0, 0, 0, 0.7)");
900
        HoverModel hover = new HoverModel();
901
        hover.setIntersect(false);
902
        hover.setMode("index");
903
 
904
        LegendModel lm = new LegendModel();
905
        lm.setPosition("top");
906
        TitleModel tm = new TitleModel();
34426 tejus.loha 907
        if (isQuantity) {
908
            tm.setText("Model Wise Sales Quantity");
909
        } else {
910
            tm.setText("Model Wise Sales Value");
911
        }
34388 tejus.loha 912
        tm.setDisplay(true);
34426 tejus.loha 913
        tm.setFontSize(30);
914
        tm.setFontColor("#050a4d");
34388 tejus.loha 915
 
916
        List<Axis> xAxes = new ArrayList<>();
917
        Axis xAxis = new Axis();
918
        xAxis.setStacked(true);
919
        xAxes.add(xAxis);
920
 
921
        List<Axis> yAxes = new ArrayList<>();
922
        Axis yAxis = new Axis();
923
        yAxis.setStacked(false);
924
        yAxes.add(yAxis);
925
 
926
        ScalesModel sm = new ScalesModel();
927
        sm.setxAxes(xAxes);
928
 
929
        OptionsModel om = new OptionsModel();
930
        om.setLegend(lm);
931
        om.setTitle(tm);
932
        om.setScales(sm);
933
        om.setHover(hover);
934
        om.setTooltips(tooltips);
935
 
936
        cm.setType("bar");
937
        cm.setData(dm);
938
        cm.setOptions(om);
939
 
940
        LOGGER.info("Model_cm" + cm);
941
        return cm;
942
 
943
    }
944
 
28468 tejbeer 945
}