Search in sources :

Example 1 with FulfillmentGroupRequest

use of org.broadleafcommerce.core.order.service.call.FulfillmentGroupRequest in project BroadleafCommerce by BroadleafCommerce.

the class FulfillmentGroupServiceImpl method addItemToFulfillmentGroup.

@Override
public // @Transactional("blTransactionManager")
FulfillmentGroup addItemToFulfillmentGroup(FulfillmentGroupItemRequest fulfillmentGroupItemRequest, boolean priceOrder, boolean save) throws PricingException {
    if (priceOrder && !save) {
        throw new IllegalArgumentException("Pricing requires a save");
    }
    Order order = fulfillmentGroupItemRequest.getOrder();
    OrderItem item = fulfillmentGroupItemRequest.getOrderItem();
    FulfillmentGroup fulfillmentGroup = fulfillmentGroupItemRequest.getFulfillmentGroup();
    if (order == null) {
        if (item.getOrder() != null) {
            order = item.getOrder();
        } else {
            throw new IllegalArgumentException("Order must not be null");
        }
    }
    // 1) Find the order 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 == null) {
        // API user is trying to add an item to a fulfillment group not created
        fulfillmentGroup = fulfillmentGroupDao.create();
        FulfillmentGroupRequest fgRequest = new FulfillmentGroupRequest();
        fgRequest.setOrder(order);
        fulfillmentGroup = addFulfillmentGroupToOrder(fgRequest, false);
        fulfillmentGroup = save(fulfillmentGroup);
        order.getFulfillmentGroups().add(fulfillmentGroup);
    }
    FulfillmentGroupItem fgi = createFulfillmentGroupItemFromOrderItem(item, fulfillmentGroup, fulfillmentGroupItemRequest.getQuantity());
    if (save) {
        fgi = fulfillmentGroupItemDao.save(fgi);
    }
    // 3) add the item to the new fulfillment group
    fulfillmentGroup.addFulfillmentGroupItem(fgi);
    if (save) {
        order = orderService.save(order, priceOrder);
    }
    return fulfillmentGroup;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) 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) FulfillmentGroupRequest(org.broadleafcommerce.core.order.service.call.FulfillmentGroupRequest) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 2 with FulfillmentGroupRequest

use of org.broadleafcommerce.core.order.service.call.FulfillmentGroupRequest 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)

Aggregations

DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)2 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)2 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)2 FulfillmentGroupRequest (org.broadleafcommerce.core.order.service.call.FulfillmentGroupRequest)2 HashMap (java.util.HashMap)1 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)1 Order (org.broadleafcommerce.core.order.domain.Order)1 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)1 OrderMultishipOption (org.broadleafcommerce.core.order.domain.OrderMultishipOption)1 Transactional (org.springframework.transaction.annotation.Transactional)1