diff --git a/MoneyMap/Pages/CreateTransfer.cshtml b/MoneyMap/Pages/CreateTransfer.cshtml deleted file mode 100644 index 4c13592..0000000 --- a/MoneyMap/Pages/CreateTransfer.cshtml +++ /dev/null @@ -1,128 +0,0 @@ -@page -@model MoneyMap.Pages.CreateTransferModel -@{ - ViewData["Title"] = "Create Transfer"; -} - -
-

Create Transfer

- Back to Transactions -
- -@if (!string.IsNullOrEmpty(Model.SuccessMessage)) -{ - -} - -@if (!string.IsNullOrEmpty(Model.ErrorMessage)) -{ - -} - -
-
-
-
- Transfer Details -
-
-
- Note: This will create two matching transactions - a debit from the source account and a credit to the destination account. -
- -
-
-
- - - -
-
- - - -
-
- -
-
- - - -
-
- - - -
-
- -
- - -
Optional note to help you identify this transfer
- -
- -
- - -
Defaults to "Transfer" if left blank
- -
- - - - - @foreach (var cat in Model.AvailableCategories) - { - - } - - -
- - Cancel -
-
-
-
- -
-
- How Transfers Work -
-
-
    -
  • Creates two linked transactions to maintain balance accuracy
  • -
  • Source account gets a debit (money out)
  • -
  • Destination account gets a credit (money in)
  • -
  • Both transactions are linked and can be identified as transfers
  • -
  • Transfers can be filtered out when viewing spending reports
  • -
-
-
-
-
- -@section Scripts { - -} diff --git a/MoneyMap/Pages/CreateTransfer.cshtml.cs b/MoneyMap/Pages/CreateTransfer.cshtml.cs deleted file mode 100644 index 9dedbf4..0000000 --- a/MoneyMap/Pages/CreateTransfer.cshtml.cs +++ /dev/null @@ -1,147 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.EntityFrameworkCore; -using MoneyMap.Data; -using MoneyMap.Models; -using System.ComponentModel.DataAnnotations; - -namespace MoneyMap.Pages -{ - public class CreateTransferModel : PageModel - { - private readonly MoneyMapContext _db; - - public CreateTransferModel(MoneyMapContext db) - { - _db = db; - } - - [BindProperty] - [Required(ErrorMessage = "Please select a source account")] - public int FromAccountId { get; set; } - - [BindProperty] - [Required(ErrorMessage = "Please select a destination account")] - public int ToAccountId { get; set; } - - [BindProperty] - [Required] - public DateTime Date { get; set; } = DateTime.Today; - - [BindProperty] - [Required] - [Range(0.01, double.MaxValue, ErrorMessage = "Amount must be greater than 0")] - public decimal Amount { get; set; } - - [BindProperty] - [MaxLength(500)] - public string Memo { get; set; } = string.Empty; - - [BindProperty] - [MaxLength(100)] - public string Category { get; set; } = string.Empty; - - public List Accounts { get; set; } = new(); - public List AvailableCategories { get; set; } = new(); - - [TempData] - public string? SuccessMessage { get; set; } - - [TempData] - public string? ErrorMessage { get; set; } - - public async Task OnGetAsync() - { - await LoadDataAsync(); - return Page(); - } - - public async Task OnPostAsync() - { - // Custom validation - if (FromAccountId == ToAccountId) - { - ModelState.AddModelError(string.Empty, "Source and destination accounts must be different"); - } - - if (!ModelState.IsValid) - { - await LoadDataAsync(); - return Page(); - } - - // Verify both accounts exist - var fromAccount = await _db.Accounts.FindAsync(FromAccountId); - var toAccount = await _db.Accounts.FindAsync(ToAccountId); - - if (fromAccount == null || toAccount == null) - { - ErrorMessage = "One or both accounts not found"; - await LoadDataAsync(); - return Page(); - } - - // Use "Transfer" as default category if not specified - var transferCategory = string.IsNullOrWhiteSpace(Category) ? "Transfer" : Category.Trim(); - var transferMemo = string.IsNullOrWhiteSpace(Memo) - ? $"Transfer to {toAccount.DisplayLabel}" - : Memo.Trim(); - - // Create the debit transaction (source account - money out) - var debitTransaction = new Transaction - { - Date = Date, - Name = $"Transfer to {toAccount.DisplayLabel}", - Memo = transferMemo, - Amount = -Amount, // Negative for debit - Category = transferCategory, - AccountId = FromAccountId, - TransferToAccountId = ToAccountId, - CardId = null // Transfers don't use cards - }; - - // Create the credit transaction (destination account - money in) - var creditTransaction = new Transaction - { - Date = Date, - Name = $"Transfer from {fromAccount.DisplayLabel}", - Memo = transferMemo, - Amount = Amount, // Positive for credit - Category = transferCategory, - AccountId = ToAccountId, - TransferToAccountId = FromAccountId, // Links back to source - CardId = null - }; - - _db.Transactions.Add(debitTransaction); - _db.Transactions.Add(creditTransaction); - - try - { - await _db.SaveChangesAsync(); - SuccessMessage = $"Transfer of {Amount:C} from {fromAccount.DisplayLabel} to {toAccount.DisplayLabel} created successfully!"; - return RedirectToPage("/Transactions"); - } - catch (Exception ex) - { - ErrorMessage = $"Failed to create transfer: {ex.Message}"; - await LoadDataAsync(); - return Page(); - } - } - - private async Task LoadDataAsync() - { - // Load accounts and order in memory by computed property - var accounts = await _db.Accounts.ToListAsync(); - Accounts = accounts.OrderBy(a => a.DisplayLabel).ToList(); - - AvailableCategories = await _db.Transactions - .Select(t => t.Category ?? "") - .Where(c => !string.IsNullOrWhiteSpace(c)) - .Distinct() - .OrderBy(c => c) - .ToListAsync(); - } - } -} diff --git a/MoneyMap/Pages/Shared/_Layout.cshtml b/MoneyMap/Pages/Shared/_Layout.cshtml index 2676960..a4e2278 100644 --- a/MoneyMap/Pages/Shared/_Layout.cshtml +++ b/MoneyMap/Pages/Shared/_Layout.cshtml @@ -41,7 +41,7 @@ Recategorize