public DistributionTimeViewModel(string budgetId, IBudgetProjection budget, IEnumerable <Category> categories) { BudgetId = budgetId; BudgetName = budget.Name; Categories = categories; _checkPoints = budget.GetCheckPoints(); var lines = budget.GetAllLines(); CheckPointsSlices = Group(lines); // No giving back //var total = lines.Where(r => r.DistributionKey != null).GroupBy(g => g.DistributionKey) // .Select(s => new KeyAmount // { // DistributionKey = s.Key, // Amount = s.Select(r => r.Amount).Sum(), // }); var total = lines.Where(r => r.DistributionKey != null) .Select(s => new KeyAmount { DistributionKey = s.DistributionKey, Amount = s.Amount, }) .Concat(_checkPoints.SelectMany(s => s.Amounts).Select(p => new KeyAmount { DistributionKey = p.DistributionKey, Amount = p.Amount })) .GroupBy(g => g.DistributionKey) .Select(r => new KeyAmount { DistributionKey = r.Key, Amount = r.Select(p => p.Amount).Sum() }).ToList(); var max = total.OrderByDescending(d => d.Amount).Select(s => s.Amount).FirstOrDefault(); SubmitCheckPoint = new SubmitCheckPoint { BudgetId = budgetId, CurrencyISOCode = budget.CurrencyISOCode, CheckPointId = "BudgetDistributionCheckPoints-" + Guid.NewGuid(), Date = DateTime.Now, Amounts = total.Select(s => new MyBudget.Web.AspNet.Models.KeyAmount { DistributionKey = s.DistributionKey, Amount = max - s.Amount, }).ToList(), }; // With giving back //var lastLine = lines.Select(s => s.Date).DefaultIfEmpty(DateTime.MinValue).Max(); // var currentSlide = CheckPointsSlices.First(); //var sharingGroups = currentSlide.Groups.Where(g => g.Name != null); //decimal totalForEach = 0; //if(sharingGroups.Any()) // totalForEach = sharingGroups.Select(r => r.TotalAmount).Sum() / sharingGroups.Count(); //SubmitCheckPoint = new SubmitCheckPoint //{ // BudgetId = budgetId, // CurrencyISOCode = budget.CurrencyISOCode, // CheckPointId = "BudgetDistributionCheckPoints-" + Guid.NewGuid(), // Date = lastLine, // Amounts = sharingGroups.Select(s => new MyBudget.Web.AspNet.Models.KeyAmount // { // DistributionKey = s.Name, // Amount = totalForEach - s.TotalAmount, // }).ToList(), //}; }
public DistributionTimeViewModel(string budgetId, IBudgetProjection budget, IEnumerable<Category> categories) { BudgetId = budgetId; BudgetName = budget.Name; Categories = categories; _checkPoints = budget.GetCheckPoints(); var lines = budget.GetAllLines(); CheckPointsSlices = Group(lines); // No giving back //var total = lines.Where(r => r.DistributionKey != null).GroupBy(g => g.DistributionKey) // .Select(s => new KeyAmount // { // DistributionKey = s.Key, // Amount = s.Select(r => r.Amount).Sum(), // }); var total = lines.Where(r => r.DistributionKey != null) .Select(s => new KeyAmount { DistributionKey = s.DistributionKey, Amount = s.Amount, }) .Concat(_checkPoints.SelectMany(s => s.Amounts).Select(p => new KeyAmount { DistributionKey = p.DistributionKey, Amount = p.Amount })) .GroupBy(g => g.DistributionKey) .Select(r => new KeyAmount { DistributionKey = r.Key, Amount = r.Select(p => p.Amount).Sum() }).ToList(); var max = total.OrderByDescending(d => d.Amount).Select(s => s.Amount).FirstOrDefault(); SubmitCheckPoint = new SubmitCheckPoint { BudgetId = budgetId, CurrencyISOCode = budget.CurrencyISOCode, CheckPointId = "BudgetDistributionCheckPoints-" + Guid.NewGuid(), Date = DateTime.Now, Amounts = total.Select(s => new MyBudget.Web.AspNet.Models.KeyAmount { DistributionKey = s.DistributionKey, Amount = max - s.Amount, }).ToList(), }; // With giving back //var lastLine = lines.Select(s => s.Date).DefaultIfEmpty(DateTime.MinValue).Max(); // var currentSlide = CheckPointsSlices.First(); //var sharingGroups = currentSlide.Groups.Where(g => g.Name != null); //decimal totalForEach = 0; //if(sharingGroups.Any()) // totalForEach = sharingGroups.Select(r => r.TotalAmount).Sum() / sharingGroups.Count(); //SubmitCheckPoint = new SubmitCheckPoint //{ // BudgetId = budgetId, // CurrencyISOCode = budget.CurrencyISOCode, // CheckPointId = "BudgetDistributionCheckPoints-" + Guid.NewGuid(), // Date = lastLine, // Amounts = sharingGroups.Select(s => new MyBudget.Web.AspNet.Models.KeyAmount // { // DistributionKey = s.Name, // Amount = totalForEach - s.TotalAmount, // }).ToList(), //}; }