/// <summary>
        /// Gets the product back-orderable information.
        /// </summary>
        /// <param name="shopName">The shop name.</param>
        /// <param name="productIds">The product ids.</param>
        /// <param name="visitorId">The visitor id.</param>
        /// <param name="location">The wharehouse location.</param>
        /// <returns>The products back-orderable information.</returns>
        public List <OrderableInformation> GetBackOrderableInformation(string shopName, IEnumerable <string> productIds, string visitorId, string location)
        {
            var request = new GetBackOrderableInformationRequest(shopName, productIds.Select(pid => new InventoryProduct {
                ProductId = pid
            }).ToList())
            {
                VisitorId = visitorId, Location = location
            };
            GetBackOrderableInformationResult result = this._serviceProvider.GetBackOrderableInformation(request);
            var orderableInfos = new List <OrderableInformation>();

            if (result == null)
            {
                return(orderableInfos);
            }

            foreach (var orderableInfo in productIds.Select(id => (result.OrderableInformation.FirstOrDefault(i => i.Product.ProductId.Equals(id, System.StringComparison.OrdinalIgnoreCase)) ?? new OrderableInformation {
                Product = new InventoryProduct {
                    ProductId = id
                }
            })).Where(orderableInfo => !orderableInfos.Contains(orderableInfo)))
            {
                orderableInfos.Add(orderableInfo);
            }

            return(orderableInfos);
        }
        public void ShouldNotAddNewRecordsIfProductBackOrderableInformationIsNotFound()
        {
            // Arrange
            var request = new GetBackOrderableInformationRequest("shopname", new List <InventoryProduct> {
                new InventoryProduct {
                    ProductId = "noExist"
                }
            });
            var result = new GetBackOrderableInformationResult();
            var args   = new ServicePipelineArgs(request, result);

            var results = new OrderableInformationModel[1];

            results[0] = null;
            this._client.GetBackOrderableInformationList("shopname", Arg.Is <string[]>(ids => (ids.Length == 1) && (ids[1] == "noExist")), new System.Guid()).Returns(results);

            // Act
            this._processor.Process(args);

            // Assert
            result.OrderableInformation.Should().HaveCount(1);
            result.OrderableInformation.ElementAt(0).Product.ProductId.Should().Be("noExist");
            result.OrderableInformation.ElementAt(0).Status.Should().Be(null);
            result.OrderableInformation.ElementAt(0).InStockDate.Should().Be(null);
            result.OrderableInformation.ElementAt(0).OrderableEndDate.Should().Be(null);
            result.OrderableInformation.ElementAt(0).OrderableStartDate.Should().Be(null);
            result.OrderableInformation.ElementAt(0).RemainingQuantity.Should().Be(0);
            result.OrderableInformation.ElementAt(0).ShippingDate.Should().Be(null);
            result.OrderableInformation.ElementAt(0).CartQuantityLimit.Should().Be(0);
        }
        /// <summary>
        /// The update stock informationS.
        /// </summary>
        /// <param name="cartLine">The cart line.</param>
        private void UpdateStockInformation([NotNull] CartLine cartLine)
        {
            Assert.ArgumentNotNull(cartLine, "cartLine");

            var products = new List <InventoryProduct> {
                new InventoryProduct {
                    ProductId = cartLine.Product.ProductId
                }
            };
            var stockInfoRequest = new GetStockInformationRequest(this.ShopName, products, StockDetailsLevel.Status);
            var stockInfoResult  = this._inventoryServiceProvider.GetStockInformation(stockInfoRequest);

            if (stockInfoResult.StockInformation == null || !stockInfoResult.StockInformation.Any())
            {
                return;
            }

            var stockInfo     = stockInfoResult.StockInformation.FirstOrDefault();
            var orderableInfo = new OrderableInformation();

            if (stockInfo != null && stockInfo.Status != null)
            {
                if (Equals(stockInfo.Status, StockStatus.PreOrderable))
                {
                    var preOrderableRequest = new GetPreOrderableInformationRequest(this.ShopName, products);
                    var preOrderableResult  = this._inventoryServiceProvider.GetPreOrderableInformation(preOrderableRequest);
                    if (preOrderableResult.OrderableInformation != null && preOrderableResult.OrderableInformation.Any())
                    {
                        orderableInfo = preOrderableResult.OrderableInformation.FirstOrDefault();
                    }
                }
                else if (Equals(stockInfo.Status, StockStatus.BackOrderable))
                {
                    var backOrderableRequest = new GetBackOrderableInformationRequest(this.ShopName, products);
                    var backOrderableResult  = this._inventoryServiceProvider.GetBackOrderableInformation(backOrderableRequest);
                    if (backOrderableResult.OrderableInformation != null && backOrderableResult.OrderableInformation.Any())
                    {
                        orderableInfo = backOrderableResult.OrderableInformation.FirstOrDefault();
                    }
                }
            }

            if (stockInfo != null)
            {
                cartLine.Product.StockStatus = stockInfo.Status;
            }

            if (orderableInfo == null)
            {
                return;
            }

            cartLine.Product.InStockDate  = orderableInfo.InStockDate;
            cartLine.Product.ShippingDate = orderableInfo.ShippingDate;
        }
        /// <summary>
        /// Gets the back orderable information.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="products">The products.</param>
        /// <returns>The manager response which returns an enumerable collection of OrderableInformation in the Result.</returns>
        public virtual ManagerResponse <GetBackOrderableInformationResult, IEnumerable <OrderableInformation> > GetBackOrderableInformation([NotNull] CommerceStorefront storefront, IEnumerable <InventoryProduct> products)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(products, "products");

            var request = new GetBackOrderableInformationRequest(storefront.ShopName, products);
            var result  = this._inventoryServiceProvider.GetBackOrderableInformation(request);

            // Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <GetBackOrderableInformationResult, IEnumerable <OrderableInformation> >(result, !result.Success || result.OrderableInformation == null ? new List <OrderableInformation>() : result.OrderableInformation));
        }
        public ManagerResponse <GetBackOrderableInformationResult, IEnumerable <OrderableInformation> > GetBackOrderableInformation(IEnumerable <InventoryProduct> products)
        {
            Assert.ArgumentNotNull(products, nameof(products));
            if (StorefrontContext.Current == null)
            {
                throw new InvalidOperationException("Cannot be called without a valid storefront context.");
            }

            var request = new GetBackOrderableInformationRequest(StorefrontContext.Current.ShopName, products);
            var result  = InventoryServiceProvider.GetBackOrderableInformation(request);

            result.WriteToSitecoreLog();
            return(new ManagerResponse <GetBackOrderableInformationResult, IEnumerable <OrderableInformation> >(result, !result.Success || result.OrderableInformation == null ? new List <OrderableInformation>() : result.OrderableInformation));
        }
        public void ShouldGetBackOrderableInformationForTheSpecifiedProducts()
        {
            // Arrange
            var request = new GetBackOrderableInformationRequest("shopname", new List <InventoryProduct> {
                new InventoryProduct {
                    ProductId = "pid1"
                }
            });
            var result = new GetBackOrderableInformationResult();
            var args   = new ServicePipelineArgs(request, result);

            this._client.GetBackOrderableInformationList("shopname", Arg.Is <string[]>(ids => (ids.Length == 1) && (ids[0] == "pid1")), new System.Guid())
            .Returns(new[] { new OrderableInformationModel {
                                 ProductId = "pid1", Status = NopCommerce.NopInventoryService.StockStatus.InStock
                             } });

            // Act
            this._processor.Process(args);

            // Assert
            result.OrderableInformation.Should().HaveCount(1);
            result.OrderableInformation.ElementAt(0).Product.ProductId.Should().Be("pid1");
            result.OrderableInformation.ElementAt(0).Status.Should().Be(1);
        }
        /// <summary>
        /// Gets the back orderable information.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="products">The products.</param>
        /// <returns>The manager response which returns an enumerable collection of OrderableInformation in the Result.</returns>
        public virtual ManagerResponse<GetBackOrderableInformationResult, IEnumerable<OrderableInformation>> GetBackOrderableInformation([NotNull] CommerceStorefront storefront, IEnumerable<InventoryProduct> products)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(products, "products");

            var request = new GetBackOrderableInformationRequest(storefront.ShopName, products);
            var result = this.InventoryServiceProvider.GetBackOrderableInformation(request);

            Helpers.LogSystemMessages(result.SystemMessages, result);
            return new ManagerResponse<GetBackOrderableInformationResult, IEnumerable<OrderableInformation>>(result, !result.Success || result.OrderableInformation == null ? new List<OrderableInformation>() : result.OrderableInformation);
        }