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