195 lines
4.6 KiB
C#
195 lines
4.6 KiB
C#
using Duempelkas.App.Services.Models;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace Duempelkas.App.Pages.Accounts;
|
|
|
|
public partial class AccountDetail
|
|
{
|
|
#region Parameters
|
|
|
|
[Parameter]
|
|
public int AccountId { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Fields
|
|
|
|
private AccountSummaryDto? account;
|
|
private AccountBalanceDto? balance;
|
|
private List<EntryDto>? entries;
|
|
private bool showAddEntry;
|
|
private bool showAddTransfer;
|
|
private bool showEditName;
|
|
private bool showEditCarryover;
|
|
private bool showCurrentYearOnly = true;
|
|
|
|
private int? confirmDeleteEntryId;
|
|
private string? confirmDeleteEntryTitle;
|
|
|
|
private int? confirmRestoreEntryId;
|
|
private string? confirmRestoreEntryTitle;
|
|
|
|
private string? savedPdfPath;
|
|
|
|
private EntryDto? editingEntry;
|
|
private EntryDto? editingTransferEntry;
|
|
|
|
#endregion
|
|
|
|
#region Navigation
|
|
|
|
private void NavigateBack() => Navigation.NavigateTo("/");
|
|
|
|
#endregion
|
|
|
|
#region Lifecycle
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
await LoadAll();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Data Loading
|
|
|
|
private async Task LoadAll()
|
|
{
|
|
account = await AccountService.GetAccountAsync(AccountId);
|
|
balance = await BalanceQueryService.GetAccountBalanceAsync(AccountId);
|
|
entries = await EntryService.GetEntriesAsync(AccountId, showCurrentYearOnly);
|
|
}
|
|
|
|
private async Task ToggleYearFilter()
|
|
{
|
|
showCurrentYearOnly = !showCurrentYearOnly;
|
|
entries = await EntryService.GetEntriesAsync(AccountId, showCurrentYearOnly);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Actions
|
|
|
|
private async Task HandleSaveName(string newName)
|
|
{
|
|
await AccountService.RenameAccountAsync(AccountId, newName);
|
|
showEditName = false;
|
|
await LoadAll();
|
|
}
|
|
|
|
private async Task HandleSaveCarryover(decimal newAmount)
|
|
{
|
|
await AccountService.UpdateCarryoverAsync(AccountId, newAmount);
|
|
showEditCarryover = false;
|
|
await LoadAll();
|
|
}
|
|
|
|
private async Task HandleEntryCreated()
|
|
{
|
|
showAddEntry = false;
|
|
await LoadAll();
|
|
}
|
|
|
|
private async Task HandleTransferCreated()
|
|
{
|
|
showAddTransfer = false;
|
|
await LoadAll();
|
|
}
|
|
|
|
private void RequestDeleteEntry(int entryId)
|
|
{
|
|
confirmDeleteEntryId = entryId;
|
|
confirmDeleteEntryTitle = entries?.FirstOrDefault(e => e.Id == entryId)?.Title;
|
|
}
|
|
|
|
private void CancelDeleteConfirm()
|
|
{
|
|
confirmDeleteEntryId = null;
|
|
confirmDeleteEntryTitle = null;
|
|
}
|
|
|
|
private async Task HandleConfirmDelete()
|
|
{
|
|
if (confirmDeleteEntryId.HasValue)
|
|
{
|
|
await EntryService.DeleteEntryAsync(confirmDeleteEntryId.Value);
|
|
confirmDeleteEntryId = null;
|
|
confirmDeleteEntryTitle = null;
|
|
await LoadAll();
|
|
}
|
|
}
|
|
|
|
private void RequestRestoreEntry(int entryId)
|
|
{
|
|
confirmRestoreEntryId = entryId;
|
|
confirmRestoreEntryTitle = entries?.FirstOrDefault(e => e.Id == entryId)?.Title;
|
|
}
|
|
|
|
private void CancelRestoreConfirm()
|
|
{
|
|
confirmRestoreEntryId = null;
|
|
confirmRestoreEntryTitle = null;
|
|
}
|
|
|
|
private async Task HandleConfirmRestore()
|
|
{
|
|
if (confirmRestoreEntryId.HasValue)
|
|
{
|
|
await EntryService.RestoreEntryAsync(confirmRestoreEntryId.Value);
|
|
confirmRestoreEntryId = null;
|
|
confirmRestoreEntryTitle = null;
|
|
await LoadAll();
|
|
}
|
|
}
|
|
|
|
private void RequestEditEntry(int entryId)
|
|
{
|
|
var entry = entries?.FirstOrDefault(e => e.Id == entryId);
|
|
if (entry?.IsTransfer == true)
|
|
{
|
|
editingTransferEntry = entry;
|
|
}
|
|
else
|
|
{
|
|
editingEntry = entry;
|
|
}
|
|
}
|
|
|
|
private async Task HandleEntryEdited()
|
|
{
|
|
editingEntry = null;
|
|
editingTransferEntry = null;
|
|
await LoadAll();
|
|
}
|
|
|
|
private async Task HandleExport()
|
|
{
|
|
var pdf = await PdfStatementService.GenerateStatementAsync(AccountId, showCurrentYearOnly);
|
|
var suffix = showCurrentYearOnly ? $"_{DateTime.Now.Year}" : "_Gesamt";
|
|
savedPdfPath = await FileSaveService.SaveFileAsync(pdf, $"{account?.Name}{suffix}.pdf");
|
|
}
|
|
|
|
private async Task HandleOpenSavedPdf()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(savedPdfPath))
|
|
{
|
|
await FileSaveService.OpenFileAsync(savedPdfPath);
|
|
}
|
|
|
|
savedPdfPath = null;
|
|
}
|
|
|
|
private void CancelOpenSavedPdf()
|
|
{
|
|
savedPdfPath = null;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Helpers
|
|
|
|
private static string FormatAmount(decimal amount) => $"{amount:N2} €";
|
|
|
|
#endregion
|
|
}
|