Search in sources :

Example 56 with OrderItem

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

the class AbstractCartRuleProcessor method checkItemCriteria.

protected boolean checkItemCriteria(ItemCriteriaDTO itemCriteria, List<OrderItem> orderItems) {
    Map<String, Object> vars = new HashMap<String, Object>();
    int foundCount = 0;
    Iterator<OrderItem> items = orderItems.iterator();
    while (foundCount < itemCriteria.getQty() && items.hasNext()) {
        OrderItem currentItem = items.next();
        vars.put("discreteOrderItem", currentItem);
        vars.put("orderItem", currentItem);
        boolean match = executeExpression(itemCriteria.getMatchRule(), vars);
        if (match) {
            foundCount = foundCount + currentItem.getQuantity();
        }
    }
    return (foundCount >= itemCriteria.getQty().intValue());
}
Also used : HashMap(java.util.HashMap) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem)

Example 57 with OrderItem

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

the class AbstractBaseProcessor method addChildOrderItemsToCandidates.

protected void addChildOrderItemsToCandidates(Offer offer, CandidatePromotionItems candidates, OfferItemCriteria criteria, List<PromotableOrderItem> promotableOrderItems, PromotableOrderItem item) {
    if (offer.getApplyToChildItems()) {
        final List<OrderItem> childItems = item.getOrderItem().getChildOrderItems();
        if (CollectionUtils.isEmpty(childItems)) {
            return;
        }
        List<PromotableOrderItem> filteredItems = new ArrayList<>();
        filteredItems.addAll(promotableOrderItems);
        CollectionUtils.filter(filteredItems, new TypedPredicate<PromotableOrderItem>() {

            @Override
            public boolean eval(PromotableOrderItem promotableOrderItem) {
                return childItems.contains(promotableOrderItem.getOrderItem());
            }
        });
        for (PromotableOrderItem promotableOrderItem : filteredItems) {
            candidates.addTarget(criteria, promotableOrderItem);
        }
    }
}
Also used : PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ArrayList(java.util.ArrayList) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)

Example 58 with OrderItem

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

the class ItemOfferProcessorImpl method markTargets.

/**
 * Loop through ItemCriteria and mark targets that can get this promotion to give the promotion to 1 or more targets.
 * @param itemOffer
 * @param order
 * @return
 */
public boolean markTargets(PromotableCandidateItemOffer itemOffer, PromotableOrder order, OrderItem relatedQualifier, boolean checkOnly) {
    Offer promotion = itemOffer.getOffer();
    if (itemOffer.getCandidateTargetsMap().keySet().isEmpty()) {
        return false;
    }
    OrderItem relatedQualifierRoot = offerServiceUtilities.findRelatedQualifierRoot(relatedQualifier);
    for (OfferItemCriteria itemCriteria : itemOffer.getCandidateTargetsMap().keySet()) {
        List<PromotableOrderItem> promotableItems = itemOffer.getCandidateTargetsMap().get(itemCriteria);
        List<PromotableOrderItemPriceDetail> priceDetails = buildPriceDetailListFromOrderItems(promotableItems);
        offerServiceUtilities.sortTargetItemDetails(priceDetails, itemOffer.getOffer().getApplyDiscountToSalePrice());
        int targetQtyNeeded = itemCriteria.getQuantity();
        // This code was added to support a requirement of buying at least "x" matching items
        // but also applying the item to "x+1", "x+2", etc.
        int minRequiredTargetQuantity = itemOffer.getMinimumRequiredTargetQuantity();
        if (minRequiredTargetQuantity > 1 && minRequiredTargetQuantity > targetQtyNeeded) {
            targetQtyNeeded = minRequiredTargetQuantity;
        }
        targetQtyNeeded = offerServiceUtilities.markTargetsForCriteria(itemOffer, relatedQualifier, checkOnly, promotion, relatedQualifierRoot, itemCriteria, priceDetails, targetQtyNeeded);
        if (targetQtyNeeded != 0) {
            return false;
        }
    }
    if (!checkOnly) {
        itemOffer.addUse();
    }
    return true;
}
Also used : PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Example 59 with OrderItem

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

the class OrderItemDaoImpl method delete.

@Override
@Transactional("blTransactionManager")
public void delete(OrderItem orderItem) {
    if (!em.contains(orderItem)) {
        orderItem = readOrderItemById(orderItem.getId());
    }
    if (GiftWrapOrderItem.class.isAssignableFrom(orderItem.getClass())) {
        final GiftWrapOrderItem giftItem = (GiftWrapOrderItem) orderItem;
        for (OrderItem wrappedItem : giftItem.getWrappedItems()) {
            wrappedItem.setGiftWrapOrderItem(null);
            wrappedItem = save(wrappedItem);
        }
    }
    em.remove(orderItem);
    em.flush();
}
Also used : OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 60 with OrderItem

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

the class AutoBundleActivity method bundleItems.

/**
 * Builds a BundleOrderItem based on the passed in productBundle.    Creates new DiscreteOrderItems.
 * Removes the existing matching DiscreteOrderItems or modifies the quantity if needed.
 *
 * @param order
 * @param productBundle
 * @param numApplications
 * @throws PricingException
 * @throws ItemNotFoundException
 */
private Order bundleItems(Order order, ProductBundle productBundle, Integer numApplications, List<DiscreteOrderItem> unbundledItems) throws PricingException, RemoveFromCartException {
    BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemDao.create(OrderItemType.BUNDLE);
    bundleOrderItem.setQuantity(numApplications);
    bundleOrderItem.setCategory(productBundle.getDefaultCategory());
    bundleOrderItem.setSku(productBundle.getDefaultSku());
    bundleOrderItem.setName(productBundle.getName());
    bundleOrderItem.setProductBundle(productBundle);
    bundleOrderItem.setOrder(order);
    // make a copy of the fulfillment group items since they will be deleted when the order items are removed
    Map<Long, FulfillmentGroupItem> skuIdFulfillmentGroupMap = new HashMap<Long, FulfillmentGroupItem>();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            if (fulfillmentGroupItem.getOrderItem() instanceof DiscreteOrderItem) {
                DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) fulfillmentGroupItem.getOrderItem();
                skuIdFulfillmentGroupMap.put(discreteOrderItem.getSku().getId(), fulfillmentGroupItem);
            }
        }
    }
    for (SkuBundleItem skuBundleItem : productBundle.getSkuBundleItems()) {
        List<DiscreteOrderItem> itemMatches = new ArrayList<DiscreteOrderItem>();
        int skuMatches = populateItemMatchesForSku(itemMatches, order, unbundledItems, skuBundleItem.getSku().getId());
        int skusRequired = skuBundleItem.getQuantity() * numApplications;
        if (skuMatches < skusRequired) {
            throw new IllegalArgumentException("Something went wrong creating automatic bundles.  Not enough skus to fulfill bundle requirements for sku id: " + skuBundleItem.getSku().getId());
        }
        // this call also deletes any fulfillment group items that are associated with that order item
        for (DiscreteOrderItem item : itemMatches) {
            order = orderService.removeItem(order.getId(), item.getId(), false);
        }
        DiscreteOrderItem baseItem = null;
        if (itemMatches.size() > 0) {
            baseItem = itemMatches.get(0);
        } else {
            for (DiscreteOrderItem discreteOrderItem : unbundledItems) {
                if (discreteOrderItem.getSku().getId().equals(skuBundleItem.getSku().getId())) {
                    baseItem = discreteOrderItem;
                }
            }
        }
        // Add item to the skuBundle
        DiscreteOrderItem newSkuBundleItem = (DiscreteOrderItem) baseItem.clone();
        newSkuBundleItem.setSkuBundleItem(skuBundleItem);
        newSkuBundleItem.setBundleOrderItem(bundleOrderItem);
        newSkuBundleItem.setQuantity(skuBundleItem.getQuantity());
        newSkuBundleItem.setOrder(null);
        bundleOrderItem.getDiscreteOrderItems().add(newSkuBundleItem);
        if (skuMatches > skusRequired) {
            // Add a non-bundled item to the order with the remaining sku count.
            DiscreteOrderItem newOrderItem = (DiscreteOrderItem) baseItem.clone();
            newOrderItem.setBundleOrderItem(null);
            newOrderItem.setSkuBundleItem(null);
            newOrderItem.setQuantity(skuMatches - skusRequired);
            newOrderItem = (DiscreteOrderItem) orderItemDao.save(newOrderItem);
            newOrderItem.setOrder(order);
            newOrderItem.updateSaleAndRetailPrices();
            // Re-associate fulfillment group item to newOrderItem
            FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(newSkuBundleItem.getSku().getId());
            if (fulfillmentGroupItem != null) {
                FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
                newFulfillmentGroupItem.setOrderItem(newOrderItem);
                newFulfillmentGroupItem.setQuantity(newOrderItem.getQuantity());
                newFulfillmentGroupItem = fulfillmentGroupItemDao.save(newFulfillmentGroupItem);
                // these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
                for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                    if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
                        fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
                    }
                }
            }
            order.getOrderItems().add(newOrderItem);
        }
    }
    bundleOrderItem.updateSaleAndRetailPrices();
    order.getOrderItems().add(bundleOrderItem);
    order = orderService.save(order, false);
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem discreteOrderItem : bundleItem.getDiscreteOrderItems()) {
                // Re-associate fulfillment group item to newly created skuBundles
                FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(discreteOrderItem.getSku().getId());
                if (fulfillmentGroupItem != null) {
                    FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
                    newFulfillmentGroupItem.setOrderItem(discreteOrderItem);
                    newFulfillmentGroupItem.setQuantity(discreteOrderItem.getQuantity());
                    // these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
                    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                        if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
                            fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
                        }
                    }
                }
            }
        }
    }
    // reload order with new fulfillment group items
    order = orderService.save(order, false);
    return order;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

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