| Line 3... |
Line 3... |
| 3 |
import com.google.common.collect.Lists;
|
3 |
import com.google.common.collect.Lists;
|
| 4 |
import com.google.gson.Gson;
|
4 |
import com.google.gson.Gson;
|
| 5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 6 |
import com.spice.profitmandi.common.model.PartnerTargetModel;
|
6 |
import com.spice.profitmandi.common.model.PartnerTargetModel;
|
| 7 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
7 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| - |
|
8 |
import com.spice.profitmandi.common.util.Utils;
|
| 8 |
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
|
9 |
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
|
| 9 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
10 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
| 10 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
11 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 11 |
import com.spice.profitmandi.dao.entity.catalog.TargetSlabEntity;
|
12 |
import com.spice.profitmandi.dao.entity.catalog.TargetSlabEntity;
|
| - |
|
13 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
| - |
|
14 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 12 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
15 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 13 |
import com.spice.profitmandi.dao.entity.fofo.OfferPayout;
|
16 |
import com.spice.profitmandi.dao.entity.fofo.OfferPayout;
|
| - |
|
17 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| - |
|
18 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 14 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
19 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| - |
|
20 |
import com.spice.profitmandi.dao.entity.warehouse.WarehouseInventoryItem;
|
| - |
|
21 |
import com.spice.profitmandi.dao.entity.warehouse.WarehouseScan;
|
| - |
|
22 |
import com.spice.profitmandi.dao.enumuration.catalog.AchievementType;
|
| 15 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
23 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 16 |
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
|
24 |
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
|
| 17 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
25 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 18 |
import com.spice.profitmandi.dao.model.AmountModel;
|
26 |
import com.spice.profitmandi.dao.model.AmountModel;
|
| 19 |
import com.spice.profitmandi.dao.model.CreateOfferRequest;
|
27 |
import com.spice.profitmandi.dao.model.CreateOfferRequest;
|
| 20 |
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
|
28 |
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
|
| - |
|
29 |
import com.spice.profitmandi.dao.model.OfferRowModel;
|
| 21 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
30 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
| - |
|
31 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
| - |
|
32 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| - |
|
33 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 22 |
import com.spice.profitmandi.dao.repository.fofo.OfferPayoutRepository;
|
34 |
import com.spice.profitmandi.dao.repository.fofo.OfferPayoutRepository;
|
| - |
|
35 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| - |
|
36 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
|
| 23 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
37 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseScanRepository;
|
| - |
|
38 |
import com.spice.profitmandi.service.pricecircular.PriceCircularService;
|
| - |
|
39 |
import com.spice.profitmandi.service.scheme.InventoryPayoutModel;
|
| 24 |
import com.spice.profitmandi.service.user.RetailerService;
|
40 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 25 |
import com.spice.profitmandi.service.wallet.WalletService;
|
41 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 26 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
42 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 27 |
import org.apache.logging.log4j.LogManager;
|
43 |
import org.apache.logging.log4j.LogManager;
|
| 28 |
import org.apache.logging.log4j.Logger;
|
44 |
import org.apache.logging.log4j.Logger;
|
| Line 58... |
Line 74... |
| 58 |
@Autowired
|
74 |
@Autowired
|
| 59 |
private OfferRepository offerRepository;
|
75 |
private OfferRepository offerRepository;
|
| 60 |
|
76 |
|
| 61 |
@Autowired
|
77 |
@Autowired
|
| 62 |
private RetailerService retailerService;
|
78 |
private RetailerService retailerService;
|
| - |
|
79 |
@Autowired
|
| - |
|
80 |
PriceCircularService priceCircularService;
|
| 63 |
|
81 |
|
| 64 |
@Autowired
|
82 |
@Autowired
|
| 65 |
OfferPayoutRepository offerPayoutRepository;
|
83 |
OfferPayoutRepository offerPayoutRepository;
|
| 66 |
|
84 |
|
| 67 |
@Autowired
|
85 |
@Autowired
|
| Line 79... |
Line 97... |
| 79 |
@Autowired
|
97 |
@Autowired
|
| 80 |
CacheManager thirtyMinsTimeOutCacheManager;
|
98 |
CacheManager thirtyMinsTimeOutCacheManager;
|
| 81 |
|
99 |
|
| 82 |
|
100 |
|
| 83 |
private static final Logger LOGGER = LogManager.getLogger(OfferServiceImpl.class);
|
101 |
private static final Logger LOGGER = LogManager.getLogger(OfferServiceImpl.class);
|
| - |
|
102 |
|
| - |
|
103 |
@Autowired
|
| - |
|
104 |
private ActivatedImeiRepository activatedImeiRepository;
|
| - |
|
105 |
@Autowired
|
| 84 |
private PurchaseRepository purchaseRepository;
|
106 |
private WarehouseScanRepository warehouseScanRepository;
|
| - |
|
107 |
|
| - |
|
108 |
@Autowired
|
| - |
|
109 |
private WarehouseInventoryItemRepository warehouseInventoryItemRepository;
|
| - |
|
110 |
|
| - |
|
111 |
@Autowired
|
| - |
|
112 |
private LineItemImeisRepository lineItemImeisRepository;
|
| - |
|
113 |
@Autowired
|
| - |
|
114 |
private InventoryItemRepository inventoryItemRepository;
|
| 85 |
|
115 |
|
| 86 |
@Override
|
116 |
@Override
|
| 87 |
public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
117 |
public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
| 88 |
LOGGER.info("createOfferRequest -- {}", createOfferRequest);
|
118 |
LOGGER.info("createOfferRequest -- {}", createOfferRequest);
|
| 89 |
this.createOffer(createOfferRequest);
|
119 |
this.createOffer(createOfferRequest);
|
| Line 574... |
Line 604... |
| 574 |
}
|
604 |
}
|
| 575 |
}
|
605 |
}
|
| 576 |
return catalogSlabPayoutMap;
|
606 |
return catalogSlabPayoutMap;
|
| 577 |
}
|
607 |
}
|
| 578 |
|
608 |
|
| - |
|
609 |
|
| - |
|
610 |
private double getAmount(InventoryPayoutModel inventoryPayoutModel, AmountModel amountModel) {
|
| - |
|
611 |
double rollout = 0;
|
| - |
|
612 |
if (amountModel.getAmountType().equals(AmountType.PERCENTAGE)) {
|
| - |
|
613 |
double effectiveDp = inventoryPayoutModel.getDp() - inventoryPayoutModel.getFixedAmount();
|
| - |
|
614 |
double totalPercentage = inventoryPayoutModel.getPercentageAmount() + amountModel.getAmount();
|
| - |
|
615 |
rollout = effectiveDp * (totalPercentage / (100 + totalPercentage) - (inventoryPayoutModel.getPercentageAmount() / (100 + inventoryPayoutModel.getPercentageAmount())));
|
| - |
|
616 |
} else {
|
| - |
|
617 |
rollout = amountModel.getAmount() * (100 / (100 + inventoryPayoutModel.getPercentageAmount()));
|
| - |
|
618 |
}
|
| - |
|
619 |
return rollout;
|
| - |
|
620 |
}
|
| - |
|
621 |
|
| - |
|
622 |
@Override
|
| - |
|
623 |
public void processActivationtOffer(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
| - |
|
624 |
//List<OfferRowModel> offerRowModels = new ArrayList<>();
|
| - |
|
625 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
| - |
|
626 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
| - |
|
627 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream().collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
| - |
|
628 |
|
| - |
|
629 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream().filter(x -> itemCriteriaPayoutMap.get(x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
| - |
|
630 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(), Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
| - |
|
631 |
|
| - |
|
632 |
|
| - |
|
633 |
Map<String, Double> serialNumberPaid = offerPayouts.stream().filter(x -> x.getRejectTimestamp() == null).collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
| - |
|
634 |
/*if (offerPayouts.size() > 0) {
|
| - |
|
635 |
throw new ProfitMandiBusinessException("Offer Already Processed", "Offer Already Processed", "Offer Already Processed");
|
| - |
|
636 |
}*/
|
| - |
|
637 |
Map<Integer, Map<Integer, List<FofoOrderItem>>> criteriaPartnerwiseTertiary = offerRepository.getCriteriaWisePartnerTertiary(createOfferRequest);
|
| - |
|
638 |
|
| - |
|
639 |
|
| - |
|
640 |
final Set<String> activatedImeis;
|
| - |
|
641 |
final Set<String> missingImeis;
|
| - |
|
642 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
| - |
|
643 |
//All Serialnumbers for all eligible retailers
|
| - |
|
644 |
List<String> serialNumbers = criteriaPartnerwiseTertiary.entrySet().stream().flatMap(x -> x.getValue().entrySet().stream().flatMap(foiList -> foiList.getValue().stream()))
|
| - |
|
645 |
.flatMap(y -> y.getFofoLineItems().stream()).filter(fli -> fli.getSerialNumber() != null)
|
| - |
|
646 |
.map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
| - |
|
647 |
if (serialNumbers.size() > 0) {
|
| - |
|
648 |
activatedImeis = activatedImeiRepository.selectBySerialNumbers(serialNumbers).stream()
|
| - |
|
649 |
.filter(x -> createOfferRequest.isWithinRange(x.getActivationTimestamp()))
|
| - |
|
650 |
.map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
| - |
|
651 |
//missingImeis = serialNumbers.stream().filter(x -> !activatedImeis.contains(x)).collect(Collectors.toSet());
|
| - |
|
652 |
//activatedImeis.addAll(missingImeis);
|
| - |
|
653 |
//serialNumbers.removeAll(activatedImeis);
|
| - |
|
654 |
} else {
|
| - |
|
655 |
activatedImeis = null;
|
| - |
|
656 |
}
|
| - |
|
657 |
// TODO Amit
|
| - |
|
658 |
/**
|
| - |
|
659 |
* Missing imeis are considered as activated as long as payout calculations are
|
| - |
|
660 |
* concerned - Add flag if calculations are meant for final payout(Wherein
|
| - |
|
661 |
* missing imeis are no longer considered to be activated)
|
| - |
|
662 |
**/
|
| - |
|
663 |
} else {
|
| - |
|
664 |
activatedImeis = null;
|
| - |
|
665 |
}
|
| - |
|
666 |
|
| - |
|
667 |
Map<Integer, Integer> userBaseQtyMap = new HashMap<>();
|
| - |
|
668 |
Map<Integer, Integer> userBaseValueMap = new HashMap<>();
|
| - |
|
669 |
Map<Integer, Integer> eligibleBaseQtyMap = new HashMap<>();
|
| - |
|
670 |
Map<Integer, Integer> eligibleBaseValueMap = new HashMap<>();
|
| - |
|
671 |
|
| - |
|
672 |
if (createOfferRequest.isBaseCriteria()) {
|
| - |
|
673 |
Map<Integer, List<FofoOrderItem>> foiMap = criteriaPartnerwiseTertiary.get(0);
|
| - |
|
674 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : foiMap.entrySet()) {
|
| - |
|
675 |
int fofoId = orderEntry.getKey();
|
| - |
|
676 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
| - |
|
677 |
int totalBaseQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
| - |
|
678 |
int totalBaseValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (Math.round(x.getDp())) * x.getQuantity()));
|
| - |
|
679 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
| - |
|
680 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
| - |
|
681 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
| - |
|
682 |
Map<Integer, Integer> inventoryItemValueMap = new HashMap<>();
|
| - |
|
683 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
| - |
|
684 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
| - |
|
685 |
lineItem -> {
|
| - |
|
686 |
if (activatedImeis == null || activatedImeis.contains(lineItem.getSerialNumber())) {
|
| - |
|
687 |
inventoryItemValueMap.put(lineItem.getInventoryItemId(), (int) fofoOrderItem.getDp());
|
| - |
|
688 |
}
|
| - |
|
689 |
}
|
| - |
|
690 |
);
|
| - |
|
691 |
}
|
| - |
|
692 |
eligibleBaseQtyMap.put(fofoId, inventoryItemValueMap.size());
|
| - |
|
693 |
eligibleBaseValueMap.put(fofoId, inventoryItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue())));
|
| - |
|
694 |
} else {
|
| - |
|
695 |
eligibleBaseQtyMap.put(fofoId, totalBaseQty);
|
| - |
|
696 |
eligibleBaseValueMap.put(fofoId, totalBaseValue);
|
| - |
|
697 |
|
| - |
|
698 |
}
|
| - |
|
699 |
|
| - |
|
700 |
}
|
| - |
|
701 |
}
|
| - |
|
702 |
//Map<Integer, Double> totalPurchaseValueMap = null;
|
| - |
|
703 |
if (createOfferRequest.getSellinPercentage() > 0) {
|
| - |
|
704 |
/*List<Order> purchaseOrders = orderRepository.selectOrdersBetweenDates(fofoIds, "billingTimestamp",
|
| - |
|
705 |
createOfferRequest.getStartDate(), createOfferRequest.getEndDate());
|
| - |
|
706 |
Map<Integer, Double> totalPurchaseValueMap = purchaseOrders.stream().collect(
|
| - |
|
707 |
Collectors.groupingBy(x -> x.getRetailerId(), Collectors.summingDouble(x -> x.getWalletAmount())));*/
|
| - |
|
708 |
}
|
| - |
|
709 |
|
| - |
|
710 |
|
| - |
|
711 |
//As of now eligible sale is considered to be without special criteria like brand/
|
| - |
|
712 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
| - |
|
713 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
| - |
|
714 |
Map<Integer, List<FofoOrderItem>> ordersMap = criteriaPartnerwiseTertiary.get(criteriaId);
|
| - |
|
715 |
double totalRolloutAmount = 0;
|
| - |
|
716 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : ordersMap.entrySet()) {
|
| - |
|
717 |
int fofoId = orderEntry.getKey();
|
| - |
|
718 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
| - |
|
719 |
OfferRowModel offerRowModel = new OfferRowModel();
|
| - |
|
720 |
int totalQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
| - |
|
721 |
int totalValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> Math.round(x.getDp()) * (x.getQuantity())));
|
| - |
|
722 |
int eligibleQty = totalQty;
|
| - |
|
723 |
int eligibleValue = totalValue;
|
| - |
|
724 |
Map<FofoLineItem, Integer> lineItemValueMap = new HashMap<>();
|
| - |
|
725 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
| - |
|
726 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
| - |
|
727 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
| - |
|
728 |
lineItem -> {
|
| - |
|
729 |
if (activatedImeis == null || activatedImeis.contains(lineItem.getSerialNumber())) {
|
| - |
|
730 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
| - |
|
731 |
}
|
| - |
|
732 |
}
|
| - |
|
733 |
);
|
| - |
|
734 |
}
|
| - |
|
735 |
eligibleQty = lineItemValueMap.size();
|
| - |
|
736 |
eligibleValue = lineItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue()));
|
| - |
|
737 |
offerRowModel.setEligibleImeis(lineItemValueMap.keySet().stream().map(x -> x.getSerialNumber()).collect(Collectors.toList()));
|
| - |
|
738 |
}
|
| - |
|
739 |
|
| - |
|
740 |
int totalBaseQty = totalQty;
|
| - |
|
741 |
int totalBaseValue = totalValue;
|
| - |
|
742 |
int eligibleBaseQty = eligibleQty;
|
| - |
|
743 |
int eligibleBaseValue = eligibleValue;
|
| - |
|
744 |
if (createOfferRequest.isBaseCriteria()) {
|
| - |
|
745 |
totalBaseQty = userBaseQtyMap.get(fofoId) == null ? 0 : userBaseQtyMap.get(fofoId);
|
| - |
|
746 |
totalBaseValue = userBaseValueMap.get(fofoId) == null ? 0 : userBaseValueMap.get(fofoId);
|
| - |
|
747 |
eligibleBaseQty = eligibleBaseQtyMap.get(fofoId) == null ? 0 : eligibleBaseQtyMap.get(fofoId);
|
| - |
|
748 |
eligibleBaseValue = eligibleBaseValueMap.get(fofoId) == null ? 0 : eligibleBaseValueMap.get(fofoId);
|
| - |
|
749 |
}
|
| - |
|
750 |
|
| - |
|
751 |
|
| - |
|
752 |
int totalSale = totalBaseValue;
|
| - |
|
753 |
int eligibleSale = eligibleBaseValue;
|
| - |
|
754 |
int criteriaSale = totalValue;
|
| - |
|
755 |
int eligibleCriteriaSale = eligibleValue;
|
| - |
|
756 |
int eligibleCriteriaSaleDp = eligibleValue;
|
| - |
|
757 |
|
| - |
|
758 |
if (createOfferRequest.getTargetType().equals(AchievementType.QUANTITY)) {
|
| - |
|
759 |
totalSale = totalBaseQty;
|
| - |
|
760 |
eligibleSale = eligibleBaseQty;
|
| - |
|
761 |
criteriaSale = totalQty;
|
| - |
|
762 |
eligibleCriteriaSale = eligibleQty;
|
| - |
|
763 |
}
|
| - |
|
764 |
|
| - |
|
765 |
offerRowModel.setTotalSale(totalSale);
|
| - |
|
766 |
offerRowModel.setEligibleSale(eligibleSale);
|
| - |
|
767 |
float eligiblePayoutValue = 0;
|
| - |
|
768 |
double finalPayout = 0;
|
| - |
|
769 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
| - |
|
770 |
if (payoutSlab.getOnwardsAmount() <= eligibleSale) {
|
| - |
|
771 |
offerRowModel.setPayoutTargetAchieved(payoutSlab.getOnwardsAmount());
|
| - |
|
772 |
eligiblePayoutValue = payoutSlab.getPayoutAmount();
|
| - |
|
773 |
offerRowModel.setPayoutValue(eligiblePayoutValue);
|
| - |
|
774 |
}
|
| - |
|
775 |
}
|
| - |
|
776 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
| - |
|
777 |
finalPayout = (eligiblePayoutValue * eligibleCriteriaSaleDp) / 100;
|
| - |
|
778 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| - |
|
779 |
finalPayout = eligiblePayoutValue * eligibleCriteriaSale;
|
| - |
|
780 |
} else {
|
| - |
|
781 |
finalPayout = eligiblePayoutValue;
|
| - |
|
782 |
Map<Integer, Double> fofoCriteriaMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
| - |
|
783 |
if (fofoCriteriaMap != null) {
|
| - |
|
784 |
if (fofoCriteriaMap.containsKey(fofoId)) {
|
| - |
|
785 |
double totalPayout = fofoCriteriaMap.get(fofoId);
|
| - |
|
786 |
if (totalPayout - finalPayout > Utils.DOUBLE_EPSILON)
|
| - |
|
787 |
finalPayout = 0;
|
| - |
|
788 |
}
|
| - |
|
789 |
}
|
| - |
|
790 |
}
|
| - |
|
791 |
|
| - |
|
792 |
if (finalPayout > 0) {
|
| - |
|
793 |
List<Integer> inventoryItemIds = lineItemValueMap.keySet().stream().map(x -> x.getInventoryItemId()).collect(Collectors.toList());
|
| - |
|
794 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItemRepository.selectAllByIds(inventoryItemIds).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| - |
|
795 |
int totalDpValue = lineItemValueMap.values().stream().collect(Collectors.summingInt(x -> x));
|
| - |
|
796 |
for (Map.Entry<FofoLineItem, Integer> lineItemValueEntry : lineItemValueMap.entrySet()) {
|
| - |
|
797 |
double amount = 0;
|
| - |
|
798 |
if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| - |
|
799 |
amount = eligiblePayoutValue * (lineItemValueEntry.getValue() / totalDpValue);
|
| - |
|
800 |
//Fixed slab should be treated as fixed after payout
|
| - |
|
801 |
} else {
|
| - |
|
802 |
amount = eligiblePayoutValue;
|
| - |
|
803 |
}
|
| - |
|
804 |
|
| - |
|
805 |
if (serialNumberPaid.containsKey(lineItemValueEntry.getKey().getSerialNumber())) {
|
| - |
|
806 |
amount = amount - serialNumberPaid.get(lineItemValueEntry.getKey().getSerialNumber());
|
| - |
|
807 |
}
|
| - |
|
808 |
//ignore reasonably small
|
| - |
|
809 |
if (amount < Utils.DOUBLE_EPSILON) continue;
|
| - |
|
810 |
AmountModel amountModel = new AmountModel();
|
| - |
|
811 |
amountModel.setAmountType(itemCriteriaPayout.getAmountType());
|
| - |
|
812 |
amountModel.setAmount(amount);
|
| - |
|
813 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItemsMap.get(lineItemValueEntry.getKey().getInventoryItemId()));
|
| - |
|
814 |
double rolloutAmount = inventoryPayoutModel.getRolloutAmount(amountModel);
|
| - |
|
815 |
totalRolloutAmount += rolloutAmount;
|
| - |
|
816 |
|
| - |
|
817 |
OfferPayout offerPayout = new OfferPayout(fofoId, createOfferRequest.getId(), criteriaId, amount, lineItemValueEntry.getKey().getSerialNumber(), rolloutAmount, SchemePayoutStatus.CREDITED, createOfferRequest.getDescription(), LocalDateTime.now());
|
| - |
|
818 |
//offerPayout.setInventoryItemId(inventoryItemSaleModelsMap.get(lineItemValueEntry.getKey().getInventoryItemId()));
|
| - |
|
819 |
offerPayoutRepository.persist(offerPayout);
|
| - |
|
820 |
}
|
| - |
|
821 |
}
|
| - |
|
822 |
walletService.addAmountToWallet((int) fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) totalRolloutAmount, LocalDateTime.now());
|
| - |
|
823 |
}
|
| - |
|
824 |
}
|
| - |
|
825 |
Offer offer = offerRepository.selectById(createOfferRequest.getId());
|
| - |
|
826 |
offer.setProcessedTimestamp(LocalDateTime.now());
|
| - |
|
827 |
}
|
| - |
|
828 |
|
| - |
|
829 |
@Override
|
| - |
|
830 |
public void processSellin(CreateOfferRequest createOfferRequest) throws Exception {
|
| - |
|
831 |
|
| - |
|
832 |
//List<OfferRowModel> offerRowModels = new ArrayList<>();
|
| - |
|
833 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
| - |
|
834 |
Set<Long> fofoIds = offerPayouts.stream().map(x -> x.getFofoId()).collect(Collectors.toSet());
|
| - |
|
835 |
|
| - |
|
836 |
Map<Integer, Map<Integer, List<Order>>> criteriaOrdersMap = offerRepository.getPartnerWiseSellin(createOfferRequest, true);
|
| - |
|
837 |
|
| - |
|
838 |
Map<Integer, List<Order>> userBaseCriteriaOrdersMap = null;
|
| - |
|
839 |
if (criteriaOrdersMap.get(0) != null && criteriaOrdersMap.get(0).size() > 0) {
|
| - |
|
840 |
userBaseCriteriaOrdersMap = criteriaOrdersMap.get(0).entrySet().stream()
|
| - |
|
841 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
| - |
|
842 |
}
|
| - |
|
843 |
|
| - |
|
844 |
Map<Integer, Integer> userBaseQtyMap = null;
|
| - |
|
845 |
Map<Integer, Integer> userBaseValueMap = null;
|
| - |
|
846 |
if (userBaseCriteriaOrdersMap != null) {
|
| - |
|
847 |
userBaseQtyMap = new HashMap<>();
|
| - |
|
848 |
userBaseValueMap = new HashMap<>();
|
| - |
|
849 |
for (Map.Entry<Integer, List<Order>> orderEntry : userBaseCriteriaOrdersMap.entrySet()) {
|
| - |
|
850 |
int fofoId = orderEntry.getKey();
|
| - |
|
851 |
List<Order> orders = orderEntry.getValue();
|
| - |
|
852 |
int totalBaseQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
| - |
|
853 |
int totalBaseValue = orders.stream().collect(Collectors.summingInt(x ->
|
| - |
|
854 |
Math.round(x.getLineItem().getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))
|
| - |
|
855 |
));
|
| - |
|
856 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
| - |
|
857 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
| - |
|
858 |
}
|
| - |
|
859 |
}
|
| - |
|
860 |
LOGGER.info("Base Qty Map - {}", userBaseQtyMap);
|
| - |
|
861 |
LOGGER.info("Base Value Map - {}", userBaseValueMap);
|
| - |
|
862 |
|
| - |
|
863 |
LOGGER.info("Processing sellin");
|
| - |
|
864 |
List<OfferPayout> offerPayoutsNew = new ArrayList<>();
|
| - |
|
865 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
| - |
|
866 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
| - |
|
867 |
LOGGER.info("Criteria Id - {}", criteriaId);
|
| - |
|
868 |
Map<Integer, List<Order>> ordersMap = criteriaOrdersMap.get(criteriaId).entrySet().stream()
|
| - |
|
869 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
| - |
|
870 |
for (Map.Entry<Integer, List<Order>> orderEntry : ordersMap.entrySet()) {
|
| - |
|
871 |
int fofoId = orderEntry.getKey();
|
| - |
|
872 |
//CustomRetailer retailer = retailerService.getAllFofoRetailers().get(fofoId);
|
| - |
|
873 |
List<Order> orders = orderEntry.getValue();
|
| - |
|
874 |
Set<String> returnedSerialNumbers = orders.stream().filter(x -> x.getSerialNumber() != null).flatMap(x -> x.getReturnedImeis().stream()).collect(Collectors.toSet());
|
| - |
|
875 |
int totalQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
| - |
|
876 |
LOGGER.info("LineItems = {}", orders.stream().map(x -> x.getLineItem().getId()).collect(Collectors.toList()));
|
| - |
|
877 |
int totalValue = orders.stream().collect(Collectors.summingInt(x -> Math.round(x.getLineItem().
|
| - |
|
878 |
getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))));
|
| - |
|
879 |
int totalBaseQty = totalQty;
|
| - |
|
880 |
int totalBaseValue = totalValue;
|
| - |
|
881 |
if (userBaseCriteriaOrdersMap != null) {
|
| - |
|
882 |
if (userBaseQtyMap.containsKey(fofoId)) {
|
| - |
|
883 |
totalBaseQty = userBaseQtyMap.get(fofoId);
|
| - |
|
884 |
totalBaseValue = userBaseValueMap.get(fofoId);
|
| - |
|
885 |
} else {
|
| - |
|
886 |
continue;
|
| - |
|
887 |
}
|
| - |
|
888 |
}
|
| - |
|
889 |
|
| - |
|
890 |
int purchasedValue;
|
| - |
|
891 |
if (createOfferRequest.getTargetType().equals(AchievementType.VALUE)) {
|
| - |
|
892 |
purchasedValue = totalBaseValue;
|
| - |
|
893 |
} else {
|
| - |
|
894 |
purchasedValue = totalBaseQty;
|
| - |
|
895 |
}
|
| - |
|
896 |
PayoutSlab currentSlab = itemCriteriaPayout.getPayoutSlabs().stream()
|
| - |
|
897 |
.filter(x -> x.getOnwardsAmount() <= purchasedValue).max(Comparator.comparing(x
|
| - |
|
898 |
-> x.getOnwardsAmount())).orElse(null);
|
| - |
|
899 |
int targetAchieved = currentSlab ==
|
| - |
|
900 |
null ? 0 : currentSlab.getOnwardsAmount();
|
| - |
|
901 |
|
| - |
|
902 |
//TODO-- payouts handling is here
|
| - |
|
903 |
if (currentSlab == null) continue;
|
| - |
|
904 |
//Need to remove hardcoding on tax
|
| - |
|
905 |
List<Integer> orderIds = orders.stream().map(x -> x.getId()).collect(Collectors.toList());
|
| - |
|
906 |
Map<Integer, LineItem> lineItemMap = orders.stream().collect(Collectors.toMap(x -> x.getLineItem().getId(), x -> x.getLineItem()));
|
| - |
|
907 |
List<WarehouseScan> warehouseScans = warehouseScanRepository.selectAllByOrderIds(orderIds);
|
| - |
|
908 |
List<Integer> inventoryItemIds = warehouseScans.stream().collect(Collectors.groupingBy(x -> x.getInventoryItemId())).entrySet()
|
| - |
|
909 |
.stream().filter(x -> x.getValue().size() == 1).map(x -> x.getKey()).collect(Collectors.toList());
|
| - |
|
910 |
Set<String> serialNumbers;
|
| - |
|
911 |
LOGGER.info("inventoryItemIds {}", inventoryItemIds);
|
| - |
|
912 |
if (inventoryItemIds.size() > 0) {
|
| - |
|
913 |
List<WarehouseInventoryItem> warehouseInventoryItems = warehouseInventoryItemRepository.selectAllByIds(inventoryItemIds);
|
| - |
|
914 |
serialNumbers = warehouseInventoryItems.stream().map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
| - |
|
915 |
} else {
|
| - |
|
916 |
serialNumbers = new HashSet<>();
|
| - |
|
917 |
}
|
| - |
|
918 |
LOGGER.info("SerialNumbers {}", serialNumbers);
|
| - |
|
919 |
Map<String, Long> serialNumberCountMap = lineItemImeisRepository.selectByLineItemIds(new ArrayList<>(lineItemMap.keySet()))
|
| - |
|
920 |
.stream().collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.counting()));
|
| - |
|
921 |
LOGGER.info("SerialNumbers Count Map {}", serialNumberCountMap);
|
| - |
|
922 |
Map<String, LineItem> serialNumberMap = lineItemImeisRepository.selectByLineItemIds(new ArrayList<>(lineItemMap.keySet()))
|
| - |
|
923 |
.stream().collect(Collectors.toMap(x -> x.getSerialNumber(), x -> lineItemMap.get(x.getLineItemId()), (x1, x2) -> x1.getId() > x2.getId() ? x1 : x2));
|
| - |
|
924 |
|
| - |
|
925 |
serialNumberMap = serialNumberMap.entrySet().stream().filter(x -> serialNumbers.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
| - |
|
926 |
|
| - |
|
927 |
LOGGER.info("serialNumberMap {}", serialNumberMap);
|
| - |
|
928 |
double amount = 0;
|
| - |
|
929 |
int lineItemsValue = serialNumberMap.values().stream().collect(Collectors.summingInt(x -> x.getUnitPrice().intValue()));
|
| - |
|
930 |
LOGGER.info("lineItemsValue - {}, totalValue - {}", lineItemsValue, totalValue);
|
| - |
|
931 |
for (Map.Entry<String, LineItem> entry : serialNumberMap.entrySet()) {
|
| - |
|
932 |
LOGGER.info("Serial no - {}", entry.getKey());
|
| - |
|
933 |
LineItem lineItem = entry.getValue();
|
| - |
|
934 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
| - |
|
935 |
float percentageAmount = currentSlab.getPayoutAmount();
|
| - |
|
936 |
amount = entry.getValue().getUnitPrice() * (percentageAmount / 100);
|
| - |
|
937 |
} else if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| - |
|
938 |
float totalAmount = currentSlab.getPayoutAmount();
|
| - |
|
939 |
amount = Math.round((totalAmount / totalValue) * lineItem.getUnitPrice());
|
| - |
|
940 |
LOGGER.info("Serial no - {}, unitPrice - {}, amount - {}", entry.getKey(), lineItem.getUnitPrice(), amount);
|
| - |
|
941 |
} else {
|
| - |
|
942 |
//per unit amount
|
| - |
|
943 |
amount = currentSlab.getPayoutAmount();
|
| - |
|
944 |
}
|
| - |
|
945 |
if (fofoIds.contains(Long.valueOf(fofoId))) continue;
|
| - |
|
946 |
OfferPayout offerPayout = new OfferPayout(fofoId, createOfferRequest.getId(), criteriaId, currentSlab.getPayoutAmount(), entry.getKey(), amount, SchemePayoutStatus.CREDITED, createOfferRequest.getDescription(), LocalDateTime.now());
|
| - |
|
947 |
//offerPayout.setLineItemId();
|
| - |
|
948 |
offerPayoutRepository.persist(offerPayout);
|
| - |
|
949 |
offerPayoutsNew.add(offerPayout);
|
| - |
|
950 |
}
|
| - |
|
951 |
|
| - |
|
952 |
}
|
| - |
|
953 |
}
|
| - |
|
954 |
Map<Long, Double> offerPayoutMap = offerPayoutsNew.stream().collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.summingDouble(x -> x.getAmount())));
|
| - |
|
955 |
for (Map.Entry<Long, Double> offerPayoutEntry : offerPayoutMap.entrySet()) {
|
| - |
|
956 |
long fofoId = offerPayoutEntry.getKey();
|
| - |
|
957 |
double amountToCredit = offerPayoutEntry.getValue();
|
| - |
|
958 |
walletService.addAmountToWallet((int) fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) amountToCredit, LocalDateTime.now());
|
| - |
|
959 |
}
|
| - |
|
960 |
Offer offer = offerRepository.selectById(createOfferRequest.getId());
|
| - |
|
961 |
offer.setProcessedTimestamp(LocalDateTime.now());
|
| - |
|
962 |
}
|
| 579 |
}
|
963 |
}
|