public void UpdatePrice(PriceUpdate updateRequest) { if (updateRequest.Price == CurrentPrice) { return; } PriceHistory.Add(updateRequest); MonthlyPrices.Add(updateRequest); HistoricalPrices.Last().Price = PriceHistory.Where(pu => pu.Date.Year == updateRequest.Date.Year).Average(value => value.Price); YearlyPrices.ElementAt(updateRequest.Date.Month - 1).Price = PriceHistory.Where(pu => pu.Date.Year == updateRequest.Date.Year && pu.Date.Month == updateRequest.Date.Month).Average(value => value.Price); CurrentPrice = updateRequest.Price; }
public void FillPriceHistory() { m_allPriceUpdates = new Dictionary <DateTime, double>(); MonthlyPrices.Clear(); YearlyPrices.Clear(); HistoricalPrices.Clear(); CurrentPrice = PriceHistory.Last().Price; foreach (PriceUpdate update in PriceHistory) { m_allPriceUpdates.Add(update.Date, update.Price); } if (m_allPriceUpdates != null && m_allPriceUpdates.Count != 0) { List <int> years = new List <int>(); DateTime now = DateTime.Now; foreach (DateTime newDate in m_allPriceUpdates.Keys) { if (!years.Contains(newDate.Year)) { // fill all yearly avg prices years.Add(newDate.Year); if (years.Count == 1) { double yearAvgPrice = m_allPriceUpdates.Where(date => date.Key.Year == newDate.Year).Average(value => value.Value); HistoricalPrices.Add(new PriceUpdate(new DateTime(newDate.Year, 1, 1), yearAvgPrice, "test")); } if (years.Count() >= 2) { // year is increment by 1 double oldPriceLastYear = m_allPriceUpdates.Where(date => date.Key.Year == years.ElementAt(years.Count - 2)).Last().Value; Dictionary <DateTime, double> copyAll = new Dictionary <DateTime, double>(m_allPriceUpdates); copyAll.Add(new DateTime(newDate.Year, 1, 1), oldPriceLastYear); double yearAvgPrice = copyAll.Where(date => date.Key.Year == newDate.Year).Average(value => value.Value); HistoricalPrices.Add(new PriceUpdate(new DateTime(newDate.Year, 1, 1), yearAvgPrice, "test")); // if there is no update for a year int yearDif = newDate.Year - years.ElementAt(years.Count - 2); if (yearDif > 1) { double oldPrice = m_allPriceUpdates.Where(date => date.Key.Year == years.ElementAt(years.Count - 2)).Last().Value; for (int i = years.ElementAt(years.Count - 2) + 1; i < newDate.Year; ++i) { HistoricalPrices.Add(new PriceUpdate(new DateTime(i, 1, 1), oldPrice, "test")); } } } // fill all months average prices for current year if (now.Year == newDate.Year) { double monthAvg = 0.0; for (int i = 1; i <= now.Month; ++i) { Dictionary <DateTime, double> copyAll = new Dictionary <DateTime, double>(m_allPriceUpdates); if (i == 1) { double oldPriceLastYear = m_allPriceUpdates.Where(date => date.Key.Year == years.ElementAt(years.Count - 2)).Last().Value; copyAll.Add(new DateTime(newDate.Year, 1, 1), oldPriceLastYear); } else { double oldPriceLastMonth = m_allPriceUpdates.Where(date => date.Key.Year == now.Year && date.Key.Month == i - 1).Last().Value; copyAll.Add(new DateTime(newDate.Year, i, 1), oldPriceLastMonth); } var temp = copyAll.Where(date => date.Key.Year == newDate.Year && date.Key.Month == i); if (temp.Count() > 0) { monthAvg = temp.Average(value => value.Value); YearlyPrices.Add(new PriceUpdate(new DateTime(newDate.Year, i, 1), monthAvg, "test")); } else { var lastChanges = m_allPriceUpdates.Where(date => date.Key.Year == newDate.Year && date.Key.Month < i); if (lastChanges.Count() > 0) { YearlyPrices.Add(new PriceUpdate(new DateTime(newDate.Year, i, 1), lastChanges.Last().Value, "test")); } } } // for days in current month var lastMonthChanges = m_allPriceUpdates.Where(date => date.Key.Year == now.Year && date.Key.Month < now.Month); if (lastMonthChanges.Count() > 0) { MonthlyPrices.Add(new PriceUpdate(new DateTime(now.Year, now.Month, 1), lastMonthChanges.Last().Value, "test")); } var currentMonthPrices = m_allPriceUpdates.Where(date => date.Key.Year == now.Year && date.Key.Month == now.Month); if (currentMonthPrices.Count() > 0) { foreach (KeyValuePair <DateTime, double> pair in currentMonthPrices) { MonthlyPrices.Add(new PriceUpdate(new DateTime(pair.Key.Year, pair.Key.Month, pair.Key.Day), pair.Value, "test")); } } if (currentMonthPrices.Count() > 0) { MonthlyPrices.Add(new PriceUpdate(new DateTime(now.Year, now.Month, now.Day), currentMonthPrices.Last().Value, "test")); } } } } } }
public ActionResult AddMonthlyPrices(MonthlyPricesViewModel mpVM) { if (!Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } else { if (ModelState.IsValid) { //send a request on the main server and await for the response DTOHelper dtoHlp = new DTOHelper(); MyRemoteServices.AgentEndpointPortClient aepc = new MyRemoteServices.AgentEndpointPortClient(); MyRemoteServices.manageMonthlyPricesRequest ampRequest = dtoHlp.GetMonthlyPricesRequest(mpVM); MyRemoteServices.manageMonthlyPricesResponse ampResponse = aepc.manageMonthlyPrices(ampRequest); bool isUpdate = false; if (ampResponse.responseWrapper.sucess) { //save localy using (var ctx = new ApplicationDbContext()) { //see if the monthly price sheet already exists var queryData = ctx.MonthlyPrices .Include("BookingUnit") .Where(x => x.BookingUnit.Id == mpVM.BookingUnitId && x.Year == mpVM.Year) .OrderBy(x => x.Month) .ToList(); if (queryData.Count == 0) { //if not, add a new one BookingUnit myUnit = ctx.BookingUnits.FirstOrDefault(x => x.Id == mpVM.BookingUnitId); //help structure for monthly prices double[] myMonths = new double[12]; myMonths[0] = mpVM.JanuaryPrice; myMonths[1] = mpVM.FebruaryPrice; myMonths[2] = mpVM.MarchPrice; myMonths[3] = mpVM.AprilPrice; myMonths[4] = mpVM.MayPrice; myMonths[5] = mpVM.JunePrice; myMonths[6] = mpVM.JulyPrice; myMonths[7] = mpVM.AugustPrice; myMonths[8] = mpVM.SeptemberPrice; myMonths[9] = mpVM.OctoberPrice; myMonths[10] = mpVM.NovemberPrice; myMonths[11] = mpVM.DecemberPrice; //long[] myMainServedIds = (long[])ampResponse.responseWrapper.mainServerId; for (int i = 0; i < 12; i++) { MonthlyPrices newMp = new MonthlyPrices { Year = mpVM.Year, Month = i + 1, BookingUnit = myUnit, Amount = myMonths[i], MainServerId = 1 }; ctx.MonthlyPrices.Add(newMp); } } else { //if yes, edit the existing one foreach (var m in queryData) { if (m.Month == 1) { m.Amount = mpVM.JanuaryPrice; } if (m.Month == 2) { m.Amount = mpVM.FebruaryPrice; } if (m.Month == 3) { m.Amount = mpVM.MarchPrice; } if (m.Month == 4) { m.Amount = mpVM.AprilPrice; } if (m.Month == 5) { m.Amount = mpVM.MayPrice; } if (m.Month == 6) { m.Amount = mpVM.JunePrice; } if (m.Month == 7) { m.Amount = mpVM.JulyPrice; } if (m.Month == 8) { m.Amount = mpVM.AugustPrice; } if (m.Month == 9) { m.Amount = mpVM.SeptemberPrice; } if (m.Month == 10) { m.Amount = mpVM.OctoberPrice; } if (m.Month == 11) { m.Amount = mpVM.NovemberPrice; } if (m.Month == 12) { m.Amount = mpVM.DecemberPrice; } } isUpdate = true; } //save changes ctx.SaveChanges(); } } else { //some error happened, retry TempData["error"] = ampResponse.responseWrapper.message; return(RedirectToAction("ManageMonhtlyPrices", "Agent", new { bookingUnitId = mpVM.BookingUnitId })); } TempData["success"] = "Successfully ADDED a new monthly prices sheet for the year " + mpVM.Year; if (isUpdate) { TempData["success"] = "Successfully UPDATED the monthly prices sheet for the year " + mpVM.Year; } return(RedirectToAction("AgentPage", "Agent")); } else { //invalid VM exception TempData["error"] = "Some form atributes are incorrect"; return(RedirectToAction("ManageMonhtlyPrices", "Agent", new { bookingUnitId = mpVM.BookingUnitId })); } } }