Search in sources :

Example 1 with ReconstructCartResponse

use of org.broadleafcommerce.core.order.service.call.ReconstructCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class LegacyMergeCartServiceImpl method reconstructCart.

@Override
public ReconstructCartResponse reconstructCart(Customer customer, boolean priceOrder) throws PricingException {
    ReconstructCartResponse reconstructCartResponse = new ReconstructCartResponse();
    Order customerCart = orderService.findCartForCustomer(customer);
    if (customerCart != null) {
        List<OrderItem> itemsToRemove = new ArrayList<OrderItem>();
        for (OrderItem orderItem : customerCart.getOrderItems()) {
            if (orderItem instanceof DiscreteOrderItem) {
                DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) orderItem;
                if (discreteOrderItem.getSku().getActiveStartDate() != null) {
                    if (!discreteOrderItem.getSku().isActive(discreteOrderItem.getProduct(), orderItem.getCategory())) {
                        itemsToRemove.add(orderItem);
                    }
                } else {
                    if (!discreteOrderItem.getProduct().isActive() || !orderItem.getCategory().isActive()) {
                        itemsToRemove.add(orderItem);
                    }
                }
            } else if (orderItem instanceof BundleOrderItem) {
                BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
                boolean removeBundle = false;
                for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
                    if (discreteOrderItem.getSku().getActiveStartDate() != null) {
                        if (!discreteOrderItem.getSku().isActive(discreteOrderItem.getProduct(), orderItem.getCategory())) {
                            /*
                                 * Bundle has an inactive item in it -- remove the
                                 * whole bundle
                                 */
                            removeBundle = true;
                            break;
                        }
                    } else {
                        if (!discreteOrderItem.getProduct().isActive() || !orderItem.getCategory().isActive()) {
                            removeBundle = true;
                            break;
                        }
                    }
                }
                if (removeBundle) {
                    itemsToRemove.add(orderItem);
                }
            }
        }
        // Remove any giftwrap items who have one or more wrapped item members that have been removed
        for (OrderItem orderItem : customerCart.getOrderItems()) {
            if (orderItem instanceof GiftWrapOrderItem) {
                for (OrderItem wrappedItem : ((GiftWrapOrderItem) orderItem).getWrappedItems()) {
                    if (itemsToRemove.contains(wrappedItem)) {
                        itemsToRemove.add(orderItem);
                        break;
                    }
                }
            }
        }
        for (OrderItem item : itemsToRemove) {
            removeItemFromOrder(customerCart, item, priceOrder);
        }
        reconstructCartResponse.setRemovedItems(itemsToRemove);
    }
    reconstructCartResponse.setOrder(customerCart);
    return reconstructCartResponse;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ArrayList(java.util.ArrayList) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) ReconstructCartResponse(org.broadleafcommerce.core.order.service.call.ReconstructCartResponse)

Example 2 with ReconstructCartResponse

use of org.broadleafcommerce.core.order.service.call.ReconstructCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class MergeCartServiceImpl method reconstructCart.

@Override
public ReconstructCartResponse reconstructCart(Customer customer, boolean priceOrder) throws PricingException, RemoveFromCartException {
    ReconstructCartResponse reconstructCartResponse = new ReconstructCartResponse();
    Order customerCart = orderService.findCartForCustomerWithEnhancements(customer);
    if (customerCart != null) {
        List<OrderItem> itemsToRemove = new ArrayList<OrderItem>();
        for (OrderItem orderItem : customerCart.getOrderItems()) {
            if (orderItem instanceof DiscreteOrderItem) {
                DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
                if (!checkActive(doi) || !checkInventory(doi) || !checkOtherValidity(orderItem)) {
                    itemsToRemove.add(orderItem);
                }
            } else if (orderItem instanceof BundleOrderItem) {
                BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
                for (DiscreteOrderItem doi : bundleOrderItem.getDiscreteOrderItems()) {
                    if (!checkActive(doi) || !checkInventory(doi) || !checkOtherValidity(orderItem)) {
                        itemsToRemove.add(doi.getBundleOrderItem());
                    }
                }
            }
        }
        // Remove any giftwrap items who have one or more wrapped item members that have been removed
        for (OrderItem orderItem : customerCart.getOrderItems()) {
            if (orderItem instanceof GiftWrapOrderItem) {
                for (OrderItem wrappedItem : ((GiftWrapOrderItem) orderItem).getWrappedItems()) {
                    if (itemsToRemove.contains(wrappedItem)) {
                        itemsToRemove.add(orderItem);
                        break;
                    }
                }
            }
        }
        for (OrderItem item : itemsToRemove) {
            orderService.removeItem(customerCart.getId(), item.getId(), false);
        }
        reconstructCartResponse.setRemovedItems(itemsToRemove);
        customerCart = orderService.save(customerCart, priceOrder);
    }
    reconstructCartResponse.setOrder(customerCart);
    return reconstructCartResponse;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ArrayList(java.util.ArrayList) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) ReconstructCartResponse(org.broadleafcommerce.core.order.service.call.ReconstructCartResponse)

Example 3 with ReconstructCartResponse

use of org.broadleafcommerce.core.order.service.call.ReconstructCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class MergeCartServiceImpl method mergeCart.

@Override
public MergeCartResponse mergeCart(Customer customer, Order anonymousCart, boolean priceOrder) throws PricingException, RemoveFromCartException {
    MergeCartResponse mergeCartResponse = new MergeCartResponse();
    // We no longer merge items, only transition cart states
    mergeCartResponse.setMerged(false);
    // We need to make sure that the old, saved customer cart is reconstructed with availability concerns in mind
    ReconstructCartResponse reconstructCartResponse = reconstructCart(customer, false);
    mergeCartResponse.setRemovedItems(reconstructCartResponse.getRemovedItems());
    Order customerCart = reconstructCartResponse.getOrder();
    if (anonymousCart != null && customerCart != null && anonymousCart.equals(customerCart)) {
        // The carts are the same, use either ensuring it's owned by the current customer
        setNewCartOwnership(anonymousCart, customer);
        mergeCartResponse.setOrder(anonymousCart);
    } else if (anonymousCart == null || anonymousCart.getOrderItems().size() == 0) {
        // The anonymous cart is of no use, use the customer cart
        mergeCartResponse.setOrder(customerCart);
        // multiple IN_PROCESS carts. We can go ahead and clean up this empty cart anyway since it's empty
        if (anonymousCart != null) {
            orderService.cancelOrder(anonymousCart);
        }
    } else if (customerCart == null || customerCart.getOrderItems().size() == 0) {
        // hanging around
        if (customerCart != null) {
            orderService.cancelOrder(customerCart);
        }
        // The customer cart is of no use, use the anonymous cart
        setNewCartOwnership(anonymousCart, customer);
        mergeCartResponse.setOrder(anonymousCart);
    } else {
        // Both carts have some items. The anonymous cart will always be the more recent one by definition
        // Save off the old customer cart and use the anonymous cart
        setSavedCartAttributes(customerCart);
        orderService.save(customerCart, false);
        setNewCartOwnership(anonymousCart, customer);
        mergeCartResponse.setOrder(anonymousCart);
    }
    if (mergeCartResponse.getOrder() != null) {
        Order savedCart = orderService.save(mergeCartResponse.getOrder(), priceOrder, priceOrder);
        mergeCartResponse.setOrder(savedCart);
    }
    return mergeCartResponse;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) ReconstructCartResponse(org.broadleafcommerce.core.order.service.call.ReconstructCartResponse) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse)

Example 4 with ReconstructCartResponse

use of org.broadleafcommerce.core.order.service.call.ReconstructCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class LegacyMergeCartServiceImpl method mergeCart.

@Override
public MergeCartResponse mergeCart(Customer customer, Order anonymousCart, boolean priceOrder) throws PricingException {
    MergeCartResponse mergeCartResponse = new MergeCartResponse();
    // reconstruct cart items (make sure they are valid)
    ReconstructCartResponse reconstructCartResponse = reconstructCart(customer, false);
    mergeCartResponse.setRemovedItems(reconstructCartResponse.getRemovedItems());
    Order customerCart = reconstructCartResponse.getOrder();
    if (anonymousCart != null && customerCart != null && anonymousCart.getId().equals(customerCart.getId())) {
        /*
             * Set merged to false if the cart ids are equal (cookied customer
             * logs in).
             */
        mergeCartResponse.setMerged(false);
    } else {
        /*
             * Set the response to merged if the saved cart has any items
             * available to merge in.
             */
        mergeCartResponse.setMerged(customerCart != null && customerCart.getOrderItems().size() > 0);
    }
    // add anonymous cart items (make sure they are valid)
    if (anonymousCart != null && (customerCart == null || !customerCart.getId().equals(anonymousCart.getId()))) {
        if (anonymousCart != null && anonymousCart.getOrderItems() != null && !anonymousCart.getOrderItems().isEmpty()) {
            if (customerCart == null) {
                customerCart = orderService.createNewCartForCustomer(customer);
            }
            Map<OrderItem, OrderItem> oldNewItemMap = new HashMap<OrderItem, OrderItem>();
            customerCart = mergeRegularOrderItems(anonymousCart, mergeCartResponse, customerCart, oldNewItemMap);
            customerCart = mergeOfferCodes(anonymousCart, customerCart);
            customerCart = removeExpiredGiftWrapOrderItems(mergeCartResponse, customerCart, oldNewItemMap);
            customerCart = mergeGiftWrapOrderItems(mergeCartResponse, customerCart, oldNewItemMap);
            orderService.cancelOrder(anonymousCart);
        }
    }
    // copy the customer's email to this order, overriding any previously set email
    if (customerCart != null && StringUtils.isNotBlank(customer.getEmailAddress())) {
        customerCart.setEmailAddress(customer.getEmailAddress());
        customerCart = orderService.save(customerCart, priceOrder);
    }
    mergeCartResponse.setOrder(customerCart);
    return mergeCartResponse;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) HashMap(java.util.HashMap) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ReconstructCartResponse(org.broadleafcommerce.core.order.service.call.ReconstructCartResponse) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse)

Aggregations

Order (org.broadleafcommerce.core.order.domain.Order)4 ReconstructCartResponse (org.broadleafcommerce.core.order.service.call.ReconstructCartResponse)4 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)3 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)3 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)3 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)3 ArrayList (java.util.ArrayList)2 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)2 HashMap (java.util.HashMap)1