public Order setUpAnonymousCartWithGiftWrap() throws PricingException { Customer customer = customerService.saveCustomer(createNamedCustomer()); Order order = cartService.createNewCartForCustomer(customer); Sku newSku = addTestSku("Small Plastic Crate", "Plastic Crate", "Crates"); Sku newInactiveSku = addTestSku("Small Red Plastic Crate", "Plastic Crate", "Crates"); Sku giftWrapSku = addTestSku("Gift Box", "Gift Box", "Gift Wraps"); Product newProduct = newSku.getAllParentProducts().get(0); Category newCategory = newProduct.getDefaultCategory(); List<OrderItem> addedItems = new ArrayList<OrderItem>(); addedItems.add( orderService.addSkuToOrder( order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2)); addedItems.add( orderService.addSkuToOrder( order.getId(), newInactiveSku.getId(), newProduct.getId(), newCategory.getId(), 2)); orderService.addGiftWrapItemToOrder( order, createGiftWrapOrderItemRequest(giftWrapSku, 1, addedItems)); return order; }
@Override public OrderItem findLastMatchingItem(Order order, Long skuId, Long productId) { if (order.getOrderItems() != null) { for (int i = (order.getOrderItems().size() - 1); i >= 0; i--) { OrderItem currentItem = (order.getOrderItems().get(i)); if (currentItem instanceof DiscreteOrderItem) { DiscreteOrderItem discreteItem = (DiscreteOrderItem) currentItem; if (skuId != null) { if (discreteItem.getSku() != null && skuId.equals(discreteItem.getSku().getId())) { return discreteItem; } } else if (productId != null && discreteItem.getProduct() != null && productId.equals(discreteItem.getProduct().getId())) { return discreteItem; } } else if (currentItem instanceof BundleOrderItem) { BundleOrderItem bundleItem = (BundleOrderItem) currentItem; if (skuId != null) { if (bundleItem.getSku() != null && skuId.equals(bundleItem.getSku().getId())) { return bundleItem; } } else if (productId != null && bundleItem.getProduct() != null && productId.equals(bundleItem.getProduct().getId())) { return bundleItem; } } } } return null; }
@Override @Transactional("blTransactionManager") public Order addItemFromNamedOrder( Order namedOrder, OrderItem item, int quantity, boolean priceOrder) throws RemoveFromCartException, AddToCartException, UpdateCartException { // Validate that the quantity requested makes sense if (quantity < 1 || quantity > item.getQuantity()) { throw new IllegalArgumentException("Cannot move 0 or less quantity"); } else if (quantity == item.getQuantity()) { return addItemFromNamedOrder(namedOrder, item, priceOrder); } Order cartOrder = orderDao.readCartForCustomer(namedOrder.getCustomer()); if (cartOrder == null) { cartOrder = createNewCartForCustomer(namedOrder.getCustomer()); } if (moveNamedOrderItems) { // Update the old item to its new quantity only if we're moving items OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO(); orderItemRequestDTO.setOrderItemId(item.getId()); orderItemRequestDTO.setQuantity(item.getQuantity() - quantity); updateItemQuantity(namedOrder.getId(), orderItemRequestDTO, false); } OrderItemRequestDTO orderItemRequest = orderItemService.buildOrderItemRequestDTOFromOrderItem(item); orderItemRequest.setQuantity(quantity); cartOrder = addItem(cartOrder.getId(), orderItemRequest, priceOrder); return cartOrder; }
public void removeAllCandidateOrderOffers() { if (delegate.getCandidateOrderOffers() != null) { for (CandidateOrderOffer candidate : delegate.getCandidateOrderOffers()) { candidate.setOrder(null); } delegate.getCandidateOrderOffers().clear(); } }
@RequestMapping(value = "/viewCart.htm", method = RequestMethod.GET) public String viewCart(ModelMap model, HttpServletRequest request) throws PricingException { Order cart = retrieveCartOrder(request, model); CartSummary cartSummary = new CartSummary(); if (cart.getOrderItems() != null) { for (OrderItem orderItem : cart.getOrderItems()) { if (orderItem instanceof DiscreteOrderItem) { Sku sku = catalogService.findSkuById(((DiscreteOrderItem) orderItem).getSku().getId()); if (!(sku.getSalePrice().equals(((DiscreteOrderItem) orderItem).getSalePrice()))) { orderItem.setSalePrice(sku.getSalePrice()); } if (!(sku.getRetailPrice().equals(((DiscreteOrderItem) orderItem).getRetailPrice()))) { orderItem.setRetailPrice(sku.getRetailPrice()); } if (orderItem.getSalePrice() != orderItem.getRetailPrice()) { orderItem.setPrice(orderItem.getSalePrice()); } else { orderItem.setPrice(orderItem.getRetailPrice()); } orderItem.getPrice(); } } } if (cart.getOrderItems() != null) { for (OrderItem orderItem : cart.getOrderItems()) { CartOrderItem cartOrderItem = new CartOrderItem(); cartOrderItem.setOrderItem(orderItem); cartOrderItem.setQuantity(orderItem.getQuantity()); cartSummary.getRows().add(cartOrderItem); } } if ((cart.getFulfillmentGroups() != null) && (cart.getFulfillmentGroups().isEmpty() == false)) { String cartShippingMethod = cart.getFulfillmentGroups().get(0).getMethod(); String cartShippingService = cart.getFulfillmentGroups().get(0).getService(); if (cartShippingMethod != null) { if (cartShippingMethod.equals("standard")) { cartSummary = createFulfillmentGroup(cartSummary, "standard", cartShippingService, cart); } else if (cartShippingMethod.equals("expedited")) { cartSummary = createFulfillmentGroup(cartSummary, "expedited", cartShippingService, cart); } } } updateFulfillmentGroups(cartSummary, cart); cartSummary.setOrderDiscounts(cart.getTotalAdjustmentsValue().getAmount()); model.addAttribute("cartSummary", cartSummary); return cartViewRedirect ? "redirect:" + cartView : cartView; }
public void sendConfirmationEmail(String orderNumber) { Order order = orderService.findOrderByOrderNumber(orderNumber); Customer customer = customerDao.readCustomerByEmail(order.getEmailAddress()); if (customer != null) { HashMap<String, Object> vars = new HashMap<String, Object>(); vars.put("customer", customer); vars.put("orderNumber", orderNumber); vars.put("order", order); emailService.sendTemplateEmail( customer.getEmailAddress(), getOrderConfirmationEmailInfo(), vars); } }
public void addOrderAdjustments(PromotableOrderAdjustment orderAdjustment) { if (delegate.getOrderAdjustments().size() == 0) { adjustmentPrice = delegate.getSubTotal().getAmount(); } adjustmentPrice = adjustmentPrice.subtract(orderAdjustment.getValue().getAmount()); delegate.getOrderAdjustments().add(orderAdjustment.getDelegate()); if (!orderAdjustment.getDelegate().getOffer().isCombinableWithOtherOffers()) { notCombinableOfferApplied = true; } resetTotalitarianOfferApplied(); hasOrderAdjustments = true; }
public void removeAllOrderAdjustments() { if (delegate.getOrderAdjustments() != null) { for (OrderAdjustment adjustment : delegate.getOrderAdjustments()) { adjustment.setOrder(null); } delegate.getOrderAdjustments().clear(); } adjustmentPrice = null; notCombinableOfferApplied = false; hasOrderAdjustments = false; resetTotalitarianOfferApplied(); }
public void resetTotalitarianOfferApplied() { totalitarianOfferApplied = false; notCombinableOfferAppliedAtAnyLevel = false; for (OrderAdjustment adjustment : delegate.getOrderAdjustments()) { if (adjustment.getOffer().isTotalitarianOffer() != null && adjustment.getOffer().isTotalitarianOffer()) { totalitarianOfferApplied = true; break; } if (!adjustment.getOffer().isCombinableWithOtherOffers()) { notCombinableOfferAppliedAtAnyLevel = true; break; } } if (!totalitarianOfferApplied || !notCombinableOfferAppliedAtAnyLevel) { check: { for (OrderItem orderItem : delegate.getOrderItems()) { for (OrderItemAdjustment adjustment : orderItem.getOrderItemAdjustments()) { if (adjustment.getOffer().isTotalitarianOffer() != null && adjustment.getOffer().isTotalitarianOffer()) { totalitarianOfferApplied = true; break check; } if (!adjustment.getOffer().isCombinableWithOtherOffers()) { notCombinableOfferAppliedAtAnyLevel = true; break check; } } } } } if (!totalitarianOfferApplied || !notCombinableOfferAppliedAtAnyLevel) { check: { for (FulfillmentGroup fg : delegate.getFulfillmentGroups()) { for (FulfillmentGroupAdjustment adjustment : fg.getFulfillmentGroupAdjustments()) { if (adjustment.getOffer().isTotalitarianOffer() != null && adjustment.getOffer().isTotalitarianOffer()) { totalitarianOfferApplied = true; break check; } if (!adjustment.getOffer().isCombinableWithOtherOffers()) { notCombinableOfferAppliedAtAnyLevel = true; break check; } } } } } }
@Override @Transactional("blTransactionManager") public PaymentInfo addPaymentToOrder( Order order, PaymentInfo payment, Referenced securePaymentInfo) { payment.setOrder(order); order.getPaymentInfos().add(payment); order = persist(order); int paymentIndex = order.getPaymentInfos().size() - 1; if (securePaymentInfo != null) { securePaymentInfoService.save(securePaymentInfo); } return order.getPaymentInfos().get(paymentIndex); }
public Order setUpNamedOrder() throws PricingException { Customer customer = customerService.saveCustomer(createNamedCustomer()); Sku newSku = addTestSku("Small Cube Box", "Cube Box", "Boxes"); Order order = orderService.createNamedOrderForCustomer("Boxes Named Order", customer); Product newProduct = newSku.getAllParentProducts().get(0); Category newCategory = newProduct.getDefaultCategory(); orderService.addSkuToOrder( order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2); return order; }
@Override @Transactional("blTransactionManager") public Order addOfferCode(Order order, OfferCode offerCode, boolean priceOrder) throws PricingException, OfferMaxUseExceededException { if (!order.getAddedOfferCodes().contains(offerCode)) { if (!offerService.verifyMaxCustomerUsageThreshold( order.getCustomer(), offerCode.getOffer())) { throw new OfferMaxUseExceededException( "The customer has used this offer code more than the maximum allowed number of times."); } order.getAddedOfferCodes().add(offerCode); order = save(order, priceOrder); } return order; }
@RequestMapping( value = "/viewCart.htm", params = "updateItemQuantity", method = RequestMethod.POST) public String updateItemQuantity( @ModelAttribute(value = "cartSummary") CartSummary cartSummary, Errors errors, ModelMap model, HttpServletRequest request) throws PricingException { if (errors.hasErrors()) { model.addAttribute("cartSummary", cartSummary); return cartView; } Order currentCartOrder = retrieveCartOrder(request, model); List<OrderItem> orderItems = currentCartOrder.getOrderItems(); List<CartOrderItem> items = new ArrayList<CartOrderItem>(cartSummary.getRows()); for (CartOrderItem cartOrderItem : items) { OrderItem orderItem = (OrderItem) CollectionUtils.find( orderItems, new BeanPropertyValueEqualsPredicate("id", cartOrderItem.getOrderItem().getId())); // in case the item was removed from the cart from another browser tab if (orderItem != null) { if (cartOrderItem.getQuantity() > 0) { orderItem.setQuantity(cartOrderItem.getQuantity()); try { cartService.updateItemQuantity(currentCartOrder, orderItem); } catch (ItemNotFoundException e) { LOG.error("Item not found in order: (" + orderItem.getId() + ")", e); } catch (PricingException e) { LOG.error("Unable to price the order: (" + currentCartOrder.getId() + ")", e); } } else { try { cartService.removeItemFromOrder(currentCartOrder, orderItem); cartSummary.getRows().remove(cartOrderItem); } catch (Exception e) { // TODO: handle exception gracefully LOG.error("Unable to remove item from the order: (" + currentCartOrder.getId() + ")"); } } } } cartSummary.setOrderDiscounts(currentCartOrder.getTotalAdjustmentsValue().getAmount()); return cartView; }
public Order setUpExistingCart(Customer customer) throws PricingException { Sku newSku = addTestSku("Large Plastic Crate", "Plastic Crate", "Crates"); Sku newOtherSku = addTestSku("Large Red Plastic Crate", "Plastic Crate", "Crates"); Product newProduct = newSku.getAllParentProducts().get(0); Category newCategory = newProduct.getDefaultCategory(); Order order = cartService.createNewCartForCustomer(customer); orderService.addSkuToOrder( order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2); orderService.addSkuToOrder( order.getId(), newOtherSku.getId(), newProduct.getId(), newCategory.getId(), 2); return order; }
@Override @Transactional("blTransactionManager") public Order removeAllOfferCodes(Order order, boolean priceOrder) throws PricingException { order.getAddedOfferCodes().clear(); order = save(order, priceOrder); return order; }
public Money getAdjustmentPrice() { return adjustmentPrice == null ? null : new Money( adjustmentPrice, delegate.getSubTotal().getCurrency(), adjustmentPrice.scale() == 0 ? BankersRounding.DEFAULT_SCALE : adjustmentPrice.scale()); }
public Money calculateOrderItemsPriceWithoutAdjustments() { Money calculatedSubTotal = new Money(); for (OrderItem orderItem : delegate.getOrderItems()) { Money price = orderItem.getPriceBeforeAdjustments(true); calculatedSubTotal = calculatedSubTotal.add(price.multiply(orderItem.getQuantity())); } return calculatedSubTotal; }
@Override @Transactional("blTransactionManager") public Order createNamedOrderForCustomer(String name, Customer customer) { Order namedOrder = orderDao.create(); namedOrder.setCustomer(customer); namedOrder.setName(name); namedOrder.setStatus(OrderStatus.NAMED); if (extensionManager != null) { extensionManager.getProxy().attachAdditionalDataToNewNamedCart(customer, namedOrder); } if (BroadleafRequestContext.getBroadleafRequestContext() != null) { namedOrder.setLocale(BroadleafRequestContext.getBroadleafRequestContext().getLocale()); } return orderDao.save(namedOrder); // No need to price here }
public List<PromotableFulfillmentGroup> getFulfillmentGroups() { if (fulfillmentGroups == null) { fulfillmentGroups = new ArrayList<PromotableFulfillmentGroup>(); for (FulfillmentGroup fulfillmentGroup : delegate.getFulfillmentGroups()) { fulfillmentGroups.add(itemFactory.createPromotableFulfillmentGroup(fulfillmentGroup, this)); } } return Collections.unmodifiableList(fulfillmentGroups); }
/* * Checks the order adjustment to see if it is not stackable */ public boolean containsNotStackableOrderOffer() { boolean isContainsNotStackableOrderOffer = false; for (OrderAdjustment orderAdjustment : delegate.getOrderAdjustments()) { if (!orderAdjustment.getOffer().isStackable()) { isContainsNotStackableOrderOffer = true; break; } } return isContainsNotStackableOrderOffer; }
@Override @Transactional( value = "blTransactionManager", rollbackFor = {RemoveFromCartException.class}) public Order removeInactiveItems(Long orderId, boolean priceOrder) throws RemoveFromCartException { Order order = findOrderById(orderId); try { for (OrderItem currentItem : new ArrayList<OrderItem>(order.getOrderItems())) { if (!currentItem.isSkuActive()) { removeItem(orderId, currentItem.getId(), priceOrder); } } } catch (Exception e) { throw new RemoveFromCartException("Could not remove from cart", e.getCause()); } return findOrderById(orderId); }
@RequestMapping( value = "/viewCart.htm", params = "removeItemFromCart", method = {RequestMethod.GET, RequestMethod.POST}) public String removeItem( @RequestParam long orderItemId, @ModelAttribute CartSummary cartSummary, ModelMap model, HttpServletRequest request) { Order currentCartOrder = retrieveCartOrder(request, model); try { currentCartOrder = cartService.removeItemFromOrder(currentCartOrder.getId(), orderItemId); } catch (PricingException e) { model.addAttribute("error", "remove"); LOG.error("An error occurred while removing an item from the cart: (" + orderItemId + ")", e); } cartSummary.setOrderDiscounts(currentCartOrder.getTotalAdjustmentsValue().getAmount()); return removeItemViewRedirect ? "redirect:" + removeItemView : removeItemView; }
public Order setUpExistingCartWithInactiveSkuAndInactiveBundle(Customer customer) throws PricingException { Sku newSku = addTestSku("Large Plastic Crate", "Plastic Crate", "Crates"); Sku newInactiveSku = addTestSku("Large Red Plastic Crate", "Plastic Crate", "Crates", false); Product newProduct = newSku.getAllParentProducts().get(0); Category newCategory = newProduct.getDefaultCategory(); Order order = cartService.createNewCartForCustomer(customer); orderService.addSkuToOrder( order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2); orderService.addSkuToOrder( order.getId(), newInactiveSku.getId(), newProduct.getId(), newCategory.getId(), 2); orderService.addBundleItemToOrder(order, createBundleOrderItemRequest()); orderService.addBundleItemToOrder(order, createBundleOrderItemRequestWithInactiveSku()); return order; }
public boolean containsNotStackableFulfillmentGroupOffer() { boolean isContainsNotStackableFulfillmentGroupOffer = false; for (FulfillmentGroup fg : delegate.getFulfillmentGroups()) { for (FulfillmentGroupAdjustment fgAdjustment : fg.getFulfillmentGroupAdjustments()) { if (!fgAdjustment.getOffer().isStackable()) { isContainsNotStackableFulfillmentGroupOffer = true; break; } } } return isContainsNotStackableFulfillmentGroupOffer; }
@Override @Transactional("blTransactionManager") public Order addAllItemsFromNamedOrder(Order namedOrder, boolean priceOrder) throws RemoveFromCartException, AddToCartException { Order cartOrder = orderDao.readCartForCustomer(namedOrder.getCustomer()); if (cartOrder == null) { cartOrder = createNewCartForCustomer(namedOrder.getCustomer()); } List<OrderItem> items = new ArrayList<OrderItem>(namedOrder.getOrderItems()); for (OrderItem item : items) { if (moveNamedOrderItems) { removeItem(namedOrder.getId(), item.getId(), false); } OrderItemRequestDTO orderItemRequest = orderItemService.buildOrderItemRequestDTOFromOrderItem(item); cartOrder = addItem(cartOrder.getId(), orderItemRequest, priceOrder); } if (deleteEmptyNamedOrders) { cancelOrder(namedOrder); } return cartOrder; }
@Override @Transactional("blTransactionManager") public void removePaymentFromOrder(Order order, PaymentInfo paymentInfo) { PaymentInfo paymentInfoToRemove = null; for (PaymentInfo info : order.getPaymentInfos()) { if (info.equals(paymentInfo)) { paymentInfoToRemove = info; } } if (paymentInfoToRemove != null) { try { securePaymentInfoService.findAndRemoveSecurePaymentInfo( paymentInfoToRemove.getReferenceNumber(), paymentInfo.getType()); } catch (WorkflowException e) { // do nothing--this is an acceptable condition LOG.debug("No secure payment is associated with the PaymentInfo", e); } order.getPaymentInfos().remove(paymentInfoToRemove); paymentInfo = paymentInfoDao.readPaymentInfoById(paymentInfoToRemove.getId()); paymentInfoDao.delete(paymentInfo); } }
@Override @Transactional("blTransactionManager") public void removePaymentsFromOrder(Order order, PaymentInfoType paymentInfoType) { List<PaymentInfo> infos = new ArrayList<PaymentInfo>(); for (PaymentInfo paymentInfo : order.getPaymentInfos()) { if (paymentInfoType == null || paymentInfoType.equals(paymentInfo.getType())) { infos.add(paymentInfo); } } order.getPaymentInfos().removeAll(infos); for (PaymentInfo paymentInfo : infos) { try { securePaymentInfoService.findAndRemoveSecurePaymentInfo( paymentInfo.getReferenceNumber(), paymentInfo.getType()); } catch (WorkflowException e) { // do nothing--this is an acceptable condition LOG.debug("No secure payment is associated with the PaymentInfo", e); } order.getPaymentInfos().remove(paymentInfo); paymentInfo = paymentInfoDao.readPaymentInfoById(paymentInfo.getId()); paymentInfoDao.delete(paymentInfo); } }
@Override @Transactional("blTransactionManager") public OrderItem addGiftWrapItemToOrder( Order order, GiftWrapOrderItemRequest itemRequest, boolean priceOrder) throws PricingException { GiftWrapOrderItem item = orderItemService.createGiftWrapOrderItem(itemRequest); item.setOrder(order); item = (GiftWrapOrderItem) orderItemService.saveOrderItem(item); order.getOrderItems().add(item); order = save(order, priceOrder); return item; }
@RequestMapping( value = "/viewCart.htm", params = "removePromoFromCart", method = {RequestMethod.GET, RequestMethod.POST}) public String removePromoCode( @RequestParam String orderOfferCode, @ModelAttribute CartSummary cartSummary, ModelMap model, HttpServletRequest request) { Order currentCartOrder = retrieveCartOrder(request, model); try { currentCartOrder = cartService.removeOfferCode( currentCartOrder, offerService.lookupOfferCodeByCode(orderOfferCode), true); } catch (PricingException e) { model.addAttribute("error", "remove"); LOG.error( "An error occurred while removing a promo from the cart: (" + orderOfferCode + ")", e); } cartSummary.setOrderDiscounts(currentCartOrder.getTotalAdjustmentsValue().getAmount()); return "redirect:/basket/viewCart.htm"; }
public Order setUpAnonymousCartWithBundleGiftWrapReferringToRootItems() throws PricingException { Customer customer = customerService.saveCustomer(createNamedCustomer()); Order order = cartService.createNewCartForCustomer(customer); Sku newSku = addTestSku("Small Plastic Bowl", "Plastic Bowl", "Bowls"); Sku newActiveSku = addTestSku("Small Red Plastic Bowl", "Plastic Bowl", "Bowls"); Product newProduct = newSku.getAllParentProducts().get(0); Category newCategory = newProduct.getDefaultCategory(); List<OrderItem> addedItems = new ArrayList<OrderItem>(); addedItems.add( orderService.addSkuToOrder( order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2)); addedItems.add( orderService.addSkuToOrder( order.getId(), newActiveSku.getId(), newProduct.getId(), newCategory.getId(), 2)); BundleOrderItem newBundle = (BundleOrderItem) orderService.addBundleItemToOrder(order, createBundleOrderItemRequestWithGiftWrap()); GiftWrapOrderItem giftItem = null; for (DiscreteOrderItem addedItem : newBundle.getDiscreteOrderItems()) { if (addedItem instanceof GiftWrapOrderItem) { giftItem = (GiftWrapOrderItem) addedItem; } } for (OrderItem addedItem : addedItems) { addedItem.setGiftWrapOrderItem(giftItem); } giftItem.getWrappedItems().addAll(addedItems); order = orderService.save(order, false); return order; }