public Set<Offer> getApplicableOffers(Order order) { applicableOffers = new HashSet<Offer>(); User user = order.getUser(); List<Offer> activeOffers = offerDao.listAllValidShowPromptly(); if (activeOffers != null) { for (Offer activeOffer : activeOffers) { if (activeOffer.getOfferTrigger() != null) { logger.debug("Active Offer ID -> " + activeOffer.getId()); OfferTriggerMatcher offerTriggerMatcher = new OfferTriggerMatcher(activeOffer.getOfferTrigger(), order.getCartLineItems()); if (offerTriggerMatcher.hasEasyMatch(false) && offerManager.isOfferValidForUser(activeOffer, user) && activeOffer.isShowPromptly()) { if (activeOffer.getOfferAction().getFreeVariant() != null) { ProductVariant freeVariant = activeOffer.getOfferAction().getFreeVariant(); if (!freeVariant.isDeleted() && !freeVariant.isOutOfStock()) { applicableOffers.add(activeOffer); } } else { applicableOffers.add(activeOffer); } } } } } List<OfferInstance> offerInstances = offerInstanceDao.getActiveOffers(user); for (OfferInstance instance : offerInstances) { if (offerManager.isOfferValidForUser(instance.getOffer(), user)) { applicableOffers.add(instance.getOffer()); } } return applicableOffers; }
@DefaultHandler public Resolution pre() { if (prodVariantId != null && !prodVariantId.isEmpty()) { loyaltyProduct = loyaltyProductDao.getProductbyVariantId(prodVariantId); prodVariant = loyaltyProduct.getVariant(); product = prodVariant.getProduct(); // To handle proxy instances from hibernate if (product instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy) product; product = (Product) proxy.getHibernateLazyInitializer().getImplementation(); } } return new ForwardResolution("/pages/loyalty/productDescription.jsp"); }
@SuppressWarnings("unchecked") @JsonHandler @DefaultHandler public Resolution buyNow() { getConfigValuesFromJson(jsonConfigValues); ProductVariant productVariant = getProductVariantService().getVariantById(variantId); User user = null; ProductReferrer productReferrer = null; if (getPrincipal() != null) { user = getUserService().getUserById(getPrincipal().getId()); // user = UserCache.getInstance().getUserById(getPrincipal().getId()).getUser(); if (user == null) { user = userManager.createAndLoginAsGuestUser(null, null); } } else { user = userManager.createAndLoginAsGuestUser(null, null); } Order order = orderManager.getOrCreateOrder(user); CartLineItemConfig lineItemConfig = new CartLineItemConfig(); boolean isLineItemCreated = false; try { for (LineItemConfigValuesDTO dto : configValues) { CartLineItemConfigValues configValue = new CartLineItemConfigValues(); configValue.setLineItemConfig(lineItemConfig); VariantConfigOption configOption = getBaseDao().get(VariantConfigOption.class, dto.getOptionId()); VariantConfigValues selectedConfigValue = getBaseDao().get(VariantConfigValues.class, dto.getValueId()); if (configOption != null && selectedConfigValue != null) { if (configOption .getAdditionalParam() .equalsIgnoreCase(VariantConfigOptionParam.ENGRAVING.param())) { configValue.setValue(getNameToBeEngraved()); } else { configValue.setValue(selectedConfigValue.getValue()); } } if (VariantConfigOptionParam.shouldPriceBeDoubledForParam( configOption.getAdditionalParam())) { configValue.setAdditionalPrice(selectedConfigValue.getAdditonalPrice() * 2); } else { configValue.setAdditionalPrice(selectedConfigValue.getAdditonalPrice()); } if (VariantConfigOptionParam.shouldPriceBeDoubledForParam( configOption.getAdditionalParam())) { configValue.setCostPrice(selectedConfigValue.getCostPrice() * 2); } else { configValue.setCostPrice(selectedConfigValue.getCostPrice()); } configValue.setVariantConfigOption(configOption); lineItemConfig.getCartLineItemConfigValues().add(configValue); } productVariant.setQty(new Long(1)); if (productReferrerId != null) { productReferrer = getBaseDao().get(ProductReferrer.class, productReferrerId); } isLineItemCreated = orderManager.createLineItems(productVariant, lineItemConfig, order, productReferrer); userProductHistoryDao.updateIsAddedToCart(productVariant.getProduct(), user, order); } catch (OutOfStockException e) { getContext().getValidationErrors().add("e2", new SimpleError(e.getMessage())); return new JsonResolution(getContext().getValidationErrors(), getContext().getLocale()); } Map dataMap = new HashMap(); String cartUrl = linkManager.getCartUrl(); dataMap.put("url", cartUrl); dataMap.put("name", productVariant.getProduct().getName()); dataMap.put("qty", productVariant.getQty()); Long itemsInCart = Long.valueOf( order.getExclusivelyProductCartLineItems().size() + order.getExclusivelyComboCartLineItems().size()); dataMap.put("itemsInCart", isLineItemCreated ? itemsInCart + 1L : itemsInCart); HealthkartResponse healthkartResponse = new HealthkartResponse( HealthkartResponse.STATUS_OK, "Product has been added to cart", dataMap); noCache(); return new JsonResolution(healthkartResponse); }
@GET @Path("/applyCoupon") @Produces("application/json") public String applyCoupon(@QueryParam("couponCode") String couponCode) { if (StringUtils.isBlank(couponCode)) { message = "Please enter a coupon code."; return message; } coupon = couponDao.findByCode(couponCode); User user = getUserService().getUserById(getPrincipal().getId()); Order order = orderManager.getOrCreateOrder(user); // If coupon is NULL // Check if the user has applied an IHO Coupon for the first time // and create the coupon - it will validate and then create coupon if (coupon == null) { coupon = ihoManager.createIHOCoupon(user, couponCode); } /*if (coupon == null) { coupon = employeeManager.createEmpCoupon(user, couponCode); }*/ if (coupon == null) { message = "Coupon code is invalid."; } else { List<OfferInstance> offerInstances = offerInstanceDao.findByUserAndCoupon(user, coupon); if (offerInstances != null && !offerInstances.isEmpty()) { offerInstance = offerInstances.get(0); } if (offerInstance != null && !coupon.getRepetitiveUsage()) { if (!offerInstance.isActive()) { error = error_alreadyUsed; message = "This offer has already been used"; } else { error = error_alreadyApplied; order.setOfferInstance(offerInstance); message = "You have already added this coupon."; } } else if (!coupon.isValid()) { message = "Coupon code has expired."; error = error_couponExpired; } else if (!offerManager.isOfferValidForUser(coupon.getOffer(), user)) { error = error_role; Offer offer = coupon.getOffer(); if (!offerManager.isOfferValidForUserDomain(coupon.getOffer(), user)) { message = "The offer is valid for the following domains only:"; for (OfferEmailDomain offerEmailDomain : offer.getOfferEmailDomains()) { message += "<br/>" + offerEmailDomain.getEmailDomain(); } } else { message = "This offer is not activated for you yet."; } } else if (user.equals(coupon.getReferrerUser())) { message = "You are not allowed to use your own referrer code."; } else if (coupon.getReferrerUser() != null && user.getReferredBy() != null) { error = error_alreadyReferrer; message = "You have already mentioned your referrer."; } else if (coupon.getReferrerUser() != null && coupon.getCouponType() != null && !coupon.getCouponType().getId().equals(EnumCouponType.AFFILIATE.getId()) && user.getCreateDate().before(coupon.getCreateDate())) { error = error_referralNotAllowed; message = "You are not allowed to use this referrer coupon."; } else if (coupon.getReferrerUser() != null && orderDao.getLatestOrderForUser(user) != null) { message = "Coupon can not be applied. This is a referral discount coupon and it is only valid before you place your first order."; } else { Date offerInstanceEndDate = null; // add referredBy to the user if coupon contains the referrerUser if (coupon.getReferrerUser() != null) { // add affiliate_to to the user if its an affiliate coupon CouponType couponType = coupon.getCouponType(); if (couponType != null && couponType.getId().equals(EnumCouponType.AFFILIATE.getId())) { Assert.assertNull(user.getAffiliateTo()); user.setAffiliateTo(coupon.getReferrerUser()); user = (User) getBaseDao().save(user); } else { // its a referral coupon Assert.assertNull(user.getReferredBy()); user.setReferredBy(coupon.getReferrerUser()); user = (User) getBaseDao().save(user); offerInstanceEndDate = new DateTime() .plusDays(OfferConstants.MAX_ALLOWED_DAYS_FOR_15_PERCENT_REFERREL_DISCOUNT) .toDate(); } } if (coupon.getRepetitiveUsage()) { List<OfferInstance> activeOfferInstances = offerInstanceDao.findActiveOfferInstances(user, coupon.getOffer()); if (activeOfferInstances == null || activeOfferInstances.isEmpty()) { offerInstance = offerInstanceDao.createOfferInstance( coupon.getOffer(), coupon, user, offerInstanceEndDate); } } else { offerInstance = offerInstanceDao.createOfferInstance( coupon.getOffer(), coupon, user, offerInstanceEndDate); } order.setOfferInstance(offerInstance); coupon.setAlreadyUsed(coupon.getAlreadyUsed() + 1); couponDao.save(coupon); success = true; ProductVariant freeVariant = coupon.getOffer().getOfferAction().getFreeVariant(); if (freeVariant != null) { // OfferTriggerMatcher offerTriggerMatcher = new // OfferTriggerMatcher(coupon.getOffer().getOfferTrigger(), order.getCartLineItems()); // && offerTriggerMatcher.hasEasyMatch(false) if (!freeVariant.isDeleted() && !freeVariant.isOutOfStock()) { orderManager.createLineItems(Arrays.asList(freeVariant), order, null, null, null); message = "Free variant successfuly added to your cart. Please <a href='javascript:location.reload();' style='font-size:1.2em;'>refresh</a> your cart."; } else { message = "Oops! Offer is over."; error = error_freeVariantStockOver; } } else { message = "Coupon applied successfully."; } } } return new JSONResponseBuilder() .addField("offerInstance", offerInstance) .addField("error", error) .build(); }