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);
}
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);
}
}
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());
}
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);
}
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);
}
Aggregations