Search in sources :

Example 6 with ShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.ShoppingCartItem 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 7 with ShoppingCartItem

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

the class ShoppingCartDataPopulator method populate.

@Override
public ShoppingCartData populate(final ShoppingCart shoppingCart, final ShoppingCartData cart, final MerchantStore store, final Language language) {
    Validate.notNull(shoppingCart, "Requires ShoppingCart");
    Validate.notNull(language, "Requires Language not null");
    int cartQuantity = 0;
    cart.setCode(shoppingCart.getShoppingCartCode());
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = shoppingCart.getLineItems();
    List<ShoppingCartItem> shoppingCartItemsList = Collections.emptyList();
    try {
        if (items != null) {
            shoppingCartItemsList = new ArrayList<ShoppingCartItem>();
            for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
                ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
                shoppingCartItem.setCode(cart.getCode());
                shoppingCartItem.setProductCode(item.getProduct().getSku());
                shoppingCartItem.setProductVirtual(item.isProductVirtual());
                shoppingCartItem.setProductId(item.getProductId());
                shoppingCartItem.setId(item.getId());
                String itemName = item.getProduct().getProductDescription().getName();
                if (!CollectionUtils.isEmpty(item.getProduct().getDescriptions())) {
                    for (ProductDescription productDescription : item.getProduct().getDescriptions()) {
                        if (language != null && language.getId().intValue() == productDescription.getLanguage().getId().intValue()) {
                            itemName = productDescription.getName();
                            break;
                        }
                    }
                }
                shoppingCartItem.setName(itemName);
                shoppingCartItem.setPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
                shoppingCartItem.setQuantity(item.getQuantity());
                cartQuantity = cartQuantity + item.getQuantity();
                shoppingCartItem.setProductPrice(item.getItemPrice());
                shoppingCartItem.setSubTotal(pricingService.getDisplayAmount(item.getSubTotal(), store));
                ProductImage image = item.getProduct().getProductImage();
                if (image != null && imageUtils != null) {
                    String imagePath = imageUtils.buildProductImageUtils(store, item.getProduct().getSku(), image.getProductImage());
                    shoppingCartItem.setImage(imagePath);
                }
                Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
                if (attributes != null) {
                    List<ShoppingCartAttribute> cartAttributes = new ArrayList<ShoppingCartAttribute>();
                    for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
                        ShoppingCartAttribute cartAttribute = new ShoppingCartAttribute();
                        cartAttribute.setId(attribute.getId());
                        cartAttribute.setAttributeId(attribute.getProductAttributeId());
                        cartAttribute.setOptionId(attribute.getProductAttribute().getProductOption().getId());
                        cartAttribute.setOptionValueId(attribute.getProductAttribute().getProductOptionValue().getId());
                        List<ProductOptionDescription> optionDescriptions = attribute.getProductAttribute().getProductOption().getDescriptionsSettoList();
                        List<ProductOptionValueDescription> optionValueDescriptions = attribute.getProductAttribute().getProductOptionValue().getDescriptionsSettoList();
                        if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
                            String optionName = optionDescriptions.get(0).getName();
                            String optionValue = optionValueDescriptions.get(0).getName();
                            for (ProductOptionDescription optionDescription : optionDescriptions) {
                                if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                    optionName = optionDescription.getName();
                                    break;
                                }
                            }
                            for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
                                if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                    optionValue = optionValueDescription.getName();
                                    break;
                                }
                            }
                            cartAttribute.setOptionName(optionName);
                            cartAttribute.setOptionValue(optionValue);
                            cartAttributes.add(cartAttribute);
                        }
                    }
                    shoppingCartItem.setShoppingCartAttributes(cartAttributes);
                }
                shoppingCartItemsList.add(shoppingCartItem);
            }
        }
        if (CollectionUtils.isNotEmpty(shoppingCartItemsList)) {
            cart.setShoppingCartItems(shoppingCartItemsList);
        }
        if (shoppingCart.getOrderId() != null) {
            cart.setOrderId(shoppingCart.getOrderId());
        }
        OrderSummary summary = new OrderSummary();
        List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
        productsList.addAll(shoppingCart.getLineItems());
        summary.setProducts(productsList.stream().filter(p -> p.getProduct().isAvailable()).collect(Collectors.toList()));
        OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(shoppingCart, store, language);
        if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
            List<OrderTotal> totals = new ArrayList<OrderTotal>();
            for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
                OrderTotal total = new OrderTotal();
                total.setCode(t.getOrderTotalCode());
                total.setText(t.getText());
                total.setValue(t.getValue());
                totals.add(total);
            }
            cart.setTotals(totals);
        }
        cart.setSubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
        cart.setTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
        cart.setQuantity(cartQuantity);
        cart.setId(shoppingCart.getId());
    } catch (ServiceException ex) {
        LOG.error("Error while converting cart Model to cart Data.." + ex);
        throw new ConversionException("Unable to create cart data", ex);
    }
    return cart;
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) OrderSummary(com.salesmanager.core.model.order.OrderSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)

Example 8 with ShoppingCartItem

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

Example 9 with ShoppingCartItem

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

the class ShoppingCartModelPopulator method createCartItem.

private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCart cart, ShoppingCartItem shoppingCartItem, MerchantStore store) throws Exception {
    Product product = productService.getById(shoppingCartItem.getProductId());
    if (product == null) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not exist");
    }
    if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not belong to merchant " + store.getId());
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = new com.salesmanager.core.model.shoppingcart.ShoppingCartItem(cart, product);
    item.setQuantity(shoppingCartItem.getQuantity());
    item.setItemPrice(shoppingCartItem.getProductPrice());
    item.setShoppingCart(cart);
    // attributes
    List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
    if (!CollectionUtils.isEmpty(cartAttributes)) {
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> newAttributes = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem>();
        for (ShoppingCartAttribute attribute : cartAttributes) {
            ProductAttribute productAttribute = productAttributeService.getById(attribute.getAttributeId());
            if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
                com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                if (attribute.getAttributeId() > 0) {
                    attributeItem.setId(attribute.getId());
                }
                item.addAttributes(attributeItem);
            // newAttributes.add( attributeItem );
            }
        }
    // item.setAttributes( newAttributes );
    }
    return item;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) HashSet(java.util.HashSet)

Example 10 with ShoppingCartItem

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

the class ShoppingCartFacadeImpl method addItemsToShoppingCart.

@Override
public ShoppingCartData addItemsToShoppingCart(final ShoppingCartData shoppingCartData, final ShoppingCartItem item, final MerchantStore store, final Language language, final Customer customer) throws Exception {
    ShoppingCart cartModel = null;
    /**
     * Sometimes a user logs in and a shopping cart is present in db (shoppingCartData
     * but ui has no cookie with shopping cart code so the cart code will have
     * to be added to the item in order to process add to cart normally
     */
    if (shoppingCartData != null && StringUtils.isBlank(item.getCode())) {
        item.setCode(shoppingCartData.getCode());
    }
    if (!StringUtils.isBlank(item.getCode())) {
        // get it from the db
        cartModel = getShoppingCartModel(item.getCode(), store);
        if (cartModel == null) {
            cartModel = createCartModel(shoppingCartData.getCode(), store, customer);
        }
    }
    if (cartModel == null) {
        final String shoppingCartCode = StringUtils.isNotBlank(shoppingCartData.getCode()) ? shoppingCartData.getCode() : null;
        cartModel = createCartModel(shoppingCartCode, store, customer);
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem shoppingCartItem = createCartItem(cartModel, item, store);
    boolean duplicateFound = false;
    if (CollectionUtils.isEmpty(item.getShoppingCartAttributes())) {
        // increment quantity
        // get duplicate item from the cart
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartModelItems = cartModel.getLineItems();
        for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem cartItem : cartModelItems) {
            if (cartItem.getProduct().getId().longValue() == shoppingCartItem.getProduct().getId().longValue()) {
                if (CollectionUtils.isEmpty(cartItem.getAttributes())) {
                    if (!duplicateFound) {
                        if (!shoppingCartItem.isProductVirtual()) {
                            cartItem.setQuantity(cartItem.getQuantity() + shoppingCartItem.getQuantity());
                        }
                        duplicateFound = true;
                        break;
                    }
                }
            }
        }
    }
    if (!duplicateFound) {
        // shoppingCartItem.getAttributes().stream().forEach(a -> {a.setProductAttributeId(productAttributeId);});
        cartModel.getLineItems().add(shoppingCartItem);
    }
    /**
     * Update cart in database with line items *
     */
    shoppingCartService.saveOrUpdate(cartModel);
    // refresh cart
    cartModel = shoppingCartService.getById(cartModel.getId(), store);
    shoppingCartCalculationService.calculate(cartModel, store, language);
    ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
    shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
    shoppingCartDataPopulator.setPricingService(pricingService);
    shoppingCartDataPopulator.setimageUtils(imageUtils);
    return shoppingCartDataPopulator.populate(cartModel, store, language);
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)

Aggregations

ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)11 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)6 HashSet (java.util.HashSet)6 PersistableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem)5 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)5 ArrayList (java.util.ArrayList)5 ServiceException (com.salesmanager.core.business.exception.ServiceException)4 Product (com.salesmanager.core.model.catalog.product.Product)4 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)4 Language (com.salesmanager.core.model.reference.language.Language)4 ShoppingCartAttribute (com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute)4 ShoppingCartData (com.salesmanager.shop.model.shoppingcart.ShoppingCartData)4 ShoppingCartDataPopulator (com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator)4 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)3 Customer (com.salesmanager.core.model.customer.Customer)3 ConversionException (org.apache.commons.beanutils.ConversionException)3 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)2 PageInformation (com.salesmanager.shop.model.shop.PageInformation)2 CartModificationException (com.salesmanager.shop.model.shoppingcart.CartModificationException)2 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)2