Adding journal entries and database tables. Increased starting balances.

This commit is contained in:
2026-07-15 12:08:28 +02:00
parent 0de38ef2df
commit 656f545281
13 changed files with 498 additions and 34 deletions
@@ -1,14 +1,40 @@
namespace FinancialApi.Application;
public record Account(int Id, decimal Balance)
public record Account(int Id, decimal Balance, string AccountType)
{
public Account ApplyPosting(decimal amount, EntryType entryType)
{
if (amount <= 0)
{
throw new ArgumentException("Credit amount must be positive.");
}
var newBalance = Balance;
if (AccountType == "Revenue")
{
newBalance = entryType == EntryType.Credit ? newBalance + amount : newBalance - amount;
}
else
{
newBalance = entryType == EntryType.Debit ? newBalance - amount : newBalance + amount;
}
if (newBalance < 0 && AccountType == "Liability")
{
throw new InvalidOperationException("Insufficient funds.");
}
return this with { Balance = newBalance };
}
public Account ApplyFee(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Fee must be positive.");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Insufficient funds.");
@@ -16,18 +42,19 @@ public record Account(int Id, decimal Balance)
return this with { Balance = Balance - amount };
}
public Account Debit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Debit amount must be positive.");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Insufficient funds.");
}
return this with { Balance = Balance - amount };
}
@@ -37,7 +64,7 @@ public record Account(int Id, decimal Balance)
{
throw new ArgumentException("Credit amount must be positive.");
}
return this with { Balance = Balance + amount };
}
}
@@ -0,0 +1,23 @@
namespace FinancialApi.Application;
public record BalancedJournal
{
public JournalEntry Entry { get; }
private BalancedJournal(JournalEntry entry) => Entry = entry;
public static BalancedJournal Create(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
{
var debits = lines.Where(l => l.Type == EntryType.Debit).Sum(l => l.Amount);
var credits = lines.Where(l => l.Type == EntryType.Credit).Sum(l => l.Amount);
if (debits != credits)
{
throw new InvalidOperationException(
$"GAAP Violation: Total Debits ({debits}) must equal Total Credits ({credits})!"
);
}
return new(new JournalEntry(id, description, createdAt, lines));
}
}
@@ -0,0 +1,7 @@
namespace FinancialApi.Application;
public enum EntryType
{
Debit,
Credit
}
@@ -0,0 +1,21 @@
namespace FinancialApi.Application;
public record JournalEntry
{
public Guid Id { get; init; }
public string Description { get; init; }
public DateTimeOffset CreatedAt { get; init; }
public List<JournalLine> Lines { get; init; } = [];
private JournalEntry()
{
}
internal JournalEntry(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
{
Lines = lines;
Id = id;
Description = description;
CreatedAt = createdAt;
}
}
@@ -0,0 +1,3 @@
namespace FinancialApi.Application;
public record JournalLine(int AccountId, decimal Amount, EntryType Type);