/**
  * Private method used by applyAllItemOffers to create an OrderItemAdjustment from a
  * CandidateItemOffer and associates the OrderItemAdjustment to the OrderItem.
  *
  * @param itemOffer a CandidateItemOffer to apply to an OrderItem
  */
 protected void applyOrderItemAdjustment(
     PromotableCandidateItemOffer itemOffer, PromotableOrderItem orderItem) {
   OrderItemAdjustment itemAdjustment = offerDao.createOrderItemAdjustment();
   itemAdjustment.init(
       orderItem.getDelegate(), itemOffer.getOffer(), itemOffer.getOffer().getName());
   // add to adjustment
   PromotableOrderItemAdjustment promotableOrderItemAdjustment =
       promotableItemFactory.createPromotableOrderItemAdjustment(itemAdjustment, orderItem);
   orderItem.addOrderItemAdjustment(
       promotableOrderItemAdjustment); // This is how we can tell if an item has been discounted
 }
  protected int checkLegacyAdjustments(
      List<PromotableOrderItem> discreteOrderItems, int appliedItemOffersCount) {
    if (appliedItemOffersCount > 0) {
      for (PromotableOrderItem discreteOrderItem : discreteOrderItems) {
        if (discreteOrderItem.isHasOrderItemAdjustments()) {
          boolean useSaleAdjustments = false;
          int adjustmentsRemoved = 0;

          Money adjustmentPrice;
          if (discreteOrderItem.getDelegate().getIsOnSale()) {
            if (discreteOrderItem
                .getSaleAdjustmentPrice()
                .lessThanOrEqual(discreteOrderItem.getRetailAdjustmentPrice())) {
              adjustmentPrice = discreteOrderItem.getSaleAdjustmentPrice();
              useSaleAdjustments = true;
            } else {
              adjustmentPrice = discreteOrderItem.getRetailAdjustmentPrice();
            }

            if (!adjustmentPrice.lessThanOrEqual(discreteOrderItem.getSalePrice())) {
              adjustmentsRemoved = adjustmentsRemoved + discreteOrderItem.removeAllAdjustments();
            }
          } else {
            if (!discreteOrderItem
                .getRetailAdjustmentPrice()
                .lessThanOrEqual(discreteOrderItem.getRetailPrice())) {
              adjustmentsRemoved = adjustmentsRemoved + discreteOrderItem.removeAllAdjustments();
            }
          }

          adjustmentsRemoved =
              adjustmentsRemoved + discreteOrderItem.fixAdjustments(useSaleAdjustments);
          appliedItemOffersCount -= adjustmentsRemoved;
        }
      }
    }
    return appliedItemOffersCount;
  }
  protected int checkAdjustments(PromotableOrder order, int appliedItemOffersCount) {
    if (appliedItemOffersCount > 0) {
      List<PromotableOrderItem> allSplitItems = order.getAllSplitItems();
      // compare adjustment price to sales price and remove adjustments if sales price is better
      for (PromotableOrderItem splitItem : allSplitItems) {
        if (splitItem.isHasOrderItemAdjustments()) {
          boolean useSaleAdjustments = false;
          int adjustmentsRemoved = 0;

          Money adjustmentPrice;
          if (splitItem.getDelegate().getIsOnSale()) {
            if (splitItem
                .getSaleAdjustmentPrice()
                .lessThanOrEqual(splitItem.getRetailAdjustmentPrice())) {
              adjustmentPrice = splitItem.getSaleAdjustmentPrice();
              useSaleAdjustments = true;
            } else {
              adjustmentPrice = splitItem.getRetailAdjustmentPrice();
            }

            if (!adjustmentPrice.lessThan(splitItem.getSalePrice())) {
              adjustmentsRemoved = adjustmentsRemoved + splitItem.removeAllAdjustments();
            }
          } else {
            if (!splitItem.getRetailAdjustmentPrice().lessThan(splitItem.getRetailPrice())) {
              adjustmentsRemoved = adjustmentsRemoved + splitItem.removeAllAdjustments();
            }
          }

          adjustmentsRemoved = adjustmentsRemoved + splitItem.fixAdjustments(useSaleAdjustments);
          appliedItemOffersCount -= adjustmentsRemoved;
        }
      }
      mergeSplitItems(order);
    }
    return appliedItemOffersCount;
  }