Search in sources :

Example 41 with FulfillmentGroupItem

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

the class FulfillmentGroupServiceImpl method matchFulfillmentGroupsToMultishipOptions.

@Override
@Transactional("blTransactionManager")
public Order matchFulfillmentGroupsToMultishipOptions(Order order, boolean priceOrder) throws PricingException {
    List<OrderMultishipOption> multishipOptions = orderMultishipOptionService.findOrderMultishipOptions(order.getId());
    // Build map of fulfillmentGroupItemId --> FulfillmentGroupItem.quantity
    // Also build map of addressId:fulfillmentOptionId --> FulfillmentGroup
    Map<Long, Integer> fgItemQuantityMap = new HashMap<Long, Integer>();
    Map<String, FulfillmentGroup> multishipGroups = new HashMap<String, FulfillmentGroup>();
    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
        if (!isShippable(fg.getType())) {
            continue;
        }
        String key = getKey(fg.getAddress(), fg.getFulfillmentOption(), fg.getType());
        multishipGroups.put(key, fg);
        for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
            fgItemQuantityMap.put(fgi.getId(), fgi.getQuantity());
        }
    }
    for (OrderMultishipOption option : multishipOptions) {
        String key = getKey(option.getAddress(), option.getFulfillmentOption(), ((DiscreteOrderItem) option.getOrderItem()).getSku().getFulfillmentType());
        FulfillmentGroup fg = multishipGroups.get(key);
        // Get or create a fulfillment group that matches this OrderMultishipOption destination
        if (fg == null) {
            FulfillmentGroupRequest fgr = new FulfillmentGroupRequest();
            fgr.setOrder(order);
            if (option.getAddress() != null) {
                fgr.setAddress(option.getAddress());
            }
            if (option.getFulfillmentOption() != null) {
                fgr.setOption(option.getFulfillmentOption());
            }
            fgr.setFulfillmentType(((DiscreteOrderItem) option.getOrderItem()).getSku().getFulfillmentType());
            fg = addFulfillmentGroupToOrder(fgr, false);
            fg = save(fg);
            order.getFulfillmentGroups().add(fg);
        }
        // See if there is a fulfillment group item that matches this OrderMultishipOption
        // OrderItem request
        FulfillmentGroupItem fulfillmentGroupItem = null;
        for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
            if (fgi.getOrderItem().getId().equals(option.getOrderItem().getId())) {
                fulfillmentGroupItem = fgi;
            }
        }
        // If there is no matching fulfillment group item, create a new one with quantity 1
        if (fulfillmentGroupItem == null) {
            fulfillmentGroupItem = fulfillmentGroupItemDao.create();
            fulfillmentGroupItem.setFulfillmentGroup(fg);
            fulfillmentGroupItem.setOrderItem(option.getOrderItem());
            fulfillmentGroupItem.setQuantity(1);
            fulfillmentGroupItem = fulfillmentGroupItemDao.save(fulfillmentGroupItem);
            fg.getFulfillmentGroupItems().add(fulfillmentGroupItem);
        } else {
            // (fulfillmentGroupItem.getId() == null)
            if (fulfillmentGroupItem.getId() != null) {
                Integer qty = fgItemQuantityMap.get(fulfillmentGroupItem.getId());
                if (qty == null || qty == 0) {
                    fulfillmentGroupItem.setQuantity(fulfillmentGroupItem.getQuantity() + 1);
                } else {
                    qty -= 1;
                    fgItemQuantityMap.put(fulfillmentGroupItem.getId(), qty);
                }
            } else {
                fulfillmentGroupItem.setQuantity(fulfillmentGroupItem.getQuantity() + 1);
            }
        }
        multishipGroups.put(key, fg);
    }
    // Furthermore, delete the empty fulfillment groups.
    for (Entry<Long, Integer> entry : fgItemQuantityMap.entrySet()) {
        if (entry.getValue() > 0) {
            FulfillmentGroupItem fgi = fulfillmentGroupItemDao.readFulfillmentGroupItemById(entry.getKey());
            if (fgi.getQuantity() == entry.getValue()) {
                FulfillmentGroup fg = fgi.getFulfillmentGroup();
                fg.getFulfillmentGroupItems().remove(fgi);
                fulfillmentGroupItemDao.delete(fgi);
                if (fg.getFulfillmentGroupItems().size() == 0) {
                    order.getFulfillmentGroups().remove(fg);
                    fulfillmentGroupDao.delete(fg);
                }
            } else {
                fgi.setQuantity(fgi.getQuantity() - entry.getValue());
                fulfillmentGroupItemDao.save(fgi);
            }
        }
    }
    return orderService.save(order, priceOrder);
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) FulfillmentGroupRequest(org.broadleafcommerce.core.order.service.call.FulfillmentGroupRequest) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) OrderMultishipOption(org.broadleafcommerce.core.order.domain.OrderMultishipOption) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with FulfillmentGroupItem

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

the class FulfillmentGroupServiceImpl method collapseToOneShippableFulfillmentGroup.

@Override
@Transactional("blTransactionManager")
public Order collapseToOneShippableFulfillmentGroup(Order order, boolean priceOrder) throws PricingException {
    if (order.getFulfillmentGroups() == null || order.getFulfillmentGroups().size() < 2) {
        return order;
    }
    List<FulfillmentGroup> shippableFulfillmentGroupList = new ArrayList<FulfillmentGroup>();
    List<FulfillmentGroup> nonShippableFulfillmentGroupList = new ArrayList<FulfillmentGroup>();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        if (isShippable(fulfillmentGroup.getType())) {
            shippableFulfillmentGroupList.add(fulfillmentGroup);
        } else {
            nonShippableFulfillmentGroupList.add(fulfillmentGroup);
        }
    }
    if (shippableFulfillmentGroupList.size() < 2) {
        return order;
    }
    // Get the default (first) shippable fulfillment group to collapse the others into
    ListIterator<FulfillmentGroup> fgIter = shippableFulfillmentGroupList.listIterator();
    FulfillmentGroup collapsedFg = fgIter.next();
    List<FulfillmentGroup> newFulfillmentGroupList = nonShippableFulfillmentGroupList;
    newFulfillmentGroupList.add(collapsedFg);
    order.setFulfillmentGroups(newFulfillmentGroupList);
    // Build out a map representing the default shippable fgs items keyed by OrderItem id
    Map<Long, FulfillmentGroupItem> fgOrderItemMap = new HashMap<Long, FulfillmentGroupItem>();
    for (FulfillmentGroupItem fgi : collapsedFg.getFulfillmentGroupItems()) {
        fgOrderItemMap.put(fgi.getOrderItem().getId(), fgi);
    }
    // For all non default shippable fgs, collapse the items into the default shippable fg
    while (fgIter.hasNext()) {
        FulfillmentGroup fg = fgIter.next();
        ListIterator<FulfillmentGroupItem> fgItemIter = fg.getFulfillmentGroupItems().listIterator();
        while (fgItemIter.hasNext()) {
            FulfillmentGroupItem fgi = fgItemIter.next();
            Long orderItemId = fgi.getOrderItem().getId();
            FulfillmentGroupItem matchingFgi = fgOrderItemMap.get(orderItemId);
            if (matchingFgi == null) {
                matchingFgi = fulfillmentGroupItemDao.create();
                matchingFgi.setFulfillmentGroup(collapsedFg);
                matchingFgi.setOrderItem(fgi.getOrderItem());
                matchingFgi.setQuantity(fgi.getQuantity());
                matchingFgi = fulfillmentGroupItemDao.save(matchingFgi);
                collapsedFg.getFulfillmentGroupItems().add(matchingFgi);
                fgOrderItemMap.put(orderItemId, matchingFgi);
            } else {
                matchingFgi.setQuantity(matchingFgi.getQuantity() + fgi.getQuantity());
            }
            fulfillmentGroupItemDao.delete(fgi);
            fgItemIter.remove();
        }
        fulfillmentGroupDao.delete(fg);
        fgIter.remove();
    }
    return orderService.save(order, priceOrder);
}
Also used : HashMap(java.util.HashMap) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) ArrayList(java.util.ArrayList) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Example 43 with FulfillmentGroupItem

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

the class LegacyOrderServiceImpl method addItemToFulfillmentGroup.

@Override
public FulfillmentGroup addItemToFulfillmentGroup(Order order, OrderItem item, FulfillmentGroup fulfillmentGroup, int quantity, boolean priceOrder) throws PricingException {
    // 1) Find the item's existing fulfillment group, if any
    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
        Iterator<FulfillmentGroupItem> itr = fg.getFulfillmentGroupItems().iterator();
        while (itr.hasNext()) {
            FulfillmentGroupItem fgItem = itr.next();
            if (fgItem.getOrderItem().equals(item)) {
                // 2) remove item from it's existing fulfillment group
                itr.remove();
                fulfillmentGroupItemDao.delete(fgItem);
            }
        }
    }
    if (fulfillmentGroup.getId() == null) {
        // API user is trying to add an item to a fulfillment group not created
        fulfillmentGroup = addFulfillmentGroupToOrder(order, fulfillmentGroup, priceOrder);
    }
    FulfillmentGroupItem fgi = createFulfillmentGroupItemFromOrderItem(item, fulfillmentGroup, quantity);
    fgi = fulfillmentGroupItemDao.save(fgi);
    // 3) add the item to the new fulfillment group
    fulfillmentGroup.addFulfillmentGroupItem(fgi);
    order = updateOrder(order, priceOrder);
    return fulfillmentGroup;
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 44 with FulfillmentGroupItem

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

the class LegacyOrderServiceImpl method addFulfillmentGroupToOrder.

@Override
public FulfillmentGroup addFulfillmentGroupToOrder(Order order, FulfillmentGroup fulfillmentGroup, boolean priceOrder) throws PricingException {
    FulfillmentGroup dfg = findDefaultFulfillmentGroupForOrder(order);
    if (dfg == null) {
        fulfillmentGroup.setPrimary(true);
    } else if (dfg.equals(fulfillmentGroup)) {
        // API user is trying to re-add the default fulfillment group to the
        // same order
        fulfillmentGroup.setPrimary(true);
        order.getFulfillmentGroups().remove(dfg);
    // fulfillmentGroupDao.delete(dfg);
    }
    fulfillmentGroup.setOrder(order);
    // 1) For each item in the new fulfillment group
    for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
        // 2) Find the item's existing fulfillment group
        for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
            // fulfillment group
            if (!fg.equals(fulfillmentGroup)) {
                // 3) remove item from it's existing fulfillment
                // group
                fg.getFulfillmentGroupItems().remove(fgItem);
            }
        }
    }
    fulfillmentGroup = fulfillmentGroupDao.save(fulfillmentGroup);
    order.getFulfillmentGroups().add(fulfillmentGroup);
    int fulfillmentGroupIndex = order.getFulfillmentGroups().size() - 1;
    order = updateOrder(order, priceOrder);
    return order.getFulfillmentGroups().get(fulfillmentGroupIndex);
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 45 with FulfillmentGroupItem

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

the class LegacyOrderServiceImpl method removeOrderItemFromFullfillmentGroup.

protected void removeOrderItemFromFullfillmentGroup(Order order, OrderItem orderItem) {
    List<FulfillmentGroup> fulfillmentGroups = order.getFulfillmentGroups();
    for (FulfillmentGroup fulfillmentGroup : fulfillmentGroups) {
        Iterator<FulfillmentGroupItem> itr = fulfillmentGroup.getFulfillmentGroupItems().iterator();
        while (itr.hasNext()) {
            FulfillmentGroupItem fulfillmentGroupItem = itr.next();
            if (fulfillmentGroupItem.getOrderItem().equals(orderItem)) {
                itr.remove();
                fulfillmentGroupItemDao.delete(fulfillmentGroupItem);
            } else if (orderItem instanceof BundleOrderItem) {
                BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
                for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
                    if (fulfillmentGroupItem.getOrderItem().equals(discreteOrderItem)) {
                        itr.remove();
                        fulfillmentGroupItemDao.delete(fulfillmentGroupItem);
                        break;
                    }
                }
            }
        }
    }
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Aggregations

FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)48 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)39 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)25 Order (org.broadleafcommerce.core.order.domain.Order)21 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)20 Money (org.broadleafcommerce.common.money.Money)18 ArrayList (java.util.ArrayList)16 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)15 Sku (org.broadleafcommerce.core.catalog.domain.Sku)12 Transactional (org.springframework.transaction.annotation.Transactional)11 FulfillmentGroupItemImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)9 Address (org.broadleafcommerce.profile.core.domain.Address)9 BigDecimal (java.math.BigDecimal)8 Test (org.testng.annotations.Test)8 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)7 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)7 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)7 HashMap (java.util.HashMap)6 AddressImpl (org.broadleafcommerce.profile.core.domain.AddressImpl)6 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)5