Search in sources :

Example 6 with OfferCode

use of org.broadleafcommerce.core.offer.domain.OfferCode in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceImpl method getOffersRetrievedFromCodes.

@Override
public Map<Offer, OfferCode> getOffersRetrievedFromCodes(List<OfferCode> codes, Set<Offer> appliedOffers) {
    HashMap<Offer, OfferCode> offerToCodeMapping = new HashMap<Offer, OfferCode>();
    for (OfferCode code : codes) {
        if (appliedOffers.contains(code.getOffer())) {
            offerToCodeMapping.put(code.getOffer(), code);
        }
        List<Offer> additionalOffersToBeApplied = new ArrayList<Offer>();
        extensionManager.getProxy().addAdditionalOffersForCode(additionalOffersToBeApplied, code);
        for (Offer additionalOfferToBeApplied : additionalOffersToBeApplied) {
            offerToCodeMapping.put(additionalOfferToBeApplied, code);
        }
    }
    return offerToCodeMapping;
}
Also used : PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) CustomerOffer(org.broadleafcommerce.core.offer.domain.CustomerOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) HashMap(java.util.HashMap) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) ArrayList(java.util.ArrayList)

Example 7 with OfferCode

use of org.broadleafcommerce.core.offer.domain.OfferCode in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceImpl method buildOfferListForOrder.

/**
 * Creates a list of offers that applies to this order.  All offers that are assigned to the customer,
 * entered during checkout, or has a delivery type of automatic are added to the list.  The same offer
 * cannot appear more than once in the list.
 *
 * @param order
 * @return a List of offers that may apply to this order
 */
@Override
public List<Offer> buildOfferListForOrder(Order order) {
    List<Offer> offers = new ArrayList<Offer>();
    List<CustomerOffer> customerOffers = lookupOfferCustomerByCustomer(order.getCustomer());
    for (CustomerOffer customerOffer : customerOffers) {
        if (!offers.contains(customerOffer.getOffer())) {
            offers.add(customerOffer.getOffer());
        }
    }
    List<OfferCode> orderOfferCodes = refreshOfferCodesIfApplicable(order);
    orderOfferCodes = removeOutOfDateOfferCodes(orderOfferCodes);
    for (OfferCode orderOfferCode : orderOfferCodes) {
        if (!offers.contains(orderOfferCode.getOffer())) {
            offers.add(orderOfferCode.getOffer());
        }
        extensionManager.getProxy().addAdditionalOffersForCode(offers, orderOfferCode);
    }
    List<Offer> globalOffers = lookupAutomaticDeliveryOffers();
    for (Offer globalOffer : globalOffers) {
        if (!offers.contains(globalOffer) && verifyMaxCustomerUsageThreshold(order, globalOffer)) {
            offers.add(globalOffer);
        }
    }
    if (extensionManager != null) {
        extensionManager.applyAdditionalFilters(offers, order);
    }
    return offers;
}
Also used : PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) CustomerOffer(org.broadleafcommerce.core.offer.domain.CustomerOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) ArrayList(java.util.ArrayList) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) CustomerOffer(org.broadleafcommerce.core.offer.domain.CustomerOffer)

Example 8 with OfferCode

use of org.broadleafcommerce.core.offer.domain.OfferCode in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceImpl method refreshOfferCodesIfApplicable.

/**
 * For enterprise installations, this will refresh any OfferCodes found to be out-of-date with
 * current sandbox status.
 *
 * @param order the order to check
 * @return the refreshed list of OfferCodes
 */
protected List<OfferCode> refreshOfferCodesIfApplicable(final Order order) {
    final List<OfferCode> orderOfferCodes = order.getAddedOfferCodes();
    transUtil.runTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

        @Override
        public void execute() {
            for (OfferCode offerCode : orderOfferCodes) {
                if (offerCode.getOffer() != null) {
                    Long sandBoxVersionId = sandBoxHelper.getSandBoxVersionId(OfferImpl.class, offerCode.getOffer().getId());
                    if (sandBoxVersionId != null && !Objects.equals(sandBoxVersionId, offerCode.getOffer().getId())) {
                        em.refresh(offerCode);
                    }
                }
            }
        }

        @Override
        public boolean shouldRetryOnTransactionLockAcquisitionFailure() {
            return true;
        }
    }, RuntimeException.class);
    return orderOfferCodes;
}
Also used : OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) OfferImpl(org.broadleafcommerce.core.offer.domain.OfferImpl) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)

Example 9 with OfferCode

use of org.broadleafcommerce.core.offer.domain.OfferCode 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 10 with OfferCode

use of org.broadleafcommerce.core.offer.domain.OfferCode in project BroadleafCommerce by BroadleafCommerce.

the class OrderImpl method createOrRetrieveCopyInstance.

@Override
public <G extends Order> CreateResponse<G> createOrRetrieveCopyInstance(MultiTenantCopyContext context) throws CloneNotSupportedException {
    CreateResponse<G> createResponse = context.createOrRetrieveCopyInstance(this);
    if (createResponse.isAlreadyPopulated()) {
        return createResponse;
    }
    Order cloned = createResponse.getClone();
    cloned.setCurrency(getCurrency());
    cloned.setEmailAddress(emailAddress);
    cloned.setLocale(getLocale());
    cloned.setName(name);
    cloned.setOrderNumber(orderNumber);
    cloned.setTotalTax(getTotalTax());
    cloned.setSubmitDate(submitDate);
    cloned.setCustomer(customer);
    cloned.setStatus(getStatus());
    cloned.setTotalFulfillmentCharges(getTotalFulfillmentCharges());
    cloned.setSubTotal(getSubTotal());
    cloned.setTaxOverride(taxOverride == null ? null : taxOverride);
    for (OrderItem entry : orderItems) {
        OrderItem clonedEntry = entry.createOrRetrieveCopyInstance(context).getClone();
        clonedEntry.setOrder(cloned);
        cloned.getOrderItems().add(clonedEntry);
    }
    for (Map.Entry<Offer, OfferInfo> entry : additionalOfferInformation.entrySet()) {
        Offer clonedOffer = entry.getKey().createOrRetrieveCopyInstance(context).getClone();
        OfferInfo clonedEntry = entry.getValue().createOrRetrieveCopyInstance(context).getClone();
        cloned.getAdditionalOfferInformation().put(clonedOffer, clonedEntry);
    }
    for (Map.Entry<String, OrderAttribute> entry : orderAttributes.entrySet()) {
        OrderAttribute clonedAttribute = entry.getValue().createOrRetrieveCopyInstance(context).getClone();
        clonedAttribute.setOrder(cloned);
        cloned.getOrderAttributes().put(entry.getKey(), clonedAttribute);
    }
    for (FulfillmentGroup fulfillmentGroup : fulfillmentGroups) {
        FulfillmentGroup fg = fulfillmentGroup.createOrRetrieveCopyInstance(context).getClone();
        fg.setOrder(cloned);
        cloned.getFulfillmentGroups().add(fg);
    }
    for (OrderPayment orderPayment : payments) {
        if (orderPayment.isActive()) {
            OrderPayment payment = orderPayment.createOrRetrieveCopyInstance(context).getClone();
            payment.setOrder(cloned);
            cloned.getPayments().add(payment);
        }
    }
    for (OfferCode entry : addedOfferCodes) {
        // We do not want to clone the offer code since that will cascade through offer. We only want to create
        // the new relationship between this cloned order and the existing offer code.
        cloned.getAddedOfferCodes().add(entry);
    }
    cloned.setTotal(total == null ? null : new Money(total));
    return createResponse;
}
Also used : OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment) Money(org.broadleafcommerce.common.money.Money) Offer(org.broadleafcommerce.core.offer.domain.Offer) CandidateOrderOffer(org.broadleafcommerce.core.offer.domain.CandidateOrderOffer) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) Map(java.util.Map) HashMap(java.util.HashMap) AdminPresentationMap(org.broadleafcommerce.common.presentation.AdminPresentationMap) OfferInfo(org.broadleafcommerce.core.offer.domain.OfferInfo) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)

Aggregations

OfferCode (org.broadleafcommerce.core.offer.domain.OfferCode)20 Offer (org.broadleafcommerce.core.offer.domain.Offer)13 Order (org.broadleafcommerce.core.order.domain.Order)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 CustomerOffer (org.broadleafcommerce.core.offer.domain.CustomerOffer)5 PromotableCandidateFulfillmentGroupOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)4 PromotableCandidateItemOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer)4 PromotableCandidateOrderOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer)4 OfferInfo (org.broadleafcommerce.core.offer.domain.OfferInfo)3 OfferMaxUseExceededException (org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Money (org.broadleafcommerce.common.money.Money)2 OfferAudit (org.broadleafcommerce.core.offer.domain.OfferAudit)2 OfferAlreadyAddedException (org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)2 OfferExpiredException (org.broadleafcommerce.core.offer.service.exception.OfferExpiredException)2 CommonSetupBaseTest (org.broadleafcommerce.test.CommonSetupBaseTest)2 Test (org.testng.annotations.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Map (java.util.Map)1