| 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.FofoOrderRepository;
|
|
|
33 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 31205 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.fofo.OfferPayoutRepository;
|
| 31920 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
|
|
36 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
|
|
|
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;
|
| 27877 |
amit.gupta |
40 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 31205 |
amit.gupta |
41 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
42 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 30066 |
amit.gupta |
43 |
import org.apache.logging.log4j.LogManager;
|
|
|
44 |
import org.apache.logging.log4j.Logger;
|
|
|
45 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
46 |
import org.apache.poi.ss.usermodel.CellType;
|
|
|
47 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
48 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
|
|
49 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
50 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
51 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
52 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
53 |
import org.springframework.cache.CacheManager;
|
|
|
54 |
import org.springframework.cache.annotation.Cacheable;
|
|
|
55 |
import org.springframework.stereotype.Component;
|
| 27877 |
amit.gupta |
56 |
|
| 30066 |
amit.gupta |
57 |
import java.io.IOException;
|
|
|
58 |
import java.io.InputStream;
|
|
|
59 |
import java.time.LocalDate;
|
| 31205 |
amit.gupta |
60 |
import java.time.LocalDateTime;
|
| 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
|
|
|
98 |
CacheManager thirtyMinsTimeOutCacheManager;
|
| 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()));
|
|
|
140 |
offerRepository.persist(offer);
|
|
|
141 |
createOfferRequest.setId(offer.getId());
|
|
|
142 |
}
|
| 27877 |
amit.gupta |
143 |
|
| 31747 |
amit.gupta |
144 |
@Override
|
|
|
145 |
public List<CreateOfferRequest> getPublishedOffers(int fofoId, YearMonth yearMonth) {
|
|
|
146 |
List<Offer> publishedOffers = offerRepository.selectAllPublishedMapByPartner(yearMonth).get(fofoId);
|
|
|
147 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
148 |
if (publishedOffers != null) {
|
|
|
149 |
for (Offer offer : publishedOffers) {
|
|
|
150 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
151 |
createOfferRequests.add(createOfferRequest);
|
|
|
152 |
setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
| 27877 |
amit.gupta |
153 |
|
| 31747 |
amit.gupta |
154 |
}
|
|
|
155 |
}
|
|
|
156 |
return createOfferRequests;
|
|
|
157 |
}
|
| 27877 |
amit.gupta |
158 |
|
| 31747 |
amit.gupta |
159 |
private void setCriteriaPayoutAchieved(int fofoId, CreateOfferRequest createOfferRequest) {
|
|
|
160 |
Map<Integer, Long> criteriaTransactionMap = null;
|
|
|
161 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
|
|
|
162 |
criteriaTransactionMap = offerRepository.getPartnerWisePurchaseSum(createOfferRequest).get(fofoId);
|
|
|
163 |
} else {
|
|
|
164 |
criteriaTransactionMap = offerRepository.getPartnerWiseSalesSum(createOfferRequest).get(fofoId);
|
|
|
165 |
}
|
|
|
166 |
LOGGER.info("I am here ------- {}", createOfferRequest.getId());
|
|
|
167 |
List<com.spice.profitmandi.dao.model.ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest
|
|
|
168 |
.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
| 27877 |
amit.gupta |
169 |
|
| 31747 |
amit.gupta |
170 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
171 |
itemCriteriaPayout.setNextSlab(null);
|
|
|
172 |
LOGGER.info("I am here --- {}", itemCriteriaPayout.getPayoutSlabs());
|
|
|
173 |
long saleAmount = 0;
|
|
|
174 |
if (criteriaTransactionMap != null
|
|
|
175 |
&& criteriaTransactionMap.containsKey(itemCriteriaPayout.getItemCriteria().getId())) {
|
| 31853 |
amit.gupta |
176 |
saleAmount = criteriaTransactionMap.containsKey(0) ? criteriaTransactionMap.get(0) : criteriaTransactionMap.get(itemCriteriaPayout.getItemCriteria().getId());
|
| 31747 |
amit.gupta |
177 |
}
|
|
|
178 |
itemCriteriaPayout.setAchievedValue(saleAmount);
|
|
|
179 |
List<com.spice.profitmandi.service.offers.PayoutSlab> payoutSlabs = itemCriteriaPayout.getPayoutSlabs();
|
|
|
180 |
for (int i = 0; i < payoutSlabs.size(); i++) {
|
|
|
181 |
com.spice.profitmandi.service.offers.PayoutSlab beginSlab = payoutSlabs.get(i);
|
|
|
182 |
com.spice.profitmandi.service.offers.PayoutSlab endSlab = null;
|
|
|
183 |
if (payoutSlabs.size() - 1 > i) {
|
|
|
184 |
endSlab = payoutSlabs.get(i + 1);
|
|
|
185 |
}
|
|
|
186 |
if (beginSlab.getOnwardsAmount() > saleAmount) {
|
|
|
187 |
itemCriteriaPayout.setShortValue(beginSlab.getOnwardsAmount() - saleAmount);
|
|
|
188 |
itemCriteriaPayout.setNextSlab(beginSlab);
|
|
|
189 |
LOGGER.info("I am here");
|
|
|
190 |
break;
|
|
|
191 |
}
|
|
|
192 |
if (endSlab != null) {
|
|
|
193 |
if (beginSlab.getOnwardsAmount() <= saleAmount && endSlab.getOnwardsAmount() > saleAmount) {
|
|
|
194 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
195 |
beginSlab.setSelected(true);
|
|
|
196 |
itemCriteriaPayout.setNextSlab(endSlab);
|
|
|
197 |
itemCriteriaPayout.setShortValue(endSlab.getOnwardsAmount() - saleAmount);
|
|
|
198 |
LOGGER.info("I am here");
|
|
|
199 |
break;
|
|
|
200 |
}
|
|
|
201 |
// Last loop
|
|
|
202 |
} else {
|
|
|
203 |
beginSlab.setSelected(true);
|
|
|
204 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
205 |
LOGGER.info("I am here");
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
LOGGER.info("I am here --- {}", itemCriteriaPayout);
|
|
|
209 |
}
|
| 29783 |
amit.gupta |
210 |
|
| 31747 |
amit.gupta |
211 |
}
|
| 27877 |
amit.gupta |
212 |
|
| 31747 |
amit.gupta |
213 |
@Override
|
|
|
214 |
@Cacheable(value = "allOffers", cacheManager = "thirtyMinsTimeOutCacheManager")
|
|
|
215 |
public Map<Integer, CreateOfferRequest> getAllOffers(YearMonth yearMonth) {
|
|
|
216 |
List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
|
|
|
217 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
218 |
for (Offer offer : allOffers) {
|
|
|
219 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
220 |
createOfferRequests.add(createOfferRequest);
|
|
|
221 |
}
|
|
|
222 |
return createOfferRequests.stream().collect(Collectors.toMap(CreateOfferRequest::getId, x -> x));
|
|
|
223 |
}
|
| 27877 |
amit.gupta |
224 |
|
| 31747 |
amit.gupta |
225 |
@Override
|
|
|
226 |
@Cacheable(value = "partnerOffers", cacheManager = "thirtyMinsTimeOutCacheManager")
|
|
|
227 |
public CreateOfferRequest getOffer(int fofoId, int offerId) {
|
|
|
228 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
229 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
230 |
if (fofoId > 0) {
|
|
|
231 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
|
|
232 |
}
|
| 29783 |
amit.gupta |
233 |
|
| 31747 |
amit.gupta |
234 |
return createOfferRequest;
|
|
|
235 |
}
|
| 27877 |
amit.gupta |
236 |
|
| 31747 |
amit.gupta |
237 |
@Override
|
|
|
238 |
public CreateOfferRequest getCreateOfferRequest(Offer fromOffer) {
|
|
|
239 |
CreateOfferRequest createOfferRequest = new CreateOfferRequest();
|
|
|
240 |
createOfferRequest.setName(fromOffer.getName());
|
|
|
241 |
createOfferRequest.setTargetType(fromOffer.getTargetType());
|
|
|
242 |
createOfferRequest.setPayoutType(fromOffer.getPayoutType());
|
|
|
243 |
createOfferRequest.setSchemeType(fromOffer.getSchemeType());
|
|
|
244 |
createOfferRequest.setActivationBrands(fromOffer.getActivationBrands());
|
|
|
245 |
createOfferRequest.setEndDate(fromOffer.getEndDate());
|
|
|
246 |
createOfferRequest.setProcessTimestamp(fromOffer.getProcessedTimestamp());
|
|
|
247 |
createOfferRequest.setStartDate(fromOffer.getStartDate());
|
|
|
248 |
createOfferRequest.setBooster(fromOffer.isBooster());
|
|
|
249 |
createOfferRequest.setActive(fromOffer.isActive());
|
|
|
250 |
createOfferRequest.setSellinPercentage(fromOffer.getSellinPercentage());
|
|
|
251 |
createOfferRequest.setBrandShareTerms(fromOffer.getBrandSharePercentage());
|
|
|
252 |
createOfferRequest.setOfferNotes(fromOffer.getOfferNotes());
|
|
|
253 |
createOfferRequest.setTerms(fromOffer.getTerms());
|
|
|
254 |
createOfferRequest.setBaseCriteria(fromOffer.isBaseCriteia());
|
|
|
255 |
createOfferRequest.setItemCriteria(
|
|
|
256 |
gson.fromJson(fromOffer.getItemCriteria(), com.spice.profitmandi.service.offers.ItemCriteria.class));
|
|
|
257 |
createOfferRequest.setPartnerCriteria(gson.fromJson(fromOffer.getPartnerCriteria(), PartnerCriteria.class));
|
|
|
258 |
createOfferRequest.setId(fromOffer.getId());
|
|
|
259 |
createOfferRequest.setCreatedOn(fromOffer.getCreatedTimestamp());
|
|
|
260 |
createOfferRequest.setItemCriteriaString(
|
|
|
261 |
itemCriteriaRepository.getItemCriteriaDescription(createOfferRequest.getItemCriteria()));
|
|
|
262 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
|
|
263 |
.getByOfferId(fromOffer.getId());
|
|
|
264 |
createOfferRequest.setPartnerCriteriaString(
|
|
|
265 |
retailerService.getPartnerCriteriaString(createOfferRequest.getPartnerCriteria()));
|
|
|
266 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
|
|
267 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs()
|
|
|
268 |
.get(0).getItemCriteriaPayouts()) {
|
|
|
269 |
itemCriteriaPayout.setNextSlab(itemCriteriaPayout.getPayoutSlabs().get(0));
|
|
|
270 |
}
|
|
|
271 |
return createOfferRequest;
|
| 27877 |
amit.gupta |
272 |
|
| 31747 |
amit.gupta |
273 |
}
|
| 27877 |
amit.gupta |
274 |
|
| 31747 |
amit.gupta |
275 |
@Autowired
|
|
|
276 |
private WalletService walletService;
|
| 31205 |
amit.gupta |
277 |
|
| 31747 |
amit.gupta |
278 |
@Override
|
|
|
279 |
public void reverseAdditionalSelloutSchemes(int fofoId, List<InventoryItem> inventoryItems) throws ProfitMandiBusinessException {
|
|
|
280 |
List<OfferPayout> offerPayouts = new ArrayList<>();
|
| 31748 |
amit.gupta |
281 |
LOGGER.info("Reversing schemes for fofoId {}, InventoryItems {}", fofoId, inventoryItems);
|
| 31747 |
amit.gupta |
282 |
//As of now we are not handling non-serialised items
|
|
|
283 |
inventoryItems = inventoryItems.stream().filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
| 31750 |
amit.gupta |
284 |
if (inventoryItems.size() > 0) {
|
| 31205 |
amit.gupta |
285 |
|
| 31750 |
amit.gupta |
286 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
287 |
offerPayouts.addAll(offerPayoutRepository.selectAllBySerialNumber(inventoryItem.getFofoId(), inventoryItem.getSerialNumber())
|
|
|
288 |
.stream()
|
|
|
289 |
.filter(x -> x.getRejectTimestamp() == null)
|
|
|
290 |
.collect(Collectors.toList()));
|
| 31747 |
amit.gupta |
291 |
}
|
| 31750 |
amit.gupta |
292 |
for (OfferPayout offerPayout : offerPayouts) {
|
|
|
293 |
offerPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
294 |
offerPayout.setRejectTimestamp(LocalDateTime.now());
|
|
|
295 |
}
|
|
|
296 |
Map<Long, Double> offerPayoutAmountMap = offerPayouts.stream().collect(Collectors.groupingBy(x -> x.getOfferId(), Collectors.summingDouble(x -> x.getAmount())));
|
|
|
297 |
for (Map.Entry<Long, Double> offerPayoutEntry : offerPayoutAmountMap.entrySet()) {
|
|
|
298 |
List<UserWalletHistory> histories = walletService.getAllByReference(fofoId, offerPayoutEntry.getKey().intValue(), WalletReferenceType.ADDITIONAL_SCHEME);
|
|
|
299 |
if (histories.size() > 0) {
|
|
|
300 |
|
|
|
301 |
walletService.rollbackAmountFromWallet(fofoId, offerPayoutEntry.getValue().floatValue(), offerPayoutEntry.getKey().intValue(),
|
|
|
302 |
WalletReferenceType.ADDITIONAL_SCHEME, "Models/Imeis for corresponding schemes were returned", LocalDateTime.now());
|
|
|
303 |
}
|
|
|
304 |
}
|
| 31747 |
amit.gupta |
305 |
}
|
|
|
306 |
}
|
| 30684 |
amit.gupta |
307 |
|
| 31747 |
amit.gupta |
308 |
private void createTargetSlabs(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
309 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = createOfferRequest.getTargetSlabs().stream()
|
|
|
310 |
.filter(x -> x.validate()).collect(Collectors.toList());
|
|
|
311 |
if (targetSlabs.size() >= 0) {
|
|
|
312 |
Collections.sort(targetSlabs);
|
|
|
313 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
314 |
if (targetSlab.validate()) {
|
|
|
315 |
List<ItemCriteriaPayout> itemCriteriaPayouts = targetSlab.getItemCriteriaPayouts();
|
|
|
316 |
for (ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
317 |
ItemCriteria itemCriteria = itemCriteriaRepository.selectById(itemCriteriaPayout.getItemCriteria().getId());
|
|
|
318 |
if (itemCriteriaPayout.getItemCriteria().getId() > 0) {
|
|
|
319 |
itemCriteriaPayout.getItemCriteria().setId(0);
|
|
|
320 |
} else {
|
| 27877 |
amit.gupta |
321 |
|
| 31747 |
amit.gupta |
322 |
String itemCriteriaJson = gson.toJson(itemCriteriaPayout.getItemCriteria());
|
|
|
323 |
itemCriteria = itemCriteriaRepository.selectByCriteria(itemCriteriaJson);
|
|
|
324 |
// ItemCriteria Only once
|
|
|
325 |
if (itemCriteria == null) {
|
|
|
326 |
itemCriteria = new ItemCriteria();
|
|
|
327 |
itemCriteria.setCriteria(gson.toJson(itemCriteriaPayout.getItemCriteria()));
|
|
|
328 |
itemCriteriaRepository.persist(itemCriteria);
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
| 31903 |
amit.gupta |
332 |
TargetSlabEntity targetSlabEntity = new TargetSlabEntity();
|
|
|
333 |
targetSlabEntity.setOfferId(createOfferRequest.getId());
|
|
|
334 |
targetSlabEntity.setOnwardsTarget(targetSlab.getOnwardsAmount());
|
|
|
335 |
targetSlabEntity.setTargetDescription(targetSlab.getTargetDescription());
|
|
|
336 |
targetSlabEntity.setPayoutTarget(payoutSlab.getOnwardsAmount());
|
|
|
337 |
targetSlabEntity.setPayoutValue(payoutSlab.getPayoutAmount());
|
|
|
338 |
targetSlabEntity.setItemCriteriaId(itemCriteria.getId());
|
|
|
339 |
targetSlabEntity.setAmountType(itemCriteriaPayout.getAmountType());
|
|
|
340 |
offerTargetSlabRepository.persist(targetSlabEntity);
|
| 31747 |
amit.gupta |
341 |
}
|
|
|
342 |
}
|
|
|
343 |
}
|
|
|
344 |
}
|
| 27877 |
amit.gupta |
345 |
|
| 31747 |
amit.gupta |
346 |
} else {
|
|
|
347 |
throw new ProfitMandiBusinessException("Invalid Target Slabs", "Invalid Target", "");
|
|
|
348 |
}
|
| 27877 |
amit.gupta |
349 |
|
| 31747 |
amit.gupta |
350 |
}
|
| 27877 |
amit.gupta |
351 |
|
| 31747 |
amit.gupta |
352 |
@Override
|
|
|
353 |
public List<CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, int catalogId) {
|
|
|
354 |
List<Offer> publishedOffers = offerRepository.selectAllPublishedMapByPartner(YearMonth.from(date)).get(fofoId);
|
|
|
355 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
356 |
if (publishedOffers != null) {
|
|
|
357 |
for (Offer offer : publishedOffers) {
|
|
|
358 |
boolean found = false;
|
|
|
359 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
|
|
360 |
.getByOfferId(offer.getId());
|
|
|
361 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
362 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
|
|
363 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
|
|
364 |
Set<com.spice.profitmandi.service.offers.ItemCriteria> itemCriteriaSet = targetSlabs.stream()
|
|
|
365 |
.flatMap(x -> x.getItemCriteriaPayouts().stream()).map(x -> x.getItemCriteria()).distinct()
|
|
|
366 |
.filter(x -> itemRepository.containsModel(x, catalogId)).collect(Collectors.toSet());
|
| 29499 |
amit.gupta |
367 |
|
| 31747 |
amit.gupta |
368 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
369 |
targetSlab.setItemCriteriaPayouts(targetSlab.getItemCriteriaPayouts().stream()
|
|
|
370 |
.filter(x -> itemCriteriaSet.contains(x.getItemCriteria())).collect(Collectors.toList()));
|
|
|
371 |
if (targetSlab.getItemCriteriaPayouts().size() > 0) {
|
|
|
372 |
found = true;
|
|
|
373 |
}
|
|
|
374 |
}
|
|
|
375 |
if (found) {
|
|
|
376 |
createOfferRequests.add(createOfferRequest);
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
}
|
| 29783 |
amit.gupta |
380 |
|
| 31747 |
amit.gupta |
381 |
return createOfferRequests;
|
|
|
382 |
}
|
| 29783 |
amit.gupta |
383 |
|
| 31747 |
amit.gupta |
384 |
@Override
|
|
|
385 |
public void createOffers(InputStream fileInputStream) throws Exception {
|
|
|
386 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = this.parseFromExcel(fileInputStream);
|
|
|
387 |
for (Map.Entry<CreateOfferRequest, List<PartnerTargetModel>> partnerTargetEntry : offerPartnerTargetMap
|
|
|
388 |
.entrySet()) {
|
|
|
389 |
CreateOfferRequest createOfferRequest = partnerTargetEntry.getKey();
|
|
|
390 |
Map<List<Integer>, List<Integer>> targetPartnerMap = partnerTargetEntry.getValue().stream()
|
|
|
391 |
.collect(Collectors.groupingBy(PartnerTargetModel::getTargets,
|
|
|
392 |
Collectors.mapping(PartnerTargetModel::getFofoId, Collectors.toList())));
|
|
|
393 |
for (Map.Entry<List<Integer>, List<Integer>> targetPartnersEntry : targetPartnerMap.entrySet()) {
|
|
|
394 |
List<Integer> fofoIds = createOfferRequest.getPartnerCriteria().getFofoIds();
|
|
|
395 |
List<Integer> filterFofoIds = targetPartnersEntry.getValue().stream().filter(x -> !fofoIds.contains(x))
|
|
|
396 |
.collect(Collectors.toList());
|
|
|
397 |
LOGGER.info("FofoIds {}", filterFofoIds);
|
|
|
398 |
List<Integer> targets = targetPartnersEntry.getKey();
|
|
|
399 |
LOGGER.info("Targets {}", targets);
|
| 29806 |
amit.gupta |
400 |
|
| 31747 |
amit.gupta |
401 |
createOfferRequest.getPartnerCriteria().setFofoIds(filterFofoIds);
|
|
|
402 |
int counter = 0;
|
|
|
403 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0)
|
|
|
404 |
.getItemCriteriaPayouts();
|
|
|
405 |
List<PayoutSlab> payoutSlabs = itemCriteriaPayouts.stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.toList());
|
|
|
406 |
for (PayoutSlab payoutSlab : payoutSlabs) {
|
|
|
407 |
payoutSlab.setOnwardsAmount(targets.get(counter));
|
|
|
408 |
counter++;
|
|
|
409 |
}
|
|
|
410 |
this.addOfferService(createOfferRequest);
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());
|
| 27877 |
amit.gupta |
414 |
|
| 31747 |
amit.gupta |
415 |
}
|
| 29783 |
amit.gupta |
416 |
|
| 31747 |
amit.gupta |
417 |
private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream)
|
|
|
418 |
throws ProfitMandiBusinessException {
|
| 29783 |
amit.gupta |
419 |
|
| 31747 |
amit.gupta |
420 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = new HashMap<>();
|
|
|
421 |
XSSFWorkbook myWorkBook = null;
|
|
|
422 |
try {
|
|
|
423 |
myWorkBook = new XSSFWorkbook(inputStream);
|
|
|
424 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
425 |
for (int i = 0; i < myWorkBook.getNumberOfSheets(); i++) {
|
| 29683 |
amit.gupta |
426 |
|
| 31747 |
amit.gupta |
427 |
XSSFSheet mySheet = myWorkBook.getSheetAt(i);
|
|
|
428 |
int offerId = Integer.parseInt(mySheet.getSheetName());
|
|
|
429 |
CreateOfferRequest existingOffer = this.getOffer(0, offerId);
|
|
|
430 |
List<PartnerTargetModel> partnerTargetModels = new ArrayList<>();
|
|
|
431 |
offerPartnerTargetMap.put(existingOffer, partnerTargetModels);
|
|
|
432 |
this.printIterator(mySheet);
|
|
|
433 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
|
|
434 |
for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
|
|
|
435 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
436 |
LOGGER.info("row {}", row);
|
|
|
437 |
PartnerTargetModel partnerTargetModel = new PartnerTargetModel();
|
| 29783 |
amit.gupta |
438 |
|
| 31747 |
amit.gupta |
439 |
if (row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
440 |
partnerTargetModel.setFofoId((int) row.getCell(0).getNumericCellValue());
|
|
|
441 |
} else {
|
|
|
442 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
443 |
"FOFO ID", row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
|
|
444 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
445 |
throw profitMandiBusinessException;
|
|
|
446 |
}
|
|
|
447 |
List<Integer> targets = new ArrayList<>();
|
|
|
448 |
long targetsSize = existingOffer.getTargetSlabs().get(0).getItemCriteriaPayouts().stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.counting());
|
|
|
449 |
for (int cellNumber = 1; cellNumber <= targetsSize; cellNumber++) {
|
|
|
450 |
if (row.getCell(cellNumber) != null
|
|
|
451 |
&& row.getCell(cellNumber).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
452 |
targets.add((int) row.getCell(cellNumber).getNumericCellValue());
|
|
|
453 |
} else {
|
|
|
454 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
455 |
"Target Column issue at " + rowNumber + ", " + cellNumber, row.getCell(cellNumber),
|
|
|
456 |
"Invalid target");
|
|
|
457 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
458 |
throw profitMandiBusinessException;
|
|
|
459 |
}
|
|
|
460 |
}
|
|
|
461 |
partnerTargetModel.setTargets(targets);
|
|
|
462 |
partnerTargetModels.add(partnerTargetModel);
|
|
|
463 |
}
|
|
|
464 |
}
|
|
|
465 |
myWorkBook.close();
|
|
|
466 |
} catch (ProfitMandiBusinessException pbse) {
|
|
|
467 |
throw pbse;
|
|
|
468 |
} catch (IOException ioException) {
|
|
|
469 |
ioException.printStackTrace();
|
|
|
470 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(),
|
|
|
471 |
"EXL_VE_1000");
|
|
|
472 |
} finally {
|
|
|
473 |
if (myWorkBook != null) {
|
|
|
474 |
try {
|
|
|
475 |
myWorkBook.close();
|
|
|
476 |
} catch (IOException e) {
|
|
|
477 |
// TODO Auto-generated catch block
|
|
|
478 |
e.printStackTrace();
|
|
|
479 |
}
|
|
|
480 |
}
|
|
|
481 |
}
|
|
|
482 |
LOGGER.info("offerPartnerTargetMap {}", offerPartnerTargetMap);
|
|
|
483 |
return offerPartnerTargetMap;
|
|
|
484 |
}
|
| 29783 |
amit.gupta |
485 |
|
| 31747 |
amit.gupta |
486 |
private void printIterator(XSSFSheet sheet) {
|
|
|
487 |
Iterator<Row> rowIterator = sheet.iterator();
|
|
|
488 |
while (rowIterator.hasNext()) {
|
|
|
489 |
Row row = rowIterator.next();
|
|
|
490 |
// For each row, iterate through all the columns
|
|
|
491 |
Iterator<Cell> cellIterator = row.cellIterator();
|
| 29683 |
amit.gupta |
492 |
|
| 31747 |
amit.gupta |
493 |
while (cellIterator.hasNext()) {
|
|
|
494 |
Cell cell = cellIterator.next();
|
|
|
495 |
// Check the cell type and format accordingly
|
|
|
496 |
switch (cell.getCellType()) {
|
|
|
497 |
case Cell.CELL_TYPE_NUMERIC:
|
|
|
498 |
System.out.print(cell.getNumericCellValue() + "t");
|
|
|
499 |
break;
|
|
|
500 |
case Cell.CELL_TYPE_STRING:
|
|
|
501 |
System.out.print(cell.getStringCellValue() + "t");
|
|
|
502 |
break;
|
|
|
503 |
}
|
|
|
504 |
}
|
|
|
505 |
System.out.println("");
|
|
|
506 |
}
|
|
|
507 |
}
|
| 29683 |
amit.gupta |
508 |
|
| 31747 |
amit.gupta |
509 |
@Override
|
|
|
510 |
public Map<Integer, CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, String brand) {
|
|
|
511 |
// TODO Auto-generated method stub
|
|
|
512 |
return null;
|
|
|
513 |
}
|
| 30684 |
amit.gupta |
514 |
|
| 31747 |
amit.gupta |
515 |
@Override
|
|
|
516 |
@Cacheable(value = "slabPayoutMap", cacheManager = "oneDayCacheManager")
|
|
|
517 |
public Map<Integer, Map<Integer, Long>> getSlabPayoutMap(CreateOfferRequest createOfferRequest) {
|
|
|
518 |
Map<Integer, Map<Integer, Long>> catalogSlabPayoutMap = new HashMap<>();
|
|
|
519 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
|
|
520 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
| 29783 |
amit.gupta |
521 |
|
| 31747 |
amit.gupta |
522 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
523 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
|
|
524 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
|
|
525 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
526 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
|
|
527 |
for (Integer catalogId : catalogIds) {
|
|
|
528 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
529 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
530 |
}
|
|
|
531 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
532 |
(long) payoutSlab.getPayoutAmount());
|
|
|
533 |
}
|
|
|
534 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_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() / payoutSlab.getOnwardsAmount()));
|
|
|
541 |
}
|
|
|
542 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
543 |
Map<Integer, TagListing> tagListingsMap = tagListingRepository.selectAllByCatalogIds(catalogIds);
|
|
|
544 |
for (Map.Entry<Integer, TagListing> tagListingEntry : tagListingsMap.entrySet()) {
|
|
|
545 |
TagListing tagListing = tagListingEntry.getValue();
|
|
|
546 |
if (!catalogSlabPayoutMap.containsKey(tagListingEntry.getKey())) {
|
|
|
547 |
catalogSlabPayoutMap.put(tagListingEntry.getKey(), new LinkedHashMap<>());
|
|
|
548 |
}
|
|
|
549 |
catalogSlabPayoutMap.get(tagListingEntry.getKey()).put(payoutSlab.getOnwardsAmount(),
|
|
|
550 |
(long) (tagListing.getSellingPrice() * payoutSlab.getPayoutAmount() / 100));
|
|
|
551 |
}
|
|
|
552 |
}
|
|
|
553 |
}
|
|
|
554 |
}
|
|
|
555 |
return catalogSlabPayoutMap;
|
|
|
556 |
}
|
|
|
557 |
|
| 31903 |
amit.gupta |
558 |
@Override
|
|
|
559 |
@Cacheable(value = "slabPayoutMap", cacheManager = "oneDayCacheManager")
|
|
|
560 |
public Map<Integer, Map<Integer, AmountModel>> getSlabPayoutMapNew(CreateOfferRequest createOfferRequest) {
|
|
|
561 |
Map<Integer, Map<Integer, AmountModel>> catalogSlabPayoutMap = new HashMap<>();
|
|
|
562 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
|
|
563 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
|
|
564 |
|
|
|
565 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
566 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
|
|
567 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
|
|
568 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
569 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
|
|
570 |
for (Integer catalogId : catalogIds) {
|
|
|
571 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
572 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
573 |
}
|
|
|
574 |
AmountModel amountModel = new AmountModel();
|
|
|
575 |
amountModel.setAmountType(AmountType.FIXED);
|
|
|
576 |
amountModel.setAmount(payoutSlab.getPayoutAmount());
|
|
|
577 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
578 |
amountModel);
|
|
|
579 |
}
|
|
|
580 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_FIXED)) {
|
|
|
581 |
for (Integer catalogId : catalogIds) {
|
|
|
582 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
583 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
AmountModel amountModel = new AmountModel();
|
|
|
587 |
amountModel.setAmountType(AmountType.FIXED);
|
|
|
588 |
amountModel.setAmount(payoutSlab.getPayoutAmount() / payoutSlab.getOnwardsAmount());
|
|
|
589 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
590 |
amountModel);
|
|
|
591 |
}
|
|
|
592 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
593 |
for (Integer catalogId : catalogIds) {
|
|
|
594 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
595 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
596 |
}
|
|
|
597 |
AmountModel amountModel = new AmountModel();
|
|
|
598 |
amountModel.setAmountType(AmountType.PERCENTAGE);
|
|
|
599 |
amountModel.setAmount(payoutSlab.getPayoutAmount());
|
|
|
600 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
601 |
amountModel);
|
|
|
602 |
}
|
|
|
603 |
}
|
|
|
604 |
}
|
|
|
605 |
}
|
|
|
606 |
return catalogSlabPayoutMap;
|
|
|
607 |
}
|
|
|
608 |
|
| 31920 |
amit.gupta |
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 |
}
|
| 27877 |
amit.gupta |
963 |
}
|