public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var spendingBudgetCategoryIds = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId, eBudgetCategoryType.Spending).ToList(); var incomeBudgetCategoryIds = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId, eBudgetCategoryType.Income).ToList(); var savingBudgetCategoryIds = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId, eBudgetCategoryType.Saving).ToList(); var budgetCategoryIds = spendingBudgetCategoryIds.Union(incomeBudgetCategoryIds).Union(savingBudgetCategoryIds); var balances = budgetCategoryIds .Select(x => _balanceService.GetTotalCategoryBalance(x)) .ToList(); var spendingBalances = balances.Where(x => spendingBudgetCategoryIds.Contains(x.BudgetCategoryId)).ToList(); var incomeBalances = balances.Where(x => incomeBudgetCategoryIds.Contains(x.BudgetCategoryId)).ToList(); var savingBalances = balances.Where(x => savingBudgetCategoryIds.Contains(x.BudgetCategoryId)).ToList(); var spendingSummary = new BudgetedAmountSummaryDto { CurrentBudgetedAmount = spendingBalances.Select(x => x.ThisMonthBudgetedAmount).Aggregate((a, b) => a + b), TotalBudgetedAmount = spendingBalances.Select(x => x.TotalBudgetedAmount).Aggregate((a, b) => a + b), ThisYearBudgetedAmount = spendingBalances.Select(x => x.ThisYearBudgetedAmount).Aggregate((a, b) => a + b), }; var incomeSummary = new BudgetedAmountSummaryDto { CurrentBudgetedAmount = incomeBalances.Select(x => x.ThisMonthBudgetedAmount).Aggregate((a, b) => a + b), TotalBudgetedAmount = incomeBalances.Select(x => x.TotalBudgetedAmount).Aggregate((a, b) => a + b), ThisYearBudgetedAmount = incomeBalances.Select(x => x.ThisYearBudgetedAmount).Aggregate((a, b) => a + b), }; var savingSummary = new BudgetedAmountSummaryDto { CurrentBudgetedAmount = savingBalances.Select(x => x.ThisMonthBudgetedAmount).Aggregate((a, b) => a + b), TotalBudgetedAmount = savingBalances.Select(x => x.TotalBudgetedAmount).Aggregate((a, b) => a + b), ThisYearBudgetedAmount = savingBalances.Select(x => x.ThisYearBudgetedAmount).Aggregate((a, b) => a + b), }; var totalSummary = new BudgetedAmountSummaryDto { CurrentBudgetedAmount = incomeSummary.CurrentBudgetedAmount - spendingSummary.CurrentBudgetedAmount - savingSummary.CurrentBudgetedAmount, TotalBudgetedAmount = incomeSummary.TotalBudgetedAmount - spendingSummary.TotalBudgetedAmount - savingSummary.TotalBudgetedAmount, ThisYearBudgetedAmount = incomeSummary.ThisYearBudgetedAmount - spendingSummary.ThisYearBudgetedAmount - savingSummary.ThisYearBudgetedAmount, }; return(new Result() { Data = new SummaryDto() { SpendingSummary = spendingSummary, IncomeSummary = incomeSummary, SavingSummary = savingSummary, TotalSummary = totalSummary } }); }
public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var budgetCategoryIds = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId); var query = _readDb.BudgetCategories .Where(x => budgetCategoryIds.Contains(x.BudgetCategoryId) && x.BudgetId == request.BudgetId); if (request.BudgetCategoryTypes != null && request.BudgetCategoryTypes.Any()) { query = query.Where(x => request.BudgetCategoryTypes.Contains(x.BudgetCategoryType)); } var budgetCategories = await query.ToListAsync(cancellationToken : cancellationToken); var dtos = _mapper.Map <List <BudgetCategoryDto> >(budgetCategories); return(new Result() { Data = dtos.Select(x => { x.BudgetedAmounts = x.BudgetedAmounts.OrderBy(s => s.ValidFrom).ToList(); return x; }) .OrderBy(x => x.Order) .ToList(), Total = budgetCategories.Count() }); }
public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var budgetCategoryIdsQuery = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId, request.BudgetCategoryType); if (request.BudgetCategoryIds != null && request.BudgetCategoryIds.Any()) { budgetCategoryIdsQuery = budgetCategoryIdsQuery.Where(x => request.BudgetCategoryIds.Any(s => s == x)); } var budgetCategoryIds = budgetCategoryIdsQuery.ToList(); var query = _readDb.Transactions.Where(x => budgetCategoryIds.Contains(x.BudgetCategoryId)); if (!string.IsNullOrEmpty(request.Search)) { query = query.Where(x => x.Description.ToLower().Contains(request.Search.ToLower())); } if (request.TransactionDateStart != null) { query = query.Where(x => x.TransactionDate >= request.TransactionDateStart.Value.Date); } if (request.TransactionDateEnd != null) { query = query.Where(x => x.TransactionDate <= request.TransactionDateEnd.Value.Date); } if (request.MinAmount != null) { query = query.Where(x => x.Amount.Amount >= request.MinAmount); } if (request.MaxAmount != null) { query = query.Where(x => x.Amount.Amount <= request.MaxAmount); } var data = await query.ProjectTo <TransactionDto>(_mapperConfiguration) .ApplyGridQueryOptions(request) .ToListAsync(cancellationToken); return(new Result() { Data = data, Total = query.Count(), PageSize = request.PageSize }); }
public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var budgetCategoryIdsQuery = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId, request.BudgetCategoryType); var budgetCategoryIds = budgetCategoryIdsQuery.ToList(); var query = _readDb.TransactionTemplates.Where(x => budgetCategoryIds.Contains(x.BudgetCategoryId)); var data = await query.ProjectTo <TransactionTemplateDto>(_mapperConfiguration) .OrderBy(x => x.Description) .ToListAsync(cancellationToken); return(new Result() { Data = data, Total = query.Count() }); }
public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var budgetCategoryIds = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId).ToList(); var query = _readDb.Allocations.Where(x => budgetCategoryIds.Any(s => s == x.TargetBudgetCategoryId)); DateTime?min = null; DateTime?max = null; if (query.Any()) { min = (await query.MinAsync(x => x.AllocationDate, cancellationToken)).Date; max = (await query.MaxAsync(x => x.AllocationDate, cancellationToken)).Date; } return(new Result() { Data = new DatesRangeDto() { MaxDate = max, MinDate = min } }); }
public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var budgetCategoryIdsQuery = _accessControlService.GetAccessibleBudgetCategoryIds(request.BudgetId, request.BudgetCategoryType); if (request.BudgetCategoryIds != null && request.BudgetCategoryIds.Any()) { budgetCategoryIdsQuery = budgetCategoryIdsQuery.Where(x => request.BudgetCategoryIds.Any(s => s == x)); } var budgetCurrency = _readDb.Budgets.First(x => x.BudgetId == request.BudgetId).Currency; var budgetCategoryIds = budgetCategoryIdsQuery.ToList(); var budgetCategories = _readDb.BudgetCategories.Where(x => budgetCategoryIds.Contains(x.BudgetCategoryId)).ToList(); var query = _readDb.Transactions.Where(x => budgetCategoryIds.Contains(x.BudgetCategoryId)); query = query.Where(x => x.TransactionDate >= request.TransactionDateStart.Date && x.TransactionDate <= request.TransactionDateEnd.Date); var transactions = await query.ToListAsync(cancellationToken); if (!transactions.Any()) { return(new Result()); } var timelinePeriod = new DateRange(request.TransactionDateStart, request.TransactionDateEnd); var periods = request.GroupingStep switch { eGroupingStep.Day => timelinePeriod.Start.EachDayTo(timelinePeriod.End), eGroupingStep.Year => timelinePeriod.Start.EachYearTo(timelinePeriod.End), eGroupingStep.Month => timelinePeriod.Start.EachMonthTo(timelinePeriod.End), eGroupingStep.Quarter => timelinePeriod.Start.EachQuarterTo(timelinePeriod.End), eGroupingStep.Week => timelinePeriod.Start.EachWeekTo(timelinePeriod.End, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek), _ => throw new ArgumentOutOfRangeException() }; var dateRanges = transactions.GroupBy(x => periods.FirstOrDefault(s => s.Contains(x.TransactionDate))) .Select(periodGroup => { var period = periodGroup.Key; var categoryDataPoints = budgetCategories.Select(category => { var categoryId = category.BudgetCategoryId; var categoryAmount = periodGroup.Where(s => s.BudgetCategoryId == categoryId) .Select(s => s.Amount + s.SubTransactions.Select(t => t.Amount).Aggregate(new MoneyAmount(budgetCurrency.CurrencyCode, 0), (a, b) => a + b)) .Aggregate(new MoneyAmount(budgetCurrency.CurrencyCode, 0), (a, b) => a + b); return(new BudgetCategoryDataPointDto() { DateRange = period, BudgetCategoryId = categoryId, Key = period.ToString(), AmountTotal = categoryAmount }); }) .ToList(); var totalAmount = categoryDataPoints.Select(x => x.AmountTotal) .Aggregate(new MoneyAmount(budgetCurrency.CurrencyCode, 0), (a, b) => a + b); return(new DateRangeData() { DateRange = period, Key = period.ToString(), BudgetCategories = categoryDataPoints, Total = new DataPointDto() { DateRange = period, Key = period.ToString(), AmountTotal = totalAmount } }); }) .OrderByDescending(x => x.DateRange.Start) .ToList(); if (dateRanges.Count > 1) { for (var i = 0; i < dateRanges.Count - 1; i++) { var current = dateRanges[i]; var previous = dateRanges[i + 1]; foreach (var budgetCategoryDataPoint in current.BudgetCategories) { var previousBudgetCategoryDataPoint = previous.BudgetCategories.FirstOrDefault(x => x.BudgetCategoryId == budgetCategoryDataPoint.BudgetCategoryId); if (previousBudgetCategoryDataPoint == null) { continue; ; } budgetCategoryDataPoint.AmountTotalChange = previousBudgetCategoryDataPoint.AmountTotal.Amount > 0 ? (budgetCategoryDataPoint.AmountTotal.Amount - previousBudgetCategoryDataPoint.AmountTotal.Amount) / previousBudgetCategoryDataPoint.AmountTotal.Amount : (decimal?)null; budgetCategoryDataPoint.AmountPerDayChange = previousBudgetCategoryDataPoint.AmountPerDay.Amount > 0 ? (budgetCategoryDataPoint.AmountPerDay.Amount - previousBudgetCategoryDataPoint.AmountPerDay.Amount) / previousBudgetCategoryDataPoint.AmountPerDay.Amount : (decimal?)null; budgetCategoryDataPoint.AmountPerWeekChange = previousBudgetCategoryDataPoint.AmountPerWeek.Amount > 0 ? (budgetCategoryDataPoint.AmountPerWeek.Amount - previousBudgetCategoryDataPoint.AmountPerWeek.Amount) / previousBudgetCategoryDataPoint.AmountPerWeek.Amount : (decimal?)null; budgetCategoryDataPoint.AmountPerMonthChange = previousBudgetCategoryDataPoint.AmountPerMonth.Amount > 0 ? (budgetCategoryDataPoint.AmountPerMonth.Amount - previousBudgetCategoryDataPoint.AmountPerMonth.Amount) / previousBudgetCategoryDataPoint.AmountPerMonth.Amount : (decimal?)null; } current.Total.AmountTotalChange = previous.Total.AmountTotal.Amount > 0 ? (current.Total.AmountTotal.Amount - previous.Total.AmountTotal.Amount) / previous.Total.AmountTotal.Amount : (decimal?)null; current.Total.AmountPerDayChange = previous.Total.AmountTotal.Amount > 0 ? (current.Total.AmountPerDay.Amount - previous.Total.AmountPerDay.Amount) / previous.Total.AmountPerDay.Amount : (decimal?)null; current.Total.AmountPerWeekChange = previous.Total.AmountTotal.Amount > 0 ? (current.Total.AmountPerWeek.Amount - previous.Total.AmountPerWeek.Amount) / previous.Total.AmountPerWeek.Amount : (decimal?)null; current.Total.AmountPerMonthChange = previous.Total.AmountTotal.Amount > 0 ? (current.Total.AmountPerMonth.Amount - previous.Total.AmountPerMonth.Amount) / previous.Total.AmountPerMonth.Amount : (decimal?)null; } } var budgetCategoryTotals = dateRanges.SelectMany(x => x.BudgetCategories) .GroupBy(x => x.BudgetCategoryId) .Select(x => new BudgetCategoryDataPointDto() { Key = timelinePeriod.ToString(), DateRange = timelinePeriod, BudgetCategoryId = x.Key, AmountTotal = x.Select(s => s.AmountTotal) .Aggregate(new MoneyAmount(budgetCurrency.CurrencyCode, 0), (a, b) => a + b) }) .ToList(); var total = new DataPointDto() { DateRange = timelinePeriod, Key = timelinePeriod.ToString(), AmountTotal = budgetCategoryTotals.Select(x => x.AmountTotal) .Aggregate(new MoneyAmount(budgetCurrency.CurrencyCode, 0), (a, b) => a + b) }; return(new Result() { Data = new ResponseDto() { DateRanges = dateRanges, BudgetCategoryTotals = budgetCategoryTotals, Total = total } }); } }