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 { ...@@ -45,12 +45,23 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
return cartDTO; 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; double finalTotalCartDiscount = 0.0;
for (PromoCodeDTO promoCodeDTO : promoCodeDTOList) { for (PromoCodeDTO promoCodeDTO : promoCodeDTOList) {
long acceptedItemsCount = cartDTO.getItems().stream()
.filter(item -> isItemIncludedOrExcluded(item, promoCodeDTO))
.count();
double promoCodeTotalCartDiscount = 0.0; double promoCodeTotalCartDiscount = 0.0;
switch (promoCodeDTO.getPromoCodeType()) { switch (promoCodeDTO.getPromoCodeType()) {
...@@ -58,11 +69,13 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -58,11 +69,13 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
promoCodeTotalCartDiscount = applyDiscountPerCart(promoCodeDTO, cartDTO); promoCodeTotalCartDiscount = applyDiscountPerCart(promoCodeDTO, cartDTO);
if (promoCodeTotalCartDiscount > 0) { if (promoCodeTotalCartDiscount > 0) {
for (ItemCartDTO item : cartDTO.getItems()) { for (ItemCartDTO item : cartDTO.getItems()) {
double itemDiscount = promoCodeTotalCartDiscount / cartDTO.getItems().size(); if (item.getProductType() != ProductType.GIFT_CARD) {
if (itemDiscount > 0) { double itemDiscount = promoCodeTotalCartDiscount / acceptedItemsCount;
double newTotalPrice = item.getTotalPrice() - itemDiscount; if (itemDiscount > 0) {
item.setTotalPrice(Math.max(newTotalPrice, 0.0)); double newTotalPrice = item.getTotalPrice() - itemDiscount;
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount); item.setTotalPrice(Math.max(newTotalPrice, 0.0));
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
}
} }
} }
} }
...@@ -71,11 +84,13 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -71,11 +84,13 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
promoCodeTotalCartDiscount = applyPercentagePerCart(promoCodeDTO, cartDTO); promoCodeTotalCartDiscount = applyPercentagePerCart(promoCodeDTO, cartDTO);
if (promoCodeTotalCartDiscount > 0) { if (promoCodeTotalCartDiscount > 0) {
for (ItemCartDTO item : cartDTO.getItems()) { for (ItemCartDTO item : cartDTO.getItems()) {
double itemDiscount = promoCodeTotalCartDiscount / cartDTO.getItems().size(); if (item.getProductType() != ProductType.GIFT_CARD) {
if (itemDiscount > 0) { double itemDiscount = promoCodeTotalCartDiscount / acceptedItemsCount;
double newTotalPrice = item.getTotalPrice() - itemDiscount; if (itemDiscount > 0) {
item.setTotalPrice(Math.max(newTotalPrice, 0.0)); double newTotalPrice = item.getTotalPrice() - itemDiscount;
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount); item.setTotalPrice(Math.max(newTotalPrice, 0.0));
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
}
} }
} }
} }
...@@ -139,6 +154,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -139,6 +154,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
@Override @Override
public boolean isItemIncludedOrExcluded(ItemCartDTO item, PromoCodeDTO promoCodeDTO) { 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 isExcluded = false;
boolean isIncluded = false; boolean isIncluded = false;
...@@ -229,10 +249,12 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -229,10 +249,12 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
totalPriceAfterDiscount = totalPriceBeforeDiscount - discount; totalPriceAfterDiscount = totalPriceBeforeDiscount - discount;
if (discount > 0) { if (discount > 0) {
for (ItemCartDTO item : cart.getItems()) { for (ItemCartDTO item : cart.getItems()) {
double itemDiscount = discount / cart.getItems().size(); if (!(item.getProductType().equals(ProductType.GIFT_CARD))) {
item.setTotalPrice(item.getTotalPrice() - itemDiscount); double itemDiscount = discount / cart.getItems().size();
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount); item.setTotalPrice(item.getTotalPrice() - itemDiscount);
totalPriceAfterDiscount += itemDiscount; item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
totalPriceAfterDiscount += itemDiscount;
}
} }
} }
} }
...@@ -241,10 +263,12 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -241,10 +263,12 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
totalPriceAfterDiscount = totalPriceBeforeDiscount - discount; totalPriceAfterDiscount = totalPriceBeforeDiscount - discount;
if (discount > 0) { if (discount > 0) {
for (ItemCartDTO item : cart.getItems()) { for (ItemCartDTO item : cart.getItems()) {
double itemDiscount = discount / cart.getItems().size(); if (!(item.getProductType().equals(ProductType.GIFT_CARD))) {
item.setTotalPrice(item.getTotalPrice() - itemDiscount); double itemDiscount = discount / cart.getItems().size();
item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount); item.setTotalPrice(item.getTotalPrice() - itemDiscount);
totalPriceAfterDiscount += itemDiscount; item.setPromoCodeDiscounts(item.getPromoCodeDiscounts() + itemDiscount);
totalPriceAfterDiscount += itemDiscount;
}
} }
} }
} }
...@@ -274,6 +298,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -274,6 +298,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
@Override @Override
public double applyDiscountPerProduct(PromoCodeDTO promoCodeDTO, ItemCartDTO item) { 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)) { if (!isItemIncludedOrExcluded(item, promoCodeDTO)) {
logger.info("Item with ID {} is not eligible for discount.", item.getProductId()); logger.info("Item with ID {} is not eligible for discount.", item.getProductId());
return 0.0; return 0.0;
...@@ -298,6 +327,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService { ...@@ -298,6 +327,11 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
@Override @Override
public double applyDiscountPerCart(PromoCodeDTO promoCodeDTO, CartDTO cart) { 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) { 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."); logger.info("Cart total does not meet the minimum required amount for discount.");
return 0.0; 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