private List <ChartItemViewModel> GetTeamChartData(List <TeamListViewModel> teams) { var chartItems = new List <ChartItemViewModel>(); foreach (var team in teams) { if (team.averageRating != -9999) { ChartItemViewModel chartItem = new ChartItemViewModel(); chartItem.baseRating = team.averageRating; chartItem.rating = team.averageRating; chartItem.seasonYear = team.seasonYear; chartItem.seasonPeriod = team.seasonPeriod; if (team.pouleCategory == 1) { if (team.regionName.Equals("Landelijk Dames")) { chartItem.color = "magenta"; } else { chartItem.color = "blue"; } } else { if (team.regionName.Equals("Landelijk Meisjes")) { chartItem.color = "red"; } else { chartItem.color = "green"; } } chartItems.Add(chartItem); } } return(chartItems.OrderBy(c => c.seasonYear).ThenBy(c => c.seasonPeriod).ThenByDescending(c => c.pouleCategory).ToList()); }
private List <ChartItemViewModel> GetPlayerChartData(List <ResultListViewModel> results) { var chartItems = new List <ChartItemViewModel>(); foreach (var result in results) { if (result.rating != -9999) { ChartItemViewModel chartItem = new ChartItemViewModel(); chartItem.baseRating = result.baseRating; chartItem.rating = result.rating; chartItem.seasonYear = result.seasonYear; chartItem.seasonPeriod = result.seasonPeriod; chartItem.pouleCategory = result.pouleCategory; chartItem.regionId = result.regionId; if (result.pouleCategory == 1) { if (result.regionName.Equals("Landelijk Dames")) { chartItem.color = "magenta"; } else { chartItem.color = "blue"; } } else { if (result.regionName.Equals("Landelijk Meisjes")) { chartItem.color = "red"; } else { chartItem.color = "green"; } } chartItems.Add(chartItem); } } List <Season> seasons = Database.Instance.seasonRepository.GetSeasons(); chartItems = chartItems.OrderBy(c => c.seasonYear).ThenBy(c => c.seasonPeriod).ThenByDescending(c => c.pouleCategory).ThenBy(c => c.regionId).ToList(); var addPrognosis = true; if ((chartItems.Count(s => s.seasonYear == seasons[0].year && s.seasonPeriod == seasons[0].period) == 0) && (chartItems.Count(s => s.seasonYear == seasons[1].year && s.seasonPeriod == seasons[1].period) == 0)) { addPrognosis = false; // No recent results } else { var prognosisItems = chartItems.GroupBy(c => new { c.seasonYear, c.seasonPeriod }).Select(g => new { Average = g.Average(p => p.rating) }).ToList(); if (prognosisItems.Count() <= 2) { addPrognosis = false; // Not enough results to calculate prognosis } } if (addPrognosis) { int prognose = GetPrognosis(chartItems); ChartItemViewModel chartItem = new ChartItemViewModel(); chartItem.baseRating = prognose; chartItem.rating = prognose; chartItem.seasonYear = 1; chartItem.seasonPeriod = 1; chartItem.color = "black"; chartItems.Add(chartItem); } return(chartItems); }
public SevenDaysDataAnswer GetLastSevenDaysData(IUnitOfWork unitOfWork) { decimal total_sales = 0; int total_clients = 0; int different_dates = 0; decimal spendingByClient = 0; decimal spendingDeviation = 0; DateTime sevenDaysDate = unitOfWork.OrderRepository.GetLastSaleDate().Add(TimeSpan.FromDays(-6)); List <ChartItemViewModel> columnsData = new List <ChartItemViewModel>(); List <ReportLineViewModel> topProducts = new List <ReportLineViewModel>(); //last 7 days //get total sales and clients from Orders->Sales var daysQuery = from item in unitOfWork.OrderRepository.Get(s => s.Date >= sevenDaysDate) //where item.Date >= sevenDaysDate group item by item.Date into dategroup orderby dategroup.Key select dategroup; foreach (var group in daysQuery) { ChartItemViewModel scc = new ChartItemViewModel(); scc.Date = group.Key; scc.X = group.Key.ToString("d/MMM");; total_sales += scc.TotalSale = Math.Round(group.Sum(x => x.Total)); total_clients += scc.Clients = group.Sum(x => x.Persons); //total_orders += group.Count(); different_dates++; columnsData.Add(scc); } //get cost from lineitems var lineitemsQuery = from li in unitOfWork.LineItemRepository.Get(x => x.Order.Date >= sevenDaysDate) //where li.Order.Date >= sevenDaysDate group li by li.Order.Date; var invSVC = ServiceContainer.GetService <IInventoryService>(); foreach (var group in lineitemsQuery) { decimal cost = group.Sum(x => invSVC.GetProductCost(x.Product, x.Quantity, x.UnitMeasure)); ChartItemViewModel scc = columnsData.Single(x => x.Date == group.Key); scc.TotalCost = Math.Round(cost); scc.Profit = scc.TotalSale - scc.TotalCost; } //calculate statistics if (total_clients > 0) { spendingByClient = total_sales / total_clients; } //SPENDING DEVIATION var queryLastSevenDaysWithClients = from item in unitOfWork.OrderRepository.Get(x => x.Date >= sevenDaysDate && x.Persons > 0) //where item.Date >= sevenDaysDate && item.Persons > 0 select item; if (queryLastSevenDaysWithClients.Count() > 0) { decimal average = queryLastSevenDaysWithClients.Average(x => x.Total / x.Persons); spendingDeviation = (decimal)Math.Sqrt(queryLastSevenDaysWithClients.Average(x => Math.Pow((double)(x.Total / x.Persons - average), 2))); } //TOP PRODUCTS var lineitemsByProduct = from li in unitOfWork.LineItemRepository.Get(x => x.Order.Date >= sevenDaysDate) //where li.Order.Date >= sevenDaysDate group li by li.Product; // into prodGroups; //let profit = prodGroups.Sum(x => x.Amount - x.Cost) //orderby profit descending //select new { Product = prodGroups.Key, Total = profit }; List <ReportLineViewModel> temp_list = new List <ReportLineViewModel>(); foreach (var group in lineitemsByProduct) { ReportLineViewModel sli = new ReportLineViewModel(); sli.Product = group.Key; sli.Amount = group.Sum(x => x.Amount); sli.Cost = group.Sum(x => invSVC.GetProductCost(x.Product, x.Quantity, x.UnitMeasure)); //sli.Profit = sli.Amount - sli.Cost; temp_list.Add(sli); } temp_list = temp_list.OrderBy(x => x.Profit).ToList(); int count = 0; for (int i = temp_list.Count - 1; i >= 0; i--) { if (count++ == 10) { break; } var sli = temp_list.ElementAt(i); sli.Quantity = count; topProducts.Add(sli); } //if (total_orders > 0) // SpendingByOrder = total_sales / total_orders; ////last four weeks //var tempQuery = (from item in context.Orders.OfType<Sale>() // where item.Date >= fourWeeksDate // select item).ToArray(); //var weeksQuery = from item in tempQuery // let mondayDate = SalesChartColumnViewModel.GetWeekMonday(item.Date) // group item by mondayDate into weekgroup // orderby weekgroup.Key // select weekgroup; //foreach (var group in weeksQuery) //{ // SalesChartColumnViewModel scc = new SalesChartColumnViewModel(); // DateTime weekMonday = group.Key; // scc.X = weekMonday.Day + "-" + weekMonday.AddDays(6).Day; // scc.TotalSale =Math.Round(group.Sum(x => x.Total)); // fourWeeksItems.Add(scc); //} SevenDaysDataAnswer answer = new SevenDaysDataAnswer(); answer.TotalSales = total_sales; answer.TotalClients = total_clients; answer.SpendingByClient = spendingByClient; answer.SpendingDeviation = spendingDeviation; answer.SevenDaysColumnData = columnsData; answer.TopProducts = topProducts; return(answer); ////best day, week, month //using (var context = new RestaurantDBEntities()) //{ // //best day // var salesGroupedByDate = from sale in context.Orders.OfType<Sale>() // group sale by sale.Date; // decimal maxTotal = 0; // DateTime maxDate = DateTime.Today; // foreach (var item in salesGroupedByDate) // { // decimal total = item.Sum(x => x.Total); // if (total > maxTotal) { maxTotal = total; maxDate = item.Key; } // } // BestDay = maxTotal; // BestDayTooltip = maxDate.ToString("dd/M/yyy"); // //best week // var allSales = (from sale in context.Orders.OfType<Sale>() // select sale).ToArray(); // var salesGroupedByWeek = from item in allSales // let mondayDate = SalesChartColumnViewModel.GetWeekMonday(item.Date) // group item by mondayDate into weekgroup // orderby weekgroup.Key // select weekgroup; // maxTotal = 0; // foreach (var item in salesGroupedByWeek) // { // decimal total = item.Sum(x => x.Total); // if (total > maxTotal) { maxTotal = total; maxDate = item.Key; } // } // BestWeek = maxTotal; // BestWeekTooltip = maxDate.ToString("dd/M/yyy") + " - " + maxDate.AddDays(6).ToString("dd/M/yyy"); // //best month // //best day // var salesGroupedByMonth = from sale in context.Orders.OfType<Sale>() // group sale by sale.Date.Year into groupByYear // from monthGroup in // (from item in groupByYear // group item by item.Date.Month) // group monthGroup by groupByYear.Key; // maxTotal = 0; // foreach (var yearGroup in salesGroupedByMonth) // { // foreach (var monthGroup in yearGroup) // { // decimal total = monthGroup.Sum(x => x.Total); // if (total > maxTotal) // { // maxTotal = total; // maxDate = new DateTime(yearGroup.Key, monthGroup.Key, 1); // } // } // } // BestMonth = maxTotal; // BestMonthTooltip = maxDate.ToString("MMM/yyy"); //} }