Search in sources :

Example 11 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method updateCartItem.

@Override
public ShoppingCartData updateCartItem(final Long itemID, final String cartId, final long newQuantity, final MerchantStore store, final Language language) throws Exception {
    if (newQuantity < 1) {
        throw new CartModificationException("Quantity must not be less than one");
    }
    if (StringUtils.isNotBlank(cartId)) {
        ShoppingCart cartModel = getCartModel(cartId, store);
        if (cartModel != null) {
            com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(itemID.longValue(), cartModel);
            if (entryToUpdate == null) {
                throw new CartModificationException("Unknown entry number.");
            }
            entryToUpdate.getProduct();
            LOG.info("Updating cart entry quantity to" + newQuantity);
            entryToUpdate.setQuantity((int) newQuantity);
            List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
            productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
            final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
            entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
            shoppingCartService.saveOrUpdate(cartModel);
            LOG.info("Cart entry updated with desired quantity");
            ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
            shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
            shoppingCartDataPopulator.setPricingService(pricingService);
            shoppingCartDataPopulator.setimageUtils(imageUtils);
            return shoppingCartDataPopulator.populate(cartModel, store, language);
        }
    }
    return null;
}
Also used : CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 12 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart 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 13 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart 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 14 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart 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)

Example 15 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartModelPopulator method populate.

@Override
public ShoppingCart populate(ShoppingCartData shoppingCart, ShoppingCart cartMdel, final MerchantStore store, Language language) {
    // if id >0 get the original from the database, override products
    try {
        if (shoppingCart.getId() > 0 && StringUtils.isNotBlank(shoppingCart.getCode())) {
            cartMdel = shoppingCartService.getByCode(shoppingCart.getCode(), store);
            if (cartMdel == null) {
                cartMdel = new ShoppingCart();
                cartMdel.setShoppingCartCode(shoppingCart.getCode());
                cartMdel.setMerchantStore(store);
                if (customer != null) {
                    cartMdel.setCustomerId(customer.getId());
                }
                shoppingCartService.create(cartMdel);
            }
        } else {
            cartMdel.setShoppingCartCode(shoppingCart.getCode());
            cartMdel.setMerchantStore(store);
            if (customer != null) {
                cartMdel.setCustomerId(customer.getId());
            }
            shoppingCartService.create(cartMdel);
        }
        List<ShoppingCartItem> items = shoppingCart.getShoppingCartItems();
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> newItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
        if (items != null && items.size() > 0) {
            for (ShoppingCartItem item : items) {
                Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = cartMdel.getLineItems();
                if (cartItems != null && cartItems.size() > 0) {
                    for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem dbItem : cartItems) {
                        if (dbItem.getId().longValue() == item.getId()) {
                            dbItem.setQuantity(item.getQuantity());
                            // compare attributes
                            Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = dbItem.getAttributes();
                            Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> newAttributes = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem>();
                            List<ShoppingCartAttribute> cartAttributes = item.getShoppingCartAttributes();
                            if (!CollectionUtils.isEmpty(cartAttributes)) {
                                for (ShoppingCartAttribute attribute : cartAttributes) {
                                    for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem dbAttribute : attributes) {
                                        if (dbAttribute.getId().longValue() == attribute.getId()) {
                                            newAttributes.add(dbAttribute);
                                        }
                                    }
                                }
                                dbItem.setAttributes(newAttributes);
                            } else {
                                dbItem.removeAllAttributes();
                            }
                            newItems.add(dbItem);
                        }
                    }
                } else {
                    // create new item
                    com.salesmanager.core.model.shoppingcart.ShoppingCartItem cartItem = createCartItem(cartMdel, item, store);
                    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> lineItems = cartMdel.getLineItems();
                    if (lineItems == null) {
                        lineItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
                        cartMdel.setLineItems(lineItems);
                    }
                    lineItems.add(cartItem);
                    shoppingCartService.update(cartMdel);
                }
            }
        // end for
        }
    // end if
    } catch (ServiceException se) {
        LOG.error("Error while converting cart data to cart model.." + se);
        throw new ConversionException("Unable to create cart model", se);
    } catch (Exception ex) {
        LOG.error("Error while converting cart data to cart model.." + ex);
        throw new ConversionException("Unable to create cart model", ex);
    }
    return cartMdel;
}
Also used : ConversionException(org.apache.commons.beanutils.ConversionException) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) HashSet(java.util.HashSet)

Aggregations

ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)38 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)18 ServiceException (com.salesmanager.core.business.exception.ServiceException)11 Customer (com.salesmanager.core.model.customer.Customer)11 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)8 HashSet (java.util.HashSet)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)7 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)6 ShoppingCartDataPopulator (com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator)6 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)5 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)5 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)5 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)5 LocalDate (java.time.LocalDate)5 ConversionException (com.salesmanager.core.business.exception.ConversionException)4