Search in sources :

Example 46 with FulfillmentGroupItem

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

the class AddWorkflowPriceOrderIfNecessaryActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    Order order = request.getOrder();
    // go ahead and carry out that delete here.
    if (CollectionUtils.isNotEmpty(request.getMultishipOptionsToDelete())) {
        for (Long[] pack : request.getMultishipOptionsToDelete()) {
            if (pack[1] == null) {
                orderMultishipOptionService.deleteOrderItemOrderMultishipOptions(pack[0]);
            } else {
                orderMultishipOptionService.deleteOrderItemOrderMultishipOptions(pack[0], pack[1].intValue());
            }
        }
    }
    // ones that should be deleted. Delete them here.
    if (CollectionUtils.isNotEmpty(request.getFgisToDelete())) {
        for (FulfillmentGroupItem fgi : request.getFgisToDelete()) {
            for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                ListIterator<FulfillmentGroupItem> fgItemIter = fg.getFulfillmentGroupItems().listIterator();
                while (fgItemIter.hasNext()) {
                    FulfillmentGroupItem fgi2 = fgItemIter.next();
                    if (fgi2 == fgi) {
                        fgItemIter.remove();
                        fgItemDao.delete(fgi2);
                    }
                }
            }
        }
    }
    // We now need to delete any OrderItems that were marked as such, including their children, if any
    for (OrderItem oi : request.getOisToDelete()) {
        order.getOrderItems().remove(oi);
        orderItemService.delete(oi);
        if (oi.getParentOrderItem() != null) {
            OrderItem parentItem = oi.getParentOrderItem();
            parentItem.getChildOrderItems().remove(oi);
        }
    }
    // We need to build up a map of OrderItem to which FulfillmentGroupItems reference that particular OrderItem.
    // We'll also save the order item and build up a map of the unsaved items to their saved counterparts.
    Map<OrderItem, List<FulfillmentGroupItem>> oiFgiMap = new HashMap<>();
    Map<OrderItem, OrderItem> savedOrderItems = new HashMap<>();
    for (OrderItem oi : order.getOrderItems()) {
        if (oi instanceof BundleOrderItem) {
            // We first need to save the discrete order items that are part of this bundle. Once they're saved, we'll
            // mark them and remove them from this bundle.
            List<DiscreteOrderItem> doisToAdd = new ArrayList<>();
            ListIterator<DiscreteOrderItem> li = ((BundleOrderItem) oi).getDiscreteOrderItems().listIterator();
            while (li.hasNext()) {
                DiscreteOrderItem doi = li.next();
                getOiFgiMap(order, oiFgiMap, doi);
                DiscreteOrderItem savedDoi = (DiscreteOrderItem) orderItemService.saveOrderItem(doi);
                savedOrderItems.put(doi, savedDoi);
                li.remove();
                doisToAdd.add(savedDoi);
            }
            // After the discrete order items are saved, we can re-add the saved versions to our bundle and then
            // save the bundle as well.
            ((BundleOrderItem) oi).getDiscreteOrderItems().addAll(doisToAdd);
            BundleOrderItem savedBoi = (BundleOrderItem) orderItemService.saveOrderItem(oi);
            savedOrderItems.put(oi, savedBoi);
            // to to the saved version of the bundle.
            for (DiscreteOrderItem doi : savedBoi.getDiscreteOrderItems()) {
                doi.setBundleOrderItem(savedBoi);
            }
        } else {
            getOiFgiMap(order, oiFgiMap, oi);
            savedOrderItems.put(oi, orderItemService.saveOrderItem(oi));
        }
    }
    // Now, we'll update the orderitems in the order to their saved counterparts
    ListIterator<OrderItem> li = order.getOrderItems().listIterator();
    List<OrderItem> oisToAdd = new ArrayList<>();
    while (li.hasNext()) {
        OrderItem oi = li.next();
        OrderItem savedOi = savedOrderItems.get(oi);
        oisToAdd.add(savedOi);
        li.remove();
    }
    order.getOrderItems().addAll(oisToAdd);
    for (Entry<OrderItem, List<FulfillmentGroupItem>> entry : oiFgiMap.entrySet()) {
        // Update any FulfillmentGroupItems that reference order items
        for (FulfillmentGroupItem fgi : entry.getValue()) {
            fgi.setOrderItem(savedOrderItems.get(entry.getKey()));
        }
        // We also need to update the orderItem on the request in case it's used by the caller of this workflow
        if (entry.getKey() == request.getOrderItem()) {
            request.setOrderItem(savedOrderItems.get(entry.getKey()));
        }
    }
    // We need to add the new item to the parent's child order items as well.
    updateChildOrderItem(request, order);
    // If a custom implementation needs to handle additional saves before the parent Order is saved, this method
    // can be overridden to provide that functionality.
    preSaveOperation(request);
    // Now that our collection items in our Order have been saved and the state of our Order is in a place where we
    // won't get a transient save exception, we are able to go ahead and save the order with optional pricing.
    order = orderService.save(order, request.isPriceOrder());
    request.setOrder(order);
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) 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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 47 with FulfillmentGroupItem

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

the class FulfillmentGroupMerchandiseTotalActivity method execute.

@Override
public ProcessContext<Order> execute(ProcessContext<Order> context) throws Exception {
    Order order = context.getSeedData();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        Money merchandiseTotal = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, fulfillmentGroup.getOrder().getCurrency());
        for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            OrderItem item = fulfillmentGroupItem.getOrderItem();
            merchandiseTotal = merchandiseTotal.add(item.getTotalPrice());
        }
        fulfillmentGroup.setMerchandiseTotal(merchandiseTotal);
    }
    context.setSeedData(order);
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 48 with FulfillmentGroupItem

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

the class FulfillmentItemPricingActivity method fixOrderSavingsRoundingIssues.

/**
 * It is possible due to rounding that the order adjustments do not match the
 * total.   This method fixes by adding or removing the pennies.
 * @param order
 * @param partialOrderItemMap
 */
protected void fixOrderSavingsRoundingIssues(Order order, Money totalOrderAdjustmentDistributed) {
    if (!order.getHasOrderAdjustments()) {
        return;
    }
    Money orderAdjustmentTotal = order.getOrderAdjustmentsValue();
    Money amountDiff = orderAdjustmentTotal.subtract(totalOrderAdjustmentDistributed);
    if (!(amountDiff.getAmount().compareTo(BigDecimal.ZERO) == 0)) {
        long numApplicationsNeeded = countNumberOfUnits(amountDiff);
        Money unitAmount = getUnitAmount(amountDiff);
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
                numApplicationsNeeded = numApplicationsNeeded - applyDifferenceToProratedAdj(fgItem, numApplicationsNeeded, unitAmount);
                if (numApplicationsNeeded == 0) {
                    break;
                }
            }
        }
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) 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