Esempio n. 1
0
        /// <summary>
        /// Calculate the tax and shipping cost for the order.
        /// </summary>
        private void CalculateTaxAndShipping(CouponInfo coupon)
        {
            if (Order != null)
            {
                List <ItemInfo> cartItems = CurrentCart.GetItems(PortalId, StoreSettings.SecureCookie);
                IShippingInfo   shippingInfo;

                // Calculate Shipping if enabled
                IShippingProvider shippingProvider = StoreController.GetShippingProvider(StoreSettings.ShippingName);
                if (StoreSettings.NoDelivery || Shipping == ShippingMode.None || ApplyFreeShipping(cartItems, ShippingAddress.CountryCode))
                {
                    shippingInfo = shippingProvider.GetFreeShipping();
                }
                else
                {
                    shippingInfo = shippingProvider.CalculateShippingFee(PortalId, BillingAddress, ShippingAddress, cartItems);
                }

                if (shippingInfo == null)
                {
                    plhCheckout.Visible = false;
                    lblError.Text       = String.Format(Localization.GetString("ErrorShippingRates", LocalResourceFile), StoreSettings.DefaultEmailAddress);
                    plhError.Visible    = true;
                    return;
                }

                plhCheckout.Visible = true;
                plhError.Visible    = false;

                // Check for coupon validity
                bool couponIsValid = false;
                if (StoreSettings.AllowCoupons)
                {
                    int couponID = Order.CouponID;
                    if (couponID != Null.NullInteger)
                    {
                        if (coupon == null || coupon.CouponID != couponID)
                        {
                            CouponController couponController = new CouponController();
                            coupon = couponController.GetCoupon(PortalId, couponID);
                        }
                        couponIsValid = ValidateCoupon(coupon, Order);
                    }
                    divStoreCheckoutCoupon.Visible = true;
                }
                else
                {
                    divStoreCheckoutCoupon.Visible = false;
                }

                // Apply Discount if coupon is valid
                if (couponIsValid)
                {
                    decimal discountTotal = 0;
                    switch (coupon.DiscountType)
                    {
                    case CouponDiscount.Percentage:
                        decimal discountPercentage = Convert.ToDecimal(coupon.DiscountPercentage) / 100;
                        foreach (ItemInfo cartItem in cartItems)
                        {
                            switch (coupon.ApplyTo)
                            {
                            case CouponApplyTo.Order:
                                cartItem.Discount = cartItem.SubTotal * discountPercentage;
                                discountTotal    += cartItem.Discount;
                                break;

                            case CouponApplyTo.Category:
                                if (ValidateCategoryCoupon(coupon, cartItem.CategoryID))
                                {
                                    cartItem.Discount = cartItem.SubTotal * discountPercentage;
                                    discountTotal    += cartItem.Discount;
                                }
                                break;

                            case CouponApplyTo.Product:
                                if (cartItem.ProductID == coupon.ItemID)
                                {
                                    cartItem.Discount = cartItem.SubTotal * discountPercentage;
                                    discountTotal    += cartItem.Discount;
                                }
                                break;
                            }
                        }
                        break;

                    case CouponDiscount.FixedAmount:
                        int itemsCount = cartItems.Count;
                        foreach (ItemInfo cartItem in cartItems)
                        {
                            switch (coupon.ApplyTo)
                            {
                            case CouponApplyTo.Order:
                                cartItem.Discount = coupon.DiscountAmount / itemsCount;
                                discountTotal     = coupon.DiscountAmount;
                                break;

                            case CouponApplyTo.Category:
                                if (ValidateCategoryCoupon(coupon, cartItem.CategoryID))
                                {
                                    cartItem.Discount = coupon.DiscountAmount * cartItem.Quantity;
                                    discountTotal    += cartItem.Discount;
                                }
                                break;

                            case CouponApplyTo.Product:
                                if (cartItem.ProductID == coupon.ItemID)
                                {
                                    cartItem.Discount = coupon.DiscountAmount * cartItem.Quantity;
                                    discountTotal    += cartItem.Discount;
                                }
                                break;
                            }
                        }
                        break;

                    case CouponDiscount.FreeShipping:
                        discountTotal = shippingInfo.Cost;
                        // If Free Shipping exclude amount before tax computation!
                        shippingInfo.Cost = 0;
                        break;
                    }
                    Order.Discount     = -discountTotal;
                    lblDiscount.Text   = Order.Discount.ToString("C", _localFormat);
                    tbDiscount.Visible = Order.Discount > 0;
                }
                else
                {
                    // Reset order discount
                    Order.CouponID = Null.NullInteger;
                    Order.Discount = Null.NullDecimal;
                    foreach (ItemInfo cartItem in cartItems)
                    {
                        cartItem.Discount = 0;
                    }
                    tbDiscount.Visible = false;
                }

                // Add Surcharges if any
                decimal fixedSurcharge   = PaymentControl.SurchargeFixed;
                decimal percentSurcharge = PaymentControl.SurchargePercent;

                if (fixedSurcharge != 0 || percentSurcharge != 0)
                {
                    Order.ShippingCost = shippingInfo.Cost + fixedSurcharge + ((Order.OrderNetTotal + shippingInfo.Cost + fixedSurcharge) * (percentSurcharge / 100));
                    shippingInfo.Cost  = Order.ShippingCost;
                }
                else
                {
                    Order.ShippingCost = shippingInfo.Cost;
                }

                plhShippingCheckout.Visible = Order.ShippingCost > 0;

                // Calculate Tax Amount
                ITaxProvider taxProvider = StoreController.GetTaxProvider(StoreSettings.TaxName);
                ITaxInfo     taxInfo     = taxProvider.CalculateSalesTax(PortalId, cartItems, shippingInfo, BillingAddress);
                if (taxInfo.ShowTax && taxInfo.SalesTax > 0)
                {
                    plhTaxCheckout.Visible = true;
                    Order.TaxTotal         = taxInfo.SalesTax;
                    Order.ShippingTax      = taxInfo.ShippingTax;
                }
                else
                {
                    plhTaxCheckout.Visible = false;
                    Order.TaxTotal         = 0;
                    Order.ShippingTax      = 0;
                }
            }
        }