Skip to content
Extraits de code Groupes Projets
Valider 1f825fc4 rédigé par chaimaa hassoune's avatar chaimaa hassoune
Parcourir les fichiers

Merge branch 'develop' into 'feature/MYD-655'

# Conflicts:
#   CHANGELOG.md
1 requête de fusion!76calculate THEORETICAL_TURNOVER
Pipeline #8310 réussi avec les étapes
in 1 minute et 3 secondes
......@@ -45,12 +45,23 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
return cartDTO;
}
List<PromoCodeDTO> promoCodeDTOList = externalApiService.getPromoCodesByIds(cartDTO.getPromoCodeIds());
boolean containsOnlyGiftCards = cartDTO.getItems().stream()
.allMatch(item -> item.getProductType() == ProductType.GIFT_CARD);
if (containsOnlyGiftCards) {
logger.info("Cart contains only Gift Cards. No promo code will be applied.");
return cartDTO;
}
List<PromoCodeDTO> promoCodeDTOList = externalApiService.getPromoCodesByIds(cartDTO.getPromoCodeIds());
double finalTotalCartDiscount = 0.0;
for (PromoCodeDTO promoCodeDTO : promoCodeDTOList) {
long acceptedItemsCount = cartDTO.getItems().stream()
.filter(item -> isItemIncludedOrExcluded(item, promoCodeDTO))
.count();
double promoCodeTotalCartDiscount = 0.0;
switch (promoCodeDTO.getPromoCodeType()) {
......@@ -58,11 +69,13 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
promoCodeTotalCartDiscount = applyDiscountPerCart(promoCodeDTO, cartDTO);
if (promoCodeTotalCartDiscount > 0) {
for (ItemCartDTO item : cartDTO.getItems()) {
double itemDiscount = promoCodeTotalCartDiscount / cartDTO.getItems().size();
if (itemDiscount > 0) {
double newTotalPrice = item.getTotalPrice() - itemDiscount;
item.setTotalPrice(Math.max(newTotalPrice, 0.0));
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
if (item.getProductType() != ProductType.GIFT_CARD) {
double itemDiscount = promoCodeTotalCartDiscount / acceptedItemsCount;
if (itemDiscount > 0) {
double newTotalPrice = item.getTotalPrice() - itemDiscount;
item.setTotalPrice(Math.max(newTotalPrice, 0.0));
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
}
}
}
}
......@@ -71,11 +84,13 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
promoCodeTotalCartDiscount = applyPercentagePerCart(promoCodeDTO, cartDTO);
if (promoCodeTotalCartDiscount > 0) {
for (ItemCartDTO item : cartDTO.getItems()) {
double itemDiscount = promoCodeTotalCartDiscount / cartDTO.getItems().size();
if (itemDiscount > 0) {
double newTotalPrice = item.getTotalPrice() - itemDiscount;
item.setTotalPrice(Math.max(newTotalPrice, 0.0));
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
if (item.getProductType() != ProductType.GIFT_CARD) {
double itemDiscount = promoCodeTotalCartDiscount / acceptedItemsCount;
if (itemDiscount > 0) {
double newTotalPrice = item.getTotalPrice() - itemDiscount;
item.setTotalPrice(Math.max(newTotalPrice, 0.0));
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
}
}
}
}
......@@ -139,6 +154,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
@Override
public boolean isItemIncludedOrExcluded(ItemCartDTO item, PromoCodeDTO promoCodeDTO) {
if (item.getProductType() == ProductType.GIFT_CARD) {
logger.info("Item with ID {} is a gift card and excluded from promo code.", item.getProductId());
return false;
}
boolean isExcluded = false;
boolean isIncluded = false;
......@@ -229,10 +249,12 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
totalPriceAfterDiscount = totalPriceBeforeDiscount - discount;
if (discount > 0) {
for (ItemCartDTO item : cart.getItems()) {
double itemDiscount = discount / cart.getItems().size();
item.setTotalPrice(item.getTotalPrice() - itemDiscount);
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
totalPriceAfterDiscount += itemDiscount;
if (!(item.getProductType().equals(ProductType.GIFT_CARD))) {
double itemDiscount = discount / cart.getItems().size();
item.setTotalPrice(item.getTotalPrice() - itemDiscount);
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
totalPriceAfterDiscount += itemDiscount;
}
}
}
}
......@@ -241,10 +263,12 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
totalPriceAfterDiscount = totalPriceBeforeDiscount - discount;
if (discount > 0) {
for (ItemCartDTO item : cart.getItems()) {
double itemDiscount = discount / cart.getItems().size();
item.setTotalPrice(item.getTotalPrice() - itemDiscount);
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
totalPriceAfterDiscount += itemDiscount;
if (!(item.getProductType().equals(ProductType.GIFT_CARD))) {
double itemDiscount = discount / cart.getItems().size();
item.setTotalPrice(item.getTotalPrice() - itemDiscount);
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
totalPriceAfterDiscount += itemDiscount;
}
}
}
}
......@@ -274,6 +298,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
@Override
public double applyDiscountPerProduct(PromoCodeDTO promoCodeDTO, ItemCartDTO item) {
if (item.getProductType() == ProductType.GIFT_CARD) {
logger.info("Item with ID {} is a gift card and excluded from discount.", item.getProductId());
return 0.0;
}
if (!isItemIncludedOrExcluded(item, promoCodeDTO)) {
logger.info("Item with ID {} is not eligible for discount.", item.getProductId());
return 0.0;
......@@ -298,6 +327,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
@Override
public double applyDiscountPerCart(PromoCodeDTO promoCodeDTO, CartDTO cart) {
if (cart.getItems().stream().anyMatch(item -> item.getProductType() == ProductType.GIFT_CARD)) {
logger.info("Cart contains gift cards, which are excluded from discount.");
return 0.0;
}
if (promoCodeDTO.getMinAmount().compareTo(BigDecimal.valueOf(cart.getTotalPrice()).setScale(2, RoundingMode.HALF_UP)) > 0) {
logger.info("Cart total does not meet the minimum required amount for discount.");
return 0.0;
......
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter