Ejemplo n.º 1
0
        public IList <IPriceValue> GetDiscountPriceList(IEnumerable <CatalogKey> catalogKeys, MarketId marketId, Currency currency)
        {
            var market = _marketService.GetMarket(marketId);

            if (market == null)
            {
                throw new ArgumentException(string.Format("market '{0}' does not exist", marketId));
            }

            var prices = catalogKeys.Select(x => PriceCalculationService.GetSalePrice(x.CatalogEntryCode, marketId, currency)).Where(x => x != null);

            return(GetDiscountPrices(prices.ToList(), market, currency));
        }
Ejemplo n.º 2
0
        private IEnumerable <DiscountedEntry> GetDiscountPrices(
            IEnumerable <ContentReference> entryLinks,
            IMarket market,
            Mediachase.Commerce.Currency marketCurrency,
            Mediachase.Commerce.Catalog.ReferenceConverter referenceConverter)
        {
            if (entryLinks is null || (market is null) || marketCurrency.IsEmpty)
            {
                throw new ArgumentNullException(nameof(marketCurrency));
            }

            List <DiscountedEntry>       source     = new List <DiscountedEntry>();
            HashSet <string>             entryCodes = new HashSet <string>(entryLinks.Select <ContentReference, string>(new Func <ContentReference, string>(referenceConverter.GetCode)));
            Dictionary <string, decimal> dictionary = new Dictionary <string, decimal>();

            foreach (RewardDescription rewardDescription in _promotionEngine.Evaluate(entryLinks, market, marketCurrency, RequestFulfillmentStatus.Fulfilled))
            {
                HashSet <string> usedCodes = new HashSet <string>();
                foreach (ILineItem lineItem in rewardDescription.Redemptions.Where <RedemptionDescription>((Func <RedemptionDescription, bool>)(x => x.AffectedEntries != null)).SelectMany <RedemptionDescription, PriceEntry>((Func <RedemptionDescription, IEnumerable <PriceEntry> >)(x => x.AffectedEntries.PriceEntries)).Where <PriceEntry>((Func <PriceEntry, bool>)(x => x != null)).Select <PriceEntry, ILineItem>((Func <PriceEntry, ILineItem>)(x => x.ParentItem)).Where <ILineItem>((Func <ILineItem, bool>)(x => !usedCodes.Contains(x.Code))).Where <ILineItem>((Func <ILineItem, bool>)(x => entryCodes.Contains(x.Code))))
                {
                    usedCodes.Add(lineItem.Code);
                    ContentReference entryLink       = referenceConverter.GetContentLink(lineItem.Code);
                    DiscountedEntry  discountedEntry = source.SingleOrDefault <DiscountedEntry>((Func <DiscountedEntry, bool>)(x => x.EntryLink == entryLink));
                    if (discountedEntry == null)
                    {
                        discountedEntry = new DiscountedEntry(entryLink, (IList <DiscountPrice>) new List <DiscountPrice>());
                        source.Add(discountedEntry);
                    }
                    if (dictionary.ContainsKey(lineItem.Code))
                    {
                        dictionary[lineItem.Code] -= rewardDescription.SavedAmount;
                    }
                    else
                    {
                        // lineItemCalculator.GetExtendedPrice(lineItem, marketCurrency).Amount;
                        decimal amount = PriceCalculationService.GetSalePrice(lineItem.Code, market.MarketId, marketCurrency).UnitPrice.Amount;
                        dictionary.Add(lineItem.Code, amount - rewardDescription.SavedAmount);
                    }
                    DiscountPrice discountPrice = new DiscountPrice((EntryPromotion)rewardDescription.Promotion, new Money(Math.Max(dictionary[lineItem.Code], 0M), marketCurrency), new Money(lineItem.PlacedPrice, marketCurrency));
                    discountedEntry.DiscountPrices.Add(discountPrice);
                }
            }
            return((IEnumerable <DiscountedEntry>)source);
        }
Ejemplo n.º 3
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 : ""
                }
            });
        }
Ejemplo n.º 4
0
        public override IOrderGroup CreateInMemoryOrderGroup(
            ContentReference entryLink,
            IMarket market,
            Mediachase.Commerce.Currency marketCurrency)
        {
            InMemoryOrderGroup memoryOrderGroup = new InMemoryOrderGroup(market, marketCurrency);

            memoryOrderGroup.CustomerId = PrincipalInfo.CurrentPrincipal.GetContactId();
            string      code  = this._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 <IOrderForm>().Shipments.First <IShipment>().LineItems.Add((ILineItem) new InMemoryLineItem()
                {
                    Quantity    = 1M,
                    Code        = code,
                    PlacedPrice = priceAmount
                });
            }

            return((IOrderGroup)memoryOrderGroup);
        }