Example #1
0
        /// <summary>
        /// Gets a shipping rate computation method
        /// </summary>
        /// <param name="ShippingRateComputationMethodID">Shipping rate computation method identifier</param>
        /// <returns>Shipping rate computation method</returns>
        public static ShippingRateComputationMethod GetShippingRateComputationMethodByID(int ShippingRateComputationMethodID)
        {
            if (ShippingRateComputationMethodID == 0)
            {
                return(null);
            }

            string key  = string.Format(SHIPPINGRATECOMPUTATIONMETHODS_BY_ID_KEY, ShippingRateComputationMethodID);
            object obj2 = NopCache.Get(key);

            if (ShippingRateComputationMethodManager.CacheEnabled && (obj2 != null))
            {
                return((ShippingRateComputationMethod)obj2);
            }

            DBShippingRateComputationMethod dbItem = DBProviderManager <DBShippingRateComputationMethodProvider> .Provider.GetShippingRateComputationMethodByID(ShippingRateComputationMethodID);

            ShippingRateComputationMethod shippingRateComputationMethod = DBMapping(dbItem);

            if (ShippingRateComputationMethodManager.CacheEnabled)
            {
                NopCache.Max(key, shippingRateComputationMethod);
            }
            return(shippingRateComputationMethod);
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="Cart">Shopping cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="ShippingAddress">Shipping address</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping options</returns>
        public static ShippingOptionCollection GetShippingOptions(ShoppingCart Cart, Customer customer, Address ShippingAddress, ref string Error)
        {
            if (Cart == null)
            {
                throw new ArgumentNullException("Cart");
            }

            bool isFreeShipping = IsFreeShipping(Cart, customer);

            ShipmentPackage ShipmentPackage = CreateShipmentPackage(Cart, customer, ShippingAddress);
            ShippingRateComputationMethod activeShippingRateComputationMethod = ActiveShippingRateComputationMethod;

            if (activeShippingRateComputationMethod == null)
            {
                throw new NopException("Shipping rate computation method could not be loaded");
            }
            IShippingRateComputationMethod iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(activeShippingRateComputationMethod.ClassName)) as IShippingRateComputationMethod;

            ShippingOptionCollection shippingOptions = iShippingRateComputationMethod.GetShippingOptions(ShipmentPackage, ref Error);

            decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(Cart, customer);

            shippingOptions.ForEach(so => so.Rate += additionalShippingCharge);

            if (isFreeShipping)
            {
                shippingOptions.ForEach(so => so.Rate = Decimal.Zero);
            }

            shippingOptions.ForEach(so => so.Rate = Math.Round(so.Rate, 2));

            return(shippingOptions);
        }
        /// <summary>
        /// Updates the shipping rate computation method
        /// </summary>
        /// <param name="shippingRateComputationMethod">Shipping rate computation method</param>
        public void UpdateShippingRateComputationMethod(ShippingRateComputationMethod shippingRateComputationMethod)
        {
            if (shippingRateComputationMethod == null)
            {
                throw new ArgumentNullException("shippingRateComputationMethod");
            }

            shippingRateComputationMethod.Name                  = CommonHelper.EnsureNotNull(shippingRateComputationMethod.Name);
            shippingRateComputationMethod.Name                  = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.Name, 100);
            shippingRateComputationMethod.Description           = CommonHelper.EnsureNotNull(shippingRateComputationMethod.Description);
            shippingRateComputationMethod.Description           = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.Description, 4000);
            shippingRateComputationMethod.ConfigureTemplatePath = CommonHelper.EnsureNotNull(shippingRateComputationMethod.ConfigureTemplatePath);
            shippingRateComputationMethod.ConfigureTemplatePath = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.ConfigureTemplatePath, 500);
            shippingRateComputationMethod.ClassName             = CommonHelper.EnsureNotNull(shippingRateComputationMethod.ClassName);
            shippingRateComputationMethod.ClassName             = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.ClassName, 500);


            if (!_context.IsAttached(shippingRateComputationMethod))
            {
                _context.ShippingRateComputationMethods.Attach(shippingRateComputationMethod);
            }

            _context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(SHIPPINGRATECOMPUTATIONMETHODS_PATTERN_KEY);
            }
        }
Example #4
0
        /// <summary>
        /// Updates the shipping rate computation method
        /// </summary>
        /// <param name="ShippingRateComputationMethodID">The shipping rate computation method identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="Description">The description</param>
        /// <param name="ConfigureTemplatePath">The configure template path</param>
        /// <param name="ClassName">The class name</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Shipping rate computation method</returns>
        public static ShippingRateComputationMethod UpdateShippingRateComputationMethod(int ShippingRateComputationMethodID, string Name, string Description,
                                                                                        string ConfigureTemplatePath, string ClassName, int DisplayOrder)
        {
            DBShippingRateComputationMethod dbItem = DBProviderManager <DBShippingRateComputationMethodProvider> .Provider.UpdateShippingRateComputationMethod(ShippingRateComputationMethodID, Name, Description,
                                                                                                                                                               ConfigureTemplatePath, ClassName, DisplayOrder);

            ShippingRateComputationMethod shippingRateComputationMethod = DBMapping(dbItem);

            if (ShippingRateComputationMethodManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SHIPPINGRATECOMPUTATIONMETHODS_PATTERN_KEY);
            }
            return(shippingRateComputationMethod);
        }
        /// <summary>
        /// Inserts a shipping rate computation method
        /// </summary>
        /// <param name="name">The name</param>
        /// <param name="description">The description</param>
        /// <param name="configureTemplatePath">The configure template path</param>
        /// <param name="className">The class name</param>
        /// <param name="isActive">The value indicating whether the method is active</param>
        /// <param name="displayOrder">The display order</param>
        /// <returns>Shipping rate computation method</returns>
        public static ShippingRateComputationMethod InsertShippingRateComputationMethod(string name,
                                                                                        string description, string configureTemplatePath, string className,
                                                                                        bool isActive, int displayOrder)
        {
            DBShippingRateComputationMethod dbItem = DBProviderManager <DBShippingRateComputationMethodProvider> .Provider.InsertShippingRateComputationMethod(name,
                                                                                                                                                               description, configureTemplatePath, className, isActive, displayOrder);

            ShippingRateComputationMethod shippingRateComputationMethod = DBMapping(dbItem);

            if (ShippingRateComputationMethodManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(SHIPPINGRATECOMPUTATIONMETHODS_PATTERN_KEY);
            }
            return(shippingRateComputationMethod);
        }
Example #6
0
        private static ShippingRateComputationMethodCollection DBMapping(DBShippingRateComputationMethodCollection dbCollection)
        {
            if (dbCollection == null)
            {
                return(null);
            }

            ShippingRateComputationMethodCollection collection = new ShippingRateComputationMethodCollection();

            foreach (DBShippingRateComputationMethod dbItem in dbCollection)
            {
                ShippingRateComputationMethod item = DBMapping(dbItem);
                collection.Add(item);
            }

            return(collection);
        }
Example #7
0
        private static ShippingRateComputationMethod DBMapping(DBShippingRateComputationMethod dbItem)
        {
            if (dbItem == null)
            {
                return(null);
            }

            ShippingRateComputationMethod item = new ShippingRateComputationMethod();

            item.ShippingRateComputationMethodID = dbItem.ShippingRateComputationMethodID;
            item.Name                  = dbItem.Name;
            item.Description           = dbItem.Description;
            item.ConfigureTemplatePath = dbItem.ConfigureTemplatePath;
            item.ClassName             = dbItem.ClassName;
            item.DisplayOrder          = dbItem.DisplayOrder;

            return(item);
        }
        /// <summary>
        /// Gets shopping cart shipping total
        /// </summary>
        /// <param name="Cart">Cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="includingTax">A value indicating whether calculated price should include tax</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping total</returns>
        public static decimal?GetShoppingCartShippingTotal(ShoppingCart Cart,
                                                           Customer customer, bool includingTax, ref string Error)
        {
            decimal?shippingTotal = null;

            bool isFreeShipping = IsFreeShipping(Cart, customer);

            if (isFreeShipping)
            {
                return(Decimal.Zero);
            }

            ShippingOption lastShippingOption = null;

            if (customer != null)
            {
                lastShippingOption = customer.LastShippingOption;
            }

            if (lastShippingOption != null)
            {
                //use last shipping option (get from cache)
                shippingTotal = TaxManager.GetShippingPrice(lastShippingOption.Rate,
                                                            includingTax,
                                                            customer,
                                                            ref Error);
            }
            else
            {
                //use fixed rate (if possible)
                Address shippingAddress = null;
                if (customer != null)
                {
                    shippingAddress = customer.ShippingAddress;
                }
                ShipmentPackage ShipmentPackage = CreateShipmentPackage(Cart, customer, shippingAddress);
                ShippingRateComputationMethod activeShippingRateComputationMethod = ActiveShippingRateComputationMethod;
                if (activeShippingRateComputationMethod == null)
                {
                    throw new NopException("Shipping rate computation method could not be loaded");
                }
                IShippingRateComputationMethod iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(activeShippingRateComputationMethod.ClassName)) as IShippingRateComputationMethod;

                decimal?fixedRate = iShippingRateComputationMethod.GetFixedRate(ShipmentPackage);
                if (fixedRate.HasValue)
                {
                    decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(Cart, customer);

                    shippingTotal = TaxManager.GetShippingPrice(fixedRate.Value + additionalShippingCharge,
                                                                includingTax,
                                                                customer,
                                                                ref Error);
                }
            }

            if (!shippingTotal.HasValue)
            {
                Error = "Shipping total could not be calculated";
            }
            else
            {
                if (HttpContext.Current.Request.Cookies["Currency"] != null && HttpContext.Current.Request.Cookies["Currency"].Value == "USD")
                {
                    shippingTotal = Math.Round(PriceConverter.ToUsd(shippingTotal.Value));
                }
                else
                {
                    shippingTotal = Math.Round(shippingTotal.Value, 2);
                }
            }
            return(shippingTotal);
        }
        /// <summary>
        /// Updates the shipping rate computation method
        /// </summary>
        /// <param name="shippingRateComputationMethod">Shipping rate computation method</param>
        public void UpdateShippingRateComputationMethod(ShippingRateComputationMethod shippingRateComputationMethod)
        {
            if (shippingRateComputationMethod == null)
                throw new ArgumentNullException("shippingRateComputationMethod");

            shippingRateComputationMethod.Name = CommonHelper.EnsureNotNull(shippingRateComputationMethod.Name);
            shippingRateComputationMethod.Name = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.Name, 100);
            shippingRateComputationMethod.Description = CommonHelper.EnsureNotNull(shippingRateComputationMethod.Description);
            shippingRateComputationMethod.Description = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.Description, 4000);
            shippingRateComputationMethod.ConfigureTemplatePath = CommonHelper.EnsureNotNull(shippingRateComputationMethod.ConfigureTemplatePath);
            shippingRateComputationMethod.ConfigureTemplatePath = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.ConfigureTemplatePath, 500);
            shippingRateComputationMethod.ClassName = CommonHelper.EnsureNotNull(shippingRateComputationMethod.ClassName);
            shippingRateComputationMethod.ClassName = CommonHelper.EnsureMaximumLength(shippingRateComputationMethod.ClassName, 500);

            if (!_context.IsAttached(shippingRateComputationMethod))
                _context.ShippingRateComputationMethods.Attach(shippingRateComputationMethod);

            _context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(SHIPPINGRATECOMPUTATIONMETHODS_PATTERN_KEY);
            }
        }