Subversion Repositories SmartDukaan

Rev

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

Rev 34706 Rev 34708
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;
-
 
17
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
16
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
18
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
17
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
19
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
18
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
20
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
19
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
21
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
20
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
Line 26... Line 25...
26
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
25
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
27
import com.spice.profitmandi.dao.repository.catalog.*;
26
import com.spice.profitmandi.dao.repository.catalog.*;
28
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
27
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
29
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
28
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
30
import com.spice.profitmandi.dao.repository.fofo.*;
29
import com.spice.profitmandi.dao.repository.fofo.*;
31
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
-
 
32
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
30
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
33
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
31
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
34
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
32
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
35
import com.spice.profitmandi.dao.repository.warehouse.AgeingSummaryModel;
33
import com.spice.profitmandi.dao.repository.warehouse.AgeingSummaryModel;
36
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
34
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
Line 69... Line 67...
69
@Component
67
@Component
70
public class SchemeServiceImpl implements SchemeService {
68
public class SchemeServiceImpl implements SchemeService {
71
 
69
 
72
    private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
70
    private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
73
    private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
71
    private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
-
 
72
//    //These Imeis provided by deena
74
    private static final List<SchemeType> FULL_DAYS_CD_SCHEME_TYPES = Arrays.asList(SchemeType.CASH_DISCOUNT, SchemeType.CASH_DISCOUNT1);
73
//    private static final List<String> BLOCKED_IMEIS =
-
 
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",
75
    private static final List<SchemeType> HALF_DAYS_CD_REJECT_SCHEME_TYPES = Arrays.asList(SchemeType.CASH_DISCOUNT1);
93
//                    "860948067442573", "860948069561750", "860948069561479", "860948069561974", "860948069562550", "860948069560679", "860948069560190", "860948069562691", "860948069735859"
76
 
94
//    );
77
 
95
 
78
    private static final List<SchemeType> ACTIVATION_SCHEME_TYPES = Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT);
96
    private static final List<SchemeType> ACTIVATION_SCHEME_TYPES = Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT);
79
 
97
 
80
    @Autowired
98
    @Autowired
81
    SchemeBlockedImeiRepository schemeBlockedImeiRepository;
99
    SchemeBlockedImeiRepository schemeBlockedImeiRepository;
Line 342... Line 360...
342
            sendNotificationModel.setType("url");
360
            sendNotificationModel.setType("url");
343
            sendNotificationModel.setMessageType(MessageType.scheme);
361
            sendNotificationModel.setMessageType(MessageType.scheme);
344
            notificationService.sendNotificationToAll(sendNotificationModel);
362
            notificationService.sendNotificationToAll(sendNotificationModel);
345
        }
363
        }
346
    }
364
    }
-
 
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;
347
    @Autowired
373
            }
-
 
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());
348
    LoanRepository loanRepository;
408
        sendNotificationModel.setType("url");
-
 
409
        sendNotificationModel.setMessageType(MessageType.scheme);
-
 
410
        notificationService.sendNotificationToAll(sendNotificationModel);
-
 
411
    }
349
 
412
 
350
    @Override
413
    @Override
351
    public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
414
    public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
352
        Scheme scheme = schemeRepository.selectById(schemeId);
415
        Scheme scheme = schemeRepository.selectById(schemeId);
353
        if (scheme == null || scheme.getActiveTimestamp() == null) {
416
        if (scheme == null || scheme.getActiveTimestamp() == null) {
Line 431... Line 494...
431
    @Autowired
494
    @Autowired
432
    OfferPayoutRepository offerPayoutRepository;
495
    OfferPayoutRepository offerPayoutRepository;
433
    @Autowired
496
    @Autowired
434
    AgeingService ageingService;
497
    AgeingService ageingService;
435
 
498
 
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
-
 
494
    public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
499
    public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
495
        LOGGER.info("Processing scheme in  for purchaseId - {}", purchaseId);
500
        LOGGER.info("Processing scheme in  for purchaseId - {}", purchaseId);
496
        Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
501
        Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
497
        String purchaseInvoice = purchase.getPurchaseReference();
-
 
498
 
-
 
499
        Loan loan = loanRepository.selectLoanByInvoice(purchaseInvoice);
-
 
500
 
-
 
501
        PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
502
        PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
502
                YearMonth.from(purchase.getCreateTimestamp()));
503
                YearMonth.from(purchase.getCreateTimestamp()));
503
        List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
504
        List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
504
        //Remove imeis from blocked imeis list
505
        //Remove imeis from blocked imeis list
505
        List<String> blockedImeis = this.getBlockedImeis();
506
        List<String> blockedImeis = this.getBlockedImeis();
Line 527... Line 528...
527
                partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), billingDate);
528
                partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), billingDate);
528
 
529
 
529
 
530
 
530
        int itemsCount = 0;
531
        int itemsCount = 0;
531
        float totalCashback = 0;
532
        float totalCashback = 0;
532
        //LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
-
 
533
        for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
533
        for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
534
            CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
534
            CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
535
            List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList());
535
            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));
536
            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;
537
            if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
538
            List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
538
            List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
539
            for (InventoryItem inventoryItem : modelInventoryItems) {
539
            for (InventoryItem inventoryItem : modelInventoryItems) {
540
                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
                }
-
 
555
                if (inventoryItemCashback > 0.01f) {
541
                if (inventoryItemCashback > 0) {
556
                    itemsCount++;
542
                    itemsCount++;
557
                    totalCashback += inventoryItemCashback;
543
                    totalCashback += inventoryItemCashback;
558
                }
544
                }
559
            }
545
            }
560
        }
546
        }
Line 613... Line 599...
613
    private float createSchemeInOut(List<SchemeSummaryModel> schemeSummaryModels, InventoryItem inventoryItem) throws ProfitMandiBusinessException {
599
    private float createSchemeInOut(List<SchemeSummaryModel> schemeSummaryModels, InventoryItem inventoryItem) throws ProfitMandiBusinessException {
614
        LOGGER.info("schemeSummaryModels - {}", schemeSummaryModels);
600
        LOGGER.info("schemeSummaryModels - {}", schemeSummaryModels);
615
        InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItem);
601
        InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItem);
616
        //Get all schemes
602
        //Get all schemes
617
        List<SchemeSummaryModel> inventoryPayoutModelToProcess = schemeSummaryModels.stream().filter(x -> x.isProcess()).collect(Collectors.toList());
603
        List<SchemeSummaryModel> inventoryPayoutModelToProcess = schemeSummaryModels.stream().filter(x -> x.isProcess()).collect(Collectors.toList());
618
        LOGGER.info("inventoryPayoutModel - {}", inventoryPayoutModel);
-
 
-
 
604
 
619
        List<SchemeInOut> paidSios = inventoryPayoutModel.getPaidSios();
605
        List<SchemeInOut> paidSios = inventoryPayoutModel.getPaidSios();
620
        List<SchemeInOut> pendingSios = inventoryPayoutModel.getPendingSios();
606
        List<SchemeInOut> pendingSios = inventoryPayoutModel.getPendingSios();
621
        Map<SchemeType, List<SchemeInOut>> paidSchemeTypesMap = inventoryPayoutModel.getPaidSios().stream().collect(Collectors.groupingBy(x -> x.getScheme().getType()));
607
        Map<SchemeType, List<SchemeInOut>> paidSchemeTypesMap = inventoryPayoutModel.getPaidSios().stream().collect(Collectors.groupingBy(x -> x.getScheme().getType()));
622
        Map<Integer, SchemeInOut> paidSchemesMap = paidSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
608
        Map<Integer, SchemeInOut> paidSchemesMap = paidSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
623
        Map<Integer, SchemeInOut> pendingSchemesMap = pendingSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
609
        Map<Integer, SchemeInOut> pendingSchemesMap = pendingSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
Line 643... Line 629...
643
                    sio.setStatus(SchemePayoutStatus.PENDING);
629
                    sio.setStatus(SchemePayoutStatus.PENDING);
644
                    schemeInOutRepository.persist(sio);
630
                    schemeInOutRepository.persist(sio);
645
                }
631
                }
646
            } else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
632
            } else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
647
                //We have got non repeating scheme type
633
                //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) {
648
 
-
 
-
 
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
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
644
                        if (paidScheme.getAmountType().equals(AmountType.PERCENTAGE)) {
650
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
645
                            percentageToPay += schemeSummaryModelToProcess.getAmount() - paidScheme.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 {
651
                } else {
-
 
652
                    if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
-
 
653
                        percentageToPay += schemeSummaryModelToProcess.getAmount();
-
 
654
                    } else {
652
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
655
                        fixedToPay += schemeSummaryModelToProcess.getAmount();
-
 
656
                    }
-
 
657
                    payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
653
                }
658
                }
654
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
-
 
655
 
-
 
656
            } else {
659
            } else {
657
                if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
660
                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
                    }
-
 
664
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
661
                    percentageToPay += schemeSummaryModelToProcess.getAmount();
665
                } else {
662
                } else {
666
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
663
                    fixedToPay += schemeSummaryModelToProcess.getAmount();
667
                }
664
                }
668
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
665
                payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
Line 1149... Line 1146...
1149
            int retailerId = purchaseIdInventoryItemEntry.getValue().get(0).getFofoId();
1146
            int retailerId = purchaseIdInventoryItemEntry.getValue().get(0).getFofoId();
1150
            this.processSchemeIn(purchaseIdInventoryItemEntry.getKey(), retailerId);
1147
            this.processSchemeIn(purchaseIdInventoryItemEntry.getKey(), retailerId);
1151
        }
1148
        }
1152
    }
1149
    }
1153
 
1150
 
1154
    /*@Override
-
 
1155
    public void processFullCD(String invoiceNumber) throws ProfitMandiBusinessException {
-
 
1156
        Purchase purchase = purchaseRepository.selectByPurchaseReference(invoiceNumber);
-
 
1157
        this.processSchemeIn(purchase.getId(), purchase.getFofoId(), SchemeType.CDS);
-
 
1158
    }
-
 
1159
 
-
 
1160
    @Override
-
 
1161
    public void processPartialCD(String invoiceNumber) throws ProfitMandiBusinessException {
-
 
1162
        Purchase purchase = purchaseRepository.selectByPurchaseReference(invoiceNumber);
-
 
1163
        this.processSchemeIn(purchase.getId(), purchase.getFofoId(), Arrays.asList(SchemeType.CASH_DISCOUNT));
-
 
1164
    }
-
 
1165
 
-
 
1166
    @Override
-
 
1167
    public void processNoCD(String invoiceNumber) throws ProfitMandiBusinessException {
-
 
1168
        Purchase purchase = purchaseRepository.selectByPurchaseReference(invoiceNumber);
-
 
1169
        this.processSchemeIn(purchase.getId(), purchase.getFofoId(), Arrays.asList());
-
 
1170
    }*/
-
 
1171
 
-
 
1172
    @Autowired
1151
    @Autowired
1173
    FofoStoreRepository fofoStoreRepository;
1152
    FofoStoreRepository fofoStoreRepository;
1174
 
1153
 
1175
 
1154
 
1176
    @Override
1155
    @Override