Search in sources :

Example 6 with ReadableShoppingCart

use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method getByCode.

@Override
public ReadableShoppingCart getByCode(String code, MerchantStore store, Language language) throws Exception {
    ShoppingCart cart = shoppingCartService.getByCode(code, store);
    ReadableShoppingCart readableCart = null;
    if (cart != null) {
        readableCart = readableShoppingCartMapper.convert(cart, store, language);
        if (!StringUtils.isBlank(cart.getPromoCode())) {
            // promo valid 1 day
            Date promoDateAdded = cart.getPromoAdded();
            if (promoDateAdded == null) {
                promoDateAdded = new Date();
            }
            Instant instant = promoDateAdded.toInstant();
            ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
            LocalDate date = zdt.toLocalDate();
            // date added < date + 1 day
            LocalDate tomorrow = LocalDate.now().plusDays(1);
            if (date.isBefore(tomorrow)) {
                readableCart.setPromoCode(cart.getPromoCode());
            }
        }
    }
    return readableCart;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 7 with ReadableShoppingCart

use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method getById.

@Override
public ReadableShoppingCart getById(Long shoppingCartId, MerchantStore store, Language language) throws Exception {
    ShoppingCart cart = shoppingCartService.getById(shoppingCartId);
    ReadableShoppingCart readableCart = null;
    if (cart != null) {
        readableCart = readableShoppingCartMapper.convert(cart, store, language);
    }
    return readableCart;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)

Example 8 with ReadableShoppingCart

use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method removeShoppingCartItem.

@Override
@Nullable
public ReadableShoppingCart removeShoppingCartItem(String cartCode, Long productId, MerchantStore merchant, Language language, boolean returnCart) throws Exception {
    Validate.notNull(cartCode, "Shopping cart code must not be null");
    Validate.notNull(productId, "product id must not be null");
    Validate.notNull(merchant, "MerchantStore must not be null");
    // get cart
    ShoppingCart cart = getCartModel(cartCode, merchant);
    if (cart == null) {
        throw new ResourceNotFoundException("Cart code [ " + cartCode + " ] not found");
    }
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem itemToDelete = null;
    for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem shoppingCartItem : cart.getLineItems()) {
        if (shoppingCartItem.getProduct().getId().longValue() == productId.longValue()) {
            // get cart item
            itemToDelete = getEntryToUpdate(shoppingCartItem.getId(), cart);
        // break;
        } else {
            items.add(shoppingCartItem);
        }
    }
    // delete item
    if (itemToDelete != null) {
        shoppingCartService.deleteShoppingCartItem(itemToDelete.getId());
    }
    // remaining items
    if (items.size() > 0) {
        cart.setLineItems(items);
    } else {
        cart.getLineItems().clear();
    }
    // update cart with remaining items
    shoppingCartService.saveOrUpdate(cart);
    if (items.size() > 0 & returnCart) {
        return this.getByCode(cartCode, merchant, language);
    }
    return null;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) HashSet(java.util.HashSet) Nullable(org.springframework.lang.Nullable)

Example 9 with ReadableShoppingCart

use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method modifyCart.

@Override
public ReadableShoppingCart modifyCart(String cartCode, String promo, MerchantStore store, Language language) throws Exception {
    ShoppingCart cart = shoppingCartService.getByCode(cartCode, store);
    cart.setPromoCode(promo);
    cart.setPromoAdded(new Date());
    shoppingCartService.save(cart);
    return readableShoppingCartMapper.convert(cart, store, language);
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 10 with ReadableShoppingCart

use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method addToCart.

@Override
public ReadableShoppingCart addToCart(PersistableShoppingCartItem item, MerchantStore store, Language language) {
    Validate.notNull(item, "PersistableShoppingCartItem cannot be null");
    // if cart does not exist create a new one
    ShoppingCart cartModel = new ShoppingCart();
    cartModel.setMerchantStore(store);
    cartModel.setShoppingCartCode(uniqueShoppingCartCode());
    if (!StringUtils.isBlank(item.getPromoCode())) {
        cartModel.setPromoCode(item.getPromoCode());
        cartModel.setPromoAdded(new Date());
    }
    try {
        return readableShoppingCart(cartModel, item, store, language);
    } catch (Exception e) {
        if (e instanceof ResourceNotFoundException) {
            throw (ResourceNotFoundException) e;
        } else {
            throw new ServiceRuntimeException(e);
        }
    }
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) LocalDate(java.time.LocalDate) NoResultException(javax.persistence.NoResultException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)19 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)10 PersistableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem)8 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)6 HttpEntity (org.springframework.http.HttpEntity)6 Order (org.junit.jupiter.api.Order)5 Test (org.junit.jupiter.api.Test)5 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)4 LocalDate (java.time.LocalDate)4 Date (java.util.Date)4 Customer (com.salesmanager.core.model.customer.Customer)3 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)3 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)2 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2