use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class AbstractCartRuleProcessor method checkItemCriteria.
protected boolean checkItemCriteria(ItemCriteriaDTO itemCriteria, List<OrderItem> orderItems) {
Map<String, Object> vars = new HashMap<String, Object>();
int foundCount = 0;
Iterator<OrderItem> items = orderItems.iterator();
while (foundCount < itemCriteria.getQty() && items.hasNext()) {
OrderItem currentItem = items.next();
vars.put("discreteOrderItem", currentItem);
vars.put("orderItem", currentItem);
boolean match = executeExpression(itemCriteria.getMatchRule(), vars);
if (match) {
foundCount = foundCount + currentItem.getQuantity();
}
}
return (foundCount >= itemCriteria.getQty().intValue());
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class AbstractBaseProcessor method addChildOrderItemsToCandidates.
protected void addChildOrderItemsToCandidates(Offer offer, CandidatePromotionItems candidates, OfferItemCriteria criteria, List<PromotableOrderItem> promotableOrderItems, PromotableOrderItem item) {
if (offer.getApplyToChildItems()) {
final List<OrderItem> childItems = item.getOrderItem().getChildOrderItems();
if (CollectionUtils.isEmpty(childItems)) {
return;
}
List<PromotableOrderItem> filteredItems = new ArrayList<>();
filteredItems.addAll(promotableOrderItems);
CollectionUtils.filter(filteredItems, new TypedPredicate<PromotableOrderItem>() {
@Override
public boolean eval(PromotableOrderItem promotableOrderItem) {
return childItems.contains(promotableOrderItem.getOrderItem());
}
});
for (PromotableOrderItem promotableOrderItem : filteredItems) {
candidates.addTarget(criteria, promotableOrderItem);
}
}
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class ItemOfferProcessorImpl method markTargets.
/**
* Loop through ItemCriteria and mark targets that can get this promotion to give the promotion to 1 or more targets.
* @param itemOffer
* @param order
* @return
*/
public boolean markTargets(PromotableCandidateItemOffer itemOffer, PromotableOrder order, OrderItem relatedQualifier, boolean checkOnly) {
Offer promotion = itemOffer.getOffer();
if (itemOffer.getCandidateTargetsMap().keySet().isEmpty()) {
return false;
}
OrderItem relatedQualifierRoot = offerServiceUtilities.findRelatedQualifierRoot(relatedQualifier);
for (OfferItemCriteria itemCriteria : itemOffer.getCandidateTargetsMap().keySet()) {
List<PromotableOrderItem> promotableItems = itemOffer.getCandidateTargetsMap().get(itemCriteria);
List<PromotableOrderItemPriceDetail> priceDetails = buildPriceDetailListFromOrderItems(promotableItems);
offerServiceUtilities.sortTargetItemDetails(priceDetails, itemOffer.getOffer().getApplyDiscountToSalePrice());
int targetQtyNeeded = itemCriteria.getQuantity();
// This code was added to support a requirement of buying at least "x" matching items
// but also applying the item to "x+1", "x+2", etc.
int minRequiredTargetQuantity = itemOffer.getMinimumRequiredTargetQuantity();
if (minRequiredTargetQuantity > 1 && minRequiredTargetQuantity > targetQtyNeeded) {
targetQtyNeeded = minRequiredTargetQuantity;
}
targetQtyNeeded = offerServiceUtilities.markTargetsForCriteria(itemOffer, relatedQualifier, checkOnly, promotion, relatedQualifierRoot, itemCriteria, priceDetails, targetQtyNeeded);
if (targetQtyNeeded != 0) {
return false;
}
}
if (!checkOnly) {
itemOffer.addUse();
}
return true;
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OrderItemDaoImpl method delete.
@Override
@Transactional("blTransactionManager")
public void delete(OrderItem orderItem) {
if (!em.contains(orderItem)) {
orderItem = readOrderItemById(orderItem.getId());
}
if (GiftWrapOrderItem.class.isAssignableFrom(orderItem.getClass())) {
final GiftWrapOrderItem giftItem = (GiftWrapOrderItem) orderItem;
for (OrderItem wrappedItem : giftItem.getWrappedItems()) {
wrappedItem.setGiftWrapOrderItem(null);
wrappedItem = save(wrappedItem);
}
}
em.remove(orderItem);
em.flush();
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class AutoBundleActivity method bundleItems.
/**
* Builds a BundleOrderItem based on the passed in productBundle. Creates new DiscreteOrderItems.
* Removes the existing matching DiscreteOrderItems or modifies the quantity if needed.
*
* @param order
* @param productBundle
* @param numApplications
* @throws PricingException
* @throws ItemNotFoundException
*/
private Order bundleItems(Order order, ProductBundle productBundle, Integer numApplications, List<DiscreteOrderItem> unbundledItems) throws PricingException, RemoveFromCartException {
BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemDao.create(OrderItemType.BUNDLE);
bundleOrderItem.setQuantity(numApplications);
bundleOrderItem.setCategory(productBundle.getDefaultCategory());
bundleOrderItem.setSku(productBundle.getDefaultSku());
bundleOrderItem.setName(productBundle.getName());
bundleOrderItem.setProductBundle(productBundle);
bundleOrderItem.setOrder(order);
// make a copy of the fulfillment group items since they will be deleted when the order items are removed
Map<Long, FulfillmentGroupItem> skuIdFulfillmentGroupMap = new HashMap<Long, FulfillmentGroupItem>();
for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
if (fulfillmentGroupItem.getOrderItem() instanceof DiscreteOrderItem) {
DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) fulfillmentGroupItem.getOrderItem();
skuIdFulfillmentGroupMap.put(discreteOrderItem.getSku().getId(), fulfillmentGroupItem);
}
}
}
for (SkuBundleItem skuBundleItem : productBundle.getSkuBundleItems()) {
List<DiscreteOrderItem> itemMatches = new ArrayList<DiscreteOrderItem>();
int skuMatches = populateItemMatchesForSku(itemMatches, order, unbundledItems, skuBundleItem.getSku().getId());
int skusRequired = skuBundleItem.getQuantity() * numApplications;
if (skuMatches < skusRequired) {
throw new IllegalArgumentException("Something went wrong creating automatic bundles. Not enough skus to fulfill bundle requirements for sku id: " + skuBundleItem.getSku().getId());
}
// this call also deletes any fulfillment group items that are associated with that order item
for (DiscreteOrderItem item : itemMatches) {
order = orderService.removeItem(order.getId(), item.getId(), false);
}
DiscreteOrderItem baseItem = null;
if (itemMatches.size() > 0) {
baseItem = itemMatches.get(0);
} else {
for (DiscreteOrderItem discreteOrderItem : unbundledItems) {
if (discreteOrderItem.getSku().getId().equals(skuBundleItem.getSku().getId())) {
baseItem = discreteOrderItem;
}
}
}
// Add item to the skuBundle
DiscreteOrderItem newSkuBundleItem = (DiscreteOrderItem) baseItem.clone();
newSkuBundleItem.setSkuBundleItem(skuBundleItem);
newSkuBundleItem.setBundleOrderItem(bundleOrderItem);
newSkuBundleItem.setQuantity(skuBundleItem.getQuantity());
newSkuBundleItem.setOrder(null);
bundleOrderItem.getDiscreteOrderItems().add(newSkuBundleItem);
if (skuMatches > skusRequired) {
// Add a non-bundled item to the order with the remaining sku count.
DiscreteOrderItem newOrderItem = (DiscreteOrderItem) baseItem.clone();
newOrderItem.setBundleOrderItem(null);
newOrderItem.setSkuBundleItem(null);
newOrderItem.setQuantity(skuMatches - skusRequired);
newOrderItem = (DiscreteOrderItem) orderItemDao.save(newOrderItem);
newOrderItem.setOrder(order);
newOrderItem.updateSaleAndRetailPrices();
// Re-associate fulfillment group item to newOrderItem
FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(newSkuBundleItem.getSku().getId());
if (fulfillmentGroupItem != null) {
FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
newFulfillmentGroupItem.setOrderItem(newOrderItem);
newFulfillmentGroupItem.setQuantity(newOrderItem.getQuantity());
newFulfillmentGroupItem = fulfillmentGroupItemDao.save(newFulfillmentGroupItem);
// these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
}
}
}
order.getOrderItems().add(newOrderItem);
}
}
bundleOrderItem.updateSaleAndRetailPrices();
order.getOrderItems().add(bundleOrderItem);
order = orderService.save(order, false);
for (OrderItem orderItem : order.getOrderItems()) {
if (orderItem instanceof BundleOrderItem) {
BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
for (DiscreteOrderItem discreteOrderItem : bundleItem.getDiscreteOrderItems()) {
// Re-associate fulfillment group item to newly created skuBundles
FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(discreteOrderItem.getSku().getId());
if (fulfillmentGroupItem != null) {
FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
newFulfillmentGroupItem.setOrderItem(discreteOrderItem);
newFulfillmentGroupItem.setQuantity(discreteOrderItem.getQuantity());
// these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
}
}
}
}
}
}
// reload order with new fulfillment group items
order = orderService.save(order, false);
return order;
}
Aggregations