Search in sources :

Example 21 with OrderItemRequestDTO

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

the class UpdateOrderItemActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
    Order order = request.getOrder();
    OrderItem orderItem = null;
    for (OrderItem oi : order.getOrderItems()) {
        if (oi.getId().equals(orderItemRequestDTO.getOrderItemId())) {
            orderItem = oi;
        }
    }
    if (orderItem == null || !order.getOrderItems().contains(orderItem)) {
        throw new ItemNotFoundException("Order Item (" + orderItemRequestDTO.getOrderItemId() + ") not found in Order (" + order.getId() + ")");
    }
    OrderItem itemFromOrder = order.getOrderItems().get(order.getOrderItems().indexOf(orderItem));
    if (orderItemRequestDTO.getQuantity() >= 0) {
        int previousQty = itemFromOrder.getQuantity();
        request.setOrderItemQuantityDelta(orderItemRequestDTO.getQuantity() - itemFromOrder.getQuantity());
        itemFromOrder.setQuantity(orderItemRequestDTO.getQuantity());
        for (OrderItem child : itemFromOrder.getChildOrderItems()) {
            int childQuantity = child.getQuantity();
            childQuantity = childQuantity / previousQty;
            child.setQuantity(childQuantity * orderItemRequestDTO.getQuantity());
        }
        // Update any additional attributes of the passed in request
        if (itemFromOrder instanceof DiscreteOrderItem) {
            DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) itemFromOrder;
            discreteOrderItem.getAdditionalAttributes().putAll(orderItemRequestDTO.getAdditionalAttributes());
        }
        request.setOrderItem(itemFromOrder);
    }
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ItemNotFoundException(org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)

Example 22 with OrderItemRequestDTO

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

the class OrderBaseTest method setUpNamedOrder.

public Order setUpNamedOrder() throws AddToCartException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());
    Order order = orderService.createNamedOrderForCustomer("Boxes Named Order", customer);
    Product newProduct = addTestProduct("Cube Box", "Boxes");
    Category newCategory = newProduct.getDefaultCategory();
    order = orderService.addItem(order.getId(), new OrderItemRequestDTO(newProduct.getId(), newProduct.getDefaultSku().getId(), newCategory.getId(), 2), true);
    return order;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Category(org.broadleafcommerce.core.catalog.domain.Category) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) Customer(org.broadleafcommerce.profile.core.domain.Customer) Product(org.broadleafcommerce.core.catalog.domain.Product)

Example 23 with OrderItemRequestDTO

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

the class OrderTest method testManyToOneFGItemToOrderItem.

@Test(groups = { "testManyToOneFGItemToOrderItem" }, dependsOnGroups = { "getItemsForOrder" })
@Transactional
public void testManyToOneFGItemToOrderItem() throws UpdateCartException, RemoveFromCartException, PricingException {
    // 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;
    FulfillmentGroupItem fgItem = null;
    for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
        if (fgi.getOrderItem().equals(updatedItem)) {
            fgItem = fgi;
        }
    }
    assert fgItem != null;
/*
        TODO because of the merging that takes place in the offer service, these tests do not
        work unless multiship options are incorporated

        // Split one of the fulfillment group items to simulate a OneToMany relationship between
        // OrderItems and FulfillmentGroupItems
        FulfillmentGroup secondFg = fulfillmentGroupService.createEmptyFulfillmentGroup();
        secondFg.setOrder(order);
        secondFg = fulfillmentGroupService.save(secondFg);
        fgItem.setQuantity(5);
        FulfillmentGroupItem clonedFgItem = fgItem.clone();
        clonedFgItem.setFulfillmentGroup(secondFg);
        secondFg.addFulfillmentGroupItem(clonedFgItem);
        order.getFulfillmentGroups().add(secondFg);
        order = orderService.save(order, false);
        
        // Set the quantity of the first OrderItem to 15
        orderItemRequestDTO = new OrderItemRequestDTO();
        orderItemRequestDTO.setOrderItemId(item.getId());
        orderItemRequestDTO.setQuantity(15);
        order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
        
        // Assert that the quantity has changed
        updatedItem = orderItemService.readOrderItemById(item.getId());
        assert updatedItem != null;
        assert updatedItem.getQuantity() == 15;
        
        // Assert that the appropriate fulfillment group item has changed
        assert order.getFulfillmentGroups().size() == 2;
        int fgItemQuantity = 0;
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            for (FulfillmentGroupItem fgi : fulfillmentGroup.getFulfillmentGroupItems()) {
                if (fgi.getOrderItem().equals(updatedItem)) {
                    fgItemQuantity += fgi.getQuantity();
                }
            }
        }
        assert fgItemQuantity == 15;
        
        // Set the quantity of the first OrderItem to 3
        orderItemRequestDTO = new OrderItemRequestDTO();
        orderItemRequestDTO.setOrderItemId(item.getId());
        orderItemRequestDTO.setQuantity(3);
        order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
        
        // Assert that the quantity has changed
        updatedItem = orderItemService.readOrderItemById(item.getId());
        assert updatedItem != null;
        assert updatedItem.getQuantity() == 3;
        
        // Assert that the appropriate fulfillment group item has changed
        assert order.getFulfillmentGroups().size() == 2;
        boolean fgItemFound = false;
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            for (FulfillmentGroupItem fgi : fulfillmentGroup.getFulfillmentGroupItems()) {
                if (fgi.getOrderItem().equals(updatedItem)) {
                    assert fgItemFound == false;
                    assert fgi.getQuantity() == 3;
                    fgItemFound = true;
                }
            }
        }
        assert fgItemFound;
        */
}
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 24 with OrderItemRequestDTO

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

the class OrderTest method addItemToOrder.

@Test(groups = { "addItemToOrder" }, dependsOnGroups = { "findCurrentCartForCustomer", "createSku", "testCatalog" })
@Rollback(false)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void addItemToOrder() throws AddToCartException {
    numOrderItems++;
    // In the database, some Skus are inactive and some are active. This ensures that we pull back an active one
    // to test a successful cart add
    Sku sku = getFirstActiveSku();
    Order order = orderService.findOrderById(orderId);
    assert order != null;
    assert sku.getId() != null;
    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
    itemRequest.setQuantity(1);
    itemRequest.setSkuId(sku.getId());
    order = orderService.addItem(orderId, itemRequest, true);
    DiscreteOrderItem item = (DiscreteOrderItem) orderService.findLastMatchingItem(order, sku.getId(), null);
    assert item != null;
    assert item.getQuantity() == numOrderItems;
    assert item.getSku() != null;
    assert item.getSku().equals(sku);
    assert order.getFulfillmentGroups().size() == 1;
    FulfillmentGroup fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    FulfillmentGroupItem fgItem = fg.getFulfillmentGroupItems().get(0);
    assert fgItem.getOrderItem().equals(item);
    assert fgItem.getQuantity() == item.getQuantity();
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with OrderItemRequestDTO

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

the class OrderTest method addAnotherItemToOrder.

@Test(groups = { "addAnotherItemToOrder" }, dependsOnGroups = { "addItemToOrder" })
@Rollback(false)
@Transactional
public void addAnotherItemToOrder() throws AddToCartException, PricingException, RemoveFromCartException {
    // In the database, some Skus are inactive and some are active. This ensures that we pull back an active one
    // to test a successful cart add
    Sku sku = getFirstActiveSku();
    Order order = orderService.findOrderById(orderId);
    assert order != null;
    assert sku.getId() != null;
    orderService.setAutomaticallyMergeLikeItems(true);
    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
    itemRequest.setQuantity(1);
    itemRequest.setSkuId(sku.getId());
    // Note that we are not incrementing the numOrderItems count because it should have gotten merged
    order = orderService.addItem(orderId, itemRequest, true);
    DiscreteOrderItem item = (DiscreteOrderItem) orderService.findLastMatchingItem(order, sku.getId(), null);
    assert item.getSku() != null;
    assert item.getSku().equals(sku);
    // item-was merged with prior item.
    assert item.getQuantity() == 2;
    order = orderService.findOrderById(orderId);
    assert (order.getOrderItems().size() == 1);
    assert (order.getOrderItems().get(0).getQuantity() == 2);
    assert order.getFulfillmentGroups().size() == 1;
    FulfillmentGroup fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    FulfillmentGroupItem fgItem = fg.getFulfillmentGroupItems().get(0);
    assert fgItem.getOrderItem().equals(item);
    assert fgItem.getQuantity() == item.getQuantity();
    // add the same item multiple times to the cart without merging or pricing
    boolean currentVal = orderService.getAutomaticallyMergeLikeItems();
    orderService.setAutomaticallyMergeLikeItems(false);
    itemRequest = new OrderItemRequestDTO();
    itemRequest.setQuantity(1);
    itemRequest.setSkuId(sku.getId());
    order = orderService.addItem(orderId, itemRequest, false);
    order = orderService.addItem(orderId, itemRequest, false);
    DiscreteOrderItem item2 = (DiscreteOrderItem) orderService.findLastMatchingItem(order, sku.getId(), null);
    assert item2.getSku() != null;
    assert item2.getSku().equals(sku);
    // item-was not auto-merged with prior items.
    assert item2.getQuantity() == 1;
    order = orderService.findOrderById(orderId);
    assert (order.getOrderItems().size() == 3);
    // reset the cart state back to what it was prior
    order = orderService.removeItem(order.getId(), order.getOrderItems().get(1).getId(), true);
    order = orderService.removeItem(order.getId(), order.getOrderItems().get(1).getId(), true);
    orderService.setAutomaticallyMergeLikeItems(currentVal);
    assert (order.getOrderItems().size() == 1);
    assert (order.getOrderItems().get(0).getQuantity() == 2);
/*
        This test is not supported currently, as the order service may only do like item merging

        // re-price the order without automatically merging.
        orderService.setAutomaticallyMergeLikeItems(false);
        numOrderItems++;
        
        itemRequest = new OrderItemRequestDTO();
        itemRequest.setQuantity(1);
        itemRequest.setSkuId(sku.getId());
        order = orderService.addItem(orderId, itemRequest, true);
        DiscreteOrderItem item2 = (DiscreteOrderItem) orderService.findLastMatchingItem(order, sku.getId(), null);

        assert item2.getSku() != null;
        assert item2.getSku().equals(sku);
        assert item2.getQuantity() == 1;  // item-was not auto-merged with prior items.

        order = orderService.findOrderById(orderId);

        assert(order.getOrderItems().size()==2);
        assert(order.getOrderItems().get(0).getQuantity()==2);
        assert(order.getOrderItems().get(1).getQuantity()==1);
        
        assert order.getFulfillmentGroups().size() == 1;
        
        fg = order.getFulfillmentGroups().get(0);
        assert fg.getFulfillmentGroupItems().size() == 2;
        
        for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
            assert fgi.getQuantity() == fgi.getOrderItem().getQuantity();
        }*/
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)30 Order (org.broadleafcommerce.core.order.domain.Order)19 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)14 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)13 Transactional (org.springframework.transaction.annotation.Transactional)10 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)9 Product (org.broadleafcommerce.core.catalog.domain.Product)8 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)8 Test (org.testng.annotations.Test)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 NonDiscreteOrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.NonDiscreteOrderItemRequestDTO)5 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)4 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)4 ArrayList (java.util.ArrayList)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3 ConfigurableOrderItemRequest (org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest)3 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)3 Customer (org.broadleafcommerce.profile.core.domain.Customer)3 Rollback (org.springframework.test.annotation.Rollback)3 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)2