use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class CartTest method testMoveItemToCartFromNamedOrder.
@Test(groups = { "testCartAndNamedOrder" })
@Transactional
public void testMoveItemToCartFromNamedOrder() throws RemoveFromCartException, AddToCartException {
Order namedOrder = setUpNamedOrder();
List<OrderItem> namedOrderItems = new ArrayList<>();
namedOrderItems.addAll(namedOrder.getOrderItems());
List<OrderItem> movedOrderItems = new ArrayList<>();
movedOrderItems.add(namedOrderItems.get(0));
Order cart = orderService.createNewCartForCustomer(namedOrder.getCustomer());
cart = orderService.addItemFromNamedOrder(namedOrder, movedOrderItems.get(0), true);
List<Order> customerNamedOrders = orderService.findOrdersForCustomer(namedOrder.getCustomer(), OrderStatus.NAMED);
assert customerNamedOrders.size() == 0;
assert cart.getOrderItems().size() == 1;
assert namedOrder.getOrderItems().size() == 0;
assert cartContainsOnlyTheseItems(cart, movedOrderItems);
}
use of org.broadleafcommerce.core.order.domain.OrderItem 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.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OfferServiceImpl method getUniqueOffersFromOrder.
@Override
@SuppressWarnings("unchecked")
public Set<Offer> getUniqueOffersFromOrder(Order order) {
HashSet<Offer> result = new HashSet<Offer>();
Transformer adjustmentToOfferTransformer = new Transformer() {
@Override
public Object transform(Object input) {
return ((Adjustment) input).getOffer();
}
};
result.addAll(CollectionUtils.collect(order.getOrderAdjustments(), adjustmentToOfferTransformer));
if (order.getOrderItems() != null) {
for (OrderItem item : order.getOrderItems()) {
result.addAll(CollectionUtils.collect(item.getOrderItemAdjustments(), adjustmentToOfferTransformer));
// record usage for price details on the item as well
if (item.getOrderItemPriceDetails() != null) {
for (OrderItemPriceDetail detail : item.getOrderItemPriceDetails()) {
result.addAll(CollectionUtils.collect(detail.getOrderItemPriceDetailAdjustments(), adjustmentToOfferTransformer));
}
}
}
}
if (order.getFulfillmentGroups() != null) {
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
result.addAll(CollectionUtils.collect(fg.getFulfillmentGroupAdjustments(), adjustmentToOfferTransformer));
}
}
return result;
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OfferServiceImpl method verifyAdjustments.
protected boolean verifyAdjustments(Order order, boolean beforeSave) {
boolean madeChange = false;
if (order.getOrderItems() == null) {
return madeChange;
}
for (OrderItem oi : order.getOrderItems()) {
if (oi.getOrderItemPriceDetails() == null) {
continue;
}
for (OrderItemPriceDetail pd : oi.getOrderItemPriceDetails()) {
if (pd.getOrderItemPriceDetailAdjustments() == null) {
continue;
}
Map<Long, OrderItemPriceDetailAdjustment> adjs = new HashMap<Long, OrderItemPriceDetailAdjustment>();
List<OrderItemPriceDetailAdjustment> adjustmentsToRemove = new ArrayList<OrderItemPriceDetailAdjustment>();
for (OrderItemPriceDetailAdjustment adj : pd.getOrderItemPriceDetailAdjustments()) {
if (adjs.containsKey(adj.getOffer().getId())) {
adjustmentsToRemove.add(adj);
if (LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder("Detected collisions ").append(beforeSave ? "before saving" : "after saving").append(" with ids ").append(adjs.get(adj.getOffer().getId()).getId()).append(" and ").append(adj.getId());
LOG.debug(sb.toString());
}
} else {
adjs.put(adj.getOffer().getId(), adj);
}
}
for (OrderItemPriceDetailAdjustment adj : adjustmentsToRemove) {
pd.getOrderItemPriceDetailAdjustments().remove(adj);
madeChange = true;
}
}
}
return madeChange;
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OfferServiceUtilitiesImpl method markRelatedQualifiersAndTargetsForItemCriteria.
@Override
public int markRelatedQualifiersAndTargetsForItemCriteria(PromotableCandidateItemOffer itemOffer, PromotableOrder order, OrderItemHolder orderItemHolder, OfferItemCriteria itemCriteria, List<PromotableOrderItemPriceDetail> priceDetails, ItemOfferMarkTargets itemOfferMarkTargets) {
sortQualifierItemDetails(priceDetails, itemOffer.getOffer().getApplyDiscountToSalePrice());
// Calculate the number of qualifiers needed that will not receive the promotion.
// These will be reserved first before the target is assigned.
int qualifierQtyNeeded = itemCriteria.getQuantity();
for (PromotableOrderItemPriceDetail detail : priceDetails) {
OrderItem oi = detail.getPromotableOrderItem().getOrderItem();
if (qualifierQtyNeeded > 0) {
int itemQtyAvailableToBeUsedAsQualifier = detail.getQuantityAvailableToBeUsedAsQualifier(itemOffer);
if (itemQtyAvailableToBeUsedAsQualifier > 0) {
// We have found a qualifier that meets this offer criteria. First, we'll save some state that we
// might need in the future.
OfferItemCriteria previousQualifierCriteria = null;
for (PromotionQualifier possibleQualifier : detail.getPromotionQualifiers()) {
if (possibleQualifier.getPromotion().equals(itemOffer.getOffer())) {
previousQualifierCriteria = possibleQualifier.getItemCriteria();
break;
}
}
// Go ahead and mark this item as a qualifier
int qtyToMarkAsQualifier = Math.min(qualifierQtyNeeded, itemQtyAvailableToBeUsedAsQualifier);
qualifierQtyNeeded -= qtyToMarkAsQualifier;
PromotionQualifier pq = detail.addPromotionQualifier(itemOffer, itemCriteria, qtyToMarkAsQualifier);
pq.setPrice(detail.getPromotableOrderItem().getPriceBeforeAdjustments(itemOffer.getOffer().getApplyDiscountToSalePrice()));
// that there is an eligible target(s) and continue on.
if (itemOfferMarkTargets.markTargets(itemOffer, order, oi, true)) {
// We found a target. Let's save this related order item used as the qualifier in case
// we succeed
orderItemHolder.setOrderItem(oi);
} else {
// If we didn't find a target, we need to roll back how we marked this item as a qualifier.
qualifierQtyNeeded += qtyToMarkAsQualifier;
if (pq.getQuantity() == qtyToMarkAsQualifier) {
detail.getPromotionQualifiers().remove(pq);
} else {
pq.setItemCriteria(previousQualifierCriteria);
pq.setQuantity(pq.getQuantity() - qtyToMarkAsQualifier);
}
}
}
}
if (qualifierQtyNeeded == 0) {
break;
}
}
return qualifierQtyNeeded;
}
Aggregations