protected void calculateDiscount() { //Apply entered coupon Decimal total; if (string.IsNullOrEmpty(txtCouponCode.Text.Trim().ToString())) { showCouponCodeError(); } else { string couponCode = ""; couponCode = txtCouponCode.Text.Trim(); Coupon coupon = CouponHelper.GetCouponByCode(couponCode); if (coupon != null) { if (coupon.CouponDiscountTypeId > 0) { CouponDiscountType couponDiscountType = CouponDiscountTypeHelper.GetCouponTypeById(coupon.CouponDiscountTypeId); if (couponDiscountType != null && couponDiscountType.Name == "Price Discount") { total = (decimal.Parse(hdnTotalPriceWithoutDisc.Value) - coupon.DiscountValue) < 0 ? 0 : (decimal.Parse(hdnTotalPriceWithoutDisc.Value) - coupon.DiscountValue); lblTotalPrice.Text = total.ToString("C"); hdnPriceDisc.Value = ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) - total).ToString(); lblDisCountPrice.Text = coupon.DiscountValue.ToString("C"); SessionWrapper.PaymentDetails.discountOffered = ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) - total); SessionWrapper.PaymentDetails.couponID = coupon.CouponDiscountTypeId; } else if (couponDiscountType != null && couponDiscountType.Name == "Percentage Discount") { total = ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) - ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) * ((coupon.DiscountValue) / 100))) < 0 ? 0 : ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) - ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) * ((coupon.DiscountValue) / 100))); lblTotalPrice.Text = total.ToString("C"); hdnPriceDisc.Value = ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) - total).ToString(); lblDisCountPrice.Text = (((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) * ((coupon.DiscountValue) / 100)) < 0 ? 0 : ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) * ((coupon.DiscountValue) / 100))).ToString("C"); SessionWrapper.PaymentDetails.discountOffered = ((decimal.Parse(hdnTotalPriceWithoutDisc.Value)) - total); SessionWrapper.PaymentDetails.couponID = coupon.CouponDiscountTypeId; } lblDisCountPrice.Visible = true; lblDiscountOffer.Visible = true; } hdnCouponID.Value = coupon.CouponId.ToString(); lblErrorCouponCode.ForeColor = System.Drawing.Color.LimeGreen; ScriptManager.RegisterStartupScript(this, this.GetType(), "ErrorMessage", "couponCodeError('Coupon code is valid.');", true); } else { showPlanSummary(); lblDisCountPrice.Visible = false; lblDiscountOffer.Visible = false; lblErrorCouponCode.ForeColor = System.Drawing.Color.Red; ScriptManager.RegisterStartupScript(this, this.GetType(), "ErrorMessage", "couponCodeError('Coupon code is invalid.');", true); } } }
/// <summary> /// Creates a coupon given a code, name, and discount type /// </summary> /// <param name="couponCode"></param> /// <param name="name"></param> /// <param name="discountType"></param> public Coupon(string couponCode, string name, CouponDiscountType discountType) { CouponCode = couponCode; Name = name; DiscountType = discountType; }
public static decimal CalculateDiscountAmount(Coupon coupon, CheckoutOrderInfo checkoutOrderInfo) { // we're assuming it's a valid coupon when this is called, maybe not a good idea?? decimal discountAmount = 0.0m; CouponDiscountType discountType = coupon.DiscountTypeName; if (discountType == CouponDiscountType.SubTotal) { if (coupon.IsAmountOff) { discountAmount = coupon.AmountOff.Value; } else if (coupon.IsPercentOff) { discountAmount = coupon.PercentOffDecimal * checkoutOrderInfo.SubTotal; } } else if (discountType == CouponDiscountType.Product) { List <int> couponProductIds = coupon.GetProductIds().ConvertAll(s => Convert.ToInt32(s)); List <vCartItemProductInfo> cartProductInfos = checkoutOrderInfo.Cart.GetCartItemsWithProductInfo(); List <int> cartProductIds = cartProductInfos.ConvertAll(p => p.ProductId.Value); List <int> intersectedProductIds = cartProductIds.Intersect(couponProductIds).ToList(); foreach (int productIdToDiscount in intersectedProductIds) { vCartItemProductInfo cartItem = cartProductInfos.Find(pi => pi.ProductId.Value == productIdToDiscount); if (cartItem != null) { if (coupon.IsAmountOff) { discountAmount += (coupon.AmountOff.Value * cartItem.Quantity.Value); } else if (coupon.IsPercentOff) { discountAmount += (coupon.PercentOffDecimal * cartItem.GetPriceForQuantity()); } } } } else if (discountType == CouponDiscountType.Shipping) { if (coupon.IsAmountOff) { discountAmount = coupon.AmountOff.Value; } else if (coupon.IsPercentOff) { discountAmount = coupon.PercentOffDecimal * checkoutOrderInfo.ShippingRate.Rate; } } else if (discountType == CouponDiscountType.SubTotalAndShipping) { if (coupon.IsAmountOff) { discountAmount = coupon.AmountOff.Value; } else if (coupon.IsPercentOff) { discountAmount = coupon.PercentOffDecimal * (checkoutOrderInfo.SubTotal + checkoutOrderInfo.ShippingRate.Rate); } } else { throw new NotImplementedException(string.Format(@"""{0}"" is an unknown CouponDiscountType", discountType)); } //--- check we didn't exceed the "Max Discount Amount Per Order" if (coupon.MaxDiscountAmountPerOrder.HasValue && (discountAmount > coupon.MaxDiscountAmountPerOrder.Value)) { discountAmount = coupon.MaxDiscountAmountPerOrder.Value; } return(discountAmount.RoundForMoney()); }
public static bool IsCouponValidForCheckout(Coupon coupon, CheckoutOrderInfo checkoutOrderInfo, out CouponStatus couponStatus) { couponStatus = CouponStatus.NotFound; //Coupon coupon = Coupon.GetCoupon(couponCode, storeId); if (coupon != null) { //--- "Active" status? if (!coupon.IsActive.Value) { couponStatus = CouponStatus.NotActive; return(false); } //--- Active Dates? if (coupon.ValidFromDate.HasValue) { if (DateTime.Today < coupon.ValidFromDate.Value) { couponStatus = CouponStatus.ActiveDateInvalidFrom; return(false); } } if (coupon.ValidToDate.HasValue) { if (DateTime.Today > coupon.ValidToDate.Value) { couponStatus = CouponStatus.ActiveDateInvalidTo; return(false); } } List <CheckoutCouponInfo> checkoutCouponInfos = checkoutOrderInfo.GetAppliedCoupons(); //--- already been applied? if (checkoutCouponInfos.Exists(c => c.CouponCode == coupon.Code)) { couponStatus = CouponStatus.AlreadyApplied; return(false); } //--- combinable? if (!coupon.IsCombinable.Value && checkoutCouponInfos.Count > 0) { // this coupon is not combinable and user is trying to combine it couponStatus = CouponStatus.NotCombinable; return(false); } if (checkoutCouponInfos.Count > 1 && checkoutCouponInfos.Exists(cc => cc.IsCombinable == false)) { // there's multiple coupons applied but at least 1 of them is NOT combinable couponStatus = CouponStatus.NonCombinableCouponAlreadyInUse; return(false); } CouponDiscountType discountType = coupon.DiscountTypeName; //--- Applies to products? if (discountType == CouponDiscountType.Product) { // make sure the Product(s) the coupon applies to are in the User's Cart List <int> couponProductIds = coupon.GetProductIds().ConvertAll(s => Convert.ToInt32(s)); List <int> cartProductIds = checkoutOrderInfo.Cart.GetCartProducts().ToList().ConvertAll(p => p.Id.Value); // 'cartProductIds' must contain at least 1 of the 'couponProductIds' List <int> intersectedIds = cartProductIds.Intersect(couponProductIds).ToList(); if (intersectedIds.Count == 0) { couponStatus = CouponStatus.NoEligibleProduct; return(false); } } //--- Applies to shipping? if (discountType == CouponDiscountType.Shipping) { // make sure one of the Shipping Option(s) the coupon applies to has been selected by the User if (checkoutOrderInfo.ShippingProvider != ShippingProviderType.UNKNOWN) { if (!coupon.GetShippingRateTypes().Contains(checkoutOrderInfo.ShippingRate.ServiceType)) { couponStatus = CouponStatus.NoEligibleShipping; return(false); } } } //--- min. cart total if (coupon.MinOrderAmount.HasValue && (checkoutOrderInfo.Total < coupon.MinOrderAmount.Value)) { couponStatus = CouponStatus.MinOrderAmountNotReached; return(false); } // TODO - Max # redemptions per user // Probably need to implement some kind of "User GUID" cookie value at the store level to track unique users/visitors // since we can't reliably use UserId (won't work for anonymous checkout) or IP address (different users behind same IP) //--- max. # redemptions - lifetime if (coupon.MaxUsesLifetime.HasValue) { int numLifetimeRedemptions = coupon.GetNumberOfRedemptions(); if (numLifetimeRedemptions >= coupon.MaxUsesLifetime.Value) { couponStatus = CouponStatus.ExceededMaxLifetimeRedemptions; return(false); } } couponStatus = CouponStatus.Valid; return(true); } return(false); }
private async Task<Coupon> CreateCoupon( CreateCouponViewModel createCouponVm, double studentPrice, double teacherCommissionRate, string couponType, CouponDiscountType discountType, string couponStatus, string eventId = null) { try { var includesSupport = createCouponVm.IncludesSupport; var itemPrice = createCouponVm.ContentItemDetails.OriginalPrice; double teacherCouponFee = 0; double siteCouponFee = 0; if (discountType == CouponDiscountType.Admin) { teacherCouponFee = studentPrice*teacherCommissionRate; siteCouponFee = (1 - teacherCommissionRate)*studentPrice; } else if (discountType == CouponDiscountType.Regular) { teacherCouponFee = itemPrice * teacherCommissionRate - (itemPrice - studentPrice); siteCouponFee = itemPrice*(1 - teacherCommissionRate); } else if (discountType == CouponDiscountType.Special) { teacherCouponFee = 0; siteCouponFee = studentPrice; } var couponFees = new CouponFeesDto { OriginalPrice = ConvertToContentItemCurrency(createCouponVm.ContentItemDetails.Currency, itemPrice), OriginalPriceNIS = ConvertToNis(itemPrice), SiteCouponFee = ConvertToContentItemCurrency(createCouponVm.ContentItemDetails.Currency,siteCouponFee), SiteCouponFeeNIS = ConvertToNis(siteCouponFee), TeacherCouponFee = ConvertToContentItemCurrency(createCouponVm.ContentItemDetails.Currency,teacherCouponFee), TeacherCouponFeeNIS = ConvertToNis(teacherCouponFee) }; return CreateCouponRecord(createCouponVm, Session.GetLoggedInUser().ObjectId, couponFees, couponType, eventId, couponStatus); } catch (Exception ex) { //SendErrorEmail(createCouponVm, "cannot create coupon, general error " + ex.Message); createCouponVm.CouponErrors.GeneralError = ex.Message; mLogger.Log(LogLevel.Error, ex); } return null; }
private async Task CreateAdminOrSelfCoupons(double discountPrice, CreateCouponViewModel createCouponVm, CouponDiscountType discountType, ParseRepository repository) { var couponType = discountPrice == 0 ? CouponTypes.SelfCoupon : CouponTypes.ManagerDiscount; var coupon = await CreateCoupon( createCouponVm, discountPrice, createCouponVm.TeacherData.TeacherCommissionRate, couponType, discountType, BL.Consts.CouponStatus.Active); await coupon.SaveAsync(); createCouponVm.CouponId = coupon.ObjectId; if (string.IsNullOrEmpty(createCouponVm.CouponId)) { createCouponVm.CouponErrors.GeneralError = MyMentorResources.generalError; } SendEmails(createCouponVm,repository); }