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;
}
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;
}
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;
*/
}
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();
}
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();
}*/
}
Aggregations