Subversion Repositories SmartDukaan

Rev

Rev 34698 | Rev 34706 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34698 Rev 34705
Line 11... Line 11...
11
import com.spice.profitmandi.common.util.StringUtils;
11
import com.spice.profitmandi.common.util.StringUtils;
12
import com.spice.profitmandi.common.util.Utils;
12
import com.spice.profitmandi.common.util.Utils;
13
import com.spice.profitmandi.dao.entity.catalog.*;
13
import com.spice.profitmandi.dao.entity.catalog.*;
14
import com.spice.profitmandi.dao.entity.fofo.*;
14
import com.spice.profitmandi.dao.entity.fofo.*;
15
import com.spice.profitmandi.dao.entity.inventory.PartnerAgeingModel;
15
import com.spice.profitmandi.dao.entity.inventory.PartnerAgeingModel;
-
 
16
import com.spice.profitmandi.dao.entity.transaction.Loan;
16
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
17
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
17
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
18
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
18
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
19
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
19
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
20
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
20
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
21
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
Line 25... Line 26...
25
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
26
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
26
import com.spice.profitmandi.dao.repository.catalog.*;
27
import com.spice.profitmandi.dao.repository.catalog.*;
27
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
28
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
28
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
29
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
29
import com.spice.profitmandi.dao.repository.fofo.*;
30
import com.spice.profitmandi.dao.repository.fofo.*;
-
 
31
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
30
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
32
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
31
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
33
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
32
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
34
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
33
import com.spice.profitmandi.dao.repository.warehouse.AgeingSummaryModel;
35
import com.spice.profitmandi.dao.repository.warehouse.AgeingSummaryModel;
34
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
36
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
Line 67... Line 69...
67
@Component
69
@Component
68
public class SchemeServiceImpl implements SchemeService {
70
public class SchemeServiceImpl implements SchemeService {
69
 
71
 
70
    private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
72
    private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
71
    private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
73
    private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
72
//    //These Imeis provided by deena
-
 
73
//    private static final List<String> BLOCKED_IMEIS =
74
    private static final List<SchemeType> FULL_DAYS_CD_SCHEME_TYPES = Arrays.asList(SchemeType.CASH_DISCOUNT, SchemeType.CASH_DISCOUNT1);
74
//            Arrays.asList(
-
 
75
//                    "864883056397593", "864883054606656", "864883056567815", "861950056518271", "869175055649511", "861362058924574", "866009066803036", "866009066816699", "866009066816137", "866009066815873",
-
 
76
//                    "866009066805536", "866009066803010", "866009066821939", "866009066802756", "866009066820592", "866009066820311", "866009066816491", "866009066816376", "866009066815899", "866009066815774",
-
 
77
//                    "866009066817937", "866009066819859", "866009066817655", "866009066820691", "866009066820832", "866009066803291", "866009066820733", "866009066814496", "866009066820451", "866009066820659",
-
 
78
//                    "866009066804976", "866009066820717", "866009066816095", "861362054898434", "869599051117852", "869599056695332", "869599056695894", "864883057389656", "862661052418692", "860118051929254",
-
 
79
//                    "862888051664998", "862680054625831", "862888051666316", "860118051738895", "868066050447970", "868066052424399", "865084051552576", "865084050755097", "865084050755295", "865084050754819",
-
 
80
//                    "864883057487278", "864883057389599", "864883057437455", "864883057388278", "862680058278058", "869599056810139", "862200053994193", "861932057188916", "861175050581774", "863933065909093",
-
 
81
//                    "863933065635391", "861362054889177", "864004062055154", "864004062069239", "862661050221676", "862661052416993", "866812058631475", "869599051118173", "869599051504273", "868066052729250",
-
 
82
//                    "864883057701397", "864883054123033", "864883054947316", "864883056235694", "868066052727692", "866030052139896", "866030052140175", "860588051522053", "860588051513193", "861932056969779",
-
 
83
//                    "869599056171995", "865594061074932", "863935059410491", "866088059072718", "869599055375894", "869599054306916", "863782054006472", "863782054012371", "860588053486992", "868066052726835",
-
 
84
//                    "868066052726694", "860688053876430", "860688053869674", "868494054682394", "869599053512357", "863132060568430", "357737717299925", "863132060568471", "869663062661835", "869663062659458",
-
 
85
//                    "863132061326994", "863132061326796", "863132060906770", "869663064230795", "869663064231876", "869663064230670", "869663064231819", "865303063431276", "865303063431391", "865303064031315",
-
 
86
//                    "865303064031273", "865303064030994", "865303064031091", "865303063689113", "865303063689576", "865303063688636", "865303063689055", "868386069690414", "868386069688533", "864288066323771",
-
 
87
//                    "864288066330156", "869657061613797", "864288065002178", "867791065108199", "867791065069839", "867791065069474", "867791065114395", "867791065113918", "867791065088318", "862814069767037",
-
 
88
//                    "867791063493114", "861433069356815", "861433069357359", "861433066654055", "861433066748675", "861433065712136", "861433065712656", "867791062605650", "867791062593559", "867791062593112",
-
 
89
//                    "867791062804436", "867791062798455", "867791062805177", "862965069680636", "862807060752412", "862807060752214", "865626066329117", "861433069357011", "861433066749319", "861433068904698",
-
 
90
//                    "861433068906057", "861433065714017", "861433065712177", "867791062804279", "866670064793915", "868386063007672", "865626064418557", "861433069357110", "864179066094829", "866382065031176",
-
 
91
//                    "864562072802477", "864562072801651", "864562072801917", "864288066786274", "864562070789478", "864562070785930", "864562070788330", "864179066116929", "864470060972569", "867492065820095",
-
 
92
//                    "867940069618279", "863718065007331", "863718065001417", "860948069657616", "860948069562733", "860948069563939", "860948068569838", "860948068569598", "860948067443092", "860948067442912",
-
 
93
//                    "860948067442573", "860948069561750", "860948069561479", "860948069561974", "860948069562550", "860948069560679", "860948069560190", "860948069562691", "860948069735859"
75
    private static final List<SchemeType> HALF_DAYS_CD_REJECT_SCHEME_TYPES = Arrays.asList(SchemeType.CASH_DISCOUNT1);
94
//    );
76
 
95
 
77
 
96
    private static final List<SchemeType> ACTIVATION_SCHEME_TYPES = Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT);
78
    private static final List<SchemeType> ACTIVATION_SCHEME_TYPES = Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT);
97
 
79
 
98
    @Autowired
80
    @Autowired
99
    SchemeBlockedImeiRepository schemeBlockedImeiRepository;
81
    SchemeBlockedImeiRepository schemeBlockedImeiRepository;
Line 360... Line 342...
360
            sendNotificationModel.setType("url");
342
            sendNotificationModel.setType("url");
361
            sendNotificationModel.setMessageType(MessageType.scheme);
343
            sendNotificationModel.setMessageType(MessageType.scheme);
362
            notificationService.sendNotificationToAll(sendNotificationModel);
344
            notificationService.sendNotificationToAll(sendNotificationModel);
363
        }
345
        }
364
    }
346
    }
365
 
-
 
366
    private void sendCombinedSchemesNotification(List<Scheme> schemes) throws ProfitMandiBusinessException {
-
 
367
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
-
 
368
        StringBuilder messageBuilder = new StringBuilder();
-
 
369
 
-
 
370
        for (Scheme scheme : schemes) {
-
 
371
            if (!ACTIVATION_SCHEME_TYPES.contains(scheme.getType())) {
-
 
372
                continue;
-
 
373
            }
347
    @Autowired
374
 
-
 
375
            String schemeTypeLabel = "Activation scheme";
-
 
376
            if (scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
-
 
377
                schemeTypeLabel = "Special Support";
-
 
378
            }
-
 
379
 
-
 
380
            List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
-
 
381
            Set<Integer> catalogIds = schemeItems.stream().map(SchemeItem::getCatalogId).collect(Collectors.toSet());
-
 
382
 
-
 
383
            List<String> itemBrand = itemRepository.selectAllByCatalogIds(catalogIds).stream()
-
 
384
                    .filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
385
                    .map(Item::getBrand)
-
 
386
                    .collect(Collectors.toList());
-
 
387
 
-
 
388
            List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream()
-
 
389
                    .filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
390
                    .map(Item::getItemDescriptionNoColor)
-
 
391
                    .collect(Collectors.toList());
-
 
392
 
-
 
393
            messageBuilder.append(String.format("*%s* on *%s* of Rs.*%s* for *%s*\n Duration from - *%s to %s*\n\n",
-
 
394
                    schemeTypeLabel,
-
 
395
                    itemBrand.get(0),
-
 
396
                    FormattingUtils.formatDecimal(scheme.getAmount()),
-
 
397
                    String.join(", ", itemDescriptions),
-
 
398
                    FormattingUtils.formatDateMonth(scheme.getStartDateTime()),
-
 
399
                    FormattingUtils.formatDateMonth(scheme.getEndDateTime())));
-
 
400
 
-
 
401
        }
-
 
402
 
-
 
403
        sendNotificationModel.setCampaignName("activationscheme");
-
 
404
        sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
-
 
405
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
-
 
406
        sendNotificationModel.setTitle("Special Support");
-
 
407
        sendNotificationModel.setMessage(messageBuilder.toString());
-
 
408
        sendNotificationModel.setType("url");
348
    LoanRepository loanRepository;
409
        sendNotificationModel.setMessageType(MessageType.scheme);
-
 
410
        notificationService.sendNotificationToAll(sendNotificationModel);
-
 
411
    }
-
 
412
 
349
 
413
    @Override
350
    @Override
414
    public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
351
    public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
415
        Scheme scheme = schemeRepository.selectById(schemeId);
352
        Scheme scheme = schemeRepository.selectById(schemeId);
416
        if (scheme == null || scheme.getActiveTimestamp() == null) {
353
        if (scheme == null || scheme.getActiveTimestamp() == null) {
Line 494... Line 431...
494
    @Autowired
431
    @Autowired
495
    OfferPayoutRepository offerPayoutRepository;
432
    OfferPayoutRepository offerPayoutRepository;
496
    @Autowired
433
    @Autowired
497
    AgeingService ageingService;
434
    AgeingService ageingService;
498
 
435
 
-
 
436
    private void sendCombinedSchemesNotification(List<Scheme> schemes) throws ProfitMandiBusinessException {
-
 
437
        if (schemes == null || schemes.isEmpty()) return;
-
 
438
 
-
 
439
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
-
 
440
        StringBuilder messageBuilder = new StringBuilder();
-
 
441
        StringBuilder titleBuilder = new StringBuilder();
-
 
442
        Set<Integer> allCatalogIds = new HashSet<>();
-
 
443
 
-
 
444
        for (Scheme scheme : schemes) {
-
 
445
            if (!ACTIVATION_SCHEME_TYPES.contains(scheme.getType())) {
-
 
446
                continue;
-
 
447
            }
-
 
448
 
-
 
449
            String schemeTypeLabel = "Activation scheme";
-
 
450
            if (scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
-
 
451
                schemeTypeLabel = "Special Support";
-
 
452
            }
-
 
453
 
-
 
454
            List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
-
 
455
            Set<Integer> catalogIds = schemeItems.stream().map(SchemeItem::getCatalogId).collect(Collectors.toSet());
-
 
456
            allCatalogIds.addAll(catalogIds);
-
 
457
 
-
 
458
            List<String> itemBrand = itemRepository.selectAllByCatalogIds(catalogIds).stream()
-
 
459
                    .filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
460
                    .map(Item::getBrand)
-
 
461
                    .collect(Collectors.toList());
-
 
462
 
-
 
463
            List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream()
-
 
464
                    .filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
465
                    .map(Item::getItemDescriptionNoColor)
-
 
466
                    .collect(Collectors.toList());
-
 
467
 
-
 
468
            // Title line
-
 
469
            titleBuilder.append(String.format("%s of Rs.%s for %s\n",
-
 
470
                    schemeTypeLabel,
-
 
471
                    FormattingUtils.formatDecimal(scheme.getAmount()),
-
 
472
                    itemBrand.get(0)));
-
 
473
 
-
 
474
            // Message details
-
 
475
            messageBuilder.append(String.format("On Selected Models: %s\n", String.join(", ", itemDescriptions)));
-
 
476
            messageBuilder.append(String.format("Duration from - %s to %s\n\n",
-
 
477
                    FormattingUtils.formatDateMonth(scheme.getStartDateTime()),
-
 
478
                    FormattingUtils.formatDateMonth(scheme.getEndDateTime())));
-
 
479
        }
-
 
480
 
-
 
481
        if (titleBuilder.length() == 0) return;
-
 
482
 
-
 
483
        sendNotificationModel.setCampaignName("activationscheme");
-
 
484
        sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
-
 
485
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
-
 
486
        sendNotificationModel.setTitle(titleBuilder.toString().trim());
-
 
487
        sendNotificationModel.setMessage(messageBuilder.toString().trim());
-
 
488
        sendNotificationModel.setType("url");
-
 
489
        sendNotificationModel.setMessageType(MessageType.scheme);
-
 
490
        notificationService.sendNotificationToAll(sendNotificationModel);
-
 
491
    }
-
 
492
 
-
 
493
    //CDs would be rejected based on cdFreeDays
499
    public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
494
    public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
500
        LOGGER.info("Processing scheme in  for purchaseId - {}", purchaseId);
495
        LOGGER.info("Processing scheme in  for purchaseId - {}", purchaseId);
501
        Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
496
        Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
-
 
497
        String purchaseInvoice = purchase.getPurchaseReference();
-
 
498
 
-
 
499
        Loan loan = loanRepository.selectLoanByInvoice(purchaseInvoice);
-
 
500
 
502
        PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
501
        PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
503
                YearMonth.from(purchase.getCreateTimestamp()));
502
                YearMonth.from(purchase.getCreateTimestamp()));
504
        List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
503
        List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
505
        //Remove imeis from blocked imeis list
504
        //Remove imeis from blocked imeis list
506
        List<String> blockedImeis = this.getBlockedImeis();
505
        List<String> blockedImeis = this.getBlockedImeis();
Line 537... Line 536...
537
            schemeSummaryModels.stream().filter(x -> x != null && x.getSchemeType().getTransactionType().equals(StockTransactionType.IN)).forEach(x -> x.setProcess(true));
536
            schemeSummaryModels.stream().filter(x -> x != null && x.getSchemeType().getTransactionType().equals(StockTransactionType.IN)).forEach(x -> x.setProcess(true));
538
            if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
537
            if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
539
            List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
538
            List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
540
            for (InventoryItem inventoryItem : modelInventoryItems) {
539
            for (InventoryItem inventoryItem : modelInventoryItems) {
541
                float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
540
                float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
-
 
541
                if (loan != null && loan.getCdFreeDays() > 0) {
-
 
542
                    List<SchemeType> rejectedCDTypes = HALF_DAYS_CD_REJECT_SCHEME_TYPES;
-
 
543
                    if (loan.getCdFreeDays() == ProfitMandiConstants.LOAN_FULL_CREDIT_DAYS) {
-
 
544
                        rejectedCDTypes = FULL_DAYS_CD_SCHEME_TYPES;
-
 
545
                    }
-
 
546
                    for (SchemeType rejectedCDType : rejectedCDTypes) {
-
 
547
                        SchemeInOut sio = schemeInOutRepository.selectAllByType(rejectedCDType, inventoryItem.getId()).get(0);
-
 
548
                        sio.setStatus(SchemePayoutStatus.REJECTED);
-
 
549
                        sio.setStatusDescription("Rejected due to free days availed");
-
 
550
                        sio.setRolledBackTimestamp(LocalDateTime.now());
-
 
551
                        inventoryItemCashback -= sio.getAmount();
-
 
552
                    }
-
 
553
 
-
 
554
                }
542
                if (inventoryItemCashback > 0) {
555
                if (inventoryItemCashback > 0.01f) {
543
                    itemsCount++;
556
                    itemsCount++;
544
                    totalCashback += inventoryItemCashback;
557
                    totalCashback += inventoryItemCashback;
545
                }
558
                }
546
            }
559
            }
547
        }
560
        }
Line 630... Line 643...
630
                    sio.setStatus(SchemePayoutStatus.PENDING);
643
                    sio.setStatus(SchemePayoutStatus.PENDING);
631
                    schemeInOutRepository.persist(sio);
644
                    schemeInOutRepository.persist(sio);
632
                }
645
                }
633
            } else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
646
            } else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
634
                //We have got non repeating scheme type
647
                //We have got non repeating scheme type
635
                if (paidSchemeTypesMap.containsKey(schemeSummaryModelToProcess.getSchemeType())) {
-
 
636
                    SchemeInOut sio = paidSchemeTypesMap.get(schemeSummaryModelToProcess.getSchemeType()).get(0);
-
 
637
                    Scheme paidScheme = sio.getScheme();
-
 
638
                    //Don't entertain if amount types do not match for same type
-
 
639
                    if (paidScheme.getAmountType().equals(schemeSummaryModelToProcess.getAmountType()) &&
-
 
640
                            schemeSummaryModelToProcess.getAmount() > paidScheme.getAmount() + Utils.DOUBLE_EPSILON) {
-
 
641
                        sio.setRolledBackTimestamp(LocalDateTime.now());
-
 
642
                        sio.setStatus(SchemePayoutStatus.REJECTED);
-
 
643
                        sio.setStatusDescription("Eligible for higher margin for " + schemeSummaryModelToProcess.getSchemeType() + ", new entry added");
-
 
644
                        schemeTypeCancelledAmountMap.put(schemeSummaryModelToProcess.getSchemeType(), sio.getAmount());
-
 
-
 
648
 
645
                        if (paidScheme.getAmountType().equals(AmountType.PERCENTAGE)) {
649
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
646
                            percentageToPay += schemeSummaryModelToProcess.getAmount() - paidScheme.getAmount();
650
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
647
                        } else {
-
 
648
                            fixedToPay += schemeSummaryModelToProcess.getAmount() - paidScheme.getAmount();
-
 
649
                        }
-
 
650
                        payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount() - paidScheme.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
651
                    }
-
 
652
                } else {
651
                } else {
653
                    if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
-
 
654
                        percentageToPay += schemeSummaryModelToProcess.getAmount();
-
 
655
                    } else {
-
 
656
                        fixedToPay += schemeSummaryModelToProcess.getAmount();
652
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
657
                    }
-
 
658
                    payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
659
                }
653
                }
-
 
654
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
655
 
660
            } else {
656
            } else {
661
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
657
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
-
 
658
                    //Check for rejected CashDiscounts dont continue if its ever rejected once
-
 
659
                    if (SchemeType.CDS.contains(schemeSummaryModelToProcess.getSchemeType())) {
-
 
660
                        List<SchemeInOut> cdSchemeInOuts = schemeInOutRepository.selectAllByType(schemeSummaryModelToProcess.getSchemeType(), inventoryItem.getId());
-
 
661
                        LOGGER.info(cdSchemeInOuts);
-
 
662
                        if (!cdSchemeInOuts.isEmpty()) continue;
-
 
663
                    }
662
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
664
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
663
                } else {
665
                } else {
664
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
666
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
665
                }
667
                }
666
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
668
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
Line 806... Line 808...
806
                if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
808
                if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
807
 
809
 
808
                List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
810
                List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
809
                for (InventoryItem inventoryItem : modelInventoryItems) {
811
                for (InventoryItem inventoryItem : modelInventoryItems) {
810
                    Map<Integer, SchemeSummaryModel> schemeSummaryModelMap = schemeSummaryModels.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
812
                    Map<Integer, SchemeSummaryModel> schemeSummaryModelMap = schemeSummaryModels.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
811
                    List<SchemeInOut> siosToReject = schemeInOutRepository.selectAll(inventoryItem.getId())
813
                    List<SchemeInOut> sios = schemeInOutRepository.selectAll(inventoryItem.getId());
-
 
814
                    List<Integer> creditedSchemeIds = sios.stream()
812
                            .stream().filter(x -> x.getStatus().equals(SchemePayoutStatus.CREDITED))
815
                            .filter(x -> x.getStatus().equals(SchemePayoutStatus.CREDITED))
813
                            .filter(x -> !schemeSummaryModelMap.containsKey(x.getSchemeId())).collect(Collectors.toList());
816
                            .map(x -> x.getSchemeId()).collect(Collectors.toList());
-
 
817
                    List<Integer> schemeIdsToReject = schemeRepository.selectBySchemeIds(creditedSchemeIds).stream().filter(x -> SchemeType.OUT_TYPES.contains(x.getType())).map(x -> x.getId()).collect(Collectors.toList());
814
 
818
 
815
                    //Reject invalid scheme payouts due to upgrade in Category or any change in schemes historically
819
                    //Reject invalid scheme payouts due to upgrade in Category or any change in schemes historically
816
                    for (SchemeInOut sioToReject : siosToReject) {
820
                    for (SchemeInOut sio : sios) {
-
 
821
                        if (schemeIdsToReject.contains(sio.getSchemeId())) {
817
                        sioToReject.setStatusDescription("Rolledback due to Category upgrade/invalid scheme");
822
                            sio.setStatusDescription("Rolledback due to Category upgrade/invalid scheme");
818
                        sioToReject.setStatus(SchemePayoutStatus.REJECTED);
823
                            sio.setStatus(SchemePayoutStatus.REJECTED);
819
                        sioToReject.setRolledBackTimestamp(LocalDateTime.now());
824
                            sio.setRolledBackTimestamp(LocalDateTime.now());
820
                        totalCashback -= sioToReject.getAmount();
825
                            totalCashback -= sio.getAmount();
-
 
826
                        }
821
                    }
827
                    }
822
                    float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
828
                    float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
823
                    if (inventoryItemCashback > 0) {
829
                    if (inventoryItemCashback > 0) {
824
                        count++;
830
                        count++;
825
                        totalCashback += inventoryItemCashback;
831
                        totalCashback += inventoryItemCashback;