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();
}
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;
}
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;
}
Aggregations