Search in sources :

Example 71 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class CartTest method testMoveItemToCartFromNamedOrder.

@Test(groups = { "testCartAndNamedOrder" })
@Transactional
public void testMoveItemToCartFromNamedOrder() throws RemoveFromCartException, AddToCartException {
    Order namedOrder = setUpNamedOrder();
    List<OrderItem> namedOrderItems = new ArrayList<>();
    namedOrderItems.addAll(namedOrder.getOrderItems());
    List<OrderItem> movedOrderItems = new ArrayList<>();
    movedOrderItems.add(namedOrderItems.get(0));
    Order cart = orderService.createNewCartForCustomer(namedOrder.getCustomer());
    cart = orderService.addItemFromNamedOrder(namedOrder, movedOrderItems.get(0), true);
    List<Order> customerNamedOrders = orderService.findOrdersForCustomer(namedOrder.getCustomer(), OrderStatus.NAMED);
    assert customerNamedOrders.size() == 0;
    assert cart.getOrderItems().size() == 1;
    assert namedOrder.getOrderItems().size() == 0;
    assert cartContainsOnlyTheseItems(cart, movedOrderItems);
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 72 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method testManyToOneFGItemToOrderItem.

@Test(groups = { "testManyToOneFGItemToOrderItem" }, dependsOnGroups = { "getItemsForOrder" })
@Transactional
public void testManyToOneFGItemToOrderItem() throws UpdateCartException, RemoveFromCartException, PricingException {
    // Grab the order and the first OrderItem
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems.size() > 0;
    OrderItem item = orderItems.get(0);
    // Set the quantity of the first OrderItem to 10
    OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(10);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the quantity has changed
    OrderItem updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem != null;
    assert updatedItem.getQuantity() == 10;
    // Assert that the appropriate fulfillment group item has changed
    assert order.getFulfillmentGroups().size() == 1;
    FulfillmentGroup fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    FulfillmentGroupItem fgItem = null;
    for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
        if (fgi.getOrderItem().equals(updatedItem)) {
            fgItem = fgi;
        }
    }
    assert fgItem != null;
/*
        TODO because of the merging that takes place in the offer service, these tests do not
        work unless multiship options are incorporated

        // Split one of the fulfillment group items to simulate a OneToMany relationship between
        // OrderItems and FulfillmentGroupItems
        FulfillmentGroup secondFg = fulfillmentGroupService.createEmptyFulfillmentGroup();
        secondFg.setOrder(order);
        secondFg = fulfillmentGroupService.save(secondFg);
        fgItem.setQuantity(5);
        FulfillmentGroupItem clonedFgItem = fgItem.clone();
        clonedFgItem.setFulfillmentGroup(secondFg);
        secondFg.addFulfillmentGroupItem(clonedFgItem);
        order.getFulfillmentGroups().add(secondFg);
        order = orderService.save(order, false);
        
        // Set the quantity of the first OrderItem to 15
        orderItemRequestDTO = new OrderItemRequestDTO();
        orderItemRequestDTO.setOrderItemId(item.getId());
        orderItemRequestDTO.setQuantity(15);
        order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
        
        // Assert that the quantity has changed
        updatedItem = orderItemService.readOrderItemById(item.getId());
        assert updatedItem != null;
        assert updatedItem.getQuantity() == 15;
        
        // Assert that the appropriate fulfillment group item has changed
        assert order.getFulfillmentGroups().size() == 2;
        int fgItemQuantity = 0;
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            for (FulfillmentGroupItem fgi : fulfillmentGroup.getFulfillmentGroupItems()) {
                if (fgi.getOrderItem().equals(updatedItem)) {
                    fgItemQuantity += fgi.getQuantity();
                }
            }
        }
        assert fgItemQuantity == 15;
        
        // Set the quantity of the first OrderItem to 3
        orderItemRequestDTO = new OrderItemRequestDTO();
        orderItemRequestDTO.setOrderItemId(item.getId());
        orderItemRequestDTO.setQuantity(3);
        order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
        
        // Assert that the quantity has changed
        updatedItem = orderItemService.readOrderItemById(item.getId());
        assert updatedItem != null;
        assert updatedItem.getQuantity() == 3;
        
        // Assert that the appropriate fulfillment group item has changed
        assert order.getFulfillmentGroups().size() == 2;
        boolean fgItemFound = false;
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            for (FulfillmentGroupItem fgi : fulfillmentGroup.getFulfillmentGroupItems()) {
                if (fgi.getOrderItem().equals(updatedItem)) {
                    assert fgItemFound == false;
                    assert fgi.getQuantity() == 3;
                    fgItemFound = true;
                }
            }
        }
        assert fgItemFound;
        */
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 73 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceImpl method getUniqueOffersFromOrder.

@Override
@SuppressWarnings("unchecked")
public Set<Offer> getUniqueOffersFromOrder(Order order) {
    HashSet<Offer> result = new HashSet<Offer>();
    Transformer adjustmentToOfferTransformer = new Transformer() {

        @Override
        public Object transform(Object input) {
            return ((Adjustment) input).getOffer();
        }
    };
    result.addAll(CollectionUtils.collect(order.getOrderAdjustments(), adjustmentToOfferTransformer));
    if (order.getOrderItems() != null) {
        for (OrderItem item : order.getOrderItems()) {
            result.addAll(CollectionUtils.collect(item.getOrderItemAdjustments(), adjustmentToOfferTransformer));
            // record usage for price details on the item as well
            if (item.getOrderItemPriceDetails() != null) {
                for (OrderItemPriceDetail detail : item.getOrderItemPriceDetails()) {
                    result.addAll(CollectionUtils.collect(detail.getOrderItemPriceDetailAdjustments(), adjustmentToOfferTransformer));
                }
            }
        }
    }
    if (order.getFulfillmentGroups() != null) {
        for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
            result.addAll(CollectionUtils.collect(fg.getFulfillmentGroupAdjustments(), adjustmentToOfferTransformer));
        }
    }
    return result;
}
Also used : Transformer(org.apache.commons.collections.Transformer) OrderItemPriceDetailAdjustment(org.broadleafcommerce.core.offer.domain.OrderItemPriceDetailAdjustment) Adjustment(org.broadleafcommerce.core.offer.domain.Adjustment) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) CustomerOffer(org.broadleafcommerce.core.offer.domain.CustomerOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) OrderItemPriceDetail(org.broadleafcommerce.core.order.domain.OrderItemPriceDetail) HashSet(java.util.HashSet)

Example 74 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceImpl method verifyAdjustments.

protected boolean verifyAdjustments(Order order, boolean beforeSave) {
    boolean madeChange = false;
    if (order.getOrderItems() == null) {
        return madeChange;
    }
    for (OrderItem oi : order.getOrderItems()) {
        if (oi.getOrderItemPriceDetails() == null) {
            continue;
        }
        for (OrderItemPriceDetail pd : oi.getOrderItemPriceDetails()) {
            if (pd.getOrderItemPriceDetailAdjustments() == null) {
                continue;
            }
            Map<Long, OrderItemPriceDetailAdjustment> adjs = new HashMap<Long, OrderItemPriceDetailAdjustment>();
            List<OrderItemPriceDetailAdjustment> adjustmentsToRemove = new ArrayList<OrderItemPriceDetailAdjustment>();
            for (OrderItemPriceDetailAdjustment adj : pd.getOrderItemPriceDetailAdjustments()) {
                if (adjs.containsKey(adj.getOffer().getId())) {
                    adjustmentsToRemove.add(adj);
                    if (LOG.isDebugEnabled()) {
                        StringBuilder sb = new StringBuilder("Detected collisions ").append(beforeSave ? "before saving" : "after saving").append(" with ids ").append(adjs.get(adj.getOffer().getId()).getId()).append(" and ").append(adj.getId());
                        LOG.debug(sb.toString());
                    }
                } else {
                    adjs.put(adj.getOffer().getId(), adj);
                }
            }
            for (OrderItemPriceDetailAdjustment adj : adjustmentsToRemove) {
                pd.getOrderItemPriceDetailAdjustments().remove(adj);
                madeChange = true;
            }
        }
    }
    return madeChange;
}
Also used : HashMap(java.util.HashMap) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ArrayList(java.util.ArrayList) OrderItemPriceDetail(org.broadleafcommerce.core.order.domain.OrderItemPriceDetail) OrderItemPriceDetailAdjustment(org.broadleafcommerce.core.offer.domain.OrderItemPriceDetailAdjustment)

Example 75 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceUtilitiesImpl method markRelatedQualifiersAndTargetsForItemCriteria.

@Override
public int markRelatedQualifiersAndTargetsForItemCriteria(PromotableCandidateItemOffer itemOffer, PromotableOrder order, OrderItemHolder orderItemHolder, OfferItemCriteria itemCriteria, List<PromotableOrderItemPriceDetail> priceDetails, ItemOfferMarkTargets itemOfferMarkTargets) {
    sortQualifierItemDetails(priceDetails, itemOffer.getOffer().getApplyDiscountToSalePrice());
    // Calculate the number of qualifiers needed that will not receive the promotion.
    // These will be reserved first before the target is assigned.
    int qualifierQtyNeeded = itemCriteria.getQuantity();
    for (PromotableOrderItemPriceDetail detail : priceDetails) {
        OrderItem oi = detail.getPromotableOrderItem().getOrderItem();
        if (qualifierQtyNeeded > 0) {
            int itemQtyAvailableToBeUsedAsQualifier = detail.getQuantityAvailableToBeUsedAsQualifier(itemOffer);
            if (itemQtyAvailableToBeUsedAsQualifier > 0) {
                // We have found a qualifier that meets this offer criteria. First, we'll save some state that we
                // might need in the future.
                OfferItemCriteria previousQualifierCriteria = null;
                for (PromotionQualifier possibleQualifier : detail.getPromotionQualifiers()) {
                    if (possibleQualifier.getPromotion().equals(itemOffer.getOffer())) {
                        previousQualifierCriteria = possibleQualifier.getItemCriteria();
                        break;
                    }
                }
                // Go ahead and mark this item as a qualifier
                int qtyToMarkAsQualifier = Math.min(qualifierQtyNeeded, itemQtyAvailableToBeUsedAsQualifier);
                qualifierQtyNeeded -= qtyToMarkAsQualifier;
                PromotionQualifier pq = detail.addPromotionQualifier(itemOffer, itemCriteria, qtyToMarkAsQualifier);
                pq.setPrice(detail.getPromotableOrderItem().getPriceBeforeAdjustments(itemOffer.getOffer().getApplyDiscountToSalePrice()));
                // that there is an eligible target(s) and continue on.
                if (itemOfferMarkTargets.markTargets(itemOffer, order, oi, true)) {
                    // We found a target. Let's save this related order item used as the qualifier in case
                    // we succeed
                    orderItemHolder.setOrderItem(oi);
                } else {
                    // If we didn't find a target, we need to roll back how we marked this item as a qualifier.
                    qualifierQtyNeeded += qtyToMarkAsQualifier;
                    if (pq.getQuantity() == qtyToMarkAsQualifier) {
                        detail.getPromotionQualifiers().remove(pq);
                    } else {
                        pq.setItemCriteria(previousQualifierCriteria);
                        pq.setQuantity(pq.getQuantity() - qtyToMarkAsQualifier);
                    }
                }
            }
        }
        if (qualifierQtyNeeded == 0) {
            break;
        }
    }
    return qualifierQtyNeeded;
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Aggregations

OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)100 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)69 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)54 Order (org.broadleafcommerce.core.order.domain.Order)46 ArrayList (java.util.ArrayList)32 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)23 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)20 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)19 Transactional (org.springframework.transaction.annotation.Transactional)19 Test (org.testng.annotations.Test)16 Money (org.broadleafcommerce.common.money.Money)13 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)13 HashMap (java.util.HashMap)11 Sku (org.broadleafcommerce.core.catalog.domain.Sku)10 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)9 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)7 BigDecimal (java.math.BigDecimal)5 ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)5 List (java.util.List)4 Product (org.broadleafcommerce.core.catalog.domain.Product)4