Example #1
0
        public Utilities.SortableCollection <Shipping.ShippingRateDisplay> FindAvailableShippingRates(Order o)
        {
            Utilities.SortableCollection <Shipping.ShippingRateDisplay> result = new Utilities.SortableCollection <Shipping.ShippingRateDisplay>();

            // Get all the methods that apply to this shipping address and store
            List <Zone>           zones   = ShippingZones.FindAllZonesForAddress(o.ShippingAddress, o.StoreId);
            List <ShippingMethod> methods = new List <ShippingMethod>();

            methods = this.ShippingMethods.FindForZones(zones);

            // Get Rates for each Method
            foreach (Shipping.ShippingMethod m in methods)
            {
                Collection <Shipping.ShippingRateDisplay> tempRates = m.GetRates(o);
                if (tempRates != null)
                {
                    for (int i = 0; i <= tempRates.Count - 1; i++)
                    {
                        ShippingRateDisplay fRate = tempRates[i].GetCopy();
                        //fRate.AdjustRate(m.AdjustmentType, m.Adjustment);
                        result.Add(fRate);
                    }
                }
            }

            // Tally up extra ship fees
            decimal totalExtraFees = 0m;

            foreach (LineItem li in o.Items)
            {
                if (li.ExtraShipCharge > 0)
                {
                    totalExtraFees += li.ExtraShipCharge;
                }
            }

            // update results with extra ship fees and handling
            foreach (Shipping.ShippingRateDisplay displayRate in result)
            {
                displayRate.Rate += totalExtraFees + o.TotalHandling;
            }


            // Apply promotions to rates here:

            // Run workflow to get shipping rate discounts
            //BusinessRules.ShippingTaskContext c = new BusinessRules.ShippingTaskContext();
            //c.Rates = result;
            //c.UserId = this.UserID;
            //c.Order = this;
            //BusinessRules.Workflow.RunByName(c, BusinessRules.WorkflowNames.ApplyShippingDiscounts);


            // Sort Rates
            result.Sort("Rate", Utilities.SortDirection.Ascending);

            if (result.Count < 1)
            {
                if (o.IsOrderFreeShipping())
                {
                    result.Add(new Shipping.ShippingRateDisplay("Free Shipping.", "", "", 0m, "FREESHIPPING"));
                }
                else
                {
                    string value = Content.SiteTerms.GetTerm(Content.SiteTermIds.ShippingUnknown);
                    if (!string.IsNullOrEmpty(value))
                    {
                        result.Add(new Shipping.ShippingRateDisplay(value, "", "", 0m, "UNKNOWN"));
                    }
                    else
                    {
                        result.Add(new Shipping.ShippingRateDisplay("To Be Determined. Contact Store for Details", "", "", 0m, "TOBEDETERMINED"));
                    }
                }
            }

            return(result);
        }
Example #2
0
        private SortableCollection <ShippingRateDisplay> FindAvailableShippingRatesInternal(Order order,
                                                                                            string shippingMethodIdFilter = null)
        {
            if (order == null)
            {
                return(new SortableCollection <ShippingRateDisplay>());
            }

            var result = new SortableCollection <ShippingRateDisplay>();

            // Get all the methods that apply to this shipping address and store
            var zones   = ShippingZones.FindAllZonesForAddress(order.ShippingAddress, order.StoreId);
            var methods = ShippingMethods.FindForZones(zones);

            if (!string.IsNullOrEmpty(shippingMethodIdFilter))
            {
                methods = methods.Where(m => m.Bvin == shippingMethodIdFilter).ToList();
            }

            // Get Rates for each Method
            if (!order.IsOrderFreeShipping())
            {
                var tasks = new List <Task <Collection <ShippingRateDisplay> > >();
                foreach (var method in methods)
                {
                    var task = Task.Factory.StartNew(hccContext =>
                    {
                        HccRequestContext.Current = hccContext as HccRequestContext;
                        return(method.GetRates(order, Context.CurrentStore));
                    },
                                                     HccRequestContext.Current);
                    tasks.Add(task);
                }
                Task.WaitAll(tasks.ToArray());

                foreach (var task in tasks)
                {
                    var tempRates = task.Result;
                    if (tempRates != null)
                    {
                        for (var i = 0; i <= tempRates.Count - 1; i++)
                        {
                            var fRate = tempRates[i].GetCopy();
                            result.Add(fRate);
                        }
                    }
                }
            }

            // Update results with extra ship fees and handling
            foreach (ShippingRateDisplay displayRate in result)
            {
                // Tally up extra ship fees
                var totalExtraFees = 0m;
                foreach (var li in order.Items)
                {
                    if (li.ExtraShipCharge > 0 && !li.MarkedForFreeShipping(displayRate.ShippingMethodId) &&
                        li.ShippingCharge == ShippingChargeType.ChargeShippingAndHandling ||
                        li.ShippingCharge == ShippingChargeType.ChargeShipping)
                    {
                        totalExtraFees += li.ExtraShipCharge * li.Quantity;
                    }
                }

                displayRate.Rate += totalExtraFees + order.TotalHandling;
            }


            // Apply promotions to rates here
            var             membershipServices = Factory.CreateService <MembershipServices>();
            CustomerAccount currentUser        = null;

            if (order.UserID != string.Empty)
            {
                currentUser = membershipServices.Customers.Find(order.UserID);
            }

            var marketingServices = Factory.CreateService <MarketingService>();
            var offers            = marketingServices.Promotions.FindAllPotentiallyActive(DateTime.UtcNow,
                                                                                          PromotionType.OfferForShipping);

            foreach (ShippingRateDisplay displayRate in result)
            {
                foreach (var offer in offers)
                {
                    if (offer.DoNotCombine && order.HasAnyNonSaleDiscounts)
                    {
                        continue;
                    }

                    var newRate = offer.ApplyToShippingRate(Context, order, currentUser, displayRate.ShippingMethodId,
                                                            displayRate.Rate);

                    if (newRate < displayRate.Rate)
                    {
                        var discount = -1 * (newRate - displayRate.Rate);

                        displayRate.PotentialDiscount = discount;
                    }
                }
            }

            //Changes to have free shipping available for single item in cart by promotion set for "Order Items" - 9May2016-Tushar

            var offersForLineItems = marketingServices.Promotions.FindAllPotentiallyActive(DateTime.UtcNow,
                                                                                           PromotionType.OfferForLineItems);

            foreach (ShippingRateDisplay displayRate in result)
            {
                foreach (var offer in offersForLineItems)
                {
                    var context = new PromotionContext(Context, PromotionType.OfferForLineItems, offer.Id)
                    {
                        Order                   = order,
                        CurrentCustomer         = currentUser,
                        CustomerDescription     = offer.CustomerDescription,
                        CurrentShippingMethodId = displayRate.ShippingMethodId,
                        AdjustedShippingRate    = displayRate.Rate,
                        OtherOffersApplied      = order.HasAnyNonSaleDiscounts
                    };

                    var isQualified = offer.ApplyForFreeShipping(context);
                    if (order.Items.Count == 1 || order.IsOrderHasAllItemsQualifiedFreeShipping())
                    {
                        var isFreeShippingAction =
                            offer.Actions.Where(
                                p =>
                                p.TypeId.ToString() == LineItemFreeShipping.TypeIdString &&
                                p.Settings.ContainsKey("methodids") &&
                                p.Settings["methodids"].ToUpperInvariant()
                                .Contains(displayRate.ShippingMethodId.ToUpperInvariant())).ToList();

                        if (isFreeShippingAction.Count > 0 && isQualified)
                        {
                            displayRate.PotentialDiscount = displayRate.Rate;
                        }
                    }
                }
            }
            //End changes to have free shipping available for single item in cart by promotion set for "Order Items" - 9May2016-Tushar

            FilterRatesByShippingMethods(result, order);

            // Sort Rates
            result.Sort("Rate", SortDirection.Ascending);

            if (result.Count < 1)
            {
                if (order.IsOrderFreeShipping())
                {
                    var rateName = order.TotalHandling > 0
                        ? GlobalLocalization.GetString("Handling")
                        : GlobalLocalization.GetString("FreeShipping");
                    result.Add(new ShippingRateDisplay(rateName, "", "", order.TotalHandling,
                                                       ShippingMethod.MethodFreeShipping));
                }
                else
                {
                    result.Add(new ShippingRateDisplay(GlobalLocalization.GetString("ToBeDetermined"), string.Empty,
                                                       string.Empty, 0m, ShippingMethod.MethodToBeDetermined));
                }
            }

            return(result);
        }