| 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;
|
| 33999 |
tejus.loha |
9 |
import com.spice.profitmandi.common.util.FileUtil;
|
| 35205 |
amit |
10 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
| 31920 |
amit.gupta |
11 |
import com.spice.profitmandi.common.util.Utils;
|
| 27877 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
|
|
|
13 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
| 29783 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 31903 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.entity.catalog.TargetSlabEntity;
|
| 31920 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
|
|
17 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 31205 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.fofo.OfferPayout;
|
| 31920 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 31747 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| 31920 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.entity.warehouse.WarehouseInventoryItem;
|
|
|
24 |
import com.spice.profitmandi.dao.entity.warehouse.WarehouseScan;
|
|
|
25 |
import com.spice.profitmandi.dao.enumuration.catalog.AchievementType;
|
| 29783 |
amit.gupta |
26 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 29499 |
amit.gupta |
27 |
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
|
| 31205 |
amit.gupta |
28 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 35394 |
amit |
29 |
import com.spice.profitmandi.dao.model.*;
|
| 35856 |
amit |
30 |
import com.spice.profitmandi.dao.repository.GenericRepository;
|
| 30066 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
| 35205 |
amit |
32 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 31920 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
|
|
34 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 31205 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.fofo.OfferPayoutRepository;
|
| 31920 |
amit.gupta |
36 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 33087 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.repository.warehouse.AgeingSummaryModel;
|
| 31920 |
amit.gupta |
38 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
|
|
|
39 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseScanRepository;
|
| 35205 |
amit |
40 |
import com.spice.profitmandi.service.NotificationService;
|
| 31920 |
amit.gupta |
41 |
import com.spice.profitmandi.service.pricecircular.PriceCircularService;
|
|
|
42 |
import com.spice.profitmandi.service.scheme.InventoryPayoutModel;
|
| 27877 |
amit.gupta |
43 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 31205 |
amit.gupta |
44 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 35205 |
amit |
45 |
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
|
| 31205 |
amit.gupta |
46 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 33999 |
tejus.loha |
47 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 30066 |
amit.gupta |
48 |
import org.apache.logging.log4j.LogManager;
|
|
|
49 |
import org.apache.logging.log4j.Logger;
|
|
|
50 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
51 |
import org.apache.poi.ss.usermodel.CellType;
|
|
|
52 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
53 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
|
|
54 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
55 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
56 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
57 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
58 |
import org.springframework.cache.CacheManager;
|
|
|
59 |
import org.springframework.cache.annotation.Cacheable;
|
| 36305 |
amit |
60 |
import org.springframework.transaction.annotation.Transactional;
|
| 30066 |
amit.gupta |
61 |
import org.springframework.stereotype.Component;
|
| 27877 |
amit.gupta |
62 |
|
| 30066 |
amit.gupta |
63 |
import java.io.IOException;
|
|
|
64 |
import java.io.InputStream;
|
|
|
65 |
import java.time.LocalDate;
|
| 31205 |
amit.gupta |
66 |
import java.time.LocalDateTime;
|
| 30066 |
amit.gupta |
67 |
import java.time.YearMonth;
|
|
|
68 |
import java.util.*;
|
|
|
69 |
import java.util.stream.Collectors;
|
|
|
70 |
|
| 27877 |
amit.gupta |
71 |
@Component
|
|
|
72 |
public class OfferServiceImpl implements OfferService {
|
|
|
73 |
|
| 35394 |
amit |
74 |
private static final Logger LOGGER = LogManager.getLogger(OfferServiceImpl.class);
|
| 29499 |
amit.gupta |
75 |
|
| 35394 |
amit |
76 |
// Constants
|
|
|
77 |
private static final List<String> TARGETS = Arrays.asList("Target 1", "Target 2", "Target 3", "Target 4", "Target 5");
|
|
|
78 |
private static final int STOCK_AGEING_THRESHOLD_DAYS = 300;
|
|
|
79 |
|
|
|
80 |
// Dependencies - Core
|
| 31747 |
amit.gupta |
81 |
@Autowired
|
|
|
82 |
private Gson gson;
|
| 27877 |
amit.gupta |
83 |
|
| 31747 |
amit.gupta |
84 |
@Autowired
|
| 36049 |
amit |
85 |
private OfferService offerServiceProxy; // self-injection for Spring cache proxy
|
| 35205 |
amit |
86 |
|
| 36049 |
amit |
87 |
@Autowired
|
|
|
88 |
private CacheManager redisCacheManager;
|
|
|
89 |
|
|
|
90 |
@Autowired
|
|
|
91 |
private CacheManager redisShortCacheManager;
|
|
|
92 |
|
| 35394 |
amit |
93 |
// Dependencies - Repositories
|
| 35205 |
amit |
94 |
@Autowired
|
| 35856 |
amit |
95 |
private GenericRepository genericRepository;
|
|
|
96 |
|
|
|
97 |
@Autowired
|
| 31747 |
amit.gupta |
98 |
private OfferRepository offerRepository;
|
| 27877 |
amit.gupta |
99 |
|
| 31747 |
amit.gupta |
100 |
@Autowired
|
| 35394 |
amit |
101 |
private OfferPayoutRepository offerPayoutRepository;
|
| 27877 |
amit.gupta |
102 |
|
| 31747 |
amit.gupta |
103 |
@Autowired
|
| 35394 |
amit |
104 |
private OfferTargetSlabRepository offerTargetSlabRepository;
|
| 31205 |
amit.gupta |
105 |
|
| 31747 |
amit.gupta |
106 |
@Autowired
|
|
|
107 |
private ItemCriteriaRepository itemCriteriaRepository;
|
| 27877 |
amit.gupta |
108 |
|
| 31747 |
amit.gupta |
109 |
@Autowired
|
|
|
110 |
private ItemRepository itemRepository;
|
| 27877 |
amit.gupta |
111 |
|
| 31747 |
amit.gupta |
112 |
@Autowired
|
|
|
113 |
private TagListingRepository tagListingRepository;
|
| 29783 |
amit.gupta |
114 |
|
| 31747 |
amit.gupta |
115 |
@Autowired
|
| 35205 |
amit |
116 |
private FofoStoreRepository fofoStoreRepository;
|
| 30684 |
amit.gupta |
117 |
|
| 31747 |
amit.gupta |
118 |
@Autowired
|
| 35394 |
amit |
119 |
private ActivatedImeiRepository activatedImeiRepository;
|
| 27877 |
amit.gupta |
120 |
|
| 31920 |
amit.gupta |
121 |
@Autowired
|
|
|
122 |
private WarehouseScanRepository warehouseScanRepository;
|
|
|
123 |
|
|
|
124 |
@Autowired
|
|
|
125 |
private WarehouseInventoryItemRepository warehouseInventoryItemRepository;
|
|
|
126 |
|
|
|
127 |
@Autowired
|
|
|
128 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 35394 |
amit |
129 |
|
| 31920 |
amit.gupta |
130 |
@Autowired
|
|
|
131 |
private InventoryItemRepository inventoryItemRepository;
|
|
|
132 |
|
| 35394 |
amit |
133 |
@Autowired
|
|
|
134 |
private OfferEligibleImeiRepository offerEligibleImeiRepository;
|
|
|
135 |
|
|
|
136 |
// Dependencies - Services
|
|
|
137 |
@Autowired
|
|
|
138 |
private NotificationService notificationService;
|
|
|
139 |
|
|
|
140 |
@Autowired
|
|
|
141 |
private RetailerService retailerService;
|
|
|
142 |
|
|
|
143 |
@Autowired
|
|
|
144 |
private PriceCircularService priceCircularService;
|
|
|
145 |
|
|
|
146 |
@Autowired
|
|
|
147 |
private WalletService walletService;
|
|
|
148 |
|
| 31747 |
amit.gupta |
149 |
@Override
|
| 36049 |
amit |
150 |
public void evictOfferCaches(int offerId) {
|
|
|
151 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
152 |
YearMonth ym = YearMonth.from(offer.getStartDate());
|
|
|
153 |
redisCacheManager.getCache("offer.definition").evict(offerId);
|
|
|
154 |
redisCacheManager.getCache("allOffers").evict(ym);
|
|
|
155 |
redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
|
|
|
156 |
redisCacheManager.getCache("offer.slabpayout").evict(offerId);
|
|
|
157 |
redisCacheManager.getCache("offer.achievement").clear();
|
|
|
158 |
redisShortCacheManager.getCache("todayOffers").clear();
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
@Override
|
| 31747 |
amit.gupta |
162 |
public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
163 |
LOGGER.info("createOfferRequest -- {}", createOfferRequest);
|
|
|
164 |
this.createOffer(createOfferRequest);
|
|
|
165 |
this.createTargetSlabs(createOfferRequest);
|
|
|
166 |
}
|
| 27877 |
amit.gupta |
167 |
|
| 31747 |
amit.gupta |
168 |
private void createOffer(CreateOfferRequest createOfferRequest) {
|
|
|
169 |
Offer offer = new Offer();
|
|
|
170 |
offer.setName(createOfferRequest.getName());
|
|
|
171 |
offer.setTargetType(createOfferRequest.getTargetType());
|
|
|
172 |
offer.setBooster(createOfferRequest.isBooster());
|
|
|
173 |
offer.setPayoutType(createOfferRequest.getPayoutType());
|
|
|
174 |
offer.setSchemeType(createOfferRequest.getSchemeType());
|
|
|
175 |
offer.setBaseCriteia(createOfferRequest.isBaseCriteria());
|
|
|
176 |
offer.setEndDate(createOfferRequest.getEndDate());
|
|
|
177 |
offer.setStartDate(createOfferRequest.getStartDate());
|
|
|
178 |
offer.setBrandSharePercentage(createOfferRequest.getBrandShareTerms());
|
|
|
179 |
offer.setSellinPercentage(createOfferRequest.getSellinPercentage());
|
|
|
180 |
offer.setOfferNotes(createOfferRequest.getOfferNotes());
|
|
|
181 |
offer.setActivationBrands(createOfferRequest.getActivationBrands());
|
|
|
182 |
offer.setTerms(createOfferRequest.getTerms());
|
|
|
183 |
offer.setItemCriteria(gson.toJson(createOfferRequest.getItemCriteria()));
|
|
|
184 |
offer.setPartnerCriteria(gson.toJson(createOfferRequest.getPartnerCriteria()));
|
| 32070 |
amit.gupta |
185 |
offer.setDiscount(createOfferRequest.isDiscount());
|
| 31747 |
amit.gupta |
186 |
offerRepository.persist(offer);
|
|
|
187 |
createOfferRequest.setId(offer.getId());
|
|
|
188 |
}
|
| 27877 |
amit.gupta |
189 |
|
| 31747 |
amit.gupta |
190 |
@Override
|
| 33247 |
ranu |
191 |
public List<CreateOfferRequest> getPublishedOffers(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
|
| 36049 |
amit |
192 |
// Past months (>1 month ago): achievement is frozen, cache the full result
|
|
|
193 |
if (yearMonth.isBefore(YearMonth.now().minusMonths(1))) {
|
|
|
194 |
return offerServiceProxy.getPublishedOffersCached(fofoId, yearMonth);
|
|
|
195 |
}
|
|
|
196 |
// Current/recent month: assemble live from cached building blocks
|
|
|
197 |
return assemblePublishedOffers(fofoId, yearMonth);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
@Cacheable(value = "publishedOffersWithAchievement.past", cacheManager = "redisCacheManager")
|
|
|
201 |
public List<CreateOfferRequest> getPublishedOffersCached(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
|
|
|
202 |
return assemblePublishedOffers(fofoId, yearMonth);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
private List<CreateOfferRequest> assemblePublishedOffers(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
|
| 35394 |
amit |
206 |
Map<Integer, List<Offer>> publishedMapByPartner = offerRepository.selectAllPublishedMapByPartner(yearMonth);
|
|
|
207 |
List<Offer> publishedOffers = publishedMapByPartner != null ? publishedMapByPartner.get(fofoId) : null;
|
| 31747 |
amit.gupta |
208 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
209 |
if (publishedOffers != null) {
|
|
|
210 |
for (Offer offer : publishedOffers) {
|
| 36049 |
amit |
211 |
CreateOfferRequest copy = deepCopy(this.getCreateOfferRequest(offer));
|
|
|
212 |
setCriteriaPayoutAchieved(fofoId, copy);
|
|
|
213 |
createOfferRequests.add(copy);
|
| 31747 |
amit.gupta |
214 |
}
|
|
|
215 |
}
|
|
|
216 |
return createOfferRequests;
|
|
|
217 |
}
|
| 27877 |
amit.gupta |
218 |
|
| 31747 |
amit.gupta |
219 |
private void setCriteriaPayoutAchieved(int fofoId, CreateOfferRequest createOfferRequest) {
|
| 36049 |
amit |
220 |
Map<Integer, Long> criteriaTransactionMap;
|
| 31747 |
amit.gupta |
221 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
|
| 36049 |
amit |
222 |
criteriaTransactionMap = offerRepository.getPurchaseSumForPartner(
|
|
|
223 |
createOfferRequest.getId(), fofoId, createOfferRequest);
|
| 31747 |
amit.gupta |
224 |
} else {
|
| 36049 |
amit |
225 |
criteriaTransactionMap = offerRepository.getSalesSumForPartner(
|
|
|
226 |
createOfferRequest.getId(), fofoId, createOfferRequest);
|
| 31747 |
amit.gupta |
227 |
}
|
|
|
228 |
List<com.spice.profitmandi.dao.model.ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest
|
|
|
229 |
.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
| 27877 |
amit.gupta |
230 |
|
| 31747 |
amit.gupta |
231 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
232 |
itemCriteriaPayout.setNextSlab(null);
|
|
|
233 |
long saleAmount = 0;
|
|
|
234 |
if (criteriaTransactionMap != null
|
|
|
235 |
&& criteriaTransactionMap.containsKey(itemCriteriaPayout.getItemCriteria().getId())) {
|
| 31853 |
amit.gupta |
236 |
saleAmount = criteriaTransactionMap.containsKey(0) ? criteriaTransactionMap.get(0) : criteriaTransactionMap.get(itemCriteriaPayout.getItemCriteria().getId());
|
| 31747 |
amit.gupta |
237 |
}
|
|
|
238 |
itemCriteriaPayout.setAchievedValue(saleAmount);
|
|
|
239 |
List<com.spice.profitmandi.service.offers.PayoutSlab> payoutSlabs = itemCriteriaPayout.getPayoutSlabs();
|
|
|
240 |
for (int i = 0; i < payoutSlabs.size(); i++) {
|
|
|
241 |
com.spice.profitmandi.service.offers.PayoutSlab beginSlab = payoutSlabs.get(i);
|
|
|
242 |
com.spice.profitmandi.service.offers.PayoutSlab endSlab = null;
|
|
|
243 |
if (payoutSlabs.size() - 1 > i) {
|
|
|
244 |
endSlab = payoutSlabs.get(i + 1);
|
|
|
245 |
}
|
|
|
246 |
if (beginSlab.getOnwardsAmount() > saleAmount) {
|
|
|
247 |
itemCriteriaPayout.setShortValue(beginSlab.getOnwardsAmount() - saleAmount);
|
|
|
248 |
itemCriteriaPayout.setNextSlab(beginSlab);
|
|
|
249 |
break;
|
|
|
250 |
}
|
|
|
251 |
if (endSlab != null) {
|
|
|
252 |
if (beginSlab.getOnwardsAmount() <= saleAmount && endSlab.getOnwardsAmount() > saleAmount) {
|
|
|
253 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
254 |
beginSlab.setSelected(true);
|
|
|
255 |
itemCriteriaPayout.setNextSlab(endSlab);
|
|
|
256 |
itemCriteriaPayout.setShortValue(endSlab.getOnwardsAmount() - saleAmount);
|
|
|
257 |
break;
|
|
|
258 |
}
|
|
|
259 |
} else {
|
|
|
260 |
beginSlab.setSelected(true);
|
|
|
261 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
}
|
| 27877 |
amit.gupta |
266 |
|
| 31747 |
amit.gupta |
267 |
@Override
|
| 36049 |
amit |
268 |
@Cacheable(value = "allOffers", cacheManager = "redisCacheManager")
|
| 33247 |
ranu |
269 |
public Map<Integer, CreateOfferRequest> getAllOffers(YearMonth yearMonth) throws ProfitMandiBusinessException {
|
| 31747 |
amit.gupta |
270 |
List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
|
|
|
271 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
272 |
for (Offer offer : allOffers) {
|
|
|
273 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
274 |
createOfferRequests.add(createOfferRequest);
|
|
|
275 |
}
|
|
|
276 |
return createOfferRequests.stream().collect(Collectors.toMap(CreateOfferRequest::getId, x -> x));
|
|
|
277 |
}
|
| 27877 |
amit.gupta |
278 |
|
| 36049 |
amit |
279 |
private CreateOfferRequest deepCopy(CreateOfferRequest source) {
|
|
|
280 |
return gson.fromJson(gson.toJson(source), CreateOfferRequest.class);
|
|
|
281 |
}
|
|
|
282 |
|
| 31747 |
amit.gupta |
283 |
@Override
|
| 36049 |
amit |
284 |
@Cacheable(value = "offer.definition", cacheManager = "redisCacheManager", key = "#offerId")
|
|
|
285 |
public CreateOfferRequest getOfferDefinition(int offerId) throws ProfitMandiBusinessException {
|
|
|
286 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
287 |
return this.getCreateOfferRequest(offer);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
@Override
|
| 33247 |
ranu |
291 |
public CreateOfferRequest getOffer(int fofoId, int offerId) throws ProfitMandiBusinessException {
|
| 36049 |
amit |
292 |
CreateOfferRequest definition = offerServiceProxy.getOfferDefinition(offerId);
|
| 31747 |
amit.gupta |
293 |
if (fofoId > 0) {
|
| 36049 |
amit |
294 |
CreateOfferRequest copy = deepCopy(definition);
|
|
|
295 |
copy.setCriteriaQtyAmountModel(this.getCriteriaQtyAmounModelMap(copy, fofoId));
|
|
|
296 |
return copy;
|
| 31747 |
amit.gupta |
297 |
}
|
| 36049 |
amit |
298 |
return definition;
|
| 31747 |
amit.gupta |
299 |
}
|
| 27877 |
amit.gupta |
300 |
|
| 31747 |
amit.gupta |
301 |
@Override
|
| 33247 |
ranu |
302 |
public CreateOfferRequest getCreateOfferRequest(Offer fromOffer) throws ProfitMandiBusinessException {
|
| 31747 |
amit.gupta |
303 |
CreateOfferRequest createOfferRequest = new CreateOfferRequest();
|
|
|
304 |
createOfferRequest.setName(fromOffer.getName());
|
| 32023 |
amit.gupta |
305 |
createOfferRequest.setDiscount(fromOffer.isDiscount());
|
| 31747 |
amit.gupta |
306 |
createOfferRequest.setTargetType(fromOffer.getTargetType());
|
|
|
307 |
createOfferRequest.setPayoutType(fromOffer.getPayoutType());
|
|
|
308 |
createOfferRequest.setSchemeType(fromOffer.getSchemeType());
|
|
|
309 |
createOfferRequest.setActivationBrands(fromOffer.getActivationBrands());
|
|
|
310 |
createOfferRequest.setEndDate(fromOffer.getEndDate());
|
|
|
311 |
createOfferRequest.setProcessTimestamp(fromOffer.getProcessedTimestamp());
|
|
|
312 |
createOfferRequest.setStartDate(fromOffer.getStartDate());
|
|
|
313 |
createOfferRequest.setBooster(fromOffer.isBooster());
|
|
|
314 |
createOfferRequest.setActive(fromOffer.isActive());
|
|
|
315 |
createOfferRequest.setSellinPercentage(fromOffer.getSellinPercentage());
|
|
|
316 |
createOfferRequest.setBrandShareTerms(fromOffer.getBrandSharePercentage());
|
|
|
317 |
createOfferRequest.setOfferNotes(fromOffer.getOfferNotes());
|
|
|
318 |
createOfferRequest.setTerms(fromOffer.getTerms());
|
|
|
319 |
createOfferRequest.setBaseCriteria(fromOffer.isBaseCriteia());
|
|
|
320 |
createOfferRequest.setItemCriteria(
|
|
|
321 |
gson.fromJson(fromOffer.getItemCriteria(), com.spice.profitmandi.service.offers.ItemCriteria.class));
|
|
|
322 |
createOfferRequest.setPartnerCriteria(gson.fromJson(fromOffer.getPartnerCriteria(), PartnerCriteria.class));
|
|
|
323 |
createOfferRequest.setId(fromOffer.getId());
|
|
|
324 |
createOfferRequest.setCreatedOn(fromOffer.getCreatedTimestamp());
|
|
|
325 |
createOfferRequest.setItemCriteriaString(
|
|
|
326 |
itemCriteriaRepository.getItemCriteriaDescription(createOfferRequest.getItemCriteria()));
|
|
|
327 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
|
|
328 |
.getByOfferId(fromOffer.getId());
|
|
|
329 |
createOfferRequest.setPartnerCriteriaString(
|
| 33030 |
amit.gupta |
330 |
this.getPartnerCriteriaString(createOfferRequest.getPartnerCriteria()));
|
| 31747 |
amit.gupta |
331 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
| 35885 |
amit |
332 |
if (!targetSlabs.isEmpty() && !targetSlabs.get(0).getItemCriteriaPayouts().isEmpty()) {
|
|
|
333 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : targetSlabs.get(0).getItemCriteriaPayouts()) {
|
|
|
334 |
if (!itemCriteriaPayout.getPayoutSlabs().isEmpty()) {
|
|
|
335 |
itemCriteriaPayout.setNextSlab(itemCriteriaPayout.getPayoutSlabs().get(0));
|
|
|
336 |
}
|
|
|
337 |
}
|
| 31747 |
amit.gupta |
338 |
}
|
|
|
339 |
return createOfferRequest;
|
|
|
340 |
}
|
| 27877 |
amit.gupta |
341 |
|
| 31747 |
amit.gupta |
342 |
@Override
|
| 33613 |
amit.gupta |
343 |
public void reverseAdditionalSelloutSchemes(List<InventoryItem> inventoryItems, String reversalReason) throws ProfitMandiBusinessException {
|
|
|
344 |
Map<Integer, List<InventoryItem>> fofoInventoryItemsMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
|
|
|
345 |
for (Map.Entry<Integer, List<InventoryItem>> fofoInventoryItemsEntry : fofoInventoryItemsMap.entrySet()) {
|
|
|
346 |
int fofoId = fofoInventoryItemsEntry.getKey();
|
|
|
347 |
List<InventoryItem> partnerInventoryItems = fofoInventoryItemsEntry.getValue();
|
|
|
348 |
List<OfferPayout> offerPayouts = new ArrayList<>();
|
|
|
349 |
LOGGER.info("Reversing schemes for fofoId {}, InventoryItems {}", fofoId, inventoryItems);
|
|
|
350 |
//As of now we are not handling non-serialised items
|
| 35394 |
amit |
351 |
List<InventoryItem> serializedItems = partnerInventoryItems.stream().filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
|
|
352 |
if (serializedItems.size() > 0) {
|
|
|
353 |
// Batch fetch: collect all serial numbers and fetch in one call per fofoId
|
|
|
354 |
Set<String> serialNumbers = serializedItems.stream().map(InventoryItem::getSerialNumber).collect(Collectors.toSet());
|
|
|
355 |
offerPayouts = offerPayoutRepository.selectAllBySerialNumbers(fofoId, serialNumbers)
|
|
|
356 |
.stream()
|
|
|
357 |
.filter(x -> x.getRejectTimestamp() == null)
|
|
|
358 |
.collect(Collectors.toList());
|
| 33613 |
amit.gupta |
359 |
for (OfferPayout offerPayout : offerPayouts) {
|
|
|
360 |
offerPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
361 |
offerPayout.setRejectTimestamp(LocalDateTime.now());
|
|
|
362 |
}
|
|
|
363 |
Map<Long, Double> offerPayoutAmountMap = offerPayouts.stream().collect(Collectors.groupingBy(x -> x.getOfferId(), Collectors.summingDouble(x -> x.getAmount())));
|
|
|
364 |
for (Map.Entry<Long, Double> offerPayoutEntry : offerPayoutAmountMap.entrySet()) {
|
|
|
365 |
List<UserWalletHistory> histories = walletService.getAllByReference(fofoId, offerPayoutEntry.getKey().intValue(), WalletReferenceType.ADDITIONAL_SCHEME);
|
|
|
366 |
if (histories.size() > 0) {
|
| 31750 |
amit.gupta |
367 |
|
| 33613 |
amit.gupta |
368 |
walletService.rollbackAmountFromWallet(fofoId, offerPayoutEntry.getValue().floatValue(), offerPayoutEntry.getKey().intValue(),
|
|
|
369 |
WalletReferenceType.ADDITIONAL_SCHEME, reversalReason, LocalDateTime.now());
|
|
|
370 |
}
|
| 31750 |
amit.gupta |
371 |
}
|
|
|
372 |
}
|
| 31747 |
amit.gupta |
373 |
}
|
|
|
374 |
}
|
| 30684 |
amit.gupta |
375 |
|
| 36103 |
amit |
376 |
@Override
|
|
|
377 |
public void restoreAdditionalSelloutSchemes(List<InventoryItem> inventoryItems, String restoreReason)
|
|
|
378 |
throws ProfitMandiBusinessException {
|
|
|
379 |
Map<Integer, List<InventoryItem>> fofoInventoryItemsMap = inventoryItems.stream()
|
|
|
380 |
.collect(Collectors.groupingBy(InventoryItem::getFofoId));
|
|
|
381 |
for (Map.Entry<Integer, List<InventoryItem>> entry : fofoInventoryItemsMap.entrySet()) {
|
|
|
382 |
int fofoId = entry.getKey();
|
|
|
383 |
List<InventoryItem> partnerItems = entry.getValue();
|
|
|
384 |
|
|
|
385 |
List<InventoryItem> serializedItems = partnerItems.stream()
|
|
|
386 |
.filter(x -> x.getSerialNumber() != null)
|
|
|
387 |
.collect(Collectors.toList());
|
|
|
388 |
if (!serializedItems.isEmpty()) {
|
|
|
389 |
Set<String> serialNumbers = serializedItems.stream()
|
|
|
390 |
.map(InventoryItem::getSerialNumber).collect(Collectors.toSet());
|
|
|
391 |
List<OfferPayout> offerPayouts = offerPayoutRepository
|
|
|
392 |
.selectAllBySerialNumbers(fofoId, serialNumbers).stream()
|
|
|
393 |
.filter(x -> x.getRejectTimestamp() != null
|
|
|
394 |
&& SchemePayoutStatus.REJECTED.equals(x.getStatus()))
|
|
|
395 |
.collect(Collectors.toList());
|
|
|
396 |
|
|
|
397 |
for (OfferPayout offerPayout : offerPayouts) {
|
|
|
398 |
offerPayout.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
399 |
offerPayout.setRejectTimestamp(null);
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
Map<Long, Double> offerPayoutAmountMap = offerPayouts.stream()
|
|
|
403 |
.collect(Collectors.groupingBy(OfferPayout::getOfferId,
|
|
|
404 |
Collectors.summingDouble(OfferPayout::getAmount)));
|
|
|
405 |
for (Map.Entry<Long, Double> offerEntry : offerPayoutAmountMap.entrySet()) {
|
|
|
406 |
if (offerEntry.getValue() > 0) {
|
|
|
407 |
walletService.addAmountToWallet(fofoId, offerEntry.getKey().intValue(),
|
|
|
408 |
WalletReferenceType.ADDITIONAL_SCHEME, restoreReason,
|
|
|
409 |
offerEntry.getValue().floatValue(), LocalDateTime.now());
|
|
|
410 |
}
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
}
|
|
|
414 |
}
|
|
|
415 |
|
| 31747 |
amit.gupta |
416 |
private void createTargetSlabs(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
417 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = createOfferRequest.getTargetSlabs().stream()
|
|
|
418 |
.filter(x -> x.validate()).collect(Collectors.toList());
|
| 35394 |
amit |
419 |
if (!targetSlabs.isEmpty()) {
|
| 31747 |
amit.gupta |
420 |
Collections.sort(targetSlabs);
|
|
|
421 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
422 |
if (targetSlab.validate()) {
|
|
|
423 |
List<ItemCriteriaPayout> itemCriteriaPayouts = targetSlab.getItemCriteriaPayouts();
|
|
|
424 |
for (ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
425 |
ItemCriteria itemCriteria = itemCriteriaRepository.selectById(itemCriteriaPayout.getItemCriteria().getId());
|
|
|
426 |
if (itemCriteriaPayout.getItemCriteria().getId() > 0) {
|
|
|
427 |
itemCriteriaPayout.getItemCriteria().setId(0);
|
|
|
428 |
} else {
|
|
|
429 |
String itemCriteriaJson = gson.toJson(itemCriteriaPayout.getItemCriteria());
|
|
|
430 |
itemCriteria = itemCriteriaRepository.selectByCriteria(itemCriteriaJson);
|
|
|
431 |
// ItemCriteria Only once
|
|
|
432 |
if (itemCriteria == null) {
|
|
|
433 |
itemCriteria = new ItemCriteria();
|
|
|
434 |
itemCriteria.setCriteria(gson.toJson(itemCriteriaPayout.getItemCriteria()));
|
|
|
435 |
itemCriteriaRepository.persist(itemCriteria);
|
|
|
436 |
}
|
|
|
437 |
}
|
|
|
438 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
| 31903 |
amit.gupta |
439 |
TargetSlabEntity targetSlabEntity = new TargetSlabEntity();
|
|
|
440 |
targetSlabEntity.setOfferId(createOfferRequest.getId());
|
|
|
441 |
targetSlabEntity.setOnwardsTarget(targetSlab.getOnwardsAmount());
|
|
|
442 |
targetSlabEntity.setTargetDescription(targetSlab.getTargetDescription());
|
|
|
443 |
targetSlabEntity.setPayoutTarget(payoutSlab.getOnwardsAmount());
|
|
|
444 |
targetSlabEntity.setPayoutValue(payoutSlab.getPayoutAmount());
|
|
|
445 |
targetSlabEntity.setItemCriteriaId(itemCriteria.getId());
|
|
|
446 |
targetSlabEntity.setAmountType(itemCriteriaPayout.getAmountType());
|
| 32979 |
amit.gupta |
447 |
targetSlabEntity.setStartDate(itemCriteriaPayout.getStartDate());
|
|
|
448 |
targetSlabEntity.setEndDate(itemCriteriaPayout.getEndDate());
|
| 31903 |
amit.gupta |
449 |
offerTargetSlabRepository.persist(targetSlabEntity);
|
| 31747 |
amit.gupta |
450 |
}
|
|
|
451 |
}
|
|
|
452 |
}
|
|
|
453 |
}
|
| 27877 |
amit.gupta |
454 |
|
| 31747 |
amit.gupta |
455 |
} else {
|
|
|
456 |
throw new ProfitMandiBusinessException("Invalid Target Slabs", "Invalid Target", "");
|
|
|
457 |
}
|
| 27877 |
amit.gupta |
458 |
|
| 31747 |
amit.gupta |
459 |
}
|
| 27877 |
amit.gupta |
460 |
|
| 31747 |
amit.gupta |
461 |
@Override
|
| 33247 |
ranu |
462 |
public List<CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, int catalogId) throws ProfitMandiBusinessException {
|
| 35394 |
amit |
463 |
Map<Integer, List<Offer>> publishedMapByPartner = offerRepository.selectAllPublishedMapByPartner(YearMonth.from(date));
|
|
|
464 |
List<Offer> publishedOffers = publishedMapByPartner != null ? publishedMapByPartner.get(fofoId) : null;
|
| 31747 |
amit.gupta |
465 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
466 |
if (publishedOffers != null) {
|
|
|
467 |
for (Offer offer : publishedOffers) {
|
|
|
468 |
boolean found = false;
|
|
|
469 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
|
|
470 |
.getByOfferId(offer.getId());
|
|
|
471 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
472 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
|
|
473 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
|
|
474 |
Set<com.spice.profitmandi.service.offers.ItemCriteria> itemCriteriaSet = targetSlabs.stream()
|
|
|
475 |
.flatMap(x -> x.getItemCriteriaPayouts().stream()).map(x -> x.getItemCriteria()).distinct()
|
|
|
476 |
.filter(x -> itemRepository.containsModel(x, catalogId)).collect(Collectors.toSet());
|
| 29499 |
amit.gupta |
477 |
|
| 31747 |
amit.gupta |
478 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
479 |
targetSlab.setItemCriteriaPayouts(targetSlab.getItemCriteriaPayouts().stream()
|
|
|
480 |
.filter(x -> itemCriteriaSet.contains(x.getItemCriteria())).collect(Collectors.toList()));
|
|
|
481 |
if (targetSlab.getItemCriteriaPayouts().size() > 0) {
|
|
|
482 |
found = true;
|
|
|
483 |
}
|
|
|
484 |
}
|
|
|
485 |
if (found) {
|
|
|
486 |
createOfferRequests.add(createOfferRequest);
|
|
|
487 |
}
|
|
|
488 |
}
|
|
|
489 |
}
|
| 29783 |
amit.gupta |
490 |
|
| 31747 |
amit.gupta |
491 |
return createOfferRequests;
|
|
|
492 |
}
|
| 29783 |
amit.gupta |
493 |
|
| 31747 |
amit.gupta |
494 |
@Override
|
|
|
495 |
public void createOffers(InputStream fileInputStream) throws Exception {
|
|
|
496 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = this.parseFromExcel(fileInputStream);
|
| 32868 |
amit.gupta |
497 |
Set<YearMonth> ymListToEvict = new HashSet<>();
|
| 31747 |
amit.gupta |
498 |
for (Map.Entry<CreateOfferRequest, List<PartnerTargetModel>> partnerTargetEntry : offerPartnerTargetMap
|
|
|
499 |
.entrySet()) {
|
|
|
500 |
CreateOfferRequest createOfferRequest = partnerTargetEntry.getKey();
|
|
|
501 |
Map<List<Integer>, List<Integer>> targetPartnerMap = partnerTargetEntry.getValue().stream()
|
|
|
502 |
.collect(Collectors.groupingBy(PartnerTargetModel::getTargets,
|
|
|
503 |
Collectors.mapping(PartnerTargetModel::getFofoId, Collectors.toList())));
|
|
|
504 |
for (Map.Entry<List<Integer>, List<Integer>> targetPartnersEntry : targetPartnerMap.entrySet()) {
|
|
|
505 |
List<Integer> fofoIds = createOfferRequest.getPartnerCriteria().getFofoIds();
|
|
|
506 |
List<Integer> filterFofoIds = targetPartnersEntry.getValue().stream().filter(x -> !fofoIds.contains(x))
|
|
|
507 |
.collect(Collectors.toList());
|
|
|
508 |
LOGGER.info("FofoIds {}", filterFofoIds);
|
|
|
509 |
List<Integer> targets = targetPartnersEntry.getKey();
|
|
|
510 |
LOGGER.info("Targets {}", targets);
|
| 29806 |
amit.gupta |
511 |
|
| 31747 |
amit.gupta |
512 |
createOfferRequest.getPartnerCriteria().setFofoIds(filterFofoIds);
|
|
|
513 |
int counter = 0;
|
|
|
514 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0)
|
|
|
515 |
.getItemCriteriaPayouts();
|
|
|
516 |
List<PayoutSlab> payoutSlabs = itemCriteriaPayouts.stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.toList());
|
|
|
517 |
for (PayoutSlab payoutSlab : payoutSlabs) {
|
|
|
518 |
payoutSlab.setOnwardsAmount(targets.get(counter));
|
|
|
519 |
counter++;
|
|
|
520 |
}
|
|
|
521 |
this.addOfferService(createOfferRequest);
|
|
|
522 |
}
|
| 32868 |
amit.gupta |
523 |
ymListToEvict.add(YearMonth.from(createOfferRequest.getStartDate()));
|
| 31747 |
amit.gupta |
524 |
}
|
| 32868 |
amit.gupta |
525 |
for (YearMonth ymToEvict : ymListToEvict) {
|
| 36049 |
amit |
526 |
redisCacheManager.getCache("allOffers").evict(ymToEvict);
|
| 32868 |
amit.gupta |
527 |
}
|
| 31747 |
amit.gupta |
528 |
}
|
| 29783 |
amit.gupta |
529 |
|
| 31747 |
amit.gupta |
530 |
private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream)
|
|
|
531 |
throws ProfitMandiBusinessException {
|
| 29783 |
amit.gupta |
532 |
|
| 31747 |
amit.gupta |
533 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = new HashMap<>();
|
|
|
534 |
XSSFWorkbook myWorkBook = null;
|
|
|
535 |
try {
|
|
|
536 |
myWorkBook = new XSSFWorkbook(inputStream);
|
|
|
537 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
538 |
for (int i = 0; i < myWorkBook.getNumberOfSheets(); i++) {
|
| 29683 |
amit.gupta |
539 |
|
| 31747 |
amit.gupta |
540 |
XSSFSheet mySheet = myWorkBook.getSheetAt(i);
|
|
|
541 |
int offerId = Integer.parseInt(mySheet.getSheetName());
|
|
|
542 |
CreateOfferRequest existingOffer = this.getOffer(0, offerId);
|
|
|
543 |
List<PartnerTargetModel> partnerTargetModels = new ArrayList<>();
|
|
|
544 |
offerPartnerTargetMap.put(existingOffer, partnerTargetModels);
|
|
|
545 |
this.printIterator(mySheet);
|
|
|
546 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
|
|
547 |
for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
|
|
|
548 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
549 |
LOGGER.info("row {}", row);
|
|
|
550 |
PartnerTargetModel partnerTargetModel = new PartnerTargetModel();
|
| 29783 |
amit.gupta |
551 |
|
| 31747 |
amit.gupta |
552 |
if (row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
553 |
partnerTargetModel.setFofoId((int) row.getCell(0).getNumericCellValue());
|
|
|
554 |
} else {
|
|
|
555 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
556 |
"FOFO ID", row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
|
|
557 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
558 |
throw profitMandiBusinessException;
|
|
|
559 |
}
|
|
|
560 |
List<Integer> targets = new ArrayList<>();
|
|
|
561 |
long targetsSize = existingOffer.getTargetSlabs().get(0).getItemCriteriaPayouts().stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.counting());
|
|
|
562 |
for (int cellNumber = 1; cellNumber <= targetsSize; cellNumber++) {
|
|
|
563 |
if (row.getCell(cellNumber) != null
|
|
|
564 |
&& row.getCell(cellNumber).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
565 |
targets.add((int) row.getCell(cellNumber).getNumericCellValue());
|
|
|
566 |
} else {
|
|
|
567 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
568 |
"Target Column issue at " + rowNumber + ", " + cellNumber, row.getCell(cellNumber),
|
|
|
569 |
"Invalid target");
|
|
|
570 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
571 |
throw profitMandiBusinessException;
|
|
|
572 |
}
|
|
|
573 |
}
|
|
|
574 |
partnerTargetModel.setTargets(targets);
|
|
|
575 |
partnerTargetModels.add(partnerTargetModel);
|
|
|
576 |
}
|
|
|
577 |
}
|
|
|
578 |
myWorkBook.close();
|
|
|
579 |
} catch (ProfitMandiBusinessException pbse) {
|
|
|
580 |
throw pbse;
|
|
|
581 |
} catch (IOException ioException) {
|
|
|
582 |
ioException.printStackTrace();
|
|
|
583 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(),
|
|
|
584 |
"EXL_VE_1000");
|
|
|
585 |
} finally {
|
|
|
586 |
if (myWorkBook != null) {
|
|
|
587 |
try {
|
|
|
588 |
myWorkBook.close();
|
|
|
589 |
} catch (IOException e) {
|
|
|
590 |
// TODO Auto-generated catch block
|
|
|
591 |
e.printStackTrace();
|
|
|
592 |
}
|
|
|
593 |
}
|
|
|
594 |
}
|
|
|
595 |
LOGGER.info("offerPartnerTargetMap {}", offerPartnerTargetMap);
|
|
|
596 |
return offerPartnerTargetMap;
|
|
|
597 |
}
|
| 29783 |
amit.gupta |
598 |
|
| 31747 |
amit.gupta |
599 |
private void printIterator(XSSFSheet sheet) {
|
|
|
600 |
Iterator<Row> rowIterator = sheet.iterator();
|
|
|
601 |
while (rowIterator.hasNext()) {
|
|
|
602 |
Row row = rowIterator.next();
|
| 35394 |
amit |
603 |
StringBuilder rowContent = new StringBuilder();
|
| 31747 |
amit.gupta |
604 |
Iterator<Cell> cellIterator = row.cellIterator();
|
| 29683 |
amit.gupta |
605 |
|
| 31747 |
amit.gupta |
606 |
while (cellIterator.hasNext()) {
|
|
|
607 |
Cell cell = cellIterator.next();
|
|
|
608 |
switch (cell.getCellType()) {
|
|
|
609 |
case Cell.CELL_TYPE_NUMERIC:
|
| 35394 |
amit |
610 |
rowContent.append(cell.getNumericCellValue()).append("\t");
|
| 31747 |
amit.gupta |
611 |
break;
|
|
|
612 |
case Cell.CELL_TYPE_STRING:
|
| 35394 |
amit |
613 |
rowContent.append(cell.getStringCellValue()).append("\t");
|
| 31747 |
amit.gupta |
614 |
break;
|
|
|
615 |
}
|
|
|
616 |
}
|
| 35394 |
amit |
617 |
LOGGER.debug("Row content: {}", rowContent);
|
| 31747 |
amit.gupta |
618 |
}
|
|
|
619 |
}
|
| 29683 |
amit.gupta |
620 |
|
| 31747 |
amit.gupta |
621 |
@Override
|
|
|
622 |
public Map<Integer, CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, String brand) {
|
|
|
623 |
// TODO Auto-generated method stub
|
|
|
624 |
return null;
|
|
|
625 |
}
|
| 30684 |
amit.gupta |
626 |
|
| 31747 |
amit.gupta |
627 |
@Override
|
| 36374 |
ranu |
628 |
public List<CreateOfferRequest> getActiveOffers(int fofoId) throws ProfitMandiBusinessException {
|
|
|
629 |
List<Offer> activeOffers = offerRepository.selectCurrentlyActiveForPartner(fofoId);
|
|
|
630 |
List<CreateOfferRequest> result = new ArrayList<>();
|
|
|
631 |
|
|
|
632 |
for (Offer offer : activeOffers) {
|
|
|
633 |
CreateOfferRequest copy = deepCopy(this.getCreateOfferRequest(offer));
|
|
|
634 |
setCriteriaPayoutAchieved(fofoId, copy);
|
|
|
635 |
result.add(copy);
|
|
|
636 |
}
|
|
|
637 |
|
|
|
638 |
return result;
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
@Override
|
| 32043 |
amit.gupta |
642 |
@Cacheable(value = "slabPayoutMap1", cacheManager = "oneDayCacheManager")
|
| 33247 |
ranu |
643 |
public Map<Integer, Map<Integer, Long>> getSlabPayoutMap(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
| 31747 |
amit.gupta |
644 |
Map<Integer, Map<Integer, Long>> catalogSlabPayoutMap = new HashMap<>();
|
|
|
645 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
|
|
646 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
| 29783 |
amit.gupta |
647 |
|
| 31747 |
amit.gupta |
648 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
649 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
|
|
650 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
|
|
651 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
652 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
|
|
653 |
for (Integer catalogId : catalogIds) {
|
|
|
654 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
655 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
656 |
}
|
|
|
657 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
658 |
(long) payoutSlab.getPayoutAmount());
|
|
|
659 |
}
|
|
|
660 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_FIXED)) {
|
|
|
661 |
for (Integer catalogId : catalogIds) {
|
|
|
662 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
663 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
664 |
}
|
|
|
665 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
666 |
(long) (payoutSlab.getPayoutAmount() / payoutSlab.getOnwardsAmount()));
|
|
|
667 |
}
|
|
|
668 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
669 |
Map<Integer, TagListing> tagListingsMap = tagListingRepository.selectAllByCatalogIds(catalogIds);
|
|
|
670 |
for (Map.Entry<Integer, TagListing> tagListingEntry : tagListingsMap.entrySet()) {
|
|
|
671 |
TagListing tagListing = tagListingEntry.getValue();
|
|
|
672 |
if (!catalogSlabPayoutMap.containsKey(tagListingEntry.getKey())) {
|
|
|
673 |
catalogSlabPayoutMap.put(tagListingEntry.getKey(), new LinkedHashMap<>());
|
|
|
674 |
}
|
|
|
675 |
catalogSlabPayoutMap.get(tagListingEntry.getKey()).put(payoutSlab.getOnwardsAmount(),
|
|
|
676 |
(long) (tagListing.getSellingPrice() * payoutSlab.getPayoutAmount() / 100));
|
|
|
677 |
}
|
|
|
678 |
}
|
|
|
679 |
}
|
|
|
680 |
}
|
|
|
681 |
return catalogSlabPayoutMap;
|
|
|
682 |
}
|
|
|
683 |
|
| 31903 |
amit.gupta |
684 |
@Override
|
| 36049 |
amit |
685 |
@Cacheable(value = "offer.slabpayout", cacheManager = "redisCacheManager", key = "#createOfferRequest.id")
|
| 31903 |
amit.gupta |
686 |
public Map<Integer, Map<Integer, AmountModel>> getSlabPayoutMapNew(CreateOfferRequest createOfferRequest) {
|
|
|
687 |
Map<Integer, Map<Integer, AmountModel>> catalogSlabPayoutMap = new HashMap<>();
|
|
|
688 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
|
|
689 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
|
|
690 |
|
|
|
691 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
692 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
|
|
693 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
|
|
694 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
695 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
|
|
696 |
for (Integer catalogId : catalogIds) {
|
|
|
697 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
698 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
699 |
}
|
|
|
700 |
AmountModel amountModel = new AmountModel();
|
|
|
701 |
amountModel.setAmountType(AmountType.FIXED);
|
|
|
702 |
amountModel.setAmount(payoutSlab.getPayoutAmount());
|
| 32021 |
amit.gupta |
703 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31903 |
amit.gupta |
704 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
705 |
amountModel);
|
|
|
706 |
}
|
|
|
707 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_FIXED)) {
|
|
|
708 |
for (Integer catalogId : catalogIds) {
|
|
|
709 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
710 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
711 |
}
|
|
|
712 |
|
|
|
713 |
AmountModel amountModel = new AmountModel();
|
|
|
714 |
amountModel.setAmountType(AmountType.FIXED);
|
| 32021 |
amit.gupta |
715 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31903 |
amit.gupta |
716 |
amountModel.setAmount(payoutSlab.getPayoutAmount() / payoutSlab.getOnwardsAmount());
|
|
|
717 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
718 |
amountModel);
|
|
|
719 |
}
|
|
|
720 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
721 |
for (Integer catalogId : catalogIds) {
|
|
|
722 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
723 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
|
|
724 |
}
|
|
|
725 |
AmountModel amountModel = new AmountModel();
|
|
|
726 |
amountModel.setAmountType(AmountType.PERCENTAGE);
|
|
|
727 |
amountModel.setAmount(payoutSlab.getPayoutAmount());
|
| 32021 |
amit.gupta |
728 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 31903 |
amit.gupta |
729 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
|
|
730 |
amountModel);
|
|
|
731 |
}
|
|
|
732 |
}
|
|
|
733 |
}
|
|
|
734 |
}
|
|
|
735 |
return catalogSlabPayoutMap;
|
|
|
736 |
}
|
|
|
737 |
|
| 31920 |
amit.gupta |
738 |
|
|
|
739 |
private double getAmount(InventoryPayoutModel inventoryPayoutModel, AmountModel amountModel) {
|
|
|
740 |
double rollout = 0;
|
|
|
741 |
if (amountModel.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
742 |
double effectiveDp = inventoryPayoutModel.getDp() - inventoryPayoutModel.getFixedAmount();
|
|
|
743 |
double totalPercentage = inventoryPayoutModel.getPercentageAmount() + amountModel.getAmount();
|
|
|
744 |
rollout = effectiveDp * (totalPercentage / (100 + totalPercentage) - (inventoryPayoutModel.getPercentageAmount() / (100 + inventoryPayoutModel.getPercentageAmount())));
|
|
|
745 |
} else {
|
|
|
746 |
rollout = amountModel.getAmount() * (100 / (100 + inventoryPayoutModel.getPercentageAmount()));
|
|
|
747 |
}
|
|
|
748 |
return rollout;
|
|
|
749 |
}
|
|
|
750 |
|
| 35394 |
amit |
751 |
@Override
|
|
|
752 |
public Map<Integer, QtyAmountModel> getCriteriaQtyAmounModelMap(CreateOfferRequest createOfferRequest, int fofoId) throws ProfitMandiBusinessException {
|
|
|
753 |
Map<Integer, QtyAmountModel> qtyAmountMap;
|
|
|
754 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
|
|
|
755 |
qtyAmountMap = getCriteriaQtyAmountModelMapForSellin(createOfferRequest, fofoId);
|
|
|
756 |
} else {
|
|
|
757 |
qtyAmountMap = getCriteriaQtyAmountModelMapForSelloutAndActivation(createOfferRequest, fofoId);
|
|
|
758 |
}
|
|
|
759 |
LOGGER.info("Qty Amount Map {}", qtyAmountMap);
|
|
|
760 |
return qtyAmountMap;
|
|
|
761 |
}
|
| 34217 |
amit.gupta |
762 |
|
| 35667 |
amit |
763 |
private PayoutSlab findEligibleSlab(List<PayoutSlab> slabs, int achievedValue) {
|
|
|
764 |
PayoutSlab eligibleSlab = null;
|
|
|
765 |
for (PayoutSlab slab : slabs) {
|
|
|
766 |
if (slab.getOnwardsAmount() <= achievedValue) {
|
|
|
767 |
eligibleSlab = slab;
|
|
|
768 |
} else {
|
|
|
769 |
break;
|
|
|
770 |
}
|
|
|
771 |
}
|
|
|
772 |
return eligibleSlab;
|
|
|
773 |
}
|
|
|
774 |
|
| 35675 |
amit |
775 |
private void setSlabProgress(ItemCriteriaPayout itemCriteriaPayout, PayoutSlab eligibleSlab, int achievedValue) {
|
|
|
776 |
List<PayoutSlab> slabs = itemCriteriaPayout.getPayoutSlabs();
|
|
|
777 |
itemCriteriaPayout.setCurrentSlab(eligibleSlab);
|
|
|
778 |
if (eligibleSlab == null) {
|
|
|
779 |
if (!slabs.isEmpty()) {
|
|
|
780 |
itemCriteriaPayout.setNextSlab(slabs.get(0));
|
|
|
781 |
itemCriteriaPayout.setShortValue(slabs.get(0).getOnwardsAmount() - achievedValue);
|
|
|
782 |
}
|
|
|
783 |
} else {
|
|
|
784 |
int idx = slabs.indexOf(eligibleSlab);
|
|
|
785 |
if (idx < slabs.size() - 1) {
|
|
|
786 |
PayoutSlab next = slabs.get(idx + 1);
|
|
|
787 |
itemCriteriaPayout.setNextSlab(next);
|
|
|
788 |
itemCriteriaPayout.setShortValue(next.getOnwardsAmount() - achievedValue);
|
|
|
789 |
} else {
|
|
|
790 |
itemCriteriaPayout.setNextSlab(null);
|
|
|
791 |
itemCriteriaPayout.setShortValue(0);
|
|
|
792 |
}
|
|
|
793 |
}
|
|
|
794 |
}
|
|
|
795 |
|
| 35667 |
amit |
796 |
private double calculateFinalPayout(PayoutSlab eligibleSlab, AmountType amountType,
|
|
|
797 |
int valueForPercentage, int valueForFixed) {
|
|
|
798 |
if (eligibleSlab == null) return 0;
|
|
|
799 |
eligibleSlab.setSelected(true);
|
|
|
800 |
float payoutRate = eligibleSlab.getPayoutAmount();
|
|
|
801 |
if (AmountType.PERCENTAGE.equals(amountType)) {
|
|
|
802 |
return (payoutRate * valueForPercentage) / 100;
|
|
|
803 |
} else if (AmountType.FIXED.equals(amountType)) {
|
|
|
804 |
return payoutRate * valueForFixed;
|
|
|
805 |
} else {
|
|
|
806 |
return payoutRate;
|
|
|
807 |
}
|
|
|
808 |
}
|
|
|
809 |
|
|
|
810 |
private QtyAmountModel calculateSellinQtyAndValue(List<Order> orders) {
|
|
|
811 |
int qty = orders.stream().collect(Collectors.summingInt(x ->
|
|
|
812 |
x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()));
|
|
|
813 |
int value = orders.stream().collect(Collectors.summingInt(x ->
|
|
|
814 |
Math.round(x.getLineItem().getUnitPrice() *
|
|
|
815 |
(x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))));
|
|
|
816 |
return new QtyAmountModel(qty, value);
|
|
|
817 |
}
|
|
|
818 |
|
| 35394 |
amit |
819 |
private Map<Integer, QtyAmountModel> getCriteriaQtyAmountModelMapForSellin(CreateOfferRequest createOfferRequest, int fofoId) throws ProfitMandiBusinessException {
|
|
|
820 |
Map<Integer, QtyAmountModel> criteriaQtyAmountMap = new HashMap<>();
|
|
|
821 |
|
|
|
822 |
Map<Integer, List<Order>> criteriaOrdersMap = offerRepository.getPartnerWiseSellin(fofoId, createOfferRequest, true);
|
|
|
823 |
|
| 35667 |
amit |
824 |
List<Order> baseCriteriaOrders = criteriaOrdersMap.get(0);
|
| 35394 |
amit |
825 |
Integer userBaseQty = null;
|
|
|
826 |
Integer userBaseValue = null;
|
| 35667 |
amit |
827 |
if (baseCriteriaOrders != null && baseCriteriaOrders.size() > 0) {
|
|
|
828 |
QtyAmountModel baseModel = calculateSellinQtyAndValue(baseCriteriaOrders);
|
|
|
829 |
userBaseQty = baseModel.getQty();
|
|
|
830 |
userBaseValue = (int) baseModel.getAmount();
|
|
|
831 |
criteriaQtyAmountMap.put(0, baseModel);
|
|
|
832 |
} else {
|
|
|
833 |
criteriaQtyAmountMap.put(0, new QtyAmountModel(0, 0));
|
| 35394 |
amit |
834 |
}
|
|
|
835 |
|
|
|
836 |
LOGGER.info("Processing sellin");
|
|
|
837 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
838 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
839 |
|
|
|
840 |
QtyAmountModel qtyAmountModel = new QtyAmountModel(0, 0);
|
| 35667 |
amit |
841 |
criteriaQtyAmountMap.put(criteriaId, qtyAmountModel);
|
| 35394 |
amit |
842 |
|
|
|
843 |
List<Order> orders = criteriaOrdersMap.get(criteriaId);
|
| 35667 |
amit |
844 |
if (orders == null || orders.isEmpty()) continue;
|
|
|
845 |
|
|
|
846 |
QtyAmountModel criteriaModel = calculateSellinQtyAndValue(orders);
|
| 36051 |
amit |
847 |
int criteriaQty = criteriaModel.getQty();
|
|
|
848 |
int totalBaseQty = criteriaQty;
|
| 35667 |
amit |
849 |
int totalBaseValue = (int) criteriaModel.getAmount();
|
| 35394 |
amit |
850 |
if (userBaseQty != null) {
|
|
|
851 |
totalBaseQty = userBaseQty;
|
|
|
852 |
totalBaseValue = userBaseValue;
|
|
|
853 |
}
|
|
|
854 |
|
|
|
855 |
int purchasedValue;
|
|
|
856 |
if (createOfferRequest.getTargetType().equals(AchievementType.VALUE)) {
|
|
|
857 |
purchasedValue = totalBaseValue;
|
|
|
858 |
} else {
|
|
|
859 |
purchasedValue = totalBaseQty;
|
|
|
860 |
}
|
|
|
861 |
qtyAmountModel.setQty(totalBaseQty);
|
|
|
862 |
qtyAmountModel.setAmount(totalBaseValue);
|
|
|
863 |
|
| 35667 |
amit |
864 |
PayoutSlab eligibleSlab = findEligibleSlab(itemCriteriaPayout.getPayoutSlabs(), purchasedValue);
|
| 35675 |
amit |
865 |
setSlabProgress(itemCriteriaPayout, eligibleSlab, purchasedValue);
|
| 36051 |
amit |
866 |
// For FIXED (per pc) payout, use per-criteria qty not base value
|
|
|
867 |
int sellinValueForFixed = itemCriteriaPayout.getAmountType().equals(AmountType.FIXED) ? criteriaQty : purchasedValue;
|
| 35667 |
amit |
868 |
double finalPayout = calculateFinalPayout(eligibleSlab, itemCriteriaPayout.getAmountType(),
|
| 36051 |
amit |
869 |
purchasedValue, sellinValueForFixed);
|
| 35394 |
amit |
870 |
qtyAmountModel.setFinalPayout(finalPayout);
|
|
|
871 |
}
|
|
|
872 |
return criteriaQtyAmountMap;
|
|
|
873 |
}
|
|
|
874 |
|
|
|
875 |
private Map<Integer, QtyAmountModel> getCriteriaQtyAmountModelMapForSelloutAndActivation(CreateOfferRequest createOfferRequest, int fofoId) {
|
|
|
876 |
Map<Integer, Map<Integer, List<FofoOrderItem>>> criteriaPartnerwiseTertiary = offerRepository.getCriteriaWisePartnerTertiary(createOfferRequest, fofoId);
|
|
|
877 |
Map<Integer, QtyAmountModel> qtyAmountMap;
|
|
|
878 |
final Map<String, LocalDateTime> activatedImeisActivationDateMap;
|
|
|
879 |
qtyAmountMap = new HashMap<>();
|
|
|
880 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
881 |
//All Serialnumbers for all eligible retailers
|
|
|
882 |
List<String> serialNumbers = criteriaPartnerwiseTertiary.entrySet().stream().flatMap(x -> x.getValue().entrySet().stream().flatMap(foiList -> foiList.getValue().stream()))
|
|
|
883 |
.flatMap(y -> y.getFofoLineItems().stream()).filter(fli -> fli.getSerialNumber() != null)
|
|
|
884 |
.map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
885 |
if (serialNumbers.size() > 0) {
|
|
|
886 |
/*if (imeisAllowedManually != null) {
|
|
|
887 |
serialNumbers = serialNumbers.stream().filter(x -> imeisAllowedManually.contains(x)).collect(Collectors.toList());
|
|
|
888 |
}*/
|
|
|
889 |
activatedImeisActivationDateMap = activatedImeiRepository.selectBySerialNumbers(serialNumbers).stream()
|
|
|
890 |
.filter(x -> createOfferRequest.isWithinRange(x.getActivationTimestamp()))
|
|
|
891 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x.getActivationTimestamp()));
|
|
|
892 |
} else {
|
|
|
893 |
activatedImeisActivationDateMap = null;
|
|
|
894 |
}
|
|
|
895 |
} else {
|
|
|
896 |
activatedImeisActivationDateMap = null;
|
|
|
897 |
}
|
|
|
898 |
|
|
|
899 |
QtyAmountModel qtyAmountModel = null;
|
|
|
900 |
if (createOfferRequest.isBaseCriteria()) {
|
| 35667 |
amit |
901 |
Map<Integer, List<FofoOrderItem>> baseCriteriaMap = criteriaPartnerwiseTertiary.get(0);
|
|
|
902 |
List<FofoOrderItem> fofoOrderItems = baseCriteriaMap == null ? null : baseCriteriaMap.get(fofoId);
|
| 35394 |
amit |
903 |
int totalBaseQty = fofoOrderItems == null ? 0 : fofoOrderItems.stream().filter(x->createOfferRequest.isWithinRange(x.getCreateTimestamp())).collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
904 |
int totalBaseValue = fofoOrderItems == null ? 0 : fofoOrderItems.stream().filter(x->createOfferRequest.isWithinRange(x.getCreateTimestamp())).mapToInt(x -> (Math.round(x.getDp())) * x.getQuantity()).sum();
|
|
|
905 |
qtyAmountModel = new QtyAmountModel(totalBaseQty, totalBaseValue);
|
|
|
906 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
907 |
Map<Integer, Integer> inventoryItemValueMap = new HashMap<>();
|
| 35667 |
amit |
908 |
if (fofoOrderItems != null) {
|
|
|
909 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
910 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
911 |
lineItem -> {
|
|
|
912 |
if (activatedImeisActivationDateMap == null || activatedImeisActivationDateMap.containsKey(lineItem.getSerialNumber())) {
|
|
|
913 |
inventoryItemValueMap.put(lineItem.getInventoryItemId(), (int) fofoOrderItem.getDp());
|
|
|
914 |
}
|
| 35394 |
amit |
915 |
}
|
| 35667 |
amit |
916 |
);
|
|
|
917 |
}
|
| 35394 |
amit |
918 |
}
|
|
|
919 |
qtyAmountModel.setActivationQty(inventoryItemValueMap.size());
|
|
|
920 |
qtyAmountModel.setActivationAmount(inventoryItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue())));
|
|
|
921 |
qtyAmountMap.put(0, qtyAmountModel);
|
|
|
922 |
}
|
|
|
923 |
}
|
|
|
924 |
|
|
|
925 |
//CriteriaPayoutWise
|
|
|
926 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
927 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
928 |
Map<Integer, List<FofoOrderItem>> ordersMap = criteriaPartnerwiseTertiary.get(criteriaId);
|
|
|
929 |
|
| 35667 |
amit |
930 |
QtyAmountModel criteriaQtyAmountModel = new QtyAmountModel(0, 0);
|
|
|
931 |
qtyAmountMap.put(criteriaId, criteriaQtyAmountModel);
|
| 35394 |
amit |
932 |
|
| 35667 |
amit |
933 |
if (ordersMap == null) continue;
|
| 35394 |
amit |
934 |
|
| 35667 |
amit |
935 |
List<FofoOrderItem> fofoOrderItems = ordersMap.get(fofoId);
|
|
|
936 |
if (fofoOrderItems == null || fofoOrderItems.isEmpty()) continue;
|
| 35394 |
amit |
937 |
|
| 35673 |
amit |
938 |
int totalQty = fofoOrderItems.stream().filter(x -> createOfferRequest.isWithinRange(x.getCreateTimestamp())).collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
939 |
int totalValue = fofoOrderItems.stream().filter(x -> createOfferRequest.isWithinRange(x.getCreateTimestamp())).collect(Collectors.summingInt(x -> Math.round(x.getDp()) * (x.getQuantity())));
|
| 35667 |
amit |
940 |
int eligibleQty = totalQty;
|
|
|
941 |
int eligibleValue = totalValue;
|
|
|
942 |
criteriaQtyAmountModel.setQty(totalQty);
|
|
|
943 |
criteriaQtyAmountModel.setAmount(totalValue);
|
| 35394 |
amit |
944 |
|
| 35667 |
amit |
945 |
Map<FofoLineItem, Integer> lineItemValueMap = new HashMap<>();
|
|
|
946 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
947 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
948 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
949 |
lineItem -> {
|
|
|
950 |
if (activatedImeisActivationDateMap == null) {
|
|
|
951 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
|
|
952 |
} else if (activatedImeisActivationDateMap.containsKey(lineItem.getSerialNumber())) {
|
|
|
953 |
if (itemCriteriaPayout.isWithinRange(activatedImeisActivationDateMap.get(lineItem.getSerialNumber()))) {
|
|
|
954 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
|
|
955 |
}
|
|
|
956 |
}
|
|
|
957 |
}
|
|
|
958 |
);
|
| 35394 |
amit |
959 |
}
|
| 35667 |
amit |
960 |
eligibleQty = lineItemValueMap.size();
|
|
|
961 |
eligibleValue = lineItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue()));
|
|
|
962 |
criteriaQtyAmountModel.setActivationQty(eligibleQty);
|
|
|
963 |
criteriaQtyAmountModel.setActivationAmount(eligibleValue);
|
|
|
964 |
}
|
| 35394 |
amit |
965 |
|
| 35667 |
amit |
966 |
int eligibleBaseQty = eligibleQty;
|
|
|
967 |
int eligibleBaseValue = eligibleValue;
|
|
|
968 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
969 |
eligibleBaseQty = qtyAmountModel.getActivationQty();
|
|
|
970 |
eligibleBaseValue = (int) qtyAmountModel.getActivationAmount();
|
|
|
971 |
}
|
| 35394 |
amit |
972 |
|
| 35667 |
amit |
973 |
int eligibleSale = eligibleBaseValue;
|
|
|
974 |
int eligibleCriteriaSale = eligibleValue;
|
|
|
975 |
int eligibleCriteriaSaleDp = eligibleValue;
|
| 35394 |
amit |
976 |
|
| 35667 |
amit |
977 |
if (createOfferRequest.getTargetType().equals(AchievementType.QUANTITY)) {
|
|
|
978 |
eligibleSale = eligibleBaseQty;
|
|
|
979 |
eligibleCriteriaSale = eligibleQty;
|
| 35394 |
amit |
980 |
}
|
|
|
981 |
|
| 36051 |
amit |
982 |
// For FIXED (per pc) payout, always use qty regardless of target type
|
|
|
983 |
int valueForFixed = itemCriteriaPayout.getAmountType().equals(AmountType.FIXED) ? eligibleQty : eligibleCriteriaSale;
|
|
|
984 |
|
| 35667 |
amit |
985 |
PayoutSlab eligibleSlab = findEligibleSlab(itemCriteriaPayout.getPayoutSlabs(), eligibleSale);
|
| 35675 |
amit |
986 |
setSlabProgress(itemCriteriaPayout, eligibleSlab, eligibleSale);
|
| 35667 |
amit |
987 |
double finalPayout = calculateFinalPayout(eligibleSlab, itemCriteriaPayout.getAmountType(),
|
| 36051 |
amit |
988 |
eligibleCriteriaSaleDp, valueForFixed);
|
| 35667 |
amit |
989 |
if (eligibleSlab != null) {
|
|
|
990 |
LOGGER.info("Eligible Slab {}", eligibleSlab);
|
|
|
991 |
}
|
|
|
992 |
criteriaQtyAmountModel.setFinalPayout(finalPayout);
|
|
|
993 |
LOGGER.info("Final Payout - {}", finalPayout);
|
| 35394 |
amit |
994 |
}
|
|
|
995 |
return qtyAmountMap;
|
|
|
996 |
}
|
|
|
997 |
|
| 31920 |
amit.gupta |
998 |
@Override
|
|
|
999 |
public void processActivationtOffer(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
1000 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
|
|
1001 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
|
|
1002 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream().collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
| 31923 |
amit.gupta |
1003 |
LOGGER.info("itemCriteriaPayoutMap {}", itemCriteriaPayoutMap);
|
|
|
1004 |
LOGGER.info("offerPayouts {}", offerPayouts);
|
| 31924 |
amit.gupta |
1005 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream().filter(x -> itemCriteriaPayoutMap.get((int) x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
| 31920 |
amit.gupta |
1006 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(), Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
|
|
1007 |
|
|
|
1008 |
|
|
|
1009 |
Map<String, Double> serialNumberPaid = offerPayouts.stream().filter(x -> x.getRejectTimestamp() == null).collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
|
|
1010 |
Map<Integer, Map<Integer, List<FofoOrderItem>>> criteriaPartnerwiseTertiary = offerRepository.getCriteriaWisePartnerTertiary(createOfferRequest);
|
|
|
1011 |
|
| 33087 |
amit.gupta |
1012 |
Map<String, AgeingSummaryModel> ageingSummaryModelsMap = null;
|
| 33662 |
amit.gupta |
1013 |
final Map<String, LocalDateTime> activatedImeisActivationDateMap;
|
| 34217 |
amit.gupta |
1014 |
//Find Only eligible imeis if any
|
|
|
1015 |
List<String> imeisAllowedManually = offerEligibleImeiRepository.selectEligibleImeisByOfferId(createOfferRequest.getId());
|
|
|
1016 |
|
| 31920 |
amit.gupta |
1017 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
1018 |
//All Serialnumbers for all eligible retailers
|
|
|
1019 |
List<String> serialNumbers = criteriaPartnerwiseTertiary.entrySet().stream().flatMap(x -> x.getValue().entrySet().stream().flatMap(foiList -> foiList.getValue().stream()))
|
|
|
1020 |
.flatMap(y -> y.getFofoLineItems().stream()).filter(fli -> fli.getSerialNumber() != null)
|
|
|
1021 |
.map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
1022 |
if (serialNumbers.size() > 0) {
|
| 34217 |
amit.gupta |
1023 |
if (imeisAllowedManually != null) {
|
|
|
1024 |
serialNumbers = serialNumbers.stream().filter(x -> imeisAllowedManually.contains(x)).collect(Collectors.toList());
|
|
|
1025 |
}
|
| 33662 |
amit.gupta |
1026 |
activatedImeisActivationDateMap = activatedImeiRepository.selectBySerialNumbers(serialNumbers).stream()
|
| 31920 |
amit.gupta |
1027 |
.filter(x -> createOfferRequest.isWithinRange(x.getActivationTimestamp()))
|
| 33441 |
amit.gupta |
1028 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x.getActivationTimestamp()));
|
| 33613 |
amit.gupta |
1029 |
ageingSummaryModelsMap = warehouseInventoryItemRepository.findStockAgeingByFofoIdSerialNumbers(0, serialNumbers).stream().collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x, (u, v) -> v));
|
| 31920 |
amit.gupta |
1030 |
} else {
|
| 33662 |
amit.gupta |
1031 |
activatedImeisActivationDateMap = null;
|
| 31920 |
amit.gupta |
1032 |
}
|
|
|
1033 |
} else {
|
| 33662 |
amit.gupta |
1034 |
activatedImeisActivationDateMap = null;
|
| 31920 |
amit.gupta |
1035 |
}
|
|
|
1036 |
|
|
|
1037 |
Map<Integer, Integer> userBaseQtyMap = new HashMap<>();
|
|
|
1038 |
Map<Integer, Integer> userBaseValueMap = new HashMap<>();
|
|
|
1039 |
Map<Integer, Integer> eligibleBaseQtyMap = new HashMap<>();
|
|
|
1040 |
Map<Integer, Integer> eligibleBaseValueMap = new HashMap<>();
|
|
|
1041 |
|
|
|
1042 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
1043 |
Map<Integer, List<FofoOrderItem>> foiMap = criteriaPartnerwiseTertiary.get(0);
|
|
|
1044 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : foiMap.entrySet()) {
|
|
|
1045 |
int fofoId = orderEntry.getKey();
|
|
|
1046 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
|
|
1047 |
int totalBaseQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
1048 |
int totalBaseValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (Math.round(x.getDp())) * x.getQuantity()));
|
|
|
1049 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
1050 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
1051 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
1052 |
Map<Integer, Integer> inventoryItemValueMap = new HashMap<>();
|
|
|
1053 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
1054 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
1055 |
lineItem -> {
|
| 33662 |
amit.gupta |
1056 |
if (activatedImeisActivationDateMap == null || activatedImeisActivationDateMap.containsKey(lineItem.getSerialNumber())) {
|
| 31920 |
amit.gupta |
1057 |
inventoryItemValueMap.put(lineItem.getInventoryItemId(), (int) fofoOrderItem.getDp());
|
|
|
1058 |
}
|
|
|
1059 |
}
|
|
|
1060 |
);
|
|
|
1061 |
}
|
|
|
1062 |
eligibleBaseQtyMap.put(fofoId, inventoryItemValueMap.size());
|
|
|
1063 |
eligibleBaseValueMap.put(fofoId, inventoryItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue())));
|
|
|
1064 |
} else {
|
|
|
1065 |
eligibleBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
1066 |
eligibleBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
1067 |
|
|
|
1068 |
}
|
|
|
1069 |
|
|
|
1070 |
}
|
|
|
1071 |
}
|
|
|
1072 |
|
|
|
1073 |
//As of now eligible sale is considered to be without special criteria like brand/
|
| 33441 |
amit.gupta |
1074 |
//CriteriaPayoutWise
|
| 31920 |
amit.gupta |
1075 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
1076 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
1077 |
Map<Integer, List<FofoOrderItem>> ordersMap = criteriaPartnerwiseTertiary.get(criteriaId);
|
| 33441 |
amit.gupta |
1078 |
//Partywise iteration
|
| 31920 |
amit.gupta |
1079 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : ordersMap.entrySet()) {
|
| 31992 |
amit.gupta |
1080 |
double totalRolloutAmount = 0;
|
| 31920 |
amit.gupta |
1081 |
int fofoId = orderEntry.getKey();
|
|
|
1082 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
|
|
1083 |
OfferRowModel offerRowModel = new OfferRowModel();
|
|
|
1084 |
int totalQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
1085 |
int totalValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> Math.round(x.getDp()) * (x.getQuantity())));
|
|
|
1086 |
int eligibleQty = totalQty;
|
|
|
1087 |
int eligibleValue = totalValue;
|
|
|
1088 |
Map<FofoLineItem, Integer> lineItemValueMap = new HashMap<>();
|
|
|
1089 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
1090 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
1091 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
1092 |
lineItem -> {
|
| 33662 |
amit.gupta |
1093 |
if (activatedImeisActivationDateMap == null) {
|
| 31920 |
amit.gupta |
1094 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
| 33441 |
amit.gupta |
1095 |
|
| 33662 |
amit.gupta |
1096 |
} else if (activatedImeisActivationDateMap.containsKey(lineItem.getSerialNumber())) {
|
|
|
1097 |
if (itemCriteriaPayout.isWithinRange(activatedImeisActivationDateMap.get(lineItem.getSerialNumber()))) {
|
| 33441 |
amit.gupta |
1098 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
|
|
1099 |
}
|
| 31920 |
amit.gupta |
1100 |
}
|
| 33441 |
amit.gupta |
1101 |
|
| 31920 |
amit.gupta |
1102 |
}
|
|
|
1103 |
);
|
|
|
1104 |
}
|
|
|
1105 |
eligibleQty = lineItemValueMap.size();
|
|
|
1106 |
eligibleValue = lineItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue()));
|
|
|
1107 |
offerRowModel.setEligibleImeis(lineItemValueMap.keySet().stream().map(x -> x.getSerialNumber()).collect(Collectors.toList()));
|
|
|
1108 |
}
|
|
|
1109 |
|
|
|
1110 |
int totalBaseQty = totalQty;
|
|
|
1111 |
int totalBaseValue = totalValue;
|
|
|
1112 |
int eligibleBaseQty = eligibleQty;
|
|
|
1113 |
int eligibleBaseValue = eligibleValue;
|
|
|
1114 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
1115 |
totalBaseQty = userBaseQtyMap.get(fofoId) == null ? 0 : userBaseQtyMap.get(fofoId);
|
|
|
1116 |
totalBaseValue = userBaseValueMap.get(fofoId) == null ? 0 : userBaseValueMap.get(fofoId);
|
|
|
1117 |
eligibleBaseQty = eligibleBaseQtyMap.get(fofoId) == null ? 0 : eligibleBaseQtyMap.get(fofoId);
|
|
|
1118 |
eligibleBaseValue = eligibleBaseValueMap.get(fofoId) == null ? 0 : eligibleBaseValueMap.get(fofoId);
|
|
|
1119 |
}
|
|
|
1120 |
|
|
|
1121 |
|
|
|
1122 |
int totalSale = totalBaseValue;
|
|
|
1123 |
int eligibleSale = eligibleBaseValue;
|
|
|
1124 |
int eligibleCriteriaSale = eligibleValue;
|
|
|
1125 |
int eligibleCriteriaSaleDp = eligibleValue;
|
|
|
1126 |
|
|
|
1127 |
if (createOfferRequest.getTargetType().equals(AchievementType.QUANTITY)) {
|
|
|
1128 |
totalSale = totalBaseQty;
|
|
|
1129 |
eligibleSale = eligibleBaseQty;
|
|
|
1130 |
eligibleCriteriaSale = eligibleQty;
|
|
|
1131 |
}
|
|
|
1132 |
|
|
|
1133 |
offerRowModel.setTotalSale(totalSale);
|
|
|
1134 |
offerRowModel.setEligibleSale(eligibleSale);
|
|
|
1135 |
float eligiblePayoutValue = 0;
|
| 33662 |
amit.gupta |
1136 |
float previousPayoutValue = 0;
|
| 32345 |
amit.gupta |
1137 |
double finalPayout;
|
| 31920 |
amit.gupta |
1138 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
|
|
1139 |
if (payoutSlab.getOnwardsAmount() <= eligibleSale) {
|
| 32039 |
amit.gupta |
1140 |
LOGGER.info("Eligible Sale {}", eligibleSale);
|
| 31920 |
amit.gupta |
1141 |
offerRowModel.setPayoutTargetAchieved(payoutSlab.getOnwardsAmount());
|
|
|
1142 |
eligiblePayoutValue = payoutSlab.getPayoutAmount();
|
| 33662 |
amit.gupta |
1143 |
previousPayoutValue = Math.min(eligiblePayoutValue, previousPayoutValue);
|
| 31920 |
amit.gupta |
1144 |
offerRowModel.setPayoutValue(eligiblePayoutValue);
|
| 33662 |
amit.gupta |
1145 |
} else {
|
|
|
1146 |
break;
|
| 31920 |
amit.gupta |
1147 |
}
|
|
|
1148 |
}
|
|
|
1149 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1150 |
finalPayout = (eligiblePayoutValue * eligibleCriteriaSaleDp) / 100;
|
|
|
1151 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| 36051 |
amit |
1152 |
// FIXED = per piece, always use qty regardless of target type
|
|
|
1153 |
finalPayout = eligiblePayoutValue * eligibleQty;
|
| 31920 |
amit.gupta |
1154 |
} else {
|
| 32039 |
amit.gupta |
1155 |
Map<Integer, Double> fofoCriteriaMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
|
|
1156 |
LOGGER.info("fofoCriteriaMap {}", fofoCriteriaMap);
|
| 31920 |
amit.gupta |
1157 |
finalPayout = eligiblePayoutValue;
|
|
|
1158 |
if (fofoCriteriaMap != null) {
|
|
|
1159 |
if (fofoCriteriaMap.containsKey(fofoId)) {
|
| 33662 |
amit.gupta |
1160 |
//double totalPayout = fofoCriteriaMap.get(fofoId);
|
|
|
1161 |
finalPayout = eligiblePayoutValue - previousPayoutValue;
|
| 32039 |
amit.gupta |
1162 |
if (Math.abs(finalPayout) < Utils.DOUBLE_EPSILON) {
|
| 31920 |
amit.gupta |
1163 |
finalPayout = 0;
|
| 32039 |
amit.gupta |
1164 |
}
|
| 31920 |
amit.gupta |
1165 |
}
|
|
|
1166 |
}
|
|
|
1167 |
}
|
|
|
1168 |
|
| 32039 |
amit.gupta |
1169 |
LOGGER.info("Final Payout - {}", finalPayout);
|
|
|
1170 |
|
| 31920 |
amit.gupta |
1171 |
if (finalPayout > 0) {
|
|
|
1172 |
List<Integer> inventoryItemIds = lineItemValueMap.keySet().stream().map(x -> x.getInventoryItemId()).collect(Collectors.toList());
|
| 33662 |
amit.gupta |
1173 |
int totalDpValue = lineItemValueMap.values().stream().collect(Collectors.summingInt(x -> x));
|
|
|
1174 |
LOGGER.info("Total DP Value {}", totalDpValue);
|
| 31920 |
amit.gupta |
1175 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItemRepository.selectAllByIds(inventoryItemIds).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1176 |
for (Map.Entry<FofoLineItem, Integer> lineItemValueEntry : lineItemValueMap.entrySet()) {
|
| 33087 |
amit.gupta |
1177 |
FofoLineItem fofoLineItem = lineItemValueEntry.getKey();
|
| 31920 |
amit.gupta |
1178 |
double amount = 0;
|
|
|
1179 |
if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| 32039 |
amit.gupta |
1180 |
amount = (eligiblePayoutValue * lineItemValueEntry.getValue()) / totalDpValue;
|
| 31920 |
amit.gupta |
1181 |
} else {
|
| 33662 |
amit.gupta |
1182 |
if (ageingSummaryModelsMap.containsKey(fofoLineItem.getSerialNumber())) {
|
| 35394 |
amit |
1183 |
if (ageingSummaryModelsMap.get(fofoLineItem.getSerialNumber()).isAgedAbove(STOCK_AGEING_THRESHOLD_DAYS)) {
|
| 33662 |
amit.gupta |
1184 |
continue;
|
|
|
1185 |
}
|
|
|
1186 |
}
|
| 31920 |
amit.gupta |
1187 |
amount = eligiblePayoutValue;
|
|
|
1188 |
}
|
| 33662 |
amit.gupta |
1189 |
if (serialNumberPaid.containsKey(fofoLineItem.getSerialNumber())) {
|
|
|
1190 |
amount = amount - serialNumberPaid.get(fofoLineItem.getSerialNumber());
|
|
|
1191 |
}
|
| 32039 |
amit.gupta |
1192 |
LOGGER.info("Amount - {}", amount);
|
| 31920 |
amit.gupta |
1193 |
//ignore reasonably small
|
|
|
1194 |
if (amount < Utils.DOUBLE_EPSILON) continue;
|
|
|
1195 |
AmountModel amountModel = new AmountModel();
|
|
|
1196 |
amountModel.setAmountType(itemCriteriaPayout.getAmountType());
|
|
|
1197 |
amountModel.setAmount(amount);
|
| 32021 |
amit.gupta |
1198 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
| 33087 |
amit.gupta |
1199 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItemsMap.get(fofoLineItem.getInventoryItemId()));
|
| 31920 |
amit.gupta |
1200 |
double rolloutAmount = inventoryPayoutModel.getRolloutAmount(amountModel);
|
|
|
1201 |
totalRolloutAmount += rolloutAmount;
|
|
|
1202 |
|
| 33087 |
amit.gupta |
1203 |
OfferPayout offerPayout = new OfferPayout(fofoId, createOfferRequest.getId(), criteriaId, amount, fofoLineItem.getSerialNumber(), rolloutAmount, SchemePayoutStatus.CREDITED, createOfferRequest.getDescription(), LocalDateTime.now());
|
| 33459 |
amit.gupta |
1204 |
offerPayout.setInventoryItemId(fofoLineItem.getInventoryItemId());
|
| 31920 |
amit.gupta |
1205 |
offerPayoutRepository.persist(offerPayout);
|
|
|
1206 |
}
|
| 32039 |
amit.gupta |
1207 |
walletService.addAmountToWallet((int) fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) totalRolloutAmount, LocalDateTime.now());
|
| 31920 |
amit.gupta |
1208 |
}
|
|
|
1209 |
}
|
|
|
1210 |
}
|
|
|
1211 |
Offer offer = offerRepository.selectById(createOfferRequest.getId());
|
|
|
1212 |
offer.setProcessedTimestamp(LocalDateTime.now());
|
|
|
1213 |
}
|
|
|
1214 |
|
|
|
1215 |
@Override
|
| 36305 |
amit |
1216 |
@Transactional(readOnly = true)
|
|
|
1217 |
public List<OfferPartnerPayoutData> calculateOfferPayouts(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
1218 |
List<OfferPartnerPayoutData> result = new ArrayList<>();
|
|
|
1219 |
|
|
|
1220 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
|
|
1221 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
|
|
1222 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream().collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
|
|
1223 |
|
|
|
1224 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream()
|
|
|
1225 |
.filter(x -> itemCriteriaPayoutMap.containsKey((int) x.getCriteriaId()) &&
|
|
|
1226 |
itemCriteriaPayoutMap.get((int) x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
|
|
1227 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(),
|
|
|
1228 |
Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
|
|
1229 |
|
|
|
1230 |
Map<String, Double> serialNumberPaid = offerPayouts.stream()
|
|
|
1231 |
.filter(x -> x.getRejectTimestamp() == null)
|
|
|
1232 |
.collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
|
|
1233 |
|
|
|
1234 |
Map<Integer, Map<Integer, List<FofoOrderItem>>> criteriaPartnerwiseTertiary = offerRepository.getCriteriaWisePartnerTertiary(createOfferRequest);
|
|
|
1235 |
|
|
|
1236 |
Map<String, AgeingSummaryModel> ageingSummaryModelsMap = null;
|
|
|
1237 |
final Map<String, LocalDateTime> activatedImeisActivationDateMap;
|
|
|
1238 |
List<String> imeisAllowedManually = offerEligibleImeiRepository.selectEligibleImeisByOfferId(createOfferRequest.getId());
|
|
|
1239 |
|
|
|
1240 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
1241 |
List<String> serialNumbers = criteriaPartnerwiseTertiary.entrySet().stream()
|
|
|
1242 |
.flatMap(x -> x.getValue().entrySet().stream().flatMap(foiList -> foiList.getValue().stream()))
|
|
|
1243 |
.flatMap(y -> y.getFofoLineItems().stream()).filter(fli -> fli.getSerialNumber() != null)
|
|
|
1244 |
.map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
1245 |
if (serialNumbers.size() > 0) {
|
|
|
1246 |
if (imeisAllowedManually != null) {
|
|
|
1247 |
serialNumbers = serialNumbers.stream().filter(x -> imeisAllowedManually.contains(x)).collect(Collectors.toList());
|
|
|
1248 |
}
|
|
|
1249 |
activatedImeisActivationDateMap = activatedImeiRepository.selectBySerialNumbers(serialNumbers).stream()
|
|
|
1250 |
.filter(x -> createOfferRequest.isWithinRange(x.getActivationTimestamp()))
|
|
|
1251 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x.getActivationTimestamp()));
|
|
|
1252 |
ageingSummaryModelsMap = warehouseInventoryItemRepository.findStockAgeingByFofoIdSerialNumbers(0, serialNumbers)
|
|
|
1253 |
.stream().collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x, (u, v) -> v));
|
|
|
1254 |
} else {
|
|
|
1255 |
activatedImeisActivationDateMap = null;
|
|
|
1256 |
}
|
|
|
1257 |
} else {
|
|
|
1258 |
activatedImeisActivationDateMap = null;
|
|
|
1259 |
}
|
|
|
1260 |
|
|
|
1261 |
Map<Integer, Integer> userBaseQtyMap = new HashMap<>();
|
|
|
1262 |
Map<Integer, Integer> userBaseValueMap = new HashMap<>();
|
|
|
1263 |
Map<Integer, Integer> calcEligibleBaseQtyMap = new HashMap<>();
|
|
|
1264 |
Map<Integer, Integer> calcEligibleBaseValueMap = new HashMap<>();
|
|
|
1265 |
|
|
|
1266 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
1267 |
Map<Integer, List<FofoOrderItem>> foiMap = criteriaPartnerwiseTertiary.get(0);
|
|
|
1268 |
if (foiMap != null) {
|
|
|
1269 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : foiMap.entrySet()) {
|
|
|
1270 |
int fofoId = orderEntry.getKey();
|
|
|
1271 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
|
|
1272 |
int totalBaseQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
1273 |
int totalBaseValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (Math.round(x.getDp())) * x.getQuantity()));
|
|
|
1274 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
1275 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
1276 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
1277 |
Map<Integer, Integer> inventoryItemValueMap = new HashMap<>();
|
|
|
1278 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
1279 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
1280 |
lineItem -> {
|
|
|
1281 |
if (activatedImeisActivationDateMap == null || activatedImeisActivationDateMap.containsKey(lineItem.getSerialNumber())) {
|
|
|
1282 |
inventoryItemValueMap.put(lineItem.getInventoryItemId(), (int) fofoOrderItem.getDp());
|
|
|
1283 |
}
|
|
|
1284 |
}
|
|
|
1285 |
);
|
|
|
1286 |
}
|
|
|
1287 |
calcEligibleBaseQtyMap.put(fofoId, inventoryItemValueMap.size());
|
|
|
1288 |
calcEligibleBaseValueMap.put(fofoId, inventoryItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue())));
|
|
|
1289 |
} else {
|
|
|
1290 |
calcEligibleBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
1291 |
calcEligibleBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
1292 |
}
|
|
|
1293 |
}
|
|
|
1294 |
}
|
|
|
1295 |
}
|
|
|
1296 |
|
|
|
1297 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
1298 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
1299 |
Map<Integer, List<FofoOrderItem>> ordersMap = criteriaPartnerwiseTertiary.get(criteriaId);
|
|
|
1300 |
if (ordersMap == null) continue;
|
|
|
1301 |
|
|
|
1302 |
for (Map.Entry<Integer, List<FofoOrderItem>> orderEntry : ordersMap.entrySet()) {
|
|
|
1303 |
int fofoId = orderEntry.getKey();
|
|
|
1304 |
List<FofoOrderItem> fofoOrderItems = orderEntry.getValue();
|
|
|
1305 |
int totalQty = fofoOrderItems.stream().collect(Collectors.summingInt(x -> (x.getQuantity())));
|
|
|
1306 |
int totalValue = fofoOrderItems.stream().collect(Collectors.summingInt(x -> Math.round(x.getDp()) * (x.getQuantity())));
|
|
|
1307 |
int eligibleQty = totalQty;
|
|
|
1308 |
int eligibleValue = totalValue;
|
|
|
1309 |
Map<FofoLineItem, Integer> lineItemValueMap = new HashMap<>();
|
|
|
1310 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.ACTIVATION)) {
|
|
|
1311 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
1312 |
fofoOrderItem.getFofoLineItems().stream().filter(x -> x.getSerialNumber() != null).forEach(
|
|
|
1313 |
lineItem -> {
|
|
|
1314 |
if (activatedImeisActivationDateMap == null) {
|
|
|
1315 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
|
|
1316 |
} else if (activatedImeisActivationDateMap.containsKey(lineItem.getSerialNumber())) {
|
|
|
1317 |
if (itemCriteriaPayout.isWithinRange(activatedImeisActivationDateMap.get(lineItem.getSerialNumber()))) {
|
|
|
1318 |
lineItemValueMap.put(lineItem, (int) fofoOrderItem.getDp());
|
|
|
1319 |
}
|
|
|
1320 |
}
|
|
|
1321 |
}
|
|
|
1322 |
);
|
|
|
1323 |
}
|
|
|
1324 |
eligibleQty = lineItemValueMap.size();
|
|
|
1325 |
eligibleValue = lineItemValueMap.entrySet().stream().collect(Collectors.summingInt(x -> x.getValue()));
|
|
|
1326 |
}
|
|
|
1327 |
|
|
|
1328 |
int eligibleBaseQty = eligibleQty;
|
|
|
1329 |
int eligibleBaseValue = eligibleValue;
|
|
|
1330 |
if (createOfferRequest.isBaseCriteria()) {
|
|
|
1331 |
eligibleBaseQty = calcEligibleBaseQtyMap.get(fofoId) == null ? 0 : calcEligibleBaseQtyMap.get(fofoId);
|
|
|
1332 |
eligibleBaseValue = calcEligibleBaseValueMap.get(fofoId) == null ? 0 : calcEligibleBaseValueMap.get(fofoId);
|
|
|
1333 |
}
|
|
|
1334 |
|
|
|
1335 |
int eligibleSale = eligibleBaseValue;
|
|
|
1336 |
int eligibleCriteriaSaleDp = eligibleValue;
|
|
|
1337 |
if (createOfferRequest.getTargetType().equals(AchievementType.QUANTITY)) {
|
|
|
1338 |
eligibleSale = eligibleBaseQty;
|
|
|
1339 |
}
|
|
|
1340 |
|
|
|
1341 |
float eligiblePayoutValue = 0;
|
|
|
1342 |
float previousPayoutValue = 0;
|
|
|
1343 |
double finalPayout;
|
|
|
1344 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
|
|
1345 |
if (payoutSlab.getOnwardsAmount() <= eligibleSale) {
|
|
|
1346 |
eligiblePayoutValue = payoutSlab.getPayoutAmount();
|
|
|
1347 |
previousPayoutValue = Math.min(eligiblePayoutValue, previousPayoutValue);
|
|
|
1348 |
} else {
|
|
|
1349 |
break;
|
|
|
1350 |
}
|
|
|
1351 |
}
|
|
|
1352 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1353 |
finalPayout = (eligiblePayoutValue * eligibleCriteriaSaleDp) / 100;
|
|
|
1354 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1355 |
finalPayout = eligiblePayoutValue * eligibleQty;
|
|
|
1356 |
} else {
|
|
|
1357 |
Map<Integer, Double> fofoCriteriaMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
|
|
1358 |
finalPayout = eligiblePayoutValue;
|
|
|
1359 |
if (fofoCriteriaMap != null && fofoCriteriaMap.containsKey(fofoId)) {
|
|
|
1360 |
finalPayout = eligiblePayoutValue - previousPayoutValue;
|
|
|
1361 |
if (Math.abs(finalPayout) < Utils.DOUBLE_EPSILON) {
|
|
|
1362 |
finalPayout = 0;
|
|
|
1363 |
}
|
|
|
1364 |
}
|
|
|
1365 |
}
|
|
|
1366 |
|
|
|
1367 |
if (finalPayout > 0) {
|
|
|
1368 |
result.add(new OfferPartnerPayoutData(
|
|
|
1369 |
fofoId, criteriaId, finalPayout, eligiblePayoutValue,
|
|
|
1370 |
lineItemValueMap, itemCriteriaPayout, serialNumberPaid,
|
|
|
1371 |
ageingSummaryModelsMap, createOfferRequest.isDiscount()));
|
|
|
1372 |
}
|
|
|
1373 |
}
|
|
|
1374 |
}
|
|
|
1375 |
|
|
|
1376 |
return result;
|
|
|
1377 |
}
|
|
|
1378 |
|
|
|
1379 |
@Override
|
| 31920 |
amit.gupta |
1380 |
public void processSellin(CreateOfferRequest createOfferRequest) throws Exception {
|
|
|
1381 |
|
|
|
1382 |
//List<OfferRowModel> offerRowModels = new ArrayList<>();
|
|
|
1383 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
| 32345 |
amit.gupta |
1384 |
Map<String, Double> serialNumberPaid = offerPayouts.stream().filter(x -> x.getRejectTimestamp() == null).collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
| 34072 |
ranu |
1385 |
LOGGER.info("serialNumberPaid {}", serialNumberPaid);
|
| 32345 |
amit.gupta |
1386 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
|
|
1387 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream().collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
|
|
1388 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream().filter(x -> itemCriteriaPayoutMap.get((int) x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
|
|
1389 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(), Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
| 31920 |
amit.gupta |
1390 |
Set<Long> fofoIds = offerPayouts.stream().map(x -> x.getFofoId()).collect(Collectors.toSet());
|
|
|
1391 |
|
|
|
1392 |
Map<Integer, Map<Integer, List<Order>>> criteriaOrdersMap = offerRepository.getPartnerWiseSellin(createOfferRequest, true);
|
|
|
1393 |
|
| 32345 |
amit.gupta |
1394 |
|
| 31920 |
amit.gupta |
1395 |
Map<Integer, List<Order>> userBaseCriteriaOrdersMap = null;
|
|
|
1396 |
if (criteriaOrdersMap.get(0) != null && criteriaOrdersMap.get(0).size() > 0) {
|
|
|
1397 |
userBaseCriteriaOrdersMap = criteriaOrdersMap.get(0).entrySet().stream()
|
|
|
1398 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
1399 |
}
|
|
|
1400 |
|
| 32345 |
amit.gupta |
1401 |
|
| 31920 |
amit.gupta |
1402 |
Map<Integer, Integer> userBaseQtyMap = null;
|
|
|
1403 |
Map<Integer, Integer> userBaseValueMap = null;
|
|
|
1404 |
if (userBaseCriteriaOrdersMap != null) {
|
|
|
1405 |
userBaseQtyMap = new HashMap<>();
|
|
|
1406 |
userBaseValueMap = new HashMap<>();
|
|
|
1407 |
for (Map.Entry<Integer, List<Order>> orderEntry : userBaseCriteriaOrdersMap.entrySet()) {
|
|
|
1408 |
int fofoId = orderEntry.getKey();
|
|
|
1409 |
List<Order> orders = orderEntry.getValue();
|
|
|
1410 |
int totalBaseQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
|
|
1411 |
int totalBaseValue = orders.stream().collect(Collectors.summingInt(x ->
|
|
|
1412 |
Math.round(x.getLineItem().getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))
|
|
|
1413 |
));
|
|
|
1414 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
1415 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
1416 |
}
|
|
|
1417 |
}
|
|
|
1418 |
LOGGER.info("Base Qty Map - {}", userBaseQtyMap);
|
|
|
1419 |
LOGGER.info("Base Value Map - {}", userBaseValueMap);
|
|
|
1420 |
|
|
|
1421 |
LOGGER.info("Processing sellin");
|
|
|
1422 |
List<OfferPayout> offerPayoutsNew = new ArrayList<>();
|
|
|
1423 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
1424 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
1425 |
Map<Integer, List<Order>> ordersMap = criteriaOrdersMap.get(criteriaId).entrySet().stream()
|
|
|
1426 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
1427 |
for (Map.Entry<Integer, List<Order>> orderEntry : ordersMap.entrySet()) {
|
|
|
1428 |
int fofoId = orderEntry.getKey();
|
|
|
1429 |
//CustomRetailer retailer = retailerService.getAllFofoRetailers().get(fofoId);
|
|
|
1430 |
List<Order> orders = orderEntry.getValue();
|
| 32345 |
amit.gupta |
1431 |
//Set<String> returnedSerialNumbers = orders.stream().filter(x -> x.getSerialNumber() != null).flatMap(x -> x.getReturnedImeis().stream()).collect(Collectors.toSet());
|
| 31920 |
amit.gupta |
1432 |
int totalQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
|
|
1433 |
int totalValue = orders.stream().collect(Collectors.summingInt(x -> Math.round(x.getLineItem().
|
|
|
1434 |
getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))));
|
|
|
1435 |
int totalBaseQty = totalQty;
|
|
|
1436 |
int totalBaseValue = totalValue;
|
|
|
1437 |
if (userBaseCriteriaOrdersMap != null) {
|
|
|
1438 |
if (userBaseQtyMap.containsKey(fofoId)) {
|
|
|
1439 |
totalBaseQty = userBaseQtyMap.get(fofoId);
|
|
|
1440 |
totalBaseValue = userBaseValueMap.get(fofoId);
|
|
|
1441 |
} else {
|
|
|
1442 |
continue;
|
|
|
1443 |
}
|
|
|
1444 |
}
|
|
|
1445 |
|
|
|
1446 |
int purchasedValue;
|
|
|
1447 |
if (createOfferRequest.getTargetType().equals(AchievementType.VALUE)) {
|
|
|
1448 |
purchasedValue = totalBaseValue;
|
|
|
1449 |
} else {
|
|
|
1450 |
purchasedValue = totalBaseQty;
|
|
|
1451 |
}
|
|
|
1452 |
|
| 33695 |
amit.gupta |
1453 |
PayoutSlab eligibleSlab = null;
|
| 33662 |
amit.gupta |
1454 |
|
|
|
1455 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
|
|
1456 |
if (payoutSlab.getOnwardsAmount() <= purchasedValue) {
|
|
|
1457 |
eligibleSlab = payoutSlab;
|
|
|
1458 |
} else {
|
|
|
1459 |
break;
|
|
|
1460 |
}
|
|
|
1461 |
}
|
| 33695 |
amit.gupta |
1462 |
if (eligibleSlab == null) continue;
|
| 31920 |
amit.gupta |
1463 |
List<Integer> orderIds = orders.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
1464 |
Map<Integer, LineItem> lineItemMap = orders.stream().collect(Collectors.toMap(x -> x.getLineItem().getId(), x -> x.getLineItem()));
|
|
|
1465 |
List<WarehouseScan> warehouseScans = warehouseScanRepository.selectAllByOrderIds(orderIds);
|
| 32540 |
amit.gupta |
1466 |
LOGGER.info("Warehosue scans --> {}", warehouseScans);
|
| 32345 |
amit.gupta |
1467 |
List<Integer> warehouseInventoryItemIds = warehouseScans.stream().collect(Collectors.groupingBy(x -> x.getInventoryItemId())).entrySet()
|
| 31920 |
amit.gupta |
1468 |
.stream().filter(x -> x.getValue().size() == 1).map(x -> x.getKey()).collect(Collectors.toList());
|
| 32795 |
amit.gupta |
1469 |
|
|
|
1470 |
List<OfferPayout> partnerPayouts = offerPayouts.stream().filter(x -> x.getFofoId() == fofoId).collect(Collectors.toList());
|
| 32868 |
amit.gupta |
1471 |
Set<String> serialNumbersRejected = partnerPayouts.stream().filter(x -> x.getStatus().equals(SchemePayoutStatus.REJECTED)).map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
| 32795 |
amit.gupta |
1472 |
|
| 34092 |
amit.gupta |
1473 |
Set<String> billedNotCancelledSerialNumbers;
|
| 32345 |
amit.gupta |
1474 |
LOGGER.info("warehouseInventoryItemIds {}", warehouseInventoryItemIds);
|
|
|
1475 |
if (warehouseInventoryItemIds.size() > 0) {
|
|
|
1476 |
List<WarehouseInventoryItem> warehouseInventoryItems = warehouseInventoryItemRepository.selectAllByIds(warehouseInventoryItemIds);
|
| 34092 |
amit.gupta |
1477 |
billedNotCancelledSerialNumbers = warehouseInventoryItems.stream().map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
| 31920 |
amit.gupta |
1478 |
} else {
|
| 34092 |
amit.gupta |
1479 |
billedNotCancelledSerialNumbers = new HashSet<>();
|
| 31920 |
amit.gupta |
1480 |
}
|
| 34092 |
amit.gupta |
1481 |
LOGGER.info("billedNotCancelledSerialNumbers {}", billedNotCancelledSerialNumbers);
|
|
|
1482 |
|
| 31920 |
amit.gupta |
1483 |
Map<String, LineItem> serialNumberMap = lineItemImeisRepository.selectByLineItemIds(new ArrayList<>(lineItemMap.keySet()))
|
| 32868 |
amit.gupta |
1484 |
.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 |
1485 |
|
| 34226 |
amit.gupta |
1486 |
List<String> manuallyEligibleImeis = offerEligibleImeiRepository.selectEligibleImeisByOfferId(createOfferRequest.getId());
|
| 32795 |
amit.gupta |
1487 |
|
| 34092 |
amit.gupta |
1488 |
serialNumberMap = serialNumberMap.entrySet().stream().filter(x -> billedNotCancelledSerialNumbers.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
| 34231 |
amit.gupta |
1489 |
|
|
|
1490 |
//Just a validation need to revaluate this
|
|
|
1491 |
Map<String, InventoryItem> serialNumberInventoryItemMap = this.getInventoryItemMap(fofoId, serialNumberMap);
|
|
|
1492 |
if (serialNumberInventoryItemMap.size() < billedNotCancelledSerialNumbers.size()) continue;
|
|
|
1493 |
|
| 34623 |
amit.gupta |
1494 |
LOGGER.info("serialNumberMap {}", serialNumberMap);
|
| 34226 |
amit.gupta |
1495 |
if (manuallyEligibleImeis != null) {
|
| 34227 |
amit.gupta |
1496 |
serialNumberMap = serialNumberMap.entrySet().stream().filter(x -> manuallyEligibleImeis.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
| 34624 |
amit.gupta |
1497 |
if (serialNumberMap.size() == 0) continue;
|
| 34231 |
amit.gupta |
1498 |
serialNumberInventoryItemMap = this.getInventoryItemMap(fofoId, serialNumberMap);
|
| 34226 |
amit.gupta |
1499 |
}
|
| 32543 |
amit.gupta |
1500 |
if (serialNumberMap.size() == 0) continue;
|
| 32064 |
amit.gupta |
1501 |
//Continue if all imeis havent been grned
|
|
|
1502 |
|
| 32345 |
amit.gupta |
1503 |
|
| 33662 |
amit.gupta |
1504 |
float eligiblePayoutValue = eligibleSlab.getPayoutAmount();
|
| 32345 |
amit.gupta |
1505 |
double finalPayout;
|
|
|
1506 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1507 |
finalPayout = (eligiblePayoutValue * purchasedValue) / 100;
|
|
|
1508 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
| 36051 |
amit |
1509 |
// FIXED = per piece, use per-criteria qty not base value
|
|
|
1510 |
finalPayout = eligiblePayoutValue * totalQty;
|
| 32345 |
amit.gupta |
1511 |
} else {
|
| 33695 |
amit.gupta |
1512 |
Map<Integer, Double> fofoCriteriaPayoutMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
|
|
1513 |
LOGGER.info("fofoCriteriaMap {}", fofoCriteriaPayoutMap);
|
| 32345 |
amit.gupta |
1514 |
finalPayout = eligiblePayoutValue;
|
| 33695 |
amit.gupta |
1515 |
if (fofoCriteriaPayoutMap != null && fofoCriteriaPayoutMap.containsKey(fofoId)) {
|
|
|
1516 |
finalPayout = eligiblePayoutValue - fofoCriteriaPayoutMap.get(fofoId);
|
|
|
1517 |
if (Math.abs(finalPayout) < Utils.DOUBLE_EPSILON) {
|
|
|
1518 |
finalPayout = 0;
|
| 32345 |
amit.gupta |
1519 |
}
|
| 31920 |
amit.gupta |
1520 |
}
|
|
|
1521 |
}
|
| 32345 |
amit.gupta |
1522 |
|
|
|
1523 |
LOGGER.info("Or - {}", finalPayout);
|
|
|
1524 |
|
|
|
1525 |
if (finalPayout > 0) {
|
|
|
1526 |
double totalRolloutAmount = 0;
|
|
|
1527 |
int totalDpValue = serialNumberInventoryItemMap.entrySet().stream().mapToInt(x -> (int) x.getValue().getUnitPrice()).sum();
|
|
|
1528 |
for (Map.Entry<String, InventoryItem> serialNumberInventoryItemEntry : serialNumberInventoryItemMap.entrySet()) {
|
|
|
1529 |
double amount = 0;
|
|
|
1530 |
LOGGER.info("Total DP Value {}", totalDpValue);
|
|
|
1531 |
if (AmountType.SLAB_FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1532 |
amount = (eligiblePayoutValue * serialNumberInventoryItemEntry.getValue().getUnitPrice()) / totalDpValue;
|
|
|
1533 |
LOGGER.info("Amount - {}", amount);
|
|
|
1534 |
} else {
|
|
|
1535 |
amount = eligiblePayoutValue;
|
|
|
1536 |
if (serialNumberPaid.containsKey(serialNumberInventoryItemEntry.getKey())) {
|
| 34073 |
ranu |
1537 |
LOGGER.info("serial number slab amount {}", serialNumberPaid.get(serialNumberInventoryItemEntry.getKey()));
|
| 32345 |
amit.gupta |
1538 |
amount = amount - serialNumberPaid.get(serialNumberInventoryItemEntry.getKey());
|
|
|
1539 |
}
|
|
|
1540 |
}
|
|
|
1541 |
LOGGER.info("Amount - {}", amount);
|
|
|
1542 |
//ignore reasonably small
|
| 33662 |
amit.gupta |
1543 |
if (Math.abs(amount) < Utils.DOUBLE_EPSILON) continue;
|
| 32345 |
amit.gupta |
1544 |
AmountModel amountModel = new AmountModel();
|
|
|
1545 |
amountModel.setAmountType(itemCriteriaPayout.getAmountType());
|
|
|
1546 |
amountModel.setAmount(amount);
|
|
|
1547 |
amountModel.setDiscount(createOfferRequest.isDiscount());
|
|
|
1548 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(serialNumberInventoryItemEntry.getValue());
|
|
|
1549 |
double rolloutAmount = inventoryPayoutModel.getRolloutAmount(amountModel);
|
|
|
1550 |
totalRolloutAmount += rolloutAmount;
|
|
|
1551 |
|
|
|
1552 |
OfferPayout offerPayout = new OfferPayout(fofoId, createOfferRequest.getId(), criteriaId, amount, serialNumberInventoryItemEntry.getKey(), rolloutAmount, SchemePayoutStatus.CREDITED, createOfferRequest.getDescription(), LocalDateTime.now());
|
| 33459 |
amit.gupta |
1553 |
offerPayout.setInventoryItemId(serialNumberInventoryItemEntry.getValue().getId());
|
| 32345 |
amit.gupta |
1554 |
offerPayoutRepository.persist(offerPayout);
|
|
|
1555 |
}
|
| 34623 |
amit.gupta |
1556 |
walletService.addAmountToWallet(fofoId, createOfferRequest.getId(), WalletReferenceType.ADDITIONAL_SCHEME, createOfferRequest.getDescription(), (float) totalRolloutAmount, LocalDateTime.now());
|
| 32345 |
amit.gupta |
1557 |
}
|
| 31920 |
amit.gupta |
1558 |
}
|
|
|
1559 |
}
|
|
|
1560 |
Offer offer = offerRepository.selectById(createOfferRequest.getId());
|
|
|
1561 |
offer.setProcessedTimestamp(LocalDateTime.now());
|
|
|
1562 |
}
|
| 32064 |
amit.gupta |
1563 |
|
| 36305 |
amit |
1564 |
@Override
|
|
|
1565 |
@Transactional(readOnly = true)
|
|
|
1566 |
public List<SellinPartnerPayoutData> calculateSellinPayouts(CreateOfferRequest createOfferRequest) throws Exception {
|
|
|
1567 |
List<SellinPartnerPayoutData> result = new ArrayList<>();
|
|
|
1568 |
|
|
|
1569 |
List<OfferPayout> offerPayouts = offerPayoutRepository.selectAllByOfferId(createOfferRequest.getId());
|
|
|
1570 |
Map<String, Double> serialNumberPaid = offerPayouts.stream()
|
|
|
1571 |
.filter(x -> x.getRejectTimestamp() == null)
|
|
|
1572 |
.collect(Collectors.groupingBy(x -> x.getSerialNumber(), Collectors.summingDouble(x -> x.getSlabAmount())));
|
|
|
1573 |
|
|
|
1574 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
|
|
1575 |
Map<Integer, ItemCriteriaPayout> itemCriteriaPayoutMap = itemCriteriaPayouts.stream()
|
|
|
1576 |
.collect(Collectors.toMap(x -> x.getItemCriteria().getId(), x -> x));
|
|
|
1577 |
Map<Integer, Map<Integer, Double>> fixedSlabCriteriaPartnerMap = offerPayouts.stream()
|
|
|
1578 |
.filter(x -> itemCriteriaPayoutMap.containsKey((int) x.getCriteriaId()) &&
|
|
|
1579 |
itemCriteriaPayoutMap.get((int) x.getCriteriaId()).getAmountType().equals(AmountType.SLAB_FIXED))
|
|
|
1580 |
.collect(Collectors.groupingBy(x -> (int) x.getCriteriaId(),
|
|
|
1581 |
Collectors.groupingBy(x -> (int) x.getFofoId(), Collectors.summingDouble(x -> x.getSlabAmount()))));
|
|
|
1582 |
Set<Long> fofoIds = offerPayouts.stream().map(x -> x.getFofoId()).collect(Collectors.toSet());
|
|
|
1583 |
|
|
|
1584 |
Map<Integer, Map<Integer, List<Order>>> criteriaOrdersMap = offerRepository.getPartnerWiseSellin(createOfferRequest, true);
|
|
|
1585 |
|
|
|
1586 |
Map<Integer, List<Order>> userBaseCriteriaOrdersMap = null;
|
|
|
1587 |
if (criteriaOrdersMap.get(0) != null && criteriaOrdersMap.get(0).size() > 0) {
|
|
|
1588 |
userBaseCriteriaOrdersMap = criteriaOrdersMap.get(0).entrySet().stream()
|
|
|
1589 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
1590 |
}
|
|
|
1591 |
|
|
|
1592 |
Map<Integer, Integer> userBaseQtyMap = null;
|
|
|
1593 |
Map<Integer, Integer> userBaseValueMap = null;
|
|
|
1594 |
if (userBaseCriteriaOrdersMap != null) {
|
|
|
1595 |
userBaseQtyMap = new HashMap<>();
|
|
|
1596 |
userBaseValueMap = new HashMap<>();
|
|
|
1597 |
for (Map.Entry<Integer, List<Order>> orderEntry : userBaseCriteriaOrdersMap.entrySet()) {
|
|
|
1598 |
int fofoId = orderEntry.getKey();
|
|
|
1599 |
List<Order> orders = orderEntry.getValue();
|
|
|
1600 |
int totalBaseQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
|
|
1601 |
int totalBaseValue = orders.stream().collect(Collectors.summingInt(x ->
|
|
|
1602 |
Math.round(x.getLineItem().getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))
|
|
|
1603 |
));
|
|
|
1604 |
userBaseQtyMap.put(fofoId, totalBaseQty);
|
|
|
1605 |
userBaseValueMap.put(fofoId, totalBaseValue);
|
|
|
1606 |
}
|
|
|
1607 |
}
|
|
|
1608 |
|
|
|
1609 |
for (ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()) {
|
|
|
1610 |
int criteriaId = itemCriteriaPayout.getItemCriteria().getId();
|
|
|
1611 |
if (criteriaOrdersMap.get(criteriaId) == null) continue;
|
|
|
1612 |
Map<Integer, List<Order>> ordersMap = criteriaOrdersMap.get(criteriaId).entrySet().stream()
|
|
|
1613 |
.filter(x -> !fofoIds.contains(x.getKey())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
1614 |
|
|
|
1615 |
for (Map.Entry<Integer, List<Order>> orderEntry : ordersMap.entrySet()) {
|
|
|
1616 |
int fofoId = orderEntry.getKey();
|
|
|
1617 |
List<Order> orders = orderEntry.getValue();
|
|
|
1618 |
int totalQty = orders.stream().collect(Collectors.summingInt(x -> (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty())));
|
|
|
1619 |
int totalValue = orders.stream().collect(Collectors.summingInt(x -> Math.round(x.getLineItem().
|
|
|
1620 |
getUnitPrice() * (x.getLineItem().getQuantity() - x.getLineItem().getReturnQty()))));
|
|
|
1621 |
int totalBaseQty = totalQty;
|
|
|
1622 |
int totalBaseValue = totalValue;
|
|
|
1623 |
if (userBaseCriteriaOrdersMap != null) {
|
|
|
1624 |
if (userBaseQtyMap.containsKey(fofoId)) {
|
|
|
1625 |
totalBaseQty = userBaseQtyMap.get(fofoId);
|
|
|
1626 |
totalBaseValue = userBaseValueMap.get(fofoId);
|
|
|
1627 |
} else {
|
|
|
1628 |
continue;
|
|
|
1629 |
}
|
|
|
1630 |
}
|
|
|
1631 |
|
|
|
1632 |
int purchasedValue;
|
|
|
1633 |
if (createOfferRequest.getTargetType().equals(AchievementType.VALUE)) {
|
|
|
1634 |
purchasedValue = totalBaseValue;
|
|
|
1635 |
} else {
|
|
|
1636 |
purchasedValue = totalBaseQty;
|
|
|
1637 |
}
|
|
|
1638 |
|
|
|
1639 |
PayoutSlab eligibleSlab = null;
|
|
|
1640 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
|
|
1641 |
if (payoutSlab.getOnwardsAmount() <= purchasedValue) {
|
|
|
1642 |
eligibleSlab = payoutSlab;
|
|
|
1643 |
} else {
|
|
|
1644 |
break;
|
|
|
1645 |
}
|
|
|
1646 |
}
|
|
|
1647 |
if (eligibleSlab == null) continue;
|
|
|
1648 |
|
|
|
1649 |
List<Integer> orderIds = orders.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
1650 |
Map<Integer, LineItem> lineItemMap = orders.stream().collect(Collectors.toMap(x -> x.getLineItem().getId(), x -> x.getLineItem()));
|
|
|
1651 |
List<WarehouseScan> warehouseScans = warehouseScanRepository.selectAllByOrderIds(orderIds);
|
|
|
1652 |
List<Integer> warehouseInventoryItemIds = warehouseScans.stream().collect(Collectors.groupingBy(x -> x.getInventoryItemId())).entrySet()
|
|
|
1653 |
.stream().filter(x -> x.getValue().size() == 1).map(x -> x.getKey()).collect(Collectors.toList());
|
|
|
1654 |
|
|
|
1655 |
List<OfferPayout> partnerPayouts = offerPayouts.stream().filter(x -> x.getFofoId() == fofoId).collect(Collectors.toList());
|
|
|
1656 |
Set<String> serialNumbersRejected = partnerPayouts.stream().filter(x -> x.getStatus().equals(SchemePayoutStatus.REJECTED)).map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
|
|
1657 |
|
|
|
1658 |
Set<String> billedNotCancelledSerialNumbers;
|
|
|
1659 |
if (warehouseInventoryItemIds.size() > 0) {
|
|
|
1660 |
List<WarehouseInventoryItem> warehouseInventoryItems = warehouseInventoryItemRepository.selectAllByIds(warehouseInventoryItemIds);
|
|
|
1661 |
billedNotCancelledSerialNumbers = warehouseInventoryItems.stream().map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
|
|
1662 |
} else {
|
|
|
1663 |
billedNotCancelledSerialNumbers = new HashSet<>();
|
|
|
1664 |
}
|
|
|
1665 |
|
|
|
1666 |
Map<String, LineItem> serialNumberMap = lineItemImeisRepository.selectByLineItemIds(new ArrayList<>(lineItemMap.keySet()))
|
|
|
1667 |
.stream().filter(x -> !serialNumbersRejected.contains(x.getSerialNumber()))
|
|
|
1668 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> lineItemMap.get(x.getLineItemId()), (x1, x2) -> x1.getId() > x2.getId() ? x1 : x2));
|
|
|
1669 |
|
|
|
1670 |
List<String> manuallyEligibleImeis = offerEligibleImeiRepository.selectEligibleImeisByOfferId(createOfferRequest.getId());
|
|
|
1671 |
|
|
|
1672 |
serialNumberMap = serialNumberMap.entrySet().stream().filter(x -> billedNotCancelledSerialNumbers.contains(x.getKey()))
|
|
|
1673 |
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
1674 |
|
|
|
1675 |
Map<String, InventoryItem> serialNumberInventoryItemMap = this.getInventoryItemMap(fofoId, serialNumberMap);
|
|
|
1676 |
if (serialNumberInventoryItemMap.size() < billedNotCancelledSerialNumbers.size()) continue;
|
|
|
1677 |
|
|
|
1678 |
if (manuallyEligibleImeis != null) {
|
|
|
1679 |
serialNumberMap = serialNumberMap.entrySet().stream().filter(x -> manuallyEligibleImeis.contains(x.getKey()))
|
|
|
1680 |
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
1681 |
if (serialNumberMap.size() == 0) continue;
|
|
|
1682 |
serialNumberInventoryItemMap = this.getInventoryItemMap(fofoId, serialNumberMap);
|
|
|
1683 |
}
|
|
|
1684 |
if (serialNumberMap.size() == 0) continue;
|
|
|
1685 |
|
|
|
1686 |
float eligiblePayoutValue = eligibleSlab.getPayoutAmount();
|
|
|
1687 |
double finalPayout;
|
|
|
1688 |
if (AmountType.PERCENTAGE.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1689 |
finalPayout = (eligiblePayoutValue * purchasedValue) / 100;
|
|
|
1690 |
} else if (AmountType.FIXED.equals(itemCriteriaPayout.getAmountType())) {
|
|
|
1691 |
finalPayout = eligiblePayoutValue * totalQty;
|
|
|
1692 |
} else {
|
|
|
1693 |
Map<Integer, Double> fofoCriteriaPayoutMap = fixedSlabCriteriaPartnerMap.get(criteriaId);
|
|
|
1694 |
finalPayout = eligiblePayoutValue;
|
|
|
1695 |
if (fofoCriteriaPayoutMap != null && fofoCriteriaPayoutMap.containsKey(fofoId)) {
|
|
|
1696 |
finalPayout = eligiblePayoutValue - fofoCriteriaPayoutMap.get(fofoId);
|
|
|
1697 |
if (Math.abs(finalPayout) < Utils.DOUBLE_EPSILON) {
|
|
|
1698 |
finalPayout = 0;
|
|
|
1699 |
}
|
|
|
1700 |
}
|
|
|
1701 |
}
|
|
|
1702 |
|
|
|
1703 |
if (finalPayout > 0) {
|
|
|
1704 |
result.add(new SellinPartnerPayoutData(
|
|
|
1705 |
fofoId, criteriaId, finalPayout, eligiblePayoutValue,
|
|
|
1706 |
serialNumberInventoryItemMap, itemCriteriaPayout,
|
|
|
1707 |
serialNumberPaid, createOfferRequest.isDiscount()));
|
|
|
1708 |
}
|
|
|
1709 |
}
|
|
|
1710 |
}
|
|
|
1711 |
|
|
|
1712 |
return result;
|
|
|
1713 |
}
|
|
|
1714 |
|
| 33441 |
amit.gupta |
1715 |
private void rollbackPayout(int fofoId, int offerId, List<
|
|
|
1716 |
OfferPayout> partnerPayouts, Set<String> returnedSerialNumbers) throws ProfitMandiBusinessException {
|
| 32795 |
amit.gupta |
1717 |
LOGGER.info("Rollback payout called for offerId {}", offerId);
|
|
|
1718 |
float amountToRollback = 0;
|
|
|
1719 |
int totalImeis = 0;
|
|
|
1720 |
for (OfferPayout partnerPayout : partnerPayouts) {
|
|
|
1721 |
LOGGER.info("serialNumberInventoryItemMap - {}", returnedSerialNumbers);
|
|
|
1722 |
LOGGER.info("Serial Number - {}", partnerPayout.getSerialNumber());
|
|
|
1723 |
if (returnedSerialNumbers.contains(partnerPayout.getSerialNumber()) && partnerPayout.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
|
|
1724 |
partnerPayout.setRejectTimestamp(LocalDateTime.now());
|
|
|
1725 |
partnerPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1726 |
amountToRollback += partnerPayout.getAmount();
|
|
|
1727 |
totalImeis += 1;
|
|
|
1728 |
}
|
|
|
1729 |
}
|
|
|
1730 |
if (amountToRollback > 0) {
|
|
|
1731 |
LOGGER.info("Amount to Rollback - {}", amountToRollback);
|
|
|
1732 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, offerId, WalletReferenceType.ADDITIONAL_SCHEME, "Stock Returned, total " + totalImeis + "pc(s)", LocalDateTime.now());
|
|
|
1733 |
}
|
|
|
1734 |
}
|
|
|
1735 |
|
| 33441 |
amit.gupta |
1736 |
private Map<String, InventoryItem> getInventoryItemMap(int fofoId, Map<String, LineItem> serialNumberMap) throws
|
|
|
1737 |
ProfitMandiBusinessException {
|
| 36337 |
amit |
1738 |
if (serialNumberMap.isEmpty()) {
|
|
|
1739 |
return new HashMap<>();
|
|
|
1740 |
}
|
| 32064 |
amit.gupta |
1741 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectBySerialNumbers(serialNumberMap.keySet());
|
| 32345 |
amit.gupta |
1742 |
inventoryItems = inventoryItems.stream().filter(x -> x.getFofoId() == fofoId).collect(Collectors.toList());
|
|
|
1743 |
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 |
1744 |
return inventoryItemMap;
|
|
|
1745 |
}
|
| 33030 |
amit.gupta |
1746 |
|
|
|
1747 |
|
|
|
1748 |
@Override
|
| 33247 |
ranu |
1749 |
public String getPartnerCriteriaString(PartnerCriteria partnerCriteria) throws ProfitMandiBusinessException {
|
| 33030 |
amit.gupta |
1750 |
StringBuilder sb = new StringBuilder();
|
|
|
1751 |
if (partnerCriteria.getFofoIds().size() > 0) {
|
|
|
1752 |
List<Integer> fofoIds = partnerCriteria.getFofoIds();
|
|
|
1753 |
appendPartnerCodes(fofoIds, sb);
|
|
|
1754 |
|
|
|
1755 |
} else {
|
|
|
1756 |
sb.append("All");
|
|
|
1757 |
if (partnerCriteria.getPartnerTypes().size() > 0) {
|
|
|
1758 |
sb.append(" ").append(String.join(", ", partnerCriteria.getPartnerTypes().stream().map(x -> x.getValue()).collect(Collectors.toList())));
|
|
|
1759 |
}
|
|
|
1760 |
sb.append(" partners ");
|
|
|
1761 |
if (partnerCriteria.getRegionIds().size() > 0) {
|
|
|
1762 |
sb.append("from ");
|
|
|
1763 |
sb.append(String.join(", ", partnerCriteria.getRegionIds().stream().map(x -> ProfitMandiConstants.WAREHOUSE_MAP.get(x)).collect(Collectors.toList())));
|
|
|
1764 |
}
|
|
|
1765 |
if (partnerCriteria.getExcludeFofoIds() != null && partnerCriteria.getExcludeFofoIds().size() > 0) {
|
| 35394 |
amit |
1766 |
sb.append("<b>excluding</b> ");
|
| 33030 |
amit.gupta |
1767 |
appendPartnerCodes(partnerCriteria.getExcludeFofoIds(), sb);
|
|
|
1768 |
}
|
|
|
1769 |
}
|
|
|
1770 |
return sb.toString();
|
|
|
1771 |
}
|
|
|
1772 |
|
| 33999 |
tejus.loha |
1773 |
@Override
|
|
|
1774 |
public ByteArrayOutputStream createCSVOfferReport(CreateOfferRequest createOfferRequest) throws Exception {
|
|
|
1775 |
List<List<?>> listOfRows = new ArrayList<>();
|
|
|
1776 |
ByteArrayOutputStream baos = null;
|
|
|
1777 |
Collection<OfferRowModel> offerRowModels = offerRepository.getOfferRows(createOfferRequest);
|
|
|
1778 |
ItemCriteriaPayout itemCriteriaPayout = createOfferRequest.getTargetSlabs().get(0).getItemCriteriaPayouts().get(0);
|
| 35394 |
amit |
1779 |
// Batch fetch all retailers once to avoid N+1 queries
|
|
|
1780 |
Map<Integer, CustomRetailer> allRetailers = retailerService.getAllFofoRetailers();
|
| 33999 |
tejus.loha |
1781 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
|
|
|
1782 |
for (OfferRowModel offerRowModel : offerRowModels) {
|
| 35394 |
amit |
1783 |
CustomRetailer customRetailer = allRetailers.get(offerRowModel.getFofoId());
|
| 33999 |
tejus.loha |
1784 |
listOfRows.add(Arrays.asList(
|
|
|
1785 |
createOfferRequest.getId()
|
|
|
1786 |
, createOfferRequest.getName()
|
|
|
1787 |
, createOfferRequest.getTargetType()
|
|
|
1788 |
, createOfferRequest.getSchemeType()
|
|
|
1789 |
, itemCriteriaPayout.getStartDate()
|
|
|
1790 |
, itemCriteriaPayout.getEndDate()
|
|
|
1791 |
// , createOfferRequest.getBrandShareTerms()
|
|
|
1792 |
, createOfferRequest.getSellinPercentage()
|
|
|
1793 |
, "--"
|
|
|
1794 |
, createOfferRequest.getItemCriteriaString()
|
|
|
1795 |
, createOfferRequest.getStartDate()
|
|
|
1796 |
, createOfferRequest.getEndDate()
|
|
|
1797 |
, createOfferRequest.getCreatedOn()
|
|
|
1798 |
, customRetailer.getPartnerId()
|
|
|
1799 |
, customRetailer.getBusinessName()
|
|
|
1800 |
, customRetailer.getCode()
|
|
|
1801 |
, offerRowModel.getTotalBasePurchaseValue()
|
|
|
1802 |
, offerRowModel.getAchievedTarget()
|
|
|
1803 |
, offerRowModel.getPayoutPurchaseValue()
|
|
|
1804 |
, offerRowModel.getPayout()
|
|
|
1805 |
, offerRowModel.getPayoutType()
|
|
|
1806 |
, offerRowModel.getFinalPayout()
|
|
|
1807 |
, String.join(", ", offerRowModel.getEligibleImeis())));
|
|
|
1808 |
}
|
|
|
1809 |
baos = FileUtil.getCSVByteStream(
|
|
|
1810 |
Arrays.asList(
|
|
|
1811 |
"Id"
|
|
|
1812 |
, "Name"
|
|
|
1813 |
, "Target Type"
|
|
|
1814 |
, "Scheme Type"
|
|
|
1815 |
, "Payout Start"
|
|
|
1816 |
, "Payout End"
|
|
|
1817 |
// , "Brand %"
|
|
|
1818 |
, "Sellin %"
|
|
|
1819 |
, "Partner Criteria"
|
|
|
1820 |
, "Item Criteria"
|
|
|
1821 |
, "Start"
|
|
|
1822 |
, "End"
|
|
|
1823 |
, "Created"
|
|
|
1824 |
, "Partner Id"
|
|
|
1825 |
, "Partner Name"
|
|
|
1826 |
, "Partner Code"
|
|
|
1827 |
, "Base Purchase"
|
|
|
1828 |
, "Achieved Target"
|
|
|
1829 |
, "Eligible Purchase"
|
|
|
1830 |
, "Slab Amount"
|
|
|
1831 |
, "Slab Amount Type"
|
|
|
1832 |
, "Payout Value(Rs.)"
|
|
|
1833 |
, "Eligible IMEIs"
|
|
|
1834 |
, "Billing Pending Imeis"
|
|
|
1835 |
), listOfRows);
|
|
|
1836 |
} else {
|
|
|
1837 |
for (OfferRowModel offerRowModel : offerRowModels) {
|
| 35394 |
amit |
1838 |
CustomRetailer customRetailer = allRetailers.get(offerRowModel.getFofoId());
|
| 33999 |
tejus.loha |
1839 |
listOfRows.add(Arrays.asList(
|
|
|
1840 |
createOfferRequest.getId()
|
|
|
1841 |
, createOfferRequest.getName()
|
|
|
1842 |
, createOfferRequest.getTargetType()
|
|
|
1843 |
, createOfferRequest.getSchemeType()
|
|
|
1844 |
, itemCriteriaPayout.getStartDate()
|
|
|
1845 |
, itemCriteriaPayout.getEndDate()
|
|
|
1846 |
// , createOfferRequest.getBrandShareTerms()
|
|
|
1847 |
, createOfferRequest.getSellinPercentage()
|
|
|
1848 |
, createOfferRequest.getPartnerCriteriaString()
|
|
|
1849 |
, createOfferRequest.getItemCriteriaString()
|
|
|
1850 |
, createOfferRequest.getStartDate()
|
|
|
1851 |
, createOfferRequest.getEndDate()
|
|
|
1852 |
, createOfferRequest.getCreatedOn()
|
|
|
1853 |
, customRetailer.getPartnerId()
|
|
|
1854 |
, customRetailer.getBusinessName()
|
|
|
1855 |
, customRetailer.getCode()
|
|
|
1856 |
, offerRowModel.getTotalSale()
|
|
|
1857 |
, offerRowModel.getEligibleSale()
|
|
|
1858 |
, offerRowModel.getPayoutTargetAchieved()
|
|
|
1859 |
, offerRowModel.getPayoutValue()
|
|
|
1860 |
, offerRowModel.getPayoutType()
|
|
|
1861 |
, offerRowModel.getPayoutValueDp()
|
|
|
1862 |
, offerRowModel.getFinalPayout()
|
|
|
1863 |
, String.join(", ", offerRowModel.getEligibleImeis())
|
|
|
1864 |
// ,String.join(", ", offerRowModel.getBaseCriteria())
|
|
|
1865 |
)
|
|
|
1866 |
);
|
|
|
1867 |
}
|
|
|
1868 |
baos = FileUtil.getCSVByteStream(
|
|
|
1869 |
Arrays.asList(
|
|
|
1870 |
"Id"
|
|
|
1871 |
, "Name"
|
|
|
1872 |
, "Target Type"
|
|
|
1873 |
, "Scheme Type"
|
|
|
1874 |
, "Payout Start"
|
|
|
1875 |
, "Payout End"
|
|
|
1876 |
// , "Brand %"
|
|
|
1877 |
, "Sellin %"
|
|
|
1878 |
, "Partner Criteria"
|
|
|
1879 |
, "Item Criteria"
|
|
|
1880 |
, "Start"
|
|
|
1881 |
, "End"
|
|
|
1882 |
, "Created"
|
|
|
1883 |
, "Partner Id"
|
|
|
1884 |
, "Partner Name"
|
|
|
1885 |
, "Partner Code"
|
|
|
1886 |
, "Total Sale"
|
|
|
1887 |
, "Eligible Sale"
|
|
|
1888 |
, "Payout Target Achieved"
|
|
|
1889 |
, "Payout Amount"
|
|
|
1890 |
, "Payout Amount Type"
|
|
|
1891 |
, "Payout Value DP"
|
|
|
1892 |
, "Amount to be credited"
|
|
|
1893 |
, "Eligible Activated Imeis"
|
|
|
1894 |
), listOfRows);
|
|
|
1895 |
}
|
|
|
1896 |
return baos;
|
|
|
1897 |
}
|
|
|
1898 |
|
| 35205 |
amit |
1899 |
@Override
|
|
|
1900 |
public void sendWhatsapp(Offer offer, List<Integer> fofoIds, String imageUrl) throws Exception {
|
|
|
1901 |
if (fofoIds == null) {
|
|
|
1902 |
fofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
1903 |
}
|
|
|
1904 |
final List<Integer> finalFofoIds = fofoIds;
|
|
|
1905 |
//List<String> mobileNumbers = retailerService.getAllFofoRetailers().entrySet().stream().filter(x -> finalFofoIds.contains(x.getKey())).map(x -> x.getValue().getMobileNumber()).collect(Collectors.toList());
|
|
|
1906 |
List<String> mobileNumbers = new ArrayList<>();
|
|
|
1907 |
mobileNumbers.add("9911565032");
|
|
|
1908 |
//mobileNumbers.add("9990381569");
|
|
|
1909 |
//mobileNumbers.add("8383849914");
|
|
|
1910 |
String message = "%s\n" +
|
|
|
1911 |
"On %s of select models\n" +
|
|
|
1912 |
"From %s to %s\n" +
|
|
|
1913 |
"\n" +
|
|
|
1914 |
"Happy Selling\n" +
|
|
|
1915 |
"Team Smartdukaan";
|
|
|
1916 |
//TV's mobile
|
|
|
1917 |
for (String mobileNumber : mobileNumbers) {
|
|
|
1918 |
notificationService.sendWhatsappMediaMessage(String.format(message, offer.getName(),
|
|
|
1919 |
offer.getSchemeType().toString(), FormattingUtils.formatDate(offer.getStartDate()),
|
|
|
1920 |
FormattingUtils.formatDate(offer.getEndDate())), mobileNumber, imageUrl, "offer-" + offer.getId() + ".png", WhatsappMessageType.IMAGE);
|
|
|
1921 |
}
|
|
|
1922 |
}
|
|
|
1923 |
|
| 33247 |
ranu |
1924 |
private void appendPartnerCodes(List<Integer> fofoIds, StringBuilder sb) throws ProfitMandiBusinessException {
|
| 33030 |
amit.gupta |
1925 |
Map<Integer, CustomRetailer> customRetailersMap = retailerService.getAllFofoRetailers();
|
| 33031 |
amit.gupta |
1926 |
List<String> businessNames = fofoIds.stream().map(x -> customRetailersMap.get(x)).filter(x -> x != null).map(x -> x.getBusinessName()).collect(Collectors.toList());
|
| 33030 |
amit.gupta |
1927 |
sb.append(String.join(", ", businessNames));
|
|
|
1928 |
}
|
| 35856 |
amit |
1929 |
|
|
|
1930 |
@Override
|
|
|
1931 |
public void deleteOffer(int offerId) throws ProfitMandiBusinessException {
|
|
|
1932 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
1933 |
if (offer == null) {
|
|
|
1934 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
|
|
1935 |
}
|
|
|
1936 |
if (offer.isActive()) {
|
|
|
1937 |
throw new ProfitMandiBusinessException("Cannot delete a published offer", "Cannot delete a published offer", "");
|
|
|
1938 |
}
|
|
|
1939 |
List<TargetSlabEntity> targetSlabs = offerTargetSlabRepository.selectByOfferIds(Collections.singletonList(offerId));
|
|
|
1940 |
for (TargetSlabEntity targetSlab : targetSlabs) {
|
|
|
1941 |
genericRepository.delete(targetSlab);
|
|
|
1942 |
}
|
|
|
1943 |
genericRepository.delete(offer);
|
|
|
1944 |
LOGGER.info("Deleted offer {} and {} target slabs", offerId, targetSlabs.size());
|
|
|
1945 |
}
|
|
|
1946 |
|
|
|
1947 |
@Override
|
|
|
1948 |
public List<Offer> publishAllUnpublished(YearMonth yearMonth) throws ProfitMandiBusinessException {
|
|
|
1949 |
List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
|
|
|
1950 |
List<Offer> unpublished = allOffers.stream().filter(o -> !o.isActive()).collect(Collectors.toList());
|
|
|
1951 |
for (Offer offer : unpublished) {
|
|
|
1952 |
offer.setActive(true);
|
|
|
1953 |
}
|
|
|
1954 |
LOGGER.info("Published {} unpublished offers for {}", unpublished.size(), yearMonth);
|
|
|
1955 |
return unpublished;
|
|
|
1956 |
}
|
| 35885 |
amit |
1957 |
|
|
|
1958 |
@Override
|
|
|
1959 |
public void removePartnersFromOffer(int offerId, List<Integer> fofoIds) throws ProfitMandiBusinessException {
|
|
|
1960 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
1961 |
if (offer == null) {
|
|
|
1962 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
|
|
1963 |
}
|
|
|
1964 |
PartnerCriteria partnerCriteria = gson.fromJson(offer.getPartnerCriteria(), PartnerCriteria.class);
|
|
|
1965 |
if (partnerCriteria.getFofoIds() != null) {
|
|
|
1966 |
partnerCriteria.getFofoIds().removeAll(fofoIds);
|
|
|
1967 |
}
|
|
|
1968 |
offer.setPartnerCriteria(gson.toJson(partnerCriteria));
|
|
|
1969 |
genericRepository.persist(offer);
|
|
|
1970 |
LOGGER.info("Removed partners {} from offer {}", fofoIds, offerId);
|
|
|
1971 |
}
|
|
|
1972 |
|
|
|
1973 |
@Override
|
|
|
1974 |
public void addPartnersToOffer(int offerId, List<Integer> fofoIds) throws ProfitMandiBusinessException {
|
|
|
1975 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
1976 |
if (offer == null) {
|
|
|
1977 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
|
|
1978 |
}
|
|
|
1979 |
PartnerCriteria partnerCriteria = gson.fromJson(offer.getPartnerCriteria(), PartnerCriteria.class);
|
|
|
1980 |
List<Integer> existingFofoIds = partnerCriteria.getFofoIds();
|
|
|
1981 |
if (existingFofoIds == null) {
|
|
|
1982 |
existingFofoIds = new ArrayList<>();
|
|
|
1983 |
}
|
|
|
1984 |
for (int fofoId : fofoIds) {
|
|
|
1985 |
if (!existingFofoIds.contains(fofoId)) {
|
|
|
1986 |
existingFofoIds.add(fofoId);
|
|
|
1987 |
}
|
|
|
1988 |
}
|
|
|
1989 |
partnerCriteria.setFofoIds(existingFofoIds);
|
|
|
1990 |
offer.setPartnerCriteria(gson.toJson(partnerCriteria));
|
|
|
1991 |
genericRepository.persist(offer);
|
|
|
1992 |
LOGGER.info("Added partners {} to offer {}", fofoIds, offerId);
|
|
|
1993 |
}
|
|
|
1994 |
|
|
|
1995 |
@Override
|
|
|
1996 |
public int cloneOfferForPartners(int sourceOfferId, List<Integer> fofoIds, List<Integer> targets) throws ProfitMandiBusinessException {
|
|
|
1997 |
Offer sourceOffer = offerRepository.selectById(sourceOfferId);
|
|
|
1998 |
if (sourceOffer == null) {
|
|
|
1999 |
throw new ProfitMandiBusinessException("Source offer not found", "Source offer not found", "");
|
|
|
2000 |
}
|
|
|
2001 |
CreateOfferRequest clonedRequest = this.getCreateOfferRequest(sourceOffer);
|
|
|
2002 |
clonedRequest.setId(0);
|
|
|
2003 |
clonedRequest.setActive(false);
|
|
|
2004 |
|
|
|
2005 |
PartnerCriteria newPartnerCriteria = new PartnerCriteria();
|
|
|
2006 |
newPartnerCriteria.setFofoIds(new ArrayList<>(fofoIds));
|
|
|
2007 |
newPartnerCriteria.setExcludeFofoIds(new ArrayList<>());
|
|
|
2008 |
newPartnerCriteria.setRegionIds(clonedRequest.getPartnerCriteria().getRegionIds() != null
|
|
|
2009 |
? new ArrayList<>(clonedRequest.getPartnerCriteria().getRegionIds()) : new ArrayList<>());
|
|
|
2010 |
newPartnerCriteria.setPartnerTypes(clonedRequest.getPartnerCriteria().getPartnerTypes() != null
|
|
|
2011 |
? new ArrayList<>(clonedRequest.getPartnerCriteria().getPartnerTypes()) : new ArrayList<>());
|
|
|
2012 |
clonedRequest.setPartnerCriteria(newPartnerCriteria);
|
|
|
2013 |
|
|
|
2014 |
if (targets != null && !targets.isEmpty() && !clonedRequest.getTargetSlabs().isEmpty()) {
|
|
|
2015 |
List<PayoutSlab> payoutSlabs = clonedRequest.getTargetSlabs().get(0).getItemCriteriaPayouts()
|
|
|
2016 |
.stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.toList());
|
|
|
2017 |
int counter = 0;
|
|
|
2018 |
for (PayoutSlab payoutSlab : payoutSlabs) {
|
|
|
2019 |
if (counter < targets.size()) {
|
|
|
2020 |
payoutSlab.setOnwardsAmount(targets.get(counter));
|
|
|
2021 |
counter++;
|
|
|
2022 |
}
|
|
|
2023 |
}
|
|
|
2024 |
}
|
|
|
2025 |
|
|
|
2026 |
this.addOfferService(clonedRequest);
|
|
|
2027 |
LOGGER.info("Cloned offer {} to new offer {} for partners {}", sourceOfferId, clonedRequest.getId(), fofoIds);
|
|
|
2028 |
return clonedRequest.getId();
|
|
|
2029 |
}
|
|
|
2030 |
|
|
|
2031 |
@Override
|
|
|
2032 |
public void updateOfferTargets(int offerId, List<Integer> newTargets) throws ProfitMandiBusinessException {
|
|
|
2033 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
2034 |
if (offer == null) {
|
|
|
2035 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
|
|
2036 |
}
|
|
|
2037 |
List<TargetSlabEntity> targetSlabs = offerTargetSlabRepository.selectEntitiesByOfferId(offerId);
|
|
|
2038 |
targetSlabs.sort(Comparator.comparing(TargetSlabEntity::getPayoutTarget));
|
|
|
2039 |
|
| 35889 |
amit |
2040 |
// Group slabs by unique payout_target (multiple item_criteria share the same target value)
|
|
|
2041 |
LinkedHashMap<Integer, List<TargetSlabEntity>> groupedByTarget = new LinkedHashMap<>();
|
|
|
2042 |
for (TargetSlabEntity slab : targetSlabs) {
|
|
|
2043 |
groupedByTarget.computeIfAbsent(slab.getPayoutTarget(), k -> new ArrayList<>()).add(slab);
|
|
|
2044 |
}
|
|
|
2045 |
List<Integer> uniqueTargets = new ArrayList<>(groupedByTarget.keySet());
|
|
|
2046 |
|
|
|
2047 |
if (newTargets.size() != uniqueTargets.size()) {
|
| 35885 |
amit |
2048 |
throw new ProfitMandiBusinessException(
|
| 35889 |
amit |
2049 |
"Target count mismatch. Expected " + uniqueTargets.size() + " but got " + newTargets.size(),
|
| 35885 |
amit |
2050 |
"Target count mismatch", "");
|
|
|
2051 |
}
|
| 35889 |
amit |
2052 |
|
|
|
2053 |
// Update each group: all slabs sharing the same old target get the new target value
|
|
|
2054 |
for (int i = 0; i < uniqueTargets.size(); i++) {
|
|
|
2055 |
int oldTarget = uniqueTargets.get(i);
|
|
|
2056 |
int newTarget = newTargets.get(i);
|
|
|
2057 |
if (oldTarget != newTarget) {
|
|
|
2058 |
for (TargetSlabEntity slab : groupedByTarget.get(oldTarget)) {
|
|
|
2059 |
genericRepository.updateById(TargetSlabEntity.class, "payoutTarget", newTarget, slab.getId());
|
|
|
2060 |
}
|
|
|
2061 |
}
|
| 35885 |
amit |
2062 |
}
|
|
|
2063 |
LOGGER.info("Updated targets for offer {} to {}", offerId, newTargets);
|
|
|
2064 |
}
|
| 35924 |
amit |
2065 |
|
|
|
2066 |
@Override
|
|
|
2067 |
public void updateOfferSlabs(com.spice.profitmandi.dao.model.UpdateOfferSlabsRequest request) throws ProfitMandiBusinessException {
|
|
|
2068 |
Offer offer = offerRepository.selectById(request.getOfferId());
|
|
|
2069 |
if (offer == null) {
|
|
|
2070 |
throw new ProfitMandiBusinessException("Offer not found", "Offer not found", "");
|
|
|
2071 |
}
|
|
|
2072 |
|
|
|
2073 |
List<TargetSlabEntity> existingSlabs = offerTargetSlabRepository.selectEntitiesByOfferId(request.getOfferId());
|
|
|
2074 |
Set<Integer> validSlabIds = existingSlabs.stream().map(TargetSlabEntity::getId).collect(Collectors.toSet());
|
|
|
2075 |
|
|
|
2076 |
for (com.spice.profitmandi.dao.model.UpdateSlabRequest slab : request.getSlabs()) {
|
|
|
2077 |
if (!validSlabIds.contains(slab.getId())) {
|
|
|
2078 |
throw new ProfitMandiBusinessException("Invalid slab ID " + slab.getId(),
|
|
|
2079 |
"Slab does not belong to offer " + request.getOfferId(), "");
|
|
|
2080 |
}
|
|
|
2081 |
genericRepository.updateById(TargetSlabEntity.class, "payoutTarget", slab.getPayoutTarget(), slab.getId());
|
|
|
2082 |
genericRepository.updateById(TargetSlabEntity.class, "payoutValue", slab.getPayoutValue(), slab.getId());
|
|
|
2083 |
}
|
|
|
2084 |
LOGGER.info("Updated slabs for offer {}: {}", request.getOfferId(), request.getSlabs());
|
|
|
2085 |
}
|
| 27877 |
amit.gupta |
2086 |
}
|