Search in sources :

Example 1 with ShoppingCartAttribute

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

the class ShoppingCartFacadeImpl method createCartItem.

private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(final ShoppingCart cartModel, final ShoppingCartItem shoppingCartItem, final 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());
    }
    /**
     * Check if product quantity is 0
     * Check if product is available
     * Check if date available <= now
     */
    Set<ProductAvailability> availabilities = product.getAvailabilities();
    if (availabilities == null) {
        throw new Exception("Item with id " + product.getId() + " is not properly configured");
    }
    for (ProductAvailability availability : availabilities) {
        if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
            throw new Exception("Item with id " + product.getId() + " is not available");
        }
    }
    if (!product.isAvailable()) {
        throw new Exception("Item with id " + product.getId() + " is not available");
    }
    if (!DateUtil.dateBeforeEqualsDate(product.getDateAvailable(), new Date())) {
        throw new Exception("Item with id " + product.getId() + " is not available");
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(product);
    item.setQuantity(shoppingCartItem.getQuantity());
    item.setShoppingCart(cartModel);
    // attributes
    List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
    if (!CollectionUtils.isEmpty(cartAttributes)) {
        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);
                item.addAttributes(attributeItem);
            }
        }
    }
    return item;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) 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) Date(java.util.Date) LocalDate(java.time.LocalDate) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability)

Example 2 with ShoppingCartAttribute

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

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

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

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)4 ShoppingCartAttribute (com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute)4 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)3 ConversionException (org.apache.commons.beanutils.ConversionException)3 Product (com.salesmanager.core.model.catalog.product.Product)2 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)2 HashSet (java.util.HashSet)2 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)1 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)1 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)1 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)1 ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)1 OrderSummary (com.salesmanager.core.model.order.OrderSummary)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)1 OrderTotal (com.salesmanager.shop.model.order.total.OrderTotal)1 CartModificationException (com.salesmanager.shop.model.shoppingcart.CartModificationException)1 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)1 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)1 LocalDate (java.time.LocalDate)1