Search in sources :

Example 11 with PricingException

use of org.broadleafcommerce.core.pricing.service.exception.PricingException in project BroadleafCommerce by BroadleafCommerce.

the class UpdateCartServiceImpl method copyCartToCurrentContext.

@Override
public UpdateCartResponse copyCartToCurrentContext(Order currentCart) {
    if (currentCart.getOrderItems() == null) {
        return null;
    }
    BroadleafCurrency currency = findActiveCurrency();
    if (currency == null) {
        return null;
    }
    // Reprice order logic
    List<OrderItemRequestDTO> itemsToReprice = new ArrayList<>();
    List<OrderItem> itemsToRemove = new ArrayList<>();
    List<OrderItem> itemsToReset = new ArrayList<>();
    boolean repriceOrder = true;
    for (OrderItem orderItem : currentCart.getOrderItems()) {
        // Lookup price in price list, if null, then add to itemsToRemove
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
            if (checkAvailabilityInLocale(doi, currency)) {
                OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                itemRequest.setProductId(doi.getProduct().getId());
                itemRequest.setQuantity(doi.getQuantity());
                itemsToReprice.add(itemRequest);
                itemsToReset.add(orderItem);
            } else {
                itemsToRemove.add(orderItem);
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem boi = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem doi : boi.getDiscreteOrderItems()) {
                if (checkAvailabilityInLocale(doi, currency)) {
                    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                    itemRequest.setProductId(doi.getProduct().getId());
                    itemRequest.setQuantity(doi.getQuantity());
                    itemsToReprice.add(itemRequest);
                    itemsToReset.add(orderItem);
                } else {
                    itemsToRemove.add(orderItem);
                }
            }
        }
    }
    for (OrderItem orderItem : itemsToReset) {
        try {
            currentCart = orderService.removeItem(currentCart.getId(), orderItem.getId(), false);
        } catch (RemoveFromCartException e) {
            LOG.error("Could not remove from cart.", e);
        }
    }
    for (OrderItemRequestDTO itemRequest : itemsToReprice) {
        try {
            currentCart = orderService.addItem(currentCart.getId(), itemRequest, false);
        } catch (AddToCartException e) {
            LOG.error("Could not add to cart.", e);
        }
    }
    // Reprice and save the cart
    try {
        currentCart = orderService.save(currentCart, repriceOrder);
    } catch (PricingException e) {
        LOG.error("Could not save cart.", e);
    }
    setSavedCurrency(currency);
    UpdateCartResponse updateCartResponse = new UpdateCartResponse();
    updateCartResponse.setRemovedItems(itemsToRemove);
    updateCartResponse.setOrder(currentCart);
    return updateCartResponse;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ArrayList(java.util.ArrayList) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) UpdateCartResponse(org.broadleafcommerce.core.order.service.call.UpdateCartResponse) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Aggregations

PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)11 Order (org.broadleafcommerce.core.order.domain.Order)7 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)4 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PaymentException (org.broadleafcommerce.common.vendor.service.exception.PaymentException)2 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)2 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)2 ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)2 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)2 Customer (org.broadleafcommerce.profile.core.domain.Customer)2 Collection (java.util.Collection)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)1 PaymentResponseDTO (org.broadleafcommerce.common.payment.dto.PaymentResponseDTO)1 PaymentGatewayConfigurationService (org.broadleafcommerce.common.payment.service.PaymentGatewayConfigurationService)1 CheckoutException (org.broadleafcommerce.core.checkout.service.exception.CheckoutException)1