예제 #1
0
 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;
 }
예제 #2
0
  @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();
  }