public ShippingMethodCollection GetShippingMethods(ShippingCalculationContext context)
        {
            var methods = GetRates(new RealTimeRateCalculationContext(
                                       shipmentAddress: context.ShippingAddress,
                                       shippingHandlingExtraFee: AppLogic.AppConfigUSDecimal("ShippingHandlingExtraFee"),
                                       cartItems: context.CartItems,
                                       shipmentValue: context.CartSubtotal(
                                           true,   // includeDiscount
                                           false,  // onlyIncludeTaxableItems
                                           false,  // includeDownloadItems
                                           true,   // includeFreeShippingItems
                                           false,  // includeSystemItems
                                           false,  // useCustomerCurrencySetting
                                           0,      // forShippingAddressId
                                           true,   // excludeTax
                                           false)  // includeShippingNotRequiredItems
                                       ,
                                       shippingTaxRate: context.TaxRate,
                                       customerId: context.Customer.CustomerID));

            var availableShippingMethods = new ShippingMethodCollection();

            if (methods.Any())
            {
                availableShippingMethods.AddRange(GetRealTimeShippingMethods(context, methods));
            }
            else if (methods.ErrorMsg.Contains(AppLogic.AppConfig("RTShipping.CallForShippingPrompt")))
            {
                availableShippingMethods.AddRange(GetCallForShippingMethods());
            }

            return(availableShippingMethods);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the shipping method query per store
        /// </summary>
        /// <param name="context">Calculation context</param>
        /// <param name="includeZone">Whether to include zone mapping</param>
        protected string GenerateShippingMethodsQuery(ShippingCalculationContext context, bool includeZone)
        {
            var shippingMethodToStateMapIsEmpty   = Shipping.ShippingMethodToStateMapIsEmpty();
            var shippingMethodToCountryMapIsEmpty = Shipping.ShippingMethodToCountryMapIsEmpty();
            var customerStateID   = AppLogic.GetStateID(context.ShippingAddress.State);
            var customerCountryID = AppLogic.GetCountryID(context.ShippingAddress.Country);

            if (customerCountryID < 1 && context.ShippingAddress.Country.Length == 2)
            {
                customerCountryID = AppLogic.GetCountryIDFromTwoLetterISOCode(context.ShippingAddress.Country);
            }

            if (customerCountryID < 1 && context.ShippingAddress.Country.Length == 3)
            {
                customerCountryID = AppLogic.GetCountryIDFromThreeLetterISOCode(context.ShippingAddress.Country);
            }

            var shipsql = new StringBuilder(1024);

            if (context.StoreId == Shipping.DONT_FILTER_PER_STORE)
            {
                shipsql.AppendFormat(
                    "select sm.* from ShippingMethod sm with(nolock) where sm.IsRTShipping = 0");
            }
            else
            {
                shipsql.AppendFormat(
                    @"select sm.* from ShippingMethod sm 
					inner join ShippingMethodStore sms on sms.ShippingMethodId = sm.ShippingMethodId 
					where sm.IsRTShipping = 0 and sms.StoreId = {0}"                    , context.StoreId);
            }

            if (!shippingMethodToStateMapIsEmpty && customerStateID <= 0)
            {
                shipsql.Append(" and sm.ShippingMethodID not in (select ShippingMethodID from ShippingMethodToStateMap with (NOLOCK))");
            }

            if (!shippingMethodToStateMapIsEmpty && customerStateID > 0)
            {
                shipsql.Append(" and sm.ShippingMethodID in (select ShippingMethodID from ShippingMethodToStateMap with (NOLOCK) where StateID = " + customerStateID.ToString() + ")");
            }

            if (!shippingMethodToCountryMapIsEmpty)
            {
                shipsql.Append(" and sm.ShippingMethodID in (select ShippingMethodID from ShippingMethodToCountryMap with (NOLOCK) where CountryID = " + customerCountryID.ToString() + ")");
            }

            // most of the shipping methods honor the state and country mapping
            // except for the zip zone mappings which only a handful or them use
            if (includeZone && !Shipping.ShippingMethodToZoneMapIsEmpty())
            {
                shipsql.Append(" and ShippingMethodID in (select ShippingMethodID from ShippingMethodToZoneMap with (NOLOCK) where ShippingZoneID = " + Shipping.ZoneLookup(context.ShippingAddress.Zip).ToString() + ")");
            }

            shipsql.Append(" order by Displayorder");

            return(shipsql.ToString());
        }
Ejemplo n.º 3
0
        public ShippingMethodCollection GetShippingMethods(ShippingCalculationContext context)
        {
            var availableShippingMethods = new ShippingMethodCollection();
            var shipSql = GenerateShippingMethodsQuery(context, UsesZones);

            using (var connection = new SqlConnection(DB.GetDBConn()))
            {
                connection.Open();
                using (var reader = DB.GetRS(shipSql, connection))
                    while (reader.Read())
                    {
                        var thisMethod = new ShippingMethod
                        {
                            Id            = DB.RSFieldInt(reader, "ShippingMethodID"),
                            Name          = DB.RSFieldByLocale(reader, "Name", context.Customer.LocaleSetting),
                            IsFree        = context.ShippingIsFreeIfIncludedInFreeList && Shipping.ShippingMethodIsInFreeList(DB.RSFieldInt(reader, "ShippingMethodID")),
                            ImageFileName = DB.RSField(reader, "ImageFileName"),
                        };

                        if (thisMethod.IsFree)
                        {
                            thisMethod.ShippingIsFree = true;
                            thisMethod.Freight        = 0m;
                        }
                        else
                        {
                            var freight = CalculateFreight(context, thisMethod.Id, reader);

                            if (freight > 0m && context.HandlingExtraFee > 0m)
                            {
                                freight += context.HandlingExtraFee;
                            }

                            if (freight < 0)
                            {
                                freight = 0;
                            }

                            thisMethod.Freight = freight;
                        }

                        if (!(context.ExcludeZeroFreightCosts == true && (thisMethod.Freight == 0m && !thisMethod.IsFree)))
                        {
                            availableShippingMethods.Add(thisMethod);
                        }
                    }
            }

            return(availableShippingMethods);
        }
Ejemplo n.º 4
0
 protected override decimal CalculateFreight(ShippingCalculationContext context, int shippingMethodId, IDataReader reader)
 {
     return(Shipping.GetShipByTotalCharge(
                shippingMethodId,
                context.CartSubtotal(
                    true,                   // includeDiscount
                    false,                  // onlyIncludeTaxableItems
                    false,                  // includeDownloadItems
                    false,                  // includeFreeShippingItems
                    true,                   // includeSystemItems
                    false,                  // useCustomerCurrencySetting
                    0,                      // forShippingAddressId
                    true,                   // excludeTax
                    false)                  // includeShippingNotRequiredItems
                ));
 }
        IEnumerable <ShippingMethod> GetRealTimeShippingMethods(ShippingCalculationContext context, IEnumerable <ShippingMethod> methods)
        {
            foreach (var method in methods)
            {
                method.IsFree     = method.IsFree && context.ShippingIsFreeIfIncludedInFreeList;
                method.IsRealTime = true;

                if (context.ShippingIsFreeIfIncludedInFreeList && Shipping.GetFreeShippingMethodIDs().ParseAsDelimitedList <int>().Contains(method.Id))
                {
                    method.Freight        = 0.0M;
                    method.ShippingIsFree = true;
                }

                if (context.ExcludeZeroFreightCosts && method.Freight == decimal.Zero)
                {
                    continue;
                }

                // Add it to the db
                var countOfMatchingMethods = DB.GetSqlN(
                    "select count(*) N from ShippingMethod with (NOLOCK) where IsRTShipping = 1 and Name = @methodName",
                    new SqlParameter("methodName", method.Name));

                if (countOfMatchingMethods == 0)
                {
                    var defaultCarrierIcon = GetDefaultCarrierIcon(method.Carrier);
                    method.ImageFileName = defaultCarrierIcon;
                    DB.ExecuteSQL(
                        "insert ShippingMethod(Name, IsRTShipping, ImageFileName) values(@MethodName, 1, @ImageFileName)",
                        new SqlParameter("MethodName", method.Name),
                        new SqlParameter("ImageFileName", defaultCarrierIcon));
                }

                yield return(method);
            }
        }
Ejemplo n.º 6
0
        protected override decimal CalculateFreight(ShippingCalculationContext context, int shippingMethodId, IDataReader reader)
        {
            var zoneID = Shipping.ZoneLookup(context.ShippingAddress.Zip);

            return(Shipping.GetShipByWeightAndZoneCharge(shippingMethodId, context.Weight, zoneID));
        }
Ejemplo n.º 7
0
 protected abstract decimal CalculateFreight(ShippingCalculationContext context, int shippingMethodId, IDataReader reader);
Ejemplo n.º 8
0
 protected override decimal CalculateFreight(ShippingCalculationContext context, int shippingMethodId, IDataReader reader)
 {
     return(DB.RSFieldDecimal(reader, "FixedRate"));
 }
 protected override decimal CalculateFreight(ShippingCalculationContext context, int shippingMethodId, IDataReader reader)
 {
     return(Shipping.GetShipByWeightCharge(shippingMethodId, context.Weight));
 }