265 lines
10 KiB
Plaintext
265 lines
10 KiB
Plaintext
@page "/salary"
|
|
@attribute [Authorize]
|
|
@inject SalaryService SalaryService
|
|
@inject AccountService AccountService
|
|
@inject AuthenticationStateProvider AuthStateProvider
|
|
@rendermode InteractiveServer
|
|
|
|
<PageTitle>Salary</PageTitle>
|
|
|
|
<h3 class="mb-2">Salary</h3>
|
|
<p class="text-muted mb-4">Define your regular income sources. Each salary generates recurring transactions at the set interval, forever.</p>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<h5 class="mb-0">Add Income Source</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
@if (accountsList is not null && accountsList.Count == 0)
|
|
{
|
|
<div class="alert alert-warning mb-0">
|
|
You need to <a href="/accounts">create an account</a> before adding a salary.
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<EditForm Model="formModel" OnValidSubmit="AddSalary" FormName="addSalary">
|
|
<ToastValidator />
|
|
<div class="row g-3">
|
|
<div class="col-md-3">
|
|
<label class="form-label">Name</label>
|
|
<InputText @bind-Value="formModel.Name" class="form-control" placeholder="e.g. Main Job" />
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label">Amount</label>
|
|
<InputNumber @bind-Value="formModel.Amount" class="form-control numeric-input" />
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label">Account</label>
|
|
<InputSelect @bind-Value="formModel.AccountId" class="form-select">
|
|
@if (accountsList is not null)
|
|
{
|
|
@foreach (var account in accountsList)
|
|
{
|
|
<option value="@account.Id">@account.Name</option>
|
|
}
|
|
}
|
|
</InputSelect>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label">Recurrence</label>
|
|
<InputSelect @bind-Value="formModel.RecurrenceType" class="form-select">
|
|
<option value="@RecurrenceType.Weekly">Weekly</option>
|
|
<option value="@RecurrenceType.Monthly">Monthly</option>
|
|
<option value="@RecurrenceType.Yearly">Yearly</option>
|
|
</InputSelect>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label">First Payday</label>
|
|
<InputDate @bind-Value="formModel.StartDate" class="form-control" title="The date of the first payment — the interval repeats from here indefinitely" />
|
|
</div>
|
|
<div class="col-md-12 col-lg-auto d-flex align-items-end">
|
|
<button type="submit" class="btn btn-primary w-100">Add</button>
|
|
</div>
|
|
</div>
|
|
</EditForm>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
@if (salaries is null)
|
|
{
|
|
<p><em>Loading...</em></p>
|
|
}
|
|
else if (salaries.Count == 0)
|
|
{
|
|
<div class="alert alert-info">No income sources yet. Add one above to start tracking your salary.</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="mb-0">Income Sources</h5>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Amount</th>
|
|
<th>Account</th>
|
|
<th class="d-none d-md-table-cell">Schedule</th>
|
|
<th>Next Payday</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var s in salaries)
|
|
{
|
|
<tr>
|
|
<td class="fw-semibold">💰 @s.Name</td>
|
|
<td class="text-success fw-semibold text-nowrap">+@s.Account.FormatAmount(s.Amount)</td>
|
|
<td>
|
|
<span class="badge" style="background-color: @s.Account.Color;">
|
|
@s.Account.Name
|
|
</span>
|
|
</td>
|
|
<td class="d-none d-md-table-cell">
|
|
<span class="badge bg-info text-nowrap">🔁 @s.GetPaydaySummary()</span>
|
|
</td>
|
|
<td class="text-nowrap">
|
|
@if (nextPaydates.TryGetValue(s.Id, out var next) && next.HasValue)
|
|
{
|
|
<span>@next.Value.ToString("MMM dd, yyyy")</span>
|
|
}
|
|
else
|
|
{
|
|
<span class="text-muted">—</span>
|
|
}
|
|
</td>
|
|
<td class="text-end text-nowrap">
|
|
<button class="btn btn-sm btn-outline-danger" @onclick="() => ConfirmDelete(s)">Delete</button>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="card-footer">
|
|
<small class="text-muted">@salaries.Count income source@(salaries.Count == 1 ? "" : "s") · Transactions are generated automatically and appear on the <a href="/transactions">Transactions</a> page.</small>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@if (showDeleteModal && pendingDeleteSalary is not null)
|
|
{
|
|
<div class="modal d-block" tabindex="-1" style="background: rgba(0,0,0,.5);">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Delete Income Source</h5>
|
|
<button type="button" class="btn-close" @onclick="CancelDelete"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>Are you sure you want to delete <strong>@pendingDeleteSalary.Name</strong>?</p>
|
|
<p>All future unpaid salary transactions will be removed. Past transactions remain in your history.</p>
|
|
<p class="text-danger mb-0">This action cannot be undone.</p>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" @onclick="CancelDelete">Cancel</button>
|
|
<button type="button" class="btn btn-danger" @onclick="ExecuteDelete">Delete</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private List<BudgetApp.Data.Salary>? salaries;
|
|
private List<Account>? accountsList;
|
|
private 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 BudgetApp.Data.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();
|
|
}
|
|
|
|
// Delete state
|
|
private bool showDeleteModal;
|
|
private BudgetApp.Data.Salary? pendingDeleteSalary;
|
|
|
|
private void ConfirmDelete(BudgetApp.Data.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;
|
|
}
|
|
}
|
|
}
|