Search in sources :

Example 1 with Discount

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

the class DiscountsCT method create_shouldPersist.

@Test
public void create_shouldPersist() {
    Discount discount = new Discount("discount777", "a discount", ORDER, DISCOUNT_RATE, AMOUNT, null, 0.1, 2.0, 1, true, null, null, false);
    entityManager.getTransaction().begin();
    service.create(discount);
    entityManager.getTransaction().commit();
    assertThat(entityManager.find(Discount.class, discount.getId())).isNotNull();
    entityManager.remove(discount);
}
Also used : Discount(org.rembx.jeeshop.catalog.model.Discount) Test(org.junit.Test)

Example 2 with Discount

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

the class DiscountsCT method delete_shouldRemove.

@Test
public void delete_shouldRemove() {
    entityManager.getTransaction().begin();
    Discount discount = new Discount("discount888", "a discount", ORDER, DISCOUNT_RATE, QUANTITY, null, 0.1, 2.0, 1, true, null, null, false);
    entityManager.persist(discount);
    entityManager.getTransaction().commit();
    entityManager.getTransaction().begin();
    service.delete(discount.getId());
    entityManager.getTransaction().commit();
    assertThat(entityManager.find(Discount.class, discount.getId())).isNull();
}
Also used : Discount(org.rembx.jeeshop.catalog.model.Discount) Test(org.junit.Test)

Example 3 with Discount

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

the class DiscountFinder method findEligibleOrderDiscounts.

/*
     * Returns all discounts eligible for end-user
     * @param applicableTo the applicable type of discount
     * @param locale the locale for results localization
     * @return the visible discounts: not disabled, with startDate and endDate respectively before and after now and without
     * voucher code, applicable to given number of orders
     */
public List<Discount> findEligibleOrderDiscounts(String locale, Long completedOrderNumbers) {
    Date now = new Date();
    List<Discount> results = new JPAQueryFactory(entityManager).selectFrom(discount).where(discount.disabled.isFalse(), discount.endDate.after(now).or(discount.endDate.isNull()), discount.startDate.before(now).or(discount.startDate.isNull()), discount.applicableTo.eq(ApplicableTo.ORDER), discount.triggerRule.ne(Discount.Trigger.ORDER_NUMBER).or(discount.triggerValue.eq(completedOrderNumbers.doubleValue() + 1)), discount.voucherCode.isNull()).fetch();
    results.forEach((discount) -> discount.setLocalizedPresentation(locale));
    return results;
}
Also used : Discount(org.rembx.jeeshop.catalog.model.Discount) JPAQueryFactory(com.querydsl.jpa.impl.JPAQueryFactory) Date(java.util.Date)

Example 4 with Discount

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

the class DiscountFinder method findVisibleDiscounts.

/*
     * Returns all discounts visible to end user
     * @param applicableTo the applicable type of discount
     * @param locale the locale for results localization
     * @return the visible discounts: not disabled, with startDate and endDate respectively before and after now and without
     * voucher code
     */
public List<Discount> findVisibleDiscounts(ApplicableTo applicableTo, String locale) {
    Date now = new Date();
    List<Discount> results = new JPAQueryFactory(entityManager).selectFrom(discount).where(discount.disabled.isFalse(), discount.endDate.after(now).or(discount.endDate.isNull()), discount.startDate.before(now).or(discount.startDate.isNull()), discount.applicableTo.eq(applicableTo), discount.voucherCode.isNull()).fetch();
    results.forEach((discount) -> discount.setLocalizedPresentation(locale));
    return results;
}
Also used : Discount(org.rembx.jeeshop.catalog.model.Discount) JPAQueryFactory(com.querydsl.jpa.impl.JPAQueryFactory) Date(java.util.Date)

Example 5 with Discount

use of org.rembx.jeeshop.catalog.model.Discount 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)

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