| Line 17... |
Line 17... |
| 17 |
import com.spice.profitmandi.dao.repository.scratch.SeasonalOfferRepository;
|
17 |
import com.spice.profitmandi.dao.repository.scratch.SeasonalOfferRepository;
|
| 18 |
import org.apache.logging.log4j.LogManager;
|
18 |
import org.apache.logging.log4j.LogManager;
|
| 19 |
import org.apache.logging.log4j.Logger;
|
19 |
import org.apache.logging.log4j.Logger;
|
| 20 |
import org.springframework.beans.factory.annotation.Autowired;
|
20 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21 |
import org.springframework.stereotype.Service;
|
21 |
import org.springframework.stereotype.Service;
|
| 22 |
import org.springframework.transaction.annotation.Propagation;
|
- |
|
| 23 |
import org.springframework.transaction.annotation.Transactional;
|
22 |
import org.springframework.transaction.annotation.Transactional;
|
| 24 |
|
23 |
|
| 25 |
import java.time.LocalDate;
|
24 |
import java.time.LocalDate;
|
| 26 |
import java.time.LocalDateTime;
|
25 |
import java.time.LocalDateTime;
|
| 27 |
import java.time.LocalTime;
|
26 |
import java.time.LocalTime;
|
| Line 45... |
Line 44... |
| 45 |
ItemRepository itemRepository;
|
44 |
ItemRepository itemRepository;
|
| 46 |
@Autowired
|
45 |
@Autowired
|
| 47 |
FofoOrderRepository fofoOrderRepository;
|
46 |
FofoOrderRepository fofoOrderRepository;
|
| 48 |
|
47 |
|
| 49 |
|
48 |
|
| 50 |
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
- |
|
| 51 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<Integer> fofoOrderItemIds) throws ProfitMandiBusinessException {
|
49 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<Integer> fofoOrderItemIds) throws ProfitMandiBusinessException {
|
| 52 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
50 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
| 53 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
51 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
| 54 |
LocalDate today = LocalDate.now();
|
52 |
LocalDate today = LocalDate.now();
|
| 55 |
for (Integer orderItemId : fofoOrderItemIds) {
|
53 |
for (Integer orderItemId : fofoOrderItemIds) {
|
| Line 63... |
Line 61... |
| 63 |
OfferEntity eligibleOffer = FindEligibleOffer(activeOffers, fofoOrder);
|
61 |
OfferEntity eligibleOffer = FindEligibleOffer(activeOffers, fofoOrder);
|
| 64 |
// Set<String> allowedPaymentMethods = Arrays.stream(eligibleOffer.getPaymentMethod().split(","))
|
62 |
// Set<String> allowedPaymentMethods = Arrays.stream(eligibleOffer.getPaymentMethod().split(","))
|
| 65 |
// .collect(Collectors.toSet());
|
63 |
// .collect(Collectors.toSet());
|
| 66 |
//
|
64 |
//
|
| 67 |
// boolean isOtherPaymentUsed = paymentOptions.stream()
|
65 |
// boolean isOtherPaymentUsed = paymentOptions.stream()
|
| 68 |
// .map(option -> String.valueOf(option.getPaymentOptionId())) // Replace with the correct getter
|
66 |
// .map(option -> String.valueOf(option.getPaymentOptionId()))
|
| 69 |
// .anyMatch(paymentId -> !allowedPaymentMethods.contains(paymentId));
|
67 |
// .anyMatch(paymentId -> !allowedPaymentMethods.contains(paymentId));
|
| 70 |
// // proceed with default because user payment method doest not match
|
68 |
// // proceed with default because user payment method doest not match
|
| 71 |
// if (isOtherPaymentUsed) {
|
69 |
// if (isOtherPaymentUsed) {
|
| 72 |
// logger.info("Customer {} has chosen a different mode of payment.", fofoOrder.getCustomerId());
|
70 |
// logger.info("Customer {} has chosen a different mode of payment.", fofoOrder.getCustomerId());
|
| 73 |
// return;
|
71 |
// return;
|
| Line 205... |
Line 203... |
| 205 |
}
|
203 |
}
|
| 206 |
}
|
204 |
}
|
| 207 |
return false;
|
205 |
return false;
|
| 208 |
}
|
206 |
}
|
| 209 |
private boolean processOffer(OfferEntity offer, FofoOrder fofoOrder, Integer itemCategoryId) {
|
207 |
private boolean processOffer(OfferEntity offer, FofoOrder fofoOrder, Integer itemCategoryId) {
|
| 210 |
List<GiftEntity> allGifts = giftRepository.findByOfferId(offer.getId());
|
208 |
List<GiftEntity> allGifts = giftRepository.findNonDefaultGIft(offer.getId());
|
| 211 |
logger.info("Processing offer {} for order {}", offer.getId(), fofoOrder.getId());
|
209 |
logger.info("Processing offer {} for order {}", offer.getId(), fofoOrder.getId());
|
| 212 |
if (isAppleItem(fofoOrder)) {
|
210 |
if (isAppleItem(fofoOrder)) {
|
| 213 |
GiftEntity lastGift = allGifts.get(allGifts.size() - 1);
|
211 |
GiftEntity lastGift = allGifts.get(allGifts.size() - 1);
|
| 214 |
if (lastGift.getMaxRedemptions() > 0) {
|
212 |
if (lastGift.getMaxRedemptions() > 0) {
|
| 215 |
createScratchOffer(fofoOrder, offer, lastGift);
|
213 |
createScratchOffer(fofoOrder, offer, lastGift);
|
| Line 229... |
Line 227... |
| 229 |
logger.warn("Offer {} has no gifts - cannot process scratch offer", offer.getId());
|
227 |
logger.warn("Offer {} has no gifts - cannot process scratch offer", offer.getId());
|
| 230 |
return false;
|
228 |
return false;
|
| 231 |
}
|
229 |
}
|
| 232 |
if (!isMoreGiftAvailable) {
|
230 |
if (!isMoreGiftAvailable) {
|
| 233 |
logger.info("Daily limit reached - assigning default gift");
|
231 |
logger.info("Daily limit reached - assigning default gift");
|
| 234 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
232 |
createDefaultGiftScratch(fofoOrder, offer, giftRepository.getDefaultGift(offer.getId()));
|
| 235 |
return true;
|
233 |
return true;
|
| 236 |
}
|
234 |
}
|
| 237 |
// Get eligible gifts
|
235 |
// Get eligible gifts
|
| 238 |
List<GiftEntity> eligibleGifts = findEligibleGifts(offer, fofoOrder, itemCategoryId);
|
236 |
List<GiftEntity> eligibleGifts = findEligibleGifts(offer, fofoOrder, itemCategoryId);
|
| 239 |
if (eligibleGifts.isEmpty()) {
|
237 |
if (eligibleGifts.isEmpty()) {
|
| 240 |
logger.info("No eligible gifts found for offer: {}", offer.getId());
|
238 |
logger.info("No eligible gifts found for offer: {}", offer.getId());
|
| 241 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
239 |
createDefaultGiftScratch(fofoOrder, offer, giftRepository.getDefaultGift(offer.getId()));
|
| 242 |
return true;
|
240 |
return true;
|
| 243 |
}
|
241 |
}
|
| 244 |
|
242 |
|
| 245 |
logger.info("Found {} eligible gifts for offer {}", eligibleGifts.size(), offer.getId());
|
243 |
logger.info("Found {} eligible gifts for offer {}", eligibleGifts.size(), offer.getId());
|
| 246 |
|
244 |
|
| Line 268... |
Line 266... |
| 268 |
logger.info("Total gifts for offer {}: {}", offer.getId(), activeOfferGifts.size());
|
266 |
logger.info("Total gifts for offer {}: {}", offer.getId(), activeOfferGifts.size());
|
| 269 |
|
267 |
|
| 270 |
float totalAmount = fofoOrder.getTotalAmount();
|
268 |
float totalAmount = fofoOrder.getTotalAmount();
|
| 271 |
logger.info("Order total amount: {}", totalAmount);
|
269 |
logger.info("Order total amount: {}", totalAmount);
|
| 272 |
|
270 |
|
| 273 |
// Filter eligible gifts
|
- |
|
| - |
|
271 |
|
| 274 |
List<GiftEntity> eligibleGifts = activeOfferGifts.stream().filter(gift -> isGiftEligible(gift, totalAmount, itemCategoryId)).collect(Collectors.toList());
|
272 |
List<GiftEntity> eligibleGifts = activeOfferGifts.stream().filter(gift -> isGiftEligible(gift, totalAmount, itemCategoryId)).collect(Collectors.toList());
|
| 275 |
|
273 |
|
| 276 |
logger.info("Filtered to {} eligible gifts", eligibleGifts.size());
|
274 |
logger.info("Filtered to {} eligible gifts", eligibleGifts.size());
|
| 277 |
|
275 |
|
| 278 |
for (GiftEntity gift : activeOfferGifts) {
|
276 |
for (GiftEntity gift : activeOfferGifts) {
|
| Line 400... |
Line 398... |
| 400 |
// TODO: Add brand-specific logic
|
398 |
// TODO: Add brand-specific logic
|
| 401 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
399 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
| 402 |
return null;
|
400 |
return null;
|
| 403 |
}
|
401 |
}
|
| 404 |
|
402 |
|
| 405 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
- |
|
| 406 |
public void createBetterLuckNextTimeScratch(FofoOrder order, OfferEntity offer) {
|
403 |
public void createBetterLuckNextTimeScratch(FofoOrder order, OfferEntity offer) {
|
| 407 |
try {
|
404 |
try {
|
| 408 |
ScratchOffer so = new ScratchOffer();
|
405 |
ScratchOffer so = new ScratchOffer();
|
| 409 |
so.setCustomerId(order.getCustomerId());
|
406 |
so.setCustomerId(order.getCustomerId());
|
| 410 |
so.setOfferId(offer.getId());
|
407 |
so.setOfferId(offer.getId());
|
| Line 418... |
Line 415... |
| 418 |
} catch (Exception e) {
|
415 |
} catch (Exception e) {
|
| 419 |
logger.error("Error creating Better Luck Next Time scratch offer: {}", e.getMessage());
|
416 |
logger.error("Error creating Better Luck Next Time scratch offer: {}", e.getMessage());
|
| 420 |
}
|
417 |
}
|
| 421 |
}
|
418 |
}
|
| 422 |
|
419 |
|
| 423 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
- |
|
| 424 |
public void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
420 |
public void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
| 425 |
try {
|
421 |
try {
|
| 426 |
GiftEntity currentGift = giftRepository.findById(gift.getId());
|
422 |
GiftEntity currentGift = giftRepository.findById(gift.getId());
|
| 427 |
|
423 |
|
| 428 |
if (currentGift == null || currentGift.getMaxRedemptions() <= 0) {
|
424 |
if (currentGift == null || currentGift.getMaxRedemptions() <= 0) {
|
| 429 |
logger.warn("Gift {} has no remaining redemptions", gift.getId());
|
425 |
logger.warn("Gift {} has no remaining redemptions", gift.getId());
|
| 430 |
return;
|
426 |
return;
|
| 431 |
}
|
427 |
}
|
| 432 |
|
428 |
|
| 433 |
// Decrement and save
|
- |
|
| 434 |
currentGift.setMaxRedemptions(currentGift.getMaxRedemptions() - 1);
|
429 |
currentGift.setMaxRedemptions(currentGift.getMaxRedemptions() - 1);
|
| 435 |
giftRepository.save(currentGift);
|
430 |
giftRepository.save(currentGift);
|
| 436 |
|
431 |
|
| 437 |
ScratchOffer so = new ScratchOffer();
|
432 |
ScratchOffer so = new ScratchOffer();
|
| 438 |
so.setCustomerId(order.getCustomerId());
|
433 |
so.setCustomerId(order.getCustomerId());
|
| Line 449... |
Line 444... |
| 449 |
logger.error("Error creating scratch offer: {}", e.getMessage());
|
444 |
logger.error("Error creating scratch offer: {}", e.getMessage());
|
| 450 |
|
445 |
|
| 451 |
}
|
446 |
}
|
| 452 |
}
|
447 |
}
|
| 453 |
|
448 |
|
| - |
|
449 |
public void createDefaultGiftScratch(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
| - |
|
450 |
try {
|
| - |
|
451 |
ScratchOffer so = new ScratchOffer();
|
| - |
|
452 |
so.setCustomerId(order.getCustomerId());
|
| - |
|
453 |
so.setOfferId(offer.getId());
|
| - |
|
454 |
so.setGiftId(gift.getId());
|
| - |
|
455 |
so.setInvoiceNumber(order.getInvoiceNumber());
|
| - |
|
456 |
so.setCreatedTimestamp(LocalDateTime.now());
|
| - |
|
457 |
so.setOfferName(gift.getName());
|
| - |
|
458 |
|
| - |
|
459 |
scratchOfferRepository.persist(so);
|
| - |
|
460 |
logger.info("Created default (unlimited) scratch offer: {}", so);
|
| - |
|
461 |
} catch (Exception e) {
|
| - |
|
462 |
logger.error("Error creating default scratch offer: {}", e.getMessage());
|
| - |
|
463 |
}
|
| - |
|
464 |
}
|
| 454 |
|
465 |
|
| 455 |
|
466 |
|
| 456 |
public boolean createOffer(ScratchRequest request) {
|
467 |
public boolean createOffer(ScratchRequest request) {
|
| 457 |
OfferEntity offer = mapToOfferEntity(request);
|
468 |
OfferEntity offer = mapToOfferEntity(request);
|
| 458 |
seasonalOfferRepository.save(offer);
|
469 |
seasonalOfferRepository.save(offer);
|
| Line 503... |
Line 514... |
| 503 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
514 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
| 504 |
gift.setOfferId(offerId);
|
515 |
gift.setOfferId(offerId);
|
| 505 |
gift.setMaxCartValue(giftData.getMaxCartValue());
|
516 |
gift.setMaxCartValue(giftData.getMaxCartValue());
|
| 506 |
gift.setFofoStore(giftData.getFofoStore());
|
517 |
gift.setFofoStore(giftData.getFofoStore());
|
| 507 |
gift.setProductCategory(giftData.getProductCategory());
|
518 |
gift.setProductCategory(giftData.getProductCategory());
|
| - |
|
519 |
gift.setIsDefault(giftData.isDefault());
|
| 508 |
giftRepository.save(gift);
|
520 |
giftRepository.save(gift);
|
| 509 |
}
|
521 |
}
|
| 510 |
}
|
522 |
}
|
| 511 |
}
|
523 |
}
|
| 512 |
|
524 |
|
| Line 516... |
Line 528... |
| 516 |
gift.setMinCartValue(data.getMinCartValue());
|
528 |
gift.setMinCartValue(data.getMinCartValue());
|
| 517 |
gift.setMaxRedemptions(data.getMaxRedemptions());
|
529 |
gift.setMaxRedemptions(data.getMaxRedemptions());
|
| 518 |
gift.setMaxCartValue(data.getMaxCartValue());
|
530 |
gift.setMaxCartValue(data.getMaxCartValue());
|
| 519 |
gift.setFofoStore(data.getFofoStore());
|
531 |
gift.setFofoStore(data.getFofoStore());
|
| 520 |
gift.setProductCategory(data.getProductCategory());
|
532 |
gift.setProductCategory(data.getProductCategory());
|
| - |
|
533 |
gift.setIsDefault(data.isDefault());
|
| 521 |
}
|
534 |
}
|
| 522 |
|
535 |
|
| 523 |
//offer
|
536 |
//offer
|
| 524 |
private OfferEntity mapToOfferEntity(ScratchRequest request) {
|
537 |
private OfferEntity mapToOfferEntity(ScratchRequest request) {
|
| 525 |
OfferEntity offer = new OfferEntity();
|
538 |
OfferEntity offer = new OfferEntity();
|
| Line 551... |
Line 564... |
| 551 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
564 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
| 552 |
gift.setOfferId(offerId);
|
565 |
gift.setOfferId(offerId);
|
| 553 |
gift.setMaxCartValue(giftData.getMaxCartValue());
|
566 |
gift.setMaxCartValue(giftData.getMaxCartValue());
|
| 554 |
gift.setFofoStore(giftData.getFofoStore());
|
567 |
gift.setFofoStore(giftData.getFofoStore());
|
| 555 |
gift.setProductCategory(giftData.getProductCategory());
|
568 |
gift.setProductCategory(giftData.getProductCategory());
|
| - |
|
569 |
gift.setIsDefault(giftData.isDefault());
|
| 556 |
return gift;
|
570 |
return gift;
|
| 557 |
}).collect(Collectors.toList());
|
571 |
}).collect(Collectors.toList());
|
| 558 |
}
|
572 |
}
|
| 559 |
|
573 |
|
| 560 |
}
|
574 |
}
|
| 561 |
|
575 |
|