Ejemplo n.º 1
0
        public IHttpActionResult GetShipmentMethods(string id)
        {
            var quote = _quoteRequestService.GetByIds(id).FirstOrDefault();

            if (quote != null)
            {
                var store = _storeService.GetById(quote.StoreId);

                if (store != null)
                {
                    var cartFromQuote = quote.ToCartModel();
                    var evalContext   = new ShippingEvaluationContext(cartFromQuote);
                    var rates         = store.ShippingMethods.Where(x => x.IsActive).SelectMany(x => x.CalculateRates(evalContext)).ToArray();
                    var retVal        = rates.Select(x => new webModel.ShipmentMethod
                    {
                        Currency           = x.Currency,
                        Name               = x.ShippingMethod.Name,
                        Price              = x.Rate,
                        OptionName         = x.OptionName,
                        ShipmentMethodCode = x.ShippingMethod.Code,
                        LogoUrl            = x.ShippingMethod.LogoUrl
                    }).ToArray();

                    return(Ok(retVal));
                }
            }
            return(NotFound());
        }
        public async Task <QuoteRequestTotals> CalculateTotalsAsync(QuoteRequest quote)
        {
            var retVal        = new QuoteRequestTotals();
            var cartFromQuote = quote.ToCartModel(AbstractTypeFactory <ShoppingCart> .TryCreateInstance());

            //Calculate shipment total
            //first try to get manual amount
            retVal.ShippingTotal = quote.ManualShippingTotal;
            if (retVal.ShippingTotal == 0 && quote.ShipmentMethod != null)
            {
                //calculate total by using shipment gateways
                var evalContext = new ShippingEvaluationContext(cartFromQuote);

                var searchCriteria = AbstractTypeFactory <ShippingMethodsSearchCriteria> .TryCreateInstance();

                searchCriteria.StoreId  = quote.StoreId;
                searchCriteria.IsActive = true;
                searchCriteria.Codes    = new[] { quote.ShipmentMethod.ShipmentMethodCode };
                var storeShippingMethods = await _shippingMethodsSearchService.SearchShippingMethodsAsync(searchCriteria);

                var rate = storeShippingMethods.Results
                           .SelectMany(x => x.CalculateRates(evalContext))
                           .FirstOrDefault(x => (quote.ShipmentMethod.OptionName == null) || quote.ShipmentMethod.OptionName == x.OptionName);

                retVal.ShippingTotal = rate != null ? rate.Rate : 0m;
            }

            //Calculate taxes
            var taxSearchCriteria = AbstractTypeFactory <TaxProviderSearchCriteria> .TryCreateInstance();

            taxSearchCriteria.StoreIds = new[] { quote.StoreId };
            taxSearchCriteria.Sort     = nameof(TaxProvider.Priority);
            var taxProvider = (await _taxProviderSearchService.SearchTaxProvidersAsync(taxSearchCriteria)).Results.FirstOrDefault();

            if (taxProvider != null)
            {
                var taxEvalContext = quote.ToTaxEvalContext(AbstractTypeFactory <TaxEvaluationContext> .TryCreateInstance());
                retVal.TaxTotal = taxProvider.CalculateRates(taxEvalContext).Select(x => x.Rate).DefaultIfEmpty(0).Sum(x => x);
            }

            //Calculate subtotal
            var items = quote.Items.Where(x => x.SelectedTierPrice != null);

            if (quote.Items != null)
            {
                retVal.OriginalSubTotalExlTax = items.Sum(x => x.SalePrice * x.SelectedTierPrice.Quantity);
                retVal.SubTotalExlTax         = items.Sum(x => x.SelectedTierPrice.Price * x.SelectedTierPrice.Quantity);
                if (quote.ManualSubTotal > 0)
                {
                    retVal.DiscountTotal  = retVal.SubTotalExlTax - quote.ManualSubTotal;
                    retVal.SubTotalExlTax = quote.ManualSubTotal;
                }
                else if (quote.ManualRelDiscountAmount > 0)
                {
                    retVal.DiscountTotal = retVal.SubTotalExlTax * quote.ManualRelDiscountAmount * 0.01m;
                }
            }

            return(retVal);
        }
Ejemplo n.º 3
0
        public virtual ICollection <ShippingRate> GetShippingRates(string shippingMethodCode)
        {
            var shippingEvaluationContext = new ShippingEvaluationContext(Cart);

            var shippingMethod = Store.ShippingMethods.FirstOrDefault(x => x.IsActive &&
                                                                      x.Code.EqualsInvariant(shippingMethodCode));

            return(shippingMethod?.CalculateRates(shippingEvaluationContext).ToList());
        }
        public async Task <IEnumerable <ShippingRate> > GetAvailableShippingRatesAsync(CartAggregate cartAggr)
        {
            if (cartAggr == null)
            {
                return(Enumerable.Empty <ShippingRate>());
            }

            //Request available shipping rates
            var shippingEvaluationContext = new ShippingEvaluationContext(cartAggr.Cart);

            var criteria = new ShippingMethodsSearchCriteria
            {
                IsActive = true,
                Take     = _takeOnSearch,
                StoreId  = cartAggr.Store?.Id
            };

            var activeAvailableShippingMethods = (await _shippingMethodsSearchService.SearchShippingMethodsAsync(criteria)).Results;

            var availableShippingRates = activeAvailableShippingMethods
                                         .SelectMany(x => x.CalculateRates(shippingEvaluationContext))
                                         .Where(x => x.ShippingMethod == null || x.ShippingMethod.IsActive)
                                         .ToArray();

            if (availableShippingRates.IsNullOrEmpty())
            {
                return(Enumerable.Empty <ShippingRate>());
            }

            //Evaluate promotions cart and apply rewards for available shipping methods
            var evalContext     = _mapper.Map <PromotionEvaluationContext>(cartAggr);
            var promoEvalResult = await cartAggr.EvaluatePromotionsAsync(evalContext);

            foreach (var shippingRate in availableShippingRates)
            {
                shippingRate.ApplyRewards(promoEvalResult.Rewards);
            }

            var taxProvider = await GetActiveTaxProviderAsync(cartAggr.Store.Id);

            if (taxProvider != null)
            {
                var taxEvalContext = _mapper.Map <TaxEvaluationContext>(cartAggr);
                taxEvalContext.Lines.Clear();
                taxEvalContext.Lines.AddRange(availableShippingRates.SelectMany(x => _mapper.Map <IEnumerable <TaxLine> >(x)));
                var taxRates = taxProvider.CalculateRates(taxEvalContext);
                foreach (var shippingRate in availableShippingRates)
                {
                    shippingRate.ApplyTaxRates(taxRates);
                }
            }

            return(availableShippingRates);
        }
        public IHttpActionResult GetShipmentMethods(string cartId)
        {
            var cart        = _shoppingCartService.GetById(cartId);
            var store       = _storeService.GetById(cart.StoreId);
            var evalContext = new ShippingEvaluationContext(cart);

            var retVal = store.ShippingMethods.Where(x => x.IsActive)
                         .SelectMany(x => x.CalculateRates(evalContext))
                         .Select(x => x.ToWebModel()).ToArray();

            return(Ok(retVal));
        }
Ejemplo n.º 6
0
        public QuoteRequestTotals CalculateTotals(QuoteRequest quote)
        {
            var retVal        = new QuoteRequestTotals();
            var cartFromQuote = quote.ToCartModel();
            var store         = _storeService.GetById(quote.StoreId);

            if (store != null)
            {
                //Calculate shipment total
                //firts try to get manual amount
                retVal.ShippingTotal = quote.ManualShippingTotal;
                if (retVal.ShippingTotal == 0 && quote.ShipmentMethod != null)
                {
                    //calculate total by using shipment gateways
                    var evalContext = new ShippingEvaluationContext(cartFromQuote);

                    var rate = store.ShippingMethods.Where(x => x.IsActive && x.Code == quote.ShipmentMethod.ShipmentMethodCode)
                               .SelectMany(x => x.CalculateRates(evalContext))
                               .Where(x => quote.ShipmentMethod.OptionName != null ? quote.ShipmentMethod.OptionName == x.OptionName : true)
                               .FirstOrDefault();
                    retVal.ShippingTotal = rate != null ? rate.Rate : 0m;
                }
                //Calculate taxes
                var taxProvider = store.TaxProviders.Where(x => x.IsActive).OrderBy(x => x.Priority).FirstOrDefault();
                if (taxProvider != null)
                {
                    var taxRequest     = quote.ToTaxRequest();
                    var taxEvalContext = new TaxEvaluationContext(taxRequest);
                    retVal.TaxTotal = taxProvider.CalculateRates(taxEvalContext).Select(x => x.Rate).DefaultIfEmpty(0).Sum(x => x);
                }
            }

            //Calculate subtotal
            var items = quote.Items.Where(x => x.SelectedTierPrice != null);

            if (quote.Items != null)
            {
                retVal.OriginalSubTotalExlTax = items.Sum(x => x.SalePrice * x.SelectedTierPrice.Quantity);
                retVal.SubTotalExlTax         = items.Sum(x => x.SelectedTierPrice.Price * x.SelectedTierPrice.Quantity);
                if (quote.ManualSubTotal > 0)
                {
                    retVal.DiscountTotal  = retVal.SubTotalExlTax - quote.ManualSubTotal;
                    retVal.SubTotalExlTax = quote.ManualSubTotal;
                }
                else if (quote.ManualRelDiscountAmount > 0)
                {
                    retVal.DiscountTotal = retVal.SubTotalExlTax * quote.ManualRelDiscountAmount * 0.01m;
                }
            }

            return(retVal);
        }
Ejemplo n.º 7
0
        public virtual ICollection <ShippingRate> GetAvailableShippingRates()
        {
            // TODO: Remake with shipmentId
            var shippingEvaluationContext = new ShippingEvaluationContext(Cart);

            var activeAvailableShippingMethods = Store.ShippingMethods.Where(x => x.IsActive).ToList();

            var availableShippingRates = activeAvailableShippingMethods
                                         .SelectMany(x => x.CalculateRates(shippingEvaluationContext))
                                         .Where(x => x.ShippingMethod == null || x.ShippingMethod.IsActive)
                                         .ToArray();

            return(availableShippingRates);
        }
Ejemplo n.º 8
0
        public virtual async Task <ICollection <ShippingRate> > GetShippingRatesAsync(string shippingMethodCode)
        {
            var shippingEvaluationContext = new ShippingEvaluationContext(Cart);

            var criteria = new ShippingMethodsSearchCriteria
            {
                IsActive = true,
                Take     = int.MaxValue,
                StoreId  = Store.Id,
                Codes    = new[] { shippingMethodCode }
            };

            var shippingMethod = (await _shippingMethodsSearchService.SearchShippingMethodsAsync(criteria))
                                 .Results.FirstOrDefault();

            return(shippingMethod?.CalculateRates(shippingEvaluationContext).ToList());
        }
Ejemplo n.º 9
0
        public IHttpActionResult GetShipmentMethods(string cartId)
        {
            var cart        = _shoppingCartService.GetById(cartId);
            var store       = _storeService.GetById(cart.StoreId);
            var evalContext = new ShippingEvaluationContext(cart);

            var retVal = store.ShippingMethods.Where(x => x.IsActive).SelectMany(x => x.CalculateRates(evalContext))
                         .Select(x => new webModel.ShippingMethod
            {
                Currency           = cart.Currency,
                Name               = x.ShippingMethod.Description,
                OptionName         = x.OptionName,
                OptionDescription  = x.OptionDescription,
                Price              = x.Rate,
                ShipmentMethodCode = x.ShippingMethod.Code,
                LogoUrl            = x.ShippingMethod.LogoUrl
            });

            return(Ok(retVal));
        }
Ejemplo n.º 10
0
        public virtual async Task <ICollection <ShippingRate> > GetAvailableShippingRatesAsync()
        {
            // TODO: Remake with shipmentId
            var shippingEvaluationContext = new ShippingEvaluationContext(Cart);

            var criteria = new ShippingMethodsSearchCriteria
            {
                IsActive = true,
                Take     = int.MaxValue,
                StoreId  = Store.Id
            };

            var activeAvailableShippingMethods = (await _shippingMethodsSearchService.SearchShippingMethodsAsync(criteria)).Results;

            var availableShippingRates = activeAvailableShippingMethods
                                         .SelectMany(x => x.CalculateRates(shippingEvaluationContext))
                                         .Where(x => x.ShippingMethod == null || x.ShippingMethod.IsActive)
                                         .ToArray();

            return(availableShippingRates);
        }
        public async Task <ActionResult <Core.Models.ShipmentMethod[]> > GetShipmentMethods(string id)
        {
            var quote = (await _quoteRequestService.GetByIdsAsync(id)).FirstOrDefault();

            if (quote != null)
            {
                var store = await _storeService.GetByIdAsync(quote.StoreId);

                if (store != null)
                {
                    var cartFromQuote = quote.ToCartModel(AbstractTypeFactory <ShoppingCart> .TryCreateInstance());
                    var evalContext   = new ShippingEvaluationContext(cartFromQuote);

                    var searchCriteria = AbstractTypeFactory <ShippingMethodsSearchCriteria> .TryCreateInstance();

                    searchCriteria.StoreId          = quote.StoreId;
                    searchCriteria.IsActive         = true;
                    searchCriteria.WithoutTransient = true;
                    var storeShippingMethods = await _shippingMethodsSearchService.SearchShippingMethodsAsync(searchCriteria);

                    var rates = storeShippingMethods.Results
                                .SelectMany(x => x.CalculateRates(evalContext))
                                .ToArray();

                    var retVal = rates.Select(x => new Core.Models.ShipmentMethod
                    {
                        Currency           = x.Currency,
                        Price              = x.Rate,
                        OptionName         = x.OptionName,
                        ShipmentMethodCode = x.ShippingMethod.Code,
                        TypeName           = x.ShippingMethod.TypeName,
                        LogoUrl            = x.ShippingMethod.LogoUrl
                    }).ToArray();

                    return(Ok(retVal));
                }
            }
            return(NotFound());
        }
Ejemplo n.º 12
0
        public IHttpActionResult GetAvailableShippingRatesByContext(ShippingEvaluationContext context)
        {
            var shippingRates = _cartBuilder.TakeCart(context.ShoppingCart).GetAvailableShippingRates();

            return(Ok(shippingRates));
        }
        public async Task <ActionResult <ICollection <ShippingRate> > > GetAvailableShippingRatesByContext(ShippingEvaluationContext context)
        {
            var builder       = _cartBuilder.TakeCart(context.ShoppingCart);
            var shippingRates = await builder.GetAvailableShippingRatesAsync();

            return(Ok(shippingRates));
        }