Subversion Repositories SmartDukaan

Rev

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