Search in sources :

Example 16 with FulfillmentGroup

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

the class OrderTest method updateItemsInOrder.

@Test(groups = { "updateItemsInOrder" }, dependsOnGroups = { "getItemsForOrder" })
@Transactional
public void updateItemsInOrder() throws UpdateCartException, RemoveFromCartException {
    // Grab the order and the first OrderItem
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems.size() > 0;
    OrderItem item = orderItems.get(0);
    // Set the quantity of the first OrderItem to 10
    OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(10);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the quantity has changed
    OrderItem updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem != null;
    assert updatedItem.getQuantity() == 10;
    // Assert that the appropriate fulfillment group item has changed
    assert order.getFulfillmentGroups().size() == 1;
    FulfillmentGroup fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    boolean fgItemUpdated = false;
    for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
        if (fgi.getOrderItem().equals(updatedItem)) {
            assert fgi.getQuantity() == 10;
            fgItemUpdated = true;
        }
    }
    assert fgItemUpdated;
    // Set the quantity of the first OrderItem to 5
    orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(5);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the quantity has changed - going to a smaller quantity is also ok
    updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem != null;
    assert updatedItem.getQuantity() == 5;
    // Assert that the appropriate fulfillment group item has changed
    assert order.getFulfillmentGroups().size() == 1;
    fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    fgItemUpdated = false;
    for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
        if (fgi.getOrderItem().equals(updatedItem)) {
            assert fgi.getQuantity() == 5;
            fgItemUpdated = true;
        }
    }
    assert fgItemUpdated;
    // Setting the quantity to 0 should in fact remove the item completely
    int startingSize = order.getOrderItems().size();
    orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(0);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the item has been removed
    updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem == null;
    assert order.getOrderItems().size() == startingSize - 1;
    // Assert that the appropriate fulfillment group item has been removed
    assert order.getFulfillmentGroups().size() == 0;
/*
        TODO Since we commented out some tests above, there is no longer an additional item
        in the cart, hence the fulfillment group is removed

        fg = order.getFulfillmentGroups().get(0);
        assert fg.getFulfillmentGroupItems().size() == startingSize - 1;
        boolean fgItemRemoved = true;
        for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
            if (fgi.getOrderItem().equals(updatedItem)) {
                fgItemRemoved = false;
            }
        }
        assert fgItemRemoved;*/
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) 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) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with FulfillmentGroup

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

the class FulfillmentItemPricingActivity method distributeOrderSavingsToItems.

/**
 * Distributes the order adjustments (if any) to the individual fulfillment group items.
 * @param order
 * @param totalAllItems
 * @return
 */
protected Money distributeOrderSavingsToItems(Order order, BigDecimal totalAllItems) {
    Money returnAmount = new Money(order.getCurrency());
    BigDecimal orderAdjAmt = order.getOrderAdjustmentsValue().getAmount();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            BigDecimal fgItemAmount = fgItem.getTotalItemAmount().getAmount();
            BigDecimal proratedAdjAmt = totalAllItems.compareTo(BigDecimal.ZERO) == 0 ? totalAllItems : orderAdjAmt.multiply(fgItemAmount).divide(totalAllItems, RoundingMode.FLOOR);
            fgItem.setProratedOrderAdjustmentAmount(new Money(proratedAdjAmt, order.getCurrency()));
            returnAmount = returnAmount.add(fgItem.getProratedOrderAdjustmentAmount());
        }
    }
    return returnAmount;
}
Also used : Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) BigDecimal(java.math.BigDecimal)

Example 18 with FulfillmentGroup

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

the class FulfillmentItemPricingActivity method populateItemTotalAmount.

/**
 * Sets the fulfillment amount which includes the relative portion of the total price for
 * the corresponding order item.
 *
 * @param order
 * @param partialOrderItemMap
 */
protected void populateItemTotalAmount(Order order, Map<OrderItem, List<FulfillmentGroupItem>> partialOrderItemMap) {
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            OrderItem orderItem = fgItem.getOrderItem();
            int fgItemQty = fgItem.getQuantity();
            int orderItemQty = orderItem.getQuantity();
            Money totalItemAmount = orderItem.getTotalPrice();
            if (fgItemQty != orderItemQty) {
                // We need to keep track of all of these items in case we need to distribute a remainder
                // to one or more of the items.
                List<FulfillmentGroupItem> fgItemList = partialOrderItemMap.get(orderItem);
                if (fgItemList == null) {
                    fgItemList = new ArrayList<>();
                    partialOrderItemMap.put(orderItem, fgItemList);
                }
                fgItemList.add(fgItem);
                fgItem.setTotalItemAmount(totalItemAmount.multiply(fgItemQty).divide(orderItemQty));
            } else {
                fgItem.setTotalItemAmount(totalItemAmount);
            }
        }
    }
}
Also used : 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 19 with FulfillmentGroup

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

the class ShippingActivity method execute.

@Override
public ProcessContext<Order> execute(ProcessContext<Order> context) throws Exception {
    Order order = context.getSeedData();
    /*
         * 1. Get FGs from Order
         * 2. take each FG and call shipping module with the shipping svc
         * 3. add FG back to order
         */
    Money totalShipping = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        fulfillmentGroup = shippingService.calculateShippingForFulfillmentGroup(fulfillmentGroup);
        totalShipping = totalShipping.add(fulfillmentGroup.getShippingPrice());
    }
    order.setTotalShipping(totalShipping);
    context.setSeedData(order);
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Money(org.broadleafcommerce.common.money.Money) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 20 with FulfillmentGroup

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

the class TotalActivity method setTaxSums.

protected void setTaxSums(Order order) {
    if (order.getTaxOverride()) {
        Money zeroMoney = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
            if (fg.getTaxes() != null) {
                fg.getTaxes().clear();
            }
            fg.setTotalTax(zeroMoney);
            for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
                if (fgi.getTaxes() != null) {
                    fgi.getTaxes().clear();
                }
                fgi.setTotalTax(zeroMoney);
            }
            for (FulfillmentGroupFee fee : fg.getFulfillmentGroupFees()) {
                if (fee.getTaxes() != null) {
                    fee.getTaxes().clear();
                }
                fee.setTotalTax(zeroMoney);
            }
            fg.setTotalFulfillmentGroupTax(zeroMoney);
            fg.setTotalItemTax(zeroMoney);
            fg.setTotalFeeTax(zeroMoney);
        }
        order.setTotalTax(zeroMoney);
        return;
    }
    Money orderTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
        Money fgTotalFgTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        Money fgTotalItemTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        Money fgTotalFeeTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        // Add in all FG specific taxes (such as shipping tax)
        if (fg.getTaxes() != null) {
            for (TaxDetail tax : fg.getTaxes()) {
                fgTotalFgTax = fgTotalFgTax.add(tax.getAmount());
            }
        }
        for (FulfillmentGroupItem item : fg.getFulfillmentGroupItems()) {
            Money itemTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
            // Add in all taxes for this item
            if (item.getTaxes() != null) {
                for (TaxDetail tax : item.getTaxes()) {
                    itemTotalTax = itemTotalTax.add(tax.getAmount());
                }
            }
            item.setTotalTax(itemTotalTax);
            fgTotalItemTax = fgTotalItemTax.add(itemTotalTax);
        }
        for (FulfillmentGroupFee fee : fg.getFulfillmentGroupFees()) {
            Money feeTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
            // Add in all taxes for this fee
            if (fee.getTaxes() != null) {
                for (TaxDetail tax : fee.getTaxes()) {
                    feeTotalTax = feeTotalTax.add(tax.getAmount());
                }
            }
            fee.setTotalTax(feeTotalTax);
            fgTotalFeeTax = fgTotalFeeTax.add(feeTotalTax);
        }
        Money fgTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency()).add(fgTotalFgTax).add(fgTotalItemTax).add(fgTotalFeeTax);
        // Set the fulfillment group tax sums
        fg.setTotalFulfillmentGroupTax(fgTotalFgTax);
        fg.setTotalItemTax(fgTotalItemTax);
        fg.setTotalFeeTax(fgTotalFeeTax);
        fg.setTotalTax(fgTotalTax);
        orderTotalTax = orderTotalTax.add(fgTotalTax);
    }
    order.setTotalTax(orderTotalTax);
}
Also used : Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) FulfillmentGroupFee(org.broadleafcommerce.core.order.domain.FulfillmentGroupFee) TaxDetail(org.broadleafcommerce.core.order.domain.TaxDetail)

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