Search in sources :

Example 1 with OfferExpiredException

use of org.broadleafcommerce.core.offer.service.exception.OfferExpiredException in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCartController method addPromo.

/**
 * Attempts to add provided Offer to Cart
 *
 * @param request
 * @param response
 * @param model
 * @param customerOffer
 * @return the return view
 * @throws IOException
 * @throws PricingException
 * @throws OfferMaxUseExceededException
 */
public String addPromo(HttpServletRequest request, HttpServletResponse response, Model model, String customerOffer) throws IOException, PricingException {
    Order cart = CartState.getCart();
    Boolean promoAdded = false;
    String exception = "";
    if (cart != null && !(cart instanceof NullOrderImpl)) {
        List<OfferCode> offerCodes = offerService.lookupAllOfferCodesByCode(customerOffer);
        if (CollectionUtils.isNotEmpty(offerCodes)) {
            for (OfferCode offerCode : offerCodes) {
                if (offerCode != null) {
                    try {
                        orderService.addOfferCode(cart, offerCode, false);
                        promoAdded = true;
                    } catch (OfferException e) {
                        if (e instanceof OfferMaxUseExceededException) {
                            exception = "Use Limit Exceeded";
                        } else if (e instanceof OfferExpiredException) {
                            exception = "Offer Has Expired";
                        } else if (e instanceof OfferAlreadyAddedException) {
                            exception = "Offer Has Already Been Added";
                        } else {
                            exception = "An Unknown Offer Error Has Occurred";
                        }
                    }
                } else {
                    exception = "Invalid Code";
                }
            }
            cart = orderService.save(cart, true);
        } else {
            exception = "Unknown Code";
        }
    } else {
        exception = "Invalid Cart";
    }
    if (isAjaxRequest(request)) {
        Map<String, Object> extraData = new HashMap<>();
        extraData.put("promoAdded", promoAdded);
        extraData.put("exception", exception);
        model.addAttribute("blcextradata", new ObjectMapper().writeValueAsString(extraData));
    } else {
        model.addAttribute("exception", exception);
    }
    return isCheckoutContext(request) ? getCheckoutView() : getCartView();
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) HashMap(java.util.HashMap) OfferException(org.broadleafcommerce.core.offer.service.exception.OfferException) NullOrderImpl(org.broadleafcommerce.core.order.domain.NullOrderImpl) OfferMaxUseExceededException(org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) OfferExpiredException(org.broadleafcommerce.core.offer.service.exception.OfferExpiredException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OfferAlreadyAddedException(org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)

Example 2 with OfferExpiredException

use of org.broadleafcommerce.core.offer.service.exception.OfferExpiredException in project BroadleafCommerce by BroadleafCommerce.

the class OrderServiceImpl method addOfferCodes.

@Override
@Transactional("blTransactionManager")
public Order addOfferCodes(Order order, List<OfferCode> offerCodes, boolean priceOrder) throws PricingException, OfferException {
    preValidateCartOperation(order);
    Set<Offer> addedOffers = offerService.getUniqueOffersFromOrder(order);
    if (extensionManager != null) {
        extensionManager.getProxy().addOfferCodes(order, offerCodes, priceOrder);
    }
    if (offerCodes != null && !offerCodes.isEmpty()) {
        for (OfferCode offerCode : offerCodes) {
            if (order.getAddedOfferCodes().contains(offerCode) || addedOffers.contains(offerCode.getOffer())) {
                throw new OfferAlreadyAddedException("The offer has already been added.");
            } else if (!offerService.verifyMaxCustomerUsageThreshold(order, offerCode)) {
                throw new OfferMaxUseExceededException("The customer has used this offer code more than the maximum allowed number of times.");
            } else if (!offerCode.isActive() || !offerCode.getOffer().isActive()) {
                throw new OfferExpiredException("The offer has expired.");
            }
            order.getAddedOfferCodes().add(offerCode);
        }
        order = save(order, priceOrder);
    }
    return order;
}
Also used : OfferMaxUseExceededException(org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException) Offer(org.broadleafcommerce.core.offer.domain.Offer) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) OfferExpiredException(org.broadleafcommerce.core.offer.service.exception.OfferExpiredException) OfferAlreadyAddedException(org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OfferCode (org.broadleafcommerce.core.offer.domain.OfferCode)2 OfferAlreadyAddedException (org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)2 OfferExpiredException (org.broadleafcommerce.core.offer.service.exception.OfferExpiredException)2 OfferMaxUseExceededException (org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMap (java.util.HashMap)1 Offer (org.broadleafcommerce.core.offer.domain.Offer)1 OfferException (org.broadleafcommerce.core.offer.service.exception.OfferException)1 NullOrderImpl (org.broadleafcommerce.core.order.domain.NullOrderImpl)1 Order (org.broadleafcommerce.core.order.domain.Order)1 Transactional (org.springframework.transaction.annotation.Transactional)1