public virtual TViewModel CreateVariant <TVariant, TViewModel>(TVariant currentContent)
            where TVariant : VariationContent
            where TViewModel : EntryViewModelBase <TVariant>, new()
        {
            var market            = _currentMarket.GetCurrentMarket();
            var currency          = _currencyservice.GetCurrentCurrency();
            var defaultPrice      = PriceCalculationService.GetSalePrice(currentContent.Code, market.MarketId, currency);
            var subscriptionPrice = PriceCalculationService.GetSubscriptionPrice(currentContent.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 productRecommendations = currentContent as IProductRecommendations;
            var isSalesRep             = PrincipalInfo.CurrentPrincipal.IsInRole("SalesRep");
            var currentWarehouse       = _warehouseRepository.GetDefaultWarehouse();

            var isInstock = true;

            if (currentContent.TrackInventory)
            {
                var inventoryRecord = _inventoryService.Get(currentContent.Code, currentWarehouse.Code);
                var inventory       = new Inventory(inventoryRecord);
                isInstock = inventory.IsTracked && inventory.InStockQuantity == 0 ? false : isInstock;
            }

            return(new TViewModel
            {
                CurrentContent = currentContent,
                ListingPrice = defaultPrice?.UnitPrice ?? new Money(0, currency),
                DiscountedPrice = discountedPrice,
                SubscriptionPrice = subscriptionPrice?.UnitPrice ?? new Money(0, currency),
                Images = currentContent.GetAssets <IContentImage>(_contentLoader, _urlResolver),
                IsAvailable = defaultPrice != null && isInstock,
                Stores = new StoreViewModel
                {
                    Stores = _storeService.GetEntryStoresViewModels(currentContent.Code),
                    SelectedStore = currentStore != null ? currentStore.Code : "",
                    SelectedStoreName = currentStore != null ? currentStore.Name : ""
                },
                StaticAssociations = associations,
                HasOrganization = contact?.OwnerId != null,
                ShowRecommendations = productRecommendations?.ShowRecommendations ?? true,
                IsSalesRep = isSalesRep,
                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>(),
                MinQuantity = (int)defaultPrice.MinQuantity
            });
        }
コード例 #2
0
        public void SetCartCurrency(ICart cart, Currency currency)
        {
            if (currency.IsEmpty || currency == cart.Currency)
            {
                return;
            }

            cart.Currency = currency;
            foreach (var lineItem in cart.GetAllLineItems())
            {
                // If there is an item which has no price in the new currency, a NullReference exception will be thrown.
                // Mixing currencies in cart is not allowed.
                // It's up to site's managers to ensure that all items have prices in allowed currency.
                lineItem.PlacedPrice = PriceCalculationService.GetSalePrice(lineItem.Code, cart.MarketId, currency).UnitPrice.Amount;
            }

            ValidateCart(cart);
        }
コード例 #3
0
        public virtual TViewModel CreatePackage <TPackage, TVariant, TViewModel>(TPackage currentContent)
            where TPackage : PackageContent
            where TVariant : VariationContent
            where TViewModel : PackageViewModelBase <TPackage>, new()
        {
            var variants          = GetVariants <TVariant, TPackage>(currentContent).ToList();
            var market            = _currentMarket.GetCurrentMarket();
            var currency          = _currencyservice.GetCurrentCurrency();
            var defaultPrice      = PriceCalculationService.GetSalePrice(currentContent.Code, market.MarketId, currency);
            var subscriptionPrice = PriceCalculationService.GetSubscriptionPrice(currentContent.Code, market.MarketId, 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 productRecommendations = currentContent as IProductRecommendations;
            var isSalesRep             = PrincipalInfo.CurrentPrincipal.IsInRole("SalesRep");

            return(new TViewModel
            {
                CurrentContent = currentContent,
                Package = currentContent,
                ListingPrice = defaultPrice?.UnitPrice ?? new Money(0, currency),
                DiscountedPrice = GetDiscountPrice(defaultPrice, market, currency),
                SubscriptionPrice = subscriptionPrice?.UnitPrice ?? new Money(0, currency),
                Images = currentContent.GetAssets <IContentImage>(_contentLoader, _urlResolver),
                IsAvailable = defaultPrice != null,
                Entries = variants,
                //Reviews = GetReviews(currentContent.Code),
                Stores = new StoreViewModel
                {
                    Stores = _storeService.GetEntryStoresViewModels(currentContent.Code),
                    SelectedStore = currentStore != null ? currentStore.Code : "",
                    SelectedStoreName = currentStore != null ? currentStore.Name : ""
                },
                StaticAssociations = associations,
                HasOrganization = contact?.OwnerId != null,
                ShowRecommendations = productRecommendations?.ShowRecommendations ?? true,
                IsSalesRep = isSalesRep,
                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>()
            });
        }
コード例 #4
0
        private ProductTileViewModel CreateProductViewModelForEntry(EntryContentBase entry)
        {
            var   market        = _currentMarketService.GetCurrentMarket();
            var   currency      = _currencyService.GetCurrentCurrency();
            var   originalPrice = PriceCalculationService.GetSalePrice(entry.Code, market.MarketId, market.DefaultCurrency);
            Money?discountedPrice;

            if (originalPrice?.UnitPrice == null || originalPrice.UnitPrice.Amount == 0)
            {
                originalPrice = new PriceValue()
                {
                    UnitPrice = new Money(0, market.DefaultCurrency)
                };
                discountedPrice = null;
            }
            else
            {
                discountedPrice = GetDiscountPrice(entry, market, currency, originalPrice.UnitPrice);
            }

            var image        = entry.GetAssets <IContentImage>(_contentLoader, _urlResolver).FirstOrDefault() ?? "";
            var currentStore = _storeService.GetCurrentStoreViewModel();

            return(new ProductTileViewModel
            {
                Code = entry.Code,
                DisplayName = entry.DisplayName,
                PlacedPrice = originalPrice.UnitPrice,
                DiscountedPrice = discountedPrice,
                ImageUrl = image,
                Url = entry.GetUrl(),
                IsAvailable = originalPrice.UnitPrice != null && originalPrice.UnitPrice.Amount > 0,
                Stores = new StoreViewModel
                {
                    Stores = _storeService.GetEntryStoresViewModels(entry.Code),
                    SelectedStore = currentStore != null ? currentStore.Code : "",
                    SelectedStoreName = currentStore != null ? currentStore.Name : ""
                }
            });
        }
        public override IOrderGroup CreateInMemoryOrderGroup(
            ContentReference entryLink,
            IMarket market,
            Currency marketCurrency)
        {
            InMemoryOrderGroup memoryOrderGroup = new InMemoryOrderGroup(market, marketCurrency);

            memoryOrderGroup.CustomerId = PrincipalInfo.CurrentPrincipal.GetContactId();
            string      code  = _referenceConverter.GetCode(entryLink);
            IPriceValue price = PriceCalculationService.GetSalePrice(code, market.MarketId, marketCurrency);

            if (price != null && price.UnitPrice != null)
            {
                decimal priceAmount = price.UnitPrice.Amount;
                memoryOrderGroup.Forms.First().Shipments.First().LineItems.Add(new InMemoryLineItem()
                {
                    Quantity    = 1M,
                    Code        = code,
                    PlacedPrice = priceAmount
                });
            }

            return(memoryOrderGroup);
        }
コード例 #6
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);
        }
コード例 #7
0
        public static ProductTileViewModel GetProductTileViewModel(this EntryContentBase entry, IMarket market, Currency currency, bool isFeaturedProduct = false)
        {
            var entryRecommendations = entry as IProductRecommendations;
            var product   = entry;
            var entryUrl  = "";
            var firstCode = "";
            var type      = typeof(GenericProduct);

            if (entry is GenericProduct)
            {
                var variants = GetProductVariants(entry);
                if (variants != null && variants.Any())
                {
                    firstCode = variants.First().Code;
                }
                entryUrl = UrlResolver.Value.GetUrl(entry.ContentLink);
            }

            if (entry is GenericBundle)
            {
                type      = typeof(GenericBundle);
                firstCode = product.Code;
                entryUrl  = UrlResolver.Value.GetUrl(product.ContentLink);
            }

            if (entry is GenericPackage)
            {
                type      = typeof(GenericPackage);
                firstCode = product.Code;
                entryUrl  = UrlResolver.Value.GetUrl(product.ContentLink);
            }

            if (entry is GenericVariant)
            {
                var variantEntry = entry as GenericVariant;
                type      = typeof(GenericVariant);
                firstCode = entry.Code;
                var parentLink = entry.GetParentProducts().FirstOrDefault();
                if (ContentReference.IsNullOrEmpty(parentLink))
                {
                    product  = ContentLoader.Value.Get <EntryContentBase>(variantEntry.ContentLink);
                    entryUrl = UrlResolver.Value.GetUrl(variantEntry.ContentLink);
                }
                else
                {
                    product  = ContentLoader.Value.Get <EntryContentBase>(parentLink) as GenericProduct;
                    entryUrl = UrlResolver.Value.GetUrl(product.ContentLink) + "?variationCode=" + variantEntry.Code;
                }
            }

            IPriceValue price = PriceCalculationService.GetSalePrice(firstCode, market.MarketId, currency);

            if (price == null)
            {
                price = GetEmptyPrice(entry, market, currency);
            }
            IPriceValue discountPrice = price;

            if (price.UnitPrice.Amount > 0 && !string.IsNullOrEmpty(firstCode))
            {
                discountPrice = PromotionService.Value.GetDiscountPrice(new CatalogKey(firstCode), market.MarketId, currency);
            }

            bool isAvailable = price.UnitPrice.Amount > 0;

            return(new ProductTileViewModel
            {
                ProductId = product.ContentLink.ID,
                Brand = entry.Property.Keys.Contains("Brand") ? entry.Property["Brand"]?.Value?.ToString() ?? "" : "",
                Code = product.Code,
                DisplayName = entry.DisplayName,
                Description = entry.Property.Keys.Contains("Description") ? entry.Property["Description"]?.Value != null ? ((XhtmlString)entry.Property["Description"].Value).ToHtmlString() : "" : "",
                LongDescription = ShortenLongDescription(entry.Property.Keys.Contains("LongDescription") ? entry.Property["LongDescription"]?.Value != null ? ((XhtmlString)entry.Property["LongDescription"].Value).ToHtmlString() : "" : ""),
                PlacedPrice = price.UnitPrice,
                DiscountedPrice = discountPrice.UnitPrice,
                FirstVariationCode = firstCode,
                ImageUrl = AssetUrlResolver.Value.GetAssetUrl <IContentImage>(entry),
                VideoAssetUrl = AssetUrlResolver.Value.GetAssetUrl <IContentVideo>(entry),
                Url = entryUrl,
                IsAvailable = isAvailable,
                OnSale = entry.Property.Keys.Contains("OnSale") && ((bool?)entry.Property["OnSale"]?.Value ?? false),
                NewArrival = entry.Property.Keys.Contains("NewArrival") && ((bool?)entry.Property["NewArrival"]?.Value ?? false),
                ShowRecommendations = entryRecommendations != null ? entryRecommendations.ShowRecommendations : true,
                EntryType = type,
                ProductStatus = entry.Property.Keys.Contains("ProductStatus") ? entry.Property["ProductStatus"]?.Value?.ToString() ?? "Active" : "Active",
                Created = entry.Created,
                IsFeaturedProduct = isFeaturedProduct
            });
        }
コード例 #8
0
        public virtual TViewModel CreatePackage <TPackage, TVariant, TViewModel>(TPackage currentContent)
            where TPackage : PackageContent
            where TVariant : VariationContent
            where TViewModel : PackageViewModelBase <TPackage>, new()
        {
            var variants          = GetVariants <TVariant, TPackage>(currentContent).Where(x => x.Prices().Where(p => p.UnitPrice > 0).Any()).ToList();
            var market            = _currentMarket.GetCurrentMarket();
            var currency          = _currencyservice.GetCurrentCurrency();
            var defaultPrice      = PriceCalculationService.GetSalePrice(currentContent.Code, market.MarketId, currency);
            var subscriptionPrice = PriceCalculationService.GetSubscriptionPrice(currentContent.Code, market.MarketId, 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 productRecommendations = currentContent as IProductRecommendations;
            var isSalesRep             = PrincipalInfo.CurrentPrincipal.IsInRole("SalesRep");

            var currentWarehouse = _warehouseRepository.GetDefaultWarehouse();
            var isInstock        = true;

            if (variants != null && variants.Count > 0)
            {
                foreach (var v in variants)
                {
                    if (v.TrackInventory)
                    {
                        var inventoryRecord = _inventoryService.Get(v.Code, currentWarehouse.Code);
                        var inventory       = new Inventory(inventoryRecord);
                        isInstock = inventory.IsTracked && inventory.InStockQuantity == 0 ? false : isInstock;
                    }
                }
            }
            else
            {
                isInstock = false;
            }

            var viewModel = new TViewModel();

            viewModel.CurrentContent    = currentContent;
            viewModel.Package           = currentContent;
            viewModel.ListingPrice      = defaultPrice?.UnitPrice ?? new Money(0, currency);
            viewModel.DiscountedPrice   = GetDiscountPrice(defaultPrice, market, currency);
            viewModel.SubscriptionPrice = subscriptionPrice?.UnitPrice ?? new Money(0, currency);
            viewModel.Images            = currentContent.GetAssets <IContentImage>(_contentLoader, _urlResolver);
            viewModel.IsAvailable       = _databaseMode.DatabaseMode != DatabaseMode.ReadOnly && defaultPrice != null && isInstock;
            viewModel.Entries           = variants;
            //Reviews = GetReviews(currentContent.Code);
            viewModel.Stores = new StoreViewModel
            {
                Stores            = _storeService.GetEntryStoresViewModels(currentContent.Code),
                SelectedStore     = currentStore != null ? currentStore.Code : "",
                SelectedStoreName = currentStore != null ? currentStore.Name : ""
            };
            viewModel.StaticAssociations  = associations;
            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.MinQuantity = defaultPrice != null ? (int)defaultPrice.MinQuantity : 0;
            viewModel.HasSaleCode = defaultPrice != null ? !string.IsNullOrEmpty(defaultPrice.CustomerPricing.PriceCode) : false;

            return(viewModel);
        }