| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.service.scheme;
|
1 |
package com.spice.profitmandi.service.scheme;
|
| 2 |
|
2 |
|
| 3 |
import java.text.MessageFormat;
|
3 |
import java.text.MessageFormat;
|
| 4 |
import java.time.LocalDate;
|
- |
|
| 5 |
import java.time.LocalDateTime;
|
4 |
import java.time.LocalDateTime;
|
| 6 |
import java.util.ArrayList;
|
5 |
import java.util.ArrayList;
|
| 7 |
import java.util.Arrays;
|
6 |
import java.util.Arrays;
|
| 8 |
import java.util.Collection;
|
- |
|
| 9 |
import java.util.HashMap;
|
7 |
import java.util.HashMap;
|
| 10 |
import java.util.HashSet;
|
8 |
import java.util.HashSet;
|
| 11 |
import java.util.List;
|
9 |
import java.util.List;
|
| 12 |
import java.util.Map;
|
10 |
import java.util.Map;
|
| 13 |
import java.util.Set;
|
11 |
import java.util.Set;
|
| 14 |
import java.util.AbstractMap.SimpleEntry;
|
- |
|
| 15 |
import java.util.stream.Collectors;
|
12 |
import java.util.stream.Collectors;
|
| 16 |
|
13 |
|
| 17 |
import javax.persistence.criteria.CriteriaBuilder;
|
14 |
import javax.persistence.criteria.CriteriaBuilder;
|
| 18 |
import javax.persistence.criteria.CriteriaQuery;
|
15 |
import javax.persistence.criteria.CriteriaQuery;
|
| 19 |
import javax.persistence.criteria.Predicate;
|
16 |
import javax.persistence.criteria.Predicate;
|
| Line 29... |
Line 26... |
| 29 |
import org.springframework.cache.annotation.Cacheable;
|
26 |
import org.springframework.cache.annotation.Cacheable;
|
| 30 |
import org.springframework.stereotype.Component;
|
27 |
import org.springframework.stereotype.Component;
|
| 31 |
|
28 |
|
| 32 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
29 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 33 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
30 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34 |
import com.spice.profitmandi.common.model.CatalogIdAggregateValue;
|
- |
|
| 35 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
31 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 36 |
import com.spice.profitmandi.common.model.SchemeModel;
|
32 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 37 |
import com.spice.profitmandi.common.util.StringUtils;
|
33 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 38 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
34 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 39 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
- |
|
| 40 |
import com.spice.profitmandi.dao.entity.catalog.OfferPartner;
|
- |
|
| 41 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
35 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
| 42 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
36 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| 43 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
37 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 44 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
- |
|
| 45 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
38 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 46 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
39 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 47 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
40 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 48 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
41 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 49 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
42 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
| 50 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
43 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 51 |
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
|
44 |
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
|
| 52 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
45 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 53 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
46 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 54 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
47 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| - |
|
48 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 55 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
49 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
| 56 |
import com.spice.profitmandi.dao.repository.GenericRepository;
|
- |
|
| 57 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
50 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 58 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
51 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 59 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
52 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 60 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
53 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 61 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
54 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
| Line 478... |
Line 471... |
| 478 |
}
|
471 |
}
|
| 479 |
}
|
472 |
}
|
| 480 |
|
473 |
|
| 481 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
474 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
| 482 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
475 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
| 483 |
float amount = 0;
|
476 |
float amountToCredit = 0;
|
| 484 |
if (schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList())
|
477 |
if (schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList())
|
| 485 |
.size() == 0) {
|
478 |
.size() == 0) {
|
| 486 |
SchemeInOut schemeInOut = new SchemeInOut();
|
479 |
SchemeInOut schemeInOut = new SchemeInOut();
|
| 487 |
amount = this.getAmount(inventoryItem, scheme);
|
480 |
amountToCredit = this.getAmount(inventoryItem, scheme);
|
| 488 |
schemeInOut.setSchemeId(scheme.getId());
|
481 |
schemeInOut.setSchemeId(scheme.getId());
|
| 489 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
482 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
| 490 |
schemeInOut.setAmount(amount);
|
483 |
schemeInOut.setAmount(amountToCredit);
|
| - |
|
484 |
if(scheme.getType().equals(SchemeType.ACTIVATION)) {
|
| - |
|
485 |
schemeInOut.setStatus(SchemePayoutStatus.PENDING);
|
| - |
|
486 |
schemeInOut.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
| - |
|
487 |
return 0;
|
| - |
|
488 |
} else {
|
| - |
|
489 |
schemeInOut.setStatus(SchemePayoutStatus.CREDITED);
|
| - |
|
490 |
schemeInOut.setStatusDescription("Credited for sale of IMEI#" + inventoryItem.getSerialNumber());
|
| - |
|
491 |
schemeInOut.setCreditTimestamp(LocalDateTime.now());
|
| - |
|
492 |
}
|
| 491 |
schemeInOutRepository.persist(schemeInOut);
|
493 |
schemeInOutRepository.persist(schemeInOut);
|
| 492 |
}
|
494 |
}
|
| 493 |
return amount;
|
495 |
return amountToCredit;
|
| 494 |
}
|
496 |
}
|
| 495 |
|
497 |
|
| 496 |
// We are maintaining price drop after grn
|
498 |
// We are maintaining price drop after grn
|
| 497 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
499 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
| 498 |
float amount = 0;
|
500 |
float amount = 0;
|
| Line 563... |
Line 565... |
| 563 |
}
|
565 |
}
|
| 564 |
|
566 |
|
| 565 |
float totalCashback = 0;
|
567 |
float totalCashback = 0;
|
| 566 |
int count = 0;
|
568 |
int count = 0;
|
| 567 |
|
569 |
|
| 568 |
Set<Scheme> allFixedSchemes = schemeRepository
|
570 |
List<Scheme> allActiveSchemes = schemeRepository
|
| 569 |
.selectActiveAll(SchemeType.OUT, partnerType, fofoOrder.getCreateTimestamp(), false).stream()
|
571 |
.selectActiveAll(SchemeType.OUT, partnerType, fofoOrder.getCreateTimestamp(), false);
|
| 570 |
.filter(scheme -> scheme.getAmountType().equals(AmountType.FIXED)).collect(Collectors.toSet());
|
572 |
allActiveSchemes.addAll(schemeRepository.selectActiveAll(SchemeType.ACTIVATION, partnerType, fofoOrder.getCreateTimestamp(), false));
|
| 571 |
|
573 |
|
| 572 |
for (InventoryItem inventoryItem : inventoryItems) {
|
574 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 573 |
float itemCashback = 0;
|
575 |
float itemCashback = 0;
|
| 574 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(inventoryItem.getPurchaseId(), retailerId);
|
576 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(inventoryItem.getPurchaseId(), retailerId);
|
| 575 |
Set<Integer> schemeIds = new HashSet<>(
|
577 |
Set<Integer> schemeIds = new HashSet<>(
|
| 576 |
schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
|
578 |
schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
|
| 577 |
List<Scheme> schemes = null;
|
- |
|
| 578 |
if (purchase.getCompleteTimestamp() == null) {
|
579 |
if (purchase.getCompleteTimestamp() == null) {
|
| 579 |
continue;
|
580 |
continue;
|
| 580 |
}
|
581 |
}
|
| 581 |
if (purchase.getCompleteTimestamp().isAfter(LocalDate.of(2019, 9, 1).atStartOfDay())) {
|
- |
|
| 582 |
schemes = schemeRepository.selectActiveAll(SchemeType.OUT, partnerType, fofoOrder.getCreateTimestamp(),
|
- |
|
| 583 |
false);
|
- |
|
| 584 |
} else {
|
- |
|
| 585 |
schemes = schemeRepository.selectActiveAll(SchemeType.OUT, partnerType, purchase.getCompleteTimestamp(),
|
- |
|
| 586 |
false);
|
- |
|
| 587 |
}
|
- |
|
| 588 |
Set<Scheme> schemesSet = schemes.stream().filter(x -> x.getAmountType().equals(AmountType.PERCENTAGE))
|
- |
|
| 589 |
.collect(Collectors.toSet());
|
- |
|
| 590 |
schemesSet.addAll(allFixedSchemes);
|
- |
|
| 591 |
schemesSet = schemesSet.stream().filter(x -> schemeIds.contains(x.getId())).collect(Collectors.toSet());
|
582 |
allActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId())).collect(Collectors.toList());
|
| 592 |
for (Scheme scheme : schemesSet) {
|
583 |
for (Scheme scheme : allActiveSchemes) {
|
| 593 |
itemCashback += this.createSchemeInOut(scheme, inventoryItem);
|
584 |
itemCashback += this.createSchemeInOut(scheme, inventoryItem);
|
| 594 |
}
|
585 |
}
|
| 595 |
if (itemCashback > 0) {
|
586 |
if (itemCashback > 0) {
|
| 596 |
count++;
|
587 |
count++;
|
| 597 |
totalCashback += itemCashback;
|
588 |
totalCashback += itemCashback;
|
| Line 613... |
Line 604... |
| 613 |
float amountToRollback = 0;
|
604 |
float amountToRollback = 0;
|
| 614 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
605 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
| 615 |
for (SchemeInOut schemeInOut : schemes) {
|
606 |
for (SchemeInOut schemeInOut : schemes) {
|
| 616 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
607 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
| 617 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
608 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 618 |
schemeInOutRepository.persist(schemeInOut);
|
609 |
if(schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
| 619 |
amountToRollback += schemeInOut.getAmount();
|
610 |
amountToRollback += schemeInOut.getAmount();
|
| - |
|
611 |
}
|
| - |
|
612 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
| - |
|
613 |
schemeInOut.setStatusDescription(rollbackReason);
|
| 620 |
}
|
614 |
}
|
| 621 |
}
|
615 |
}
|
| 622 |
if (amountToRollback > 0) {
|
616 |
if (amountToRollback > 0) {
|
| 623 |
int inventoryItemId = inventoryItemIds.get(0);
|
617 |
int inventoryItemId = inventoryItemIds.get(0);
|
| 624 |
InventoryItem ii = inventoryItemRepository.selectById(inventoryItemId);
|
618 |
InventoryItem ii = inventoryItemRepository.selectById(inventoryItemId);
|
| 625 |
Purchase p = purchaseRepository.selectById(ii.getPurchaseId());
|
- |
|
| 626 |
Integer fofoId = ii.getFofoId();
|
619 |
Integer fofoId = ii.getFofoId();
|
| - |
|
620 |
//Purchase p = purchaseRepository.selectById(ii.getPurchaseId());
|
| 627 |
// TODO//
|
621 |
// TODO//
|
| 628 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, ii.getPurchaseId(),
|
622 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, ii.getPurchaseId(),
|
| 629 |
WalletReferenceType.SCHEME_IN, rollbackReason, LocalDateTime.now());
|
623 |
WalletReferenceType.SCHEME_IN, rollbackReason, LocalDateTime.now());
|
| 630 |
}
|
624 |
}
|
| 631 |
}
|
625 |
}
|
| Line 730... |
Line 724... |
| 730 |
SchemeType schemeType) throws ProfitMandiBusinessException {
|
724 |
SchemeType schemeType) throws ProfitMandiBusinessException {
|
| 731 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
725 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
| 732 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
726 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 733 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
727 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 734 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
728 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
| 735 |
float amount = 0;
|
729 |
float amountToRollback = 0;
|
| 736 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size()).stream()
|
730 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size()).stream()
|
| 737 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
731 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 738 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
732 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
| 739 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
733 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
| 740 |
if (scheme.getType().equals(schemeType)) {
|
734 |
if (scheme.getType().equals(schemeType)) {
|
| 741 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
735 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
| 742 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
736 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 743 |
schemeInOutRepository.persist(schemeInOut);
|
737 |
if(schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
| 744 |
amount += schemeInOut.getAmount();
|
738 |
amountToRollback += schemeInOut.getAmount();
|
| - |
|
739 |
}
|
| - |
|
740 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
| - |
|
741 |
schemeInOut.setStatusDescription(reversalReason);
|
| 745 |
}
|
742 |
}
|
| 746 |
}
|
743 |
}
|
| 747 |
}
|
744 |
}
|
| 748 |
int fofoId = inventoryItems.get(0).getFofoId();
|
745 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 749 |
WalletReferenceType walletReferenceType = schemeType.equals(SchemeType.OUT) ? WalletReferenceType.SCHEME_OUT
|
746 |
WalletReferenceType walletReferenceType = schemeType.equals(SchemeType.OUT) ? WalletReferenceType.SCHEME_OUT
|
| 750 |
: WalletReferenceType.SCHEME_IN;
|
747 |
: schemeType.equals(SchemeType.IN) ? WalletReferenceType.SCHEME_IN : WalletReferenceType.ACTIVATION_SCHEME;
|
| 751 |
if (amount > 0) {
|
748 |
if (amountToRollback > 0) {
|
| 752 |
// TODO//
|
749 |
// TODO//
|
| 753 |
walletService.rollbackAmountFromWallet(fofoId, amount, reversalReference, walletReferenceType,
|
750 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, reversalReference, walletReferenceType,
|
| 754 |
reversalReason, LocalDateTime.now());
|
751 |
reversalReason, LocalDateTime.now());
|
| 755 |
}
|
752 |
}
|
| 756 |
}
|
753 |
}
|
| 757 |
|
754 |
|
| 758 |
@Override
|
755 |
@Override
|