Subversion Repositories SmartDukaan

Rev

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

Rev 34708 Rev 34709
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 361... Line 343...
361
            sendNotificationModel.setMessageType(MessageType.scheme);
343
            sendNotificationModel.setMessageType(MessageType.scheme);
362
            notificationService.sendNotificationToAll(sendNotificationModel);
344
            notificationService.sendNotificationToAll(sendNotificationModel);
363
        }
345
        }
364
    }
346
    }
365
 
347
 
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
            }
348
    @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");
349
    LoanRepository loanRepository;
409
        sendNotificationModel.setMessageType(MessageType.scheme);
-
 
410
        notificationService.sendNotificationToAll(sendNotificationModel);
-
 
411
    }
-
 
412
 
350
 
413
    @Override
351
    @Override
414
    public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
352
    public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
415
        Scheme scheme = schemeRepository.selectById(schemeId);
353
        Scheme scheme = schemeRepository.selectById(schemeId);
416
        if (scheme == null || scheme.getActiveTimestamp() == null) {
354
        if (scheme == null || scheme.getActiveTimestamp() == null) {
Line 494... Line 432...
494
    @Autowired
432
    @Autowired
495
    OfferPayoutRepository offerPayoutRepository;
433
    OfferPayoutRepository offerPayoutRepository;
496
    @Autowired
434
    @Autowired
497
    AgeingService ageingService;
435
    AgeingService ageingService;
498
 
436
 
-
 
437
    private void sendCombinedSchemesNotification(List<Scheme> schemes) throws ProfitMandiBusinessException {
-
 
438
        if (schemes == null || schemes.isEmpty()) return;
-
 
439
 
-
 
440
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
-
 
441
        StringBuilder messageBuilder = new StringBuilder();
-
 
442
        StringBuilder titleBuilder = new StringBuilder();
-
 
443
        Set<Integer> allCatalogIds = new HashSet<>();
-
 
444
 
-
 
445
        for (Scheme scheme : schemes) {
-
 
446
            if (!ACTIVATION_SCHEME_TYPES.contains(scheme.getType())) {
-
 
447
                continue;
-
 
448
            }
-
 
449
 
-
 
450
            String schemeTypeLabel = "Activation scheme";
-
 
451
            if (scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
-
 
452
                schemeTypeLabel = "Special Support";
-
 
453
            }
-
 
454
 
-
 
455
            List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
-
 
456
            Set<Integer> catalogIds = schemeItems.stream().map(SchemeItem::getCatalogId).collect(Collectors.toSet());
-
 
457
            allCatalogIds.addAll(catalogIds);
-
 
458
 
-
 
459
            List<String> itemBrand = itemRepository.selectAllByCatalogIds(catalogIds).stream()
-
 
460
                    .filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
461
                    .map(Item::getBrand)
-
 
462
                    .collect(Collectors.toList());
-
 
463
 
-
 
464
            List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream()
-
 
465
                    .filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
466
                    .map(Item::getItemDescriptionNoColor)
-
 
467
                    .collect(Collectors.toList());
-
 
468
 
-
 
469
            // Title line
-
 
470
            titleBuilder.append(String.format("%s of Rs.%s for %s\n",
-
 
471
                    schemeTypeLabel,
-
 
472
                    FormattingUtils.formatDecimal(scheme.getAmount()),
-
 
473
                    itemBrand.get(0)));
-
 
474
 
-
 
475
            // Message details
-
 
476
            messageBuilder.append(String.format("On Selected Models: %s\n", String.join(", ", itemDescriptions)));
-
 
477
            messageBuilder.append(String.format("Duration from - %s to %s\n\n",
-
 
478
                    FormattingUtils.formatDateMonth(scheme.getStartDateTime()),
-
 
479
                    FormattingUtils.formatDateMonth(scheme.getEndDateTime())));
-
 
480
        }
-
 
481
 
-
 
482
        if (titleBuilder.length() == 0) return;
-
 
483
 
-
 
484
        sendNotificationModel.setCampaignName("activationscheme");
-
 
485
        sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
-
 
486
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
-
 
487
        sendNotificationModel.setTitle(titleBuilder.toString().trim());
-
 
488
        sendNotificationModel.setMessage(messageBuilder.toString().trim());
-
 
489
        sendNotificationModel.setType("url");
-
 
490
        sendNotificationModel.setMessageType(MessageType.scheme);
-
 
491
        notificationService.sendNotificationToAll(sendNotificationModel);
-
 
492
    }
-
 
493
 
-
 
494
    //CDs would be rejected based on cdFreeDays
499
    public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
495
    public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
500
        LOGGER.info("Processing scheme in  for purchaseId - {}", purchaseId);
496
        LOGGER.info("Processing scheme in  for purchaseId - {}", purchaseId);
501
        Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
497
        Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
-
 
498
        String purchaseInvoice = purchase.getPurchaseReference();
-
 
499
 
-
 
500
        Loan loan = loanRepository.selectLoanByInvoice(purchaseInvoice);
-
 
501
 
502
        PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
502
        PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
503
                YearMonth.from(purchase.getCreateTimestamp()));
503
                YearMonth.from(purchase.getCreateTimestamp()));
504
        List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
504
        List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
505
        //Remove imeis from blocked imeis list
505
        //Remove imeis from blocked imeis list
506
        List<String> blockedImeis = this.getBlockedImeis();
506
        List<String> blockedImeis = this.getBlockedImeis();
Line 528... Line 528...
528
                partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), billingDate);
528
                partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), billingDate);
529
 
529
 
530
 
530
 
531
        int itemsCount = 0;
531
        int itemsCount = 0;
532
        float totalCashback = 0;
532
        float totalCashback = 0;
-
 
533
        //LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
533
        for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
534
        for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
534
            CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
535
            CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
535
            List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList());
536
            List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList());
536
            schemeSummaryModels.stream().filter(x -> x != null && x.getSchemeType().getTransactionType().equals(StockTransactionType.IN)).forEach(x -> x.setProcess(true));
537
            schemeSummaryModels.stream().filter(x -> x != null && x.getSchemeType().getTransactionType().equals(StockTransactionType.IN)).forEach(x -> x.setProcess(true));
537
            if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
538
            if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
538
            List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
539
            List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
539
            for (InventoryItem inventoryItem : modelInventoryItems) {
540
            for (InventoryItem inventoryItem : modelInventoryItems) {
540
                float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
541
                float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
-
 
542
                if (loan != null && loan.getCdFreeDays() > 0) {
-
 
543
                    List<SchemeType> rejectedCDTypes = HALF_DAYS_CD_REJECT_SCHEME_TYPES;
-
 
544
                    if (loan.getCdFreeDays() == ProfitMandiConstants.LOAN_FULL_CREDIT_DAYS) {
-
 
545
                        rejectedCDTypes = FULL_DAYS_CD_SCHEME_TYPES;
-
 
546
                    }
-
 
547
                    for (SchemeType rejectedCDType : rejectedCDTypes) {
-
 
548
                        SchemeInOut sio = schemeInOutRepository.selectAllByType(rejectedCDType, inventoryItem.getId()).get(0);
-
 
549
                        sio.setStatus(SchemePayoutStatus.REJECTED);
-
 
550
                        sio.setStatusDescription("Rejected due to free days availed");
-
 
551
                        sio.setRolledBackTimestamp(LocalDateTime.now());
-
 
552
                        inventoryItemCashback -= sio.getAmount();
-
 
553
                    }
-
 
554
 
-
 
555
                }
541
                if (inventoryItemCashback > 0) {
556
                if (inventoryItemCashback > 0.01f) {
542
                    itemsCount++;
557
                    itemsCount++;
543
                    totalCashback += inventoryItemCashback;
558
                    totalCashback += inventoryItemCashback;
544
                }
559
                }
545
            }
560
            }
546
        }
561
        }
Line 599... Line 614...
599
    private float createSchemeInOut(List<SchemeSummaryModel> schemeSummaryModels, InventoryItem inventoryItem) throws ProfitMandiBusinessException {
614
    private float createSchemeInOut(List<SchemeSummaryModel> schemeSummaryModels, InventoryItem inventoryItem) throws ProfitMandiBusinessException {
600
        LOGGER.info("schemeSummaryModels - {}", schemeSummaryModels);
615
        LOGGER.info("schemeSummaryModels - {}", schemeSummaryModels);
601
        InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItem);
616
        InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItem);
602
        //Get all schemes
617
        //Get all schemes
603
        List<SchemeSummaryModel> inventoryPayoutModelToProcess = schemeSummaryModels.stream().filter(x -> x.isProcess()).collect(Collectors.toList());
618
        List<SchemeSummaryModel> inventoryPayoutModelToProcess = schemeSummaryModels.stream().filter(x -> x.isProcess()).collect(Collectors.toList());
604
 
-
 
-
 
619
        LOGGER.info("inventoryPayoutModel - {}", inventoryPayoutModel);
605
        List<SchemeInOut> paidSios = inventoryPayoutModel.getPaidSios();
620
        List<SchemeInOut> paidSios = inventoryPayoutModel.getPaidSios();
606
        List<SchemeInOut> pendingSios = inventoryPayoutModel.getPendingSios();
621
        List<SchemeInOut> pendingSios = inventoryPayoutModel.getPendingSios();
607
        Map<SchemeType, List<SchemeInOut>> paidSchemeTypesMap = inventoryPayoutModel.getPaidSios().stream().collect(Collectors.groupingBy(x -> x.getScheme().getType()));
622
        Map<SchemeType, List<SchemeInOut>> paidSchemeTypesMap = inventoryPayoutModel.getPaidSios().stream().collect(Collectors.groupingBy(x -> x.getScheme().getType()));
608
        Map<Integer, SchemeInOut> paidSchemesMap = paidSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
623
        Map<Integer, SchemeInOut> paidSchemesMap = paidSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
609
        Map<Integer, SchemeInOut> pendingSchemesMap = pendingSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
624
        Map<Integer, SchemeInOut> pendingSchemesMap = pendingSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
Line 629... Line 644...
629
                    sio.setStatus(SchemePayoutStatus.PENDING);
644
                    sio.setStatus(SchemePayoutStatus.PENDING);
630
                    schemeInOutRepository.persist(sio);
645
                    schemeInOutRepository.persist(sio);
631
                }
646
                }
632
            } else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
647
            } else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
633
                //We have got non repeating scheme type
648
                //We have got non repeating scheme type
634
                if (paidSchemeTypesMap.containsKey(schemeSummaryModelToProcess.getSchemeType())) {
-
 
635
                    SchemeInOut sio = paidSchemeTypesMap.get(schemeSummaryModelToProcess.getSchemeType()).get(0);
-
 
636
                    Scheme paidScheme = sio.getScheme();
-
 
637
                    //Don't entertain if amount types do not match for same type
-
 
638
                    if (paidScheme.getAmountType().equals(schemeSummaryModelToProcess.getAmountType()) &&
-
 
639
                            schemeSummaryModelToProcess.getAmount() > paidScheme.getAmount() + Utils.DOUBLE_EPSILON) {
-
 
640
                        sio.setRolledBackTimestamp(LocalDateTime.now());
-
 
641
                        sio.setStatus(SchemePayoutStatus.REJECTED);
-
 
642
                        sio.setStatusDescription("Eligible for higher margin for " + schemeSummaryModelToProcess.getSchemeType() + ", new entry added");
-
 
643
                        schemeTypeCancelledAmountMap.put(schemeSummaryModelToProcess.getSchemeType(), sio.getAmount());
-
 
-
 
649
 
644
                        if (paidScheme.getAmountType().equals(AmountType.PERCENTAGE)) {
650
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
645
                            percentageToPay += schemeSummaryModelToProcess.getAmount() - paidScheme.getAmount();
651
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
646
                        } else {
-
 
647
                            fixedToPay += schemeSummaryModelToProcess.getAmount() - paidScheme.getAmount();
-
 
648
                        }
-
 
649
                        payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount() - paidScheme.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
650
                    }
-
 
651
                } else {
652
                } else {
652
                    if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
-
 
653
                        percentageToPay += schemeSummaryModelToProcess.getAmount();
-
 
654
                    } else {
-
 
655
                        fixedToPay += schemeSummaryModelToProcess.getAmount();
653
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
656
                    }
-
 
657
                    payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
658
                }
654
                }
-
 
655
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
656
 
659
            } else {
657
            } else {
660
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
658
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
-
 
659
                    //Check for rejected CashDiscounts dont continue if its ever rejected once
-
 
660
                    if (SchemeType.CDS.contains(schemeSummaryModelToProcess.getSchemeType())) {
-
 
661
                        List<SchemeInOut> cdSchemeInOuts = schemeInOutRepository.selectAllByType(schemeSummaryModelToProcess.getSchemeType(), inventoryItem.getId());
-
 
662
                        LOGGER.info(cdSchemeInOuts);
-
 
663
                        if (!cdSchemeInOuts.isEmpty()) continue;
-
 
664
                    }
661
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
665
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
662
                } else {
666
                } else {
663
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
667
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
664
                }
668
                }
665
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
669
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));