public static void AdjustCoupon(Basket basket, CouponAdjustment coupAdj)
        {
            if (coupAdj != null)
            {
                BasketItem basketItem = new BasketItem();
                basketItem.OrderItemType = OrderItemType.Coupon;
                basketItem.Price         = -1 * coupAdj.appliedamount.Value;
                basketItem.Quantity      = 1;
                basketItem.BasketId      = basket.BasketId;
                basketItem.Sku           = coupAdj.code;

                Coupon coupon = CouponDataSource.LoadForCouponCode(coupAdj.code);
                if (coupon == null)
                {
                    //shouldn't ever happen. However if it does we have
                    //no option but to add as an unknown coupon
                    basketItem.Name = "Unknown Coupon '" + coupAdj.code + "' (GoogleCheckout)";
                    Logger.Warn("A coupon applied by Google Checkout is not recognized in the Store. Coupon Code : " + coupAdj.code + ", Applied Amount : " + coupAdj.appliedamount);
                }
                else
                {
                    basketItem.Name = coupon.Name;
                    basket.BasketCoupons.Add(new BasketCoupon(basket.BasketId, coupon.CouponId));
                }

                basket.Items.Add(basketItem);
            }
        }
        private void SaveOrder()
        {
            if (AlwaysConvert.ToInt16(ItemType.SelectedValue) == (Int16)OrderItemType.Coupon)
            {
                //IF IT IS A COUPON, VALIDATE THE ENTERED SKU ( COUPON CODE )
                if (CouponDataSource.LoadForCouponCode(Sku.Text) == null)
                {
                    CustomValidator skuValidator = new CustomValidator();
                    skuValidator.IsValid           = false;
                    skuValidator.ControlToValidate = "Sku";
                    skuValidator.ValidationGroup   = ValidationSummary.ValidationGroup;
                    skuValidator.ErrorMessage      = "Sku is not a valid coupon code.";
                    skuValidator.Text = "*";
                    phSkuValidators.Controls.Add(skuValidator);
                    return;
                }
            }
            //add the custom item
            OrderItem oi = new OrderItem();

            oi.OrderId         = _OrderId;
            oi.OrderItemTypeId = AlwaysConvert.ToInt16(ItemType.SelectedValue);
            if (_OrderShipment == null)
            {
                oi.OrderShipmentId = AlwaysConvert.ToInt(ShipmentsList.SelectedValue);
            }
            else
            {
                oi.OrderShipmentId = _OrderShipmentId;
            }
            oi.Name = Name.Text;
            oi.Sku  = Sku.Text;
            Decimal price = AlwaysConvert.ToDecimal(Price.Text);

            //We should not allow positive values for Discount or Credit.  We should not
            //  allow negative values for Charge.
            switch (oi.OrderItemTypeId)
            {
            case ((short)OrderItemType.Discount):
            case ((short)OrderItemType.Credit):
                if (price > 0)
                {
                    price = Decimal.Negate(price);
                }
                break;

            case ((short)OrderItemType.Charge):
                if (price < 0)
                {
                    price = Decimal.Negate(price);
                }
                break;
            }

            oi.Price    = price;
            oi.Quantity = AlwaysConvert.ToInt16(Quantity.Text);
            _Order.Items.Add(oi);
            _Order.Save(true, false);
        }
Exemple #3
0
        internal LSDecimal GetProductSubtotal()
        {
            LSDecimal productSubtotal = 0;

            foreach (OrderItem oi in this.Items)
            {
                if (oi.OrderItemType == OrderItemType.Product || oi.OrderItemType == OrderItemType.Discount)
                {
                    productSubtotal += oi.ExtendedPrice;
                }
                else if (oi.OrderItemType == OrderItemType.Coupon)
                {
                    //ONLY ADD COUPON IF IT IS NOT A SHIPPING COUPON
                    string couponCode = oi.Sku;
                    Coupon coupon     = CouponDataSource.LoadForCouponCode(couponCode);
                    if (coupon != null && coupon.CouponType != CouponType.Shipping)
                    {
                        productSubtotal += oi.ExtendedPrice;
                    }
                }
            }
            return(productSubtotal);
        }
        private void ApplyCoupon()
        {
            ValidCouponMessage.Visible = false;
            string couponCode            = StringHelper.StripHtml(Request.Form[CouponCode.UniqueID]);
            Coupon coupon                = CouponDataSource.LoadForCouponCode(couponCode);
            String couponValidityMessage = String.Empty;

            InvalidCouponMessage.Text = "Invalid coupon code.";
            if (coupon != null)
            {
                ICouponCalculator couponCalculator = AbleContext.Resolve <ICouponCalculator>();
                if (!couponCalculator.IsCouponAlreadyUsed(AbleContext.Current.User.Basket, coupon))
                {
                    if (couponCalculator.IsCouponValid(AbleContext.Current.User.Basket, coupon, out couponValidityMessage))
                    {
                        Basket       basket       = AbleContext.Current.User.Basket;
                        BasketCoupon recentCoupon = new BasketCoupon(AbleContext.Current.User.Basket, coupon);
                        recentCoupon.AppliedDate = LocaleHelper.LocalNow;
                        basket.BasketCoupons.Add(recentCoupon);
                        // APPLY COUPON COMBINE RULE
                        //THE RULE:
                        //If most recently applied coupon is marked "do not combine", then all previously
                        //entered coupons must be removed from basket.

                        //If most recently applied coupon is marked "combine", then remove any applied
                        //coupon that is marked "do not combine".  (Logically, in this instance there
                        //could be at most one other coupon of this type...)
                        string previousCouponsRemoved = "";

                        if (recentCoupon.Coupon.AllowCombine)
                        {
                            //IF ALLOW COMBINE, REMOVE ALL PREVIOUS NOCOMBINE COUPONS
                            for (int i = (basket.BasketCoupons.Count - 1); i >= 0; i--)
                            {
                                if (!basket.BasketCoupons[i].Coupon.AllowCombine)
                                {
                                    if (previousCouponsRemoved.Length > 0)
                                    {
                                        previousCouponsRemoved += "," + basket.BasketCoupons[i].Coupon.Name;
                                    }
                                    else
                                    {
                                        previousCouponsRemoved = basket.BasketCoupons[i].Coupon.Name;
                                    }

                                    basket.BasketCoupons.DeleteAt(i);
                                }
                            }
                        }
                        else
                        {
                            //IF NOT ALLOW COMBINE, REMOVE ALL EXCEPT THIS COUPON
                            for (int i = (basket.BasketCoupons.Count - 1); i >= 0; i--)
                            {
                                if (basket.BasketCoupons[i] != recentCoupon)
                                {
                                    if (previousCouponsRemoved.Length > 0)
                                    {
                                        previousCouponsRemoved += "," + basket.BasketCoupons[i].Coupon.Name;
                                    }
                                    else
                                    {
                                        previousCouponsRemoved = basket.BasketCoupons[i].Coupon.Name;
                                    }
                                    basket.BasketCoupons.DeleteAt(i);
                                }
                            }
                        }

                        basket.Save();
                        IBasketService preCheckoutService = AbleContext.Resolve <IBasketService>();
                        preCheckoutService.Recalculate(basket);
                        if (previousCouponsRemoved.Length > 0)
                        {
                            if (recentCoupon.Coupon.AllowCombine)
                            {
                                CombineCouponRemoveMessage.Text    = String.Format(CombineCouponRemoveMessage.Text, recentCoupon.Coupon.Name, previousCouponsRemoved, previousCouponsRemoved);
                                CombineCouponRemoveMessage.Visible = true;
                            }
                            else
                            {
                                NotCombineCouponRemoveMessage.Text    = String.Format(NotCombineCouponRemoveMessage.Text, recentCoupon.Coupon.Name, previousCouponsRemoved);
                                NotCombineCouponRemoveMessage.Visible = true;
                            }
                        }
                        ValidCouponMessage.Visible = true;
                    }
                    else
                    {
                        InvalidCouponMessage.Text = couponValidityMessage + "<br /><br />";
                    }
                }
                else
                {
                    InvalidCouponMessage.Text = string.Format(InvalidCouponMessage.Text, "The coupon code you've entered is already in use.");
                }
            }
            else
            {
                InvalidCouponMessage.Text = string.Format(InvalidCouponMessage.Text, "The coupon code you've entered is invalid.");
            }
            InvalidCouponMessage.Visible = !ValidCouponMessage.Visible;
        }
Exemple #5
0
        protected int SaveCoupon()
        {
            if (Page.IsValid)
            {
                // VALIDATE IF A PROPER END DATE IS SELECTED
                if (EndDate.SelectedEndDate != DateTime.MinValue && DateTime.Compare(EndDate.SelectedEndDate, StartDate.SelectedEndDate) < 0)
                {
                    CustomValidator dateValidator = new CustomValidator();
                    dateValidator.ControlToValidate = "Name"; // THIS SHOULD BE "EndDate" CONTROL, BUT THAT CANNOT BE VALIDATED
                    dateValidator.Text         = "*";
                    dateValidator.ErrorMessage = "End date can not be earlier than start date.";
                    dateValidator.IsValid      = false;
                    phEndDateValidator.Controls.Add(dateValidator);
                    return(0);
                }

                Coupon existingCoupon = CouponDataSource.LoadForCouponCode(CouponCode.Text);
                if (existingCoupon != null)
                {
                    CustomValidator codeValidator = new CustomValidator();
                    codeValidator.ControlToValidate = "CouponCode";
                    codeValidator.Text         = "*";
                    codeValidator.ErrorMessage = "The coupon code " + CouponCode.Text + " is already in use.";
                    codeValidator.IsValid      = false;
                    phCouponCodeValidator.Controls.Add(codeValidator);
                    return(0);
                }

                Coupon _Coupon = new Coupon();
                _Coupon.CouponType     = this.CouponType;
                _Coupon.Name           = Name.Text;
                _Coupon.CouponCode     = CouponCode.Text;
                _Coupon.DiscountAmount = AlwaysConvert.ToDecimal(DiscountAmount.Text);
                _Coupon.IsPercent      = (DiscountType.SelectedIndex == 0);
                //QUANTITY SETTINGS (PRODUCT COUPON)
                if (_Coupon.CouponType == CouponType.Product)
                {
                    _Coupon.MinQuantity = AlwaysConvert.ToInt16(Quantity.Text);
                    if (RepeatCoupon.Checked)
                    {
                        _Coupon.MaxQuantity      = 0;
                        _Coupon.QuantityInterval = _Coupon.MinQuantity;
                    }
                    else
                    {
                        _Coupon.MaxQuantity      = _Coupon.MinQuantity;
                        _Coupon.QuantityInterval = 0;
                    }
                    _Coupon.MaxValue    = 0;
                    _Coupon.MinPurchase = 0;
                }
                //PURCHASE RESTRICTIONS (ORDER AND SHIPPING COUPONS)
                else
                {
                    _Coupon.MaxValue         = AlwaysConvert.ToDecimal(MaxValue.Text);
                    _Coupon.MinPurchase      = AlwaysConvert.ToDecimal(MinPurchase.Text);
                    _Coupon.MinQuantity      = 0;
                    _Coupon.MaxQuantity      = 0;
                    _Coupon.QuantityInterval = 0;
                }
                //SET START DATE
                _Coupon.StartDate = StartDate.SelectedDate;
                //SET END DATE
                _Coupon.EndDate = EndDate.SelectedEndDate;
                //MAX USES
                _Coupon.MaxUsesPerCustomer = AlwaysConvert.ToInt16(MaximumUsesPerCustomer.Text);
                _Coupon.MaxUses            = AlwaysConvert.ToInt16(MaximumUses.Text);
                //COMBINE RULE
                _Coupon.AllowCombine = AllowCombine.Checked;
                //PRODUCT (OR SHIPPING) RULE
                if (_Coupon.CouponType != CouponType.Shipping)
                {
                    _Coupon.ProductRule = (CouponRule)ProductRule.SelectedIndex;
                }
                else
                {
                    _Coupon.ProductRule = (CouponRule)ShipMethodRule.SelectedIndex;
                    _Coupon.ShipMethods.Clear();
                    _Coupon.Save();
                    if (_Coupon.ProductRule != CouponRule.All)
                    {
                        foreach (ListItem item in ShipMethodList.Items)
                        {
                            ShipMethod shipMethod = ShipMethodDataSource.Load(AlwaysConvert.ToInt(item.Value));
                            if (item.Selected)
                            {
                                _Coupon.ShipMethods.Add(shipMethod);
                            }
                        }
                    }
                }
                //GROUP RESTRICTION
                if (UseGroupRestriction.SelectedIndex > 0)
                {
                    _Coupon.Groups.Clear();
                    _Coupon.Save();
                    foreach (ListItem item in GroupList.Items)
                    {
                        Group group = GroupDataSource.Load(AlwaysConvert.ToInt(item.Value));
                        if (item.Selected)
                        {
                            _Coupon.Groups.Add(group);
                        }
                    }
                }
                _Coupon.Save();
                return(_Coupon.Id);
            }
            return(0);
        }
Exemple #6
0
        protected void ApplyCouponButton_Click(object sender, EventArgs e)
        {
            ValidCouponMessage.Visible = false;
            CouponCode.Text            = StringHelper.StripHtml(CouponCode.Text);
            Coupon            coupon = CouponDataSource.LoadForCouponCode(CouponCode.Text);
            String            couponValidityMessage = String.Empty;
            ICouponCalculator couponCalculator      = AbleContext.Resolve <ICouponCalculator>();

            if (coupon != null)
            {
                if (!couponCalculator.IsCouponAlreadyUsed(_Basket, coupon))
                {
                    if (couponCalculator.IsCouponValid(_Basket, coupon, out couponValidityMessage))
                    {
                        BasketCoupon recentCoupon = new BasketCoupon(_Basket, coupon);
                        _Basket.BasketCoupons.Add(recentCoupon);
                        // APPLY COUPON COMBINE RULE
                        //THE RULE:
                        //If most recently applied coupon is marked "do not combine", then all previously
                        //entered coupons must be removed from _Basket.

                        //If most recently applied coupon is marked "combine", then remove any applied
                        //coupon that is marked "do not combine".  (Logically, in this instance there
                        //could be at most one other coupon of this type...)
                        string previousCouponsRemoved = "";

                        if (recentCoupon.Coupon.AllowCombine)
                        {
                            //IF ALLOW COMBINE, REMOVE ALL PREVIOUS NOCOMBINE COUPONS
                            for (int i = (_Basket.BasketCoupons.Count - 1); i >= 0; i--)
                            {
                                if (!_Basket.BasketCoupons[i].Coupon.AllowCombine)
                                {
                                    if (previousCouponsRemoved.Length > 0)
                                    {
                                        previousCouponsRemoved += "," + _Basket.BasketCoupons[i].Coupon.Name;
                                    }
                                    else
                                    {
                                        previousCouponsRemoved = _Basket.BasketCoupons[i].Coupon.Name;
                                    }

                                    _Basket.BasketCoupons.DeleteAt(i);
                                }
                            }
                        }
                        else
                        {
                            //IF NOT ALLOW COMBINE, REMOVE ALL EXCEPT THIS COUPON
                            for (int i = (_Basket.BasketCoupons.Count - 1); i >= 0; i--)
                            {
                                if (_Basket.BasketCoupons[i] != recentCoupon)
                                {
                                    if (previousCouponsRemoved.Length > 0)
                                    {
                                        previousCouponsRemoved += "," + _Basket.BasketCoupons[i].Coupon.Name;
                                    }
                                    else
                                    {
                                        previousCouponsRemoved = _Basket.BasketCoupons[i].Coupon.Name;
                                    }
                                    _Basket.BasketCoupons.DeleteAt(i);
                                }
                            }
                        }

                        _Basket.Save();
                        IBasketService preCheckoutService = AbleContext.Resolve <IBasketService>();
                        preCheckoutService.Recalculate(_Basket);
                        if (previousCouponsRemoved.Length > 0)
                        {
                            if (recentCoupon.Coupon.AllowCombine)
                            {
                                CombineCouponRemoveMessage.Text    = String.Format(CombineCouponRemoveMessage.Text, recentCoupon.Coupon.Name, previousCouponsRemoved, previousCouponsRemoved);
                                CombineCouponRemoveMessage.Visible = true;
                            }
                            else
                            {
                                NotCombineCouponRemoveMessage.Text    = String.Format(NotCombineCouponRemoveMessage.Text, recentCoupon.Coupon.Name, previousCouponsRemoved);
                                NotCombineCouponRemoveMessage.Visible = true;
                            }
                        }
                        ValidCouponMessage.Visible = true;
                    }
                    else
                    {
                        InvalidCouponMessage.Text = couponValidityMessage + "<br /><br />";
                    }
                }
                else
                {
                    InvalidCouponMessage.Text = "The coupon code you've entered is already in use.<br /><br />";
                }
            }
            else
            {
                InvalidCouponMessage.Text = "The coupon code you've entered is invalid.<br /><br />";
            }
            CouponCode.Text = string.Empty;
            InvalidCouponMessage.Visible = !ValidCouponMessage.Visible;
        }