Search in sources :

Example 6 with SKU

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

the class PriceEngineImplTest method computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantityWithFixedDeliveryFee.

@Test
public void computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantityWithFixedDeliveryFee() {
    SKU sku = new SKU();
    sku.setPrice(10.0);
    SKU sku2 = new SKU();
    sku2.setPrice(20.0);
    when(entityManager.find(SKU.class, 1L)).thenReturn(sku);
    when(entityManager.find(SKU.class, 2L)).thenReturn(sku2);
    when(orderConfiguration.getFixedDeliveryFee()).thenReturn(11.0);
    Order order = new Order();
    Set<OrderItem> items = new HashSet<>();
    items.add(new OrderItem(1L, 1L, 1));
    items.add(new OrderItem(2L, 2L, 2));
    order.setItems(items);
    orderPriceEngine.computePrice(order);
    verify(entityManager).find(SKU.class, 1L);
    verify(entityManager).find(SKU.class, 2L);
    verify(orderConfiguration).getFixedDeliveryFee();
    assertThat(order.getPrice()).isEqualTo(61.0);
}
Also used : Order(org.rembx.jeeshop.order.model.Order) OrderItem(org.rembx.jeeshop.order.model.OrderItem) SKU(org.rembx.jeeshop.catalog.model.SKU) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with SKU

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

the class PriceEngineImplTest method computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantity.

@Test
public void computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantity() {
    SKU sku = new SKU();
    sku.setPrice(10.0);
    SKU sku2 = new SKU();
    sku2.setPrice(20.0);
    when(entityManager.find(SKU.class, 1L)).thenReturn(sku);
    when(entityManager.find(SKU.class, 2L)).thenReturn(sku2);
    when(orderConfiguration.getFixedDeliveryFee()).thenReturn(null);
    Order order = new Order();
    Set<OrderItem> items = new HashSet<>();
    items.add(new OrderItem(1L, 1L, 1));
    items.add(new OrderItem(2L, 2L, 2));
    order.setItems(items);
    orderPriceEngine.computePrice(order);
    verify(entityManager).find(SKU.class, 1L);
    verify(entityManager).find(SKU.class, 2L);
    verify(orderConfiguration).getFixedDeliveryFee();
    assertThat(order.getPrice()).isEqualTo(50.0);
    for (OrderItem item : order.getItems()) {
        if (item.getSkuId().equals(1L))
            assertThat(item.getPrice()).isEqualTo(10.0);
        else
            assertThat(item.getPrice()).isEqualTo(20.0);
    }
}
Also used : Order(org.rembx.jeeshop.order.model.Order) OrderItem(org.rembx.jeeshop.order.model.OrderItem) SKU(org.rembx.jeeshop.catalog.model.SKU) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 8 with SKU

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

the class OrderFinder method enhanceOrder.

/**
 * Enhance given order with Catalog items data and order static configuration.
 *
 * @param order the order to enhance
 */
public void enhanceOrder(Order order) {
    User user = order.getUser();
    order.getItems().forEach(orderItem -> {
        Product product = catalogEntityManager.find(Product.class, orderItem.getProductId());
        SKU sku = catalogEntityManager.find(SKU.class, orderItem.getSkuId());
        product.setLocalizedPresentation(user.getPreferredLocale());
        orderItem.setDisplayName(product.getLocalizedPresentation() != null ? product.getLocalizedPresentation().getDisplayName() : product.getName());
        orderItem.setSkuReference(sku.getReference());
        if (product.getLocalizedPresentation() != null && product.getLocalizedPresentation().getSmallImage() != null)
            orderItem.setPresentationImageURI("products/" + orderItem.getProductId() + "/" + product.getLocalizedPresentation().getLocale() + "/" + product.getLocalizedPresentation().getSmallImage().getUri());
    });
    order.getOrderDiscounts().forEach(orderDiscount -> {
        Discount discount = catalogEntityManager.find(Discount.class, orderDiscount.getDiscountId());
        discount.setLocalizedPresentation(user.getPreferredLocale());
        orderDiscount.setDisplayName(discount.getLocalizedPresentation().getDisplayName() != null ? discount.getLocalizedPresentation().getDisplayName() : discount.getName());
        orderDiscount.setRateType(discount.getRateType());
        if (discount.getLocalizedPresentation() != null && discount.getLocalizedPresentation().getSmallImage() != null)
            orderDiscount.setPresentationImageURI("discounts/" + orderDiscount.getDiscountId() + "/" + discount.getLocalizedPresentation().getLocale() + "/" + discount.getLocalizedPresentation().getSmallImage().getUri());
    });
    order.setDeliveryFee(orderConfiguration.getFixedDeliveryFee());
    order.setVat(orderConfiguration.getVAT());
}
Also used : User(org.rembx.jeeshop.user.model.User) Discount(org.rembx.jeeshop.catalog.model.Discount) Product(org.rembx.jeeshop.catalog.model.Product) SKU(org.rembx.jeeshop.catalog.model.SKU)

Example 9 with SKU

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

the class PriceEngineImpl method computePrice.

@Override
public void computePrice(Order order) {
    if (CollectionUtils.isEmpty(order.getItems())) {
        throw new IllegalStateException("Order items list is empty " + order);
    }
    Double price = 0.0;
    for (OrderItem orderItem : order.getItems()) {
        SKU sku = entityManager.find(SKU.class, (orderItem).getSkuId());
        price += (sku.getPrice() * (orderItem).getQuantity());
        orderItem.setPrice(sku.getPrice());
    }
    final Double fixedDeliveryFee = orderConfiguration.getFixedDeliveryFee();
    price = applyEligibleDiscounts(order, price);
    if (fixedDeliveryFee != null) {
        price += fixedDeliveryFee;
        order.setDeliveryFee(fixedDeliveryFee);
    }
    order.setPrice(price);
}
Also used : OrderItem(org.rembx.jeeshop.order.model.OrderItem) SKU(org.rembx.jeeshop.catalog.model.SKU)

Example 10 with SKU

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

the class SKUsCT method create_shouldPersist.

@Test
public void create_shouldPersist() {
    SKU sku = new SKU("name", "description", 1.0, 2, "reference", new Date(), new Date(), false, 1);
    entityManager.getTransaction().begin();
    service.create(sku);
    entityManager.getTransaction().commit();
    assertThat(entityManager.find(SKU.class, sku.getId())).isNotNull();
    entityManager.remove(sku);
}
Also used : SKU(org.rembx.jeeshop.catalog.model.SKU) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SKU (org.rembx.jeeshop.catalog.model.SKU)17 Test (org.junit.jupiter.api.Test)12 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)4 Date (java.util.Date)3 OrderItem (org.rembx.jeeshop.order.model.OrderItem)3 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 Order (org.rembx.jeeshop.order.model.Order)2 Discount (org.rembx.jeeshop.catalog.model.Discount)1 Product (org.rembx.jeeshop.catalog.model.Product)1 User (org.rembx.jeeshop.user.model.User)1