151 lines
4.5 KiB
C#
151 lines
4.5 KiB
C#
|
|
using BudgetApp.Data.Entities;
|
|
|
|
namespace BudgetApp.Components.Pages;
|
|
|
|
public partial class Salary
|
|
{
|
|
private List<Data.Entities.Salary>? _salaries;
|
|
private List<Account>? _accountsList;
|
|
private readonly Dictionary<int, DateTime?> _nextPaydates = [];
|
|
private string? _userId;
|
|
private SalaryForm _formModel = new();
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
_userId = authState.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
|
|
await LoadData();
|
|
}
|
|
|
|
private async Task LoadData()
|
|
{
|
|
if (_userId is null) return;
|
|
|
|
var windowEnd = DateTime.Today.AddDays(90);
|
|
await SalaryService.MaterializeSalaryTransactionsAsync(_userId, windowEnd);
|
|
|
|
_salaries = await SalaryService.GetSalariesAsync(_userId);
|
|
_accountsList = await AccountService.GetAccountsAsync(_userId);
|
|
|
|
_nextPaydates.Clear();
|
|
foreach (var sal in _salaries)
|
|
{
|
|
_nextPaydates[sal.Id] = await SalaryService.GetNextPaydateAsync(sal.Id);
|
|
}
|
|
|
|
if (_accountsList.Count > 0 && _formModel.AccountId == 0)
|
|
{
|
|
_formModel.AccountId = _accountsList[0].Id;
|
|
}
|
|
}
|
|
|
|
private async Task AddSalary()
|
|
{
|
|
if (_userId is null) return;
|
|
|
|
var entry = new Data.Entities.Salary
|
|
{
|
|
Name = _formModel.Name,
|
|
Amount = _formModel.Amount,
|
|
AccountId = _formModel.AccountId,
|
|
RecurrenceType = _formModel.RecurrenceType,
|
|
StartDate = _formModel.StartDate,
|
|
UserId = _userId
|
|
};
|
|
|
|
await SalaryService.CreateSalaryAsync(entry, DateTime.Today.AddDays(90));
|
|
|
|
_formModel = new SalaryForm
|
|
{
|
|
AccountId = _accountsList?.FirstOrDefault()?.Id ?? 0
|
|
};
|
|
|
|
await LoadData();
|
|
}
|
|
|
|
private bool _showDeleteModal;
|
|
private Data.Entities.Salary? _pendingDeleteSalary;
|
|
|
|
private bool _showEditModal;
|
|
private int _editSalaryId;
|
|
private SalaryForm _editFormModel = new();
|
|
|
|
private void OpenEditModal(Data.Entities.Salary s)
|
|
{
|
|
_editSalaryId = s.Id;
|
|
_editFormModel = new SalaryForm
|
|
{
|
|
Name = s.Name,
|
|
Amount = s.Amount,
|
|
AccountId = s.AccountId,
|
|
RecurrenceType = s.RecurrenceType,
|
|
StartDate = s.StartDate
|
|
};
|
|
_showEditModal = true;
|
|
}
|
|
|
|
private void CancelEdit() => _showEditModal = false;
|
|
|
|
private async Task SaveEdit()
|
|
{
|
|
if (_userId is null) return;
|
|
var salary = new Data.Entities.Salary
|
|
{
|
|
Id = _editSalaryId,
|
|
Name = _editFormModel.Name,
|
|
Amount = _editFormModel.Amount,
|
|
AccountId = _editFormModel.AccountId,
|
|
RecurrenceType = _editFormModel.RecurrenceType,
|
|
StartDate = _editFormModel.StartDate,
|
|
UserId = _userId
|
|
};
|
|
await SalaryService.UpdateSalaryAsync(salary, DateTime.Today.AddDays(90), _userId);
|
|
_showEditModal = false;
|
|
await LoadData();
|
|
}
|
|
|
|
private void ConfirmDelete(Data.Entities.Salary s)
|
|
{
|
|
_pendingDeleteSalary = s;
|
|
_showDeleteModal = true;
|
|
}
|
|
|
|
private void CancelDelete()
|
|
{
|
|
_showDeleteModal = false;
|
|
_pendingDeleteSalary = null;
|
|
}
|
|
|
|
private async Task ExecuteDelete()
|
|
{
|
|
if (_userId is null || _pendingDeleteSalary is null) return;
|
|
_showDeleteModal = false;
|
|
await SalaryService.DeleteSalaryAsync(_pendingDeleteSalary.Id, _userId);
|
|
_pendingDeleteSalary = null;
|
|
await LoadData();
|
|
}
|
|
|
|
private sealed class SalaryForm : System.ComponentModel.DataAnnotations.IValidatableObject
|
|
{
|
|
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Name is required")]
|
|
[System.ComponentModel.DataAnnotations.MaxLength(200)]
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
[System.ComponentModel.DataAnnotations.Range(0.01, double.MaxValue,
|
|
ErrorMessage = "Amount must be greater than zero")]
|
|
public decimal Amount { get; set; }
|
|
|
|
public int AccountId { get; set; }
|
|
|
|
public RecurrenceType RecurrenceType { get; set; } = RecurrenceType.Monthly;
|
|
|
|
public DateTime StartDate { get; set; } = DateTime.Today;
|
|
|
|
public IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> Validate(
|
|
System.ComponentModel.DataAnnotations.ValidationContext validationContext)
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
} |