Search in sources :

Example 1 with OfferMaxUseExceededException

use of org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException 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 OfferMaxUseExceededException

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

the class VerifyCustomerMaxOfferUsesActivity method execute.

@Override
public ProcessContext<CheckoutSeed> execute(ProcessContext<CheckoutSeed> context) throws Exception {
    Order order = context.getSeedData().getOrder();
    Set<Offer> appliedOffers = offerService.getUniqueOffersFromOrder(order);
    for (Offer offer : appliedOffers) {
        if (offer.isLimitedUsePerCustomer()) {
            Long currentUses = offerAuditService.countUsesByCustomer(order, order.getCustomer().getId(), offer.getId());
            if (currentUses >= offer.getMaxUsesPerCustomer()) {
                throw new OfferMaxUseExceededException("The customer has used this offer more than the maximum allowed number of times.");
            }
        }
    }
    // TODO: allow lenient checking on offer code usage
    for (OfferCode code : order.getAddedOfferCodes()) {
        if (code.isLimitedUse()) {
            Long currentCodeUses = offerAuditService.countOfferCodeUses(order, code.getId());
            if (currentCodeUses >= code.getMaxUses()) {
                throw new OfferMaxUseExceededException("Offer code " + code.getOfferCode() + " with id " + code.getId() + " has been than the maximum allowed number of times.");
            }
        }
    }
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OfferMaxUseExceededException(org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException) Offer(org.broadleafcommerce.core.offer.domain.Offer) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode)

Example 3 with OfferMaxUseExceededException

use of org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException 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)3 OfferMaxUseExceededException (org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException)3 Offer (org.broadleafcommerce.core.offer.domain.Offer)2 OfferAlreadyAddedException (org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)2 OfferExpiredException (org.broadleafcommerce.core.offer.service.exception.OfferExpiredException)2 Order (org.broadleafcommerce.core.order.domain.Order)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMap (java.util.HashMap)1 OfferException (org.broadleafcommerce.core.offer.service.exception.OfferException)1 NullOrderImpl (org.broadleafcommerce.core.order.domain.NullOrderImpl)1 Transactional (org.springframework.transaction.annotation.Transactional)1