Search in sources :

Example 11 with Discount

use of org.rembx.jeeshop.catalog.model.Discount in project jeeshop by remibantos.

the class DiscountsCT method modifyUnknownDiscount_ShouldThrowNotFoundException.

@Test
public void modifyUnknownDiscount_ShouldThrowNotFoundException() {
    Discount detachedDiscountToModify = new Discount(9999L);
    try {
        localService.modify(tester.getSecurityContext(), detachedDiscountToModify);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.NOT_FOUND);
    }
}
Also used : Discount(org.rembx.jeeshop.catalog.model.Discount) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Test(org.junit.jupiter.api.Test)

Example 12 with Discount

use of org.rembx.jeeshop.catalog.model.Discount in project jeeshop by remibantos.

the class PriceEngineImpl method applyEligibleDiscounts.

private Double applyEligibleDiscounts(Order order, Double price) {
    double originalPrice = price;
    Long userCompletedOrders = orderFinder.countUserCompletedOrders(order.getUser());
    List<Discount> userEligibleOrderDiscounts = discountFinder.findEligibleOrderDiscounts(null, userCompletedOrders);
    if (userEligibleOrderDiscounts == null) {
        return price;
    }
    if (order.getOrderDiscounts() == null) {
        order.setOrderDiscounts(new HashSet<>());
    }
    for (Discount discount : userEligibleOrderDiscounts) {
        if (discount.isEligible(originalPrice)) {
            price = discount.processDiscount(price, originalPrice);
            order.getOrderDiscounts().add(new OrderDiscount(discount.getId(), discount.getDiscountValue()));
        }
    }
    return price;
}
Also used : OrderDiscount(org.rembx.jeeshop.order.model.OrderDiscount) Discount(org.rembx.jeeshop.catalog.model.Discount) OrderDiscount(org.rembx.jeeshop.order.model.OrderDiscount)

Aggregations

Discount (org.rembx.jeeshop.catalog.model.Discount)12 Test (org.junit.jupiter.api.Test)6 JPAQueryFactory (com.querydsl.jpa.impl.JPAQueryFactory)2 Date (java.util.Date)2 Test (org.junit.Test)2 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)2 Product (org.rembx.jeeshop.catalog.model.Product)1 SKU (org.rembx.jeeshop.catalog.model.SKU)1 OrderDiscount (org.rembx.jeeshop.order.model.OrderDiscount)1 User (org.rembx.jeeshop.user.model.User)1