Search in sources :

Example 56 with FulfillmentGroup

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

the class FulfillmentGroupServiceImpl method getFulfillmentGroupItemsForOrderItem.

@Override
public List<FulfillmentGroupItem> getFulfillmentGroupItemsForOrderItem(Order order, OrderItem orderItem) {
    List<FulfillmentGroupItem> fgis = new ArrayList<FulfillmentGroupItem>();
    List<FulfillmentGroup> fulfillmentGroups = order.getFulfillmentGroups();
    for (FulfillmentGroup fulfillmentGroup : fulfillmentGroups) {
        for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            if (fulfillmentGroupItem.getOrderItem().equals(orderItem)) {
                fgis.add(fulfillmentGroupItem);
            } else if (orderItem instanceof BundleOrderItem) {
                BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
                for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
                    if (fulfillmentGroupItem.getOrderItem().equals(discreteOrderItem)) {
                        fgis.add(fulfillmentGroupItem);
                        break;
                    }
                }
            }
        }
    }
    return fgis;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) ArrayList(java.util.ArrayList) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 57 with FulfillmentGroup

use of org.broadleafcommerce.core.order.domain.FulfillmentGroup 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 58 with FulfillmentGroup

use of org.broadleafcommerce.core.order.domain.FulfillmentGroup 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 59 with FulfillmentGroup

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

the class LegacyOrderServiceImpl method createDefaultFulfillmentGroup.

@Override
public FulfillmentGroup createDefaultFulfillmentGroup(Order order, Address address) {
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        if (fulfillmentGroup.isPrimary()) {
            return fulfillmentGroup;
        }
    }
    FulfillmentGroup newFg = fulfillmentGroupService.createEmptyFulfillmentGroup();
    newFg.setOrder(order);
    newFg.setPrimary(true);
    newFg.setAddress(address);
    for (OrderItem orderItem : order.getOrderItems()) {
        newFg.addFulfillmentGroupItem(createFulfillmentGroupItemFromOrderItem(orderItem, newFg, orderItem.getQuantity()));
    }
    return newFg;
}
Also used : OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 60 with FulfillmentGroup

use of org.broadleafcommerce.core.order.domain.FulfillmentGroup 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)

Aggregations

FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)67 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)39 Order (org.broadleafcommerce.core.order.domain.Order)32 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)24 Money (org.broadleafcommerce.common.money.Money)22 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)19 Transactional (org.springframework.transaction.annotation.Transactional)17 ArrayList (java.util.ArrayList)16 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)15 Address (org.broadleafcommerce.profile.core.domain.Address)15 Test (org.testng.annotations.Test)12 Sku (org.broadleafcommerce.core.catalog.domain.Sku)9 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)9 FulfillmentGroupItemImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)8 Customer (org.broadleafcommerce.profile.core.domain.Customer)8 HashMap (java.util.HashMap)7 BigDecimal (java.math.BigDecimal)6 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)6 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)6 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)5