| 27877 |
amit.gupta |
1 |
package com.spice.profitmandi.service.offers;
|
|
|
2 |
|
| 29783 |
amit.gupta |
3 |
import com.google.common.collect.Lists;
|
| 27877 |
amit.gupta |
4 |
import com.google.gson.Gson;
|
|
|
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 29683 |
amit.gupta |
6 |
import com.spice.profitmandi.common.model.PartnerTargetModel;
|
|
|
7 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 31920 |
amit.gupta |
8 |
import com.spice.profitmandi.common.util.Utils;
|
| 27877 |
amit.gupta |
9 |
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
| 29783 |
amit.gupta |
11 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 31903 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.entity.catalog.TargetSlabEntity;
|
| 31920 |
amit.gupta |
13 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
|
|
14 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 31205 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
|
|
16 |
import com.spice.profitmandi.dao.entity.fofo.OfferPayout;
|
| 31920 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
|
|
18 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 31747 |
amit.gupta |
19 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| 31920 |
amit.gupta |
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;
|
| 29783 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 29499 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
|
| 31205 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 31903 |
amit.gupta |
26 |
import com.spice.profitmandi.dao.model.AmountModel;
|
| 27877 |
amit.gupta |
27 |
import com.spice.profitmandi.dao.model.CreateOfferRequest;
|
|
|
28 |
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
|
| 31920 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.model.OfferRowModel;
|
| 30066 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
| 31920 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 31205 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.repository.fofo.OfferPayoutRepository;
|
| 31920 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
|
|
35 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
|
|
|
36 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseScanRepository;
|
|
|
37 |
import com.spice.profitmandi.service.pricecircular.PriceCircularService;
|
|
|
38 |
import com.spice.profitmandi.service.scheme.InventoryPayoutModel;
|
| 27877 |
amit.gupta |
39 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 31205 |
amit.gupta |
40 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
41 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 30066 |
amit.gupta |
42 |
import org.apache.logging.log4j.LogManager;
|
|
|
43 |
import org.apache.logging.log4j.Logger;
|
|
|
44 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
45 |
import org.apache.poi.ss.usermodel.CellType;
|
|
|
46 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
47 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
|
|
48 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
49 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
50 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
51 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
52 |
import org.springframework.cache.CacheManager;
|
|
|
53 |
import org.springframework.cache.annotation.Cacheable;
|
|
|
54 |
import org.springframework.stereotype.Component;
|
| 27877 |
amit.gupta |
55 |
|
| 30066 |
amit.gupta |
56 |
import java.io.IOException;
|
|
|
57 |
import java.io.InputStream;
|
|
|
58 |
import java.time.LocalDate;
|
| 31205 |
amit.gupta |
59 |
import java.time.LocalDateTime;
|
| 32868 |
amit.gupta |
60 |
import java.time.Year;
|
| 30066 |
amit.gupta |
61 |
import java.time.YearMonth;
|
|
|
62 |
import java.util.*;
|
|
|
63 |
import java.util.stream.Collectors;
|
|
|
64 |
|
| 27877 |
amit.gupta |
65 |
@Component
|
|
|
66 |
public class OfferServiceImpl implements OfferService {
|
|
|
67 |
|
| 31747 |
amit.gupta |
68 |
private static final List<String> targets = Arrays.asList("Target 1", "Target 2", "Target 3", "Target 4",
|
|
|
69 |
"Target 5");
|
| 29499 |
amit.gupta |
70 |
|
| 31747 |
amit.gupta |
71 |
@Autowired
|
|
|
72 |
private Gson gson;
|
| 27877 |
amit.gupta |
73 |
|
| 31747 |
amit.gupta |
74 |
@Autowired
|
|
|
75 |
private OfferRepository offerRepository;
|
| 27877 |
amit.gupta |
76 |
|
| 31747 |
amit.gupta |
77 |
@Autowired
|
|
|
78 |
private RetailerService retailerService;
|
| 31920 |
amit.gupta |
79 |
@Autowired
|
|
|
80 |
PriceCircularService priceCircularService;
|
| 27877 |
amit.gupta |
81 |
|
| 31747 |
amit.gupta |
82 |
@Autowired
|
|
|
83 |
OfferPayoutRepository offerPayoutRepository;
|
| 31205 |
amit.gupta |
84 |
|
| 31747 |
amit.gupta |
85 |
@Autowired
|
|
|
86 |
private ItemCriteriaRepository itemCriteriaRepository;
|
| 27877 |
amit.gupta |
87 |
|
| 31747 |
amit.gupta |
88 |
@Autowired
|
|
|
89 |
private ItemRepository itemRepository;
|
| 27877 |
amit.gupta |
90 |
|
| 31747 |
amit.gupta |
91 |
@Autowired
|
|
|
92 |
private TagListingRepository tagListingRepository;
|
| 29783 |
amit.gupta |
93 |
|
| 31747 |
amit.gupta |
94 |
@Autowired
|
|
|
95 |
private OfferTargetSlabRepository offerTargetSlabRepository;
|
| 30684 |
amit.gupta |
96 |
|
| 31747 |
amit.gupta |
97 |
@Autowired
|
| 32868 |
amit.gupta |
98 |
CacheManager oneDayCacheManager;
|
| 27877 |
amit.gupta |
99 |
|
| 29806 |
amit.gupta |
100 |
|
| 31747 |
amit.gupta |
101 |
private static final Logger LOGGER = LogManager.getLogger(OfferServiceImpl.class);
|
| 27877 |
amit.gupta |
102 |
|
| 31920 |
amit.gupta |
103 |
@Autowired
|
|
|
104 |
private ActivatedImeiRepository activatedImeiRepository;
|
|
|
105 |
@Autowired
|
|
|
106 |
private WarehouseScanRepository warehouseScanRepository;
|
|
|
107 |
|
|
|
108 |
@Autowired
|
|
|
109 |
private WarehouseInventoryItemRepository warehouseInventoryItemRepository;
|
|
|
110 |
|
|
|
111 |
@Autowired
|
|
|
112 |
private LineItemImeisRepository lineItemImeisRepository;
|
|
|
113 |
@Autowired
|
|
|
114 |
private InventoryItemRepository inventoryItemRepository;
|
|
|
115 |
|
| 31747 |
amit.gupta |
116 |
@Override
|
|
|
117 |
public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
118 |
LOGGER.info("createOfferRequest -- {}", createOfferRequest);
|
|
|
119 |
this.createOffer(createOfferRequest);
|
|
|
120 |
this.createTargetSlabs(createOfferRequest);
|
|
|
121 |
}
|
| 27877 |
amit.gupta |
122 |
|
| 31747 |
amit.gupta |
123 |
private void createOffer(CreateOfferRequest createOfferRequest) {
|
|
|
124 |
Offer offer = new Offer();
|
|
|
125 |
offer.setName(createOfferRequest.getName());
|
|
|
126 |
offer.setTargetType(createOfferRequest.getTargetType());
|
|
|
127 |
offer.setBooster(createOfferRequest.isBooster());
|
|
|
128 |
offer.setPayoutType(createOfferRequest.getPayoutType());
|
|
|
129 |
offer.setSchemeType(createOfferRequest.getSchemeType());
|
|
|
130 |
offer.setBaseCriteia(createOfferRequest.isBaseCriteria());
|
|
|
131 |
offer.setEndDate(createOfferRequest.getEndDate());
|
|
|
132 |
offer.setStartDate(createOfferRequest.getStartDate());
|
|
|
133 |
offer.setBrandSharePercentage(createOfferRequest.getBrandShareTerms());
|
|
|
134 |
offer.setSellinPercentage(createOfferRequest.getSellinPercentage());
|
|
|
135 |
offer.setOfferNotes(createOfferRequest.getOfferNotes());
|
|
|
136 |
offer.setActivationBrands(createOfferRequest.getActivationBrands());
|
|
|
137 |
offer.setTerms(createOfferRequest.getTerms());
|
|
|
138 |
offer.setItemCriteria(gson.toJson(createOfferRequest.getItemCriteria()));
|
|
|
139 |
offer.setPartnerCriteria(gson.toJson(createOfferRequest.getPartnerCriteria()));
|
| 32070 |
amit.gupta |
140 |
offer.setDiscount(createOfferRequest.isDiscount());
|
| 31747 |
amit.gupta |
141 |
offerRepository.persist(offer);
|
|
|
142 |
createOfferRequest.setId(offer.getId());
|
|
|
143 |
}
|
| 27877 |
amit.gupta |
144 |
|
| 31747 |
amit.gupta |
145 |
@Override
|
|
|
146 |
public List<CreateOfferRequest> getPublishedOffers(int fofoId, YearMonth yearMonth) {
|
|
|
147 |
List<Offer> publishedOffers = offerRepository.selectAllPublishedMapByPartner(yearMonth).get(fofoId);
|
|
|
148 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
149 |
if (publishedOffers != null) {
|
|
|
150 |
for (Offer offer : publishedOffers) {
|
|
|
151 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
152 |
createOfferRequests.add(createOfferRequest);
|
|
|
153 |
setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
| 27877 |
amit.gupta |
154 |
|
| 31747 |
amit.gupta |
155 |
}
|
|
|
156 |
}
|
|
|
157 |
return createOfferRequests;
|
|
|
158 |
}
|
| 27877 |
amit.gupta |
159 |
|
| 31747 |
amit.gupta |
160 |
private void setCriteriaPayoutAchieved(int fofoId, CreateOfferRequest createOfferRequest) {
|
|
|
161 |
Map<Integer, Long> criteriaTransactionMap = null;
|
|
|
162 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
|
|
|
163 |
criteriaTransactionMap = offerRepository.getPartnerWisePurchaseSum(createOfferRequest).get(fofoId);
|
|
|
164 |
} else {
|
|
|
165 |
criteriaTransactionMap = offerRepository.getPartnerWiseSalesSum(createOfferRequest).get(fofoId);
|
|
|
166 |
}
|
|
|
167 |
LOGGER.info("I am here ------- {}", createOfferRequest.getId());
|
|
|
168 |
List<com.spice.profitmandi.dao.model.ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest
|
|
|
169 |
.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
| 27877 |
amit.gupta |
170 |
|
| 31747 |
amit.gupta |
171 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
172 |
itemCriteriaPayout.setNextSlab(null);
|
|
|
173 |
LOGGER.info("I am here --- {}", itemCriteriaPayout.getPayoutSlabs());
|
|
|
174 |
long saleAmount = 0;
|
|
|
175 |
if (criteriaTransactionMap != null
|
|
|
176 |
&& criteriaTransactionMap.containsKey(itemCriteriaPayout.getItemCriteria().getId())) {
|
| 31853 |
amit.gupta |
177 |
saleAmount = criteriaTransactionMap.containsKey(0) ? criteriaTransactionMap.get(0) : criteriaTransactionMap.get(itemCriteriaPayout.getItemCriteria().getId());
|
| 31747 |
amit.gupta |
178 |
}
|
|
|
179 |
itemCriteriaPayout.setAchievedValue(saleAmount);
|
|
|
180 |
List<com.spice.profitmandi.service.offers.PayoutSlab> payoutSlabs = itemCriteriaPayout.getPayoutSlabs();
|
|
|
181 |
for (int i = 0; i < payoutSlabs.size(); i++) {
|
|
|
182 |
com.spice.profitmandi.service.offers.PayoutSlab beginSlab = payoutSlabs.get(i);
|
|
|
183 |
com.spice.profitmandi.service.offers.PayoutSlab endSlab = null;
|
|
|
184 |
if (payoutSlabs.size() - 1 > i) {
|
|
|
185 |
endSlab = payoutSlabs.get(i + 1);
|
|
|
186 |
}
|
|
|
187 |
if (beginSlab.getOnwardsAmount() > saleAmount) {
|
|
|
188 |
itemCriteriaPayout.setShortValue(beginSlab.getOnwardsAmount() - saleAmount);
|
|
|
189 |
itemCriteriaPayout.setNextSlab(beginSlab);
|
|
|
190 |
LOGGER.info("I am here");
|
|
|
191 |
break;
|
|
|
192 |
}
|
|
|
193 |
if (endSlab != null) {
|
|
|
194 |
if (beginSlab.getOnwardsAmount() <= saleAmount && endSlab.getOnwardsAmount() > saleAmount) {
|
|
|
195 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
196 |
beginSlab.setSelected(true);
|
|
|
197 |
itemCriteriaPayout.setNextSlab(endSlab);
|
|
|
198 |
itemCriteriaPayout.setShortValue(endSlab.getOnwardsAmount() - saleAmount);
|
|
|
199 |
LOGGER.info("I am here");
|
|
|
200 |
break;
|
|
|
201 |
}
|
|
|
202 |
// Last loop
|
|
|
203 |
} else {
|
|
|
204 |
beginSlab.setSelected(true);
|
|
|
205 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
206 |
LOGGER.info("I am here");
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
LOGGER.info("I am here --- {}", itemCriteriaPayout);
|
|
|
210 |
}
|
| 29783 |
amit.gupta |
211 |
|
| 31747 |
amit.gupta |
212 |
}
|
| 27877 |
amit.gupta |
213 |
|
| 31747 |
amit.gupta |
214 |
@Override
|
| 32868 |
amit.gupta |
215 |
@Cacheable(value = "allOffers", cacheManager = "oneDayCacheManager")
|
| 31747 |
amit.gupta |
216 |
public Map<Integer, CreateOfferRequest> getAllOffers(YearMonth yearMonth) {
|
|
|
217 |
List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
|
|
|
218 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
219 |
for (Offer offer : allOffers) {
|
|
|
220 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
221 |
createOfferRequests.add(createOfferRequest);
|
|
|
222 |
}
|
|
|
223 |
return createOfferRequests.stream().collect(Collectors.toMap(CreateOfferRequest::getId, x -> x));
|
|
|
224 |
}
|
| 27877 |
amit.gupta |
225 |
|
| 31747 |
amit.gupta |
226 |
@Override
|
|
|
227 |
@Cacheable(value = "partnerOffers", cacheManager = "thirtyMinsTimeOutCacheManager")
|
|
|
228 |
public CreateOfferRequest getOffer(int fofoId, int offerId) {
|
|
|
229 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
230 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
231 |
if (fofoId > 0) {
|
|
|
232 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
|
|
233 |
}
|
| 29783 |
amit.gupta |
234 |
|
| 31747 |
amit.gupta |
235 |
return createOfferRequest;
|
|
|
236 |
}
|
| 27877 |
amit.gupta |
237 |
|
| 31747 |
amit.gupta |
238 |
@Override
|
|
|
239 |
public CreateOfferRequest getCreateOfferRequest(Offer fromOffer) {
|
|
|
240 |
CreateOfferRequest createOfferRequest = new CreateOfferRequest();
|
|
|
241 |
createOfferRequest.setName(fromOffer.getName());
|
| 32023 |
amit.gupta |
242 |
createOfferRequest.setDiscount(fromOffer.isDiscount());
|
| 31747 |
amit.gupta |
243 |
createOfferRequest.setTargetType(fromOffer.getTargetType());
|
|
|
244 |
createOfferRequest.setPayoutType(fromOffer.getPayoutType());
|
|
|
245 |
createOfferRequest.setSchemeType(fromOffer.getSchemeType());
|
|
|
246 |
createOfferRequest.setActivationBrands(fromOffer.getActivationBrands());
|
|
|
247 |
createOfferRequest.setEndDate(fromOffer.getEndDate());
|
|
|
248 |
createOfferRequest.setProcessTimestamp(fromOffer.getProcessedTimestamp());
|
|
|
249 |
createOfferRequest.setStartDate(fromOffer.getStartDate());
|
|
|
250 |
createOfferRequest.setBooster(fromOffer.isBooster());
|
|
|
251 |
createOfferRequest.setActive(fromOffer.isActive());
|
|
|
252 |
createOfferRequest.setSellinPercentage(fromOffer.getSellinPercentage());
|
|
|
253 |
createOfferRequest.setBrandShareTerms(fromOffer.getBrandSharePercentage());
|
|
|
254 |
createOfferRequest.setOfferNotes(fromOffer.getOfferNotes());
|
|
|
255 |
createOfferRequest.setTerms(fromOffer.getTerms());
|
|
|
256 |
createOfferRequest.setBaseCriteria(fromOffer.isBaseCriteia());
|
|
|
257 |
createOfferRequest.setItemCriteria(
|
|
|
258 |
gson.fromJson(fromOffer.getItemCriteria(), com.spice.profitmandi.service.offers.ItemCriteria.class));
|
|
|
259 |
createOfferRequest.setPartnerCriteria(gson.fromJson(fromOffer.getPartnerCriteria(), PartnerCriteria.class));
|
|
|
260 |
createOfferRequest.setId(fromOffer.getId());
|
|
|
261 |
createOfferRequest.setCreatedOn(fromOffer.getCreatedTimestamp());
|
|
|
262 |
createOfferRequest.setItemCriteriaString(
|
|
|
263 |
itemCriteriaRepository.getItemCriteriaDescription(createOfferRequest.getItemCriteria()));
|
|
|
264 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
|
|
265 |
.getByOfferId(fromOffer.getId());
|
|
|
266 |
createOfferRequest.setPartnerCriteriaString(
|
|
|
267 |
retailerService.getPartnerCriteriaString(createOfferRequest.getPartnerCriteria()));
|
|
|
268 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
|
|
269 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs()
|
|
|
270 |
.get(0).getItemCriteriaPayouts()) {
|
|
|
271 |
itemCriteriaPayout.setNextSlab(itemCriteriaPayout.getPayoutSlabs().get(0));
|
|
|
272 |
}
|
|
|
273 |
return createOfferRequest;
|
| 27877 |
amit.gupta |
274 |
|
| 31747 |
amit.gupta |
275 |
}
|
| 27877 |
amit.gupta |
276 |
|
| 31747 |
amit.gupta |
277 |
@Autowired
|
|
|
278 |
private WalletService walletService;
|
| 31205 |
amit.gupta |
279 |
|
| 31747 |
amit.gupta |
280 |
@Override
|
|
|
281 |
public void reverseAdditionalSelloutSchemes(int fofoId, List<InventoryItem> inventoryItems) throws ProfitMandiBusinessException {
|
|
|
282 |
List<OfferPayout> offerPayouts = new ArrayList<>();
|
| 31748 |
amit.gupta |
283 |
LOGGER.info("Reversing schemes for fofoId {}, InventoryItems {}", fofoId, inventoryItems);
|
| 31747 |
amit.gupta |
284 |
//As of now we are not handling non-serialised items
|
|
|
285 |
inventoryItems = inventoryItems.stream().filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
| 31750 |
amit.gupta |
286 |
if (inventoryItems.size() > 0) {
|
| 31205 |
amit.gupta |
287 |
|
| 31750 |
amit.gupta |
288 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
289 |
offerPayouts.addAll(offerPayoutRepository.selectAllBySerialNumber(inventoryItem.getFofoId(), inventoryItem.getSerialNumber())
|
|
|
290 |
.stream()
|
|
|
291 |
.filter(x -> x.getRejectTimestamp() == null)
|
|
|
292 |
.collect(Collectors.toList()));
|
| 31747 |
amit.gupta |
293 |
}
|
| 31750 |
amit.gupta |
294 |
for (OfferPayout offerPayout : offerPayouts) {
|
|
|
295 |
offerPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
296 |
offerPayout.setRejectTimestamp(LocalDateTime.now());
|
|
|
297 |
}
|
|
|
298 |
Map<Long, Double> offerPayoutAmountMap = offerPayouts.stream().collect(Collectors.groupingBy(x -> x.getOfferId(), Collectors.summingDouble(x -> x.getAmount())));
|
|
|
299 |
for (Map.Entry<Long, Double> offerPayoutEntry : offerPayoutAmountMap.entrySet()) {
|
|
|
300 |
List<UserWalletHistory> histories = walletService.getAllByReference(fofoId, offerPayoutEntry.getKey().intValue(), WalletReferenceType.ADDITIONAL_SCHEME);
|
|
|
301 |
if (histories.size() > 0) {
|
|
|
302 |
|
|
|
303 |
walletService.rollbackAmountFromWallet(fofoId, offerPayoutEntry.getValue().floatValue(), offerPayoutEntry.getKey().intValue(),
|
|
|
304 |
WalletReferenceType.ADDITIONAL_SCHEME, "Models/Imeis for corresponding schemes were returned", LocalDateTime.now());
|
|
|
305 |
}
|
|
|
306 |
}
|
| 31747 |
amit.gupta |
307 |
}
|
|
|
308 |
}
|
| 30684 |
amit.gupta |
309 |
|
| 31747 |
amit.gupta |
310 |
private void createTargetSlabs(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
311 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = createOfferRequest.getTargetSlabs().stream()
|
|
|
312 |
.filter(x -> x.validate()).collect(Collectors.toList());
|
|
|
313 |
if (targetSlabs.size() >= 0) {
|
|
|
314 |
Collections.sort(targetSlabs);
|
|
|
315 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
316 |
if (targetSlab.validate()) {
|
|
|
317 |
List<ItemCriteriaPayout> itemCriteriaPayouts = targetSlab.getItemCriteriaPayouts();
|
|
|
318 |
for (ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
319 |
ItemCriteria itemCriteria = itemCriteriaRepository.selectById(itemCriteriaPayout.getItemCriteria().getId());
|
|
|
320 |
if (itemCriteriaPayout.getItemCriteria().getId() > 0) {
|
|
|
321 |
itemCriteriaPayout.getItemCriteria().setId(0);
|
|
|
322 |
} else {
|
| 27877 |
amit.gupta |
323 |
|
| 31747 |
amit.gupta |
324 |
String itemCriteriaJson = gson.toJson(itemCriteriaPayout.getItemCriteria());
|
|
|
325 |
itemCriteria = itemCriteriaRepository.selectByCriteria(itemCriteriaJson);
|
|
|
326 |
// ItemCriteria Only once
|
|
|
327 |
if (itemCriteria == null) {
|
|
|
328 |
itemCriteria = new ItemCriteria();
|
|
|
329 |
itemCriteria.setCriteria(gson.toJson(itemCriteriaPayout.getItemCriteria()));
|
|
|
330 |
itemCriteriaRepository.persist(itemCriteria);
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
| 31903 |
amit.gupta |
334 |
TargetSlabEntity targetSlabEntity = new TargetSlabEntity();
|
|
|
335 |
targetSlabEntity.setOfferId(createOfferRequest.getId());
|
|
|
336 |
targetSlabEntity.setOnwardsTarget(targetSlab.getOnwardsAmount());
|
|
|
337 |
targetSlabEntity.setTargetDescription(targetSlab.getTargetDescription());
|
|
|
338 |
targetSlabEntity.setPayoutTarget(payoutSlab.getOnwardsAmount());
|
|
|
339 |
targetSlabEntity.setPayoutValue(payoutSlab.getPayoutAmount());
|
|
|
340 |
targetSlabEntity.setItemCriteriaId(itemCriteria.getId());
|
|
|
341 |
targetSlabEntity.setAmountType(itemCriteriaPayout.getAmountType());
|
| 32979 |
amit.gupta |
342 |
targetSlabEntity.setStartDate(itemCriteriaPayout.getStartDate());
|
|
|
343 |
targetSlabEntity.setEndDate(itemCriteriaPayout.getEndDate());
|
| 31903 |
amit.gupta |
344 |
offerTargetSlabRepository.persist(targetSlabEntity);
|
| 31747 |
amit.gupta |
345 |
}
|
|
|
346 |
}
|
|
|
347 |
}
|
|
|
348 |
}
|
| 27877 |
amit.gupta |
349 |
|
| 31747 |
amit.gupta |
350 |
} else {
|
|
|
351 |
throw new ProfitMandiBusinessException("Invalid Target Slabs", "Invalid Target", "");
|
|
|
352 |
}
|
| 27877 |
amit.gupta |
353 |
|
| 31747 |
amit.gupta |
354 |
}
|
| 27877 |
amit.gupta |
355 |
|
| 31747 |
amit.gupta |
356 |
@Override
|
|
|
357 |
public List<CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, int catalogId) {
|
|
|
358 |
List<Offer> publishedOffers = offerRepository.selectAllPublishedMapByPartner(YearMonth.from(date)).get(fofoId);
|
|
|
359 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
360 |
if (publishedOffers != null) {
|
|
|
361 |
for (Offer offer : publishedOffers) {
|
|
|
362 |
boolean found = false;
|
|
|
363 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
|
|
364 |
.getByOfferId(offer.getId());
|
|
|
365 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
366 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
|
|
367 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
|
|
368 |
Set<com.spice.profitmandi.service.offers.ItemCriteria> itemCriteriaSet = targetSlabs.stream()
|
|
|
369 |
.flatMap(x -> x.getItemCriteriaPayouts().stream()).map(x -> x.getItemCriteria()).distinct()
|
|
|
370 |
.filter(x -> itemRepository.containsModel(x, catalogId)).collect(Collectors.toSet());
|
| 29499 |
amit.gupta |
371 |
|
| 31747 |
amit.gupta |
372 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
373 |
targetSlab.setItemCriteriaPayouts(targetSlab.getItemCriteriaPayouts().stream()
|
|
|
374 |
.filter(x -> itemCriteriaSet.contains(x.getItemCriteria())).collect(Collectors.toList()));
|
|
|
375 |
if (targetSlab.getItemCriteriaPayouts().size() > 0) {
|
|
|
376 |
found = true;
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
if (found) {
|
|
|
380 |
createOfferRequests.add(createOfferRequest);
|
|
|
381 |
}
|
|
|
382 |
}
|
|
|
383 |
}
|
| 29783 |
amit.gupta |
384 |
|
| 31747 |
amit.gupta |
385 |
return createOfferRequests;
|
|
|
386 |
}
|
| 29783 |
amit.gupta |
387 |
|
| 31747 |
amit.gupta |
388 |
@Override
|
|
|
389 |
public void createOffers(InputStream fileInputStream) throws Exception {
|
|
|
390 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = this.parseFromExcel(fileInputStream);
|
| 32868 |
amit.gupta |
391 |
Set<YearMonth> ymListToEvict = new HashSet<>();
|
| 31747 |
amit.gupta |
392 |
for (Map.Entry<CreateOfferRequest, List<PartnerTargetModel>> partnerTargetEntry : offerPartnerTargetMap
|
|
|
393 |
.entrySet()) {
|
|
|
394 |
CreateOfferRequest createOfferRequest = partnerTargetEntry.getKey();
|
|
|
395 |
Map<List<Integer>, List<Integer>> targetPartnerMap = partnerTargetEntry.getValue().stream()
|
|
|
396 |
.collect(Collectors.groupingBy(PartnerTargetModel::getTargets,
|
|
|
397 |
Collectors.mapping(PartnerTargetModel::getFofoId, Collectors.toList())));
|
|
|
398 |
for (Map.Entry<List<Integer>, List<Integer>> targetPartnersEntry : targetPartnerMap.entrySet()) {
|
|
|
399 |
List<Integer> fofoIds = createOfferRequest.getPartnerCriteria().getFofoIds();
|
|
|
400 |
List<Integer> filterFofoIds = targetPartnersEntry.getValue().stream().filter(x -> !fofoIds.contains(x))
|
|
|
401 |
.collect(Collectors.toList());
|
|
|
402 |
LOGGER.info("FofoIds {}", filterFofoIds);
|
|
|
403 |
List<Integer> targets = targetPartnersEntry.getKey();
|
|
|
404 |
LOGGER.info("Targets {}", targets);
|
| 29806 |
amit.gupta |
405 |
|
| 31747 |
amit.gupta |
406 |
createOfferRequest.getPartnerCriteria().setFofoIds(filterFofoIds);
|
|
|
407 |
int counter = 0;
|
|
|
408 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0)
|
|
|
409 |
.getItemCriteriaPayouts();
|
|
|
410 |
List<PayoutSlab> payoutSlabs = itemCriteriaPayouts.stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.toList());
|
|
|
411 |
for (PayoutSlab payoutSlab : payoutSlabs) {
|
|
|
412 |
payoutSlab.setOnwardsAmount(targets.get(counter));
|
|
|
413 |
counter++;
|
|
|
414 |
}
|
|
|
415 |
this.addOfferService(createOfferRequest);
|
|
|
416 |
}
|
| 32868 |
amit.gupta |
417 |
ymListToEvict.add(YearMonth.from(createOfferRequest.getStartDate()));
|
| 31747 |
amit.gupta |
418 |
}
|
| 32868 |
amit.gupta |
419 |
for (YearMonth ymToEvict : ymListToEvict) {
|
|
|
420 |
oneDayCacheManager.getCache("allOffers").evict(ymToEvict);
|
|
|
421 |
}
|
| 27877 |
amit.gupta |
422 |
|
| 31747 |
amit.gupta |
423 |
}
|
| 29783 |
amit.gupta |
424 |
|
| 31747 |
amit.gupta |
425 |
private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream)
|
|
|
426 |
throws ProfitMandiBusinessException {
|
| 29783 |
amit.gupta |
427 |
|
| 31747 |
amit.gupta |
428 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = new HashMap<>();
|
|
|
429 |
XSSFWorkbook myWorkBook = null;
|
|
|
430 |
try {
|
|
|
431 |
myWorkBook = new XSSFWorkbook(inputStream);
|
|
|
432 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
433 |
for (int i = 0; i < myWorkBook.getNumberOfSheets(); i++) {
|
| 29683 |
amit.gupta |
434 |
|
| 31747 |
amit.gupta |
435 |
XSSFSheet mySheet = myWorkBook.getSheetAt(i);
|
|
|
436 |
int offerId = Integer.parseInt(mySheet.getSheetName());
|
|
|
437 |
CreateOfferRequest existingOffer = this.getOffer(0, offerId);
|
|
|
438 |
List<PartnerTargetModel> partnerTargetModels = new ArrayList<>();
|
|
|
439 |
offerPartnerTargetMap.put(existingOffer, partnerTargetModels);
|
|
|
440 |
this.printIterator(mySheet);
|
|
|
441 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
|
|
442 |
for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
|
|
|
443 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
444 |
LOGGER.info("row {}", row);
|
|
|
445 |
PartnerTargetModel partnerTargetModel = new PartnerTargetModel();
|
| 29783 |
amit.gupta |
446 |
|
| 31747 |
amit.gupta |
447 |
if (row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
448 |
partnerTargetModel.setFofoId((int) row.getCell(0).getNumericCellValue());
|
|
|
449 |
} else {
|
|
|
450 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
451 |
"FOFO ID", row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
|
|
452 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
453 |
throw profitMandiBusinessException;
|
|
|
454 |
}
|
|
|
455 |
List<Integer> targets = new ArrayList<>();
|
|
|
456 |
long targetsSize = existingOffer.getTargetSlabs().get(0).getItemCriteriaPayouts().stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.counting());
|
|
|
457 |
for (int cellNumber = 1; cellNumber <= targetsSize; cellNumber++) {
|
|
|
458 |
if (row.getCell(cellNumber) != null
|
|
|
459 |
&& row.getCell(cellNumber).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
460 |
targets.add((int) row.getCell(cellNumber).getNumericCellValue());
|
|
|
461 |
} else {
|
|
|
462 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
463 |
"Target Column issue at " + rowNumber + ", " + cellNumber, row.getCell(cellNumber),
|
|
|
464 |
"Invalid target");
|
|
|
465 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
466 |
throw profitMandiBusinessException;
|
|
|
467 |
}
|
|
|
468 |
}
|
|
|
469 |
partnerTargetModel.setTargets(targets);
|
|
|
470 |
partnerTargetModels.add(partnerTargetModel);
|
|
|
471 |
}
|
|
|
472 |
}
|
|
|
473 |
myWorkBook.close();
|
|
|
474 |
} catch (ProfitMandiBusinessException pbse) {
|
|
|
475 |
throw pbse;
|
|
|
476 |
} catch (IOException ioException) {
|
|
|
477 |
ioException.printStackTrace();
|
|
|
478 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(),
|
|
|
479 |
"EXL_VE_1000");
|
|
|
480 |
} finally {
|
|
|
481 |
if (myWorkBook != null) {
|
|
|
482 |
try {
|
|
|
483 |
myWorkBook.close();
|
|
|
484 |
} catch (IOException e) {
|
|
|
485 |
// TODO Auto-generated catch block
|
|
|
486 |
e.printStackTrace();
|
|
|
487 |
}
|
|
|
488 |
}
|
|
|
489 |
}
|
|
|
490 |
LOGGER.info("offerPartnerTargetMap {}", offerPartnerTargetMap);
|
|
|
491 |
return offerPartnerTargetMap;
|
|
|
492 |
}
|
| 29783 |
amit.gupta |
493 |
|
| 31747 |
amit.gupta |
494 |
private void printIterator(XSSFSheet sheet) {
|
|
|
495 |
Iterator<Row> rowIterator = sheet.iterator();
|
|
|
496 |
while (rowIterator.hasNext()) {
|
|
|
497 |
Row row = rowIterator.next();
|
|
|
498 |
// For each row, iterate through all the columns
|
|
|
499 |
Iterator<Cell> cellIterator = row.cellIterator();
|
| 29683 |
amit.gupta |
500 |
|
| 31747 |
amit.gupta |
501 |
while (cellIterator.hasNext()) {
|
|
|
502 |
Cell cell = cellIterator.next();
|
|
|
503 |
// Check the cell type and format accordingly
|
|
|
504 |
switch (cell.getCellType()) {
|
|
|
505 |
case Cell.CELL_TYPE_NUMERIC:
|
|
|
506 |
System.out.print(cell.getNumericCellValue() + "t");
|
|
|
507 |
break;
|
|
|
508 |
case Cell.CELL_TYPE_STRING:
|
|
|
509 |
System.out.print(cell.getStringCellValue() + "t");
|
|
|
510 |
break;
|
|
|
511 |
}
|
|
|
512 |
}
|
|
|
513 |
System.out.println("");
|
|
|
514 |
}
|
|
|
515 |
}
|
| 29683 |
amit.gupta |
516 |
|
| 31747 |
amit.gupta |
517 |
@Override
|
|
|
518 |
public Map<Integer, CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, String brand) {
|
|
|
519 |
// TODO Auto-generated method stub
|
|
|
520 |
return null;
|
|
|
521 |
}
|
| 30684 |
amit.gupta |
522 |
|
| 31747 |
amit.gupta |
523 |
@Override
|
| 32043 |
amit.gupta |
524 |
@Cacheable(value = "slabPayoutMap1", cacheManager = "oneDayCacheManager")
|
| 31747 |
amit.gupta |
525 |
public Map<Integer, Map<Integer, Long>> getSlabPayoutMap(CreateOfferRequest createOfferRequest) {
|
|
|
526 |
Map<Integer, Map<Integer, Long>> catalogSlabPayoutMap = new HashMap<>();
|
|
|
527 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
|
|
528 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
| 29783 |
amit.gupta |
529 |
|
| 31747 |
amit.gupta |
530 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
531 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
|
|
532 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
|
|
533 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
534 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
|
|
535 |
for (Integer catalogId : catalogIds) {
|
|
|
536 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
537 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
538 |
}
|
|
|
539 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
540 |
(long) payoutSlab.getPayoutAmount());
|
|
|
541 |
}
|
|
|
542 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_FIXED)) {
|
|
|
543 |
for (Integer catalogId : catalogIds) {
|
|
|
544 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
545 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
546 |
}
|
|
|
547 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
548 |
(long) (payoutSlab.getPayoutAmount() / payoutSlab.getOnwardsAmount()));
|
|
|
549 |
}
|
|
|
550 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
551 |
Map<Integer, TagListing> tagListingsMap = tagListingRepository.selectAllByCatalogIds(catalogIds);
|
|
|
552 |
for (Map.Entry<Integer, TagListing> tagListingEntry : tagListingsMap.entrySet()) {
|
|
|
553 |
TagListing tagListing = tagListingEntry.getValue();
|
|
|
554 |
if (!catalogSlabPayoutMap.containsKey(tagListingEntry.getKey())) {
|
|
|
555 |
catalogSlabPayoutMap.put(tagListingEntry.getKey(), new LinkedHashMap<>());
|
|
|
556 |
}
|
|
|
557 |
catalogSlabPayoutMap.get(tagListingEntry.getKey()).put(payoutSlab.getOnwardsAmount(),
|
|
|
558 |
(long) (tagListing.getSellingPrice() * payoutSlab.getPayoutAmount() / 100));
|
|
|
559 |
}
|
|
|
560 |
}
|
|
|
561 |
}
|
|
|
562 |
}
|
|
|
563 |
return catalogSlabPayoutMap;
|
|
|
564 |
}
|
|
|
565 |
|
| 31903 |
amit.gupta |
566 |
@Override
|
|
|
567 |
@Cacheable(value = "slabPayoutMap", cacheManager = "oneDayCacheManager")
|
|
|
568 |
public Map<Integer, Map<Integer, AmountModel>> getSlabPayoutMapNew(CreateOfferRequest createOfferRequest) {
|
|
|
569 |
Map<Integer, Map<Integer, AmountModel>> catalogSlabPayoutMap = new HashMap<>();
|
|
|
570 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
|
|
571 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
|
|
572 |
|
|
|
573 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
574 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
|
|
575 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
|
|
576 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
577 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
|
|
578 |
for (Integer catalogId : catalogIds) {
|
|
|
579 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
580 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
581 |
}
|
|
|
582 |
AmountModel amountModel = new AmountModel();
|
|
|
583 |
amountModel.setAmountType(AmountType.FIXED);
|
|
|
584 |
amountModel.setAmount(payoutSlab.getPayoutAmount());
|
| 32021 |
amit.gupta |
585 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31903 |
amit.gupta |
586 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
587 |
amountModel);
|
|
|
588 |
}
|
|
|
589 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_FIXED)) {
|
|
|
590 |
for (Integer catalogId : catalogIds) {
|
|
|
591 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
592 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
593 |
}
|
|
|
594 |
|
|
|
595 |
AmountModel amountModel = new AmountModel();
|
|
|
596 |
amountModel.setAmountType(AmountType.FIXED);
|
| 32021 |
amit.gupta |
597 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31903 |
amit.gupta |
598 |
amountModel.setAmount(payoutSlab.getPayoutAmount() / payoutSlab.getOnwardsAmount());
|
|
|
599 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
600 |
amountModel);
|
|
|
601 |
}
|
|
|
602 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
603 |
for (Integer catalogId : catalogIds) {
|
|
|
604 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
605 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
606 |
}
|
|
|
607 |
AmountModel amountModel = new AmountModel();
|
|
|
608 |
amountModel.setAmountType(AmountType.PERCENTAGE);
|
|
|
609 |
amountModel.setAmount(payoutSlab.getPayoutAmount());
|
| 32021 |
amit.gupta |
610 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31903 |
amit.gupta |
611 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
612 |
amountModel);
|
|
|
613 |
}
|
|
|
614 |
}
|
|
|
615 |
}
|
|
|
616 |
}
|
|
|
617 |
return catalogSlabPayoutMap;
|
|
|
618 |
}
|
|
|
619 |
|
| 31920 |
amit.gupta |
620 |
|
|
|
621 |
private double getAmount(InventoryPayoutModel inventoryPayoutModel, AmountModel amountModel) {
|
|
|
622 |
double rollout = 0;
|
|
|
623 |
if (amountModel.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
624 |
double effectiveDp = inventoryPayoutModel.getDp() - inventoryPayoutModel.getFixedAmount();
|
|
|
625 |
double totalPercentage = inventoryPayoutModel.getPercentageAmount() + amountModel.getAmount();
|
|
|
626 |
rollout = effectiveDp * (totalPercentage / (100 + totalPercentage) - (inventoryPayoutModel.getPercentageAmount() / (100 + inventoryPayoutModel.getPercentageAmount())));
|
|
|
627 |
} else {
|
|
|
628 |
rollout = amountModel.getAmount() * (100 / (100 + inventoryPayoutModel.getPercentageAmount()));
|
|
|
629 |
}
|
|
|
630 |
return rollout;
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
@Override
|
|
|
634 |
public void processActivationtOffer(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
635 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
|
|
636 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
|
|
637 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream().collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
| 31923 |
amit.gupta |
638 |
LOGGER.info("itemCriteriaPayoutMap {}", itemCriteriaPayoutMap);
|
|
|
639 |
LOGGER.info("offerPayouts {}", offerPayouts);
|
| 31924 |
amit.gupta |
640 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream().filter(x -> itemCriteriaPayoutMap.get((int) x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
| 31920 |
amit.gupta |
641 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(), Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
|
|
642 |
|
|
|
643 |
|
|
|
644 |
Map<String, Double> serialNumberPaid = offerPayouts.stream().filter(x -> x.getRejectTimestamp() == null).collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
|
|
645 |
Map<Integer, Map<Integer, List<FofoOrderItem>>> criteriaPartnerwiseTertiary = offerRepository.getCriteriaWisePartnerTertiary(createOfferRequest);
|
|
|
646 |
|
|
|
647 |
|
|
|
648 |
final Set<String> activatedImeis;
|
|
|
649 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
650 |
//All Serialnumbers for all eligible retailers
|
|
|
651 |
List<String> serialNumbers = criteriaPartnerwiseTertiary.entrySet().stream().flatMap(x -> x.getValue().entrySet().stream().flatMap(foiList -> foiList.getValue().stream()))
|
|
|
652 |
.flatMap(y -> y.getFofoLineItems().stream()).filter(fli -> fli.getSerialNumber() != null)
|
|
|
653 |
.map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
654 |
if (serialNumbers.size() > 0) {
|
|
|
655 |
activatedImeis = activatedImeiRepository.selectBySerialNumbers(serialNumbers).stream()
|
|
|
656 |
.filter(x -> createOfferRequest.isWithinRange(x.getActivationTimestamp()))
|
|
|
657 |
.map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
|
|
658 |
} else {
|
|
|
659 |
activatedImeis = null;
|
|
|
660 |
}
|
|
|
661 |
} else {
|
|
|
662 |
activatedImeis = null;
|
|
|
663 |
}
|
|
|
664 |
|
|
|
665 |
Map<Integer, Integer> userBaseQtyMap = new HashMap<>();
|
|
|
666 |
Map<Integer, Integer> userBaseValueMap = new HashMap<>();
|
|
|
667 |
Map<Integer, Integer> eligibleBaseQtyMap = new HashMap<>();
|
|
|
668 |
Map<Integer, Integer> eligibleBaseValueMap = new HashMap<>();
|
|
|
669 |
|
|
|
670 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
671 |
Map<Integer, List<FofoOrderItem>> foiMap = criteriaPartnerwiseTertiary.get(0);
|
|
|
672 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : foiMap.entrySet()) {
|
|
|
673 |
int fofoId = orderEntry.getKey();
|
|
|
674 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
|
|
675 |
int totalBaseQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
676 |
int totalBaseValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (Math.round(x.getDp())) * x.getQuantity()));
|
|
|
677 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
678 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
679 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
680 |
Map<Integer, Integer> inventoryItemValueMap = new HashMap<>();
|
|
|
681 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
682 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
683 |
lineItem -> {
|
|
|
684 |
if (activatedImeis == null || activatedImeis.contains(lineItem.getSerialNumber())) {
|
|
|
685 |
inventoryItemValueMap.put(lineItem.getInventoryItemId(), (int) fofoOrderItem.getDp());
|
|
|
686 |
}
|
|
|
687 |
}
|
|
|
688 |
);
|
|
|
689 |
}
|
|
|
690 |
eligibleBaseQtyMap.put(fofoId, inventoryItemValueMap.size());
|
|
|
691 |
eligibleBaseValueMap.put(fofoId, inventoryItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue())));
|
|
|
692 |
} else {
|
|
|
693 |
eligibleBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
694 |
eligibleBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
695 |
|
|
|
696 |
}
|
|
|
697 |
|
|
|
698 |
}
|
|
|
699 |
}
|
|
|
700 |
|
|
|
701 |
//As of now eligible sale is considered to be without special criteria like brand/
|
|
|
702 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
703 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
704 |
Map<Integer, List<FofoOrderItem>> ordersMap = criteriaPartnerwiseTertiary.get(criteriaId);
|
|
|
705 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : ordersMap.entrySet()) {
|
| 31992 |
amit.gupta |
706 |
double totalRolloutAmount = 0;
|
| 31920 |
amit.gupta |
707 |
int fofoId = orderEntry.getKey();
|
|
|
708 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
|
|
709 |
OfferRowModel offerRowModel = new OfferRowModel();
|
|
|
710 |
int totalQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
711 |
int totalValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> Math.round(x.getDp()) * (x.getQuantity())));
|
|
|
712 |
int eligibleQty = totalQty;
|
|
|
713 |
int eligibleValue = totalValue;
|
|
|
714 |
Map<FofoLineItem, Integer> lineItemValueMap = new HashMap<>();
|
|
|
715 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
716 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
717 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
718 |
lineItem -> {
|
|
|
719 |
if (activatedImeis == null || activatedImeis.contains(lineItem.getSerialNumber())) {
|
|
|
720 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
|
|
721 |
}
|
|
|
722 |
}
|
|
|
723 |
);
|
|
|
724 |
}
|
|
|
725 |
eligibleQty = lineItemValueMap.size();
|
|
|
726 |
eligibleValue = lineItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue()));
|
|
|
727 |
offerRowModel.setEligibleImeis(lineItemValueMap.keySet().stream().map(x -> x.getSerialNumber()).collect(Collectors.toList()));
|
|
|
728 |
}
|
|
|
729 |
|
|
|
730 |
int totalBaseQty = totalQty;
|
|
|
731 |
int totalBaseValue = totalValue;
|
|
|
732 |
int eligibleBaseQty = eligibleQty;
|
|
|
733 |
int eligibleBaseValue = eligibleValue;
|
|
|
734 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
735 |
totalBaseQty = userBaseQtyMap.get(fofoId) == null ? 0 : userBaseQtyMap.get(fofoId);
|
|
|
736 |
totalBaseValue = userBaseValueMap.get(fofoId) == null ? 0 : userBaseValueMap.get(fofoId);
|
|
|
737 |
eligibleBaseQty = eligibleBaseQtyMap.get(fofoId) == null ? 0 : eligibleBaseQtyMap.get(fofoId);
|
|
|
738 |
eligibleBaseValue = eligibleBaseValueMap.get(fofoId) == null ? 0 : eligibleBaseValueMap.get(fofoId);
|
|
|
739 |
}
|
|
|
740 |
|
|
|
741 |
|
|
|
742 |
int totalSale = totalBaseValue;
|
|
|
743 |
int eligibleSale = eligibleBaseValue;
|
|
|
744 |
int criteriaSale = totalValue;
|
|
|
745 |
int eligibleCriteriaSale = eligibleValue;
|
|
|
746 |
int eligibleCriteriaSaleDp = eligibleValue;
|
|
|
747 |
|
|
|
748 |
if (createOfferRequest.getTargetType().equals(AchievementType.QUANTITY)) {
|
|
|
749 |
totalSale = totalBaseQty;
|
|
|
750 |
eligibleSale = eligibleBaseQty;
|
|
|
751 |
criteriaSale = totalQty;
|
|
|
752 |
eligibleCriteriaSale = eligibleQty;
|
|
|
753 |
}
|
|
|
754 |
|
|
|
755 |
offerRowModel.setTotalSale(totalSale);
|
|
|
756 |
offerRowModel.setEligibleSale(eligibleSale);
|
|
|
757 |
float eligiblePayoutValue = 0;
|
| 32345 |
amit.gupta |
758 |
double finalPayout;
|
| 31920 |
amit.gupta |
759 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
|
|
760 |
if (payoutSlab.getOnwardsAmount() <= eligibleSale) {
|
| 32039 |
amit.gupta |
761 |
LOGGER.info("Eligible Sale {}", eligibleSale);
|
| 31920 |
amit.gupta |
762 |
offerRowModel.setPayoutTargetAchieved(payoutSlab.getOnwardsAmount());
|
|
|
763 |
eligiblePayoutValue = payoutSlab.getPayoutAmount();
|
|
|
764 |
offerRowModel.setPayoutValue(eligiblePayoutValue);
|
|
|
765 |
}
|
|
|
766 |
}
|
|
|
767 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
768 |
finalPayout = (eligiblePayoutValue * eligibleCriteriaSaleDp) / 100;
|
|
|
769 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
770 |
finalPayout = eligiblePayoutValue * eligibleCriteriaSale;
|
|
|
771 |
} else {
|
| 32039 |
amit.gupta |
772 |
Map<Integer, Double> fofoCriteriaMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
|
|
773 |
LOGGER.info("fofoCriteriaMap {}", fofoCriteriaMap);
|
| 31920 |
amit.gupta |
774 |
finalPayout = eligiblePayoutValue;
|
|
|
775 |
if (fofoCriteriaMap != null) {
|
|
|
776 |
if (fofoCriteriaMap.containsKey(fofoId)) {
|
|
|
777 |
double totalPayout = fofoCriteriaMap.get(fofoId);
|
| 32039 |
amit.gupta |
778 |
finalPayout = eligiblePayoutValue - totalPayout;
|
|
|
779 |
if (Math.abs(finalPayout) < Utils.DOUBLE_EPSILON) {
|
| 31920 |
amit.gupta |
780 |
finalPayout = 0;
|
| 32039 |
amit.gupta |
781 |
}
|
| 31920 |
amit.gupta |
782 |
}
|
|
|
783 |
}
|
|
|
784 |
}
|
|
|
785 |
|
| 32039 |
amit.gupta |
786 |
LOGGER.info("Final Payout - {}", finalPayout);
|
|
|
787 |
|
| 31920 |
amit.gupta |
788 |
if (finalPayout > 0) {
|
|
|
789 |
List<Integer> inventoryItemIds = lineItemValueMap.keySet().stream().map(x -> x.getInventoryItemId()).collect(Collectors.toList());
|
|
|
790 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItemRepository.selectAllByIds(inventoryItemIds).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
791 |
for (Map.Entry<FofoLineItem, Integer> lineItemValueEntry : lineItemValueMap.entrySet()) {
|
|
|
792 |
double amount = 0;
|
| 32039 |
amit.gupta |
793 |
int totalDpValue = lineItemValueMap.values().stream().collect(Collectors.summingInt(x -> x));
|
|
|
794 |
LOGGER.info("Total DP Value {}", totalDpValue);
|
| 31920 |
amit.gupta |
795 |
if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| 32039 |
amit.gupta |
796 |
amount = (eligiblePayoutValue * lineItemValueEntry.getValue()) / totalDpValue;
|
|
|
797 |
LOGGER.info("Amount - {}", amount);
|
| 31920 |
amit.gupta |
798 |
//Fixed slab should be treated as fixed after payout
|
|
|
799 |
} else {
|
|
|
800 |
amount = eligiblePayoutValue;
|
| 32039 |
amit.gupta |
801 |
//Fixed slab has already taken care of that amount;
|
|
|
802 |
if (serialNumberPaid.containsKey(lineItemValueEntry.getKey().getSerialNumber())) {
|
|
|
803 |
amount = amount - serialNumberPaid.get(lineItemValueEntry.getKey().getSerialNumber());
|
|
|
804 |
}
|
| 31920 |
amit.gupta |
805 |
}
|
| 32039 |
amit.gupta |
806 |
LOGGER.info("Amount - {}", amount);
|
| 31920 |
amit.gupta |
807 |
//ignore reasonably small
|
|
|
808 |
if (amount < Utils.DOUBLE_EPSILON) continue;
|
|
|
809 |
AmountModel amountModel = new AmountModel();
|
|
|
810 |
amountModel.setAmountType(itemCriteriaPayout.getAmountType());
|
|
|
811 |
amountModel.setAmount(amount);
|
| 32021 |
amit.gupta |
812 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31920 |
amit.gupta |
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 |
}
|
| 32039 |
amit.gupta |
821 |
walletService.addAmountToWallet((int) fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) totalRolloutAmount, LocalDateTime.now());
|
| 31920 |
amit.gupta |
822 |
}
|
|
|
823 |
}
|
|
|
824 |
}
|
|
|
825 |
Offer offer = offerRepository.selectById(createOfferRequest.getId());
|
|
|
826 |
offer.setProcessedTimestamp(LocalDateTime.now());
|
|
|
827 |
}
|
|
|
828 |
|
| 32064 |
amit.gupta |
829 |
private void extracted(CreateOfferRequest createOfferRequest, Map<String, Double> serialNumberPaid, ItemCriteriaPayout itemCriteriaPayout, int criteriaId, double totalRolloutAmount, int fofoId, Map<FofoLineItem, Integer> lineItemValueMap, float eligiblePayoutValue) throws ProfitMandiBusinessException {
|
|
|
830 |
List<Integer> inventoryItemIds = lineItemValueMap.keySet().stream().map(x -> x.getInventoryItemId()).collect(Collectors.toList());
|
|
|
831 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItemRepository.selectAllByIds(inventoryItemIds).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
832 |
for (Map.Entry<FofoLineItem, Integer> lineItemValueEntry : lineItemValueMap.entrySet()) {
|
|
|
833 |
double amount = 0;
|
|
|
834 |
int totalDpValue = lineItemValueMap.values().stream().collect(Collectors.summingInt(x -> x));
|
|
|
835 |
LOGGER.info("Total DP Value {}", totalDpValue);
|
|
|
836 |
if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
837 |
amount = (eligiblePayoutValue * lineItemValueEntry.getValue()) / totalDpValue;
|
|
|
838 |
LOGGER.info("Amount - {}", amount);
|
|
|
839 |
//Fixed slab should be treated as fixed after payout
|
|
|
840 |
} else {
|
|
|
841 |
amount = eligiblePayoutValue;
|
|
|
842 |
//Fixed slab has already taken care of that amount;
|
|
|
843 |
if (serialNumberPaid.containsKey(lineItemValueEntry.getKey().getSerialNumber())) {
|
|
|
844 |
amount = amount - serialNumberPaid.get(lineItemValueEntry.getKey().getSerialNumber());
|
|
|
845 |
}
|
|
|
846 |
}
|
|
|
847 |
LOGGER.info("Amount - {}", amount);
|
|
|
848 |
//ignore reasonably small
|
|
|
849 |
if (amount < Utils.DOUBLE_EPSILON) continue;
|
|
|
850 |
AmountModel amountModel = new AmountModel();
|
|
|
851 |
amountModel.setAmountType(itemCriteriaPayout.getAmountType());
|
|
|
852 |
amountModel.setAmount(amount);
|
|
|
853 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
|
|
854 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItemsMap.get(lineItemValueEntry.getKey().getInventoryItemId()));
|
|
|
855 |
double rolloutAmount = inventoryPayoutModel.getRolloutAmount(amountModel);
|
|
|
856 |
totalRolloutAmount += rolloutAmount;
|
|
|
857 |
|
|
|
858 |
OfferPayout offerPayout = new OfferPayout(fofoId, createOfferRequest.getId(), criteriaId, amount, lineItemValueEntry.getKey().getSerialNumber(), rolloutAmount, SchemePayoutStatus.CREDITED, createOfferRequest.getDescription(), LocalDateTime.now());
|
|
|
859 |
//offerPayout.setInventoryItemId(inventoryItemSaleModelsMap.get(lineItemValueEntry.getKey().getInventoryItemId()));
|
|
|
860 |
offerPayoutRepository.persist(offerPayout);
|
|
|
861 |
}
|
|
|
862 |
walletService.addAmountToWallet((int) fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) totalRolloutAmount, LocalDateTime.now());
|
|
|
863 |
}
|
|
|
864 |
|
| 31920 |
amit.gupta |
865 |
@Override
|
|
|
866 |
public void processSellin(CreateOfferRequest createOfferRequest) throws Exception {
|
|
|
867 |
|
|
|
868 |
//List<OfferRowModel> offerRowModels = new ArrayList<>();
|
|
|
869 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
| 32345 |
amit.gupta |
870 |
Map<String, Double> serialNumberPaid = offerPayouts.stream().filter(x -> x.getRejectTimestamp() == null).collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
|
|
871 |
|
|
|
872 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
|
|
873 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream().collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
|
|
874 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream().filter(x -> itemCriteriaPayoutMap.get((int) x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
|
|
875 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(), Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
| 31920 |
amit.gupta |
876 |
Set<Long> fofoIds = offerPayouts.stream().map(x -> x.getFofoId()).collect(Collectors.toSet());
|
|
|
877 |
|
|
|
878 |
Map<Integer, Map<Integer, List<Order>>> criteriaOrdersMap = offerRepository.getPartnerWiseSellin(createOfferRequest, true);
|
|
|
879 |
|
| 32345 |
amit.gupta |
880 |
|
| 31920 |
amit.gupta |
881 |
Map<Integer, List<Order>> userBaseCriteriaOrdersMap = null;
|
|
|
882 |
if (criteriaOrdersMap.get(0) != null && criteriaOrdersMap.get(0).size() > 0) {
|
|
|
883 |
userBaseCriteriaOrdersMap = criteriaOrdersMap.get(0).entrySet().stream()
|
|
|
884 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
885 |
}
|
|
|
886 |
|
| 32345 |
amit.gupta |
887 |
|
| 31920 |
amit.gupta |
888 |
Map<Integer, Integer> userBaseQtyMap = null;
|
|
|
889 |
Map<Integer, Integer> userBaseValueMap = null;
|
|
|
890 |
if (userBaseCriteriaOrdersMap != null) {
|
|
|
891 |
userBaseQtyMap = new HashMap<>();
|
|
|
892 |
userBaseValueMap = new HashMap<>();
|
|
|
893 |
for (Map.Entry<Integer, List<Order>> orderEntry : userBaseCriteriaOrdersMap.entrySet()) {
|
|
|
894 |
int fofoId = orderEntry.getKey();
|
|
|
895 |
List<Order> orders = orderEntry.getValue();
|
|
|
896 |
int totalBaseQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
|
|
897 |
int totalBaseValue = orders.stream().collect(Collectors.summingInt(x ->
|
|
|
898 |
Math.round(x.getLineItem().getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))
|
|
|
899 |
));
|
|
|
900 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
901 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
902 |
}
|
|
|
903 |
}
|
|
|
904 |
LOGGER.info("Base Qty Map - {}", userBaseQtyMap);
|
|
|
905 |
LOGGER.info("Base Value Map - {}", userBaseValueMap);
|
|
|
906 |
|
|
|
907 |
LOGGER.info("Processing sellin");
|
|
|
908 |
List<OfferPayout> offerPayoutsNew = new ArrayList<>();
|
|
|
909 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
910 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
911 |
Map<Integer, List<Order>> ordersMap = criteriaOrdersMap.get(criteriaId).entrySet().stream()
|
|
|
912 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
913 |
for (Map.Entry<Integer, List<Order>> orderEntry : ordersMap.entrySet()) {
|
|
|
914 |
int fofoId = orderEntry.getKey();
|
|
|
915 |
//CustomRetailer retailer = retailerService.getAllFofoRetailers().get(fofoId);
|
|
|
916 |
List<Order> orders = orderEntry.getValue();
|
| 32345 |
amit.gupta |
917 |
//Set<String> returnedSerialNumbers = orders.stream().filter(x -> x.getSerialNumber() != null).flatMap(x -> x.getReturnedImeis().stream()).collect(Collectors.toSet());
|
| 31920 |
amit.gupta |
918 |
int totalQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
|
|
919 |
int totalValue = orders.stream().collect(Collectors.summingInt(x -> Math.round(x.getLineItem().
|
|
|
920 |
getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))));
|
|
|
921 |
int totalBaseQty = totalQty;
|
|
|
922 |
int totalBaseValue = totalValue;
|
|
|
923 |
if (userBaseCriteriaOrdersMap != null) {
|
|
|
924 |
if (userBaseQtyMap.containsKey(fofoId)) {
|
|
|
925 |
totalBaseQty = userBaseQtyMap.get(fofoId);
|
|
|
926 |
totalBaseValue = userBaseValueMap.get(fofoId);
|
|
|
927 |
} else {
|
|
|
928 |
continue;
|
|
|
929 |
}
|
|
|
930 |
}
|
|
|
931 |
|
|
|
932 |
int purchasedValue;
|
|
|
933 |
if (createOfferRequest.getTargetType().equals(AchievementType.VALUE)) {
|
|
|
934 |
purchasedValue = totalBaseValue;
|
|
|
935 |
} else {
|
|
|
936 |
purchasedValue = totalBaseQty;
|
|
|
937 |
}
|
|
|
938 |
PayoutSlab currentSlab = itemCriteriaPayout.getPayoutSlabs().stream()
|
|
|
939 |
.filter(x -> x.getOnwardsAmount() <= purchasedValue).max(Comparator.comparing(x
|
|
|
940 |
-> x.getOnwardsAmount())).orElse(null);
|
|
|
941 |
|
|
|
942 |
//TODO-- payouts handling is here
|
|
|
943 |
if (currentSlab == null) continue;
|
|
|
944 |
List<Integer> orderIds = orders.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
945 |
Map<Integer, LineItem> lineItemMap = orders.stream().collect(Collectors.toMap(x -> x.getLineItem().getId(), x -> x.getLineItem()));
|
|
|
946 |
List<WarehouseScan> warehouseScans = warehouseScanRepository.selectAllByOrderIds(orderIds);
|
| 32540 |
amit.gupta |
947 |
LOGGER.info("Warehosue scans --> {}", warehouseScans);
|
| 32345 |
amit.gupta |
948 |
List<Integer> warehouseInventoryItemIds = warehouseScans.stream().collect(Collectors.groupingBy(x -> x.getInventoryItemId())).entrySet()
|
| 31920 |
amit.gupta |
949 |
.stream().filter(x -> x.getValue().size() == 1).map(x -> x.getKey()).collect(Collectors.toList());
|
| 32795 |
amit.gupta |
950 |
|
|
|
951 |
List<OfferPayout> partnerPayouts = offerPayouts.stream().filter(x -> x.getFofoId() == fofoId).collect(Collectors.toList());
|
| 32868 |
amit.gupta |
952 |
Set<String> serialNumbersRejected = partnerPayouts.stream().filter(x -> x.getStatus().equals(SchemePayoutStatus.REJECTED)).map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
| 32795 |
amit.gupta |
953 |
|
| 31920 |
amit.gupta |
954 |
Set<String> serialNumbers;
|
| 32345 |
amit.gupta |
955 |
LOGGER.info("warehouseInventoryItemIds {}", warehouseInventoryItemIds);
|
|
|
956 |
if (warehouseInventoryItemIds.size() > 0) {
|
|
|
957 |
List<WarehouseInventoryItem> warehouseInventoryItems = warehouseInventoryItemRepository.selectAllByIds(warehouseInventoryItemIds);
|
| 31920 |
amit.gupta |
958 |
serialNumbers = warehouseInventoryItems.stream().map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
|
|
959 |
} else {
|
|
|
960 |
serialNumbers = new HashSet<>();
|
|
|
961 |
}
|
|
|
962 |
LOGGER.info("SerialNumbers {}", serialNumbers);
|
|
|
963 |
Map<String, Long> serialNumberCountMap = lineItemImeisRepository.selectByLineItemIds(new ArrayList<>(lineItemMap.keySet()))
|
|
|
964 |
.stream().collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.counting()));
|
|
|
965 |
LOGGER.info("SerialNumbers Count Map {}", serialNumberCountMap);
|
|
|
966 |
Map<String, LineItem> serialNumberMap = lineItemImeisRepository.selectByLineItemIds(new ArrayList<>(lineItemMap.keySet()))
|
| 32868 |
amit.gupta |
967 |
.stream().filter(x -> !serialNumbersRejected.contains(x.getSerialNumber())).collect(Collectors.toMap(x -> x.getSerialNumber(), x -> lineItemMap.get(x.getLineItemId()), (x1, x2) -> x1.getId() > x2.getId() ? x1 : x2));
|
| 31920 |
amit.gupta |
968 |
|
| 32795 |
amit.gupta |
969 |
|
| 31920 |
amit.gupta |
970 |
serialNumberMap = serialNumberMap.entrySet().stream().filter(x -> serialNumbers.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
| 32542 |
amit.gupta |
971 |
LOGGER.info("serialNumberMap {}", serialNumberMap);
|
| 32543 |
amit.gupta |
972 |
if (serialNumberMap.size() == 0) continue;
|
| 32064 |
amit.gupta |
973 |
Map<String, InventoryItem> serialNumberInventoryItemMap = this.getInventoryItemMap(fofoId, serialNumberMap);
|
|
|
974 |
//Continue if all imeis havent been grned
|
| 32345 |
amit.gupta |
975 |
if (serialNumberInventoryItemMap.size() < serialNumberCountMap.size()) continue;
|
| 32064 |
amit.gupta |
976 |
|
| 32345 |
amit.gupta |
977 |
|
|
|
978 |
float eligiblePayoutValue = currentSlab.getPayoutAmount();
|
|
|
979 |
double finalPayout;
|
|
|
980 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
981 |
finalPayout = (eligiblePayoutValue * purchasedValue) / 100;
|
|
|
982 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
983 |
finalPayout = eligiblePayoutValue * purchasedValue;
|
|
|
984 |
} else {
|
|
|
985 |
Map<Integer, Double> fofoCriteriaMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
|
|
986 |
LOGGER.info("fofoCriteriaMap {}", fofoCriteriaMap);
|
|
|
987 |
finalPayout = eligiblePayoutValue;
|
|
|
988 |
if (fofoCriteriaMap != null) {
|
|
|
989 |
if (fofoCriteriaMap.containsKey(fofoId)) {
|
|
|
990 |
double totalPayout = fofoCriteriaMap.get(fofoId);
|
|
|
991 |
finalPayout = eligiblePayoutValue - totalPayout;
|
|
|
992 |
if (Math.abs(finalPayout) < Utils.DOUBLE_EPSILON) {
|
|
|
993 |
finalPayout = 0;
|
|
|
994 |
}
|
|
|
995 |
}
|
| 31920 |
amit.gupta |
996 |
}
|
|
|
997 |
}
|
| 32345 |
amit.gupta |
998 |
|
|
|
999 |
LOGGER.info("Or - {}", finalPayout);
|
|
|
1000 |
|
|
|
1001 |
if (finalPayout > 0) {
|
|
|
1002 |
double totalRolloutAmount = 0;
|
|
|
1003 |
int totalDpValue = serialNumberInventoryItemMap.entrySet().stream().mapToInt(x -> (int) x.getValue().getUnitPrice()).sum();
|
|
|
1004 |
for (Map.Entry<String, InventoryItem> serialNumberInventoryItemEntry : serialNumberInventoryItemMap.entrySet()) {
|
|
|
1005 |
double amount = 0;
|
|
|
1006 |
LOGGER.info("Total DP Value {}", totalDpValue);
|
|
|
1007 |
if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1008 |
amount = (eligiblePayoutValue * serialNumberInventoryItemEntry.getValue().getUnitPrice()) / totalDpValue;
|
|
|
1009 |
LOGGER.info("Amount - {}", amount);
|
|
|
1010 |
//Fixed slab should be treated as fixed after payout
|
|
|
1011 |
} else {
|
|
|
1012 |
amount = eligiblePayoutValue;
|
|
|
1013 |
//Fixed slab has already taken care of that amount;
|
|
|
1014 |
if (serialNumberPaid.containsKey(serialNumberInventoryItemEntry.getKey())) {
|
|
|
1015 |
amount = amount - serialNumberPaid.get(serialNumberInventoryItemEntry.getKey());
|
|
|
1016 |
}
|
|
|
1017 |
}
|
|
|
1018 |
LOGGER.info("Amount - {}", amount);
|
|
|
1019 |
//ignore reasonably small
|
|
|
1020 |
if (amount < Utils.DOUBLE_EPSILON) continue;
|
|
|
1021 |
AmountModel amountModel = new AmountModel();
|
|
|
1022 |
amountModel.setAmountType(itemCriteriaPayout.getAmountType());
|
|
|
1023 |
amountModel.setAmount(amount);
|
|
|
1024 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
|
|
1025 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(serialNumberInventoryItemEntry.getValue());
|
|
|
1026 |
double rolloutAmount = inventoryPayoutModel.getRolloutAmount(amountModel);
|
|
|
1027 |
totalRolloutAmount += rolloutAmount;
|
|
|
1028 |
|
|
|
1029 |
OfferPayout offerPayout = new OfferPayout(fofoId, createOfferRequest.getId(), criteriaId, amount, serialNumberInventoryItemEntry.getKey(), rolloutAmount, SchemePayoutStatus.CREDITED, createOfferRequest.getDescription(), LocalDateTime.now());
|
|
|
1030 |
offerPayoutRepository.persist(offerPayout);
|
|
|
1031 |
}
|
|
|
1032 |
walletService.addAmountToWallet((int) fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) totalRolloutAmount, LocalDateTime.now());
|
|
|
1033 |
}
|
| 31920 |
amit.gupta |
1034 |
}
|
|
|
1035 |
}
|
|
|
1036 |
Offer offer = offerRepository.selectById(createOfferRequest.getId());
|
|
|
1037 |
offer.setProcessedTimestamp(LocalDateTime.now());
|
|
|
1038 |
}
|
| 32064 |
amit.gupta |
1039 |
|
| 32795 |
amit.gupta |
1040 |
private void rollbackPayout(int fofoId, int offerId, List<OfferPayout> partnerPayouts, Set<String> returnedSerialNumbers) throws ProfitMandiBusinessException {
|
|
|
1041 |
LOGGER.info("Rollback payout called for offerId {}", offerId);
|
|
|
1042 |
float amountToRollback = 0;
|
|
|
1043 |
int totalImeis = 0;
|
|
|
1044 |
for (OfferPayout partnerPayout : partnerPayouts) {
|
|
|
1045 |
LOGGER.info("serialNumberInventoryItemMap - {}", returnedSerialNumbers);
|
|
|
1046 |
LOGGER.info("Serial Number - {}", partnerPayout.getSerialNumber());
|
|
|
1047 |
if (returnedSerialNumbers.contains(partnerPayout.getSerialNumber()) && partnerPayout.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
|
|
1048 |
partnerPayout.setRejectTimestamp(LocalDateTime.now());
|
|
|
1049 |
partnerPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1050 |
amountToRollback += partnerPayout.getAmount();
|
|
|
1051 |
totalImeis += 1;
|
|
|
1052 |
}
|
|
|
1053 |
}
|
|
|
1054 |
if (amountToRollback > 0) {
|
|
|
1055 |
LOGGER.info("Amount to Rollback - {}", amountToRollback);
|
|
|
1056 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, offerId, WalletReferenceType.ADDITIONAL_SCHEME, "Stock Returned, total " + totalImeis + "pc(s)", LocalDateTime.now());
|
|
|
1057 |
}
|
|
|
1058 |
}
|
|
|
1059 |
|
| 32064 |
amit.gupta |
1060 |
private Map<String, InventoryItem> getInventoryItemMap(int fofoId, Map<String, LineItem> serialNumberMap) {
|
|
|
1061 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectBySerialNumbers(serialNumberMap.keySet());
|
| 32345 |
amit.gupta |
1062 |
inventoryItems = inventoryItems.stream().filter(x -> x.getFofoId() == fofoId).collect(Collectors.toList());
|
|
|
1063 |
Map<String, InventoryItem> inventoryItemMap = inventoryItems.stream().collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x, (u, v) -> v.getCreateTimestamp().isAfter(u.getCreateTimestamp()) ? v : u));
|
| 32064 |
amit.gupta |
1064 |
return inventoryItemMap;
|
|
|
1065 |
}
|
| 27877 |
amit.gupta |
1066 |
}
|