public ActionResult Index(BaseProduct currentContent, string variationCode = "", bool quickview = false)
        {
            var variations = GetVariations(currentContent).ToList();

            if (_isInEditMode && !variations.Any())
            {
                var productWithoutVariation = new BaseProductViewModel
                {
                    Product         = currentContent,
                    Images          = currentContent.GetAssets <IContentImage>(_contentLoader, _urlResolver),
                    CategoryPage    = _contentLoader.Get <NodeContent>(_contentLoader.Get <NodeContent>(currentContent.ParentLink).ParentLink),
                    SubcategoryPage = _contentLoader.Get <NodeContent>(currentContent.ParentLink)
                };
                return(Request.IsAjaxRequest() ? PartialView("ProductWithoutVariation", productWithoutVariation) : (ActionResult)View("ProductWithoutVariation", productWithoutVariation));
            }

            BaseVariant variation;

            if (!TryGetVariant(variations, variationCode, out variation))
            {
                return(HttpNotFound());
            }

            var market   = _currentMarket.GetCurrentMarket();
            var currency = _currencyservice.GetCurrentCurrency();

            var defaultPrice    = GetDefaultPrice(variation, market, currency);
            var discountedPrice = GetDiscountPrice(defaultPrice, market, currency);

            var viewModel = new BaseProductViewModel
            {
                Product         = currentContent,
                Variation       = variation,
                ListingPrice    = defaultPrice != null ? defaultPrice.UnitPrice : new Money(0, currency),
                DiscountedPrice = discountedPrice,
                Colors          = variations
                                  .Where(x => x.Size != null)
                                  .GroupBy(x => x.Color)
                                  .Select(g => new SelectListItem
                {
                    Selected = false,
                    Text     = g.First().Color,
                    Value    = g.First().Color
                })
                                  .ToList(),
                Sizes = variations
                        .Where(x => x.Color != null && x.Color.Equals(variation.Color, StringComparison.OrdinalIgnoreCase))
                        .Select(x => new SelectListItem
                {
                    Selected = false,
                    Text     = x.Size,
                    Value    = x.Size
                })
                        .ToList(),
                Color       = variation.Color,
                Size        = variation.Size,
                Images      = variation.GetAssets <IContentImage>(_contentLoader, _urlResolver),
                IsAvailable = defaultPrice != null,
                Variants    = variations.Select(variant =>
                {
                    var variantImage        = variant.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault();
                    var variantDefaultPrice = GetDefaultPrice(variant, market, currency);
                    return(new VariantViewModel
                    {
                        Sku = variant.Code,
                        Size = $"{variant.Color} {variant.Size}",
                        ImageUrl = string.IsNullOrEmpty(variantImage) ? "http://placehold.it/54x54/" : variantImage,
                        DiscountedPrice = GetDiscountPrice(variantDefaultPrice, market, currency),
                        ListingPrice = variantDefaultPrice?.UnitPrice ?? new Money(0, currency),
                        StockQuantity = _quickOrderService.GetTotalInventoryByEntry(variant.Code)
                    });
                }).ToList(),
                CategoryPage    = _contentLoader.Get <NodeContent>(_contentLoader.Get <NodeContent>(currentContent.ParentLink).ParentLink),
                SubcategoryPage = _contentLoader.Get <NodeContent>(currentContent.ParentLink)
            };

            if (Session[Constants.ErrorMesages] != null)
            {
                var messages = Session[Constants.ErrorMesages] as List <string>;
                viewModel.ReturnedMessages      = messages;
                Session[Constants.ErrorMesages] = "";
            }

            if (quickview)
            {
                return(PartialView("Quickview", viewModel));
            }

            return(Request.IsAjaxRequest() ? PartialView(viewModel) : (ActionResult)View(viewModel));
        }
Exemplo n.º 2
0
        public virtual TViewModel Create <TProduct, TVariant, TViewModel>(TProduct currentContent, string variationCode)
            where TProduct : ProductContent
            where TVariant : VariationContent
            where TViewModel : ProductViewModelBase <TProduct, TVariant>, new()
        {
            var viewModel = new TViewModel();
            var market    = _currentMarket.GetCurrentMarket();
            var currency  = _currencyservice.GetCurrentCurrency();
            var variants  = GetVariants <TVariant, TProduct>(currentContent)
                            .Where(v => v.Prices().Any(x => x.MarketId == _currentMarket.GetCurrentMarket().MarketId))
                            .ToList();
            var variantsState = GetVarantsState(variants, market);

            if (!TryGetVariant(variants, variationCode, out var variant))
            {
                return(new TViewModel
                {
                    Product = currentContent,
                    CurrentContent = currentContent,
                    Images = currentContent.GetAssets <IContentImage>(_contentLoader, _urlResolver),
                    Media = currentContent.GetAssetsWithType(_contentLoader, _urlResolver),
                    Colors = new List <SelectListItem>(),
                    Sizes = new List <SelectListItem>(),
                    StaticAssociations = new List <ProductTileViewModel>(),
                    Variants = new List <VariantViewModel>()
                });
            }

            variationCode = string.IsNullOrEmpty(variationCode) ? variants.FirstOrDefault()?.Code : variationCode;
            var isInstock        = true;
            var currentWarehouse = _warehouseRepository.GetDefaultWarehouse();

            if (!string.IsNullOrEmpty(variationCode))
            {
                var inStockQuantity = GetAvailableStockQuantity(variant, currentWarehouse);
                isInstock = inStockQuantity > 0;
                viewModel.InStockQuantity = inStockQuantity;
            }

            var defaultPrice      = PriceCalculationService.GetSalePrice(variant.Code, market.MarketId, currency);
            var subscriptionPrice = PriceCalculationService.GetSubscriptionPrice(variant.Code, market.MarketId, currency);
            var discountedPrice   = GetDiscountPrice(defaultPrice, market, currency);
            var currentStore      = _storeService.GetCurrentStoreViewModel();
            var relatedProducts   = currentContent.GetRelatedEntries().ToList();
            var associations      = relatedProducts.Any() ?
                                    _productService.GetProductTileViewModels(relatedProducts) :
                                    new List <ProductTileViewModel>();
            var contact                = PrincipalInfo.CurrentPrincipal.GetCustomerContact();
            var baseVariant            = variant as GenericVariant;
            var productRecommendations = currentContent as IProductRecommendations;
            var isSalesRep             = PrincipalInfo.CurrentPrincipal.IsInRole("SalesRep");

            viewModel.CurrentContent    = currentContent;
            viewModel.Product           = currentContent;
            viewModel.Variant           = variant;
            viewModel.ListingPrice      = defaultPrice?.UnitPrice ?? new Money(0, currency);
            viewModel.DiscountedPrice   = discountedPrice;
            viewModel.SubscriptionPrice = subscriptionPrice?.UnitPrice ?? new Money(0, currency);
            viewModel.Colors            = variants.OfType <GenericVariant>()
                                          .Where(x => x.Color != null)
                                          .GroupBy(x => x.Color)
                                          .Select(g => new SelectListItem
            {
                Selected = false,
                Text     = g.Key,
                Value    = g.Key
            }).ToList();
            viewModel.Sizes = variants.OfType <GenericVariant>()
                              .Where(x => (x.Color == null || x.Color.Equals(baseVariant?.Color, StringComparison.OrdinalIgnoreCase)) && x.Size != null)
                              .Select(x => new SelectListItem
            {
                Selected = false,
                Text     = x.Size,
                Value    = x.Size,
                Disabled = !variantsState.FirstOrDefault(v => v.Key == x.Code).Value
            }).ToList();
            viewModel.Color       = baseVariant?.Color;
            viewModel.Size        = baseVariant?.Size;
            viewModel.Images      = variant.GetAssets <IContentImage>(_contentLoader, _urlResolver);
            viewModel.Media       = variant.GetAssetsWithType(_contentLoader, _urlResolver);
            viewModel.IsAvailable = _databaseMode.DatabaseMode != DatabaseMode.ReadOnly && defaultPrice != null && isInstock;
            viewModel.Stores      = new StoreViewModel
            {
                Stores            = _storeService.GetEntryStoresViewModels(variant.Code),
                SelectedStore     = currentStore != null ? currentStore.Code : "",
                SelectedStoreName = currentStore != null ? currentStore.Name : ""
            };
            viewModel.StaticAssociations = associations;
            viewModel.Variants           = variants.Select(x =>
            {
                var variantImage        = x.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault();
                var variantDefaultPrice = GetDefaultPrice(x.Code, market, currency);
                return(new VariantViewModel
                {
                    Sku = x.Code,
                    Name = x.Name,
                    Size = x is GenericVariant ? $"{(x as GenericVariant).Color} {(x as GenericVariant).Size}" : "",
                    ImageUrl = string.IsNullOrEmpty(variantImage) ? "http://placehold.it/54x54/" : variantImage,
                    DiscountedPrice = GetDiscountPrice(variantDefaultPrice, market, currency),
                    ListingPrice = variantDefaultPrice?.UnitPrice ?? new Money(0, currency),
                    StockQuantity = _quickOrderService.GetTotalInventoryByEntry(x.Code)
                });
            }).ToList();
            viewModel.HasOrganization     = contact?.OwnerId != null;
            viewModel.ShowRecommendations = productRecommendations?.ShowRecommendations ?? true;
            viewModel.IsSalesRep          = isSalesRep;
            viewModel.SalesMaterials      = isSalesRep ? currentContent.CommerceMediaCollection.Where(x => !string.IsNullOrEmpty(x.GroupName) && x.GroupName.Equals("sales"))
                                            .Select(x => _contentLoader.Get <MediaData>(x.AssetLink)).ToList() : new List <MediaData>();
            viewModel.Documents = currentContent.CommerceMediaCollection
                                  .Where(o => o.AssetType.Equals(typeof(PdfFile).FullName.ToLowerInvariant()) || o.AssetType.Equals(typeof(StandardFile).FullName.ToLowerInvariant()))
                                  .Select(x => _contentLoader.Get <MediaData>(x.AssetLink)).ToList();
            viewModel.MinQuantity = (int)defaultPrice.MinQuantity;
            viewModel.HasSaleCode = defaultPrice != null ? !string.IsNullOrEmpty(defaultPrice.CustomerPricing.PriceCode) : false;

            return(viewModel);
        }