Esempio n. 1
0
        public IEnumerable<PromotionMatch> MatchApplicablePromotions(PricingContext context, IEnumerable<Promotion> candidatePromotions)
        {
            var matches = new List<PromotionMatch>();

            // Lower priority promotions check first
            candidatePromotions = candidatePromotions.OrderBy(x => x.Priority).ThenBy(x => x.Id);

            foreach (var promotion in candidatePromotions)
            {
                var match = TryMatchPromotion(promotion, context);
                if (match != null)
                {
                    matches.Add(match);
                }
            }

            if (matches.Count > 0)
            {
                // Higher priority check first
                matches.Reverse();
                CheckOverlappingUsage(matches);
            }

            return matches;
        }
 public List <Item> FilterInnerItems(string input)
 {
     using (var db = new PricingContext())
     {
         return(db.Items.Where(i => i.ItemName.Contains(input) && i.ItemType == 0).GroupBy(i => new { i.ChainID, i.ItemCode, i.ItemType }).Select(g => g.FirstOrDefault()).ToList());
     }
 }
        public CheckPromotionConditionResult CheckConditions(Promotion promotion, PricingContext context)
        {
            var result = new CheckPromotionConditionResult();

            if (!promotion.Conditions.Any())
            {
                result.Success = true;
                foreach (var item in context.Items)
                {
                    result.MatchedItems.Add(item);
                }

                return result;
            }

            var operators = _ruleEngine.ComparisonOperatorManager.Operators.Select(o => o.Name).ToList();

            foreach (var item in context.Items)
            {
                var contextModel = new PromotionConditionContextModel
                {
                    Item = item,
                    Customer = context.Customer
                };

                if (_ruleEngine.CheckConditions(promotion.Conditions, contextModel))
                {
                    result.Success = true;
                    result.MatchedItems.Add(item);
                }
            }

            return result;
        }
 public decimal GetPrice(long chainId, int storeId, long itemCode, int itemType)
 {
     using (var db = new PricingContext())
     {
         return(db.Items.Where(i => i.ItemCode == itemCode && i.ItemType == itemType && i.StoreID == storeId && i.ChainID == chainId).Select(i => i.Price).FirstOrDefault());
     }
 }
 public bool Login(string user, string password)
 {
     using (var db = new PricingContext())
     {
         return(db.Accounts.Any(a => a.UserID == user && a.Password == password));
     }
 }
Esempio n. 6
0
 public bool CheckStore(long chainId, int storeId)
 {
     using (var db = new PricingContext())
     {
         return(db.Stores.Any(s => s.ChainID == chainId && s.StoreID == storeId));
     }
 }
 public void ResetDb()
 {
     using (var db = new PricingContext())
     {
         if (db.Stores.Any())
         {
             var filteredNewItems = db.Items.Where(i => !db.HistoryItems.Any(hist => hist.ChainID == i.ChainID && hist.StoreID == i.StoreID && i.ItemCode == hist.ItemCode &&
                                                                             i.ItemType == hist.ItemType && DateTime.Compare(hist.LastUpdateDate, i.LastUpdateDate) >= 0)).Select(i => new
             {
                 ChainID        = i.ChainID,
                 StoreID        = i.StoreID,
                 ItemCode       = i.ItemCode,
                 ItemType       = i.ItemType,
                 LastUpdateDate = i.LastUpdateDate,
                 Price          = i.Price
             }).ToList();
             var convertedToHistItems = filteredNewItems.Select(i => new HistoryItem()
             {
                 ChainID        = i.ChainID,
                 StoreID        = i.StoreID,
                 ItemCode       = i.ItemCode,
                 ItemType       = i.ItemType,
                 LastUpdateDate = i.LastUpdateDate,
                 Price          = i.Price
             }).ToList();
             db.HistoryItems.AddRange(convertedToHistItems);
             db.Stores.RemoveRange(db.Stores);
             db.SaveChanges();
         }
     }
 }
 public List <string> GetLocations(List <long> chains)
 {
     using (var db = new PricingContext())
     {
         return(db.Stores.Where(s => chains.Contains(s.ChainID) && !string.Equals(s.Location, string.Empty)).Select(s => s.Location).Distinct().ToList());
     }
 }
Esempio n. 9
0
        private void AddLineItem_Click(object sender, RoutedEventArgs e)
        {
            var sku   = txtProductcode.Text;
            var units = 0;

            var p = productStore.GetProductBySku(sku);

            if (!int.TryParse(txtUnits.Text, out units) || p == null)
            {
                Clear();
                return;
            }

            var pricingContext = new PricingContext()
            {
                Quantity = units, UnitPrice = p.UnitPrice, Promotion = p.Promotion
            };
            var price = priceCalculator.GetCalculator(p.Promotion.PromotionType).Calculate(pricingContext);

            lineItems.Add(new LineItem()
            {
                ProductName = p.Name, Units = units, UnitPrice = p.UnitPrice, Discount = price.Description, TotalPrice = price.PromoPrice
            });

            invoice.AddLineItem(p.Name, units, p.UnitPrice, price.Description, price.PromoPrice);

            Clear();
        }
Esempio n. 10
0
 public void WriteToDb()
 {
     using (var db = new PricingContext())
     {
         db.Stores.AddRange(Stores);
         db.SaveChanges();
     }
 }
Esempio n. 11
0
 public StoreHeader GetStoreHeader(long chainId, int storeId)
 {
     using (var db = new PricingContext())
     {
         return(db.Stores.Where(s => s.ChainID == chainId && s.StoreID == storeId).Select(s => new
                                                                                          { s.ChainID, s.StoreID, s.ChainName, s.StoreName }).AsEnumerable().Select(s => new StoreHeader(s.ChainID, s.StoreID, s.ChainName, s.StoreName)).FirstOrDefault());
     }
 }
Esempio n. 12
0
 public List <StoreHeader> GetStores(List <long> chains, string location)
 {
     using (var db = new PricingContext())
     {
         return(db.Stores.Where(s => chains.Contains(s.ChainID) && (s.Location == location || location == null)).Select(s => new
                                                                                                                        { s.ChainID, s.StoreID, s.ChainName, s.StoreName }).AsEnumerable().Select(s => new StoreHeader(s.ChainID, s.StoreID, s.ChainName, s.StoreName)).Distinct().ToList());
     }
 }
 public void Execute(PricingContext context)
 {
     if (context.PaymentMethod != null)
     {
         context.PaymentMethodCost.SetOriginalValue(
             context.PaymentMethod.GetPaymentMethodCost(context.Subtotal.OriginalValue));
     }
 }
 public List <KeyValuePair <long, int> > GetStores(CartItem currItem)
 {
     using (var db = new PricingContext())
     {
         return(db.HistoryItems.Where(i => i.ItemCode == currItem.ItemCode && i.ItemType == currItem.ItemType && (currItem.ItemType != 0 || i.ChainID == currItem.ChainId)).Select(i => new { Chain = i.ChainID, Store = i.StoreID }).Distinct()
                .AsEnumerable().Select(i => new KeyValuePair <long, int>(i.Chain, i.Store)).ToList());
     }
 }
 public List <KeyValuePair <DateTime, decimal> > GetItemHistory(CartItem currItem, StoreHeader currStore)
 {
     using (var db = new PricingContext())
     {
         return(db.HistoryItems.Where(i => i.ItemCode == currItem.ItemCode && i.ItemType == currItem.ItemType && i.StoreID == currStore.StoreId && i.ChainID == currStore.ChainId).Select(i => new { Date = i.LastUpdateDate, Price = i.Price }).OrderBy(i => i.Date)
                .AsEnumerable().Select(i => new KeyValuePair <DateTime, decimal>(i.Date, i.Price)).ToList());
     }
 }
Esempio n. 16
0
 public List <KeyValuePair <long, string> > GetAllChains()
 {
     using (var db = new PricingContext())
     {
         return(db.Stores.Select(s => new { s.ChainID, s.ChainName }).Distinct().AsEnumerable().
                Select(s => new KeyValuePair <long, string>(s.ChainID, s.ChainName)).ToList());
     }
 }
Esempio n. 17
0
 public PromotionPrice Calculate(PricingContext context)
 {
     return(new PromotionPrice()
     {
         PromoPrice = context.UnitPrice * context.Quantity,
         Description = ""
     });
 }
Esempio n. 18
0
 public ItemInfo GetItemInfo(CartItem currItem)
 {
     using (var db = new PricingContext())
     {
         return(db.Items.Where(i => i.ItemCode == currItem.ItemCode && i.ItemType == currItem.ItemType && i.ChainID == currItem.ChainId)
                .Select(i => new { i.ItemCode, i.ItemName, i.ItemType, i.Store.ChainName, i.Quantity, i.UnitQuantity }).AsEnumerable()
                .Select(i => new ItemInfo(i.ItemCode, i.ItemName, i.ItemType, i.ChainName, i.UnitQuantity, i.Quantity)).FirstOrDefault());
     }
 }
Esempio n. 19
0
        public PromotionPrice Calculate(PricingContext context)
        {
            var discountPercent = ((PercentagePromotion)context.Promotion).PromotionPercentage;

            return(new PromotionPrice()
            {
                PromoPrice = (double)(((decimal)(context.UnitPrice * context.Quantity)) * (1 - (decimal)(discountPercent / 100))),
                Description = $"{discountPercent}% off"
            });
        }
Esempio n. 20
0
 public void WriteToDb()
 {
     using (var db = new PricingContext())
     {
         var distinctItems = Items.GroupBy(i => new { i.ChainID, i.StoreID, i.ItemCode, i.ItemType }).Select(g => g.FirstOrDefault());
         db.Configuration.AutoDetectChangesEnabled = false;
         db.Configuration.ValidateOnSaveEnabled    = false;
         db.Items.AddRange(distinctItems);
         db.SaveChanges();
     }
 }
Esempio n. 21
0
        public PromotionContext(
            Promotion promotion,
            IEnumerable<PricingItem> conditionMatchedItems,
            PricingContext pricingContext)
        {
            Require.NotNull(promotion, "promotion");
            Require.NotNull(pricingContext, "pricingContext");

            Promotion = promotion;
            ConditionMatchedItems = (conditionMatchedItems ?? Enumerable.Empty<PricingItem>()).ToList();
            PricingContext = pricingContext;
        }
Esempio n. 22
0
 public void Execute(PricingContext context)
 {
     if (context.ShippingMethod != null)
     {
         var provider = _factory.FindByName(context.ShippingMethod.ShippingRateProviderName);
         if (provider != null)
         {
             var shippingCost = provider.GetShippingRate(context.ShippingMethod, new ShippingRateCalculationContext(context));
             context.ShippingCost.SetOriginalValue(shippingCost);
         }
     }
 }
 public bool AddAccount(string user, string password)
 {
     using (var db = new PricingContext())
     {
         if (!db.Accounts.Any(a => a.UserID == user))
         {
             Account newAccount = new Account(user, password);
             db.Accounts.Add(newAccount);
             db.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
Esempio n. 24
0
 public List <KeyValuePair <long, int> > GetCheapestStore(List <long> chainIds, List <CartItem> itemsToCheck, string location, List <StoreHeader> markedStores, int itemsToFetch)
 {
     using (var db = new PricingContext())
     {
         List <long> itemCodes      = itemsToCheck.Select(i => i.ItemCode).ToList();
         var         filteredStores = db.Items.Where(i => (i.Store.Location == location || location == null) && chainIds.Contains(i.ChainID) && itemCodes.Contains(i.ItemCode)).ToList();
         var         filteredItems  = filteredStores.Where(i => !markedStores.Any(s => s.ChainId == i.ChainID && s.StoreId == i.StoreID) && itemsToCheck.Any(s => s.ItemCode == i.ItemCode && s.ItemType == i.ItemType && (i.ItemType != 0 || i.ChainID == s.ChainId))).Select(i => new
         {
             ChainID = i.ChainID,
             StoreID = i.StoreID,
             Price   = i.Price * itemsToCheck.Where(s => s.ItemCode == i.ItemCode && s.ItemType == i.ItemType && (i.ItemType != 0 || i.ChainID == s.ChainId)).Select(s => s.Amount).FirstOrDefault()
         }).ToList();
         var bestStore = filteredItems.GroupBy(i => new { i.ChainID, i.StoreID }).Select(g => new { ChainID = g.Key.ChainID, StoreID = g.Key.StoreID, Count = g.Count(), TotalPrice = g.Sum(g1 => g1.Price) }).OrderByDescending(i => i.Count).ThenBy(i => i.TotalPrice).Take(itemsToFetch);
         return(bestStore.Select(s => new KeyValuePair <long, int>(s.ChainID, s.StoreID)).ToList());
     }
 }
Esempio n. 25
0
        public void Execute(PricingContext context)
        {
            var matcher = new PromotionMatcher(_ruleEngine);
            var promotions = _promotionService.Query().WhereAvailableNow().ToList();

            var matches = matcher.MatchApplicablePromotions(context, promotions);

            foreach (var match in matches)
            {
                var policy = _policyFactory.FindByName(match.Promotion.PromotionPolicyName);
                if (policy == null)
                    throw new InvalidOperationException("Cannot load promotion policy with name '" + match.Promotion.PromotionPolicyName + "'. Ensure corresponding add-in has been installed.");

                policy.Execute(new PromotionContext(match.Promotion, match.ConditionMatchedItems, context));
                context.AppliedPromotions.Add(match.Promotion);
            }
        }
Esempio n. 26
0
        public PromotionPrice Calculate(PricingContext context)
        {
            var promo = ((BogoPromotion)context.Promotion);
            var buy   = promo.Buy;
            var get   = promo.Get;

            var factor = (double)buy / (buy + get);

            var pricingQty = (double)context.Quantity * factor;

            pricingQty += (pricingQty - (int)pricingQty) > 0 ? 1 : 0;

            return(new PromotionPrice()
            {
                PromoPrice = (context.UnitPrice * (int)pricingQty),
                Description = $"Buy {buy} get {get} free"
            });
        }
Esempio n. 27
0
        private CalculatePriceResult GetResult(PricingContext ctx)
        {
            var result = new CalculatePriceResult();

            foreach (var item in ctx.Items)
            {
                result.Items.Add(new CalculateItemPriceResult
                {
                    Id = item.ItemId,
                    Subtotal = item.Subtotal.ToDto()
                });
            }

            result.ShippingCost = ctx.ShippingCost.ToDto();
            result.PaymentMethodCost = ctx.PaymentMethodCost.ToDto();
            result.Tax = ctx.Tax.ToDto();
            result.Subtotal = ctx.Subtotal.ToDto();
            result.Total = ctx.Total;

            return result;
        }
Esempio n. 28
0
 public void WriteToDbPartial()
 {
     using (var db = new PricingContext())
     {
         foreach (var item in Items)
         {
             Item currItem = db.Items.Find(item.StoreID, item.ChainID, item.ItemCode, item.ItemType);
             if (currItem != null)
             {
                 HistoryItem histItem = db.HistoryItems.Find(currItem.StoreID, currItem.ChainID, currItem.ItemCode, currItem.ItemType, currItem.LastUpdateDate);
                 if (histItem == null)
                 {
                     db.HistoryItems.Add(new HistoryItem(currItem.StoreID, currItem.ChainID, currItem.ItemCode, currItem.ItemType, currItem.Price, currItem.LastUpdateDate));
                 }
                 db.Entry(currItem).CurrentValues.SetValues(item);
             }
             else
             {
                 db.Items.Add(item);
             }
         }
         db.SaveChanges();
     }
 }
Esempio n. 29
0
        private PromotionMatch TryMatchPromotion(Promotion promotion, PricingContext context)
        {
            if (promotion.RequireCouponCode && context.CouponCode != promotion.CouponCode)
            {
                return null;
            }

            var isMatch = false;
            var conditionMatchedItems = new List<PricingItem>();

            if (!promotion.Conditions.Any())
            {
                isMatch = true;
            }
            else
            {
                var conditionChecker = new PromotionConditionChecker(_ruleEngine);
                var result = conditionChecker.CheckConditions(promotion, context);
                if (result.Success)
                {
                    isMatch = true;
                    foreach (var item in result.MatchedItems)
                    {
                        if (!conditionMatchedItems.Contains(item))
                        {
                            conditionMatchedItems.Add(item);
                        }
                    }
                }
            }

            if (isMatch)
            {
                return new PromotionMatch(promotion, conditionMatchedItems);
            }

            return null;
        }
Esempio n. 30
0
 public HomeController(PricingContext context)
 {
     _context = context;
 }
Esempio n. 31
0
 public PricingController(PricingContext pricingContext, IPricingCalculator calculator, IConfiguration config)
 {
     this.pricingContext = pricingContext;
     this.calculator     = calculator;
     this.config         = config;
 }
 public PriceController(ILogger <PriceController> logger, PricingContext pricingContext)
 {
     _logger         = logger;
     _pricingContext = pricingContext;
 }
 public ShippingRateCalculationContext(PricingContext pricingContext)
 {
     PricingContext = pricingContext;
 }
Esempio n. 34
0
 void given_this_price_context(PricingContext pricingContext)
 {
     _pricingContext = pricingContext;
 }
Esempio n. 35
0
 void given_pricing_context_includes_a_zip_code()
 {
     _pricingContext = _testProduct.GetPricingContext(zipCode: "12345");
 }