diff --git a/MoneyMap/Pages/Index.cshtml b/MoneyMap/Pages/Index.cshtml index f25cac3..dd38a2c 100644 --- a/MoneyMap/Pages/Index.cshtml +++ b/MoneyMap/Pages/Index.cshtml @@ -71,6 +71,7 @@ Category Total Spend + % of Total Txns @@ -84,6 +85,9 @@ @c.TotalSpend.ToString("C") + + @c.PercentageOfTotal.ToString("F1")% + @c.Count } diff --git a/MoneyMap/Pages/Index.cshtml.cs b/MoneyMap/Pages/Index.cshtml.cs index ceab630..b124efb 100644 --- a/MoneyMap/Pages/Index.cshtml.cs +++ b/MoneyMap/Pages/Index.cshtml.cs @@ -44,6 +44,7 @@ namespace MoneyMap.Pages public string Category { get; set; } = ""; public decimal TotalSpend { get; set; } public int Count { get; set; } + public decimal PercentageOfTotal { get; set; } } public class RecentTxnRow @@ -176,20 +177,30 @@ namespace MoneyMap.Pages { var since = DateTime.UtcNow.Date.AddDays(-lastDays); - return await _db.Transactions + // Get all expense transactions for the period + var expenseTransactions = await _db.Transactions .Where(t => t.Date >= since && t.Amount < 0) .ExcludeTransfers() // Exclude credit card payments and transfers + .ToListAsync(); + + // Calculate total spend + var totalSpend = expenseTransactions.Sum(t => -t.Amount); + + // Group by category and calculate percentages + var topCategories = expenseTransactions .GroupBy(t => t.Category ?? "") .Select(g => new IndexModel.TopCategoryRow { Category = g.Key, TotalSpend = g.Sum(x => -x.Amount), - Count = g.Count() + Count = g.Count(), + PercentageOfTotal = totalSpend > 0 ? (g.Sum(x => -x.Amount) / totalSpend) * 100 : 0 }) .OrderByDescending(x => x.TotalSpend) .Take(count) - .AsNoTracking() - .ToListAsync(); + .ToList(); + + return topCategories; } }