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