public static Interval CalculatePriceUpdates(List<ClientDrinkOrder> allOrdersItems, Interval[] intervals, int idCurrentInterval, bool predict, BeursfuifViewModelBase viewmodel) { viewmodel.PointInCode("PriceCalculation: CalculatePriceUpdates"); Interval currentInterval = intervals.FirstOrDefault(x => x.Id == idCurrentInterval); if (currentInterval == null) throw new Exception("Current interval isn't part of the Interval array"); int currentIntervalIndex = Array.IndexOf(intervals, intervals.FirstOrDefault(x => x.Id == idCurrentInterval)); //the first to intervals don't trigger an update if (currentIntervalIndex == 0) return intervals[1]; Interval previousInterval = intervals[currentIntervalIndex - 1]; //no new update possible, end of beursfuif if (currentIntervalIndex == intervals.Length - 1) return null; Interval nextInterval = (predict ? intervals[currentIntervalIndex + 1].Clone() : intervals[currentIntervalIndex + 1]); int previousAllDrinkCount = allOrdersItems.Where(x => x.IntervalId == previousInterval.Id).Sum(x => x.Count); viewmodel.SendLogMessage("Previous drink count: " + previousAllDrinkCount, LogType.SETTINGS_VM); int currentAllDrinkCount = allOrdersItems.Where(x => x.IntervalId == currentInterval.Id).Sum(x => x.Count); viewmodel.SendLogMessage("Current drink count: " + previousAllDrinkCount, LogType.SETTINGS_VM); //2 in the excel file int differenceAllDrinks = currentAllDrinkCount - previousAllDrinkCount; viewmodel.SendLogMessage("Current all drinkcount - Previous all drinkcount", LogType.SETTINGS_VM); Drink[] drinksForNextInterval = currentInterval.Drinks.Where(x => x.Available).ToArray(); int numberOfDrinks = drinksForNextInterval.Length; for (int i = 0; i < numberOfDrinks; i++) { CalculateNewPrice(allOrdersItems, predict, currentInterval, previousInterval, nextInterval, previousAllDrinkCount, currentAllDrinkCount, differenceAllDrinks, drinksForNextInterval, i); } return nextInterval; }
private static void CalculateNewPrice(List<ClientDrinkOrder> allOrdersItems, bool predict, Interval currentInterval, Interval previousInterval, Interval nextInterval, int previousAllDrinkCount, int currentAllDrinkCount, int differenceAllDrinks, Drink[] drinksForNextInterval, int i) { Drink drink = drinksForNextInterval[i]; int previousDrinkCount = allOrdersItems.Where(x => x.DrinkId == drink.Id && x.IntervalId == previousInterval.Id).Sum(x => x.Count); int currentDrinkCount = allOrdersItems.Where(x => x.DrinkId == drink.Id && x.IntervalId == currentInterval.Id).Sum(x => x.Count); //1 in the excel int differenceDrinkCount = currentDrinkCount - previousDrinkCount; //3 in the excel double differenceProcentage = ((double)currentDrinkCount / (double)currentAllDrinkCount) - ((double)previousDrinkCount / (double)previousAllDrinkCount); if (differenceDrinkCount >= 0) { #region 1A //the drink has been drank more if (differenceAllDrinks >= 0) { #region 2AA //more drinks have been drunk in general if (differenceProcentage > 0) { //the drink has been drunk more procentually drink.PriceFactor = PriceFactor.BigRise; } else { //the drink has been drunk less procentually drink.PriceFactor = PriceFactor.BigDecrease; } #endregion } else { #region 2AB //less drinks have been drunk in general if (differenceProcentage > 0) { //the drink has been drunk more procentually drink.PriceFactor = PriceFactor.SmallRise; } else { //the drink has been drunk less procentually drink.PriceFactor = PriceFactor.SmallDecrease; } #endregion } #endregion } else { #region 1B //the drink has been drank less if (differenceAllDrinks > 0) { #region 2BA //more drinks have been drunk in general if (differenceProcentage > 0) { //the drink has been drunk more procentually drink.PriceFactor = PriceFactor.BigDecrease; } else { //the drink has been drunk less procentually drink.PriceFactor = PriceFactor.BigRise; } #endregion } else { #region 2BB //more drinks have been drunk in general if (differenceProcentage > 0) { //the drink has been drunk more procentually drink.PriceFactor = PriceFactor.SmallDecrease; } else { //the drink has been drunk less procentually drink.PriceFactor = PriceFactor.SmallRise; } #endregion } #endregion } //check if we need to use the override factor if (drink.OverrideFactor != 0 && !predict) drink.PriceFactor = PriceFactor.Override; Drink nextDrink = nextInterval.Drinks.FirstOrDefault(x => x.Id == drink.Id); double priceFactor = drink.GetPriceFactorValue(); double priceWithoutRouding = drink.CurrentPrice * priceFactor; int nextPrice = (int)Math.Round(priceWithoutRouding); if (nextPrice > nextDrink.MaximumPrice) nextPrice = nextDrink.MaximumPrice; if (nextPrice < nextDrink.MiniumPrice) nextPrice = nextDrink.MiniumPrice; nextDrink.CurrentPrice = nextPrice; if (predict) nextDrink.PriceFactor = drink.PriceFactor; }
public IntervalViewModel(IBeursfuifData beursfuifData) : base(beursfuifData) { if (IsInDesignMode) { #region DummyData Intervals = new Interval[]{ new Interval(){ Id = 1, StartTime = new DateTime(1970,1,1,21,0,0), EndTime = new DateTime(1970,1,1,21,15,0) }, new Interval(){ Id = 2, StartTime = new DateTime(1970,1,1,21,15,0), EndTime = new DateTime(1970,1,1,21,30,0) }, new Interval(){ Id = 3, StartTime = new DateTime(1970,1,1,21,30,0), EndTime = new DateTime(1970,1,1,21,45,0) }, new Interval(){ Id = 4, StartTime = new DateTime(1970,1,1,21,45,0), EndTime = new DateTime(1970,1,1,22,00,0) } }; #endregion } else { PointInCode("IntervalViewModel: Ctor"); _beursfuifData = beursfuifData; if (Intervals != null && Intervals.Any()) { BeginTime = Intervals[0].StartTime; EndTime = Intervals[Intervals.Length - 1].EndTime; ChosenInterval = IntervalChoices.FirstOrDefault(x => x.TotalMinutes == Intervals[0].Duration.TotalMinutes); SendLogMessage("Intervals found in data folder", LogType.INTERVAL_VM); } InitCommands(); CheckCanEdit(); _beursfuifData.BeursfuifBusyChanged += BeursfuifData_BeursfuifBusyChanged; _beursfuifData.DataReset += BeursfuifData_DataReset; _beursfuifData.BeursfuifDataImported += BeursfuifData_BeursfuifDataImported; } }
//Because c# is always byRef on objects, that's why private void FillInDrinks(Interval interval, Drink[] drinks) { PointInCode("SettingsViewModel: FillInDrinks"); int length = drinks.Length; interval.Drinks = new Drink[length]; for (int i = 0; i < length; i++) { interval.Drinks[i] = drinks[i].Clone(); } }
private void CreateIntervals(object state) { PointInCode("IntervalViewModel: CreateIntervals"); int numberOfIntervals = Convert.ToInt32(state); Intervals = new Interval[numberOfIntervals]; for (int i = 0; i < numberOfIntervals; i++) { Intervals[i] = new Interval() { StartTime = BeginTime.AddMinutes(i * ChosenInterval.TotalMinutes), EndTime = BeginTime.AddMinutes((i+1) * ChosenInterval.TotalMinutes), Id = i + 1 }; } SendLogMessage("New intervals created", LogType.INTERVAL_VM); SaveIntervals(); }