Search in sources :

Example 6 with ShoppingCart

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

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

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

the class ShoppingCartFacadeImpl method createCartItems.

// used for api
private List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> createCartItems(ShoppingCart cartModel, List<PersistableShoppingCartItem> shoppingCartItems, MerchantStore store) throws Exception {
    List<Long> productIds = shoppingCartItems.stream().map(s -> Long.valueOf(s.getProduct())).collect(Collectors.toList());
    List<Product> products = productService.getProductsByIds(productIds);
    if (products == null || products.size() != shoppingCartItems.size()) {
        LOG.warn("----------------------- Items with in id-list " + productIds + " does not exist");
        throw new ResourceNotFoundException("Item with id " + productIds + " does not exist");
    }
    List<Product> wrongStoreProducts = products.stream().filter(p -> p.getMerchantStore().getId() != store.getId()).collect(Collectors.toList());
    if (wrongStoreProducts.size() > 0) {
        throw new ResourceNotFoundException("One or more of the items with id's " + wrongStoreProducts.stream().map(s -> Long.valueOf(s.getId())).collect(Collectors.toList()) + " does not belong to merchant " + store.getId());
    }
    List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new ArrayList<>();
    for (Product p : products) {
        com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(p);
        Optional<PersistableShoppingCartItem> oShoppingCartItem = shoppingCartItems.stream().filter(i -> i.getProduct() == p.getId()).findFirst();
        if (!oShoppingCartItem.isPresent()) {
            // Should never happen if not something is updated in realtime or user has item in local storage and add it long time after to cart!
            LOG.warn("Missing shoppingCartItem for product " + p.getSku() + " ( " + p.getId() + " )");
            continue;
        }
        PersistableShoppingCartItem shoppingCartItem = oShoppingCartItem.get();
        item.setQuantity(shoppingCartItem.getQuantity());
        item.setShoppingCart(cartModel);
        /**
         * Check if product is available
         * Check if product quantity is 0
         * Check if date available <= now
         */
        if (!p.isAvailable()) {
            throw new Exception("Item with id " + p.getId() + " is not available");
        }
        Set<ProductAvailability> availabilities = p.getAvailabilities();
        if (availabilities == null) {
            throw new Exception("Item with id " + p.getId() + " is not properly configured");
        }
        for (ProductAvailability availability : availabilities) {
            if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
                throw new Exception("Item with id " + p.getId() + " is not available");
            }
        }
        if (!DateUtil.dateBeforeEqualsDate(p.getDateAvailable(), new Date())) {
            throw new Exception("Item with id " + p.getId() + " is not available");
        }
        // end qty & availablility checks
        // set attributes
        List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
        if (!CollectionUtils.isEmpty(attributes)) {
            for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
                ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
                if (productAttribute != null && productAttribute.getProduct().getId().longValue() == p.getId().longValue()) {
                    com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                    item.addAttributes(attributeItem);
                }
            }
        }
        items.add(item);
    }
    return items;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) NoResultException(javax.persistence.NoResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ServiceException(com.salesmanager.core.business.exception.ServiceException) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Set(java.util.Set) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) ShoppingCartCalculationService(com.salesmanager.core.business.services.shoppingcart.ShoppingCartCalculationService) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) LocalDate(java.time.LocalDate) Optional(java.util.Optional) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductPriceUtils(com.salesmanager.core.business.utils.ProductPriceUtils) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) Constants(com.salesmanager.shop.constants.Constants) ReadableShoppingCartMapper(com.salesmanager.shop.mapper.cart.ReadableShoppingCartMapper) DateUtil(com.salesmanager.shop.utils.DateUtil) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Nullable(org.springframework.lang.Nullable) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ShoppingCartService(com.salesmanager.core.business.services.shoppingcart.ShoppingCartService) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) Customer(com.salesmanager.core.model.customer.Customer) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) Validate(org.apache.commons.lang3.Validate) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) 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) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)

Example 9 with ShoppingCart

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

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

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