Skip to content

Commit

Permalink
Add savings to report
Browse files Browse the repository at this point in the history
  • Loading branch information
lig1990 committed Mar 10, 2024
1 parent f3d2d18 commit f38c45a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Savings.SPA/Pages/Reports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ else
<b>@RecurrentItems[i].Note</b>

@{
var currentItemProjection = Projections.Where(x => x.RecurrentMoneyItemID == RecurrentItems[i].ID);
var currentItemProjection = Installments.Where(x => x.RecurrentMoneyItemID == RecurrentItems[i].ID);
}
<table class="table table-sm table-striped table-hover">
<thead class="table-dark">
Expand Down Expand Up @@ -108,6 +108,6 @@ else
</div>
}
</div>
<div><b>TOTAL: @Projections.Sum(x => x.Amount).ToString("N2")</b></div>
<div><b>TOTAL: @Installments.Sum(x => x.Amount).ToString("N2")</b></div>

}
33 changes: 26 additions & 7 deletions src/Savings.SPA/Pages/Reports.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
using Radzen;
using Savings.Model;
using Savings.SPA.Services;
using System.ComponentModel;
using System.Linq.Dynamic.Core;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace Savings.SPA.Pages
{
Expand All @@ -20,7 +17,9 @@ public partial class Reports : ComponentBase

private RecurrentMoneyItem[] RecurrentItems { get; set; }

private MaterializedMoneyItem[] Projections { get; set; }
private MaterializedMoneyItem[] Installments { get; set; }

private MaterializedMoneyItem[] EndPeriods { get; set; }

public string FilterCategoryGroupByPeriod { get; set; } = "yy/MM";

Expand All @@ -33,6 +32,7 @@ public partial class Reports : ComponentBase
async void DateTimeDateChanged(DateTime? value, string name)
{
await InitializeCategoryResume();
await InitializeEndPeriods();
StateHasChanged();
}

Expand All @@ -51,8 +51,9 @@ protected override async Task OnInitializedAsync()
FilterDateTo = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
FilterDateFrom = FilterDateTo.AddYears(-1);

await InitializeInstallmentResume();
await InitializeCategoryResume();
await InitializeInstallmentResume();
await InitializeEndPeriods();
}

async Task InitializeInstallmentResume()
Expand All @@ -65,14 +66,25 @@ async Task InitializeInstallmentResume()
endDate = RecurrentItems.Max(x => x.EndDate.Value);
}
var projections = await savingsAPI.GetSavings(null, endDate, true);
Projections = projections.Where(x => x.RecurrentMoneyItemID.HasValue && x.Amount != 0 && x.Date >= DateTime.Now).ToArray();
Installments = projections.Where(x => x.RecurrentMoneyItemID.HasValue && x.Amount != 0 && x.Date >= DateTime.Now).ToArray();
}

async Task InitializeCategoryResume()
{
statistics = await savingsAPI.GetCategoryResume(FilterCategoryGroupByPeriod, FilterDateFrom, FilterDateTo);
}

async Task InitializeEndPeriods()
{
var past = await savingsAPI.GetMaterializedMoneyItems(FilterDateFrom, FilterDateTo, false);
past = past.Where(x => x.EndPeriod).ToArray();

var projection = await savingsAPI.GetSavings(FilterDateFrom, FilterDateTo);
projection = projection.Where(x => x.EndPeriod).ToArray();

EndPeriods = past.Union(projection).ToArray();
}

async Task OpenDetails(long? category, string period)
{
var res = await dialogService.OpenAsync<ReportsDetail>($"Report details",
Expand Down Expand Up @@ -106,11 +118,18 @@ ReportCategory[] FilterStatisticsResume()
.Select(x => new ReportPeriodAmount { Period = x.incItem.Period, Amount = x.incItem.Amount - x.outgItem.Amount });


var savings = EndPeriods
.Where(x => x.Date >= FilterDateFrom && x.Date <= FilterDateTo)
.GroupBy(x => x.Date.ToString(FilterCategoryGroupByPeriod))
.Select(x => new ReportPeriodAmount { Period = x.Key, Amount = (double)x.OrderByDescending(x => x.Date).First().Projection });


return new ReportCategory[]
{
new ReportCategory { Category="Outgoing", Data=outgoing.OrderBy(x=>x.Period).ToArray() },
new ReportCategory { Category="Incoming", Data=incoming.OrderBy(x=>x.Period).ToArray() },
new ReportCategory { Category="Gain", Data=gain.OrderBy(x=>x.Period).ToArray() }
new ReportCategory { Category="Gain", Data=gain.OrderBy(x=>x.Period).ToArray() },
new ReportCategory { Category="Savings", Data=savings.OrderBy(x=>x.Period).ToArray() }
};
}

Expand Down

0 comments on commit f38c45a

Please sign in to comment.