Search in sources :

Example 1 with ShoppingCartDataPopulator

use of com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method removeCartItem.

@Override
public ShoppingCartData removeCartItem(final Long itemID, final String cartId, final MerchantStore store, final Language language) throws Exception {
    if (StringUtils.isNotBlank(cartId)) {
        ShoppingCart cartModel = getCartModel(cartId, store);
        if (cartModel != null) {
            if (CollectionUtils.isNotEmpty(cartModel.getLineItems())) {
                Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> shoppingCartItemSet = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
                for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem shoppingCartItem : cartModel.getLineItems()) {
                    if (shoppingCartItem.getId().longValue() == itemID.longValue()) {
                        shoppingCartService.deleteShoppingCartItem(itemID);
                    } else {
                        shoppingCartItemSet.add(shoppingCartItem);
                    }
                }
                cartModel.setLineItems(shoppingCartItemSet);
                ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
                shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
                shoppingCartDataPopulator.setPricingService(pricingService);
                shoppingCartDataPopulator.setimageUtils(imageUtils);
                return shoppingCartDataPopulator.populate(cartModel, store, language);
            }
        }
    }
    return null;
}
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) HashSet(java.util.HashSet)

Example 2 with ShoppingCartDataPopulator

use of com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method updateCartItems.

// TODO promoCode request parameter
@Override
public ShoppingCartData updateCartItems(Optional<String> promoCode, final List<ShoppingCartItem> shoppingCartItems, final MerchantStore store, final Language language) throws Exception {
    Validate.notEmpty(shoppingCartItems, "shoppingCartItems null or empty");
    ShoppingCart cartModel = null;
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
    for (ShoppingCartItem item : shoppingCartItems) {
        if (item.getQuantity() < 1) {
            throw new CartModificationException("Quantity must not be less than one");
        }
        if (cartModel == null) {
            cartModel = getCartModel(item.getCode(), store);
        }
        com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(item.getId(), cartModel);
        if (entryToUpdate == null) {
            throw new CartModificationException("Unknown entry number.");
        }
        entryToUpdate.getProduct();
        LOG.info("Updating cart entry quantity to" + item.getQuantity());
        entryToUpdate.setQuantity((int) item.getQuantity());
        List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
        productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
        final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
        entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
        cartItems.add(entryToUpdate);
    }
    cartModel.setPromoCode(null);
    if (promoCode.isPresent()) {
        cartModel.setPromoCode(promoCode.get());
        cartModel.setPromoAdded(new Date());
    }
    cartModel.setLineItems(cartItems);
    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);
}
Also used : CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Date(java.util.Date) LocalDate(java.time.LocalDate) 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) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 3 with ShoppingCartDataPopulator

use of com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator 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 4 with ShoppingCartDataPopulator

use of com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method getShoppingCartData.

// @Override
public ShoppingCartData getShoppingCartData(ShoppingCart shoppingCartModel, Language language) throws Exception {
    Validate.notNull(shoppingCartModel, "Shopping Cart cannot be null");
    ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
    shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
    shoppingCartDataPopulator.setPricingService(pricingService);
    shoppingCartDataPopulator.setimageUtils(imageUtils);
    // Language language = (Language) getKeyValue( Constants.LANGUAGE );
    MerchantStore merchantStore = (MerchantStore) getKeyValue(Constants.MERCHANT_STORE);
    return shoppingCartDataPopulator.populate(shoppingCartModel, merchantStore, language);
}
Also used : ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 5 with ShoppingCartDataPopulator

use of com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator 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

ShoppingCartDataPopulator (com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator)8 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)5 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)5 PersistableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem)3 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)3 ConversionException (com.salesmanager.core.business.exception.ConversionException)2 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)2 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)2 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 CartModificationException (com.salesmanager.shop.model.shoppingcart.CartModificationException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 ShoppingCartData (com.salesmanager.shop.model.shoppingcart.ShoppingCartData)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1 NoResultException (javax.persistence.NoResultException)1